Add safety checks on all hostList traversals
[openafs.git] / src / viced / host.c
index 8193a68..01ebee9 100644 (file)
@@ -5,16 +5,17 @@
  * 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
+ *
+ * Portions Copyright (c) 2006 Sine Nomine Associates
  */
 
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
 #include <stdio.h>
 #include <errno.h>
+#include <string.h>
 #ifdef AFS_NT40_ENV
 #include <fcntl.h>
 #include <winsock2.h>
@@ -24,20 +25,14 @@ RCSID
 #include <netinet/in.h>
 #endif
 
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-
 #include <afs/stds.h>
 #include <rx/xdr.h>
 #include <afs/assert.h>
 #include <lwp.h>
 #include <lock.h>
 #include <afs/afsint.h>
+#define FSINT_COMMON_XG
+#include <afs/afscbint.h>
 #include <afs/rxgen_consts.h>
 #include <afs/nfs.h>
 #include <afs/errors.h>
@@ -49,16 +44,22 @@ RCSID
 #endif
 #include <afs/acl.h>
 #include <afs/ptclient.h>
+#include <afs/ptuser.h>
 #include <afs/prs_fs.h>
 #include <afs/auth.h>
 #include <afs/afsutil.h>
+#include <afs/com_err.h>
 #include <rx/rx.h>
 #include <afs/cellconfig.h>
 #include <stdlib.h>
 #include "viced_prototypes.h"
 #include "viced.h"
 #include "host.h"
-
+#include "callback.h"
+#ifdef AFS_DEMAND_ATTACH_FS
+#include "../util/afsutil_prototypes.h"
+#include "../tviced/serialize_state.h"
+#endif /* AFS_DEMAND_ATTACH_FS */
 
 #ifdef AFS_PTHREAD_ENV
 pthread_mutex_t host_glock_mutex;
@@ -74,6 +75,7 @@ extern struct afsconf_dir *confDir;   /* config dir object */
 extern int lwps;               /* the max number of server threads */
 extern afsUUID FS_HostUUID;
 
+afsUUID nulluuid;
 int CEs = 0;                   /* active clients */
 int CEBlocks = 0;              /* number of blocks of CEs */
 struct client *CEFree = 0;     /* first free client */
@@ -82,12 +84,17 @@ int hostCount = 0;          /* number of hosts in hostList */
 int rxcon_ident_key;
 int rxcon_client_key;
 
+static struct rx_securityClass *sc = NULL;
+
+static void h_SetupCallbackConn_r(struct host * host);
+static int h_threadquota(int);
+
 #define CESPERBLOCK 73
 struct CEBlock {               /* block of CESPERBLOCK file entries */
     struct client entry[CESPERBLOCK];
 };
 
-static void h_TossStuff_r(register struct host *host);
+void h_TossStuff_r(register struct host *host);
 
 /*
  * Make sure the subnet macros have been defined.
@@ -108,20 +115,10 @@ static void h_TossStuff_r(register struct host *host);
 #define        IN_CLASSB_SUBNET        0xffffff00
 #endif
 
-#define rxr_GetEpoch(aconn) (((struct rx_connection *)(aconn))->epoch)
-
-#define rxr_CidOf(aconn) (((struct rx_connection *)(aconn))->cid)
-
-#define rxr_PortOf(aconn) \
-    rx_PortOf(rx_PeerOf(((struct rx_connection *)(aconn))))
-
-#define rxr_HostOf(aconn) \
-    rx_HostOf(rx_PeerOf((struct rx_connection *)(aconn)))
-
 
 /* get a new block of CEs and chain it on CEFree */
 static void
-GetCEBlock()
+GetCEBlock(void)
 {
     register struct CEBlock *block;
     register int i;
@@ -146,7 +143,7 @@ GetCEBlock()
 
 /* get the next available CE */
 static struct client *
-GetCE()
+GetCE(void)
 {
     register struct client *entry;
 
@@ -160,7 +157,7 @@ GetCE()
     entry = CEFree;
     CEFree = entry->next;
     CEs++;
-    memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
+    memset(entry, 0, CLIENT_TO_ZERO(entry));
     return (entry);
 
 }                              /*GetCE */
@@ -170,6 +167,8 @@ GetCE()
 static void
 FreeCE(register struct client *entry)
 {
+    entry->VenusEpoch = 0;
+    entry->sid = 0;
     entry->next = CEFree;
     CEFree = entry;
     CEs--;
@@ -189,9 +188,9 @@ static struct host *HTFree = 0;     /* first free file entry */
  * to map IP addresses onto host pointers, and another
  * to map host UUIDs onto host pointers.
  */
-static struct h_hashChain *hostHashTable[h_HASHENTRIES];
-static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
-#define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
+static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
+static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
+#define h_HashIndex(hostip) (ntohl(hostip) & (h_HASHENTRIES-1))
 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
 
 struct HTBlock {               /* block of HTSPERBLOCK file entries */
@@ -201,12 +200,17 @@ struct HTBlock {          /* block of HTSPERBLOCK file entries */
 
 /* get a new block of HTs and chain it on HTFree */
 static void
-GetHTBlock()
+GetHTBlock(void)
 {
     register struct HTBlock *block;
     register int i;
     static int index = 0;
 
+    if (HTBlocks == h_MAXHOSTTABLES) {
+       ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
+       ShutDownAndCore(PANIC);
+    }
+
     block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
     if (!block) {
        ViceLog(0, ("Failed malloc in GetHTBlock\n"));
@@ -231,17 +235,17 @@ GetHTBlock()
 
 /* get the next available HT */
 static struct host *
-GetHT()
+GetHT(void)
 {
     register struct host *entry;
 
-    if (HTFree == 0)
+    if (HTFree == NULL)
        GetHTBlock();
-    assert(HTFree != 0);
+    assert(HTFree != NULL);
     entry = HTFree;
     HTFree = entry->next;
     HTs++;
-    memset((char *)entry, 0, HOST_TO_ZERO(entry));
+    memset(entry, 0, HOST_TO_ZERO(entry));
     return (entry);
 
 }                              /*GetHT */
@@ -257,59 +261,235 @@ FreeHT(register struct host *entry)
 
 }                              /*FreeHT */
 
+afs_int32
+hpr_Initialize(struct ubik_client **uclient)
+{
+    afs_int32 code;
+    struct rx_connection *serverconns[MAXSERVERS];
+    struct rx_securityClass *sc[3];
+    struct afsconf_dir *tdir;
+    struct ktc_token ttoken;
+    afs_int32 scIndex;
+    struct afsconf_cell info;
+    afs_int32 i;
+    char cellstr[64];
+
+    tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
+    if (!tdir) {
+       ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
+       return -1;
+    }
+    
+    code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
+    if (code) {
+       ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]", code));
+       afsconf_Close(tdir);
+       return code;
+    }
+    
+    code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
+    if (code) {
+       ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s", cellstr, confDir, AFSDIR_CELLSERVDB_FILE));
+       afsconf_Close(tdir);
+       return code;
+    }
+    
+    code = rx_Init(0);
+    if (code) {
+       ViceLog(0, ("hpr_Initialize: Could not initialize rx."));
+       afsconf_Close(tdir);
+        return code;
+    }
+    
+    scIndex = 2;
+    sc[0] = 0;
+    sc[1] = 0;
+    sc[2] = 0;
+    /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
+     * to force use of the KeyFile.  secLevel == 0 implies -noauth was
+     * specified. */
+    if ((afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
+        code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
+        if (code)
+           ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s (so trying noauth)", code, afs_error_message(code)));
+        if (code)
+            scIndex = 0;        /* use noauth */
+        if (scIndex != 2)
+            /* if there was a problem, an unauthenticated conn is returned */
+            sc[scIndex] = sc[2];
+    } else {
+        struct ktc_principal sname;
+        strcpy(sname.cell, info.name);
+        sname.instance[0] = 0;
+        strcpy(sname.name, "afs");
+        code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
+        if (code)
+            scIndex = 0;
+        else {
+            if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
+                /* this is a kerberos ticket, set scIndex accordingly */
+                scIndex = 2;
+            else {
+                ViceLog(0, ("hpr_Initialize: funny kvno (%d) in ticket, proceeding", ttoken.kvno));
+                scIndex = 2;
+            }
+            sc[2] =
+                rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
+                                              ttoken.kvno, ttoken.ticketLen,
+                                              ttoken.ticket);
+        }
+    }
+    if ((scIndex == 0) && (sc[0] == 0))
+        sc[0] = rxnull_NewClientSecurityObject();
+    if ((scIndex == 0))
+       ViceLog(0, ("hpr_Initialize: Could not get afs tokens, running unauthenticated. [%d]", code));
+    
+    memset(serverconns, 0, sizeof(serverconns));        /* terminate list!!! */
+    for (i = 0; i < info.numServers; i++) {
+        serverconns[i] =
+            rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
+                             info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
+                             scIndex);
+    }
 
-static short consolePort = 0;
+    code = ubik_ClientInit(serverconns, uclient);
+    if (code) {
+       ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
+    }
+    afsconf_Close(tdir);
+    code = rxs_Release(sc[scIndex]);
+    return code;
+}
 
 int
-h_Release(register struct host *host)
+hpr_End(struct ubik_client *uclient)
 {
-    H_LOCK;
-    h_Release_r(host);
-    H_UNLOCK;
+    int code = 0;
+
+    if (uclient) {
+        code = ubik_ClientDestroy(uclient);
+    }
+    return code;
+}
+
+int
+hpr_GetHostCPS(afs_int32 host, prlist *CPS)
+{
+#ifdef AFS_PTHREAD_ENV
+    register afs_int32 code;
+    afs_int32 over;
+    struct ubik_client *uclient = 
+       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+
+    if (!uclient) {
+        code = hpr_Initialize(&uclient);
+       if (!code) 
+           assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       else
+           return code;
+    }
+
+    over = 0;
+    code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
+    if (code != PRSUCCESS)
+        return code;
+    if (over) {
+      /* do something about this, probably make a new call */
+      /* don't forget there's a hard limit in the interface */
+        fprintf(stderr,
+                "membership list for host id %d exceeds display limit\n",
+                host);
+    }
     return 0;
+#else
+    return pr_GetHostCPS(host, CPS);
+#endif
 }
 
-/**
- * If this thread does not have a hold on this host AND
- * if other threads also dont have any holds on this host AND
- * If either the HOSTDELETED or CLIENTDELETED flags are set
- * then toss the host
- */
 int
-h_Release_r(register struct host *host)
+hpr_NameToId(namelist *names, idlist *ids)
 {
+#ifdef AFS_PTHREAD_ENV
+    register afs_int32 code;
+    register afs_int32 i;
+    struct ubik_client *uclient = 
+       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+
+    if (!uclient) {
+        code = hpr_Initialize(&uclient);
+       if (!code)
+           assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       else
+           return code;
+    }
 
-    if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
-       if (!h_OtherHolds_r(host)) {
-           /* must avoid masking this until after h_OtherHolds_r runs
-            * but it should be run before h_TossStuff_r */
-           (host)->holds[h_holdSlot()] &= ~h_holdbit();
-           if ((host->hostFlags & HOSTDELETED)
-               || (host->hostFlags & CLIENTDELETED)) {
-               h_TossStuff_r(host);
-           }
-       } else
-           (host)->holds[h_holdSlot()] &= ~h_holdbit();
-    } else
-       (host)->holds[h_holdSlot()] &= ~h_holdbit();
+    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
+}
 
-    return 0;
+int
+hpr_IdToName(idlist *ids, namelist *names)
+{
+#ifdef AFS_PTHREAD_ENV
+    register afs_int32 code;
+    struct ubik_client *uclient = 
+       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+    
+    if (!uclient) {
+        code = hpr_Initialize(&uclient);
+       if (!code)
+           assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       else
+           return code;
+    }
+
+    code = ubik_PR_IDToName(uclient, 0, ids, names);
+    return code;
+#else
+    return pr_IdToName(ids, names);
+#endif
 }
 
 int
-h_OtherHolds_r(register struct host *host)
+hpr_GetCPS(afs_int32 id, prlist *CPS)
 {
-    register int i, bit, slot;
-    bit = h_holdbit();
-    slot = h_holdSlot();
-    for (i = 0; i < h_maxSlots; i++) {
-       if (host->holds[i] != ((i == slot) ? bit : 0)) {
-           return 1;
-       }
+#ifdef AFS_PTHREAD_ENV
+    register afs_int32 code;
+    afs_int32 over;
+    struct ubik_client *uclient = 
+       (struct ubik_client *)pthread_getspecific(viced_uclient_key);
+
+    if (!uclient) {
+        code = hpr_Initialize(&uclient);
+       if (!code)
+           assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
+       else
+           return code;
+    }
+
+    over = 0;
+    code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
+    if (code != PRSUCCESS)
+        return code;
+    if (over) {
+      /* do something about this, probably make a new call */
+      /* don't forget there's a hard limit in the interface */
+        fprintf(stderr, "membership list for id %d exceeds display limit\n",
+                id);
     }
     return 0;
+#else
+    return pr_GetCPS(id, CPS);
+#endif
 }
 
+static short consolePort = 0;
+
 int
 h_Lock_r(register struct host *host)
 {
@@ -451,12 +631,13 @@ h_gethostcps_r(register struct host *host, register afs_int32 now)
        free(host->hcps.prlist_val);    /* this is for hostaclRefresh */
     host->hcps.prlist_val = NULL;
     host->hcps.prlist_len = 0;
-    slept ? (host->cpsCall = FT_ApproxTime()) : (host->cpsCall = now);
+    host->cpsCall = slept ? (FT_ApproxTime()) : (now);
 
     H_UNLOCK;
-    code = pr_GetHostCPS(htonl(host->host), &host->hcps);
+    code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
     H_LOCK;
     if (code) {
+        char hoststr[16];
        /*
         * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
         * and reevaluates things if no sync server or quorum is found we could still end up
@@ -478,13 +659,13 @@ h_gethostcps_r(register struct host *host, register afs_int32 now)
             */
            host->hcpsfailed = 1;
            ViceLog(0,
-                   ("Warning:  GetHostCPS failed (%d) for %x; will retry\n",
-                    code, host->host));
+                   ("Warning:  GetHostCPS failed (%d) for %x (%s:%d); will retry\n",
+                    code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
        } else {
            host->hcpsfailed = 0;
            ViceLog(1,
-                   ("gethost:  GetHostCPS failed (%d) for %x; ignored\n",
-                    code, host->host));
+                   ("gethost:  GetHostCPS failed (%d) for %x (%s:%d); ignored\n",
+                    code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
        }
        if (host->hcps.prlist_val)
            free(host->hcps.prlist_val);
@@ -508,17 +689,15 @@ h_gethostcps_r(register struct host *host, register afs_int32 now)
 
 /* args in net byte order */
 void
-h_flushhostcps(register afs_uint32 hostaddr, register afs_uint32 hport)
+h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
 {
-    register struct host *host;
-    int held = 0;
+    struct host *host;
 
     H_LOCK;
-    host = h_Lookup_r(hostaddr, hport, &held);
+    h_Lookup_r(hostaddr, hport, &host);
     if (host) {
        host->hcpsfailed = 1;
-       if (!held)
-           h_Release_r(host);
+       h_Release_r(host);
     }
     H_UNLOCK;
     return;
@@ -535,29 +714,19 @@ struct host *
 h_Alloc_r(register struct rx_connection *r_con)
 {
     struct servent *serverentry;
-    register index = h_HashIndex(rxr_HostOf(r_con));
-    register struct host *host;
-    static struct rx_securityClass *sc = 0;
+    struct host *host;
     afs_int32 now;
-    struct h_hashChain *h_hashChain;
 #if FS_STATS_DETAILED
     afs_uint32 newHostAddr_HBO;        /*New host IP addr, in host byte order */
 #endif /* FS_STATS_DETAILED */
 
     host = GetHT();
 
-    h_hashChain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
-    if (!h_hashChain) {
-       ViceLog(0, ("Failed malloc in h_Alloc_r\n"));
-       assert(0);
-    }
-    h_hashChain->hostPtr = host;
-    h_hashChain->addr = rxr_HostOf(r_con);
-    h_hashChain->next = hostHashTable[index];
-    hostHashTable[index] = h_hashChain;
-
     host->host = rxr_HostOf(r_con);
     host->port = rxr_PortOf(r_con);
+
+    h_AddHostToAddrHashTable_r(host->host, host->port, host);
+
     if (consolePort == 0) {    /* find the portal number for console */
 #if    defined(AFS_OSF_ENV)
        serverentry = getservbyname("ropcons", "");
@@ -573,24 +742,17 @@ h_Alloc_r(register struct rx_connection *r_con)
        host->Console = 1;
     /* 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. */
-    {
-       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);
     now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
     host->hostFlags = 0;
     host->hcps.prlist_val = NULL;
     host->hcps.prlist_len = 0;
-    host->interface = 0;
+    host->interface = NULL;
 #ifdef undef
     host->hcpsfailed = 0;      /* save cycles */
     h_gethostcps(host);                /* do this under host hold/lock */
 #endif
-    host->FirstClient = 0;
+    host->FirstClient = NULL;
     h_Hold_r(host);
     h_Lock_r(host);
     h_InsertList_r(host);      /* update global host List */
@@ -608,33 +770,52 @@ h_Alloc_r(register struct rx_connection *r_con)
 }                              /*h_Alloc_r */
 
 
-/* Lookup a host given an IP address and UDP port number. */
-/* hostaddr and hport are in network order */
-/* Note: host should be released by caller if 0 == *heldp and non-null */
-/* hostaddr and hport are in network order */
-struct host *
-h_Lookup_r(afs_uint32 hostaddr, afs_uint32 hport, int *heldp)
+
+/* 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. */
+static void
+h_SetupCallbackConn_r(struct host * host)
+{
+    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_Lookup_r
+ * Lookup a host given an IP address and UDP port number.
+ * hostaddr and hport are in network order
+ * hostaddr and hport are in network order
+ * On return, refCount is incremented.
+ */
+int
+h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
 {
-    register afs_int32 now;
-    register struct host *host = 0;
-    register struct h_hashChain *chain;
-    register index = h_HashIndex(hostaddr);
+    afs_int32 now;
+    struct host *host = NULL;
+    struct h_AddrHashChain *chain;
+    int index = h_HashIndex(haddr);
     extern int hostaclRefresh;
 
   restart:
-    for (chain = hostHashTable[index]; chain; chain = chain->next) {
+    for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
        assert(host);
-       if (!(host->hostFlags & HOSTDELETED) && chain->addr == hostaddr
-           && host->port == hport) {
-           *heldp = h_Held_r(host);
-           if (!*heldp)
-               h_Hold_r(host);
+       if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
+           && chain->port == hport) {
+           if ((host->hostFlags & HWHO_INPROGRESS) && 
+               h_threadquota(host->lock.num_waiting)) {
+               *hostp = 0;
+               return VBUSY;
+           }
+           h_Hold_r(host);
            h_Lock_r(host);
            if (host->hostFlags & HOSTDELETED) {
                h_Unlock_r(host);
-               if (!*heldp)
-                   h_Release_r(host);
+               h_Release_r(host);
+               host = NULL;
                goto restart;
            }
            h_Unlock_r(host);
@@ -646,7 +827,7 @@ h_Lookup_r(afs_uint32 hostaddr, afs_uint32 hport, int *heldp)
                 * first time that the host is added to a group.  Also
                 * here we also retry on previous legitimate hcps failures.
                 *
-                * If we get here we still have a host hold.
+                * If we get here refCount is elevated.
                 */
                h_gethostcps_r(host, now);
            }
@@ -654,61 +835,50 @@ h_Lookup_r(afs_uint32 hostaddr, afs_uint32 hport, int *heldp)
        }
        host = NULL;
     }
-    return host;
-
+    *hostp = host;
+    return 0;
 }                              /*h_Lookup */
 
 /* Lookup a host given its UUID. */
 struct host *
 h_LookupUuid_r(afsUUID * uuidp)
 {
-    register struct host *host = 0;
-    register struct h_hashChain *chain;
-    register index = h_UuidHashIndex(uuidp);
+    struct host *host = 0;
+    struct h_UuidHashChain *chain;
+    int index = h_UuidHashIndex(uuidp);
 
     for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
        assert(host);
        if (!(host->hostFlags & HOSTDELETED) && host->interface
            && afs_uuid_equal(&host->interface->uuid, uuidp)) {
-           break;
+            return host;
        }
-       host = NULL;
     }
-    return host;
-
+    return NULL;
 }                              /*h_Lookup */
 
 
-/*
- * h_Hold_r: Establish a hold by the current LWP on this host--the host
- * or its clients will not be physically deleted until all holds have
- * been released.
- * NOTE: h_Hold_r is a macro defined in host.h.
- */
-
 /* h_TossStuff_r:  Toss anything in the host structure (the host or
  * clients marked for deletion.  Called from h_Release_r ONLY.
  * To be called, there must be no holds, and either host->deleted
  * or host->clientDeleted must be set.
  */
-static void
+void
 h_TossStuff_r(register struct host *host)
 {
     register struct client **cp, *client;
-    int i;
 
     /* if somebody still has this host held */
-    for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
-    if (i != h_maxSlots)
+    if (host->refCount > 0)
        return;
 
     /* if somebody still has this host locked */
     if (h_NBLock_r(host) != 0) {
        char hoststr[16];
        ViceLog(0,
-               ("Warning:  h_TossStuff_r failed; Host %s:%d was locked.\n",
-                afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+               ("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)));
        return;
     } else {
        h_Unlock_r(host);
@@ -717,23 +887,34 @@ h_TossStuff_r(register struct host *host)
     /* ASSUMPTION: rxi_FreeConnection() does not yield */
     for (cp = &host->FirstClient; (client = *cp);) {
        if ((host->hostFlags & HOSTDELETED) || client->deleted) {
+           int code;
+           ObtainWriteLockNoBlock(&client->lock, code);
+           if (code < 0) {
+               char hoststr[16];
+               ViceLog(0,
+                       ("Warning: h_TossStuff_r failed: Host %" AFS_PTR_FMT " (%s:%d) client %x was locked.\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
+                        ntohs(host->port), client));
+               return;
+           }
+                
            if (client->refCount) {
                char hoststr[16];
                ViceLog(0,
-                       ("Warning: Host %s:%d client %x refcount %d while deleting.\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
+                       ("Warning: h_TossStuff_r failed: Host %" AFS_PTR_FMT " (%s:%d) client %x refcount %d.\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port), client, client->refCount));
-           }
-           if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val) {
-               free(client->CPS.prlist_val);
-               client->CPS.prlist_val = NULL;
+               /* This is the same thing we do if the host is locked */
+               ReleaseWriteLock(&client->lock);
+               return;
            }
            client->CPS.prlist_len = 0;
-           if (client->tcon) {
-               rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
-           }
+           if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
+               free(client->CPS.prlist_val);
+           client->CPS.prlist_val = NULL;
            CurrentConnections--;
            *cp = client->next;
+           ReleaseWriteLock(&client->lock);
            FreeCE(client);
        } else
            cp = &client->next;
@@ -743,26 +924,14 @@ h_TossStuff_r(register struct host *host)
     host->hostFlags &= ~CLIENTDELETED;
 
     if (host->hostFlags & HOSTDELETED) {
-       register struct h_hashChain **hp, *th;
        register struct rx_connection *rxconn;
-       afsUUID *uuidp;
-       afs_uint32 hostAddr;
+       struct AddrPort hostAddrPort;
        int i;
 
        if (host->Console & 1)
            Console--;
        if ((rxconn = host->callback_rxcon)) {
            host->callback_rxcon = (struct rx_connection *)0;
-           /*
-            * If rx_DestroyConnection calls h_FreeConnection we will
-            * deadlock on the host_glock_mutex. Work around the problem
-            * by unhooking the client from the connection before
-            * destroying the connection.
-            */
-           client = rx_GetSpecific(rxconn, rxcon_client_key);
-           if (client && client->tcon == rxconn)
-               client->tcon = NULL;
-           rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
            rx_DestroyConnection(rxconn);
        }
        if (host->hcps.prlist_val)
@@ -774,81 +943,51 @@ h_TossStuff_r(register struct host *host)
 
        /* if alternate addresses do not exist */
        if (!(host->interface)) {
-           for (hp = &hostHashTable[h_HashIndex(host->host)]; (th = *hp);
-                hp = &th->next) {
-               assert(th->hostPtr);
-               if (th->hostPtr == host) {
-                   *hp = th->next;
-                   h_DeleteList_r(host);
-                   FreeHT(host);
-                   free(th);
-                   break;
-               }
-           }
+           h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
        } else {
-           /* delete all hash entries for the UUID */
-           uuidp = &host->interface->uuid;
-           for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (th = *hp);
-                hp = &th->next) {
-               assert(th->hostPtr);
-               if (th->hostPtr == host) {
-                   *hp = th->next;
-                   free(th);
-                   break;
-               }
-           }
-           /* delete all hash entries for alternate addresses */
-           assert(host->interface->numberOfInterfaces > 0);
+            h_DeleteHostFromUuidHashTable_r(host);
+           h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
+           /* delete the hash entry for each valid alternate addresses */
            for (i = 0; i < host->interface->numberOfInterfaces; i++) {
-               hostAddr = host->interface->addr[i];
-               for (hp = &hostHashTable[h_HashIndex(hostAddr)]; (th = *hp);
-                    hp = &th->next) {
-                   assert(th->hostPtr);
-                   if (th->hostPtr == host) {
-                       *hp = th->next;
-                       free(th);
-                       break;
-                   }
-               }
+               hostAddrPort = host->interface->interface[i];
+                /* 
+                 * if the interface addr/port is the primary, we already
+                 * removed it.  If the addr/port is not valid, its not
+                 * in the hash table.
+                 */
+                if (hostAddrPort.valid &&
+                    (host->host != hostAddrPort.addr ||
+                     host->port != hostAddrPort.port))
+                    h_DeleteHostFromAddrHashTable_r(hostAddrPort.addr, hostAddrPort.port, host);
            }
            free(host->interface);
            host->interface = NULL;
-           h_DeleteList_r(host);       /* remove host from global host List */
-           FreeHT(host);
        }                       /* if alternate address exists */
+
+        h_DeleteList_r(host);  /* remove host from global host List */
+        FreeHT(host);
     }
 }                              /*h_TossStuff_r */
 
 
-/* Called by rx when a server connection disappears */
-int
-h_FreeConnection(struct rx_connection *tcon)
-{
-    register struct client *client;
-
-    client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
-    if (client) {
-       H_LOCK;
-       if (client->tcon == tcon)
-           client->tcon = (struct rx_connection *)0;
-       H_UNLOCK;
-    }
-    return 0;
-}                              /*h_FreeConnection */
-
 
 /* h_Enumerate: Calls (*proc)(host, held, 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.  (*proc) is always called with
- * host h_held().  The hold state of the host with respect to this lwp is passed
- * to (*proc) as the param held.  The proc should return 0 if the host should be
- * released, 1 if it should be held after enumeration.
+ * (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)?
+ *
+ * **The proc should return 0 if the host should be released, 1 if it should
+ * be held after enumeration.
  */
 void
-h_Enumerate(int (*proc) (), char *param)
+h_Enumerate(int (*proc) (struct host*, int, void *), void *param)
 {
     register struct host *host, **list;
-    register int *held;
+    register int *flags;
     register int i, count;
 
     H_LOCK;
@@ -858,216 +997,753 @@ h_Enumerate(int (*proc) (), char *param)
     }
     list = (struct host **)malloc(hostCount * sizeof(struct host *));
     if (!list) {
-       ViceLog(0, ("Failed malloc in h_Enumerate\n"));
+       ViceLog(0, ("Failed malloc in h_Enumerate (list)\n"));
        assert(0);
     }
-    held = (int *)malloc(hostCount * sizeof(int));
-    if (!held) {
-       ViceLog(0, ("Failed malloc in h_Enumerate\n"));
+    flags = (int *)malloc(hostCount * sizeof(int));
+    if (!flags) {
+       ViceLog(0, ("Failed malloc in h_Enumerate (flags)\n"));
        assert(0);
     }
-    for (count = 0, host = hostList; host; host = host->next, count++) {
+    for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
        list[count] = host;
-       if (!(held[count] = h_Held_r(host)))
-           h_Hold_r(host);
+       h_Hold_r(host);
+    }
+    if (count != hostCount) {
+       ViceLog(0, ("h_Enumerate found %d of %d hosts\n", count, hostCount));
+    } else if (host != NULL) {
+       ViceLog(0, ("h_Enumerate found more than %d hosts\n", hostCount));
+       ShutDownAndCore(PANIC);
     }
-    assert(count == hostCount);
     H_UNLOCK;
     for (i = 0; i < count; i++) {
-       held[i] = (*proc) (list[i], held[i], param);
-       if (!held[i])
-           h_Release(list[i]); /* this might free up the host */
+       flags[i] = (*proc) (list[i], flags[i], param);
+       h_Release_r(list[i]);
+       /* bail out of the enumeration early */
+       if (H_ENUMERATE_ISSET_BAIL(flags[i]))
+           break;
     }
     free((void *)list);
-    free((void *)held);
-}                              /*h_Enumerate */
+    free((void *)flags);
+}      /* h_Enumerate */
+
 
 /* h_Enumerate_r (revised):
- * Calls (*proc)(host, held, param) for each host in hostList, starting
- * at enumstart
- * Hosts may be deleted (have delete flag set); ditto for clients.
- * (*proc) is always called with
- * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
- * host with respect to this lwp is passed to (*proc) as the param held.
- * The proc should return 0 if the host should be released, 1 if it should
+ * Calls (*proc)(host, flags, 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)?
+ *
+ * **The proc should return 0 if the host should be released, 1 if it should
  * be held after enumeration.
  */
 void
-h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
+h_Enumerate_r(int (*proc) (struct host *, int, void *), 
+             struct host *enumstart, void *param)
 {
-    register struct host *host;
-    register int held;
+    register struct host *host, *next;
+    int flags = 0;
+    int nflags = 0;
+    int count;
 
     if (hostCount == 0) {
        return;
     }
-    for (host = enumstart; host; host = host->next) {
-       if (!(held = h_Held_r(host)))
-           h_Hold_r(host);
-       held = (*proc) (host, held, param);
-       if (!held)
-           h_Release_r(host);  /* this might free up the host */
+    h_Hold_r(enumstart);
+    for (count = 0, host = enumstart; host && count < hostCount; host = next, flags = nflags, count++) {
+       next = host->next;
+       if (next && !H_ENUMERATE_ISSET_BAIL(flags))
+           h_Hold_r(next);
+       flags = (*proc) (host, flags, param);
+       if (H_ENUMERATE_ISSET_BAIL(flags)) {
+           h_Release_r(host); /* this might free up the host */
+           break;
+       }
+       h_Release_r(host); /* this might free up the host */
+    }
+    if (host != NULL) {
+       ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", hostCount));
+       ShutDownAndCore(PANIC);
     }
-}                              /*h_Enumerate_r */
+}      /*h_Enumerate_r */
+
 
 /* inserts a new HashChain structure corresponding to this UUID */
 void
-hashInsertUuid_r(struct afsUUID *uuid, struct host *host)
+h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
 {
     int index;
-    struct h_hashChain *chain;
+    struct h_UuidHashChain *chain;
+    char uuid1[128], uuid2[128];
+    char hoststr[16];
 
     /* hash into proper bucket */
     index = h_UuidHashIndex(uuid);
 
+    /* don't add the same entry multiple times */
+    for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
+       if (!chain->hostPtr)
+           continue;
+
+       if (chain->hostPtr->interface && 
+           afs_uuid_equal(&chain->hostPtr->interface->uuid, uuid)) {
+           if (LogLevel >= 125) {
+               afsUUID_to_string(&chain->hostPtr->interface->uuid, uuid1, 
+                                 127);
+               afsUUID_to_string(uuid, uuid2, 127);
+               ViceLog(125, ("h_AddHostToUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s) exists as %s:%d (uuid %s)\n", 
+                             host, uuid1,
+                             afs_inet_ntoa_r(chain->hostPtr->host, hoststr), 
+                             ntohs(chain->hostPtr->port), uuid2));
+           }
+           return;
+       }
+    }
+
     /* insert into beginning of list for this bucket */
-    chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
+    chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
     if (!chain) {
-       ViceLog(0, ("Failed malloc in hashInsertUuid_r\n"));
+       ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
        assert(0);
     }
-    assert(chain);
     chain->hostPtr = host;
     chain->next = hostUuidHashTable[index];
     hostUuidHashTable[index] = chain;
+         if (LogLevel < 125)
+              return;
+     afsUUID_to_string(uuid, uuid2, 127);
+     ViceLog(125, 
+            ("h_AddHostToUuidHashTable_r: host %" AFS_PTR_FMT " (%s:%d) added as uuid %s\n",
+             host, afs_inet_ntoa_r(chain->hostPtr->host, hoststr), 
+             ntohs(chain->hostPtr->port), uuid));
 }
 
-/* Host is returned held */
-struct host *
-h_GetHost_r(struct rx_connection *tcon)
+/* deletes a HashChain structure corresponding to this host */
+int
+h_DeleteHostFromUuidHashTable_r(struct host *host)
 {
-    struct host *host;
-    struct host *oldHost;
-    int code;
-    int held;
-    struct interfaceAddr interf;
-    int interfValid = 0;
-    struct Identity *identP = NULL;
-    afs_int32 haddr;
-    afs_int32 hport;
-    char hoststr[16], hoststr2[16];
-    Capabilities caps;
-    struct rx_connection *cb_conn = NULL;
+     int index;
+     register struct h_UuidHashChain **uhp, *uth;
+     char uuid1[128];
+     char hoststr[16];
+     if (!host->interface)
+       return 0;
+     /* hash into proper bucket */
+     index = h_UuidHashIndex(&host->interface->uuid);
+     
+     if (LogLevel >= 125)
+        afsUUID_to_string(&host->interface->uuid, uuid1, 127);
+     for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
+         assert(uth->hostPtr);
+        if (uth->hostPtr == host) {
+            ViceLog(125, 
+                    ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d)\n",
+                     host, uuid1, afs_inet_ntoa_r(host->host, hoststr), 
+                     ntohs(host->port)));
+            *uhp = uth->next;
+            free(uth);
+            return 1;
+        }
+     }
+     ViceLog(125, 
+            ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d) not found\n",
+             host, uuid1, afs_inet_ntoa_r(host->host, hoststr), 
+             ntohs(host->port)));
+     return 0;
+}
 
-    caps.Capabilities_val = NULL;
+/* inserts a new HashChain structure corresponding to this address */
+void
+h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
+{
+    int index;
+    struct h_AddrHashChain *chain;
+    char hoststr[16], hoststr2[16];
 
-    haddr = rxr_HostOf(tcon);
-    hport = rxr_PortOf(tcon);
-  retry:
-    if (caps.Capabilities_val)
-       free(caps.Capabilities_val);
-    caps.Capabilities_val = NULL;
-    caps.Capabilities_len = 0;
+    /* hash into proper bucket */
+    index = h_HashIndex(addr);
 
-    code = 0;
-    host = h_Lookup_r(haddr, hport, &held);
-    identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
-    if (host && !identP && !(host->Console & 1)) {
-       /* This is a new connection, and we already have a host
-        * structure for this address. Verify that the identity
-        * of the caller matches the identity in the host structure.
-        */
-       h_Lock_r(host);
-       if (!(host->hostFlags & ALTADDR)) {
-           /* Another thread is doing initialization */
-           h_Unlock_r(host);
-           if (!held)
-               h_Release_r(host);
-           ViceLog(125,
-                   ("Host %s:%d starting h_Lookup again\n",
-                    afs_inet_ntoa_r(host->host, hoststr),
-                    ntohs(host->port)));
-           goto retry;
+    /* don't add the same entry multiple times */
+    for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
+       if (chain->hostPtr == host) {
+           if (chain->addr != addr || chain->port != port) {
+               ViceLog(0, 
+                       ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " exists as %s:%d when adding %s:%d\n",
+                        host, afs_inet_ntoa_r(chain->addr, hoststr), 
+                        ntohs(chain->port), afs_inet_ntoa_r(addr, hoststr2), 
+                        ntohs(port)));
+           } else
+               ViceLog(125, 
+                       ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) already hashed\n",
+                        host, afs_inet_ntoa_r(chain->addr, hoststr), 
+                        ntohs(chain->port)));
+           
+           return;
        }
-       host->hostFlags &= ~ALTADDR;
-       cb_conn = host->callback_rxcon;
-       rx_GetConnection(cb_conn);
-       H_UNLOCK;
-       code =
-           RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
-       if (code == RXGEN_OPCODE)
-           code = RXAFSCB_WhoAreYou(cb_conn, &interf);
-       rx_PutConnection(cb_conn);
-       cb_conn=NULL;
-       H_LOCK;
-       if (code == RXGEN_OPCODE) {
-           identP = (struct Identity *)malloc(sizeof(struct Identity));
-           if (!identP) {
-               ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
-               assert(0);
-           }
-           identP->valid = 0;
-           rx_SetSpecific(tcon, rxcon_ident_key, identP);
-           /* The host on this connection was unable to respond to 
-            * the WhoAreYou. We will treat this as a new connection
-            * from the existing host. The worst that can happen is
-            * that we maintain some extra callback state information */
-           if (host->interface) {
-               ViceLog(0,
-                       ("Host %s:%d used to support WhoAreYou, deleting.\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
-                        ntohs(host->port)));
-               host->hostFlags |= HOSTDELETED;
-               h_Unlock_r(host);
-               if (!held)
-                   h_Release_r(host);
-               host = NULL;
-               goto retry;
-           }
-       } else if (code == 0) {
-           interfValid = 1;
-           identP = (struct Identity *)malloc(sizeof(struct Identity));
-           if (!identP) {
-               ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
-               assert(0);
-           }
-           identP->valid = 1;
-           identP->uuid = interf.uuid;
-           rx_SetSpecific(tcon, rxcon_ident_key, identP);
-           /* Check whether the UUID on this connection matches
-            * the UUID in the host structure. If they don't match
-            * then this is not the same host as before. */
-           if (!host->interface
-               || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
-               ViceLog(25,
-                       ("Host %s:%d has changed its identity, deleting.\n",
-                        afs_inet_ntoa_r(host->host, hoststr), host->port));
-               host->hostFlags |= HOSTDELETED;
-               h_Unlock_r(host);
-               if (!held)
+    }
+
+    /* insert into beginning of list for this bucket */
+    chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
+    if (!chain) {
+       ViceLog(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
+       assert(0);
+    }
+    chain->hostPtr = host;
+    chain->next = hostAddrHashTable[index];
+    chain->addr = addr;
+    chain->port = port;
+    hostAddrHashTable[index] = chain;
+    ViceLog(125, ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " added as %s:%d\n",
+                 host, afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
+}
+
+/*
+ * This is called with host locked and held. 
+ * It is called to either validate or add an additional interface
+ * address/port on the specified host.  
+ *
+ * All addresses are in network byte order.
+ */
+int
+addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
+{
+    int i;
+    int number;
+    struct Interface *interface;
+    char hoststr[16], hoststr2[16];
+                                                   
+    assert(host);
+    assert(host->interface);
+
+    /*
+     * Make sure this address is on the list of known addresses
+     * for this host.
+     */
+    number = host->interface->numberOfInterfaces;
+    for (i = 0; i < number; i++) {
+       if (host->interface->interface[i].addr == addr &&
+             host->interface->interface[i].port == port) {
+           ViceLog(125, 
+                   ("addInterfaceAddr : found host %" AFS_PTR_FMT " (%s:%d) adding %s:%d%s\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr), 
+                    ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2), 
+                    ntohs(port), host->interface->interface[i].valid ? "" : 
+                    ", validating"));
+     
+           if (host->interface->interface[i].valid == 0) {
+               host->interface->interface[i].valid = 1;
+               h_AddHostToAddrHashTable_r(addr, port, host);
+           }
+           return 0;
+        }
+    }
+
+    ViceLog(125, ("addInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) adding %s:%d\n", 
+                 host, afs_inet_ntoa_r(host->host, hoststr), 
+                 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2), 
+                 ntohs(port)));
+    
+    interface = (struct Interface *)
+       malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
+    if (!interface) {
+       ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
+       assert(0);
+    }
+    interface->numberOfInterfaces = number + 1;
+    interface->uuid = host->interface->uuid;
+    for (i = 0; i < number; i++)
+       interface->interface[i] = host->interface->interface[i];
+    
+    /* Add the new valid interface */
+    interface->interface[number].addr = addr;
+    interface->interface[number].port = port;
+    interface->interface[number].valid = 1;
+    h_AddHostToAddrHashTable_r(addr, port, host);
+    free(host->interface);
+    host->interface = interface;
+    
+    return 0;
+}
+
+
+/*
+ * This is called with host locked and held.
+ *
+ * All addresses are in network byte order.
+ */
+int
+removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
+{
+    int i;
+    int number;
+    struct Interface *interface;
+    char hoststr[16], hoststr2[16];
+
+    assert(host);
+    assert(host->interface);
+
+    ViceLog(125, ("removeInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n", 
+                 host, afs_inet_ntoa_r(host->host, hoststr), 
+                 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2), 
+                 ntohs(port)));
+
+    /*
+     * Make sure this address is on the list of known addresses
+     * for this host.
+     */
+    interface = host->interface;
+    number = host->interface->numberOfInterfaces;
+    for (i = 0; i < number; i++) {
+       if (interface->interface[i].addr == addr &&
+           interface->interface[i].port == port) {
+           if (interface->interface[i].valid)
+               h_DeleteHostFromAddrHashTable_r(addr, port, host);
+           number--;
+           for (; i < number; i++) {
+               interface->interface[i] = interface->interface[i+1];
+           }
+           interface->numberOfInterfaces = number;
+           return 0;
+       }
+    }  
+    /* not found */
+    return 0;
+}
+
+/*
+ * This is called with host locked and held.
+ *
+ * All addresses are in network byte order.
+ */
+int
+invalidateInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
+{
+    int i;
+    int number;
+    struct Interface *interface;
+    char hoststr[16], hoststr2[16];
+    
+    assert(host);
+    assert(host->interface);
+    
+    ViceLog(125, ("invalidateInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n", 
+                 host, afs_inet_ntoa_r(host->host, hoststr), 
+                 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2), 
+                 ntohs(port)));
+    
+    /*
+     * Make sure this address is on the list of known addresses
+     * for this host.
+     */
+    interface = host->interface;
+    number = host->interface->numberOfInterfaces;
+    for (i = 0; i < number; i++) {
+       if (interface->interface[i].addr == addr &&
+           interface->interface[i].port == port) {
+            if (interface->interface[i].valid) {
+                h_DeleteHostFromAddrHashTable_r(addr, port, host);
+               interface->interface[i].valid = 0;
+           }
+           return 0;
+       }
+    }
+    
+    /* not found */
+    return 0;
+}
+
+/*
+ * This is called with host locked and held.  This function differs
+ * from removeInterfaceAddr_r in that it is called when the address
+ * is being removed from the host regardless of whether or not there
+ * is an interface list for the host.  This function will delete the
+ * host if there are no addresses left on it.
+ *
+ * All addresses are in network byte order.
+ */
+int
+removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
+{
+    int i;
+    char hoststr[16], hoststr2[16];
+    struct rx_connection *rxconn;
+
+    if (!host->interface || host->interface->numberOfInterfaces == 1) {
+        if (host->host == addr && host->port == port) {
+            ViceLog(25,
+                    ("Removing only address for host %" AFS_PTR_FMT " (%s:%d), deleting host.\n",
+                     host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+            host->hostFlags |= HOSTDELETED;
+            /* 
+             * Do not remove the primary addr/port from the hash table.
+             * It will be ignored due to the HOSTDELETED flag and will
+             * be removed when h_TossStuff_r() cleans up the HOSTDELETED
+             * host.  Removing it here will only result in a search for 
+             * the host/addr/port in the hash chain which will fail.
+             */
+        } else {
+            ViceLog(0,
+                    ("Removing address that does not belong to host %" AFS_PTR_FMT " (%s:%d).\n",
+                     host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+        }
+    } else {
+        if (host->host == addr && host->port == port)  {
+            removeInterfaceAddr_r(host, addr, port);
+
+            for (i=0; i < host->interface->numberOfInterfaces; i++) {
+                if (host->interface->interface[i].valid) {
+                    ViceLog(25,
+                             ("Removed address for host %" AFS_PTR_FMT " (%s:%d), new primary interface %s:%d.\n",
+                               host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
+                               afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2), 
+                               ntohs(host->interface->interface[i].port)));
+                    host->host = host->interface->interface[i].addr;
+                    host->port = host->interface->interface[i].port;
+                    h_AddHostToAddrHashTable_r(host->host, host->port, host);
+                    break;
+                }
+            }
+
+            if (i == host->interface->numberOfInterfaces) {
+                ViceLog(25,
+                         ("Removed only address for host %" AFS_PTR_FMT " (%s:%d), no valid alternate interfaces, deleting host.\n",
+                           host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+                host->hostFlags |= HOSTDELETED;
+                /* addr/port was removed from the hash table */
+                host->host = 0;
+                host->port = 0;
+            } else {
+                rxconn = host->callback_rxcon;
+                host->callback_rxcon = NULL;
+
+                if (rxconn) {
+                    rx_DestroyConnection(rxconn);
+                    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);
+            }
+        } else {
+            /* not the primary addr/port, just invalidate it */
+            invalidateInterfaceAddr_r(host, addr, port);
+        }
+    }
+
+    return 0;
+}
+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;
+    }
+    return 0;
+}
+
+/* If found, host is returned with refCount incremented */
+struct host *
+h_GetHost_r(struct rx_connection *tcon)
+{
+    struct host *host;
+    struct host *oldHost;
+    int code;
+    struct interfaceAddr interf;
+    int interfValid = 0;
+    struct Identity *identP = NULL;
+    afs_uint32 haddr;
+    afs_uint16 hport;
+    char hoststr[16], hoststr2[16];
+    Capabilities caps;
+    struct rx_connection *cb_conn = NULL;
+    struct rx_connection *cb_in = NULL;
+
+    caps.Capabilities_val = NULL;
+
+    haddr = rxr_HostOf(tcon);
+    hport = rxr_PortOf(tcon);
+  retry:
+    if (cb_in) {
+        rx_DestroyConnection(cb_in);
+        cb_in = NULL;
+    }
+    if (caps.Capabilities_val)
+       free(caps.Capabilities_val);
+    caps.Capabilities_val = NULL;
+    caps.Capabilities_len = 0;
+
+    code = 0;
+    if (h_Lookup_r(haddr, hport, &host))
+       return 0;
+    identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
+    if (host && !identP && !(host->Console & 1)) {
+       /* This is a new connection, and we already have a host
+        * structure for this address. Verify that the identity
+        * of the caller matches the identity in the host structure.
+        */
+       if ((host->hostFlags & HWHO_INPROGRESS) && 
+           h_threadquota(host->lock.num_waiting)) {
+               h_Release_r(host);
+           host = NULL;
+           goto gethost_out;
+       }
+       h_Lock_r(host);
+       if (!(host->hostFlags & ALTADDR) ||
+            (host->hostFlags & HOSTDELETED)) {
+           /* Another thread is doing initialization
+             * or this host was deleted while we
+             * waited for the lock. */
+           h_Unlock_r(host);
+           ViceLog(125,
+                   ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
+                    ntohs(host->port)));
+           h_Release_r(host);
+           goto retry;
+       }
+       host->hostFlags |= HWHO_INPROGRESS;
+       host->hostFlags &= ~ALTADDR;
+
+        /* We received a new connection from an IP address/port
+         * that is associated with 'host' but the address/port of
+         * the callback connection does not have to match it.
+         * If there is a match, we can use the existing callback
+         * connection to verify the UUID.  If they do not match
+         * we need to use a new callback connection to verify the
+         * UUID of the incoming caller and perhaps use the old 
+         * callback connection to verify that the old address/port
+         * is still valid.
+         */
+       
+       cb_conn = host->callback_rxcon;
+       rx_GetConnection(cb_conn);
+       H_UNLOCK;
+        if (haddr == host->host && hport == host->port) {
+            /* The existing callback connection matches the 
+             * incoming connection so just use it.
+             */
+           code =
+               RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
+           if (code == RXGEN_OPCODE)
+               code = RXAFSCB_WhoAreYou(cb_conn, &interf);
+       } else {
+            /* We do not have a match.  Create a new connection
+             * for the new addr/port and use multi_Rx to probe
+             * both of them simultaneously.
+             */
+           if (!sc)
+                sc = rxnull_NewClientSecurityObject();
+            cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
+            rx_SetConnDeadTime(cb_in, 50);
+            rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
+           
+            code =
+                RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
+           if (code == RXGEN_OPCODE)
+                code = RXAFSCB_WhoAreYou(cb_in, &interf);
+       }
+       rx_PutConnection(cb_conn);
+       cb_conn=NULL;
+       H_LOCK;
+       if ((code == RXGEN_OPCODE) || 
+           ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
+           identP = (struct Identity *)malloc(sizeof(struct Identity));
+           if (!identP) {
+               ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
+               assert(0);
+           }
+           identP->valid = 0;
+           rx_SetSpecific(tcon, rxcon_ident_key, identP);
+           if (cb_in == NULL) {
+               /* The host on this connection was unable to respond to 
+                * the WhoAreYou. We will treat this as a new connection
+                * from the existing host. The worst that can happen is
+                * that we maintain some extra callback state information */
+               if (host->interface) {
+                   ViceLog(0,
+                           ("Host %" AFS_PTR_FMT " (%s:%d) used to support WhoAreYou, deleting.\n",
+                            host, 
+                            afs_inet_ntoa_r(host->host, hoststr),
+                            ntohs(host->port)));
+                   host->hostFlags |= HOSTDELETED;
+                   host->hostFlags &= ~HWHO_INPROGRESS;
+                   h_Unlock_r(host);
                    h_Release_r(host);
+                   host = NULL;
+                   goto retry;
+               }
+           } else {
+               /* The incoming connection does not support WhoAreYou but
+                * the original one might have.  Use removeAddress_r() to
+                 * remove this addr/port from the host that was found.
+                 * If there are no more addresses left for the host it 
+                 * will be deleted.  Then we retry.
+                 */
+                removeAddress_r(host, haddr, hport);
+                host->hostFlags &= ~HWHO_INPROGRESS;
+                host->hostFlags |= ALTADDR;
+                h_Unlock_r(host);
+               h_Release_r(host);
+                host = NULL;
+                goto retry;
+           }
+       } else if (code == 0) {
+           interfValid = 1;
+           identP = (struct Identity *)malloc(sizeof(struct Identity));
+           if (!identP) {
+               ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
+               assert(0);
+           }
+           identP->valid = 1;
+           identP->uuid = interf.uuid;
+           rx_SetSpecific(tcon, rxcon_ident_key, identP);
+           /* Check whether the UUID on this connection matches
+            * the UUID in the host structure. If they don't match
+            * then this is not the same host as before. */
+           if (!host->interface
+               || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
+               if (cb_in) {
+                       ViceLog(25,
+                                       ("Uuid doesn't match connection (%s:%d).\n",
+                                        afs_inet_ntoa_r(haddr, hoststr), ntohs(hport)));
+                       removeAddress_r(host, haddr, hport);
+               } else {
+                   ViceLog(25,
+                           ("Uuid doesn't match host %" AFS_PTR_FMT " (%s:%d).\n",
+                            host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+                   
+                   removeAddress_r(host, host->host, host->port);
+               }
+               host->hostFlags &= ~HWHO_INPROGRESS;
+                host->hostFlags |= ALTADDR;
+               h_Unlock_r(host);
+               h_Release_r(host);
                host = NULL;
                goto retry;
+           } else if (cb_in) {
+               /* the UUID matched the client at the incoming addr/port 
+                 * but this is not the address of the active callback 
+                 * connection.  Try that connection and see if the client
+                 * is still there and if the reported UUID is the same.
+                 */
+                int code2;
+                afsUUID uuid = host->interface->uuid;
+                cb_conn = host->callback_rxcon;
+                rx_GetConnection(cb_conn);
+                rx_SetConnDeadTime(cb_conn, 2);
+                rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
+                H_UNLOCK;
+                code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
+                H_LOCK;
+                rx_SetConnDeadTime(cb_conn, 50);
+                rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
+                rx_PutConnection(cb_conn);
+                cb_conn=NULL;
+                if (code2) {
+                    /* The primary address is either not responding or
+                     * is not the client we are looking for.  Need to
+                     * remove the primary address and add swap in the new 
+                     * callback connection, and destroy the old one.
+                     */
+                    struct rx_connection *rxconn;
+                    ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
+                              host, 
+                              afs_inet_ntoa_r(host->host, hoststr),
+                              ntohs(host->port),code2));
+
+                    /* 
+                     * make sure we add and then remove.  otherwise, we
+                     * might end up with no valid interfaces after the 
+                     * remove and the host will have been marked deleted.
+                     */
+                    addInterfaceAddr_r(host, haddr, hport);
+                    removeInterfaceAddr_r(host, host->host, host->port);
+                    host->host = haddr;
+                    host->port = hport;
+                    rxconn = host->callback_rxcon;
+                    host->callback_rxcon = cb_in;
+                    cb_in = NULL;
+                   
+                    if (rxconn) {
+                        /*
+                         * If rx_DestroyConnection calls h_FreeConnection we
+                        * will deadlock on the host_glock_mutex. Work around
+                        * the problem by unhooking the client from the
+                        * connection before destroying the connection.
+                         */
+                        rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
+                        rx_DestroyConnection(rxconn);
+                   }
+               }
            }
        } else {
-           afs_inet_ntoa_r(host->host, hoststr);
-           ViceLog(0,
-                   ("CB: WhoAreYou failed for %s:%d, error %d\n", hoststr,
-                    ntohs(host->port), code));
-           host->hostFlags |= VENUSDOWN;
+            if (cb_in) {
+                /* A callback to the incoming connection address is failing.  
+                 * Assume that the addr/port is no longer associated with the host
+                 * returned by h_Lookup_r.
+                 */
+                ViceLog(0,
+                       ("CB: WhoAreYou failed for connection (%s:%d) , error %d\n",
+                        afs_inet_ntoa_r(haddr, hoststr), ntohs(hport), code));
+                removeAddress_r(host, haddr, hport);
+                host->hostFlags &= ~HWHO_INPROGRESS;
+                host->hostFlags |= ALTADDR;
+                h_Unlock_r(host);
+               h_Release_r(host);
+                host = NULL;
+                rx_DestroyConnection(cb_in);
+               cb_in = NULL;
+               goto gethost_out;
+           } else {
+               ViceLog(0,
+                       ("CB: WhoAreYou failed for host %" AFS_PTR_FMT " (%s:%d), error %d\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
+                        ntohs(host->port), code));
+               host->hostFlags |= VENUSDOWN;
+           }
        }
        if (caps.Capabilities_val
-           && (caps.Capabilities_val[0] & CAPABILITY_ERRORTRANS))
+           && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
            host->hostFlags |= HERRORTRANS;
        else
            host->hostFlags &= ~(HERRORTRANS);
        host->hostFlags |= ALTADDR;
+       host->hostFlags &= ~HWHO_INPROGRESS;
        h_Unlock_r(host);
     } else if (host) {
        if (!(host->hostFlags & ALTADDR)) {
            /* another thread is doing the initialisation */
            ViceLog(125,
-                   ("Host %s:%d waiting for host-init to complete\n",
-                    afs_inet_ntoa_r(host->host, hoststr),
+                   ("Host %" AFS_PTR_FMT " (%s:%d) waiting for host-init to complete\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
                     ntohs(host->port)));
            h_Lock_r(host);
            h_Unlock_r(host);
-           if (!held)
-               h_Release_r(host);
            ViceLog(125,
-                   ("Host %s:%d starting h_Lookup again\n",
-                    afs_inet_ntoa_r(host->host, hoststr),
+                   ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
                     ntohs(host->port)));
+           h_Release_r(host);
            goto retry;
        }
        /* We need to check whether the identity in the host structure
@@ -1085,17 +1761,17 @@ h_GetHost_r(struct rx_connection *tcon)
            if (host->interface)
                afsUUID_to_string(&host->interface->uuid, uuid2, 127);
            ViceLog(0,
-                   ("CB: new identity for host %s:%d, deleting(%x %x %s %s)\n",
-                    afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
+                   ("CB: new identity for host %" AFS_PTR_FMT " (%s:%d), deleting(%x %x %s %s)\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
                     identP->valid, host->interface,
-                    identP->valid ? uuid1 : "",
-                    host->interface ? uuid2 : ""));
+                    identP->valid ? uuid1 : "no_uuid",
+                    host->interface ? uuid2 : "no_uuid"));
 
            /* The host in the cache is not the host for this connection */
+            h_Lock_r(host);
            host->hostFlags |= HOSTDELETED;
            h_Unlock_r(host);
-           if (!held)
-               h_Release_r(host);
+           h_Release_r(host);
            goto retry;
        }
     } else {
@@ -1105,6 +1781,7 @@ h_GetHost_r(struct rx_connection *tcon)
            int pident = 0;
            cb_conn = host->callback_rxcon;
            rx_GetConnection(cb_conn);
+           host->hostFlags |= HWHO_INPROGRESS;
            H_UNLOCK;
            code =
                RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
@@ -1113,7 +1790,8 @@ h_GetHost_r(struct rx_connection *tcon)
            rx_PutConnection(cb_conn);
            cb_conn=NULL;
            H_LOCK;
-           if (code == RXGEN_OPCODE) {
+           if ((code == RXGEN_OPCODE) || 
+               ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
                if (!identP)
                    identP =
                        (struct Identity *)malloc(sizeof(struct Identity));
@@ -1128,8 +1806,8 @@ h_GetHost_r(struct rx_connection *tcon)
                if (!pident)
                    rx_SetSpecific(tcon, rxcon_ident_key, identP);
                ViceLog(25,
-                       ("Host %s:%d does not support WhoAreYou.\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
+                       ("Host %" AFS_PTR_FMT " (%s:%d) does not support WhoAreYou.\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port)));
                code = 0;
            } else if (code == 0) {
@@ -1149,8 +1827,8 @@ h_GetHost_r(struct rx_connection *tcon)
                if (!pident)
                    rx_SetSpecific(tcon, rxcon_ident_key, identP);
                ViceLog(25,
-                       ("WhoAreYou success on %s:%d\n",
-                        afs_inet_ntoa_r(host->host, hoststr),
+                       ("WhoAreYou success on host %" AFS_PTR_FMT " (%s:%d)\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port)));
            }
            if (code == 0 && !identP->valid) {
@@ -1163,27 +1841,122 @@ h_GetHost_r(struct rx_connection *tcon)
                H_LOCK;
            } else if (code == 0) {
                oldHost = h_LookupUuid_r(&identP->uuid);
+                if (oldHost) {
+                   h_Hold_r(oldHost);
+                   h_Lock_r(oldHost);
+
+                   if (oldHost->hostFlags & HOSTDELETED) {
+                       h_Unlock_r(oldHost);
+                       h_Release_r(oldHost);
+                       oldHost = NULL;
+                   }
+               }
+
                if (oldHost) {
+                   int probefail = 0;
+
+                   oldHost->hostFlags |= HWHO_INPROGRESS;
+
+                    if (oldHost->interface) {
+                       int code2;
+                       afsUUID uuid = oldHost->interface->uuid;
+                        cb_conn = oldHost->callback_rxcon;
+                        rx_GetConnection(cb_conn);
+                       rx_SetConnDeadTime(cb_conn, 2);
+                       rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
+                       H_UNLOCK;
+                       code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
+                       H_LOCK;
+                       rx_SetConnDeadTime(cb_conn, 50);
+                       rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
+                        rx_PutConnection(cb_conn);
+                        cb_conn=NULL;
+                       if (code2) {
+                           /* The primary address is either not responding or
+                            * is not the client we are looking for.  
+                            * MultiProbeAlternateAddress_r() will remove the
+                            * alternate interfaces that do not have the same
+                            * Uuid. */
+                           ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
+                                        oldHost, 
+                                         afs_inet_ntoa_r(oldHost->host, hoststr),
+                                        ntohs(oldHost->port),code2));
+                           MultiProbeAlternateAddress_r(oldHost);
+                            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. */
-                   if (!(held = h_Held_r(oldHost)))
-                       h_Hold_r(oldHost);
-                   h_Lock_r(oldHost);
-                   ViceLog(25,
-                           ("CB: new addr %s:%d for old host %s:%d\n",
-                            afs_inet_ntoa_r(host->host, hoststr),
-                            ntohs(host->port), afs_inet_ntoa_r(oldHost->host,
-                                                               hoststr2),
-                            ntohs(oldHost->port)));
+
+                    /* prevent warnings while manipulating interface lists */
                    host->hostFlags |= HOSTDELETED;
+
+                   if (oldHost->host != haddr || oldHost->port != hport) {
+                       struct rx_connection *rxconn;
+
+                       ViceLog(25,
+                                 ("CB: Host %" AFS_PTR_FMT " (%s:%d) has new addr %s:%d\n",
+                                   oldHost, 
+                                   afs_inet_ntoa_r(oldHost->host, hoststr2),
+                                   ntohs(oldHost->port),
+                                   afs_inet_ntoa_r(haddr, hoststr),
+                                   ntohs(hport)));
+                       /* 
+                        * add then remove.  otherwise the host may get marked
+                        * deleted if we removed the only valid address.
+                        */
+                       addInterfaceAddr_r(oldHost, haddr, hport);
+                       if (probefail || oldHost->host == haddr) {
+                           /* 
+                            * The probe failed which means that the old 
+                            * address is either unreachable or is not the 
+                            * same host we were just contacted by.  We will 
+                            * also remove addresses if only the port has 
+                            * changed because that indicates the client
+                            * is behind a NAT. 
+                            */
+                           removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
+                       } else {
+                           int i;
+                           struct Interface *interface = oldHost->interface;
+                           int number = oldHost->interface->numberOfInterfaces;
+                           for (i = 0; i < number; i++) {
+                               if (interface->interface[i].addr == haddr &&
+                                   interface->interface[i].port != hport) {
+                                   /* 
+                                    * We have just been contacted by a client
+                                    * that has been seen from behind a NAT 
+                                    * and at least one other address.
+                                    */
+                                   removeInterfaceAddr_r(oldHost, haddr, 
+                                                         interface->interface[i].port);
+                                   break;
+                               }
+                           }
+                       }
+                       h_AddHostToAddrHashTable_r(haddr, hport, oldHost);
+                       oldHost->host = haddr;
+                       oldHost->port = hport;
+                       rxconn = oldHost->callback_rxcon;
+                       oldHost->callback_rxcon = host->callback_rxcon;
+                       host->callback_rxcon = rxconn;
+                       
+                        /* don't destroy rxconn here; let h_TossStuff_r
+                         * take care of that via h_Release_r below */
+                   }
+                   host->hostFlags &= ~HWHO_INPROGRESS;
                    h_Unlock_r(host);
+                   /* release host because it was allocated by h_Alloc_r */
                    h_Release_r(host);
                    host = oldHost;
-                   addInterfaceAddr_r(host, haddr);
+                   /* the new host is held and locked */
                } else {
                    /* This really is a new host */
-                   hashInsertUuid_r(&identP->uuid, host);
+                   h_AddHostToUuidHashTable_r(&identP->uuid, host);
                    cb_conn = host->callback_rxcon;
                    rx_GetConnection(cb_conn);          
                    H_UNLOCK;
@@ -1195,8 +1968,8 @@ h_GetHost_r(struct rx_connection *tcon)
                    H_LOCK;
                    if (code == 0) {
                        ViceLog(25,
-                               ("InitCallBackState3 success on %s:%d\n",
-                                afs_inet_ntoa_r(host->host, hoststr),
+                               ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
+                                host, afs_inet_ntoa_r(host->host, hoststr),
                                 ntohs(host->port)));
                        assert(interfValid == 1);
                        initInterfaceAddr_r(host, &interf);
@@ -1204,47 +1977,71 @@ h_GetHost_r(struct rx_connection *tcon)
                }
            }
            if (code) {
-               afs_inet_ntoa_r(host->host, hoststr);
                ViceLog(0,
-                       ("CB: RCallBackConnectBack failed for %s:%d\n",
-                        hoststr, ntohs(host->port)));
+                       ("CB: RCallBackConnectBack failed for %" AFS_PTR_FMT " (%s:%d)\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
                host->hostFlags |= VENUSDOWN;
-           } else
+           } else {
+               ViceLog(125,
+                       ("CB: RCallBackConnectBack succeeded for %" AFS_PTR_FMT " (%s:%d)\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
                host->hostFlags |= RESETDONE;
-
+           }
        }
        if (caps.Capabilities_val
-           && (caps.Capabilities_val[0] & CAPABILITY_ERRORTRANS))
+           && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
            host->hostFlags |= HERRORTRANS;
        else
            host->hostFlags &= ~(HERRORTRANS);
-       host->hostFlags |= ALTADDR;     /* host structure iniatilisation complete */
+       host->hostFlags |= ALTADDR;     /* host structure initialization complete */
+       host->hostFlags &= ~HWHO_INPROGRESS;
        h_Unlock_r(host);
     }
+
+ gethost_out:
     if (caps.Capabilities_val)
        free(caps.Capabilities_val);
     caps.Capabilities_val = NULL;
     caps.Capabilities_len = 0;
+    if (cb_in) {
+        rx_DestroyConnection(cb_in);
+        cb_in = NULL;
+    }
     return host;
 
 }                              /*h_GetHost_r */
 
 
 static char localcellname[PR_MAXNAMELEN + 1];
-char local_realm[AFS_REALM_SZ] = "";
+char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
+int  num_lrealms = -1;
 
 /* not reentrant */
 void
-h_InitHostPackage()
+h_InitHostPackage(void)
 {
+    memset(&nulluuid, 0, sizeof(afsUUID));
     afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
-    if (!local_realm[0]) {
-       if (afs_krb_get_lrealm(local_realm, 0) != 0 /*KSUCCESS*/) {
+    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;
+       }
+
+       if (i == 0) {
            ViceLog(0,
                    ("afs_krb_get_lrealm failed, using %s.\n",
                     localcellname));
-           strcpy(local_realm, 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';
     }
     rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
     rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
@@ -1274,11 +2071,10 @@ MapName_r(char *aname, char *acell, afs_int32 * aval)
 
     cnamelen = strlen(acell);
     if (cnamelen) {
-       if (strcasecmp(local_realm, acell)
-           && strcasecmp(localcellname, acell)) {
+       if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
            ViceLog(2,
-                   ("MapName: cell is foreign.  cell=%s, localcell=%s, localrealm=%s\n",
-                    acell, localcellname, local_realm));
+                   ("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",
@@ -1300,7 +2096,7 @@ MapName_r(char *aname, char *acell, afs_int32 * aval)
     }
 
     H_UNLOCK;
-    code = pr_NameToId(&lnames, &lids);
+    code = hpr_NameToId(&lnames, &lids);
     H_LOCK;
     if (code == 0) {
        if (lids.idlist_val) {
@@ -1328,32 +2124,36 @@ MapName_r(char *aname, char *acell, afs_int32 * aval)
 /*MapName*/
 
 
-/* NOTE: this returns the client with a Shared lock */
+/* NOTE: this returns the client with a Write lock and a refCount */
 struct client *
 h_ID2Client(afs_int32 vid)
 {
     register struct client *client;
     register struct host *host;
+    int count;
 
     H_LOCK;
-    for (host = hostList; host; host = host->next) {
+    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;
-               ObtainSharedLock(&client->lock);
-               H_LOCK;
-               client->refCount--;
-               H_UNLOCK;
+               ObtainWriteLock(&client->lock);
                return client;
            }
        }
     }
+    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);
+    }
 
     H_UNLOCK;
-    return 0;
+    return NULL;
 }
 
 /*
@@ -1362,14 +2162,17 @@ h_ID2Client(afs_int32 vid)
  * isn't around. The client is returned with its reference count incremented
  * by one. The caller must call h_ReleaseClient_r when finished with
  * the client.
+ *
+ * The refCount on client->host is returned incremented.  h_ReleaseClient_r
+ * does not decrement the refCount on client->host.
  */
 struct client *
 h_FindClient_r(struct rx_connection *tcon)
 {
     register struct client *client;
-    register struct host *host;
+    struct host *host = NULL;
     struct client *oldClient;
-    afs_int32 viceid;
+    afs_int32 viceid = 0;
     afs_int32 expTime;
     afs_int32 code;
     int authClass;
@@ -1381,12 +2184,15 @@ h_FindClient_r(struct rx_connection *tcon)
     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->deleted) {
+    if (client && client->sid == rxr_CidOf(tcon) 
+       && client->VenusEpoch == rxr_GetEpoch(tcon)) {
        client->refCount++;
        h_Hold_r(client->host);
-       if (client->prfail != 2) {      /* Could add shared lock on client here */
+       if (!client->deleted && 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
             * until initialization is done, and we only get here if
@@ -1397,8 +2203,8 @@ h_FindClient_r(struct rx_connection *tcon)
        H_UNLOCK;
        ObtainWriteLock(&client->lock); /* released at end */
        H_LOCK;
-    } else if (client) {
-       client->refCount++;
+    } else {
+       client = NULL;
     }
 
     authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
@@ -1412,9 +2218,9 @@ h_FindClient_r(struct rx_connection *tcon)
        expTime = 0x7fffffff;
     } else if (authClass == 2) {
        afs_int32 kvno;
-
+    
        /* kerberos ticket */
-       code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
+       code = rxkad_GetServerInfo(tcon, /*level */ 0, (afs_uint32 *)&expTime,
                                   tname, tinst, tcell, &kvno);
        if (code) {
            ViceLog(1, ("Failed to get rxkad ticket info\n"));
@@ -1449,32 +2255,17 @@ h_FindClient_r(struct rx_connection *tcon)
        expTime = 0x7fffffff;
     }
 
-    if (!client) {
-       host = h_GetHost_r(tcon);       /* Returns it h_Held */
+    if (!client) { /* loop */
+       host = h_GetHost_r(tcon);       /* Returns with incremented refCount  */
 
+       if (!host) 
+           return NULL;
+
+    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->tcon && (client->tcon != tcon)) {
-                   ViceLog(0,
-                           ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
-                            client->ViceId, client->sid, client->tcon,
-                            tcon));
-                   oldClient =
-                       (struct client *)rx_GetSpecific(client->tcon,
-                                                       rxcon_client_key);
-                   if (oldClient) {
-                       if (oldClient == client)
-                           rx_SetSpecific(client->tcon, rxcon_client_key,
-                                          NULL);
-                       else
-                           ViceLog(0,
-                                   ("Client-conn mismatch: CL1=%x, CN=%x, CL2=%x\n",
-                                    client, client->tcon, oldClient));
-                   }
-                   client->tcon = (struct rx_connection *)0;
-               }
                client->refCount++;
                H_UNLOCK;
                ObtainWriteLock(&client->lock);
@@ -1485,12 +2276,25 @@ h_FindClient_r(struct rx_connection *tcon)
 
        /* Still no client structure - get one */
        if (!client) {
+           h_Lock_r(host);
+            if (host->hostFlags & HOSTDELETED) {
+                h_Unlock_r(host);
+                h_Release_r(host);
+                return NULL;
+            }
+           /* 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))) {
+                   h_Unlock_r(host);
+                   goto retryfirstclient;
+               }
+           }
+           created = 1;
            client = GetCE();
            ObtainWriteLock(&client->lock);
            client->refCount = 1;
            client->host = host;
-           client->next = host->FirstClient;
-           host->FirstClient = client;
 #if FS_STATS_DETAILED
            client->InSameNetwork = host->InSameNetwork;
 #endif /* FS_STATS_DETAILED */
@@ -1499,19 +2303,18 @@ h_FindClient_r(struct rx_connection *tcon)
            client->authClass = authClass;      /* rx only */
            client->sid = rxr_CidOf(tcon);
            client->VenusEpoch = rxr_GetEpoch(tcon);
-           client->CPS.prlist_val = 0;
+           client->CPS.prlist_val = NULL;
            client->CPS.prlist_len = 0;
-           CurrentConnections++;       /* increment number of connections */
+           h_Unlock_r(host);
        }
     }
     client->prfail = fail;
 
     if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
-       if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID)) {
+       client->CPS.prlist_len = 0;
+       if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
            free(client->CPS.prlist_val);
-       }
        client->CPS.prlist_val = NULL;
-       client->CPS.prlist_len = 0;
        client->ViceId = viceid;
        client->expTime = expTime;
 
@@ -1520,14 +2323,14 @@ h_FindClient_r(struct rx_connection *tcon)
            client->CPS.prlist_val = AnonCPS.prlist_val;
        } else {
            H_UNLOCK;
-           code = pr_GetCPS(viceid, &client->CPS);
+           code = hpr_GetCPS(viceid, &client->CPS);
            H_LOCK;
            if (code) {
                char hoststr[16];
                ViceLog(0,
-                       ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
-                        code, viceid, afs_inet_ntoa_r(client->host->host,
-                                                      hoststr),
+                       ("pr_GetCPS failed(%d) for user %d, host %" AFS_PTR_FMT " (%s:%d)\n",
+                        code, viceid, client->host, 
+                         afs_inet_ntoa_r(client->host->host,hoststr),
                         ntohs(client->host->port)));
 
                /* Although ubik_Call (called by pr_GetCPS) traverses thru
@@ -1560,14 +2363,70 @@ 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->tcon == tcon) {
-       oldClient->tcon = (struct rx_connection *)0;
-       ViceLog(0, ("FindClient: client %x(%x) already had conn %x (host %x), stolen by client %x(%x)\n", 
-                   oldClient, oldClient->sid, tcon, 
-                   rx_HostOf(rx_PeerOf(tcon)), client, client->sid));
-       /* rx_SetSpecific will be done immediately below */
-    }
-    client->tcon = tcon;
+    if (oldClient && oldClient != client && oldClient->sid == rxr_CidOf(tcon)
+       && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
+       char hoststr[16];
+       if (!oldClient->deleted) {
+           /* if we didn't create it, it's not ours to put back */
+           if (created) {
+               ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n", 
+                           client, client->sid, tcon, 
+                           afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
+                           ntohs(rxr_PortOf(tcon)),
+                           oldClient, oldClient->sid));
+               if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
+                   free(client->CPS.prlist_val);
+               client->CPS.prlist_val = NULL;
+               client->CPS.prlist_len = 0;
+           }
+           /* We should perhaps check for 0 here */
+           client->refCount--;
+           ReleaseWriteLock(&client->lock);
+           if (created) {
+               FreeCE(client);
+               created = 0;
+           } 
+           oldClient->refCount++;
+           H_UNLOCK;
+           ObtainWriteLock(&oldClient->lock);
+           H_LOCK;
+           client = oldClient;
+       } else {
+           ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n", 
+                       oldClient, oldClient->sid, tcon, 
+                       afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
+                       ntohs(rxr_PortOf(tcon)),
+                       client, client->sid));
+           /* rx_SetSpecific will be done immediately below */
+       }
+    }
+    /* Avoid chaining in more than once. */
+    if (created) {
+       h_Lock_r(host);
+
+        if (host->hostFlags & HOSTDELETED) {
+            h_Unlock_r(host);
+            h_Release_r(host);
+
+            host = NULL;
+            client->host = NULL;
+
+            if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
+                free(client->CPS.prlist_val);
+            client->CPS.prlist_val = NULL;
+            client->CPS.prlist_len = 0;
+
+            client->refCount--;
+            ReleaseWriteLock(&client->lock);
+            FreeCE(client);
+            return NULL;
+        }
+
+       client->next = host->FirstClient;
+       host->FirstClient = client;
+       h_Unlock_r(host);
+       CurrentConnections++;   /* increment number of connections */
+    }
     rx_SetSpecific(tcon, rxcon_client_key, client);
     ReleaseWriteLock(&client->lock);
 
@@ -1595,35 +2454,27 @@ int
 GetClient(struct rx_connection *tcon, struct client **cp)
 {
     register struct client *client;
+    char hoststr[16];
 
     H_LOCK;
-    *cp = client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
-    if (client == NULL || client->tcon == NULL) {
+    *cp = NULL;
+    client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
+    if (client == NULL) {
        ViceLog(0,
-               ("GetClient: no client in conn %x (host %x), VBUSYING\n",
-                tcon, rx_HostOf(rx_PeerOf(tcon))));
+               ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
+                tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
+                 ntohs(rxr_PortOf(tcon))));
        H_UNLOCK;
        return VBUSY;
     }
-    if (rxr_CidOf(client->tcon) != client->sid) {
+    if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
        ViceLog(0,
                ("GetClient: tcon %x tcon sid %d client sid %d\n",
-                client->tcon, rxr_CidOf(client->tcon), client->sid));
+                tcon, rxr_CidOf(tcon), client->sid));
        H_UNLOCK;
        return VBUSY;
     }
-    if (!(client && client->tcon && rxr_CidOf(client->tcon) == client->sid)) {
-       if (!client)
-           ViceLog(0, ("GetClient: no client in conn %x\n", tcon));
-       else
-           ViceLog(0,
-                   ("GetClient: tcon %x tcon sid %d client sid %d\n",
-                    client->tcon, client->tcon ? rxr_CidOf(client->tcon)
-                    : -1, client->sid));
-       assert(0);
-    }
     if (client && client->LastCall > client->expTime && client->expTime) {
-       char hoststr[16];
        ViceLog(1,
                ("Token for %s at %s:%d expired %d\n", h_UserName(client),
                 afs_inet_ntoa_r(client->host->host, hoststr),
@@ -1632,11 +2483,25 @@ GetClient(struct rx_connection *tcon, struct client **cp)
        return VICETOKENDEAD;
     }
 
+    client->refCount++;
+    *cp = client;
     H_UNLOCK;
     return 0;
-
 }                              /*GetClient */
 
+int
+PutClient(struct client **cp)
+{
+    if (*cp == NULL) 
+       return -1;
+
+    H_LOCK;
+    h_ReleaseClient_r(*cp);
+    *cp = NULL;
+    H_UNLOCK;
+    return 0;
+}                              /*PutClient */
+
 
 /* Client user name for short term use.  Note that this is NOT inexpensive */
 char *
@@ -1655,7 +2520,7 @@ h_UserName(struct client *client)
     lnames.namelist_len = 0;
     lnames.namelist_val = (prname *) 0;
     lids.idlist_val[0] = client->ViceId;
-    if (pr_IdToName(&lids, &lnames)) {
+    if (hpr_IdToName(&lids, &lnames)) {
        /* We need to free id we alloced above! */
        free(lids.idlist_val);
        return "*UNKNOWN USER NAME*";
@@ -1664,12 +2529,11 @@ h_UserName(struct client *client)
     free(lids.idlist_val);
     free(lnames.namelist_val);
     return User;
-
 }                              /*h_UserName */
 
 
 void
-h_PrintStats()
+h_PrintStats(void)
 {
     ViceLog(0,
            ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
@@ -1679,29 +2543,32 @@ h_PrintStats()
 
 
 static int
-h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
+h_PrintClient(register struct host *host, int flags, void *rock)
 {
+    StreamHandle_t *file = (StreamHandle_t *)rock;
     register struct client *client;
     int i;
     char tmpStr[256];
     char tbuffer[32];
     char hoststr[16];
+    time_t LastCall, expTime;
 
     H_LOCK;
+    LastCall = host->LastCall;
     if (host->hostFlags & HOSTDELETED) {
        H_UNLOCK;
-       return held;
+       return flags;
     }
     (void)afs_snprintf(tmpStr, sizeof tmpStr,
                       "Host %s:%d down = %d, LastCall %s",
                       afs_inet_ntoa_r(host->host, hoststr),
                       ntohs(host->port), (host->hostFlags & VENUSDOWN),
-                      afs_ctime((time_t *) & host->LastCall, tbuffer,
+                      afs_ctime(&LastCall, tbuffer,
                                 sizeof(tbuffer)));
     (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
     for (client = host->FirstClient; client; client = client->next) {
        if (!client->deleted) {
-           if (client->tcon) {
+               expTime = client->expTime;
                (void)afs_snprintf(tmpStr, sizeof tmpStr,
                                   "    user id=%d,  name=%s, sl=%s till %s",
                                   client->ViceId, h_UserName(client),
@@ -1709,17 +2576,10 @@ h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
                                   authClass ? "Authenticated" :
                                   "Not authenticated",
                                   client->
-                                  authClass ? afs_ctime((time_t *) & client->
-                                                        expTime, tbuffer,
+                                  authClass ? afs_ctime(&expTime, tbuffer,
                                                         sizeof(tbuffer))
                                   : "No Limit\n");
                (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-           } else {
-               (void)afs_snprintf(tmpStr, sizeof tmpStr,
-                                  "    user=%s, no current server connection\n",
-                                  h_UserName(client));
-               (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-           }
            (void)afs_snprintf(tmpStr, sizeof tmpStr, "      CPS-%d is [",
                               client->CPS.prlist_len);
            (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
@@ -1735,7 +2595,7 @@ h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
        }
     }
     H_UNLOCK;
-    return held;
+    return flags;
 
 }                              /*h_PrintClient */
 
@@ -1746,7 +2606,7 @@ h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
  * if known
  */
 void
-h_PrintClients()
+h_PrintClients(void)
 {
     time_t now;
     char tmpStr[256];
@@ -1772,74 +2632,612 @@ h_PrintClients()
 
 
 
-static int
-h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
-{
-    int i;
-    char tmpStr[256];
+static int
+h_DumpHost(register struct host *host, int flags, void *rock)
+{
+    StreamHandle_t *file = (StreamHandle_t *)rock;
+    
+    int i;
+    char tmpStr[256];
+    char hoststr[16];
+
+    H_LOCK;
+    (void)afs_snprintf(tmpStr, sizeof tmpStr,
+                      "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
+                      afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
+                      host->cblist, CheckLock(&host->lock), host->LastCall,
+                      host->ActiveCall, (host->hostFlags & VENUSDOWN),
+                      host->hostFlags & HOSTDELETED, host->Console,
+                      host->hostFlags & CLIENTDELETED, host->hcpsfailed,
+                      host->cpsCall);
+    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+    if (host->hcps.prlist_val)
+       for (i = 0; i < host->hcps.prlist_len; i++) {
+           (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
+                              host->hcps.prlist_val[i]);
+           (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+       }
+    sprintf(tmpStr, "] [");
+    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+    if (host->interface)
+       for (i = 0; i < host->interface->numberOfInterfaces; i++) {
+           char hoststr[16];
+           sprintf(tmpStr, " %s:%d", 
+                    afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
+                    ntohs(host->interface->interface[i].port));
+           (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+       }
+    sprintf(tmpStr, "] refCount: %d\n", host->refCount);
+    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+
+    H_UNLOCK;
+    return flags;
+
+}                              /*h_DumpHost */
+
+
+void
+h_DumpHosts(void)
+{
+    time_t now;
+    StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
+    char tmpStr[256];
+    char tbuffer[32];
+
+    if (file == NULL) {
+       ViceLog(0,
+               ("Couldn't create host dump file %s\n",
+                AFSDIR_SERVER_HOSTDUMP_FILEPATH));
+       return;
+    }
+    now = FT_ApproxTime();
+    (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
+                      afs_ctime(&now, tbuffer, sizeof(tbuffer)));
+    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+    h_Enumerate(h_DumpHost, (char *)file);
+    STREAM_REALLYCLOSE(file);
+    ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
+
+}                              /*h_DumpHosts */
+
+#ifdef AFS_DEMAND_ATTACH_FS
+/*
+ * demand attach fs
+ * host state serialization
+ */
+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_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_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port);
+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);
+
+
+/* this procedure saves all host state to disk for fast startup */
+int
+h_stateSave(struct fs_dump_state * state)
+{
+    AssignInt64(state->eof_offset, &state->hdr->h_offset);
+
+    /* XXX debug */
+    ViceLog(0, ("h_stateSave:  hostCount=%d\n", hostCount));
+
+    /* invalidate host state header */
+    memset(state->h_hdr, 0, sizeof(struct host_state_header));
+
+    if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
+                           sizeof(struct host_state_header))) {
+       state->bail = 1;
+       goto done;
+    }
+
+    fs_stateIncEOF(state, sizeof(struct host_state_header));
+
+    h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
+    if (state->bail) {
+       goto done;
+    }
+
+    h_stateFillHeader(state->h_hdr);
+
+    /* write the real header to disk */
+    state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
+                                     sizeof(struct host_state_header));
+
+ done:
+    return state->bail;
+}
+
+/* demand attach fs
+ * host state serialization
+ *
+ * this procedure restores all host state from a disk for fast startup 
+ */
+int
+h_stateRestore(struct fs_dump_state * state)
+{
+    int i, records;
+
+    /* seek to the right position and read in the host state header */
+    if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
+                          sizeof(struct host_state_header))) {
+       state->bail = 1;
+       goto done;
+    }
+
+    /* check the validity of the header */
+    if (h_stateCheckHeader(state->h_hdr)) {
+       state->bail = 1;
+       goto done;
+    }
+
+    records = state->h_hdr->records;
+
+    if (h_stateAllocMap(state)) {
+       state->bail = 1;
+       goto done;
+    }
+
+    /* iterate over records restoring host state */
+    for (i=0; i < records; i++) {
+       if (h_stateRestoreHost(state) != 0) {
+           state->bail = 1;
+           break;
+       }
+    }
+
+ done:
+    return state->bail;
+}
+
+int
+h_stateRestoreIndices(struct fs_dump_state * state)
+{
+    h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
+    return state->bail;
+}
+
+static int
+h_stateRestoreIndex(struct host * h, int flags, 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 flags;
+}
+
+int
+h_stateVerify(struct fs_dump_state * state)
+{
+    h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
+    return state->bail;
+}
+
+static int
+h_stateVerifyHost(struct host * h, int flags, 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);
+    }
+
+    if (h->interface) {
+       for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
+           if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr, 
+                                     h->interface->interface[i].port)) {
+               state->bail = 1;
+           }
+       }
+       if (h_stateVerifyUuidHash(state, h)) {
+           state->bail = 1;
+       }
+    } else if (h_stateVerifyAddrHash(state, h, h->host, h->port)) {
+       state->bail = 1;
+    }
+
+    if (cb_stateVerifyHCBList(state, h)) {
+       state->bail = 1;
+    }
+
+    return flags;
+}
+
+static int
+h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port)
+{
+    int ret = 0, found = 0;
+    struct host *host = NULL;
+    struct h_AddrHashChain *chain;
+    int index = h_HashIndex(addr);
+    char tmp[16];
+    int chain_len = 0;
+
+    for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
+       host = chain->hostPtr;
+       if (host == NULL) {
+           afs_inet_ntoa_r(addr, tmp);
+           ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
+           ret = 1;
+           goto done;
+       }
+       if ((chain->addr == addr) && (chain->port == port)) {
+           if (host != h) {
+               ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry points to different host struct (%d, %d)\n", 
+                           h->index, host->index));
+               state->flags.warnings_generated = 1;
+           }
+           found = 1;
+           break;
+       }
+       if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
+           ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
+                       FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
+           ret = 1;
+           goto done;
+       }
+       chain_len++;
+    }
+
+    if (!found) {
+       afs_inet_ntoa_r(addr, tmp);
+       if (state->mode == FS_STATE_LOAD_MODE) {
+           ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s not found in hash\n", tmp));
+           ret = 1;
+           goto done;
+       } else {
+           ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s not found in hash\n", tmp));
+           state->flags.warnings_generated = 1;
+       }
+    }
+
+ done:
+    return ret;
+}
+
+static int
+h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
+{
+    int ret = 0, found = 0;
+    struct host *host = NULL;
+    struct h_UuidHashChain *chain;
+    afsUUID * uuidp = &h->interface->uuid;
+    int index = h_UuidHashIndex(uuidp);
+    char tmp[40];
+    int chain_len = 0;
+
+    for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
+       host = chain->hostPtr;
+       if (host == NULL) {
+           afsUUID_to_string(uuidp, tmp, sizeof(tmp));
+           ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
+           ret = 1;
+           goto done;
+       }
+       if (host->interface &&
+           afs_uuid_equal(&host->interface->uuid, uuidp)) {
+           if (host != h) {
+               ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n", 
+                           h->index, host->index));
+               state->flags.warnings_generated = 1;
+           }
+           found = 1;
+           goto done;
+       }
+       if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
+           ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
+                       FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
+           ret = 1;
+           goto done;
+       }
+       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;
+       }
+    }
+
+ done:
+    return ret;
+}
+
+/* create the host state header structure */
+static int
+h_stateFillHeader(struct host_state_header * hdr)
+{
+    hdr->stamp.magic = HOST_STATE_MAGIC;
+    hdr->stamp.version = HOST_STATE_VERSION;
+    return 0;
+}
+
+/* check the contents of the host state header structure */
+static int
+h_stateCheckHeader(struct host_state_header * hdr)
+{
+    int ret=0;
+
+    if (hdr->stamp.magic != HOST_STATE_MAGIC) {
+       ViceLog(0, ("check_host_state_header: invalid state header\n"));
+       ret = 1;
+    }
+    else if (hdr->stamp.version != HOST_STATE_VERSION) {
+       ViceLog(0, ("check_host_state_header: unknown version number\n"));
+       ret = 1;
+    }
+    return ret;
+}
+
+/* allocate the host id mapping table */
+static int
+h_stateAllocMap(struct fs_dump_state * state)
+{
+    state->h_map.len = state->h_hdr->index_max + 1;
+    state->h_map.entries = (struct idx_map_entry_t *)
+       calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
+    return (state->h_map.entries != NULL) ? 0 : 1;
+}
+
+/* function called by h_Enumerate to save a host to disk */
+static int
+h_stateSaveHost(struct host * host, int flags, void* rock)
+{
+    struct fs_dump_state *state = (struct fs_dump_state *) rock;
+    int if_len=0, hcps_len=0;
+    struct hostDiskEntry hdsk;
+    struct host_state_entry_header hdr;
+    struct Interface * ifp = NULL;
+    afs_int32 * hcps = NULL;
+    struct iovec iov[4];
+    int iovcnt = 2;
+
+    memset(&hdr, 0, sizeof(hdr));
+
+    if (state->h_hdr->index_max < host->index) {
+       state->h_hdr->index_max = host->index;
+    }
+
+    h_hostToDiskEntry_r(host, &hdsk);
+    if (host->interface) {
+       if_len = sizeof(struct Interface) + 
+           ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
+       ifp = (struct Interface *) malloc(if_len);
+       assert(ifp != NULL);
+       memcpy(ifp, host->interface, if_len);
+       hdr.interfaces = host->interface->numberOfInterfaces;
+       iov[iovcnt].iov_base = (char *) ifp;
+       iov[iovcnt].iov_len = if_len;
+       iovcnt++;
+    }
+    if (host->hcps.prlist_val) {
+       hdr.hcps = host->hcps.prlist_len;
+       hcps_len = hdr.hcps * sizeof(afs_int32);
+       hcps = (afs_int32 *) malloc(hcps_len);
+       assert(hcps != NULL);
+       memcpy(hcps, host->hcps.prlist_val, hcps_len);
+       iov[iovcnt].iov_base = (char *) hcps;
+       iov[iovcnt].iov_len = hcps_len;
+       iovcnt++;
+    }
+
+    if (hdsk.index > state->h_hdr->index_max)
+       state->h_hdr->index_max = hdsk.index;
+
+    hdr.len = sizeof(struct host_state_entry_header) + 
+       sizeof(struct hostDiskEntry) + if_len + hcps_len;
+    hdr.magic = HOST_STATE_ENTRY_MAGIC;
+
+    iov[0].iov_base = (char *) &hdr;
+    iov[0].iov_len = sizeof(hdr);
+    iov[1].iov_base = (char *) &hdsk;
+    iov[1].iov_len = sizeof(struct hostDiskEntry);
+    
+    if (fs_stateWriteV(state, iov, iovcnt)) {
+       ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
+       state->bail = 1;
+    }
+
+    fs_stateIncEOF(state, hdr.len);
+
+    state->h_hdr->records++;
+
+    if (ifp)
+       free(ifp);
+    if (hcps)
+       free(hcps);
+    if (state->bail) {
+       return H_ENUMERATE_BAIL(flags);
+    }
+    return flags;
+}
+
+/* restores a host from disk */
+static int
+h_stateRestoreHost(struct fs_dump_state * state)
+{
+    int ifp_len=0, hcps_len=0, bail=0;
+    struct host_state_entry_header hdr;
+    struct hostDiskEntry hdsk;
+    struct host *host = NULL;
+    struct Interface *ifp = NULL;
+    afs_int32 * hcps = NULL;
+    struct iovec iov[3];
+    int iovcnt = 1;
+
+    if (fs_stateRead(state, &hdr, sizeof(hdr))) {
+       ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
+                   state->fn));
+       bail = 1;
+       goto done;
+    }
+
+    if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
+       ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
+                   state->fn));
+       bail = 1;
+       goto done;
+    }
+
+    iov[0].iov_base = (char *) &hdsk;
+    iov[0].iov_len = sizeof(struct hostDiskEntry);
+
+    if (hdr.interfaces) {
+       ifp_len = sizeof(struct Interface) +
+           ((hdr.interfaces-1) * sizeof(struct AddrPort));
+       ifp = (struct Interface *) malloc(ifp_len);
+       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);
+       assert(hcps != NULL);
+       iov[iovcnt].iov_base = (char *) hcps;
+       iov[iovcnt].iov_len = hcps_len;
+       iovcnt++;
+    }
+
+    if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
+       ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
+       bail = 1;
+       goto done;
+    }
+
+    if (fs_stateReadV(state, iov, iovcnt)) {
+       ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
+       bail = 1;
+       goto done;
+    }
+
+    if (!hdr.hcps && hdsk.hcps_valid) {
+       /* valid, zero-length host cps ; does this ever happen? */
+       hcps = (afs_int32 *) malloc(sizeof(afs_int32));
+       assert(hcps != NULL);
+    }
+
+    host = GetHT();
+    assert(host != NULL);
+
+    if (ifp) {
+       host->interface = ifp;
+    }
+    if (hcps) {
+       host->hcps.prlist_val = hcps;
+       host->hcps.prlist_len = hdr.hcps;
+    }
 
-    H_LOCK;
-    (void)afs_snprintf(tmpStr, sizeof tmpStr,
-                      "ip:%x port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
-                      host->host, ntohs(host->port), host->index,
-                      host->cblist, CheckLock(&host->lock), host->LastCall,
-                      host->ActiveCall, (host->hostFlags & VENUSDOWN),
-                      host->hostFlags & HOSTDELETED, host->Console,
-                      host->hostFlags & CLIENTDELETED, host->hcpsfailed,
-                      host->cpsCall);
-    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-    if (host->hcps.prlist_val)
-       for (i = 0; i < host->hcps.prlist_len; i++) {
-           (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
-                              host->hcps.prlist_val[i]);
-           (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-       }
-    sprintf(tmpStr, "] [");
-    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-    if (host->interface)
-       for (i = 0; i < host->interface->numberOfInterfaces; i++) {
-           sprintf(tmpStr, " %x", host->interface->addr[i]);
-           (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-       }
-    sprintf(tmpStr, "] holds: ");
-    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+    h_diskEntryToHost_r(&hdsk, host);
+    h_SetupCallbackConn_r(host);
 
-    for (i = 0; i < h_maxSlots; i++) {
-       sprintf(tmpStr, "%04x", host->holds[i]);
-       (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+    h_AddHostToAddrHashTable_r(host->host, host->port, host);
+    if (ifp) {
+       int i;
+       for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
+            if (ifp->interface[i].valid && 
+                !(ifp->interface[i].addr == host->host &&
+                  ifp->interface[i].port == host->port)) {
+                h_AddHostToAddrHashTable_r(ifp->interface[i].addr, 
+                                           ifp->interface[i].port, 
+                                           host);
+            }
+       }
+       h_AddHostToUuidHashTable_r(&ifp->uuid, host);
     }
-    sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
-    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
+    h_InsertList_r(host);
+
+    /* setup host id map entry */
+    state->h_map.entries[hdsk.index].old_idx = hdsk.index;
+    state->h_map.entries[hdsk.index].new_idx = host->index;
+
+ done:
+    if (bail) {
+       if (ifp)
+           free(ifp);
+       if (hcps)
+           free(hcps);
+    }
+    return bail;
+}
 
-    H_UNLOCK;
-    return held;
+/* serialize a host structure to disk */
+static void
+h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
+{
+    out->host = in->host;
+    out->port = in->port;
+    out->hostFlags = in->hostFlags;
+    out->Console = in->Console;
+    out->hcpsfailed = in->hcpsfailed;
+    out->LastCall = in->LastCall;
+    out->ActiveCall = in->ActiveCall;
+    out->cpsCall = in->cpsCall;
+    out->cblist = in->cblist;
+#ifdef FS_STATS_DETAILED
+    out->InSameNetwork = in->InSameNetwork;
+#endif
 
-}                              /*h_DumpHost */
+    /* special fields we save, but are not memcpy'd back on restore */
+    out->index = in->index;
+    out->hcps_len = in->hcps.prlist_len;
+    out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
+}
 
+/* restore a host structure from disk */
+static void
+h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
+{
+    out->host = in->host;
+    out->port = in->port;
+    out->hostFlags = in->hostFlags;
+    out->Console = in->Console;
+    out->hcpsfailed = in->hcpsfailed;
+    out->LastCall = in->LastCall;
+    out->ActiveCall = in->ActiveCall;
+    out->cpsCall = in->cpsCall;
+    out->cblist = in->cblist;
+#ifdef FS_STATS_DETAILED
+    out->InSameNetwork = in->InSameNetwork;
+#endif
+}
 
-void
-h_DumpHosts()
+/* index translation routines */
+int
+h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
 {
-    time_t now;
-    StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
-    char tmpStr[256];
-    char tbuffer[32];
+    int ret = 0;
 
-    if (file == NULL) {
-       ViceLog(0,
-               ("Couldn't create host dump file %s\n",
-                AFSDIR_SERVER_HOSTDUMP_FILEPATH));
-       return;
+    /* hosts use a zero-based index, so old==0 is valid */
+
+    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 */
+       ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
+       ret = 1;
+    } else {
+       *new = state->h_map.entries[old].new_idx;
     }
-    now = FT_ApproxTime();
-    (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
-                      afs_ctime(&now, tbuffer, sizeof(tbuffer)));
-    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-    h_Enumerate(h_DumpHost, (char *)file);
-    STREAM_REALLYCLOSE(file);
-    ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
 
-}                              /*h_DumpHosts */
+    return ret;
+}
+#endif /* AFS_DEMAND_ATTACH_FS */
 
 
 /*
@@ -1853,9 +3251,10 @@ h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
 {
     register struct host *host;
     register int num = 0, active = 0, del = 0;
+    int count;
 
     H_LOCK;
-    for (host = hostList; host; host = host->next) {
+    for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
        if (!(host->hostFlags & HOSTDELETED)) {
            num++;
            if (host->ActiveCall > cutofftime)
@@ -1864,6 +3263,12 @@ h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
                del++;
        }
     }
+    if (count != hostCount) {
+       ViceLog(0, ("h_GetWorkStats found %d of %d hosts\n", count, hostCount));
+    } else if (host != NULL) {
+       ViceLog(0, ("h_GetWorkStats found more than %d hosts\n", hostCount));
+       ShutDownAndCore(PANIC);
+    }
     H_UNLOCK;
     if (nump)
        *nump = num;
@@ -2018,6 +3423,7 @@ h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
 
     register struct host *hostP;       /*Ptr to current host entry */
     register afs_uint32 currAddr_HBO;  /*Curr host addr, host byte order */
+    int count;
 
     /*
      * Clear out the storage pointed to by our parameters.
@@ -2028,7 +3434,7 @@ h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
     *a_diffNetworkP = (afs_int32) 0;
 
     H_LOCK;
-    for (hostP = hostList; hostP; hostP = hostP->next) {
+    for (count = 0, hostP = hostList; hostP && count < hostCount; hostP = hostP->next, count++) {
        if (!(hostP->hostFlags & HOSTDELETED)) {
            /*
             * Bump the number of undeleted host entries found.
@@ -2042,6 +3448,12 @@ h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
                              a_diffNetworkP);
        }                       /*Only look at non-deleted hosts */
     }                          /*For each host record hashed to this index */
+    if (count != hostCount) {
+       ViceLog(0, ("h_GetHostNetStats found %d of %d hosts\n", count, hostCount));
+    } else if (hostP != NULL) {
+       ViceLog(0, ("h_GetHostNetStats found more than %d hosts\n", hostCount));
+       ShutDownAndCore(PANIC);
+    }
     H_UNLOCK;
 }                              /*h_GetHostNetStats */
 
@@ -2055,13 +3467,24 @@ static struct AFSFid zerofid;
  * Since it can serialize them, and pile up, it should be a separate LWP
  * from other events.
  */
-int
-CheckHost(register struct host *host, int held)
+#if 0
+static int
+CheckHost(register struct host *host, int flags, void *rock)
 {
     register 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) {
@@ -2072,9 +3495,10 @@ CheckHost(register struct host *host, int held)
     }
     if (host->LastCall < checktime) {
        h_Lock_r(host);
-       cb_conn = host->callback_rxcon;
-       rx_GetConnection(cb_conn);
        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)) {
@@ -2104,11 +3528,13 @@ CheckHost(register struct host *host, int held)
                     * 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.
+                    * 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;
@@ -2116,11 +3542,8 @@ CheckHost(register struct host *host, int held)
                        H_LOCK;
                        if (code) {
                            if (MultiProbeAlternateAddress_r(host)) {
-                               char hoststr[16];
-                               (void)afs_inet_ntoa_r(host->host, hoststr);
-                               ViceLog(0,
-                                       ("ProbeUuid failed for host %s:%d\n",
-                                        hoststr, ntohs(host->port)));
+                               ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
+                                           hoststr, ntohs(host->port), code));
                                host->hostFlags |= VENUSDOWN;
                            }
                        }
@@ -2129,27 +3552,128 @@ CheckHost(register struct host *host, int held)
                        code = RXAFSCB_Probe(cb_conn);
                        H_LOCK;
                        if (code) {
-                           char hoststr[16];
-                           (void)afs_inet_ntoa_r(host->host, hoststr);
                            ViceLog(0,
-                                   ("Probe failed for host %s:%d\n", hoststr,
-                                    ntohs(host->port)));
+                                   ("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;
-       rx_PutConnection(cb_conn);
-       cb_conn=NULL;
-       H_LOCK;
        h_Unlock_r(host);
     }
     H_UNLOCK;
     return held;
 
 }                              /*CheckHost */
+#endif
+
+int
+CheckHost_r(register struct host *host, int flags, void *dummy)
+{
+    register 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_r */
+    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)) {
+           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_r: 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_r: 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;
+       }
+       h_Unlock_r(host);
+    }
+    return flags;
+
+}                              /*CheckHost_r */
 
 
 /*
@@ -2162,58 +3686,95 @@ CheckHost(register struct host *host, int held)
  * This routine is called roughly every 5 minutes.
  */
 void
-h_CheckHosts()
+h_CheckHosts(void)
 {
     afs_uint32 now = FT_ApproxTime();
 
-    memset((char *)&zerofid, 0, sizeof(zerofid));
+    memset(&zerofid, 0, sizeof(zerofid));
     /*
      * Send a probe to the workstation if it hasn't been heard from in
      * 15 minutes
      */
     checktime = now - 15 * 60;
     clientdeletetime = now - 120 * 60; /* 2 hours ago */
-    h_Enumerate(CheckHost, NULL);
-
+    
+    H_LOCK;
+    h_Enumerate_r(CheckHost_r, hostList, NULL);
+    H_UNLOCK;
 }                              /*h_CheckHosts */
 
 /*
  * This is called with host locked and held. At this point, the
- * hostHashTable should not be having entries for the alternate
- * interfaces. This function has to insert these entries in the
- * hostHashTable.
+ * hostAddrHashTable has an entry for the primary addr/port inserted
+ * by h_Alloc_r().  No other interfaces should be considered valid.
  *
- * The addresses in the ineterfaceAddr list are in host byte order.
+ * The addresses in the interfaceAddr list are in host byte order.
  */
 int
 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
 {
     int i, j;
     int number, count;
-    afs_int32 myHost;
+    afs_uint32 myAddr;
+    afs_uint16 myPort;
     int found;
     struct Interface *interface;
+    char hoststr[16];
+    char uuidstr[128];
+    afs_uint16 port7001 = htons(7001);
 
     assert(host);
     assert(interf);
 
-    ViceLog(125,
-           ("initInterfaceAddr : host %x numAddr %d\n", host->host,
-            interf->numberOfInterfaces));
-
     number = interf->numberOfInterfaces;
-    myHost = host->host;       /* current interface address */
+    myAddr = host->host;       /* current interface address */
+    myPort = host->port;       /* current port */
+
+    ViceLog(125,
+           ("initInterfaceAddr : host %s:%d numAddr %d\n", 
+             afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
 
     /* validation checks */
     if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
        ViceLog(0,
-               ("Number of alternate addresses returned is %d\n", number));
+               ("Invalid number of alternate addresses is %d\n", number));
        return -1;
     }
 
     /*
-     * Convert IP addresses to network byte order, and remove for
-     * duplicate IP addresses from the interface list.
+     * The client's notion of its own IP addresses is not reliable.  
+     *
+     * 1. The client list might contain private address ranges which
+     *    are likely to be re-used by many clients allocated addresses
+     *    by a NAT.
+     *
+     * 2. The client list will not include any public addresses that
+     *    are hidden by a NAT.
+     *
+     * 3. Private address ranges that are exposed to the server will
+     *    be obtained from the rx connections that use them.
+     *
+     * 4. Lists provided by the client are not necessarily truthful.
+     *    Many existing clients (UNIX) do not refresh the IP address
+     *    list as the actual assigned addresses change.  The end result
+     *    is that they report the initial address list for the lifetime
+     *    of the process.  In other words, a client can report addresses
+     *    that they are in fact not using.  Adding these addresses to
+     *    the host interface list without verification is not only
+     *    pointless, it is downright dangerous.
+     *
+     * We therefore do not add alternate addresses to the addr hash table.
+     * We only use them for multi-rx callback breaks.
+     */
+
+    /*
+     * Convert IP addresses to network byte order, and remove
+     * duplicate 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
+     * in the interface list are port 7001 and the port number
+     * for this connection might not be 7001.
      */
     for (i = 0, count = 0, found = 0; i < number; i++) {
        interf->addr_in[i] = htonl(interf->addr_in[i]);
@@ -2223,7 +3784,8 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
        }
        if (j == count) {
            interf->addr_in[count] = interf->addr_in[i];
-           if (interf->addr_in[count] == myHost)
+           if (interf->addr_in[count] == myAddr &&
+                port7001 == myPort)
                found = 1;
            count++;
        }
@@ -2235,130 +3797,85 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
     if (found) {
        interface = (struct Interface *)
            malloc(sizeof(struct Interface) +
-                  (sizeof(afs_int32) * (count - 1)));
+                  (sizeof(struct AddrPort) * (count - 1)));
        if (!interface) {
-           ViceLog(0, ("Failed malloc in initInterfaceAddr_r\n"));
+           ViceLog(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
            assert(0);
        }
        interface->numberOfInterfaces = count;
     } else {
        interface = (struct Interface *)
-           malloc(sizeof(struct Interface) + (sizeof(afs_int32) * count));
-       assert(interface);
+           malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
+       if (!interface) {
+           ViceLog(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
+           assert(0);
+       }
        interface->numberOfInterfaces = count + 1;
-       interface->addr[count] = myHost;
-    }
-    interface->uuid = interf->uuid;
-    for (i = 0; i < count; i++)
-       interface->addr[i] = interf->addr_in[i];
-
-    assert(!host->interface);
-    host->interface = interface;
-
-    for (i = 0; i < host->interface->numberOfInterfaces; i++) {
-       ViceLog(125, ("--- alt address %x\n", host->interface->addr[i]));
+       interface->interface[count].addr = myAddr;
+       interface->interface[count].port = myPort;
+        interface->interface[count].valid = 1;
     }
 
-    return 0;
-}
-
-/* inserts a new HashChain structure corresponding to this address */
-void
-hashInsert_r(afs_int32 addr, struct host *host)
-{
-    int index;
-    struct h_hashChain *chain;
-
-    /* hash into proper bucket */
-    index = h_HashIndex(addr);
+    for (i = 0; i < count; i++) {
 
-    /* insert into beginning of list for this bucket */
-    chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
-    if (!chain) {
-       ViceLog(0, ("Failed malloc in hashInsert_r\n"));
-       assert(0);
+        interface->interface[i].addr = interf->addr_in[i];
+       /* We store the port as 7001 because the addresses reported by 
+        * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
+        * are coming from fully connected hosts (no NAT/PATs)
+        */
+       interface->interface[i].port = port7001;
+        interface->interface[i].valid = 
+            (interf->addr_in[i] == myAddr && port7001 == myPort) ? 1 : 0;
     }
-    chain->hostPtr = host;
-    chain->next = hostHashTable[index];
-    chain->addr = addr;
-    hostHashTable[index] = chain;
-
-}
-
-/*
- * This is called with host locked and held. At this point, the
- * hostHashTable should not be having entries for the alternate
- * interfaces. This function has to insert these entries in the
- * hostHashTable.
- *
- * All addresses are in network byte order.
- */
-int
-addInterfaceAddr_r(struct host *host, afs_int32 addr)
-{
-    int i;
-    int number;
-    int found;
-    struct Interface *interface;
 
-    assert(host);
-    assert(host->interface);
+    interface->uuid = interf->uuid;
 
-    ViceLog(125, ("addInterfaceAddr : host %x addr %d\n", host->host, addr));
+    assert(!host->interface);
+    host->interface = interface;
 
-    /*
-     * Make sure this address is on the list of known addresses
-     * for this host.
-     */
-    number = host->interface->numberOfInterfaces;
-    for (i = 0, found = 0; i < number && !found; i++) {
-       if (host->interface->addr[i] == addr)
-           found = 1;
-    }
-    if (!found) {
-       interface = (struct Interface *)
-           malloc(sizeof(struct Interface) + (sizeof(afs_int32) * number));
-       if (!interface) {
-           ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
-           assert(0);
+    if (LogLevel >= 125) {
+       afsUUID_to_string(&interface->uuid, uuidstr, 127);
+       
+       ViceLog(125, ("--- uuid %s\n", uuidstr));
+       for (i = 0; i < host->interface->numberOfInterfaces; i++) {
+           ViceLog(125, ("--- alt address %s:%d\n", 
+                         afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
+                         ntohs(host->interface->interface[i].port)));
        }
-       interface->numberOfInterfaces = number + 1;
-       interface->uuid = host->interface->uuid;
-       for (i = 0; i < number; i++)
-           interface->addr[i] = host->interface->addr[i];
-       interface->addr[number] = addr;
-       free(host->interface);
-       host->interface = interface;
     }
 
-    /*
-     * Create a hash table entry for this address
-     */
-    hashInsert_r(addr, host);
-
     return 0;
 }
 
 /* deleted a HashChain structure for this address and host */
 /* returns 1 on success */
 int
-hashDelete_r(afs_int32 addr, struct host *host)
+h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, 
+                               struct host *host)
 {
-    int flag;
-    register struct h_hashChain **hp, *th;
+    char hoststr[16];
+    register struct h_AddrHashChain **hp, *th;
 
-    for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp);) {
-       assert(th->hostPtr);
-       if (th->hostPtr == host && th->addr == addr) {
-           *hp = th->next;
-           free(th);
-           flag = 1;
-           break;
-       } else {
-           hp = &th->next;
-       }
+    if (addr == 0 && port == 0)
+       return 1;
+
+    for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp); 
+        hp = &th->next) {
+        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),
+                         ntohs(host->port)));
+            *hp = th->next;
+            free(th);
+           return 1;
+        }
     }
-    return flag;
+    ViceLog(125, 
+           ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) not found\n",
+            host, afs_inet_ntoa_r(host->host, hoststr), 
+            ntohs(host->port)));
+    return 0;
 }
 
 
@@ -2370,12 +3887,18 @@ void
 printInterfaceAddr(struct host *host, int level)
 {
     int i, number;
+    char hoststr[16];
+
     if (host->interface) {
        /* check alternate addresses */
        number = host->interface->numberOfInterfaces;
-       assert(number > 0);
-       for (i = 0; i < number; i++)
-           ViceLog(level, ("%x ", host->interface->addr[i]));
+        if (number == 0) {
+            ViceLog(level, ("no-addresses "));
+       } else {
+            for (i = 0; i < number; i++)
+                ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
+                                ntohs(host->interface->interface[i].port)));
+        }
     }
     ViceLog(level, ("\n"));
 }