Don't allocate objects of 0 length
[openafs.git] / src / viced / callback.c
index 0a8075a..5460702 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
@@ -12,7 +12,7 @@
 /*
  * NEW callback package callback.c (replaces vicecb.c)
  * Updated call back routines, NOW with:
- * 
+ *
  *     Faster DeleteVenus (Now called DeleteAllCallBacks)
  *     Call back breaking for volumes
  *     Adaptive timeouts on call backs
  *     nblocks must be < 65536
  *     Space used is nblocks*16 bytes
  *     Note that space will be reclaimed by breaking callbacks of old hosts
- * 
+ *
  * time = AddCallBack(host, fid)
  *     Add a call back.
  *     Returns the expiration time at the workstation.
- * 
+ *
  * BreakCallBack(host, fid)
  *     Break all call backs for fid, except for the specified host.
  *     Delete all of them.
- * 
- * BreakVolumeCallBacks(volume)
+ *
+ * BreakVolumeCallBacksLater(volume)
  *     Break all call backs on volume, using single call to each host
  *     Delete all the call backs.
- * 
+ *
  * DeleteCallBack(host,fid)
  *     Delete (do not break) single call back for fid.
- * 
+ *
  * DeleteFileCallBacks(fid)
  *     Delete (do not break) all call backs for fid.
  *
  * DeleteAllCallBacks(host)
  *     Delete (do not break) all call backs for host.
- * 
+ *
  * CleanupTimedOutCallBacks()
  *     Delete all timed out call back entries
  *     Must be called periodically by file server.
- * 
+ *
  * BreakDelayedCallBacks(host)
  *     Break all delayed call backs for host.
  *     Returns 1: one or more failed, 0: success.
- * 
+ *
  * PrintCallBackStats()
  *     Print statistics about call backs to stdout.
- * 
+ *
  * DumpCallBacks() ---wishful thinking---
  *     Dump call back state to /tmp/callback.state.
  *     This is separately interpretable by the program pcb.
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
-#include <stdio.h>
-#include <stdlib.h>            /* for malloc() */
-#include <time.h>              /* ANSI standard location for time stuff */
-#ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#include <io.h>
-#else
-#include <sys/time.h>
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
 #endif
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-#include <afs/assert.h>
-
-#include <afs/stds.h>
 
+#include <afs/opr.h>
+#include <opr/lock.h>
 #include <afs/nfs.h>           /* yuck.  This is an abomination. */
-#include <lwp.h>
 #include <rx/rx.h>
-#include <afscbint.h>
+#include <rx/rx_queue.h>
+#include <afs/afscbint.h>
 #include <afs/afsutil.h>
-#include <lock.h>
 #include <afs/ihandle.h>
+#include <afs/partition.h>
 #include <afs/vnode.h>
 #include <afs/volume.h>
 #include "viced_prototypes.h"
@@ -124,13 +109,16 @@ RCSID
 #include "host.h"
 #include "callback.h"
 #ifdef AFS_DEMAND_ATTACH_FS
-#include "../tviced/serialize_state.h"
+#include "serialize_state.h"
 #endif /* AFS_DEMAND_ATTACH_FS */
 
 
 extern afsUUID FS_HostUUID;
 extern int hostCount;
+
+#ifndef INTERPRET_DUMP
 static int ShowProblems = 1;
+#endif
 
 struct cbcounters cbstuff;
 
@@ -159,7 +147,9 @@ static int TimeOuts[] = {
 };                             /* Anything more: MinTimeOut */
 
 /* minimum time given for a call back */
+#ifndef INTERPRET_DUMP
 static int MinTimeOut = (7 * 60);
+#endif
 
 /* Heads of CB queues; a timeout index is 1+index into this array */
 static afs_uint32 timeout[CB_NUM_TIMEOUT_QUEUES];
@@ -173,32 +163,33 @@ struct object {
 };
 
 /* Prototypes for static routines */
-static struct FileEntry *FindFE(register AFSFid * fid);
-static struct CallBack *iGetCB(register int *nused);
-static int iFreeCB(register struct CallBack *cb, register int *nused);
-static struct FileEntry *iGetFE(register int *nused);
-static int iFreeFE(register struct FileEntry *fe, register int *nused);
-static int TAdd(register struct CallBack *cb, register afs_uint32 * thead);
-static int TDel(register struct CallBack *cb);
-static int HAdd(register struct CallBack *cb, register struct host *host);
-static int HDel(register struct CallBack *cb);
+static struct FileEntry *FindFE(AFSFid * fid);
+
+#ifndef INTERPRET_DUMP
+static struct CallBack *iGetCB(int *nused);
+static int iFreeCB(struct CallBack *cb, int *nused);
+static struct FileEntry *iGetFE(int *nused);
+static int iFreeFE(struct FileEntry *fe, int *nused);
+static int TAdd(struct CallBack *cb, afs_uint32 * thead);
+static int TDel(struct CallBack *cb);
+static int HAdd(struct CallBack *cb, struct host *host);
+static int HDel(struct CallBack *cb);
 static int CDel(struct CallBack *cb, int deletefe);
-static int CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
+static int CDelPtr(struct FileEntry *fe, afs_uint32 * cbp,
                   int deletefe);
 static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host);
-static int FDel(register struct FileEntry *fe);
+static int FDel(struct FileEntry *fe);
 static int AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead,
                          int type, int locked);
 static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
-                                struct AFSCBFids *afidp, struct host *xhost);
-static int MultiBreakVolumeCallBack_r(struct host *host, int isheld,
+                                struct AFSCBFids *afidp);
+static int MultiBreakVolumeCallBack_r(struct host *host,
                                      struct VCBParams *parms, int deletefe);
-static int MultiBreakVolumeCallBack(struct host *host, int isheld,
-                                   struct VCBParams *parms);
-static int MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
-                                        struct VCBParams *parms);
+static int MultiBreakVolumeLaterCallBack(struct host *host, void *rock);
 static int GetSomeSpace_r(struct host *hostp, int locked);
 static int ClearHostCallbacks_r(struct host *hp, int locked);
+static int DumpCallBackState_r(void);
+#endif
 
 #define GetCB() ((struct CallBack *)iGetCB(&cbstuff.nCBs))
 #define GetFE() ((struct FileEntry *)iGetFE(&cbstuff.nFEs))
@@ -207,16 +198,16 @@ static int ClearHostCallbacks_r(struct host *hp, int locked);
 
 
 /* Other protos - move out sometime */
-void PrintCB(register struct CallBack *cb, afs_uint32 now);
+void PrintCB(struct CallBack *cb, afs_uint32 now);
 
 static afs_uint32 HashTable[FEHASH_SIZE];      /* File entry hash table */
 
 static struct FileEntry *
-FindFE(register AFSFid * fid)
+FindFE(AFSFid * fid)
 {
     int hash;
-    register int fei;
-    register struct FileEntry *fe;
+    int fei;
+    struct FileEntry *fe;
 
     hash = FEHash(fid->Volume, fid->Unique);
     for (fei = HashTable[hash]; fei; fei = fe->fnext) {
@@ -231,9 +222,9 @@ FindFE(register AFSFid * fid)
 #ifndef INTERPRET_DUMP
 
 static struct CallBack *
-iGetCB(register int *nused)
+iGetCB(int *nused)
 {
-    register struct CallBack *ret;
+    struct CallBack *ret;
 
     if ((ret = CBfree)) {
        CBfree = (struct CallBack *)(((struct object *)ret)->next);
@@ -243,7 +234,7 @@ iGetCB(register int *nused)
 }
 
 static int
-iFreeCB(register struct CallBack *cb, register int *nused)
+iFreeCB(struct CallBack *cb, int *nused)
 {
     ((struct object *)cb)->next = (struct object *)CBfree;
     CBfree = cb;
@@ -252,9 +243,9 @@ iFreeCB(register struct CallBack *cb, register int *nused)
 }
 
 static struct FileEntry *
-iGetFE(register int *nused)
+iGetFE(int *nused)
 {
-    register struct FileEntry *ret;
+    struct FileEntry *ret;
 
     if ((ret = FEfree)) {
        FEfree = (struct FileEntry *)(((struct object *)ret)->next);
@@ -264,7 +255,7 @@ iGetFE(register int *nused)
 }
 
 static int
-iFreeFE(register struct FileEntry *fe, register int *nused)
+iFreeFE(struct FileEntry *fe, int *nused)
 {
     ((struct object *)fe)->next = (struct object *)FEfree;
     FEfree = fe;
@@ -274,12 +265,12 @@ iFreeFE(register struct FileEntry *fe, register int *nused)
 
 /* Add cb to end of specified timeout list */
 static int
-TAdd(register struct CallBack *cb, register afs_uint32 * thead)
+TAdd(struct CallBack *cb, afs_uint32 * thead)
 {
     if (!*thead) {
        (*thead) = cb->tnext = cb->tprev = cbtoi(cb);
     } else {
-       register struct CallBack *thp = itocb(*thead);
+       struct CallBack *thp = itocb(*thead);
 
        cb->tprev = thp->tprev;
        cb->tnext = *thead;
@@ -296,9 +287,9 @@ TAdd(register struct CallBack *cb, register afs_uint32 * thead)
 
 /* Delete call back entry from timeout list */
 static int
-TDel(register struct CallBack *cb)
+TDel(struct CallBack *cb)
 {
-    register afs_uint32 *thead = itot(cb->thead);
+    afs_uint32 *thead = itot(cb->thead);
 
     if (*thead == cbtoi(cb))
        *thead = (*thead == cb->tnext ? 0 : cb->tnext);
@@ -311,13 +302,13 @@ TDel(register struct CallBack *cb)
 
 /* Add cb to end of specified host list */
 static int
-HAdd(register struct CallBack *cb, register struct host *host)
+HAdd(struct CallBack *cb, struct host *host)
 {
     cb->hhead = h_htoi(host);
     if (!host->cblist) {
        host->cblist = cb->hnext = cb->hprev = cbtoi(cb);
     } else {
-       register struct CallBack *fcb = itocb(host->cblist);
+       struct CallBack *fcb = itocb(host->cblist);
 
        cb->hprev = fcb->hprev;
        cb->hnext = cbtoi(fcb);
@@ -328,9 +319,9 @@ HAdd(register struct CallBack *cb, register struct host *host)
 
 /* Delete call back entry from host list */
 static int
-HDel(register struct CallBack *cb)
+HDel(struct CallBack *cb)
 {
-    register afs_uint32 *hhead = &h_itoh(cb->hhead)->cblist;
+    afs_uint32 *hhead = &h_itoh(cb->hhead)->cblist;
 
     if (*hhead == cbtoi(cb))
        *hhead = (*hhead == cb->hnext ? 0 : cb->hnext);
@@ -348,17 +339,16 @@ CDel(struct CallBack *cb, int deletefe)
 {
     int cbi = cbtoi(cb);
     struct FileEntry *fe = itofe(cb->fhead);
-    register afs_uint32 *cbp;
-    register int safety;
+    afs_uint32 *cbp;
+    int safety;
 
     for (safety = 0, cbp = &fe->firstcb; *cbp && *cbp != cbi;
         cbp = &itocb(*cbp)->cnext, safety++) {
        if (safety > cbstuff.nblks + 10) {
-           assert(0);
-           ViceLog(0,
-                   ("CDel: Internal Error -- shutting down: wanted %d from %d, now at %d\n",
-                    cbi, fe->firstcb, *cbp));
-           DumpCallBackState();
+           ViceLogThenPanic(0, ("CDel: Internal Error -- shutting down: "
+                                "wanted %d from %d, now at %d\n",
+                                cbi, fe->firstcb, *cbp));
+           DumpCallBackState_r();
            ShutDownAndCore(PANIC);
        }
     }
@@ -374,10 +364,10 @@ CDel(struct CallBack *cb, int deletefe)
 static int Ccdelpt = 0, CcdelB = 0;
 
 static int
-CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
+CDelPtr(struct FileEntry *fe, afs_uint32 * cbp,
        int deletefe)
 {
-    register struct CallBack *cb;
+    struct CallBack *cb;
 
     if (!*cbp)
        return 0;
@@ -387,7 +377,7 @@ CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
        CcdelB++;
     *cbp = cb->cnext;
     FreeCB(cb);
-    if (deletefe && (--fe->ncbs == 0))
+    if ((--fe->ncbs == 0) && deletefe)
        FDel(fe);
     return 0;
 }
@@ -395,15 +385,15 @@ CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
 static afs_uint32 *
 FindCBPtr(struct FileEntry *fe, struct host *host)
 {
-    register afs_uint32 hostindex = h_htoi(host);
-    register struct CallBack *cb;
-    register afs_uint32 *cbp;
-    register int safety;
+    afs_uint32 hostindex = h_htoi(host);
+    struct CallBack *cb;
+    afs_uint32 *cbp;
+    int safety;
 
     for (safety = 0, cbp = &fe->firstcb; *cbp; cbp = &cb->cnext, safety++) {
        if (safety > cbstuff.nblks) {
            ViceLog(0, ("FindCBPtr: Internal Error -- shutting down.\n"));
-           DumpCallBackState();
+           DumpCallBackState_r();
            ShutDownAndCore(PANIC);
        }
        cb = itocb(*cbp);
@@ -415,14 +405,14 @@ FindCBPtr(struct FileEntry *fe, struct host *host)
 
 /* Delete file entry from hash table */
 static int
-FDel(register struct FileEntry *fe)
+FDel(struct FileEntry *fe)
 {
-    register int fei = fetoi(fe);
-    register afs_uint32 *p = &HashTable[FEHash(fe->volid, fe->unique)];
+    int fei = fetoi(fe);
+    afs_uint32 *p = &HashTable[FEHash(fe->volid, fe->unique)];
 
     while (*p && *p != fei)
        p = &itofe(*p)->fnext;
-    assert(*p);
+    opr_Assert(*p);
     *p = fe->fnext;
     FreeFE(fe);
     return 0;
@@ -432,23 +422,23 @@ FDel(register struct FileEntry *fe)
 int
 InitCallBack(int nblks)
 {
+    opr_Assert(nblks > 0);
+
     H_LOCK;
-    tfirst = CBtime(FT_ApproxTime());
+    tfirst = CBtime(time(NULL));
     /* N.B. The "-1", below, is because
      * FE[0] and CB[0] are not used--and not allocated */
-    FE = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry))));
+    FE = calloc(nblks, sizeof(struct FileEntry));
     if (!FE) {
-       ViceLog(0, ("Failed malloc in InitCallBack\n"));
-       assert(0);
+       ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
     }
     FE--;  /* FE[0] is supposed to point to junk */
     cbstuff.nFEs = nblks;
     while (cbstuff.nFEs)
        FreeFE(&FE[cbstuff.nFEs]);      /* This is correct */
-    CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))));
+    CB = calloc(nblks, sizeof(struct CallBack));
     if (!CB) {
-       ViceLog(0, ("Failed malloc in InitCallBack\n"));
-       assert(0);
+       ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
     }
     CB--;  /* CB[0] is supposed to point to junk */
     cbstuff.nCBs = nblks;
@@ -464,17 +454,15 @@ afs_int32
 XCallBackBulk_r(struct host * ahost, struct AFSFid * fids, afs_int32 nfids)
 {
     struct AFSCallBack tcbs[AFSCBMAX];
-    register int i;
+    int i;
     struct AFSCBFids tf;
     struct AFSCBs tc;
     int code;
     int j;
     struct rx_connection *cb_conn = NULL;
 
-#ifdef ADAPT_MTU
     rx_SetConnDeadTime(ahost->callback_rxcon, 4);
     rx_SetConnHardDeadTime(ahost->callback_rxcon, AFS_HARDDEADTIME);
-#endif
 
     code = 0;
     j = 0;
@@ -504,7 +492,7 @@ XCallBackBulk_r(struct host * ahost, struct AFSFid * fids, afs_int32 nfids)
     return code;
 }
 
-/* the locked flag tells us if the host entry has already been locked 
+/* the locked flag tells us if the host entry has already been locked
  * by our parent.  I don't think anybody actually calls us with the
  * host locked, but here's how to make that work:  GetSomeSpace has to
  * change so that it doesn't attempt to lock any hosts < "host".  That
@@ -517,12 +505,13 @@ int
 AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
             int locked)
 {
-    int retVal;
+    int retVal = 0;
     H_LOCK;
     if (!locked) {
        h_Lock_r(host);
     }
-    retVal = AddCallBack1_r(host, fid, thead, type, 1);
+    if (!(host->hostFlags & HOSTDELETED))
+        retVal = AddCallBack1_r(host, fid, thead, type, 1);
 
     if (!locked) {
        h_Unlock_r(host);
@@ -538,15 +527,17 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
     struct FileEntry *fe;
     struct CallBack *cb = 0, *lastcb = 0;
     struct FileEntry *newfe = 0;
-    afs_uint32 time_out;
+    afs_uint32 time_out = 0;
     afs_uint32 *Thead = thead;
     struct CallBack *newcb = 0;
     int safety;
 
+    cbstuff.AddCallBacks++;
+
     host->Console |= 2;
 
     /* allocate these guys first, since we can't call the allocator with
-     * the host structure locked -- or we might deadlock. However, we have 
+     * the host structure locked -- or we might deadlock. However, we have
      * to avoid races with FindFE... */
     while (!(newcb = GetCB())) {
        GetSomeSpace_r(host, locked);
@@ -561,23 +552,28 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
     if (!locked) {
        h_Lock_r(host);         /* this can yield, so do it before we get any */
        /* fragile info */
+        if (host->hostFlags & HOSTDELETED) {
+            host->Console &= ~2;
+            h_Unlock_r(host);
+            return 0;
+        }
     }
 
     fe = FindFE(fid);
     if (type == CB_NORMAL) {
        time_out =
-           TimeCeiling(FT_ApproxTime() + TimeOut(fe ? fe->ncbs : 0) +
+           TimeCeiling(time(NULL) + TimeOut(fe ? fe->ncbs : 0) +
                        ServerBias);
        Thead = THead(CBtime(time_out));
     } else if (type == CB_VOLUME) {
-       time_out = TimeCeiling((60 * 120 + FT_ApproxTime()) + ServerBias);
+       time_out = TimeCeiling((60 * 120 + time(NULL)) + ServerBias);
        Thead = THead(CBtime(time_out));
     } else if (type == CB_BULK) {
        /* bulk status can get so many callbacks all at once, and most of them
         * are probably not for things that will be used for long.
         */
        time_out =
-           TimeCeiling(FT_ApproxTime() + ServerBias +
+           TimeCeiling(time(NULL) + ServerBias +
                        TimeOut(22 + (fe ? fe->ncbs : 0)));
        Thead = THead(CBtime(time_out));
     }
@@ -585,7 +581,7 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
     host->Console &= ~2;
 
     if (!fe) {
-       register afs_uint32 hash;
+       afs_uint32 hash;
 
        fe = newfe;
        newfe = NULL;
@@ -603,7 +599,7 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
         lastcb = cb, cb = itocb(cb->cnext), safety++) {
        if (safety > cbstuff.nblks) {
            ViceLog(0, ("AddCallBack1: Internal Error -- shutting down.\n"));
-           DumpCallBackState();
+           DumpCallBackState_r();
            ShutDownAndCore(PANIC);
        }
        if (cb->hhead == h_htoi(host))
@@ -650,19 +646,27 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
     return 0;
 }
 
-/* Take an array full of hosts, all held.  Break callbacks to them, and 
- * release the holds once you're done, except don't release xhost.  xhost 
- * may be NULL.  Currently only works for a single Fid in afidp array.
+static int
+CompareCBA(const void *e1, const void *e2)
+{
+    const struct cbstruct *cba1 = (const struct cbstruct *)e1;
+    const struct cbstruct *cba2 = (const struct cbstruct *)e2;
+    return ((cba1->hp)->index - (cba2->hp)->index);
+}
+
+/* Take an array full of hosts, all held.  Break callbacks to them, and
+ * release the holds once you're done.
+ * Currently only works for a single Fid in afidp array.
  * If you want to make this work with multiple fids, you need to fix
  * the error handling.  One approach would be to force a reset if a
  * multi-fid call fails, or you could add delayed callbacks for each
  * fid.   You probably also need to sort and remove duplicate hosts.
- * When this is called from the BreakVolumeCallBacks path, it does NOT 
- * force a reset if the RPC fails, it just marks the host down and tries 
+ * When this is called from the BreakVolumeCallBacks path, it does NOT
+ * force a reset if the RPC fails, it just marks the host down and tries
  * to create a delayed callback. */
 /* N.B.  be sure that code works when ncbas == 0 */
 /* N.B.  requires all the cba[*].hp pointers to be valid... */
-/* This routine does not hold a lock on the host for the duration of 
+/* This routine does not hold a lock on the host for the duration of
  * the BreakCallBack RPC, which is a significant deviation from tradition.
  * It _does_ get a lock on the host before setting VenusDown = 1,
  * which is sufficient only if VenusDown = 0 only happens when the
@@ -670,14 +674,17 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
  * wherever that is done. */
 static void
 MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
-                    struct AFSCBFids *afidp, struct host *xhost)
+                    struct AFSCBFids *afidp)
 {
     int i, j;
     struct rx_connection *conns[MAX_CB_HOSTS];
     static struct AFSCBs tc = { 0, 0 };
     int multi_to_cba_map[MAX_CB_HOSTS];
 
-    assert(ncbas <= MAX_CB_HOSTS);
+    opr_Assert(ncbas <= MAX_CB_HOSTS);
+
+    /* sort cba list to avoid makecall issues */
+    qsort(cba, ncbas, sizeof(struct cbstruct), CompareCBA);
 
     /* set up conns for multi-call */
     for (i = 0, j = 0; i < ncbas; i++) {
@@ -689,10 +696,8 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
        multi_to_cba_map[j] = i;
        conns[j++] = thishost->callback_rxcon;
 
-#ifdef ADAPT_MTU
        rx_SetConnDeadTime(thishost->callback_rxcon, 4);
        rx_SetConnHardDeadTime(thishost->callback_rxcon, AFS_HARDDEADTIME);
-#endif
     }
 
     if (j) {                   /* who knows what multi would do with 0 conns? */
@@ -711,16 +716,17 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
 
                if (!hp || !idx) {
                    ViceLog(0,
-                           ("BCB: INTERNAL ERROR: hp=%x, cba=%x, thead=%u\n", 
+                           ("BCB: INTERNAL ERROR: hp=%p, cba=%p, thead=%u\n",
                             hp, cba, idx));
                } else {
-                   /* 
+                   /*
                     ** try breaking callbacks on alternate interface addresses
                     */
                    if (MultiBreakCallBackAlternateAddress(hp, afidp)) {
                        if (ShowProblems) {
                            ViceLog(7,
-                                   ("BCB: Failed on file %u.%u.%u, Host %x (%s:%d) is down\n",
+                                   ("BCB: Failed on file %u.%u.%u, "
+                                    "Host %p (%s:%d) is down\n",
                                     afidp->AFSCBFids_val->Volume,
                                     afidp->AFSCBFids_val->Vnode,
                                     afidp->AFSCBFids_val->Unique,
@@ -730,14 +736,16 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
                        }
 
                        H_LOCK;
-                       h_Lock_r(hp); 
-                       hp->hostFlags |= VENUSDOWN;
-               /**
-                 * We always go into AddCallBack1_r with the host locked
-                 */
-                       AddCallBack1_r(hp, afidp->AFSCBFids_val, itot(idx),
-                                      CB_DELAYED, 1);
-                       h_Unlock_r(hp); 
+                       h_Lock_r(hp);
+                        if (!(hp->hostFlags & HOSTDELETED)) {
+                            hp->hostFlags |= VENUSDOWN;
+                            /**
+                             * We always go into AddCallBack1_r with the host locked
+                             */
+                            AddCallBack1_r(hp, afidp->AFSCBFids_val, itot(idx),
+                                           CB_DELAYED, 1);
+                        }
+                       h_Unlock_r(hp);
                        H_UNLOCK;
                    }
                }
@@ -751,7 +759,7 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
     for (i = 0; i < ncbas; i++) {
        struct host *hp;
        hp = cba[i].hp;
-       if (hp && xhost != hp) {
+       if (hp) {
            h_Release_r(hp);
        }
     }
@@ -773,10 +781,10 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
  * Break all call backs for fid, except for the specified host (unless flag
  * is true, in which case all get a callback message. Assumption: the specified
  * host is h_Held, by the caller; the others aren't.
- * Specified host may be bogus, that's ok.  This used to check to see if the 
- * host was down in two places, once right after the host was h_held, and 
+ * Specified host may be bogus, that's ok.  This used to check to see if the
+ * host was down in two places, once right after the host was h_held, and
  * again after it was locked.  That race condition is incredibly rare and
- * relatively harmless even when it does occur, so we don't check for it now. 
+ * relatively harmless even when it does occur, so we don't check for it now.
  */
 /* if flag is true, send a break callback msg to "host", too */
 int
@@ -784,16 +792,21 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
 {
     struct FileEntry *fe;
     struct CallBack *cb, *nextcb;
-    struct cbstruct cba[MAX_CB_HOSTS];
-    int ncbas;
+    struct cbstruct cbaDef[MAX_CB_HOSTS], *cba = cbaDef;
+    unsigned int ncbas, cbaAlloc = MAX_CB_HOSTS;
     struct AFSCBFids tf;
     int hostindex;
     char hoststr[16];
 
-    ViceLog(7,
-           ("BCB: BreakCallBack(Host %x all but %s:%d, (%u,%u,%u))\n",
-            xhost, afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
-            fid->Volume, fid->Vnode, fid->Unique));
+    if (xhost)
+       ViceLog(7,
+               ("BCB: BreakCallBack(Host %p all but %s:%d, (%u,%u,%u))\n",
+                xhost, afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
+                fid->Volume, fid->Vnode, fid->Unique));
+    else
+       ViceLog(7,
+               ("BCB: BreakCallBack(No Host, (%u,%u,%u))\n",
+               fid->Volume, fid->Vnode, fid->Unique));
 
     H_LOCK;
     cbstuff.BreakCallBacks++;
@@ -801,7 +814,7 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
     if (!fe) {
        goto done;
     }
-    hostindex = h_htoi(xhost);
+    hostindex = xhost ? h_htoi(xhost) : 0;
     cb = itocb(fe->firstcb);
     if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
        /* the most common case is what follows the || */
@@ -810,8 +823,7 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
     tf.AFSCBFids_len = 1;
     tf.AFSCBFids_val = fid;
 
-    for (; cb;) {
-       for (ncbas = 0; cb && ncbas < MAX_CB_HOSTS; cb = nextcb) {
+       for (ncbas = 0; cb ; cb = nextcb) {
            nextcb = itocb(cb->cnext);
            if ((cb->hhead != hostindex || flag)
                && (cb->status == CB_BULK || cb->status == CB_NORMAL
@@ -821,38 +833,51 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
                    ViceLog(0, ("BCB: BOGUS! cb->hhead is NULL!\n"));
                } else if (thishost->hostFlags & VENUSDOWN) {
                    ViceLog(7,
-                           ("BCB: %x (%s:%d) is down; delaying break call back\n",
+                           ("BCB: %p (%s:%d) is down; delaying break call back\n",
                             thishost, afs_inet_ntoa_r(thishost->host, hoststr),
                             ntohs(thishost->port)));
                    cb->status = CB_DELAYED;
                } else {
-                   h_Hold_r(thishost);
-                   cba[ncbas].hp = thishost;
-                   cba[ncbas].thead = cb->thead;
-                   ncbas++;
+                   if (!(thishost->hostFlags & HOSTDELETED)) {
+                       h_Hold_r(thishost);
+                       if (ncbas == cbaAlloc) {        /* Need more space */
+                           int curLen = cbaAlloc*sizeof(cba[0]);
+                           struct cbstruct *cbaOld = (cba == cbaDef) ? NULL : cba;
+
+                           /* There are logical contraints elsewhere that the number of hosts
+                              (i.e. h_HTSPERBLOCK*h_MAXHOSTTABLES) remains in the realm of a signed "int".
+                              cbaAlloc is defined unsigned int hence doubling below cannot overflow
+                           */
+                           cbaAlloc = cbaAlloc<<1;     /* double */
+                           cba = realloc(cbaOld, cbaAlloc * sizeof(cba[0]));
+
+                           if (cbaOld == NULL) {       /* realloc wouldn't have copied from cbaDef */
+                               memcpy(cba, cbaDef, curLen);
+                           }
+                       }
+                       cba[ncbas].hp = thishost;
+                       cba[ncbas].thead = cb->thead;
+                       ncbas++;
+                   }
                    TDel(cb);
                    HDel(cb);
-                   CDel(cb, 1);        /* Usually first; so this delete 
+                   CDel(cb, 1);        /* Usually first; so this delete
                                         * is reasonably inexpensive */
                }
            }
        }
 
        if (ncbas) {
-           MultiBreakCallBack_r(cba, ncbas, &tf, xhost);
+           struct cbstruct *cba2;
+           int num;
 
-           /* we need to to all these initializations again because MultiBreakCallBack may block */
-           fe = FindFE(fid);
-           if (!fe) {
-               goto done;
-           }
-           cb = itocb(fe->firstcb);
-           if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
-               /* the most common case is what follows the || */
-               goto done;
+           for (cba2 = cba, num = ncbas; ncbas > 0; cba2 += num, ncbas -= num) {
+               num = (ncbas > MAX_CB_HOSTS) ? MAX_CB_HOSTS : ncbas;
+               MultiBreakCallBack_r(cba2, num, &tf);
            }
        }
-    }
+
+       if (cba != cbaDef) free(cba);
 
   done:
     H_UNLOCK;
@@ -863,14 +888,15 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
 int
 DeleteCallBack(struct host *host, AFSFid * fid)
 {
-    register struct FileEntry *fe;
-    register afs_uint32 *pcb;
+    struct FileEntry *fe;
+    afs_uint32 *pcb;
     char hoststr[16];
 
+    H_LOCK;
     cbstuff.DeleteCallBacks++;
 
-    H_LOCK;
     h_Lock_r(host);
+    /* do not care if the host has been HOSTDELETED */
     fe = FindFE(fid);
     if (!fe) {
        h_Unlock_r(host);
@@ -883,7 +909,7 @@ DeleteCallBack(struct host *host, AFSFid * fid)
     pcb = FindCBPtr(fe, host);
     if (!*pcb) {
        ViceLog(8,
-               ("DCB: No call back for host %x (%s:%d), (%u, %u, %u)\n",
+               ("DCB: No call back for host %p (%s:%d), (%u, %u, %u)\n",
                 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
                 fid->Volume, fid->Vnode, fid->Unique));
        h_Unlock_r(host);
@@ -907,10 +933,10 @@ DeleteCallBack(struct host *host, AFSFid * fid)
 int
 DeleteFileCallBacks(AFSFid * fid)
 {
-    register struct FileEntry *fe;
-    register struct CallBack *cb;
-    register afs_uint32 cbi;
-    register int n;
+    struct FileEntry *fe;
+    struct CallBack *cb;
+    afs_uint32 cbi;
+    int n;
 
     H_LOCK;
     cbstuff.DeleteFiles++;
@@ -928,6 +954,7 @@ DeleteFileCallBacks(AFSFid * fid)
        TDel(cb);
        HDel(cb);
        FreeCB(cb);
+       fe->ncbs--;
     }
     FDel(fe);
     H_UNLOCK;
@@ -939,8 +966,8 @@ DeleteFileCallBacks(AFSFid * fid)
 int
 DeleteAllCallBacks_r(struct host *host, int deletefe)
 {
-    register struct CallBack *cb;
-    register int cbi, first;
+    struct CallBack *cb;
+    int cbi, first;
 
     cbstuff.DeleteAllCallBacks++;
     cbi = first = host->cblist;
@@ -977,7 +1004,6 @@ int
 BreakDelayedCallBacks_r(struct host *host)
 {
     struct AFSFid fids[AFSCBMAX];
-    u_byte thead[AFSCBMAX];    /* This should match thead in struct Callback */
     int cbi, first, nfids;
     struct CallBack *cb;
     int code;
@@ -1004,15 +1030,17 @@ BreakDelayedCallBacks_r(struct host *host)
        if (code) {
            if (ShowProblems) {
                ViceLog(0,
-                       ("CB: Call back connect back failed (in break delayed) for Host %x (%s:%d)\n",
+                       ("CB: Call back connect back failed (in break delayed) "
+                        "for Host %p (%s:%d)\n",
                         host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port)));
            }
            host->hostFlags |= VENUSDOWN;
        } else {
            ViceLog(25,
-                   ("InitCallBackState success on %x (%s:%d)\n",
-                    host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+                   ("InitCallBackState success on %p (%s:%d)\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
+                    ntohs(host->port)));
            /* reset was done successfully */
            host->hostFlags |= RESETDONE;
            host->hostFlags &= ~VENUSDOWN;
@@ -1029,8 +1057,7 @@ BreakDelayedCallBacks_r(struct host *host)
                cb = itocb(cbi);
                cbi = cb->hnext;
                if (cb->status == CB_DELAYED) {
-                   register struct FileEntry *fe = itofe(cb->fhead);
-                   thead[nfids] = cb->thead;
+                   struct FileEntry *fe = itofe(cb->fhead);
                    fids[nfids].Volume = fe->volid;
                    fids[nfids].Vnode = fe->vnode;
                    fids[nfids].Unique = fe->unique;
@@ -1051,14 +1078,16 @@ BreakDelayedCallBacks_r(struct host *host)
                int i;
                if (ShowProblems) {
                    ViceLog(0,
-                           ("CB: XCallBackBulk failed, Host %x (%s:%d); callback list follows:\n",
+                           ("CB: XCallBackBulk failed, Host %p (%s:%d); "
+                            "callback list follows:\n",
                              host, afs_inet_ntoa_r(host->host, hoststr),
                             ntohs(host->port)));
                }
                for (i = 0; i < nfids; i++) {
                    if (ShowProblems) {
                        ViceLog(0,
-                               ("CB: Host %x (%s:%d), file %u.%u.%u (part of bulk callback)\n",
+                               ("CB: Host %p (%s:%d), file %u.%u.%u "
+                                "(part of bulk callback)\n",
                                 host, afs_inet_ntoa_r(host->host, hoststr),
                                 ntohs(host->port), fids[i].Volume,
                                 fids[i].Vnode, fids[i].Unique));
@@ -1079,51 +1108,42 @@ BreakDelayedCallBacks_r(struct host *host)
 
     cbstuff.nbreakers--;
     /* If we succeeded it's always ok to unset HFE_LATER */
-    if (!host->hostFlags & VENUSDOWN)
+    if (!(host->hostFlags & VENUSDOWN))
        host->hostFlags &= ~HFE_LATER;
     return (host->hostFlags & VENUSDOWN);
 }
 
-/*
-** isheld is 0 if the host is held in h_Enumerate
-** isheld is 1 if the host is held in BreakVolumeCallBacks
-*/
 static int
-MultiBreakVolumeCallBack_r(struct host *host, int isheld,
+MultiBreakVolumeCallBack_r(struct host *host,
                           struct VCBParams *parms, int deletefe)
 {
     char hoststr[16];
 
-    if (!isheld)
-       return isheld;          /* host is held only by h_Enumerate, do nothing */
     if (host->hostFlags & HOSTDELETED)
-       return 0;               /* host is deleted, release hold */
+       return 0;
+
+    if (!(host->hostFlags & HCBREAK))
+       return 0;               /* host is not flagged to notify */
 
     if (host->hostFlags & VENUSDOWN) {
        h_Lock_r(host);
-       if (host->hostFlags & HOSTDELETED) {
-           h_Unlock_r(host);
-           return 0;           /* Release hold */
-       }
-       ViceLog(8,
-               ("BVCB: volume call back for Host %x (%s:%d) failed\n",
-                 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+        /* Do not care if the host is now HOSTDELETED */
        if (ShowProblems) {
            ViceLog(0,
-                   ("CB: volume callback for Host %x (%s:%d) failed\n",
+                   ("BVCB: volume callback for Host %p (%s:%d) failed\n",
                     host, afs_inet_ntoa_r(host->host, hoststr),
                     ntohs(host->port)));
        }
-       DeleteAllCallBacks_r(host, deletefe);   /* Delete all callback state 
-                                                * rather than attempting to 
+       DeleteAllCallBacks_r(host, deletefe);   /* Delete all callback state
+                                                * rather than attempting to
                                                 * selectively remember to
                                                 * delete the volume callbacks
                                                 * later */
-       host->hostFlags &= ~RESETDONE;  /* Do InitCallBackState when host returns */
+       host->hostFlags &= ~(RESETDONE|HCBREAK);        /* Do InitCallBackState when host returns */
        h_Unlock_r(host);
-       return 0;               /* release hold */
+       return 0;
     }
-    assert(parms->ncbas <= MAX_CB_HOSTS);
+    opr_Assert(parms->ncbas <= MAX_CB_HOSTS);
 
     /* Do not call MultiBreakCallBack on the current host structure
      ** because it would prematurely release the hold on the host
@@ -1135,41 +1155,28 @@ MultiBreakVolumeCallBack_r(struct host *host, int isheld,
        tf.AFSCBFids_val = parms->fid;
 
        /* this releases all the hosts */
-       MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf, 0 /* xhost */ );
+       MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf);
 
        parms->ncbas = 0;
     }
     parms->cba[parms->ncbas].hp = host;
     parms->cba[(parms->ncbas)++].thead = parms->thead;
-    return 1;                  /* DON'T release hold, because we still need it. */
-}
+    host->hostFlags &= ~HCBREAK;
 
-/*
-** isheld is 0 if the host is held in h_Enumerate
-** isheld is 1 if the host is held in BreakVolumeCallBacks
-*/
-static int
-MultiBreakVolumeCallBack(struct host *host, int isheld,
-                        struct VCBParams *parms)
-{
-    int retval;
-    H_LOCK;
-    retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 1);
-    H_UNLOCK;
-    return retval;
+    /* we have more work to do on this host, so make sure we keep a reference
+     * to it */
+    h_Hold_r(host);
+
+    return 0;
 }
 
-/*
-** isheld is 0 if the host is held in h_Enumerate
-** isheld is 1 if the host is held in BreakVolumeCallBacks
-*/
 static int
-MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
-                             struct VCBParams *parms)
+MultiBreakVolumeLaterCallBack(struct host *host, void *rock)
 {
+    struct VCBParams *parms = (struct VCBParams *)rock;
     int retval;
     H_LOCK;
-    retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 0);
+    retval = MultiBreakVolumeCallBack_r(host, parms, 0);
     H_UNLOCK;
     return retval;
 }
@@ -1182,82 +1189,13 @@ MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
  * this function is executing.  It is just a temporary state, however,
  * since the callback will be broken later by this same function.
  *
- * Now uses multi-RX for CallBack RPC.  Note that the
- * multiBreakCallBacks routine does not force a reset if the RPC
- * fails, unlike the previous version of this routine, but does create
- * a delayed callback.  Resets will be forced if the host is
- * determined to be down before the RPC is executed.
+ * Now uses multi-RX for CallBack RPC in a different thread,
+ * only marking them here.
  */
-int
-BreakVolumeCallBacks(afs_uint32 volume)
-{
-    struct AFSFid fid;
-    int hash;
-    afs_uint32 *feip;
-    struct CallBack *cb;
-    struct FileEntry *fe;
-    struct host *host;
-    struct VCBParams henumParms;
-    afs_uint32 tthead = 0;     /* zero is illegal value */
-
-    H_LOCK;
-    fid.Volume = volume, fid.Vnode = fid.Unique = 0;
-    for (hash = 0; hash < FEHASH_SIZE; hash++) {
-       for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
-           if (fe->volid == volume) {
-               register struct CallBack *cbnext;
-               for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
-                   host = h_itoh(cb->hhead);
-                   h_Hold_r(host);
-                   cbnext = itocb(cb->cnext);
-                   if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
-                       tthead = cb->thead;
-                   }
-                   TDel(cb);
-                   HDel(cb);
-                   FreeCB(cb);
-                   /* leave hold for MultiBreakVolumeCallBack to clear */
-               }
-               *feip = fe->fnext;
-               FreeFE(fe);
-           } else {
-               feip = &fe->fnext;
-           }
-       }
-    }
-
-    if (!tthead) {
-       /* didn't find any callbacks, so return right away. */
-       H_UNLOCK;
-       return 0;
-    }
-    henumParms.ncbas = 0;
-    henumParms.fid = &fid;
-    henumParms.thead = tthead;
-    H_UNLOCK;
-    h_Enumerate(MultiBreakVolumeCallBack, (char *)&henumParms);
-    H_LOCK;
-    if (henumParms.ncbas) {    /* do left-overs */
-       struct AFSCBFids tf;
-       tf.AFSCBFids_len = 1;
-       tf.AFSCBFids_val = &fid;
-
-       MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf, 0);
-
-       henumParms.ncbas = 0;
-    }
-    H_UNLOCK;
-    return 0;
-}
-
-#ifdef AFS_PTHREAD_ENV
 extern pthread_cond_t fsync_cond;
-#else
-extern char fsync_wait[];
-#endif
 
 int
-BreakVolumeCallBacksLater(afs_uint32 volume)
+BreakVolumeCallBacksLater(VolumeId volume)
 {
     int hash;
     afs_uint32 *feip;
@@ -1266,12 +1204,13 @@ BreakVolumeCallBacksLater(afs_uint32 volume)
     struct host *host;
     int found = 0;
 
-    ViceLog(25, ("Setting later on volume %u\n", volume));
+    ViceLog(25, ("Setting later on volume %" AFS_VOLID_FMT "\n",
+                afs_printable_VolumeId_lu(volume)));
     H_LOCK;
     for (hash = 0; hash < FEHASH_SIZE; hash++) {
        for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
            if (fe->volid == volume) {
-               register struct CallBack *cbnext;
+               struct CallBack *cbnext;
                for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
                    host = h_itoh(cb->hhead);
                    host->hostFlags |= HFE_LATER;
@@ -1293,13 +1232,9 @@ BreakVolumeCallBacksLater(afs_uint32 volume)
     }
 
     ViceLog(25, ("Fsync thread wakeup\n"));
-#ifdef AFS_PTHREAD_ENV
     FSYNC_LOCK;
-    assert(pthread_cond_broadcast(&fsync_cond) == 0);
+    opr_cv_broadcast(&fsync_cond);
     FSYNC_UNLOCK;
-#else
-    LWP_NoYieldSignal(fsync_wait);
-#endif
     return 0;
 }
 
@@ -1331,10 +1266,11 @@ BreakLaterCallBacks(void)
                /* Ugly, but used to avoid left side casting */
                struct object *tmpfe;
                ViceLog(125,
-                       ("Unchaining for %u:%u:%u\n", fe->vnode, fe->unique,
-                        fe->volid));
+                       ("Unchaining for %u:%u:%" AFS_VOLID_FMT "\n", fe->vnode,
+                        fe->unique, afs_printable_VolumeId_lu(fe->volid)));
                fid.Volume = fe->volid;
                *feip = fe->fnext;
+               fe->status &= ~FE_LATER; /* not strictly needed */
                /* Works since volid is deeper than the largest pointer */
                tmpfe = (struct object *)fe;
                tmpfe->next = (struct object *)myfe;
@@ -1353,24 +1289,28 @@ BreakLaterCallBacks(void)
     /* loop over FEs from myfe and free/break */
     tthead = 0;
     for (fe = myfe; fe;) {
-       register struct CallBack *cbnext;
+       struct CallBack *cbnext;
        for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
            cbnext = itocb(cb->cnext);
            host = h_itoh(cb->hhead);
            if (cb->status == CB_DELAYED) {
-               h_Hold_r(host);
-               if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
-                   tthead = cb->thead;
+               if (!(host->hostFlags & HOSTDELETED)) {
+                   /* mark this host for notification */
+                   host->hostFlags |= HCBREAK;
+                   if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
+                       tthead = cb->thead;
+                   }
                }
                TDel(cb);
                HDel(cb);
                CDel(cb, 0);    /* Don't let CDel clean up the fe */
-               /* leave hold for MultiBreakVolumeCallBack to clear */
+               /* leave flag for MultiBreakVolumeCallBack to clear */
            } else {
                ViceLog(125,
-                       ("Found host %x (%s:%d) non-DELAYED cb for %u:%u:%u\n", 
+                       ("Found host %p (%s:%d) non-DELAYED cb for %u:%u:%" AFS_VOLID_FMT "\n",
                         host, afs_inet_ntoa_r(host->host, hoststr),
-                        ntohs(host->port), fe->vnode, fe->unique, fe->volid));
+                        ntohs(host->port), fe->vnode, fe->unique,
+                        afs_printable_VolumeId_lu(fe->volid)));
            }
        }
        myfe = fe;
@@ -1391,7 +1331,7 @@ BreakLaterCallBacks(void)
            tf.AFSCBFids_len = 1;
            tf.AFSCBFids_val = &fid;
 
-           MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf, 0);
+           MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf);
            henumParms.ncbas = 0;
        }
     }
@@ -1417,31 +1357,32 @@ CleanupTimedOutCallBacks(void)
 int
 CleanupTimedOutCallBacks_r(void)
 {
-    afs_uint32 now = CBtime(FT_ApproxTime());
-    register afs_uint32 *thead;
-    register struct CallBack *cb;
-    register int ntimedout = 0;
+    afs_uint32 now = CBtime(time(NULL));
+    afs_uint32 *thead;
+    struct CallBack *cb;
+    int ntimedout = 0;
     char hoststr[16];
 
     while (tfirst <= now) {
-       register int cbi;
+       int cbi;
        cbi = *(thead = THead(tfirst));
        if (cbi) {
            do {
                cb = itocb(cbi);
                cbi = cb->tnext;
                ViceLog(8,
-                       ("CCB: deleting timed out call back %x (%s:%d), (%u,%u,%u)\n",
+                       ("CCB: deleting timed out call back %x (%s:%d), (%" AFS_VOLID_FMT ",%u,%u)\n",
                          h_itoh(cb->hhead)->host,
                          afs_inet_ntoa_r(h_itoh(cb->hhead)->host, hoststr),
-                        h_itoh(cb->hhead)->port, itofe(cb->fhead)->volid,
+                        h_itoh(cb->hhead)->port,
+                        afs_printable_VolumeId_lu(itofe(cb->fhead)->volid),
                         itofe(cb->fhead)->vnode, itofe(cb->fhead)->unique));
                HDel(cb);
                CDel(cb, 1);
                ntimedout++;
                if (ntimedout > cbstuff.nblks) {
                    ViceLog(0, ("CCB: Internal Error -- shutting down...\n"));
-                   DumpCallBackState();
+                   DumpCallBackState_r();
                    ShutDownAndCore(PANIC);
                }
            } while (cbi != *thead);
@@ -1454,74 +1395,123 @@ CleanupTimedOutCallBacks_r(void)
     return (ntimedout > 0);
 }
 
-static struct host *lih_host;
-static int lih_host_held;
+/**
+ * parameters to pass to lih*_r from h_Enumerate_r when trying to find a host
+ * from which to clear callbacks.
+ */
+struct lih_params {
+    /**
+     * Points to the least interesting host found; try to clear callbacks on
+     * this host after h_Enumerate_r(lih*_r)'ing.
+     */
+    struct host *lih;
+
+    /**
+     * The last host we got from lih*_r, but we couldn't clear its callbacks
+     * for some reason. Choose the next-best host after this one (with the
+     * current lih*_r, this means to only select hosts that have an ActiveCall
+     * newer than lastlih).
+     */
+    struct host *lastlih;
+};
 
-/* This version does not allow 'host' to be selected unless its ActiveCall 
- * is newer than 'hostp' which is the host with the oldest ActiveCall from
- * the last pass (if it is provided).  We filter out any hosts that are
- * are held by other threads.
+/* Value of host->refCount that allows us to reliably infer that
+ * host may be held by some other thread */
+#define OTHER_MUSTHOLD_LIH 2
+
+/* This version does not allow 'host' to be selected unless its ActiveCall
+ * is newer than 'params->lastlih' which is the host with the oldest
+ * ActiveCall from the last pass (if it is provided).  We filter out any hosts
+ * that are are held by other threads.
+ *
+ * There is a small problem here, but it may not be easily fixable. Say we
+ * select some host A, and give it back to GetSomeSpace_r. GSS_r for some
+ * reason cannot clear the callbacks on A, and so calls us again with
+ * lastlih = A. Suppose there is another host B that has the same ActiveCall
+ * time as A. We will now skip over host B, since
+ * 'hostB->ActiveCall > hostA->ActiveCall' is not true. This could result in
+ * us prematurely going to the GSS_r 2nd or 3rd pass, and making us a little
+ * inefficient. This should be pretty rare, though, except perhaps in cases
+ * with very small numbers of hosts.
+ *
+ * Also filter out any hosts with HOSTDELETED set. h_Enumerate_r should in
+ * theory not give these to us anyway, but be paranoid.
  */
 static int
-lih0_r(register struct host *host, register int held,
-      register struct host *hostp)
+lih0_r(struct host *host, void *rock)
 {
+    struct lih_params *params = (struct lih_params *)rock;
+
+    /* OTHER_MUSTHOLD_LIH is because the h_Enum loop holds us once */
     if (host->cblist
-       && (hostp && host != hostp) 
-       && (!held && !h_OtherHolds_r(host))
-       && (!lih_host || host->ActiveCall < lih_host->ActiveCall) 
-       && (!hostp || host->ActiveCall > hostp->ActiveCall)) {
-       if (lih_host != NULL && lih_host_held) {
-           h_Release_r(lih_host);
+       && (!(host->hostFlags & HOSTDELETED))
+       && (host->refCount < OTHER_MUSTHOLD_LIH)
+       && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
+       && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
+
+       if (params->lih) {
+           h_Release_r(params->lih); /* release prev host */
        }
-       lih_host = host;
-       lih_host_held = !held;
-       held = 1;
+
+       h_Hold_r(host);
+       params->lih = host;
     }
-    return held;
+    return 0;
 }
 
-/* This version does not allow 'host' to be selected unless its ActiveCall 
- * is newer than 'hostp' which is the host with the oldest ActiveCall from
- * the last pass (if it is provided).  In this second varient, we do not 
- * prevent held hosts from being selected.
- */
+/* same as lih0_r, except we do not prevent held hosts from being selected. */
 static int
-lih1_r(register struct host *host, register int held,
-      register struct host *hostp)
+lih1_r(struct host *host, void *rock)
 {
+    struct lih_params *params = (struct lih_params *)rock;
+
     if (host->cblist
-       && (hostp && host != hostp) 
-       && (!lih_host || host->ActiveCall < lih_host->ActiveCall) 
-       && (!hostp || host->ActiveCall > hostp->ActiveCall)) {
-       if (lih_host != NULL && lih_host_held) {
-           h_Release_r(lih_host);
+       && (!(host->hostFlags & HOSTDELETED))
+       && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
+       && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
+
+       if (params->lih) {
+           h_Release_r(params->lih); /* release prev host */
        }
-       lih_host = host;
-       lih_host_held = !held;
-       held = 1;
+
+       h_Hold_r(host);
+       params->lih = host;
     }
-    return held;
+    return 0;
 }
 
 /* This could be upgraded to get more space each time */
 /* first pass: sequentially find the oldest host which isn't held by
                anyone for which we can clear callbacks;
               skipping 'hostp' */
-/* second pass: sequentially find the oldest host regardless of 
+/* second pass: sequentially find the oldest host regardless of
                whether or not the host is held; skipping 'hostp' */
 /* third pass: attempt to clear callbacks from 'hostp' */
 /* always called with hostp unlocked */
 
-/* Note: hostlist is ordered most recently created host first and 
+/* Note: hostlist is ordered most recently created host first and
  * its order has no relationship to the most recently used. */
 extern struct host *hostList;
 static int
 GetSomeSpace_r(struct host *hostp, int locked)
 {
-    register struct host *hp, *hp1, *hp2;
+    struct host *hp;
+    struct lih_params params;
     int i = 0;
 
+    if (cbstuff.GotSomeSpaces == 0) {
+       /* only log this once; if GSS is getting called constantly, that's not
+        * good but don't make things worse by spamming the log. */
+       ViceLog(0, ("We have run out of callback space; forcing callback revocation. "
+                   "This suggests the fileserver is configured with insufficient "
+                   "callbacks; you probably want to increase the -cb fileserver "
+                   "parameter (current setting: %u). The fileserver will continue "
+                   "to operate, but this may indicate a severe performance problem\n",
+                   cbstuff.nblks));
+       ViceLog(0, ("This message is logged at most once; for more information "
+                   "see the OpenAFS documentation and fileserver xstat collection 3\n"));
+    }
+
     cbstuff.GotSomeSpaces++;
     ViceLog(5,
            ("GSS: First looking for timed out call backs via CleanupCallBacks\n"));
@@ -1531,36 +1521,39 @@ GetSomeSpace_r(struct host *hostp, int locked)
     }
 
     i = 0;
-    hp1 = NULL;
-    hp2 = hostList;
+    params.lastlih = NULL;
+
     do {
-       lih_host = 0;
-       h_Enumerate_r(i == 0 ? lih0_r : lih1_r, hp2, (char *)hp1);
-       hp = lih_host;
+       params.lih = NULL;
+
+       h_Enumerate_r(i == 0 ? lih0_r : lih1_r, hostList, &params);
+
+       hp = params.lih;
+       if (params.lastlih) {
+           h_Release_r(params.lastlih);
+           params.lastlih = NULL;
+       }
+
        if (hp) {
-           /* set in lih_r! private copy before giving up H_LOCK */
-           int lih_host_held2=lih_host_held;   
+           /* note that 'hp' was held by lih*_r; we will need to release it */
            cbstuff.GSS4++;
            if ((hp != hostp) && !ClearHostCallbacks_r(hp, 0 /* not locked or held */ )) {
-               if (lih_host_held2)
-                   h_Release_r(hp);
+                h_Release_r(hp);
                return 0;
            }
-           if (lih_host_held2)
-               h_Release_r(hp);
-           hp1 = hp;
-           hp2 = hostList;
+
+           params.lastlih = hp;
+           /* params.lastlih will be released on the next iteration, after
+            * h_Enumerate_r */
+
        } else {
            /*
             * Next time try getting callbacks from any host even if
-            * it's deleted (that's actually great since we can freely
-            * remove its callbacks) or it's held since the only other
-            * option is starvation for the file server (i.e. until the
-            * callback timeout arrives).
+            * it's held, since the only other option is starvation for
+            * the file server (i.e. until the callback timeout arrives).
             */
            i++;
-           hp1 = NULL;
-           hp2 = hostList;
+           params.lastlih = NULL;
            cbstuff.GSS1++;
            ViceLog(5,
                    ("GSS: Try harder for longest inactive host cnt= %d\n",
@@ -1585,25 +1578,30 @@ static int
 ClearHostCallbacks_r(struct host *hp, int locked)
 {
     int code;
-    int held = 0;
     char hoststr[16];
     struct rx_connection *cb_conn = NULL;
 
     ViceLog(5,
-           ("GSS: Delete longest inactive host %x (%s:%d)\n",
+           ("GSS: Delete longest inactive host %p (%s:%d)\n",
              hp, afs_inet_ntoa_r(hp->host, hoststr), ntohs(hp->port)));
-    if (!(held = h_Held_r(hp)))
-       h_Hold_r(hp);
+
+    if ((hp->hostFlags & HOSTDELETED)) {
+       /* hp could go away after reacquiring H_LOCK in h_NBLock_r, so we can't
+        * really use it; its callbacks will get cleared anyway when
+        * h_TossStuff_r gets its hands on it */
+       return 1;
+    }
+
+    h_Hold_r(hp);
 
     /** Try a non-blocking lock. If the lock is already held return
       * after releasing hold on hp
       */
     if (!locked) {
-       if (h_NBLock_r(hp)) {
-           if (!held)
-               h_Release_r(hp);
-           return 1;
-       }
+        if (h_NBLock_r(hp)) {
+            h_Release_r(hp);
+            return 1;
+        }
     }
     if (hp->Console & 2) {
        /*
@@ -1620,7 +1618,7 @@ ClearHostCallbacks_r(struct host *hp, int locked)
     DeleteAllCallBacks_r(hp, 1);
     if (hp->hostFlags & VENUSDOWN) {
        hp->hostFlags &= ~RESETDONE;    /* remember that we must do a reset */
-    } else {
+    } else if (!(hp->hostFlags & HOSTDELETED)) {
        /* host is up, try a call */
        hp->hostFlags &= ~ALTADDR;      /* alternate addresses are invalid */
        cb_conn = hp->callback_rxcon;
@@ -1646,11 +1644,9 @@ ClearHostCallbacks_r(struct host *hp, int locked)
            hp->hostFlags |= RESETDONE;
        }
     }
-    if (!locked) {
-       h_Unlock_r(hp);
-    }
-    if (!held)
-       h_Release_r(hp);
+    if (!locked)
+        h_Unlock_r(hp);
+    h_Release_r(hp);
 
     return 0;
 }
@@ -1668,11 +1664,15 @@ PrintCallBackStats(void)
     fprintf(stderr, "%d CBs, %d FEs, (%d of total of %d 16-byte blocks)\n",
            cbstuff.nCBs, cbstuff.nFEs, cbstuff.nCBs + cbstuff.nFEs,
            cbstuff.nblks);
+    fprintf(stderr, "%d GSS1, %d GSS2, %d GSS3, %d GSS4, %d GSS5 (internal counters)\n",
+           cbstuff.GSS1, cbstuff.GSS2, cbstuff.GSS3, cbstuff.GSS4, cbstuff.GSS5);
 
     return 0;
 }
 
 #define MAGIC 0x12345678       /* To check byte ordering of dump when it is read in */
+#define MAGICV2 0x12345679      /* To check byte ordering & version of dump when it is read in */
+
 
 #ifndef INTERPRET_DUMP
 
@@ -1689,7 +1689,7 @@ static int cb_stateRestoreTimeouts(struct fs_dump_state * state);
 static int cb_stateRestoreFEHash(struct fs_dump_state * state);
 static int cb_stateRestoreFEs(struct fs_dump_state * state);
 static int cb_stateRestoreFE(struct fs_dump_state * state);
-static int cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe, 
+static int cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
                              struct iovec * iov, int niovecs);
 
 static int cb_stateVerifyFEHash(struct fs_dump_state * state);
@@ -1752,7 +1752,7 @@ cb_stateSave(struct fs_dump_state * state)
        ret = 1;
        goto done;
     }
-    
+
  done:
     return ret;
 }
@@ -1826,7 +1826,7 @@ cb_stateRestoreIndices(struct fs_dump_state * state)
            }
        }
     }
-    
+
     /* restore indices in the CallBack structures */
     for (i = 1; i < state->cb_map.len; i++) {
        if (state->cb_map.entries[i].new_idx) {
@@ -1837,7 +1837,7 @@ cb_stateRestoreIndices(struct fs_dump_state * state)
                ret = 1;
                goto done;
            }
-           
+
            /* restore the cb->fhead entry */
            if (fe_OldToNew(state, cb->fhead, &cb->fhead)) {
                ret = 1;
@@ -1909,7 +1909,6 @@ cb_stateVerify(struct fs_dump_state * state)
        ret = 1;
     }
 
- done:
     return ret;
 }
 
@@ -1943,7 +1942,6 @@ cb_stateVerifyFEHash(struct fs_dump_state * state)
        }
     }
 
- done:
     return ret;
 }
 
@@ -1954,16 +1952,18 @@ cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe)
 
     if ((fe->firstcb && !fe->ncbs) ||
        (!fe->firstcb && fe->ncbs)) {
-       ViceLog(0, ("cb_stateVerifyFE: error: fe->firstcb does not agree with fe->ncbs (fei=%d, fe->firstcb=%d, fe->ncbs=%d)\n",
-                   fetoi(fe), fe->firstcb, fe->ncbs));
+       ViceLog(0, ("cb_stateVerifyFE: error: fe->firstcb does not agree with fe->ncbs (fei=%lu, fe->firstcb=%lu, fe->ncbs=%lu)\n",
+                   afs_printable_uint32_lu(fetoi(fe)),
+                   afs_printable_uint32_lu(fe->firstcb),
+                   afs_printable_uint32_lu(fe->ncbs)));
        ret = 1;
     }
     if (cb_stateVerifyFCBList(state, fe)) {
-       ViceLog(0, ("cb_stateVerifyFE: error: FCBList failed verification (fei=%d)\n", fetoi(fe)));
+       ViceLog(0, ("cb_stateVerifyFE: error: FCBList failed verification (fei=%lu)\n",
+                   afs_printable_uint32_lu(fetoi(fe))));
        ret = 1;
     }
 
- done:
     return ret;
 }
 
@@ -2121,7 +2121,6 @@ cb_stateVerifyTimeoutQueues(struct fs_dump_state * state)
        }
     }
 
- done:
     return ret;
 }
 
@@ -2166,7 +2165,7 @@ cb_stateRestoreTimeouts(struct fs_dump_state * state)
     int ret = 0, len;
 
     if (fs_stateReadHeader(state, &state->cb_hdr->timeout_offset,
-                          state->cb_timeout_hdr, 
+                          state->cb_timeout_hdr,
                           sizeof(struct callback_state_timeout_header))) {
        ret = 1;
        goto done;
@@ -2239,7 +2238,7 @@ cb_stateRestoreFEHash(struct fs_dump_state * state)
     int ret = 0, len;
 
     if (fs_stateReadHeader(state, &state->cb_hdr->fehash_offset,
-                          state->cb_fehash_hdr, 
+                          state->cb_fehash_hdr,
                           sizeof(struct callback_state_fehash_header))) {
        ret = 1;
        goto done;
@@ -2275,8 +2274,8 @@ static int
 cb_stateSaveFEs(struct fs_dump_state * state)
 {
     int ret = 0;
-    register int fei, hash;
-    register struct FileEntry *fe;
+    int fei, hash;
+    struct FileEntry *fe;
 
     AssignInt64(state->eof_offset, &state->cb_hdr->fe_offset);
 
@@ -2315,7 +2314,7 @@ cb_stateRestoreFEs(struct fs_dump_state * state)
 static int
 cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
 {
-    int ret = 0, iovcnt, cbi, idx, len, written = 0;
+    int ret = 0, iovcnt, cbi, written = 0;
     afs_uint32 fei;
     struct callback_state_entry_header hdr;
     struct FEDiskEntry fedsk;
@@ -2336,24 +2335,24 @@ cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
     }
 
     iov[0].iov_base = (char *)&hdr;
-    len = iov[0].iov_len = sizeof(hdr);
+    iov[0].iov_len = sizeof(hdr);
     iov[1].iov_base = (char *)&fedsk;
-    len += iov[1].iov_len = sizeof(struct FEDiskEntry);
+    iov[1].iov_len = sizeof(struct FEDiskEntry);
     iovcnt = 2;
 
-    for (cbi = fe->firstcb, cb = itocb(cbi), idx = 2; 
-        cb != NULL; 
-        cbi = cb->cnext, cb = itocb(cbi), idx++, hdr.nCBs++) {
+    for (cbi = fe->firstcb, cb = itocb(cbi);
+        cb != NULL;
+        cbi = cb->cnext, cb = itocb(cbi), hdr.nCBs++) {
        if (cbi > state->cb_hdr->cb_max) {
            state->cb_hdr->cb_max = cbi;
        }
-       if (cb_stateCBToDiskEntry(cb, &cbdsk[idx])) {
+       if (cb_stateCBToDiskEntry(cb, &cbdsk[iovcnt])) {
            ret = 1;
            goto done;
        }
-       cbdsk[idx].index = cbi;
-       iov[idx].iov_base = (char *)&cbdsk[idx];
-       len += iov[idx].iov_len = sizeof(struct CBDiskEntry);
+       cbdsk[iovcnt].index = cbi;
+       iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
+       iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
        iovcnt++;
        if ((iovcnt == 16) || (!cb->cnext)) {
            if (fs_stateWriteV(state, iov, iovcnt)) {
@@ -2362,12 +2361,11 @@ cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
            }
            written = 1;
            iovcnt = 0;
-           len = 0;
        }
     }
 
     hdr.magic = CALLBACK_STATE_ENTRY_MAGIC;
-    hdr.len = sizeof(hdr) + sizeof(struct FEDiskEntry) + 
+    hdr.len = sizeof(hdr) + sizeof(struct FEDiskEntry) +
        (hdr.nCBs * sizeof(struct CBDiskEntry));
 
     if (!written) {
@@ -2401,18 +2399,17 @@ cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
 static int
 cb_stateRestoreFE(struct fs_dump_state * state)
 {
-    int ret = 0, iovcnt, len, nCBs, idx;
+    int ret = 0, iovcnt, nCBs;
     struct callback_state_entry_header hdr;
     struct FEDiskEntry fedsk;
     struct CBDiskEntry cbdsk[16];
     struct iovec iov[16];
     struct FileEntry * fe;
-    struct CallBack * cb;
 
     iov[0].iov_base = (char *)&hdr;
-    len = iov[0].iov_len = sizeof(hdr);
+    iov[0].iov_len = sizeof(hdr);
     iov[1].iov_base = (char *)&fedsk;
-    len += iov[1].iov_len = sizeof(fedsk);
+    iov[1].iov_len = sizeof(fedsk);
     iovcnt = 2;
 
     if (fs_stateReadV(state, iov, iovcnt)) {
@@ -2438,11 +2435,11 @@ cb_stateRestoreFE(struct fs_dump_state * state)
     }
 
     if (hdr.nCBs) {
-       for (iovcnt = 0, idx = 0, len = 0, nCBs = 0;
+       for (iovcnt = 0, nCBs = 0;
             nCBs < hdr.nCBs;
-            idx++, nCBs++) {
-           iov[idx].iov_base = (char *)&cbdsk[idx];
-           len += iov[idx].iov_len = sizeof(struct CBDiskEntry);
+            nCBs++) {
+           iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
+           iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
            iovcnt++;
            if ((iovcnt == 16) || (nCBs == hdr.nCBs - 1)) {
                if (fs_stateReadV(state, iov, iovcnt)) {
@@ -2453,29 +2450,31 @@ cb_stateRestoreFE(struct fs_dump_state * state)
                    ret = 1;
                    goto done;
                }
-               len = 0;
                iovcnt = 0;
            }
        }
     }
-    
+
  done:
     return ret;
 }
 
 static int
-cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe, 
+cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
                   struct iovec * iov, int niovecs)
 {
     int ret = 0, idx;
-    register struct CallBack * cb;
+    struct CallBack * cb;
     struct CBDiskEntry * cbdsk;
-    afs_uint32 fei;
-
-    fei = fetoi(fe);
 
     for (idx = 0; idx < niovecs; idx++) {
        cbdsk = (struct CBDiskEntry *) iov[idx].iov_base;
+
+       if (cbdsk->cb.hhead < state->h_map.len &&
+           state->h_map.entries[cbdsk->cb.hhead].valid == FS_STATE_IDX_SKIPPED) {
+           continue;
+       }
+
        if ((cb = GetCB()) == NULL) {
            ViceLog(0, ("cb_stateRestoreCBs: ran out of free CallBack structures\n"));
            ret = 1;
@@ -2528,7 +2527,7 @@ cb_stateFEToDiskEntry(struct FileEntry * in, struct FEDiskEntry * out)
 }
 
 static int
-cb_stateDiskEntryToFE(struct fs_dump_state * state, 
+cb_stateDiskEntryToFE(struct fs_dump_state * state,
                      struct FEDiskEntry * in, struct FileEntry * out)
 {
     int ret = 0;
@@ -2542,6 +2541,7 @@ cb_stateDiskEntryToFE(struct fs_dump_state * state,
        ret = 1;
        goto done;
     }
+    state->fe_map.entries[in->index].valid = FS_STATE_IDX_VALID;
     state->fe_map.entries[in->index].old_idx = in->index;
     state->fe_map.entries[in->index].new_idx = fetoi(out);
 
@@ -2572,6 +2572,7 @@ cb_stateDiskEntryToCB(struct fs_dump_state * state,
        ret = 1;
        goto done;
     }
+    state->cb_map.entries[in->index].valid = FS_STATE_IDX_VALID;
     state->cb_map.entries[in->index].old_idx = in->index;
     state->cb_map.entries[in->index].new_idx = cbtoi(out);
 
@@ -2606,7 +2607,8 @@ fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
     if (old >= state->fe_map.len) {
        ViceLog(0, ("fe_OldToNew: index %d is out of range\n", old));
        ret = 1;
-    } else if (state->fe_map.entries[old].old_idx != old) { /* sanity check */
+    } else if (state->fe_map.entries[old].valid != FS_STATE_IDX_VALID ||
+               state->fe_map.entries[old].old_idx != old) { /* sanity check */
        ViceLog(0, ("fe_OldToNew: index %d points to an invalid FileEntry record\n", old));
        ret = 1;
     } else {
@@ -2631,7 +2633,8 @@ cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
     if (old >= state->cb_map.len) {
        ViceLog(0, ("cb_OldToNew: index %d is out of range\n", old));
        ret = 1;
-    } else if (state->cb_map.entries[old].old_idx != old) { /* sanity check */
+    } else if (state->cb_map.entries[old].valid != FS_STATE_IDX_VALID ||
+               state->cb_map.entries[old].old_idx != old) { /* sanity check */
        ViceLog(0, ("cb_OldToNew: index %d points to an invalid CallBack record\n", old));
        ret = 1;
     } else {
@@ -2643,14 +2646,17 @@ cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
 }
 #endif /* AFS_DEMAND_ATTACH_FS */
 
-int
-DumpCallBackState(void)
+static int
+DumpCallBackState_r(void)
 {
-    int fd;
-    afs_uint32 magic = MAGIC, now = FT_ApproxTime(), freelisthead;
+    int fd, oflag;
+    afs_uint32 magic = MAGICV2, now = (afs_int32) time(NULL), freelisthead;
 
-    fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, O_WRONLY | O_CREAT | O_TRUNC,
-             0666);
+    oflag = O_WRONLY | O_CREAT | O_TRUNC;
+#ifdef AFS_NT40_ENV
+    oflag |= O_BINARY;
+#endif
+    fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, oflag, 0666);
     if (fd < 0) {
        ViceLog(0,
                ("Couldn't create callback dump file %s\n",
@@ -2675,6 +2681,17 @@ DumpCallBackState(void)
     return 0;
 }
 
+int
+DumpCallBackState(void) {
+    int rc;
+
+    H_LOCK;
+    rc = DumpCallBackState_r();
+    H_UNLOCK;
+
+    return(rc);
+}
+
 #endif /* !INTERPRET_DUMP */
 
 #ifdef INTERPRET_DUMP
@@ -2682,34 +2699,48 @@ DumpCallBackState(void)
 /* This is only compiled in for the callback analyzer program */
 /* Returns the time of the dump */
 time_t
-ReadDump(char *file)
+ReadDump(char *file, int timebits)
 {
-    int fd;
+    int fd, oflag;
     afs_uint32 magic, freelisthead;
-    time_t now;
+    afs_uint32 now;
+    afs_int64 now64;
 
-    fd = open(file, O_RDONLY);
+    oflag = O_RDONLY;
+#ifdef AFS_NT40_ENV
+    oflag |= O_BINARY;
+#endif
+    fd = open(file, oflag);
     if (fd < 0) {
        fprintf(stderr, "Couldn't read dump file %s\n", file);
        exit(1);
     }
     read(fd, &magic, sizeof(magic));
-    if (magic != MAGIC) {
-       fprintf(stderr,
-               "Magic number of %s is invalid.  You might be trying to\n",
-               file);
-       fprintf(stderr,
-               "run this program on a machine type with a different byte ordering.\n");
-       exit(1);
+    if (magic == MAGICV2) {
+       timebits = 32;
+    } else {
+       if (magic != MAGIC) {
+           fprintf(stderr,
+                   "Magic number of %s is invalid.  You might be trying to\n",
+                   file);
+           fprintf(stderr,
+                   "run this program on a machine type with a different byte ordering.\n");
+           exit(1);
+       }
     }
-    read(fd, &now, sizeof(now));
+    if (timebits == 64) {
+       read(fd, &now64, sizeof(afs_int64));
+       now = (afs_int32) now64;
+    } else
+       read(fd, &now, sizeof(afs_int32));
+
     read(fd, &cbstuff, sizeof(cbstuff));
     read(fd, TimeOuts, sizeof(TimeOuts));
     read(fd, timeout, sizeof(timeout));
     read(fd, &tfirst, sizeof(tfirst));
     read(fd, &freelisthead, sizeof(freelisthead));
     CB = ((struct CallBack
-          *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
+          *)(calloc(cbstuff.nblks, sizeof(struct CallBack)))) - 1;
     FE = ((struct FileEntry
           *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
     CBfree = (struct CallBack *)itocb(freelisthead);
@@ -2731,14 +2762,17 @@ ReadDump(char *file)
 #include "AFS_component_version_number.c"
 #endif
 
+static afs_uint32 *cbTrack;
+
 int
 main(int argc, char **argv)
 {
     int err = 0, cbi = 0, stats = 0, noptions = 0, all = 0, vol = 0, raw = 0;
     static AFSFid fid;
-    register struct FileEntry *fe;
-    register struct CallBack *cb;
+    struct FileEntry *fe;
+    struct CallBack *cb;
     time_t now;
+    int timebits = 32;
 
     memset(&fid, 0, sizeof(fid));
     argc--;
@@ -2771,6 +2805,17 @@ main(int argc, char **argv)
            all = 1;
        } else if (!strcmp(*argv, "-raw")) {
            raw = 1;
+       } else if (!strcmp(*argv, "-timebits")) {
+           if (argc < 1) {
+               err++;
+               break;
+           }
+           argc--;
+           timebits = atoi(*++argv);
+           if ((timebits != 32)
+               && (timebits != 64)
+               )
+               err++;
        } else if (!strcmp(*argv, "-volume")) {
            if (argc < 1) {
                err++;
@@ -2784,19 +2829,24 @@ main(int argc, char **argv)
     }
     if (err || argc != 1) {
        fprintf(stderr,
-               "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] callbackdumpfile\n");
+               "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] [-timebits 32"
+               "|64"
+               "] callbackdumpfile\n");
        fprintf(stderr,
                "[cbid is shown for each host in the hosts.dump file]\n");
        exit(1);
     }
-    now = ReadDump(*argv);
+    now = ReadDump(*argv, timebits);
     if (stats || noptions == 0) {
-       time_t uxtfirst = UXtime(tfirst);
-       printf("The time of the dump was %u %s", now, ctime(&now));
-       printf("The last time cleanup ran was %u %s", uxtfirst,
+       time_t uxtfirst = UXtime(tfirst), tnow = now;
+       printf("The time of the dump was %u %s", (unsigned int) now, ctime(&tnow));
+       printf("The last time cleanup ran was %u %s", (unsigned int) uxtfirst,
               ctime(&uxtfirst));
        PrintCallBackStats();
     }
+
+    cbTrack = calloc(cbstuff.nblks, sizeof(cbTrack[0]));
+
     if (all || vol) {
        int hash;
        afs_uint32 *feip;
@@ -2804,12 +2854,27 @@ main(int argc, char **argv)
        struct FileEntry *fe;
 
        for (hash = 0; hash < FEHASH_SIZE; hash++) {
-           for (feip = &HashTable[hash]; fe = itofe(*feip);) {
+           for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
                if (!vol || (fe->volid == vol)) {
-                   register struct CallBack *cbnext;
-                   for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
+                   afs_uint32 fe_i = fetoi(fe);
+
+                   for (cb = itocb(fe->firstcb); cb; cb = itocb(cb->cnext)) {
+                       afs_uint32 cb_i = cbtoi(cb);
+
+                       if (cb_i > cbstuff.nblks) {
+                           printf("CB index out of range (%u > %d), stopped for this FE\n",
+                               cb_i, cbstuff.nblks);
+                           break;
+                       }
+
+                       if (cbTrack[cb_i]) {
+                           printf("CB entry already claimed for FE[%u] (this is FE[%u]), stopped\n",
+                               cbTrack[cb_i], fe_i);
+                           break;
+                       }
+                       cbTrack[cb_i] = fe_i;
+
                        PrintCB(cb, now);
-                       cbnext = itocb(cb->cnext);
                    }
                    *feip = fe->fnext;
                } else {
@@ -2845,11 +2910,13 @@ main(int argc, char **argv)
            printf("%d:%12x%12x%12x%12x\n", i, p[0], p[1], p[2], p[3]);
        }
     }
+
+    free(cbTrack);
     exit(0);
 }
 
 void
-PrintCB(register struct CallBack *cb, afs_uint32 now)
+PrintCB(struct CallBack *cb, afs_uint32 now)
 {
     struct FileEntry *fe = itofe(cb->fhead);
     time_t expires = TIndexToTime(cb->thead);
@@ -2857,9 +2924,9 @@ PrintCB(register struct CallBack *cb, afs_uint32 now)
     if (fe == NULL)
        return;
 
-    printf("vol=%u vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %d secs at %s",
-          fe->volid, fe->vnode, fe->ncbs, cb->hhead, cb->status, fe->status,
-          expires - now, ctime(&expires));
+    printf("vol=%" AFS_VOLID_FMT " vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %lu secs at %s",
+          afs_printable_VolumeId_lu(fe->volid), fe->vnode, fe->ncbs,
+          cb->hhead, cb->status, fe->status, expires - now, ctime(&expires));
 }
 
 #endif
@@ -2907,9 +2974,8 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
     interfaces = calloc(i, sizeof(struct AddrPort));
     conns = calloc(i, sizeof(struct rx_connection *));
     if (!interfaces || !conns) {
-       ViceLog(0,
-               ("Failed malloc in MultiBreakCallBackAlternateAddress_r\n"));
-       assert(0);
+       ViceLogThenPanic(0, ("Failed malloc in "
+                            "MultiBreakCallBackAlternateAddress_r\n"));
     }
 
     /* initialize alternate rx connections */
@@ -2921,16 +2987,16 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
 
        interfaces[j] = host->interface->interface[i];
        conns[j] =
-           rx_NewConnection(interfaces[j].addr, 
+           rx_NewConnection(interfaces[j].addr,
                             interfaces[j].port, 1, sc, 0);
        rx_SetConnDeadTime(conns[j], 2);
        rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
        j++;
     }
 
-    assert(j);                 /* at least one alternate address */
+    opr_Assert(j);                     /* at least one alternate address */
     ViceLog(125,
-           ("Starting multibreakcall back on all addr for host %x (%s:%d)\n",
+           ("Starting multibreakcall back on all addr for host %p (%s:%d)\n",
              host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
     H_UNLOCK;
     multi_Rx(conns, j) {
@@ -2941,10 +3007,12 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
            if (host->callback_rxcon)
                rx_DestroyConnection(host->callback_rxcon);
            host->callback_rxcon = conns[multi_i];
-            h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
+           /* add then remove */
+           addInterfaceAddr_r(host, interfaces[multi_i].addr,
+                                    interfaces[multi_i].port);
+           removeInterfaceAddr_r(host, host->host, host->port);
            host->host = interfaces[multi_i].addr;
            host->port = interfaces[multi_i].port;
-            h_AddHostToAddrHashTable_r(host->host, host->port, host);
            connSuccess = conns[multi_i];
            rx_SetConnDeadTime(host->callback_rxcon, 50);
            rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
@@ -2974,7 +3042,7 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
 
 
 /*
-** try multi_RX probes to host. 
+** try multi_RX probes to host.
 ** return 0 on success, non-0 on failure
 */
 int
@@ -3003,8 +3071,8 @@ MultiProbeAlternateAddress_r(struct host *host)
     interfaces = calloc(i, sizeof(struct AddrPort));
     conns = calloc(i, sizeof(struct rx_connection *));
     if (!interfaces || !conns) {
-       ViceLog(0, ("Failed malloc in MultiProbeAlternateAddress_r\n"));
-       assert(0);
+       ViceLogThenPanic(0, ("Failed malloc in "
+                            "MultiProbeAlternateAddress_r\n"));
     }
 
     /* initialize alternate rx connections */
@@ -3016,16 +3084,16 @@ MultiProbeAlternateAddress_r(struct host *host)
 
        interfaces[j] = host->interface->interface[i];
        conns[j] =
-           rx_NewConnection(interfaces[j].addr, 
+           rx_NewConnection(interfaces[j].addr,
                             interfaces[j].port, 1, sc, 0);
        rx_SetConnDeadTime(conns[j], 2);
        rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
        j++;
     }
 
-    assert(j);                 /* at least one alternate address */
+    opr_Assert(j);                     /* at least one alternate address */
     ViceLog(125,
-           ("Starting multiprobe on all addr for host %x (%s:%d)\n",
+           ("Starting multiprobe on all addr for host %p (%s:%d)\n",
              host, afs_inet_ntoa_r(host->host, hoststr),
              ntohs(host->port)));
     H_UNLOCK;
@@ -3037,10 +3105,12 @@ MultiProbeAlternateAddress_r(struct host *host)
            if (host->callback_rxcon)
                rx_DestroyConnection(host->callback_rxcon);
            host->callback_rxcon = conns[multi_i];
-            h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
+           /* add then remove */
+           addInterfaceAddr_r(host, interfaces[multi_i].addr,
+                                    interfaces[multi_i].port);
+           removeInterfaceAddr_r(host, host->host, host->port);
            host->host = interfaces[multi_i].addr;
            host->port = interfaces[multi_i].port;
-            h_AddHostToAddrHashTable_r(host->host, host->port, host);
            connSuccess = conns[multi_i];
            rx_SetConnDeadTime(host->callback_rxcon, 50);
            rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
@@ -3055,11 +3125,11 @@ MultiProbeAlternateAddress_r(struct host *host)
                    ("multiprobe failure with addr %s:%d\n",
                     afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
                      ntohs(interfaces[multi_i].port)));
-            
+
             /* This is less than desirable but its the best we can do.
-             * The AFS Cache Manager will return either 0 for a Uuid  
-             * match and a 1 for a non-match.   If the error is 1 we 
-             * therefore know that our mapping of IP address to Uuid 
+             * The AFS Cache Manager will return either 0 for a Uuid
+             * match and a 1 for a non-match.   If the error is 1 we
+             * therefore know that our mapping of IP address to Uuid
              * is wrong.   We should attempt to find the correct
              * Uuid and fix the host tables.
              */