Add rx security index enum
[openafs.git] / src / viced / host.c
index bf5aeb6..8796bdd 100644 (file)
@@ -12,8 +12,6 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
 #include <stdio.h>
 #include <errno.h>
@@ -33,6 +31,8 @@ RCSID
 #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>
@@ -87,13 +87,14 @@ 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.
@@ -117,7 +118,7 @@ static void h_TossStuff_r(register struct host *host);
 
 /* get a new block of CEs and chain it on CEFree */
 static void
-GetCEBlock()
+GetCEBlock(void)
 {
     register struct CEBlock *block;
     register int i;
@@ -142,7 +143,7 @@ GetCEBlock()
 
 /* get the next available CE */
 static struct client *
-GetCE()
+GetCE(void)
 {
     register struct client *entry;
 
@@ -156,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 */
@@ -189,7 +190,7 @@ static struct host *HTFree = 0;     /* first free file entry */
  */
 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
-#define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
+#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 */
@@ -199,7 +200,7 @@ 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;
@@ -234,7 +235,7 @@ GetHTBlock()
 
 /* get the next available HT */
 static struct host *
-GetHT()
+GetHT(void)
 {
     register struct host *entry;
 
@@ -244,7 +245,7 @@ GetHT()
     entry = HTFree;
     HTFree = entry->next;
     HTs++;
-    memset((char *)entry, 0, HOST_TO_ZERO(entry));
+    memset(entry, 0, HOST_TO_ZERO(entry));
     return (entry);
 
 }                              /*GetHT */
@@ -265,11 +266,8 @@ hpr_Initialize(struct ubik_client **uclient)
 {
     afs_int32 code;
     struct rx_connection *serverconns[MAXSERVERS];
-    struct rx_securityClass *sc[3];
+    struct rx_securityClass *sc;
     struct afsconf_dir *tdir;
-    char tconfDir[100] = "";
-    char tcell[64] = "";
-    struct ktc_token ttoken;
     afs_int32 scIndex;
     struct afsconf_cell info;
     afs_int32 i;
@@ -290,7 +288,8 @@ hpr_Initialize(struct ubik_client **uclient)
     
     code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
     if (code) {
-       ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s", cellstr, confDir, AFSDIR_CELLSERVDB_FILE));
+       ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s",
+                   cellstr, confDir->name, AFSDIR_CELLSERVDB_FILE));
        afsconf_Close(tdir);
        return code;
     }
@@ -302,55 +301,29 @@ hpr_Initialize(struct ubik_client **uclient)
         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);
+        code = afsconf_ClientAuthSecure(tdir, &sc, &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];
+            scIndex = RX_SECIDX_NULL;
     } 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);
-        }
+       afsconf_ClientAuthToken(&info, 0, &sc, &scIndex, NULL);
     }
-    if ((scIndex == 0) && (sc[0] == 0))
-        sc[0] = rxnull_NewClientSecurityObject();
-    if ((scIndex == 0))
+    if ((scIndex == RX_SECIDX_NULL) && (sc == NULL))
+        sc = rxnull_NewClientSecurityObject();
+    if (scIndex == RX_SECIDX_NULL)
        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);
+                             info.hostAddr[i].sin_port, PRSRV,
+                            sc, scIndex);
     }
 
     code = ubik_ClientInit(serverconns, uclient);
@@ -358,7 +331,7 @@ hpr_Initialize(struct ubik_client **uclient)
        ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
     }
     afsconf_Close(tdir);
-    code = rxs_Release(sc[scIndex]);
+    code = rxs_Release(sc);
     return code;
 }
 
@@ -492,56 +465,6 @@ hpr_GetCPS(afs_int32 id, prlist *CPS)
 static short consolePort = 0;
 
 int
-h_Release(register struct host *host)
-{
-    H_LOCK;
-    h_Release_r(host);
-    H_UNLOCK;
-    return 0;
-}
-
-/**
- * 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)
-{
-
-    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();
-
-    return 0;
-}
-
-int
-h_OtherHolds_r(register struct host *host)
-{
-    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;
-       }
-    }
-    return 0;
-}
-
-int
 h_Lock_r(register struct host *host)
 {
     H_UNLOCK;
@@ -710,12 +633,12 @@ h_gethostcps_r(register struct host *host, register afs_int32 now)
             */
            host->hcpsfailed = 1;
            ViceLog(0,
-                   ("Warning:  GetHostCPS failed (%d) for %x (%s:%d); will retry\n",
+                   ("Warning:  GetHostCPS failed (%d) for %p (%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 (%s:%d); ignored\n",
+                   ("gethost:  GetHostCPS failed (%d) for %p (%s:%d); ignored\n",
                     code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
        }
        if (host->hcps.prlist_val)
@@ -742,15 +665,13 @@ h_gethostcps_r(register struct host *host, register afs_int32 now)
 void
 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;
@@ -837,12 +758,14 @@ h_SetupCallbackConn_r(struct host * host)
     rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
 }
 
-/* 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 haddr, afs_uint16 hport, int *heldp)
+/* 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)
 {
     afs_int32 now;
     struct host *host = NULL;
@@ -856,14 +779,17 @@ h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
        assert(host);
        if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
            && chain->port == hport) {
-           *heldp = h_Held_r(host);
-           if (!*heldp)
-               h_Hold_r(host);
+           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);
@@ -875,7 +801,7 @@ h_Lookup_r(afs_uint32 haddr, afs_uint16 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);
            }
@@ -883,8 +809,8 @@ h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
        }
        host = NULL;
     }
-    return host;
-
+    *hostp = host;
+    return 0;
 }                              /*h_Lookup */
 
 /* Lookup a host given its UUID. */
@@ -900,43 +826,32 @@ h_LookupUuid_r(afsUUID * uuidp)
        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 %x (%s:%d) was locked.\n",
+               ("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 {
@@ -951,7 +866,8 @@ h_TossStuff_r(register struct host *host)
            if (code < 0) {
                char hoststr[16];
                ViceLog(0,
-                       ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x was locked.\n",
+                       ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
+                        "client %p was locked.\n",
                         host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port), client));
                return;
@@ -960,7 +876,8 @@ h_TossStuff_r(register struct host *host)
            if (client->refCount) {
                char hoststr[16];
                ViceLog(0,
-                       ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x refcount %d.\n",
+                       ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
+                        "client %p refcount %d.\n",
                         host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port), client, client->refCount));
                /* This is the same thing we do if the host is locked */
@@ -983,9 +900,7 @@ h_TossStuff_r(register struct host *host)
     host->hostFlags &= ~CLIENTDELETED;
 
     if (host->hostFlags & HOSTDELETED) {
-        register struct h_AddrHashChain **ahp, *ath;
        register struct rx_connection *rxconn;
-       afsUUID *uuidp;
        struct AddrPort hostAddrPort;
        int i;
 
@@ -1004,44 +919,22 @@ h_TossStuff_r(register struct host *host)
 
        /* if alternate addresses do not exist */
        if (!(host->interface)) {
-           for (ahp = &hostAddrHashTable[h_HashIndex(host->host)]; (ath = *ahp);
-                ahp = &ath->next) {
-               assert(ath->hostPtr);
-               if (ath->hostPtr == host) {
-                   *ahp = ath->next;
-                   free(ath);
-                   break;
-               }
-           }
+           h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
        } else {
-            register struct h_UuidHashChain **uhp, *uth;
-           /* delete the hash entry for the UUID */
-           uuidp = &host->interface->uuid;
-           for (uhp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (uth = *uhp);
-                uhp = &uth->next) {
-               assert(uth->hostPtr);
-               if (uth->hostPtr == host) {
-                   *uhp = uth->next;
-                   free(uth);
-                   break;
-               }
-           }
-           /* delete the hash entry for each alternate addresses */
+            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++) {
                hostAddrPort = host->interface->interface[i];
-
-                if (!hostAddrPort.valid)
-                    continue;
-
-               for (ahp = &hostAddrHashTable[h_HashIndex(hostAddrPort.addr)]; (ath = *ahp);
-                    ahp = &ath->next) {
-                   assert(ath->hostPtr);
-                   if (ath->hostPtr == host) {
-                       *ahp = ath->next;
-                       free(ath);
-                       break;
-                   }
-               }
+                /* 
+                 * 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;
@@ -1053,18 +946,24 @@ h_TossStuff_r(register struct host *host)
 }                              /*h_TossStuff_r */
 
 
+
 /* 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;
@@ -1074,71 +973,80 @@ 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 (!H_ENUMERATE_ISSET_HELD(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(held[i]))
+       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, *next;
-    register int held, nheld;
+    int flags = 0;
+    int nflags = 0;
+    int count;
 
     if (hostCount == 0) {
        return;
     }
-    if (enumstart && !(held = h_Held_r(enumstart)))
-       h_Hold_r(enumstart); 
-    for (host = enumstart; host; host = next, held = nheld) {
+    h_Hold_r(enumstart);
+    for (count = 0, host = enumstart; host && count < hostCount; host = next, flags = nflags, count++) {
        next = host->next;
-       if (next && !(nheld = h_Held_r(next)) && !H_ENUMERATE_ISSET_BAIL(held))
+       if (next && !H_ENUMERATE_ISSET_BAIL(flags))
            h_Hold_r(next);
-       held = (*proc) (host, held, param);
-       if (!H_ENUMERATE_ISSET_HELD(held))
+       flags = (*proc) (host, flags, param);
+       if (H_ENUMERATE_ISSET_BAIL(flags)) {
            h_Release_r(host); /* this might free up the host */
-       if (H_ENUMERATE_ISSET_BAIL(held)) {
-           if (!H_ENUMERATE_ISSET_HELD(nheld))
-               h_Release_r(next); /* this might free up the host */
            break;
        }
+       h_Release_r(host); /* this might free up the host */
     }
-}                              /*h_Enumerate_r */
+    if (host != NULL) {
+       ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", hostCount));
+       ShutDownAndCore(PANIC);
+    }
+}      /*h_Enumerate_r */
+
 
 /* inserts a new HashChain structure corresponding to this UUID */
 void
@@ -1146,14 +1054,30 @@ h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
 {
     int index;
     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 (host->interface && afs_uuid_equal(&host->interface->uuid, uuid))
+       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 */
@@ -1162,12 +1086,53 @@ h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
        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 %p (%s:%d) added as uuid %s\n",
+             host, afs_inet_ntoa_r(chain->hostPtr->host, hoststr), 
+             ntohs(chain->hostPtr->port), uuid2));
 }
 
+/* deletes a HashChain structure corresponding to this host */
+int
+h_DeleteHostFromUuidHashTable_r(struct host *host)
+{
+     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;
+}
 
 /* inserts a new HashChain structure corresponding to this address */
 void
@@ -1175,27 +1140,32 @@ h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
 {
     int index;
     struct h_AddrHashChain *chain;
-    int found = 0;
     char hoststr[16];
 
     /* hash into proper bucket */
     index = h_HashIndex(addr);
 
-    /* don't add the same entry multiple times */
+    /* don't add the same address:port pair entry multiple times */
     for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
        if (chain->addr == addr && chain->port == port) {
-            if (chain->hostPtr == host)
-                found = 1;
-            else if (!(host->hostFlags & HOSTDELETED))
-                ViceLog(125, ("Addr %s:%d assigned to %x and %x.\n",
-                            afs_inet_ntoa_r(addr, hoststr), ntohs(port),
-                            host, chain));
-        }
+           if (chain->hostPtr == host) {
+               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;
+           }
+           if (!(chain->hostPtr->hostFlags & HOSTDELETED)) {
+               ViceLog(0,
+                       ("h_AddHostToAddrHashTable_r: refusing to hash host %" AFS_PTR_FMT ", %"
+                        AFS_PTR_FMT " (%s:%d) already hashed\n",
+                        host, chain->hostPtr, afs_inet_ntoa_r(chain->addr, hoststr),
+                         ntohs(chain->port)));
+               return;
+           }
+       }
     }
 
-    if (found)
-        return;
-
     /* insert into beginning of list for this bucket */
     chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
     if (!chain) {
@@ -1207,13 +1177,14 @@ h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
     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. At this point, the
- * hostAddrHashTable should not have entries for the alternate
- * interfaces. This function has to insert these entries in the
- * hostAddrHashTable.
+ * 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.
  */
@@ -1222,10 +1193,9 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
 {
     int i;
     int number;
-    int found;
     struct Interface *interface;
     char hoststr[16], hoststr2[16];
-
+                                                   
     assert(host);
     assert(host->interface);
 
@@ -1234,45 +1204,54 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
      * for this host.
      */
     number = host->interface->numberOfInterfaces;
-    for (i = 0, found = 0; i < number && !found; i++) {
+    for (i = 0; i < number; i++) {
        if (host->interface->interface[i].addr == addr &&
              host->interface->interface[i].port == port) {
-           found = 1;
-            host->interface->interface[i].valid = 1;
+           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 %s:%d addr %s:%d : found:%d\n", 
-                  afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), 
-                  afs_inet_ntoa_r(addr, hoststr2), ntohs(port), found));
+    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)));
     
-    if (!found) {
-       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];
-       interface->interface[number].addr = addr;
-       interface->interface[number].port = port;
-        interface->interface[number].valid = 1;
-       free(host->interface);
-       host->interface = interface;
+    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. At this point, the
- * hostAddrHashTable should not be having entries for the alternate
- * interfaces. This function has to insert these entries in the
- * hostAddrHashTable.
+ * This is called with host locked and held.
  *
  * All addresses are in network byte order.
  */
@@ -1281,16 +1260,16 @@ removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
 {
     int i;
     int number;
-    int found;
     struct Interface *interface;
     char hoststr[16], hoststr2[16];
 
     assert(host);
     assert(host->interface);
 
-    ViceLog(125, ("removeInterfaceAddr : host %s:%d addr %s:%d\n", 
-                  afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), 
-                  afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
+    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
@@ -1298,26 +1277,62 @@ removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
      */
     interface = host->interface;
     number = host->interface->numberOfInterfaces;
-    for (i = 0, found = 0; i < number && !found; i++) {
+    for (i = 0; i < number; i++) {
        if (interface->interface[i].addr == addr &&
            interface->interface[i].port == port) {
-           found = 1;
-            interface->interface[i].valid = 0;
-       }
-    }
-    if (found) {
-       number--;
-       for (; i < number; i++) {
-           interface->interface[i] = interface->interface[i+1];
+           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;
        }
-       interface->numberOfInterfaces = number;
-    }
+    }  
+    /* 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)));
+    
     /*
-     * Remove the hash table entry for this address
+     * Make sure this address is on the list of known addresses
+     * for this host.
      */
-    h_DeleteHostFromAddrHashTable_r(addr, port, 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;
 }
 
@@ -1335,44 +1350,34 @@ 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) {
+    if (!host->interface || host->interface->numberOfInterfaces == 1) {
         if (host->host == addr && host->port == port) {
             ViceLog(25,
-                    ("Removing only address for host %x (%s:%d), deleting host.\n",
+                    ("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 {
-        removeInterfaceAddr_r(host, host->host, host->port);
-        if (host->interface->numberOfInterfaces == 0) {
-            ViceLog(25,
-                     ("Removed only address for host %x (%s:%d), no alternate interfaces, deleting host.\n",
-                       host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
-            host->hostFlags |= HOSTDELETED;
-        } else {
-            struct rx_connection *rxconn;
-
-            rxconn = host->callback_rxcon;
-            host->callback_rxcon = NULL;
-
-            if (rxconn) {
-                struct client *client;
-                /*
-                * 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);
-                rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
-                rx_DestroyConnection(rxconn);
-            }
+        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 %x (%s:%d), new primary interface %s:%d.\n",
+                             ("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)));
@@ -1385,10 +1390,21 @@ removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
 
             if (i == host->interface->numberOfInterfaces) {
                 ViceLog(25,
-                         ("Removed only address for host %x (%s:%d), no valid alternate interfaces, deleting host.\n",
+                         ("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 =
@@ -1396,12 +1412,15 @@ removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
                 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;
 }
-int 
+static int
 h_threadquota(int waiting) 
 {
     if (lwps > 64) {
@@ -1420,14 +1439,13 @@ h_threadquota(int waiting)
     return 0;
 }
 
-/* Host is returned held */
+/* 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;
-    int held;
     struct interfaceAddr interf;
     int interfValid = 0;
     struct Identity *identP = NULL;
@@ -1436,19 +1454,25 @@ h_GetHost_r(struct rx_connection *tcon)
     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;
-    host = h_Lookup_r(haddr, hport, &held);
+    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
@@ -1457,36 +1481,70 @@ h_GetHost_r(struct rx_connection *tcon)
         */
        if ((host->hostFlags & HWHO_INPROGRESS) && 
            h_threadquota(host->lock.num_waiting)) {
-           if (!held)
                h_Release_r(host);
-           return 0;
+           host = NULL;
+           goto gethost_out;
        }
        h_Lock_r(host);
-       if (!(host->hostFlags & ALTADDR)) {
-           host->hostFlags &= ~HWHO_INPROGRESS;
-           /* Another thread is doing initialization */
+       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);
-           if (!held)
-               h_Release_r(host);
            ViceLog(125,
-                   ("Host %x (%s:%d) starting h_Lookup again\n",
+                   ("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;
-       code =
-           RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
-       if (code == RXGEN_OPCODE)
-           code = RXAFSCB_WhoAreYou(cb_conn, &interf);
+        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) || 
-           (afs_uuid_equal(&interf.uuid, &nulluuid))) {
+           ((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"));
@@ -1494,23 +1552,38 @@ h_GetHost_r(struct rx_connection *tcon)
            }
            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 %x (%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);
-               if (!held)
+           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;
+                   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;
@@ -1527,24 +1600,105 @@ h_GetHost_r(struct rx_connection *tcon)
             * then this is not the same host as before. */
            if (!host->interface
                || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
-                ViceLog(25,
-                         ("Uuid doesn't match host %x (%s:%d).\n",
-                           host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
-
-                removeAddress_r(host, host->host, host->port);
+               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);
-               if (!held)
-                   h_Release_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 {
-           ViceLog(0,
-                   ("CB: WhoAreYou failed for host %x (%s:%d), error %d\n",
-                     host, afs_inet_ntoa_r(host->host, 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] & CLIENT_CAPABILITY_ERRORTRANS))
@@ -1558,18 +1712,16 @@ h_GetHost_r(struct rx_connection *tcon)
        if (!(host->hostFlags & ALTADDR)) {
            /* another thread is doing the initialisation */
            ViceLog(125,
-                   ("Host %x (%s:%d) waiting for host-init to complete\n",
+                   ("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);
-           host->hostFlags &= ~HWHO_INPROGRESS;
            h_Unlock_r(host);
-           if (!held)
-               h_Release_r(host);
            ViceLog(125,
-                   ("Host %x (%s:%d) starting h_Lookup again\n",
+                   ("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
@@ -1587,7 +1739,8 @@ 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 %x (%s:%d), deleting(%x %x %s %s)\n",
+                   ("CB: new identity for host %p (%s:%d), "
+                    "deleting(%x %p %s %s)\n",
                     host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
                     identP->valid, host->interface,
                     identP->valid ? uuid1 : "no_uuid",
@@ -1596,10 +1749,8 @@ h_GetHost_r(struct rx_connection *tcon)
            /* The host in the cache is not the host for this connection */
             h_Lock_r(host);
            host->hostFlags |= HOSTDELETED;
-           host->hostFlags &= ~HWHO_INPROGRESS;
            h_Unlock_r(host);
-           if (!held)
-               h_Release_r(host);
+           h_Release_r(host);
            goto retry;
        }
     } else {
@@ -1609,6 +1760,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);
@@ -1618,7 +1770,7 @@ h_GetHost_r(struct rx_connection *tcon)
            cb_conn=NULL;
            H_LOCK;
            if ((code == RXGEN_OPCODE) || 
-               afs_uuid_equal(&interf.uuid, &nulluuid)) {
+               ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
                if (!identP)
                    identP =
                        (struct Identity *)malloc(sizeof(struct Identity));
@@ -1633,7 +1785,7 @@ h_GetHost_r(struct rx_connection *tcon)
                if (!pident)
                    rx_SetSpecific(tcon, rxcon_ident_key, identP);
                ViceLog(25,
-                       ("Host %x (%s:%d) does not support WhoAreYou.\n",
+                       ("Host %" AFS_PTR_FMT " (%s:%d) does not support WhoAreYou.\n",
                         host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port)));
                code = 0;
@@ -1654,7 +1806,7 @@ h_GetHost_r(struct rx_connection *tcon)
                if (!pident)
                    rx_SetSpecific(tcon, rxcon_ident_key, identP);
                ViceLog(25,
-                       ("WhoAreYou success on host %x (%s:%d)\n",
+                       ("WhoAreYou success on host %" AFS_PTR_FMT " (%s:%d)\n",
                         host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port)));
            }
@@ -1669,11 +1821,19 @@ h_GetHost_r(struct rx_connection *tcon)
            } else if (code == 0) {
                oldHost = h_LookupUuid_r(&identP->uuid);
                 if (oldHost) {
-                    int probefail = 0;
-
-                   if (!h_Held_r(oldHost))
-                       h_Hold_r(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) {
@@ -1696,7 +1856,7 @@ h_GetHost_r(struct rx_connection *tcon)
                             * MultiProbeAlternateAddress_r() will remove the
                             * alternate interfaces that do not have the same
                             * Uuid. */
-                           ViceLog(0,("CB: ProbeUuid for host %x (%s:%d) failed %d\n",
+                           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));
@@ -1718,57 +1878,54 @@ h_GetHost_r(struct rx_connection *tcon)
                        struct rx_connection *rxconn;
 
                        ViceLog(25,
-                                 ("CB: Host %x (%s:%d) has new addr %s:%d\n",
+                                 ("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
+                           /* 
+                            * 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, found;
+                           int i;
                            struct Interface *interface = oldHost->interface;
                            int number = oldHost->interface->numberOfInterfaces;
-                           for (i = 0, found = 0; i < number; i++) {
+                           for (i = 0; i < number; i++) {
                                if (interface->interface[i].addr == haddr &&
                                    interface->interface[i].port != hport) {
-                                   found = 1;
+                                   /* 
+                                    * 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;
                                }
                            }
-                           if (found) {
-                               /* 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);
-                           }
                        }
-                       addInterfaceAddr_r(oldHost, haddr, hport);
+                       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 = NULL;
+                       host->callback_rxcon = rxconn;
                        
-                       if (rxconn) {
-                           struct client *client;
-                           /*
-                            * 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);
-                           rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
-                           rx_DestroyConnection(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);
@@ -1790,7 +1947,7 @@ h_GetHost_r(struct rx_connection *tcon)
                    H_LOCK;
                    if (code == 0) {
                        ViceLog(25,
-                               ("InitCallBackState3 success on host %x (%s:%d)\n",
+                               ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
                                 host, afs_inet_ntoa_r(host->host, hoststr),
                                 ntohs(host->port)));
                        assert(interfValid == 1);
@@ -1800,12 +1957,12 @@ h_GetHost_r(struct rx_connection *tcon)
            }
            if (code) {
                ViceLog(0,
-                       ("CB: RCallBackConnectBack failed for %x (%s:%d)\n",
+                       ("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 {
                ViceLog(125,
-                       ("CB: RCallBackConnectBack succeeded for %x (%s:%d)\n",
+                       ("CB: RCallBackConnectBack succeeded for %" AFS_PTR_FMT " (%s:%d)\n",
                         host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
                host->hostFlags |= RESETDONE;
            }
@@ -1819,10 +1976,16 @@ h_GetHost_r(struct rx_connection *tcon)
        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 */
@@ -1834,7 +1997,7 @@ int  num_lrealms = -1;
 
 /* not reentrant */
 void
-h_InitHostPackage()
+h_InitHostPackage(void)
 {
     memset(&nulluuid, 0, sizeof(afsUUID));
     afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
@@ -1920,13 +2083,13 @@ MapName_r(char *aname, char *acell, afs_int32 * aval)
            if (*aval == AnonymousID) {
                ViceLog(2,
                        ("MapName: NameToId on %s returns anonymousID\n",
-                        lnames.namelist_val));
+                        lnames.namelist_val[0]));
            }
            free(lids.idlist_val);      /* return parms are not malloced in stub if server proc aborts */
        } else {
            ViceLog(0,
                    ("MapName: NameToId on '%s' is unknown\n",
-                    lnames.namelist_val));
+                    lnames.namelist_val[0]));
            code = -1;
        }
     }
@@ -1946,9 +2109,10 @@ 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) {
@@ -1960,6 +2124,12 @@ h_ID2Client(afs_int32 vid)
            }
        }
     }
+    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 NULL;
@@ -1972,16 +2142,16 @@ h_ID2Client(afs_int32 vid)
  * by one. The caller must call h_ReleaseClient_r when finished with
  * the client.
  *
- * the client->host is returned held.  h_ReleaseClient_r does not release
- * the hold on client->host.
+ * 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;
@@ -2029,7 +2199,7 @@ h_FindClient_r(struct rx_connection *tcon)
        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"));
@@ -2065,10 +2235,10 @@ h_FindClient_r(struct rx_connection *tcon)
     }
 
     if (!client) { /* loop */
-       host = h_GetHost_r(tcon);       /* Returns it h_Held */
+       host = h_GetHost_r(tcon);       /* Returns with incremented refCount  */
 
        if (!host) 
-           return 0;
+           return NULL;
 
     retryfirstclient:
        /* First try to find the client structure */
@@ -2086,6 +2256,11 @@ 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))
@@ -2132,7 +2307,7 @@ h_FindClient_r(struct rx_connection *tcon)
            if (code) {
                char hoststr[16];
                ViceLog(0,
-                       ("pr_GetCPS failed(%d) for user %d, host %x (%s:%d)\n",
+                       ("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)));
@@ -2173,7 +2348,8 @@ h_FindClient_r(struct rx_connection *tcon)
        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", 
+               ViceLog(0, ("FindClient: stillborn client %p(%x); "
+                           "conn %p (host %s:%d) had client %p(%x)\n",
                            client, client->sid, tcon, 
                            afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
                            ntohs(rxr_PortOf(tcon)),
@@ -2196,7 +2372,8 @@ h_FindClient_r(struct rx_connection *tcon)
            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", 
+           ViceLog(0, ("FindClient: deleted client %p(%x) already had "
+                       "conn %p (host %s:%d), stolen by client %p(%x)\n",
                        oldClient, oldClient->sid, tcon, 
                        afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
                        ntohs(rxr_PortOf(tcon)),
@@ -2207,6 +2384,25 @@ h_FindClient_r(struct rx_connection *tcon)
     /* 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);
@@ -2246,7 +2442,7 @@ GetClient(struct rx_connection *tcon, struct client **cp)
     client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
     if (client == NULL) {
        ViceLog(0,
-               ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
+               ("GetClient: no client in conn %p (host %s:%d), VBUSYING\n",
                 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
                  ntohs(rxr_PortOf(tcon))));
        H_UNLOCK;
@@ -2254,7 +2450,7 @@ GetClient(struct rx_connection *tcon, struct client **cp)
     }
     if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
        ViceLog(0,
-               ("GetClient: tcon %x tcon sid %d client sid %d\n",
+               ("GetClient: tcon %p tcon sid %d client sid %d\n",
                 tcon, rxr_CidOf(tcon), client->sid));
        H_UNLOCK;
        return VBUSY;
@@ -2318,7 +2514,7 @@ h_UserName(struct client *client)
 
 
 void
-h_PrintStats()
+h_PrintStats(void)
 {
     ViceLog(0,
            ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
@@ -2328,8 +2524,9 @@ 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];
@@ -2341,7 +2538,7 @@ h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
     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",
@@ -2379,7 +2576,7 @@ h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
        }
     }
     H_UNLOCK;
-    return held;
+    return flags;
 
 }                              /*h_PrintClient */
 
@@ -2390,7 +2587,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];
@@ -2417,8 +2614,10 @@ h_PrintClients()
 
 
 static int
-h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
+h_DumpHost(register struct host *host, int flags, void *rock)
 {
+    StreamHandle_t *file = (StreamHandle_t *)rock;
+    
     int i;
     char tmpStr[256];
     char hoststr[16];
@@ -2449,24 +2648,17 @@ h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
                     ntohs(host->interface->interface[i].port));
            (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
        }
-    sprintf(tmpStr, "] holds: ");
-    (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-
-    for (i = 0; i < h_maxSlots; i++) {
-       sprintf(tmpStr, "%04x", host->holds[i]);
-       (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
-    }
-    sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
+    sprintf(tmpStr, "] refCount:%d hostFlags:%hu\n", host->refCount, host->hostFlags);
     (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
 
     H_UNLOCK;
-    return held;
+    return flags;
 
 }                              /*h_DumpHost */
 
 
 void
-h_DumpHosts()
+h_DumpHosts(void)
 {
     time_t now;
     StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
@@ -2497,10 +2689,10 @@ h_DumpHosts()
 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(register struct host * host, int held, 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 held, struct fs_dump_state * state);
-static int h_stateVerifyHost(struct host * h, int held, 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);
@@ -2592,12 +2784,13 @@ h_stateRestoreIndices(struct fs_dump_state * state)
 }
 
 static int
-h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state)
+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(held);
+       return H_ENUMERATE_BAIL(flags);
     }
-    return held;
+    return flags;
 }
 
 int
@@ -2608,13 +2801,14 @@ h_stateVerify(struct fs_dump_state * state)
 }
 
 static int
-h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state)
+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(held);
+       return H_ENUMERATE_BAIL(flags);
     }
 
     if (h->interface) {
@@ -2635,8 +2829,7 @@ h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state)
        state->bail = 1;
     }
 
- done:
-    return held;
+    return flags;
 }
 
 static int
@@ -2751,6 +2944,7 @@ 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 */
@@ -2782,9 +2976,10 @@ h_stateAllocMap(struct fs_dump_state * state)
 
 /* function called by h_Enumerate to save a host to disk */
 static int
-h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state)
+h_stateSaveHost(struct host * host, int flags, void* rock)
 {
-    int i, if_len=0, hcps_len=0;
+    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;
@@ -2842,15 +3037,14 @@ h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * st
 
     state->h_hdr->records++;
 
- done:
     if (ifp)
        free(ifp);
     if (hcps)
        free(hcps);
     if (state->bail) {
-       return H_ENUMERATE_BAIL(held);
+       return H_ENUMERATE_BAIL(flags);
     }
-    return held;
+    return flags;
 }
 
 /* restores a host from disk */
@@ -2933,13 +3127,20 @@ h_stateRestoreHost(struct fs_dump_state * state)
     h_diskEntryToHost_r(&hdsk, host);
     h_SetupCallbackConn_r(host);
 
+    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);
     }
-    h_AddHostToAddrHashTable_r(host->host, host->port, host);
     h_InsertList_r(host);
 
     /* setup host id map entry */
@@ -3015,7 +3216,6 @@ h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
        *new = state->h_map.entries[old].new_idx;
     }
 
- done:
     return ret;
 }
 #endif /* AFS_DEMAND_ATTACH_FS */
@@ -3032,9 +3232,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)
@@ -3043,6 +3244,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;
@@ -3197,6 +3404,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.
@@ -3207,7 +3415,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.
@@ -3221,6 +3429,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 */
 
@@ -3234,8 +3448,9 @@ static struct AFSFid zerofid;
  * Since it can serialize them, and pile up, it should be a separate LWP
  * from other events.
  */
+#if 0
 static int
-CheckHost(register struct host *host, int held)
+CheckHost(register struct host *host, int flags, void *rock)
 {
     register struct client *client;
     struct rx_connection *cb_conn = NULL;
@@ -3246,7 +3461,7 @@ CheckHost(register struct host *host, int held)
     FS_STATE_RDLOCK;
     if (fs_state.mode == FS_MODE_SHUTDOWN) {
        FS_STATE_UNLOCK;
-       return H_ENUMERATE_BAIL(held);
+       return H_ENUMERATE_BAIL(flags);
     }
     FS_STATE_UNLOCK;
 #endif
@@ -3261,8 +3476,8 @@ CheckHost(register struct host *host, int held)
     }
     if (host->LastCall < checktime) {
        h_Lock_r(host);
-       host->hostFlags |= HWHO_INPROGRESS;
        if (!(host->hostFlags & HOSTDELETED)) {
+            host->hostFlags |= HWHO_INPROGRESS;
            cb_conn = host->callback_rxcon;
            rx_GetConnection(cb_conn);
            if (host->LastCall < clientdeletetime) {
@@ -3294,7 +3509,7 @@ 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 {
@@ -3330,17 +3545,18 @@ CheckHost(register struct host *host, int held)
            rx_PutConnection(cb_conn);
            cb_conn=NULL;
            H_LOCK;
+            host->hostFlags &= ~HWHO_INPROGRESS;
        }
-       host->hostFlags &= ~HWHO_INPROGRESS;
        h_Unlock_r(host);
     }
     H_UNLOCK;
     return held;
 
 }                              /*CheckHost */
+#endif
 
 int
-CheckHost_r(register struct host *host, int held, char *dummy)
+CheckHost_r(register struct host *host, int flags, void *dummy)
 {
     register struct client *client;
     struct rx_connection *cb_conn = NULL;
@@ -3351,7 +3567,7 @@ CheckHost_r(register struct host *host, int held, char *dummy)
     FS_STATE_RDLOCK;
     if (fs_state.mode == FS_MODE_SHUTDOWN) {
        FS_STATE_UNLOCK;
-       return H_ENUMERATE_BAIL(held);
+       return H_ENUMERATE_BAIL(flags);
     }
     FS_STATE_UNLOCK;
 #endif
@@ -3397,7 +3613,7 @@ CheckHost_r(register struct host *host, int held, char *dummy)
                     * 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 {
@@ -3436,7 +3652,7 @@ CheckHost_r(register struct host *host, int held, char *dummy)
        }
        h_Unlock_r(host);
     }
-    return held;
+    return flags;
 
 }                              /*CheckHost_r */
 
@@ -3455,7 +3671,7 @@ 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
@@ -3470,9 +3686,8 @@ h_CheckHosts(void)
 
 /*
  * This is called with host locked and held. At this point, the
- * hostAddrHashTable should not have any entries for the alternate
- * interfaces. This function has to insert these entries in the
- * hostAddrHashTable.
+ * 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 interfaceAddr list are in host byte order.
  */
@@ -3590,7 +3805,8 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
         * are coming from fully connected hosts (no NAT/PATs)
         */
        interface->interface[i].port = port7001;
-        interface->interface[i].valid = 1;      /* valid until a conflict is found */
+        interface->interface[i].valid = 
+            (interf->addr_in[i] == myAddr && port7001 == myPort) ? 1 : 0;
     }
 
     interface->uuid = interf->uuid;
@@ -3598,13 +3814,15 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
     assert(!host->interface);
     host->interface = interface;
 
-    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)));
+    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)));
+       }
     }
 
     return 0;
@@ -3613,23 +3831,32 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
 /* deleted a HashChain structure for this address and host */
 /* returns 1 on success */
 int
-h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
+h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, 
+                               struct host *host)
 {
-    int flag;
+    char hoststr[16];
     register struct h_AddrHashChain **hp, *th;
 
-    for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);) {
-       assert(th->hostPtr);
-       if (th->hostPtr == host && th->addr == addr && th->port == port) {
-           *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;
 }
 
 
@@ -3646,9 +3873,9 @@ printInterfaceAddr(struct host *host, int level)
     if (host->interface) {
        /* check alternate addresses */
        number = host->interface->numberOfInterfaces;
-        if (number == 0)
+        if (number == 0) {
             ViceLog(level, ("no-addresses "));
-        else {
+       } 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)));