Revert "viced: Enable NAT ping on hosts"
[openafs.git] / src / viced / host.c
index 2d80f43..1eee625 100644 (file)
 
 #include <roken.h>
 #include <afs/opr.h>
+#include <opr/lock.h>
 
 #ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
 #endif
 
-#include <rx/xdr.h>
-#include <lwp.h>
-#include <lock.h>
 #include <afs/afsint.h>
 #define FSINT_COMMON_XG
 #include <afs/afscbint.h>
@@ -30,8 +28,6 @@
 #include <afs/nfs.h>
 #include <afs/errors.h>
 #include <afs/ihandle.h>
-#include <afs/vnode.h>
-#include <afs/volume.h>
 #include <afs/acl.h>
 #include <afs/ptclient.h>
 #include <afs/ptuser.h>
@@ -61,6 +57,7 @@ extern int LogLevel;
 extern struct afsconf_dir *confDir;    /* config dir object */
 extern int lwps;               /* the max number of server threads */
 extern afsUUID FS_HostUUID;
+extern char *FS_configPath;
 
 afsUUID nulluuid;
 int CEs = 0;                   /* active clients */
@@ -72,9 +69,18 @@ int rxcon_ident_key;
 int rxcon_client_key;
 
 static struct rx_securityClass *sc = NULL;
+static int h_quota_limit;
+
+/* arguments for PerHost_EnumerateClient enumeration */
+struct enumclient_args {
+    VolumeId vid;
+    int (*proc)(struct client *client, void *rock);
+    void *rock;
+};
 
 static void h_SetupCallbackConn_r(struct host * host);
 static int h_threadquota(int);
+static int initInterfaceAddr_r(struct host *, struct interfaceAddr *);
 
 #define CESPERBLOCK 73
 struct CEBlock {               /* block of CESPERBLOCK file entries */
@@ -204,7 +210,7 @@ GetHTBlock(void)
        ShutDownAndCore(PANIC);
     }
     for (i = 0; i < (h_HTSPERBLOCK); i++)
-       CV_INIT(&block->entry[i].cond, "block entry", CV_DEFAULT, 0);
+       opr_cv_init(&block->entry[i].cond);
     for (i = 0; i < (h_HTSPERBLOCK); i++)
        Lock_Init(&block->entry[i].lock);
     for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
@@ -259,9 +265,11 @@ hpr_Initialize(struct ubik_client **uclient)
     afs_int32 i;
     char cellstr[64];
 
-    tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
+    tdir = afsconf_Open(FS_configPath);
     if (!tdir) {
-       ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
+       ViceLog(0,
+               ("hpr_Initialize: Could not open configuration directory: %s",
+                FS_configPath));
        return -1;
     }
 
@@ -343,7 +351,7 @@ getThreadClient(struct ubik_client **client)
     if (code)
        return code;
 
-    osi_Assert(pthread_setspecific(viced_uclient_key, *client) == 0);
+    opr_Verify(pthread_setspecific(viced_uclient_key, *client) == 0);
 
     return 0;
 }
@@ -555,7 +563,7 @@ h_gethostcps_r(struct host *host, afs_int32 now)
     while (host->hostFlags & HCPS_INPROGRESS) {
        slept = 1;              /* I did sleep */
        host->hostFlags |= HCPS_WAITING;        /* I am sleeping now */
-       CV_WAIT(&host->cond, &host_glock_mutex);
+       opr_cv_wait(&host->cond, &host_glock_mutex);
     }
 
 
@@ -564,7 +572,7 @@ h_gethostcps_r(struct host *host, afs_int32 now)
        free(host->hcps.prlist_val);    /* this is for hostaclRefresh */
     host->hcps.prlist_val = NULL;
     host->hcps.prlist_len = 0;
-    host->cpsCall = slept ? (FT_ApproxTime()) : (now);
+    host->cpsCall = slept ? time(NULL) : (now);
 
     H_UNLOCK;
     code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
@@ -611,7 +619,7 @@ h_gethostcps_r(struct host *host, afs_int32 now)
     /* signal all who are waiting */
     if (host->hostFlags & HCPS_WAITING) {      /* somebody is waiting */
        host->hostFlags &= ~HCPS_WAITING;
-       CV_BROADCAST(&host->cond);
+       opr_cv_broadcast(&host->cond);
     }
 }
 
@@ -676,7 +684,7 @@ h_Alloc_r(struct rx_connection *r_con)
     /* Make a callback channel even for the console, on the off chance that it
      * makes a request that causes a break call back.  It shouldn't. */
     h_SetupCallbackConn_r(host);
-    host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
+    host->LastCall = host->cpsCall = host->ActiveCall = time(NULL);
     host->hostFlags = 0;
     host->hcps.prlist_val = NULL;
     host->hcps.prlist_len = 0;
@@ -711,7 +719,6 @@ h_SetupCallbackConn_r(struct host * host)
        rx_NewConnection(host->host, host->port, 1, sc, 0);
     rx_SetConnDeadTime(host->callback_rxcon, 50);
     rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
-    rx_SetConnSecondsUntilNatPing(host->callback_rxcon, 20);
 }
 
 /* h_Lookup_r
@@ -732,7 +739,7 @@ h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
   restart:
     for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
-       osi_Assert(host);
+       opr_Assert(host);
        if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
            && chain->port == hport) {
            if ((host->hostFlags & HWHO_INPROGRESS) &&
@@ -749,7 +756,7 @@ h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
                goto restart;
            }
            h_Unlock_r(host);
-           now = FT_ApproxTime();      /* always evaluate "now" */
+           now = time(NULL);   /* always evaluate "now" */
            if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
                /*
                 * Every hostaclRefresh period (def 2 hrs) get the new
@@ -779,7 +786,7 @@ h_LookupUuid_r(afsUUID * uuidp)
 
     for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
        host = chain->hostPtr;
-       osi_Assert(host);
+       opr_Assert(host);
        if (!(host->hostFlags & HOSTDELETED) && host->interface
            && afs_uuid_equal(&host->interface->uuid, uuidp)) {
             return host;
@@ -899,6 +906,9 @@ h_TossStuff_r(struct host *host)
            free(host->hcps.prlist_val);
        host->hcps.prlist_val = NULL;
        host->hcps.prlist_len = 0;
+       free(host->tmay_caps.Capabilities_val);
+       host->tmay_caps.Capabilities_val = NULL;
+       host->tmay_caps.Capabilities_len = 0;
        DeleteAllCallBacks_r(host, 1);
        host->hostFlags &= ~RESETDONE;  /* just to be safe */
 
@@ -1162,7 +1172,7 @@ h_DeleteHostFromUuidHashTable_r(struct host *host)
      if (LogLevel >= 125)
         afsUUID_to_string(&host->interface->uuid, uuid1, 127);
      for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
-         osi_Assert(uth->hostPtr);
+         opr_Assert(uth->hostPtr);
         if (uth->hostPtr == host) {
             ViceLog(125,
                     ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d)\n",
@@ -1193,8 +1203,8 @@ invalidateInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     struct Interface *interface;
     char hoststr[16], hoststr2[16];
 
-    osi_Assert(host);
-    osi_Assert(host->interface);
+    opr_Assert(host);
+    opr_Assert(host->interface);
 
     ViceLog(125, ("invalidateInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
                  host, afs_inet_ntoa_r(host->host, hoststr),
@@ -1347,7 +1357,7 @@ reconcileHosts_r(afs_uint32 addr, afs_uint16 port, struct host *newHost,
             AFS_PTR_FMT, afs_inet_ntoa_r(addr, hoststr), ntohs(port),
             newHost, oldHost));
 
-    osi_Assert(oldHost != newHost);
+    opr_Assert(oldHost != newHost);
     caps.Capabilities_val = NULL;
 
     if (!sc) {
@@ -1497,8 +1507,8 @@ addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     struct Interface *interface;
     char hoststr[16], hoststr2[16];
 
-    osi_Assert(host);
-    osi_Assert(host->interface);
+    opr_Assert(host);
+    opr_Assert(host->interface);
 
     /*
      * Make sure this address is on the list of known addresses
@@ -1563,8 +1573,8 @@ removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     struct Interface *interface;
     char hoststr[16], hoststr2[16];
 
-    osi_Assert(host);
-    osi_Assert(host->interface);
+    opr_Assert(host);
+    opr_Assert(host->interface);
 
     ViceLog(125, ("removeInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
                  host, afs_inet_ntoa_r(host->host, hoststr),
@@ -1594,23 +1604,216 @@ removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
     return 0;
 }
 
+/*
+ * The following few functions deal with caching TellMeAboutYourself calls
+ * that we issued to clients. Why do we do this? Well:
+ *
+ * Q: First, why do we need to issue a TMAY against a client on an incoming new
+ * Rx connection?
+ *
+ * A: We must verify that the incoming Rx connection is the same host that
+ * we have a 'host' structure for. On new calls for existing connections, we
+ * can remember which host corresponds to that connection, but for new
+ * connections, we have no way to find what host it is for, except by looking
+ * up the host by IP address. Since hosts can change IP addresses, we need to
+ * contact the IP address to see if it's the host we think it is.
+ *
+ * Q: Okay, then why cache the results?
+ *
+ * A: The TMAY calls to a single host are serialized, because they are issued
+ * with the host's host->lock held. If we get 4 Rx calls each on new
+ * connections to the same host at the same time, the 1st call will lock the
+ * host, and issue a TMAY. Once that's done, the second call will issue a
+ * TMAY, then the 3rd and then the 4th. However, the 3rd and 4th calls have
+ * been waiting to issue a TMAY since before the 2nd call even _started_ to
+ * issue a TMAY. So, we can just effectively give the results of the 2nd
+ * call's TMAY to the 3rd and 4th, too. Since it is nondeterministic which of
+ * those calls gets to issue a TMAY "first", we can just assume they all got
+ * the same result.
+ *
+ * Note that in the above example, we cannot reuse the results of the 1st TMAY
+ * for the 2nd, 3rd, and 4th calls (we only reuse the results of the 2nd).
+ * This is because by the time the 2nd call starts waiting for the host lock,
+ * it doesn't know how long the 1st TMAY has been running, so the 2nd call
+ * might indeed get a different TMAY result (though this probably would be
+ * extremely rare).
+ *
+ * Anyway, so, if we don't cache the results, we are issuing TMAYs that are
+ * pure overhead. In an environment with clients that create a lot of
+ * connections to a fileserver (keep in mind each new PAG on OpenAFS clients
+ * creates a new connection), this can mean a significant amount of overhead.
+ * So, we use this "TMAY cache" to avoid this overhead for the common case.
+ */
 
+/**
+ * Should we skip calling TellMeAboutYourself on this host, and instead rely
+ * on cached TMAY results?
+ *
+ * @param[in] host  The host we are dealing with
+ * @param[in] prewait_tmays  What the value of host->n_tmays was _before_ we
+ *                           locked 'host'
+ * @param[in] prewait_host   What the primary IP address for 'host' was before
+ *                           we locked 'host'
+ * @param[in] prewait_port   What the primary port was for 'host' before we
+ *                           locked 'host'
+ *
+ * @return Whether we should skip calling TMAY, and instead rely on cached
+ *         results in host
+ */
+static int
+ShouldSkipTMAY(struct host *host, int prewait_tmays, afs_uint32 prewait_host,
+               afs_uint16 prewait_port)
+{
+    int skiptmay = 0;
+    if (host->n_tmays > prewait_tmays + 1) {
+       /* while we were waiting for the host lock, the in-progress TMAY
+        * call finished, someone else started a new TMAY call, and that
+        * finished. so, calling TMAY again won't give us any information
+        * or additional guarantees. */
+       skiptmay = 1;
+    }
+    if (host->host != prewait_host || host->port != prewait_port) {
+       /* ...but don't skip it if the host has changed */
+       skiptmay = 0;
+    }
+    return skiptmay;
+}
 
+/**
+ * If appropriate, simulate a TellMeAboutYourself call on 'host' by extracting
+ * cached interfaceAddr and Capabilities information from 'host' itself.
+ *
+ * @param[in] host  The host we're dealing with
+ * @param[inout] askiptmay  On entering this function, this should contain the
+ *                          result of ShouldSkipTMAY. On return, it is 1 if we
+ *                          actually did use cached TMAY results, or 0 if we
+ *                          did not.
+ * @param[out] interf  The interfaceAddr result of the simulated TMAY call
+ * @param[out] caps    The Capabilities result of the simulated TMAY call
+ *
+ * @return status
+ * @retval 0 We used the cached TMAY results; do NOT make a real TMAY request
+ * @retval otherwise We did not use cached TMAY results; issue a real TMAY request
+ */
 static int
-h_threadquota(int waiting)
+SimulateTMAY(struct host *host, int *askiptmay, struct interfaceAddr *interf,
+             Capabilities *caps)
 {
-    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;
+    size_t capsize;
+
+    if (!*askiptmay) {
+       /* we're not supposed to skip the actual TMAY call */
+       return -1;
+    }
+
+    *interf = host->tmay_interf;
+
+    free(caps->Capabilities_val);
+    caps->Capabilities_val = NULL;
+    caps->Capabilities_len = 0;
+
+    if (!host->tmay_caps.Capabilities_val) {
+       return 0;
+    }
+
+    capsize = sizeof(caps->Capabilities_val[0]) * host->tmay_caps.Capabilities_len;
+
+    caps->Capabilities_val = malloc(capsize);
+    if (!caps->Capabilities_val) {
+       /* we should/did _not_ skip the real TMAY call, since we couldn't
+        * alloc memory to use the cached results */
+       *askiptmay = 0;
+       return -1;
+    }
+    caps->Capabilities_len = host->tmay_caps.Capabilities_len;
+    memcpy(caps->Capabilities_val, host->tmay_caps.Capabilities_val, capsize);
+
+    return 0;
+}
+
+/**
+ * If appropriate, store the given results from a real TellmeAboutYourself
+ * call, and cache them in the given host structure.
+ *
+ * @param[in] host  The host we're dealing with
+ * @param[in] skiptmay  1 if we skipped making a real TMAY call, 0 otherwise
+ * @param[in] didtmay  1 if we issued a successful real TMAY call, 0 otherwise
+ * @param[in] interf  The interfaceAddr result from the real TMAY call
+ * @param[in] caps    The Capabilities result from the real TMAY call
+ */
+static void
+CacheTMAY(struct host *host, int skiptmay, int didtmay,
+          struct interfaceAddr *interf, Capabilities *caps)
+{
+    size_t capsize;
+
+    if (skiptmay) {
+       /* we simulated the TMAY call, so the state of the world hasn't
+        * changed; don't touch anything */
+       return;
+    }
+    if (!didtmay) {
+       /* we did not perform a successful TMAY, so we don't have valid
+        * results to cache. blow away the existing cache so we don't use
+        * stale results */
+       goto resetcache;
+    }
+    if (host->n_tmays == INT_MAX) {
+       /* make sure int rollover doesn't screw up our ordering */
+       goto resetcache;
+    }
+    if (host->lock.num_waiting == 0) {
+       /* nobody is waiting for this host, so no reason to cache anything */
+       goto resetcache;
+    }
+
+    /* okay, if we got here, everything looks good; let's cache the given
+     * 'interf' and 'caps' */
+
+    host->tmay_interf = *interf;
+
+    if (!caps->Capabilities_val) {
+       free(host->tmay_caps.Capabilities_val);
+       host->tmay_caps.Capabilities_val = NULL;
+       host->tmay_caps.Capabilities_len = 0;
+
     } else {
-       if (waiting > 2)
-           return 1;
+       if (caps->Capabilities_len != host->tmay_caps.Capabilities_len) {
+           free(host->tmay_caps.Capabilities_val);
+           host->tmay_caps.Capabilities_val = NULL;
+           host->tmay_caps.Capabilities_len = 0;
+       }
+
+       capsize = sizeof(caps->Capabilities_val[0]) * caps->Capabilities_len;
+
+       if (!host->tmay_caps.Capabilities_val) {
+           host->tmay_caps.Capabilities_val = malloc(capsize);
+           if (!host->tmay_caps.Capabilities_val) {
+               goto resetcache;
+           }
+       }
+
+       host->tmay_caps.Capabilities_len = caps->Capabilities_len;
+       memcpy(host->tmay_caps.Capabilities_val, caps->Capabilities_val, capsize);
+    }
+    host->n_tmays++;
+    return;
+
+ resetcache:
+    /* blow away the cached TMAY data; pretend we never saw anything */
+    free(host->tmay_caps.Capabilities_val);
+    host->tmay_caps.Capabilities_val = NULL;
+    host->tmay_caps.Capabilities_len = 0;
+    memset(&host->tmay_interf, 0, sizeof(host->tmay_interf));
+
+    host->n_tmays = 0;
+}
+
+static int
+h_threadquota(int waiting)
+{
+    if (waiting > h_quota_limit) {
+       return 1;
     }
     return 0;
 }
@@ -1655,12 +1858,24 @@ h_GetHost_r(struct rx_connection *tcon)
         * structure for this address. Verify that the identity
         * of the caller matches the identity in the host structure.
         */
+
+       int didtmay = 0; /* did we make a successful TMAY call against host->host? */
+       unsigned int prewait_tmays;
+       afs_uint32 prewait_host;
+       afs_uint16 prewait_port;
+       int skiptmay;
+
        if ((host->hostFlags & HWHO_INPROGRESS) &&
            h_threadquota(host->lock.num_waiting)) {
                h_Release_r(host);
            host = NULL;
            goto gethost_out;
        }
+
+       prewait_tmays = host->n_tmays;
+       prewait_host = host->host;
+       prewait_port = host->port;
+
        h_Lock_r(host);
        if (!(host->hostFlags & ALTADDR) ||
             (host->hostFlags & HOSTDELETED)) {
@@ -1691,15 +1906,29 @@ h_GetHost_r(struct rx_connection *tcon)
 
        cb_conn = host->callback_rxcon;
        rx_GetConnection(cb_conn);
+
+       skiptmay = ShouldSkipTMAY(host, prewait_tmays, prewait_host, prewait_port);
+
        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);
+
+           if (SimulateTMAY(host, &skiptmay, &interf, &caps) == 0) {
+               /* noop; we don't need to call TellMeAboutYourself; we can
+                * trust the results from the last TMAY call */
+               code = 0;
+
+           } else {
+               code =
+                   RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
+               if (code == RXGEN_OPCODE)
+                   code = RXAFSCB_WhoAreYou(cb_conn, &interf);
+               if (code == 0) {
+                   didtmay = 1;
+               }
+           }
        } else {
             /* We do not have a match.  Create a new connection
              * for the new addr/port and use multi_Rx to probe
@@ -1710,7 +1939,6 @@ h_GetHost_r(struct rx_connection *tcon)
             cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
             rx_SetConnDeadTime(cb_in, 50);
             rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
-           rx_SetConnSecondsUntilNatPing(cb_in, 20);
 
             code =
                 RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
@@ -1720,6 +1948,9 @@ h_GetHost_r(struct rx_connection *tcon)
        rx_PutConnection(cb_conn);
        cb_conn=NULL;
        H_LOCK;
+
+       CacheTMAY(host, skiptmay, didtmay, &interf, &caps);
+
        if ((code == RXGEN_OPCODE) ||
            ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
            identP = malloc(sizeof(struct Identity));
@@ -1932,7 +2163,7 @@ h_GetHost_r(struct rx_connection *tcon)
        host = h_Alloc_r(tcon); /* returned held and locked */
        if (!host)
            goto gethost_out;
-       h_gethostcps_r(host, FT_ApproxTime());
+       h_gethostcps_r(host, time(NULL));
        if (!(host->Console & 1)) {
            int pident = 0;
            cb_conn = host->callback_rxcon;
@@ -2120,7 +2351,9 @@ h_GetHost_r(struct rx_connection *tcon)
                    /* the new host is held and locked */
                } else {
                    /* This really is a new host */
-                   h_AddHostToUuidHashTable_r(&identP->uuid, host);
+                   opr_Assert(interfValid == 1);
+                   initInterfaceAddr_r(host, &interf);
+
                    cb_conn = host->callback_rxcon;
                    rx_GetConnection(cb_conn);
                    H_UNLOCK;
@@ -2135,8 +2368,6 @@ h_GetHost_r(struct rx_connection *tcon)
                                ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
                                 host, afs_inet_ntoa_r(host->host, hoststr),
                                 ntohs(host->port)));
-                       osi_Assert(interfValid == 1);
-                       initInterfaceAddr_r(host, &interf);
                    }
                }
            }
@@ -2176,17 +2407,17 @@ h_GetHost_r(struct rx_connection *tcon)
 }                              /*h_GetHost_r */
 
 
-static char localcellname[PR_MAXNAMELEN + 1];
-
 /* not reentrant */
 void
-h_InitHostPackage(void)
+h_InitHostPackage(int hquota)
 {
+    opr_Assert(hquota > 0);
+    h_quota_limit = hquota;
+
     memset(&nulluuid, 0, sizeof(afsUUID));
-    afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
     rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
     rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
-    MUTEX_INIT(&host_glock_mutex, "host glock", MUTEX_DEFAULT, 0);
+    opr_mutex_init(&host_glock_mutex);
 }
 
 static int
@@ -2226,36 +2457,46 @@ MapName_r(char *uname, afs_int32 * aval)
 /*MapName*/
 
 
-/* NOTE: this returns the client with a Write lock and a refCount */
-struct client *
-h_ID2Client(afs_int32 vid)
+static int
+PerHost_EnumerateClient(struct host *host, void *arock)
 {
+    struct enumclient_args *args = arock;
     struct client *client;
-    struct host *host;
-    int count;
+    int code;
 
-    H_LOCK;
-    for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
-       if (host->hostFlags & HOSTDELETED)
-           continue;
-       for (client = host->FirstClient; client; client = client->next) {
-           if (!client->deleted && client->ViceId == vid) {
-               client->refCount++;
-               H_UNLOCK;
-               ObtainWriteLock(&client->lock);
-               return client;
+    for (client = host->FirstClient; client; client = client->next) {
+       if (!client->deleted && client->ViceId == args->vid) {
+
+           client->refCount++;
+           H_UNLOCK;
+
+           code = (*args->proc)(client, args->rock);
+
+           H_LOCK;
+           h_ReleaseClient_r(client);
+
+           if (code) {
+               return H_ENUMERATE_BAIL(0);
            }
        }
     }
-    if (count != hostCount) {
-       ViceLog(0, ("h_ID2Client found %d of %d hosts\n", count, hostCount));
-    } else if (host != NULL) {
-       ViceLog(0, ("h_ID2Client found more than %d hosts\n", hostCount));
-       ShutDownAndCore(PANIC);
-    }
 
+    return 0;
+}
+
+void
+h_EnumerateClients(VolumeId vid,
+                   int (*proc)(struct client *client, void *rock),
+                   void *rock)
+{
+    struct enumclient_args args;
+    args.vid = vid;
+    args.proc = proc;
+    args.rock = rock;
+
+    H_LOCK;
+    h_Enumerate_r(PerHost_EnumerateClient, hostList, &args);
     H_UNLOCK;
-    return NULL;
 }
 
 static int
@@ -2363,9 +2604,12 @@ getPeerDetails(struct rx_connection *conn,
  *
  * The refCount on client->host is returned incremented.  h_ReleaseClient_r
  * does not decrement the refCount on client->host.
+ *
+ * *a_viceid is set to the user's ViceId, even if we don't return a client
+ * struct.
  */
 struct client *
-h_FindClient_r(struct rx_connection *tcon)
+h_FindClient_r(struct rx_connection *tcon, afs_int32 *a_viceid)
 {
     struct client *client;
     struct host *host = NULL;
@@ -2384,6 +2628,9 @@ h_FindClient_r(struct rx_connection *tcon)
        && !(client->host->hostFlags & HOSTDELETED)
        && !client->deleted) {
 
+       if (a_viceid) {
+           *a_viceid = client->ViceId;
+       }
        client->refCount++;
        h_Hold_r(client->host);
        if (client->prfail != 2) {
@@ -2408,6 +2655,10 @@ h_FindClient_r(struct rx_connection *tcon)
     if (code)
        fail = 1;
 
+    if (a_viceid) {
+       *a_viceid = viceid;
+    }
+
     if (!client) { /* loop */
        host = h_GetHost_r(tcon);       /* Returns with incremented refCount  */
 
@@ -2600,7 +2851,7 @@ h_FindClient_r(struct rx_connection *tcon)
 int
 h_ReleaseClient_r(struct client *client)
 {
-    osi_Assert(client->refCount > 0);
+    opr_Assert(client->refCount > 0);
     client->refCount--;
     return 0;
 }
@@ -2793,7 +3044,7 @@ h_PrintClients(void)
                 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
        return;
     }
-    now = FT_ApproxTime();
+    now = time(NULL);
     strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
             localtime_r(&now, &tm));
     snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n\n",
@@ -2867,7 +3118,7 @@ h_DumpHosts(void)
                 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
        return;
     }
-    now = FT_ApproxTime();
+    now = time(NULL);
     strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
             localtime_r(&now, &tm));
     snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n\n", tbuffer);
@@ -3148,7 +3399,7 @@ h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
 static int
 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
 {
-    int ret = 0, found = 0;
+    int ret = 0;
     struct host *host = NULL;
     struct h_UuidHashChain *chain;
     afsUUID * uuidp = &h->interface->uuid;
@@ -3171,7 +3422,6 @@ h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
                            h->index, host->index));
                state->flags.warnings_generated = 1;
            }
-           found = 1;
            goto done;
        }
        if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
@@ -3183,16 +3433,16 @@ h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
        chain_len++;
     }
 
-    if (!found) {
-       afsUUID_to_string(uuidp, tmp, sizeof(tmp));
-       if (state->mode == FS_STATE_LOAD_MODE) {
-           ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
-           ret = 1;
-           goto done;
-       } else {
-           ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
-           state->flags.warnings_generated = 1;
-       }
+    /* Fall through, so host not found */
+
+    afsUUID_to_string(uuidp, tmp, sizeof(tmp));
+    if (state->mode == FS_STATE_LOAD_MODE) {
+       ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
+       ret = 1;
+       goto done;
+    } else {
+       ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
+       state->flags.warnings_generated = 1;
     }
 
  done:
@@ -3269,7 +3519,7 @@ h_stateSaveHost(struct host * host, void* rock)
        if_len = sizeof(struct Interface) +
            ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
        ifp = malloc(if_len);
-       osi_Assert(ifp != NULL);
+       opr_Assert(ifp != NULL);
        memcpy(ifp, host->interface, if_len);
        hdr.interfaces = host->interface->numberOfInterfaces;
        iov[iovcnt].iov_base = (char *) ifp;
@@ -3280,7 +3530,7 @@ h_stateSaveHost(struct host * host, void* rock)
        hdr.hcps = host->hcps.prlist_len;
        hcps_len = hdr.hcps * sizeof(afs_int32);
        hcps = malloc(hcps_len);
-       osi_Assert(hcps != NULL);
+       opr_Assert(hcps != NULL);
        memcpy(hcps, host->hcps.prlist_val, hcps_len);
        iov[iovcnt].iov_base = (char *) hcps;
        iov[iovcnt].iov_len = hcps_len;
@@ -3352,7 +3602,7 @@ h_stateRestoreHost(struct fs_dump_state * state)
        ifp_len = sizeof(struct Interface) +
            ((hdr.interfaces-1) * sizeof(struct AddrPort));
        ifp = malloc(ifp_len);
-       osi_Assert(ifp != NULL);
+       opr_Assert(ifp != NULL);
        iov[iovcnt].iov_base = (char *) ifp;
        iov[iovcnt].iov_len = ifp_len;
        iovcnt++;
@@ -3360,7 +3610,7 @@ h_stateRestoreHost(struct fs_dump_state * state)
     if (hdr.hcps) {
        hcps_len = hdr.hcps * sizeof(afs_int32);
        hcps = malloc(hcps_len);
-       osi_Assert(hcps != NULL);
+       opr_Assert(hcps != NULL);
        iov[iovcnt].iov_base = (char *) hcps;
        iov[iovcnt].iov_len = hcps_len;
        iovcnt++;
@@ -3381,7 +3631,7 @@ h_stateRestoreHost(struct fs_dump_state * state)
     if (!hdr.hcps && hdsk.hcps_valid) {
        /* valid, zero-length host cps ; does this ever happen? */
        hcps = malloc(sizeof(afs_int32));
-       osi_Assert(hcps != NULL);
+       opr_Assert(hcps != NULL);
     }
 
     if ((hdsk.hostFlags & HWHO_INPROGRESS) || !(hdsk.hostFlags & ALTADDR)) {
@@ -3396,7 +3646,7 @@ h_stateRestoreHost(struct fs_dump_state * state)
 
     /* for restoring state, we better be able to get a host! */
     host = GetHT();
-    osi_Assert(host != NULL);
+    opr_Assert(host != NULL);
 
     if (ifp) {
        host->interface = ifp;
@@ -3741,112 +3991,6 @@ static struct AFSFid zerofid;
  * Since it can serialize them, and pile up, it should be a separate LWP
  * from other events.
  */
-#if 0
-static int
-CheckHost(struct host *host, int flags, void *rock)
-{
-    struct client *client;
-    struct rx_connection *cb_conn = NULL;
-    int code;
-
-#ifdef AFS_DEMAND_ATTACH_FS
-    /* kill the checkhost lwp ASAP during shutdown */
-    FS_STATE_RDLOCK;
-    if (fs_state.mode == FS_MODE_SHUTDOWN) {
-       FS_STATE_UNLOCK;
-       return H_ENUMERATE_BAIL(flags);
-    }
-    FS_STATE_UNLOCK;
-#endif
-
-    /* Host is held by h_Enumerate */
-    H_LOCK;
-    for (client = host->FirstClient; client; client = client->next) {
-       if (client->refCount == 0 && client->LastCall < clientdeletetime) {
-           client->deleted = 1;
-           host->hostFlags |= CLIENTDELETED;
-       }
-    }
-    if (host->LastCall < checktime) {
-       h_Lock_r(host);
-       if (!(host->hostFlags & HOSTDELETED)) {
-            host->hostFlags |= HWHO_INPROGRESS;
-           cb_conn = host->callback_rxcon;
-           rx_GetConnection(cb_conn);
-           if (host->LastCall < clientdeletetime) {
-               host->hostFlags |= HOSTDELETED;
-               if (!(host->hostFlags & VENUSDOWN)) {
-                   host->hostFlags &= ~ALTADDR;        /* alternate address invalid */
-                   if (host->interface) {
-                       H_UNLOCK;
-                       code =
-                           RXAFSCB_InitCallBackState3(cb_conn,
-                                                      &FS_HostUUID);
-                       H_LOCK;
-                   } else {
-                       H_UNLOCK;
-                       code =
-                           RXAFSCB_InitCallBackState(cb_conn);
-                       H_LOCK;
-                   }
-                   host->hostFlags |= ALTADDR; /* alternate addresses valid */
-                   if (code) {
-                       char hoststr[16];
-                       (void)afs_inet_ntoa_r(host->host, hoststr);
-                       ViceLog(0,
-                               ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
-                                hoststr, ntohs(host->port)));
-                       host->hostFlags |= VENUSDOWN;
-                   }
-                   /* Note:  it's safe to delete hosts even if they have call
-                    * back state, because break delayed callbacks (called when a
-                    * message is received from the workstation) will always send a
-                    * break all call backs to the workstation if there is no
-                    * callback.
-                    */
-               }
-           } else {
-               if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
-                   char hoststr[16];
-                   (void)afs_inet_ntoa_r(host->host, hoststr);
-                   if (host->interface) {
-                       afsUUID uuid = host->interface->uuid;
-                       H_UNLOCK;
-                       code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
-                       H_LOCK;
-                       if (code) {
-                           if (MultiProbeAlternateAddress_r(host)) {
-                               ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
-                                           hoststr, ntohs(host->port), code));
-                               host->hostFlags |= VENUSDOWN;
-                           }
-                       }
-                   } else {
-                       H_UNLOCK;
-                       code = RXAFSCB_Probe(cb_conn);
-                       H_LOCK;
-                       if (code) {
-                           ViceLog(0,
-                                   ("CheckHost: Probe failed for host %s:%d, code %d\n",
-                                    hoststr, ntohs(host->port), code));
-                           host->hostFlags |= VENUSDOWN;
-                       }
-                   }
-               }
-           }
-           H_UNLOCK;
-           rx_PutConnection(cb_conn);
-           cb_conn=NULL;
-           H_LOCK;
-            host->hostFlags &= ~HWHO_INPROGRESS;
-       }
-       h_Unlock_r(host);
-    }
-    H_UNLOCK;
-    return held;
-
-}                              /*CheckHost */
-#endif
 
 int
 CheckHost_r(struct host *host, void *dummy)
@@ -3964,7 +4108,7 @@ CheckHost_r(struct host *host, void *dummy)
 void
 h_CheckHosts(void)
 {
-    afs_uint32 now = FT_ApproxTime();
+    afs_uint32 now = time(NULL);
 
     memset(&zerofid, 0, sizeof(zerofid));
     /*
@@ -3986,7 +4130,7 @@ h_CheckHosts(void)
  *
  * The addresses in the interfaceAddr list are in host byte order.
  */
-int
+static int
 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
 {
     int i, j;
@@ -3999,8 +4143,8 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
     char uuidstr[128];
     afs_uint16 port7001 = htons(7001);
 
-    osi_Assert(host);
-    osi_Assert(interf);
+    opr_Assert(host);
+    opr_Assert(interf);
 
     number = interf->numberOfInterfaces;
     myAddr = host->host;       /* current interface address */
@@ -4106,9 +4250,11 @@ initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
 
     interface->uuid = interf->uuid;
 
-    osi_Assert(!host->interface);
+    opr_Assert(!host->interface);
     host->interface = interface;
 
+    h_AddHostToUuidHashTable_r(&interface->uuid, host);
+
     if (LogLevel >= 125) {
        afsUUID_to_string(&interface->uuid, uuidstr, 127);
 
@@ -4137,7 +4283,7 @@ h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port,
 
     for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);
         hp = &th->next) {
-        osi_Assert(th->hostPtr);
+        opr_Assert(th->hostPtr);
         if (th->hostPtr == host && th->addr == addr && th->port == port) {
            ViceLog(125, ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d)\n",
                          host, afs_inet_ntoa_r(host->host, hoststr),