fix-cbd-20051213
[openafs.git] / src / viced / callback.c
index f2187f1..8b25dfc 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID("$Header$");
+RCSID
+    ("$Header$");
 
-#include <stdio.h> 
-#include <stdlib.h>      /* for malloc() */
-#include <time.h>        /* ANSI standard location for time stuff */
+#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>
@@ -105,7 +106,7 @@ RCSID("$Header$");
 
 #include <afs/stds.h>
 
-#include <afs/nfs.h>   /* yuck.  This is an abomination. */
+#include <afs/nfs.h>           /* yuck.  This is an abomination. */
 #include <lwp.h>
 #include <rx/rx.h>
 #include <afscbint.h>
@@ -117,7 +118,7 @@ RCSID("$Header$");
 #include "viced_prototypes.h"
 #include "viced.h"
 
-#include <afs/ptclient.h>  /* need definition of prlist for host.h */
+#include <afs/ptclient.h>      /* need definition of prlist for host.h */
 #include "host.h"
 
 extern afsUUID FS_HostUUID;
@@ -133,49 +134,49 @@ int ShowProblems = 1;
 #define MAX_CB_HOSTS   10
 
 /* max time to break a callback, otherwise client is dead or net is hosed */
-#define MAXCBT 25  
+#define MAXCBT 25
 
 #define u_byte unsigned char
 
 struct cbcounters cbstuff;
 
 struct cbstruct {
-  struct host * hp;
-  afs_uint32 thead;
-} ;
+    struct host *hp;
+    afs_uint32 thead;
+};
 
 struct FileEntry {
-    afs_uint32     vnode;      
-    afs_uint32      unique;
-    afs_uint32     volid;
-    afs_uint32     fnext;
-    afs_uint32     ncbs;
-    afs_uint32     firstcb;
-    afs_uint32      status;
-    afs_uint32      spare;
-} *FE; /* Don't use FE[0] */
+    afs_uint32 vnode;
+    afs_uint32 unique;
+    afs_uint32 volid;
+    afs_uint32 fnext;
+    afs_uint32 ncbs;
+    afs_uint32 firstcb;
+    afs_uint32 status;
+    afs_uint32 spare;
+} *FE;                         /* Don't use FE[0] */
 #define FE_LATER 0x1
 
 struct CallBack {
-    afs_uint32     cnext;              /* Next call back entry */
-    afs_uint32     fhead;              /* Head of this call back chain */
-    u_byte         thead;              /* Head of timeout chain */
-    u_byte         status;             /* Call back status; see definitions, below */
-    afs_uint32     hhead;              /* Head of host table chain */
-    afs_uint32     tprev, tnext;       /* Timeout chain */
-    afs_uint32     hprev, hnext;       /* Chain from host table */
-    unsigned short  spare;              /* make it a multiple of 32 bits. */
-} *CB; /* Don't use CB[0] */
+    afs_uint32 cnext;          /* Next call back entry */
+    afs_uint32 fhead;          /* Head of this call back chain */
+    u_byte thead;              /* Head of timeout chain */
+    u_byte status;             /* Call back status; see definitions, below */
+    afs_uint32 hhead;          /* Head of host table chain */
+    afs_uint32 tprev, tnext;   /* Timeout chain */
+    afs_uint32 hprev, hnext;   /* Chain from host table */
+    unsigned short spare;      /* make it a multiple of 32 bits. */
+} *CB;                         /* Don't use CB[0] */
 
 /* status values for status field of CallBack structure */
-#define CB_NORMAL   1  /* Normal call back */
-#define CB_DELAYED  2  /* Delayed call back due to rpc problems.
-                       The call back entry will be added back to the
-                        host list at the END of the list, so that
-                        searching backwards in the list will find all
-                        the (consecutive)host. delayed call back entries */
-#define CB_VOLUME   3  /* Callback for a volume */
-#define CB_BULK     4  /* Normal callbacks, handed out from FetchBulkStatus */
+#define CB_NORMAL   1          /* Normal call back */
+#define CB_DELAYED  2          /* Delayed call back due to rpc problems.
+                                * The call back entry will be added back to the
+                                * host list at the END of the list, so that
+                                * searching backwards in the list will find all
+                                * the (consecutive)host. delayed call back entries */
+#define CB_VOLUME   3          /* Callback for a volume */
+#define CB_BULK     4          /* Normal callbacks, handed out from FetchBulkStatus */
 
 /* call back indices to pointers, and vice-versa */
 #define itocb(i)    ((i)?CB+(i):0)
@@ -215,18 +216,18 @@ struct CallBack {
 
 static int TimeOuts[] = {
 /* Note: don't make the first entry larger than 4 hours (see above) */
-    4*60*60,   /* 0-7 users */
-    1*60*60,   /* 8-15 users */
-    30*60,     /* 16-23 users */
-    15*60,     /* 24-31 users */
-    15*60,     /* 32-39 users */
-    10*60,     /* 40-47 users */
-    10*60,     /* 48-55 users */
-    10*60,     /* 56-63 users */
-};  /* Anything more: MinTimeOut */
+    4 * 60 * 60,               /* 0-7 users */
+    1 * 60 * 60,               /* 8-15 users */
+    30 * 60,                   /* 16-23 users */
+    15 * 60,                   /* 24-31 users */
+    15 * 60,                   /* 32-39 users */
+    10 * 60,                   /* 40-47 users */
+    10 * 60,                   /* 48-55 users */
+    10 * 60,                   /* 56-63 users */
+};                             /* Anything more: MinTimeOut */
 
 /* minimum time given for a call back */
-static int MinTimeOut = (7*60);
+static int MinTimeOut = (7 * 60);
 
 #define TimeOutCutoff   ((sizeof(TimeOuts)/sizeof(TimeOuts[0]))*8)
 #define TimeOut(nusers)  ((nusers)>=TimeOutCutoff? MinTimeOut: TimeOuts[(nusers)>>3])
@@ -265,35 +266,42 @@ struct object {
 };
 
 struct VCBParams {
-  struct cbstruct cba[MAX_CB_HOSTS];  /* re-entrant storage */
-  unsigned int ncbas;
-  afs_uint32 thead;     /* head of timeout queue for youngest callback */
-  struct AFSFid *fid;
+    struct cbstruct cba[MAX_CB_HOSTS]; /* re-entrant storage */
+    unsigned int ncbas;
+    afs_uint32 thead;          /* head of timeout queue for youngest callback */
+    struct AFSFid *fid;
 };
 
 struct CallBack *CBfree = 0;
 struct FileEntry *FEfree = 0;
 
 /* Prototypes for static routines */
-static struct FileEntry *FindFE (register AFSFid *fid);
+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 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 int CDel(struct CallBack *cb, int deletefe);
-static int CDelPtr(register struct FileEntry *fe, register afs_uint32 *cbp, int deletefe);
+static int CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
+                  int deletefe);
 static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host);
 static int FDel(register 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 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 lih_r(register struct host *host, register int held, register struct host *hostp);
+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 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 lih_r(register struct host *host, register int held,
+                register struct host *hostp);
 static int GetSomeSpace_r(struct host *hostp, int locked);
 static int ClearHostCallbacks_r(struct host *hp, int locked);
 
@@ -305,21 +313,22 @@ static int ClearHostCallbacks_r(struct host *hp, int locked);
 /* Other protos - move out sometime */
 extern void ShutDown();
 
-#define VHASH 512      /* Power of 2 */
-static afs_uint32 HashTable[VHASH]; /* File entry hash table */
+#define VHASH 512              /* Power of 2 */
+static afs_uint32 HashTable[VHASH];    /* File entry hash table */
 #define VHash(volume, unique) (((volume)+(unique))&(VHASH-1))
 
-static struct FileEntry *FindFE (register AFSFid *fid)
+static struct FileEntry *
+FindFE(register AFSFid * fid)
 {
     int hash;
     register int fei;
     register struct FileEntry *fe;
 
     hash = VHash(fid->Volume, fid->Unique);
-    for (fei=HashTable[hash]; fei; fei = fe->fnext) {
+    for (fei = HashTable[hash]; fei; fei = fe->fnext) {
        fe = itofe(fei);
-       if (fe->volid == fid->Volume && fe->unique == fid->Unique &&
-           fe->vnode == fid->Vnode) 
+       if (fe->volid == fid->Volume && fe->unique == fid->Unique
+           && fe->vnode == fid->Vnode && (fe->status & FE_LATER) != FE_LATER)
            return fe;
     }
     return 0;
@@ -327,7 +336,8 @@ static struct FileEntry *FindFE (register AFSFid *fid)
 
 #ifndef INTERPRET_DUMP
 
-static struct CallBack *iGetCB(register int *nused)
+static struct CallBack *
+iGetCB(register int *nused)
 {
     register struct CallBack *ret;
 
@@ -338,7 +348,8 @@ static struct CallBack *iGetCB(register int *nused)
     return ret;
 }
 
-static int iFreeCB(register struct CallBack *cb, register int *nused)
+static int
+iFreeCB(register struct CallBack *cb, register int *nused)
 {
     ((struct object *)cb)->next = (struct object *)CBfree;
     CBfree = cb;
@@ -346,7 +357,8 @@ static int iFreeCB(register struct CallBack *cb, register int *nused)
     return 0;
 }
 
-static struct FileEntry *iGetFE(register int *nused)
+static struct FileEntry *
+iGetFE(register int *nused)
 {
     register struct FileEntry *ret;
 
@@ -357,7 +369,8 @@ static struct FileEntry *iGetFE(register int *nused)
     return ret;
 }
 
-static int iFreeFE(register struct FileEntry *fe, register int *nused)
+static int
+iFreeFE(register struct FileEntry *fe, register int *nused)
 {
     ((struct object *)fe)->next = (struct object *)FEfree;
     FEfree = fe;
@@ -366,7 +379,8 @@ static int 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)
+static int
+TAdd(register struct CallBack *cb, register afs_uint32 * thead)
 {
     if (!*thead) {
        (*thead) = cb->tnext = cb->tprev = cbtoi(cb);
@@ -376,10 +390,10 @@ static int TAdd(register struct CallBack *cb, register afs_uint32 *thead)
        cb->tprev = thp->tprev;
        cb->tnext = *thead;
        if (thp) {
-         if (thp->tprev) 
-           thp->tprev = (itocb(thp->tprev)->tnext = cbtoi(cb));
-         else 
-           thp->tprev = cbtoi(cb);
+           if (thp->tprev)
+               thp->tprev = (itocb(thp->tprev)->tnext = cbtoi(cb));
+           else
+               thp->tprev = cbtoi(cb);
        }
     }
     cb->thead = ttoi(thead);
@@ -387,27 +401,28 @@ static int TAdd(register struct CallBack *cb, register afs_uint32 *thead)
 }
 
 /* Delete call back entry from timeout list */
-static int TDel(register struct CallBack *cb)
+static int
+TDel(register struct CallBack *cb)
 {
     register afs_uint32 *thead = itot(cb->thead);
 
     if (*thead == cbtoi(cb))
-       *thead = (*thead == cb->tnext? 0: cb->tnext);
-    if (itocb(cb->tprev)) 
-      itocb(cb->tprev)->tnext = cb->tnext;
-    if (itocb(cb->tnext)) 
-      itocb(cb->tnext)->tprev = cb->tprev;
+       *thead = (*thead == cb->tnext ? 0 : cb->tnext);
+    if (itocb(cb->tprev))
+       itocb(cb->tprev)->tnext = cb->tnext;
+    if (itocb(cb->tnext))
+       itocb(cb->tnext)->tprev = cb->tprev;
     return 0;
 }
 
 /* Add cb to end of specified host list */
-static int HAdd(register struct CallBack *cb, register struct host *host)
+static int
+HAdd(register struct CallBack *cb, register struct host *host)
 {
     cb->hhead = h_htoi(host);
     if (!host->cblist) {
-        host->cblist = cb->hnext = cb->hprev = cbtoi(cb);
-    }
-    else {
+       host->cblist = cb->hnext = cb->hprev = cbtoi(cb);
+    } else {
        register struct CallBack *hhp = itocb(host->cblist);
 
        cb->hprev = hhp->hprev;
@@ -418,12 +433,13 @@ static int HAdd(register struct CallBack *cb, register struct host *host)
 }
 
 /* Delete call back entry from host list */
-static int HDel(register struct CallBack *cb)
+static int
+HDel(register struct CallBack *cb)
 {
     register afs_uint32 *hhead = &h_itoh(cb->hhead)->cblist;
 
     if (*hhead == cbtoi(cb))
-       *hhead = (*hhead == cb->hnext? 0: cb->hnext);
+       *hhead = (*hhead == cb->hnext ? 0 : cb->hnext);
     itocb(cb->hprev)->hnext = cb->hnext;
     itocb(cb->hnext)->hprev = cb->hprev;
     return 0;
@@ -433,21 +449,24 @@ static int HDel(register struct CallBack *cb)
 /* N.B.  This one also deletes the CB, and also possibly parent FE, so
  * make sure that it is not on any other list before calling this
  * routine */
-static int CDel(struct CallBack *cb, int deletefe)
+static int
+CDel(struct CallBack *cb, int deletefe)
 {
     int cbi = cbtoi(cb);
     struct FileEntry *fe = itofe(cb->fhead);
     register afs_uint32 *cbp;
     register int safety;
 
-    for (safety = 0, cbp = &fe->firstcb; *cbp && *cbp != cbi; 
+    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();
-       ShutDownAndCore(PANIC);
-      }
+       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();
+           ShutDownAndCore(PANIC);
+       }
     }
     CDelPtr(fe, cbp, deletefe);
     return 0;
@@ -458,10 +477,11 @@ static int CDel(struct CallBack *cb, int deletefe)
 /* N.B.  This one also deletes the CB, and also possibly parent FE, so
  * make sure that it is not on any other list before calling this
  * routine */
-int Ccdelpt=0, CcdelB=0;
+int Ccdelpt = 0, CcdelB = 0;
 
-static int CDelPtr(register struct FileEntry *fe, 
-       register afs_uint32 *cbp, int deletefe)
+static int
+CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
+       int deletefe)
 {
     register struct CallBack *cb;
 
@@ -469,7 +489,7 @@ static int CDelPtr(register struct FileEntry *fe,
        return 0;
     Ccdelpt++;
     cb = itocb(*cbp);
-    if (cb != &CB[*cbp]) 
+    if (cb != &CB[*cbp])
        CcdelB++;
     *cbp = cb->cnext;
     FreeCB(cb);
@@ -478,7 +498,8 @@ static int CDelPtr(register struct FileEntry *fe,
     return 0;
 }
 
-static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host)
+static afs_uint32 *
+FindCBPtr(struct FileEntry *fe, struct host *host)
 {
     register afs_uint32 hostindex = h_htoi(host);
     register struct CallBack *cb;
@@ -487,9 +508,9 @@ static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host)
 
     for (safety = 0, cbp = &fe->firstcb; *cbp; cbp = &cb->cnext, safety++) {
        if (safety > cbstuff.nblks) {
-         ViceLog(0,("FindCBPtr: Internal Error -- shutting down.\n"));
-         DumpCallBackState();
-         ShutDownAndCore(PANIC);
+           ViceLog(0, ("FindCBPtr: Internal Error -- shutting down.\n"));
+           DumpCallBackState();
+           ShutDownAndCore(PANIC);
        }
        cb = itocb(*cbp);
        if (cb->hhead == hostindex)
@@ -499,7 +520,8 @@ static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host)
 }
 
 /* Delete file entry from hash table */
-static int FDel(register struct FileEntry *fe)
+static int
+FDel(register struct FileEntry *fe)
 {
     register int fei = fetoi(fe);
     register afs_uint32 *p = &HashTable[VHash(fe->volid, fe->unique)];
@@ -512,36 +534,37 @@ static int FDel(register struct FileEntry *fe)
     return 0;
 }
 
-int InitCallBack(int nblks)
+int
+InitCallBack(int nblks)
 {
-    H_LOCK
+    H_LOCK;
     tfirst = CBtime(FT_ApproxTime());
     /* 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))))-1;
+     * FE[0] and CB[0] are not used--and not allocated */
+    FE = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry)))) - 1;
     if (!FE) {
        ViceLog(0, ("Failed malloc in InitCallBack\n"));
        assert(0);
     }
     cbstuff.nFEs = nblks;
     while (cbstuff.nFEs)
-       FreeFE(&FE[cbstuff.nFEs]); /* This is correct */
-    CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))))-1;
+       FreeFE(&FE[cbstuff.nFEs]);      /* This is correct */
+    CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack)))) - 1;
     if (!CB) {
        ViceLog(0, ("Failed malloc in InitCallBack\n"));
        assert(0);
     }
     cbstuff.nCBs = nblks;
     while (cbstuff.nCBs)
-       FreeCB(&CB[cbstuff.nCBs]); /* This is correct */
+       FreeCB(&CB[cbstuff.nCBs]);      /* This is correct */
     cbstuff.nblks = nblks;
     cbstuff.nbreakers = 0;
-    H_UNLOCK
+    H_UNLOCK;
     return 0;
 }
 
-afs_int32 XCallBackBulk_r(struct host *ahost, struct AFSFid *fids, 
-       afs_int32 nfids)
+afs_int32
+XCallBackBulk_r(struct host * ahost, struct AFSFid * fids, afs_int32 nfids)
 {
     struct AFSCallBack tcbs[AFSCBMAX];
     register int i;
@@ -549,9 +572,10 @@ afs_int32 XCallBackBulk_r(struct host *ahost, struct AFSFid *fids,
     struct AFSCBs tc;
     int code;
     int j;
+    struct rx_connection *cb_conn = NULL;
 
 #ifdef ADAPT_MTU
-    rx_SetConnDeadTime(ahost->callback_rxcon, 4); 
+    rx_SetConnDeadTime(ahost->callback_rxcon, 4);
     rx_SetConnHardDeadTime(ahost->callback_rxcon, AFS_HARDDEADTIME);
 #endif
 
@@ -559,21 +583,25 @@ afs_int32 XCallBackBulk_r(struct host *ahost, struct AFSFid *fids,
     j = 0;
     while (nfids > 0) {
 
-      for(i=0;i<nfids && i < AFSCBMAX;i++) {
-       tcbs[i].CallBackVersion = CALLBACK_VERSION;
-       tcbs[i].ExpirationTime = 0;
-       tcbs[i].CallBackType = CB_DROPPED;
-      }
-      tf.AFSCBFids_len = i;
-      tf.AFSCBFids_val = &(fids[j]);
-      nfids -= i;
-      j += i;
-      tc.AFSCBs_len = i;
-      tc.AFSCBs_val = tcbs;
-
-      H_UNLOCK
-      code |= RXAFSCB_CallBack(ahost->callback_rxcon, &tf, &tc);
-      H_LOCK
+       for (i = 0; i < nfids && i < AFSCBMAX; i++) {
+           tcbs[i].CallBackVersion = CALLBACK_VERSION;
+           tcbs[i].ExpirationTime = 0;
+           tcbs[i].CallBackType = CB_DROPPED;
+       }
+       tf.AFSCBFids_len = i;
+       tf.AFSCBFids_val = &(fids[j]);
+       nfids -= i;
+       j += i;
+       tc.AFSCBs_len = i;
+       tc.AFSCBs_val = tcbs;
+
+       cb_conn = ahost->callback_rxcon;
+       rx_GetConnection(cb_conn);
+       H_UNLOCK;
+       code |= RXAFSCB_CallBack(cb_conn, &tf, &tc);
+       rx_PutConnection(cb_conn);
+       cb_conn = NULL;
+       H_LOCK;
     }
 
     return code;
@@ -588,25 +616,27 @@ afs_int32 XCallBackBulk_r(struct host *ahost, struct AFSFid *fids,
  * as well. If so, the host->ResetDone should probably be set to 0,
  * and we probably don't want to return a callback promise to the
  * cache manager, either. */
-int AddCallBack1(struct host *host, AFSFid *fid, afs_uint32 *thead, 
-       int type, int locked)
+int
+AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
+            int locked)
 {
     int retVal;
-    H_LOCK
-    if ( !locked ) {
+    H_LOCK;
+    if (!locked) {
        h_Lock_r(host);
     }
     retVal = AddCallBack1_r(host, fid, thead, type, 1);
 
-    if ( !locked ) {
+    if (!locked) {
        h_Unlock_r(host);
     }
-    H_UNLOCK
+    H_UNLOCK;
     return retVal;
 }
 
-static int AddCallBack1_r(struct host *host, AFSFid *fid, afs_uint32 *thead, 
-       int type, int locked)
+static int
+AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
+              int locked)
 {
     struct FileEntry *fe;
     struct CallBack *cb = 0, *lastcb = 0;
@@ -615,42 +645,43 @@ static int AddCallBack1_r(struct host *host, AFSFid *fid, afs_uint32 *thead,
     afs_uint32 *Thead = thead;
     struct CallBack *newcb = 0;
     int safety;
-    
+
     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 
-       to avoid races with FindFE... */
+     * the host structure locked -- or we might deadlock. However, we have 
+     * to avoid races with FindFE... */
     while (!(newcb = GetCB())) {
        GetSomeSpace_r(host, locked);
     }
-    while(!(newfe = GetFE())) {  /* Get it now, so we don't have to call */
-      /* GetSomeSpace with the host locked, later.  This might turn out to */
-      /* have been unneccessary, but that's actually kind of unlikely, since */
-      /* most files are not shared. */
-      GetSomeSpace_r(host, locked);
+    while (!(newfe = GetFE())) {       /* Get it now, so we don't have to call */
+       /* GetSomeSpace with the host locked, later.  This might turn out to */
+       /* have been unneccessary, but that's actually kind of unlikely, since */
+       /* most files are not shared. */
+       GetSomeSpace_r(host, locked);
     }
 
     if (!locked) {
-      h_Lock_r(host);  /* this can yield, so do it before we get any */
-                    /* fragile info */
+       h_Lock_r(host);         /* this can yield, so do it before we get any */
+       /* fragile info */
     }
 
     fe = FindFE(fid);
     if (type == CB_NORMAL) {
-       time_out = TimeCeiling(FT_ApproxTime()+TimeOut(fe?fe->ncbs:0)+ServerBias);
+       time_out =
+           TimeCeiling(FT_ApproxTime() + TimeOut(fe ? fe->ncbs : 0) +
+                       ServerBias);
        Thead = THead(CBtime(time_out));
-    }
-    else if (type == CB_VOLUME) {
-       time_out = TimeCeiling((60*120+FT_ApproxTime())+ServerBias);
+    } else if (type == CB_VOLUME) {
+       time_out = TimeCeiling((60 * 120 + FT_ApproxTime()) + 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 
-                              + TimeOut(22 + (fe?fe->ncbs:0)));
+    } 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 +
+                       TimeOut(22 + (fe ? fe->ncbs : 0)));
        Thead = THead(CBtime(time_out));
     }
 
@@ -662,25 +693,26 @@ static int AddCallBack1_r(struct host *host, AFSFid *fid, afs_uint32 *thead,
        fe = newfe;
        newfe = NULL;
        fe->firstcb = 0;
-        fe->volid = fid->Volume;
+       fe->volid = fid->Volume;
        fe->vnode = fid->Vnode;
        fe->unique = fid->Unique;
        fe->ncbs = 0;
-        hash = VHash(fid->Volume, fid->Unique);
+       fe->status = 0;
+       hash = VHash(fid->Volume, fid->Unique);
        fe->fnext = HashTable[hash];
        HashTable[hash] = fetoi(fe);
     }
     for (safety = 0, lastcb = cb = itocb(fe->firstcb); cb;
         lastcb = cb, cb = itocb(cb->cnext), safety++) {
        if (safety > cbstuff.nblks) {
-         ViceLog(0,("AddCallBack1: Internal Error -- shutting down.\n"));
-         DumpCallBackState();
-         ShutDownAndCore(PANIC);
+           ViceLog(0, ("AddCallBack1: Internal Error -- shutting down.\n"));
+           DumpCallBackState();
+           ShutDownAndCore(PANIC);
        }
        if (cb->hhead == h_htoi(host))
            break;
     }
-    if (cb) {/* Already have call back:  move to new timeout list */
+    if (cb) {                  /* Already have call back:  move to new timeout list */
        /* don't change delayed callbacks back to normal ones */
        if (cb->status != CB_DELAYED)
            cb->status = type;
@@ -689,10 +721,15 @@ static int AddCallBack1_r(struct host *host, AFSFid *fid, afs_uint32 *thead,
            TDel(cb);
            TAdd(cb, Thead);
        }
+       if (newfe == NULL) {    /* we are using the new FE */
+            fe->firstcb = cbtoi(cb);
+            fe->ncbs++;
+            cb->fhead = fetoi(fe);
+        }
     } else {
        cb = newcb;
        newcb = NULL;
-       *(lastcb?&lastcb->cnext:&fe->firstcb) = cbtoi(cb);
+       *(lastcb ? &lastcb->cnext : &fe->firstcb) = cbtoi(cb);
        fe->ncbs++;
        cb->cnext = 0;
        cb->fhead = fetoi(fe);
@@ -702,14 +739,16 @@ static int AddCallBack1_r(struct host *host, AFSFid *fid, afs_uint32 *thead,
     }
 
     /* now free any still-unused callback or host entries */
-    if (newcb) FreeCB(newcb);
-    if (newfe) FreeFE(newfe);
+    if (newcb)
+       FreeCB(newcb);
+    if (newfe)
+       FreeFE(newfe);
 
-    if (!locked)         /* freecb and freefe might(?) yield */
+    if (!locked)               /* freecb and freefe might(?) yield */
        h_Unlock_r(host);
 
-    if (type == CB_NORMAL || type == CB_VOLUME || type == CB_BULK ) 
-       return time_out-ServerBias; /* Expires sooner at workstation */
+    if (type == CB_NORMAL || type == CB_VOLUME || type == CB_BULK)
+       return time_out - ServerBias;   /* Expires sooner at workstation */
 
     return 0;
 }
@@ -732,99 +771,116 @@ static int AddCallBack1_r(struct host *host, AFSFid *fid, afs_uint32 *thead,
  * which is sufficient only if VenusDown = 0 only happens when the
  * lock is held over the RPC and the subsequent VenusDown == 0
  * wherever that is done. */
-static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas, 
-       struct AFSCBFids *afidp, struct host *xhost)
+static void
+MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
+                    struct AFSCBFids *afidp, struct host *xhost)
 {
-  int i,j;
-  struct rx_connection *conns[MAX_CB_HOSTS];
-  int opt_TO;  /* secs, but internal adaptive parms are in ms */
-  static struct AFSCBs tc = {0,0};
+    int i, j;
+    struct rx_connection *conns[MAX_CB_HOSTS];
+    static struct AFSCBs tc = { 0, 0 };
 
-  assert(ncbas <= MAX_CB_HOSTS);
+    assert(ncbas <= MAX_CB_HOSTS);
+
+    /* set up conns for multi-call */
+    for (i = 0, j = 0; i < ncbas; i++) {
+       struct host *thishost = cba[i].hp;
+       if (!thishost || (thishost->hostFlags & HOSTDELETED)) {
+           continue;
+       }
+       rx_GetConnection(thishost->callback_rxcon);
+       conns[j++] = thishost->callback_rxcon;
 
-  /* set up conns for multi-call */
-  for (i=0,j=0; i<ncbas; i++) {
-    struct host *thishost = cba[i].hp;
-    if (!thishost || (thishost->hostFlags & HOSTDELETED)) {
-      continue;
-    }
-    conns[j++] = thishost->callback_rxcon;
-       
 #ifdef ADAPT_MTU
-    rx_SetConnDeadTime (thishost->callback_rxcon, 4); 
-    rx_SetConnHardDeadTime (thishost->callback_rxcon, AFS_HARDDEADTIME); 
+       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? */
-    cbstuff.nbreakers++;
-  H_UNLOCK
-  multi_Rx(conns, j) {
-    multi_RXAFSCB_CallBack(afidp, &tc);
-    if (multi_error) {
-      afs_uint32 idx ;
-      struct host *hp;
-      char hoststr[16];
-
-      idx = 0;
-      /* If there's an error, we have to hunt for the right host. 
-       * The conns array _should_ correspond one-to-one to the cba
-       * array, except in some rare cases it might be missing one 
-       * or more elements.  So the optimistic case is almost 
-       * always right.  At worst, it's the starting point for the 
-       * hunt. */
-      for (hp=0,i=multi_i;i<j;i++) { 
-       hp = cba[i].hp;   /* optimistic, but usually right */
-       if (!hp) {
-         break;
-       }
-       if (conns[multi_i] == hp->callback_rxcon) {
-         idx = cba[i].thead;
-         break;
-       }
-      }
-      
-      if (!hp) {
-       ViceLog(0, ("BCB: INTERNAL ERROR: hp=%x, cba=%x\n",hp,cba));
-      }
-      else {
-       /* 
-       ** try breaking callbacks on alternate interface addresses
-       */
-       if ( MultiBreakCallBackAlternateAddress(hp, afidp) )
-       {
-         if (ShowProblems) {
-               ViceLog(7, 
-                 ("BCB: Failed on file %u.%u.%u, host %s:%d is down\n",
-                  afidp->AFSCBFids_val->Volume, afidp->AFSCBFids_val->Vnode,
-                  afidp->AFSCBFids_val->Unique, afs_inet_ntoa_r(hp->host,hoststr), ntohs(hp->port)));
+    }
+
+    if (j) {                   /* who knows what multi would do with 0 conns? */
+       cbstuff.nbreakers++;
+       H_UNLOCK;
+       multi_Rx(conns, j) {
+           multi_RXAFSCB_CallBack(afidp, &tc);
+           if (multi_error) {
+               afs_uint32 idx;
+               struct host *hp;
+               char hoststr[16];
+
+               idx = 0;
+               /* If there's an error, we have to hunt for the right host. 
+                * The conns array _should_ correspond one-to-one to the cba
+                * array, except in some rare cases it might be missing one 
+                * or more elements.  So the optimistic case is almost 
+                * always right.  At worst, it's the starting point for the 
+                * hunt. */
+               for (hp = 0, i = multi_i; i < j; i++) {
+                   hp = cba[i].hp;     /* optimistic, but usually right */
+                   if (!hp) {
+                       break;
+                   }
+                   if (conns[multi_i] == hp->callback_rxcon) {
+                       idx = cba[i].thead;
+                       break;
+                   }
                }
 
-               H_LOCK
-               h_Lock_r(hp);
-               hp->hostFlags |= VENUSDOWN;
-               /**
+               if (!hp) {
+                   ViceLog(0,
+                           ("BCB: INTERNAL ERROR: hp=%x, cba=%x\n", hp,
+                            cba));
+               } else {
+                   /* 
+                    ** try breaking callbacks on alternate interface addresses
+                    */
+                   if (MultiBreakCallBackAlternateAddress(hp, afidp)) {
+                       if (ShowProblems) {
+                           ViceLog(7,
+                                   ("BCB: Failed on file %u.%u.%u, host %s:%d is down\n",
+                                    afidp->AFSCBFids_val->Volume,
+                                    afidp->AFSCBFids_val->Vnode,
+                                    afidp->AFSCBFids_val->Unique,
+                                    afs_inet_ntoa_r(hp->host, hoststr),
+                                    ntohs(hp->port)));
+                       }
+
+                       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_UNLOCK
+                       AddCallBack1_r(hp, afidp->AFSCBFids_val, itot(idx),
+                                      CB_DELAYED, 1);
+                       h_Unlock_r(hp); 
+                       H_UNLOCK;
+                   }
+               }
+           }
        }
-      }
-    }
-  } multi_End;
-  H_LOCK
-  cbstuff.nbreakers--;
-  }
-  
-  for (i=0; i<ncbas; i++) {
-    struct host *hp;
-    hp = cba[i].hp;
-    if (hp && xhost != hp)
-      h_Release_r(hp);
-  }
+       multi_End;
+       H_LOCK;
+       cbstuff.nbreakers--;
+    }
+
+    for (i = 0; i < ncbas; i++) {
+       struct host *hp;
+       hp = cba[i].hp;
+       if (hp && xhost != hp) {
+           h_Release_r(hp);
+       }
+    }
+
+    /* H_UNLOCK around this so h_FreeConnection does not deadlock.
+       h_FreeConnection should *never* be called on a callback connection,
+       but on 10/27/04 a deadlock occurred where it was, when we know why,
+       this should be reverted. -- shadow */
+    H_UNLOCK;
+    for (i = 0; i < j; i++) {
+       rx_PutConnection(conns[i]);
+    }
+    H_LOCK;
 
-  return;
+    return;
 }
 
 /*
@@ -837,118 +893,122 @@ static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
  * 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 BreakCallBack(struct host *xhost, AFSFid *fid, int flag)
+int
+BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
 {
     struct FileEntry *fe;
     struct CallBack *cb, *nextcb;
     struct cbstruct cba[MAX_CB_HOSTS];
     int ncbas;
-    struct rx_connection *conns[MAX_CB_HOSTS];
     struct AFSCBFids tf;
     int hostindex;
     char hoststr[16];
 
-    ViceLog(7,("BCB: BreakCallBack(all but %s:%d, (%u,%u,%u))\n",
-              afs_inet_ntoa_r(xhost->host,hoststr), ntohs(xhost->port), fid->Volume, fid->Vnode, 
-              fid->Unique));
-
-    H_LOCK
+    ViceLog(7,
+           ("BCB: BreakCallBack(all but %s:%d, (%u,%u,%u))\n",
+            afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
+            fid->Volume, fid->Vnode, fid->Unique));
 
-    cbstuff.BreakCallBacks++; 
+    H_LOCK;
+    cbstuff.BreakCallBacks++;
     fe = FindFE(fid);
     if (!fe) {
-      goto done;
+       goto done;
     }
     hostindex = h_htoi(xhost);
     cb = itocb(fe->firstcb);
     if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
-      /* the most common case is what follows the || */
-      goto done;
+       /* the most common case is what follows the || */
+       goto done;
     }
     tf.AFSCBFids_len = 1;
     tf.AFSCBFids_val = fid;
 
-    for(;cb;) { 
-      for (ncbas=0; cb && ncbas<MAX_CB_HOSTS; cb=nextcb) {
-       nextcb = itocb(cb->cnext);
-       if ((cb->hhead != hostindex || flag) 
-           && (cb->status == CB_BULK || cb->status == CB_NORMAL
-               || cb->status == CB_VOLUME) ) {
-         struct host *thishost = h_itoh(cb->hhead);
-         if (!thishost) {
-           ViceLog(0,("BCB: BOGUS! cb->hhead is NULL!\n"));
-         }
-         else if (thishost->hostFlags & VENUSDOWN) {
-           ViceLog(7,("BCB: %s:%d is down; delaying break call back\n",
-                      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++;
-           TDel(cb);
-           HDel(cb);
-           CDel(cb, 1); /* Usually first; so this delete 
-                           is reasonably inexpensive */
-         }
+    for (; cb;) {
+       for (ncbas = 0; cb && ncbas < MAX_CB_HOSTS; cb = nextcb) {
+           nextcb = itocb(cb->cnext);
+           if ((cb->hhead != hostindex || flag)
+               && (cb->status == CB_BULK || cb->status == CB_NORMAL
+                   || cb->status == CB_VOLUME)) {
+               struct host *thishost = h_itoh(cb->hhead);
+               if (!thishost) {
+                   ViceLog(0, ("BCB: BOGUS! cb->hhead is NULL!\n"));
+               } else if (thishost->hostFlags & VENUSDOWN) {
+                   ViceLog(7,
+                           ("BCB: %s:%d is down; delaying break call back\n",
+                            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++;
+                   TDel(cb);
+                   HDel(cb);
+                   CDel(cb, 1);        /* Usually first; so this delete 
+                                        * is reasonably inexpensive */
+               }
+           }
+       }
+
+       if (ncbas) {
+           MultiBreakCallBack_r(cba, ncbas, &tf, xhost);
+
+           /* 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;
+           }
        }
-      }
-      
-      if (ncbas) {
-       MultiBreakCallBack_r(cba, ncbas, &tf, xhost);
-
-       /* 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;
-       }
-      }
     }
 
   done:
-    H_UNLOCK
+    H_UNLOCK;
     return 0;
 }
 
 /* Delete (do not break) single call back for fid */
-int DeleteCallBack(struct host *host, AFSFid *fid)
+int
+DeleteCallBack(struct host *host, AFSFid * fid)
 {
     register struct FileEntry *fe;
     register afs_uint32 *pcb;
     char hoststr[16];
 
     cbstuff.DeleteCallBacks++;
-    
-    H_LOCK
+
+    H_LOCK;
     h_Lock_r(host);
     fe = FindFE(fid);
     if (!fe) {
-        h_Unlock_r(host);
-       H_UNLOCK
-       ViceLog(8,("DCB: No call backs for fid (%u, %u, %u)\n",
-           fid->Volume, fid->Vnode, fid->Unique));
+       h_Unlock_r(host);
+       H_UNLOCK;
+       ViceLog(8,
+               ("DCB: No call backs for fid (%u, %u, %u)\n", fid->Volume,
+                fid->Vnode, fid->Unique));
        return 0;
     }
     pcb = FindCBPtr(fe, host);
     if (!*pcb) {
-       ViceLog(8,("DCB: No call back for host %s:%d, (%u, %u, %u)\n",
-           afs_inet_ntoa_r(host->host,hoststr), ntohs(host->port), fid->Volume, fid->Vnode, fid->Unique));
+       ViceLog(8,
+               ("DCB: No call back for host %s:%d, (%u, %u, %u)\n",
+                afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
+                fid->Volume, fid->Vnode, fid->Unique));
        h_Unlock_r(host);
-       H_UNLOCK
+       H_UNLOCK;
        return 0;
     }
     HDel(itocb(*pcb));
     TDel(itocb(*pcb));
     CDelPtr(fe, pcb, 1);
     h_Unlock_r(host);
-    H_UNLOCK
+    H_UNLOCK;
     return 0;
 }
 
@@ -958,23 +1018,25 @@ int DeleteCallBack(struct host *host, AFSFid *fid)
  * since we're not adding callbacks, but deleting them.  I'm not sure
  * why it doesn't set the lock, however; perhaps it should.
  */
-int DeleteFileCallBacks(AFSFid *fid)
+int
+DeleteFileCallBacks(AFSFid * fid)
 {
     register struct FileEntry *fe;
     register struct CallBack *cb;
     register afs_uint32 cbi;
     register int n;
 
-    H_LOCK
+    H_LOCK;
     cbstuff.DeleteFiles++;
     fe = FindFE(fid);
     if (!fe) {
-       H_UNLOCK
-       ViceLog(8,("DF: No fid (%u,%u,%u) to delete\n",
-           fid->Volume, fid->Vnode, fid->Unique));
+       H_UNLOCK;
+       ViceLog(8,
+               ("DF: No fid (%u,%u,%u) to delete\n", fid->Volume, fid->Vnode,
+                fid->Unique));
        return 0;
     }
-    for (n=0,cbi = fe->firstcb; cbi; n++) {
+    for (n = 0, cbi = fe->firstcb; cbi; n++) {
        cb = itocb(cbi);
        cbi = cb->cnext;
        TDel(cb);
@@ -982,13 +1044,14 @@ int DeleteFileCallBacks(AFSFid *fid)
        FreeCB(cb);
     }
     FDel(fe);
-    H_UNLOCK
+    H_UNLOCK;
     return 0;
 }
 
 /* Delete (do not break) all call backs for host.  The host should be
  * locked. */
-int DeleteAllCallBacks_r(struct host *host, int deletefe)
+int
+DeleteAllCallBacks_r(struct host *host, int deletefe)
 {
     register struct CallBack *cb;
     register int cbi, first;
@@ -996,10 +1059,10 @@ int DeleteAllCallBacks_r(struct host *host, int deletefe)
     cbstuff.DeleteAllCallBacks++;
     cbi = first = host->cblist;
     if (!cbi) {
-       ViceLog(8,("DV: no call backs\n"));
+       ViceLog(8, ("DV: no call backs\n"));
        return 0;
     }
-    do {       
+    do {
        cb = itocb(cbi);
        cbi = cb->hnext;
        TDel(cb);
@@ -1014,110 +1077,119 @@ int DeleteAllCallBacks_r(struct host *host, int deletefe)
  * successfully broken; 0 otherwise.  Assumes host is h_Held and h_Locked.
  * Must be called with VenusDown set for this host
  */
-int BreakDelayedCallBacks(struct host *host)
+int
+BreakDelayedCallBacks(struct host *host)
 {
     int retVal;
-    H_LOCK
+    H_LOCK;
     retVal = BreakDelayedCallBacks_r(host);
-    H_UNLOCK
+    H_UNLOCK;
     return retVal;
 }
 
-int BreakDelayedCallBacks_r(struct host *host)
+int
+BreakDelayedCallBacks_r(struct host *host)
 {
     struct AFSFid fids[AFSCBMAX];
-    u_byte thead[AFSCBMAX]; /* This should match thead in struct Callback */
+    u_byte thead[AFSCBMAX];    /* This should match thead in struct Callback */
     int cbi, first, nfids;
     struct CallBack *cb;
-    struct interfaceAddr interf;
     int code;
     char hoststr[16];
+    struct rx_connection *cb_conn;
 
     cbstuff.nbreakers++;
     if (!(host->hostFlags & RESETDONE) && !(host->hostFlags & HOSTDELETED)) {
-       host->hostFlags &= ~ALTADDR; /* alterrnate addresses are invalid */
-       if ( host->interface ) {
-           H_UNLOCK
-           code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
-                                             &FS_HostUUID);
-           H_LOCK
+       host->hostFlags &= ~ALTADDR;    /* alterrnate addresses are invalid */
+       cb_conn = host->callback_rxcon;
+       rx_GetConnection(cb_conn);
+       if (host->interface) {
+           H_UNLOCK;
+           code =
+               RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
        } else {
-           H_UNLOCK
-           code = RXAFSCB_InitCallBackState(host->callback_rxcon);
-           H_LOCK
-       }
-       host->hostFlags |= ALTADDR; /* alternate addresses are valid */
+           H_UNLOCK;
+           code = RXAFSCB_InitCallBackState(cb_conn);
+       }
+       rx_PutConnection(cb_conn);
+       cb_conn = NULL;
+       H_LOCK;
+       host->hostFlags |= ALTADDR;     /* alternate addresses are valid */
        if (code) {
            if (ShowProblems) {
                ViceLog(0,
-          ("CB: Call back connect back failed (in break delayed) for %s:%d\n",
-                       afs_inet_ntoa_r(host->host,hoststr), ntohs(host->port)));
-             }
+                       ("CB: Call back connect back failed (in break delayed) for %s:%d\n",
+                        afs_inet_ntoa_r(host->host, hoststr),
+                        ntohs(host->port)));
+           }
            host->hostFlags |= VENUSDOWN;
-       }
-       else {
-           ViceLog(25,("InitCallBackState success on %s\n",afs_inet_ntoa_r(host->host,hoststr)));
+       } else {
+           ViceLog(25,
+                   ("InitCallBackState success on %s\n",
+                    afs_inet_ntoa_r(host->host, hoststr)));
            /* reset was done successfully */
            host->hostFlags |= RESETDONE;
            host->hostFlags &= ~VENUSDOWN;
        }
-    }
-    else while (!(host->hostFlags & HOSTDELETED)) {
-       nfids = 0;
-       host->hostFlags &= ~VENUSDOWN;  /* presume up */
-       cbi = first = host->cblist;
-       if (!cbi) 
-           break;
-       do {
-           first = host->cblist;
-           cb = itocb(cbi);
-           cbi = cb->hnext;
-           if (cb->status == CB_DELAYED) {
-               register struct FileEntry *fe = itofe(cb->fhead);
-               thead[nfids] = cb->thead;
-               fids[nfids].Volume = fe->volid;
-               fids[nfids].Vnode =  fe->vnode;
-               fids[nfids].Unique = fe->unique;
-               nfids++;
-               HDel(cb);
-               TDel(cb);
-               CDel(cb, 1);
-           }
-       } while (cbi && cbi != first && nfids < AFSCBMAX);
-
-       if (nfids==0) {
-         break;
-       }
+    } else
+       while (!(host->hostFlags & HOSTDELETED)) {
+           nfids = 0;
+           host->hostFlags &= ~VENUSDOWN;      /* presume up */
+           cbi = first = host->cblist;
+           if (!cbi)
+               break;
+           do {
+               first = host->cblist;
+               cb = itocb(cbi);
+               cbi = cb->hnext;
+               if (cb->status == CB_DELAYED) {
+                   register struct FileEntry *fe = itofe(cb->fhead);
+                   thead[nfids] = cb->thead;
+                   fids[nfids].Volume = fe->volid;
+                   fids[nfids].Vnode = fe->vnode;
+                   fids[nfids].Unique = fe->unique;
+                   nfids++;
+                   HDel(cb);
+                   TDel(cb);
+                   CDel(cb, 1);
+               }
+           } while (cbi && cbi != first && nfids < AFSCBMAX);
 
-       if (XCallBackBulk_r(host, fids, nfids)) {
-           /* Failed, again:  put them back, probably with old
-            timeout values */
-           int i;
-           if (ShowProblems) {
-               ViceLog(0,
-            ("CB: XCallBackBulk failed, host=%s:%d; callback list follows:\n",
-                   afs_inet_ntoa_r(host->host,hoststr), ntohs(host->port)));
+           if (nfids == 0) {
+               break;
            }
-           for (i = 0; i<nfids; i++) {
+
+           if (XCallBackBulk_r(host, fids, nfids)) {
+               /* Failed, again:  put them back, probably with old
+                * timeout values */
+               int i;
                if (ShowProblems) {
                    ViceLog(0,
-                   ("CB: Host %s:%d, file %u.%u.%u (part of bulk callback)\n",
-                              afs_inet_ntoa_r(host->host,hoststr), ntohs(host->port), 
-                              fids[i].Volume, fids[i].Vnode, fids[i].Unique));
+                           ("CB: XCallBackBulk failed, host=%s:%d; callback list follows:\n",
+                            afs_inet_ntoa_r(host->host, hoststr),
+                            ntohs(host->port)));
+               }
+               for (i = 0; i < nfids; i++) {
+                   if (ShowProblems) {
+                       ViceLog(0,
+                               ("CB: Host %s:%d, file %u.%u.%u (part of bulk callback)\n",
+                                afs_inet_ntoa_r(host->host, hoststr),
+                                ntohs(host->port), fids[i].Volume,
+                                fids[i].Vnode, fids[i].Unique));
+                   }
+                   /* used to do this:
+                    * AddCallBack1_r(host, &fids[i], itot(thead[i]), CB_DELAYED, 1);
+                    * * but it turns out to cause too many tricky locking problems.
+                    * * now, if break delayed fails, screw it. */
                }
-               /* used to do this:
-                  AddCallBack1_r(host, &fids[i], itot(thead[i]), CB_DELAYED, 1);
-                * but it turns out to cause too many tricky locking problems.
-                * now, if break delayed fails, screw it. */
+               host->hostFlags |= VENUSDOWN;   /* Failed */
+               ClearHostCallbacks_r(host, 1 /* locked */ );
+               nfids = 0;
+               break;
            }
-           host->hostFlags |= VENUSDOWN; /* Failed */
-           ClearHostCallbacks_r(host, 1/* locked */);
-           nfids = 0;
-           break;
+           if (nfids < AFSCBMAX)
+               break;
        }
-       if (nfids < AFSCBMAX)
-           break;
-    }
 
     cbstuff.nbreakers--;
     /* If we succeeded it's always ok to unset HFE_LATER */
@@ -1130,69 +1202,74 @@ int BreakDelayedCallBacks_r(struct host *host)
 ** 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, struct VCBParams *parms, int deletefe)
+static int
+MultiBreakVolumeCallBack_r(struct host *host, int isheld,
+                          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 */
+    if (!isheld)
+       return isheld;          /* host is held only by h_Enumerate, do nothing */
+    if (host->hostFlags & HOSTDELETED)
+       return 0;               /* host is deleted, release hold */
 
     if (host->hostFlags & VENUSDOWN) {
        h_Lock_r(host);
        if (host->hostFlags & HOSTDELETED) {
            h_Unlock_r(host);
-           return 0;      /* Release hold */
+           return 0;           /* Release hold */
        }
-       ViceLog(8,("BVCB: volume call back for host %s:%d failed\n",
-                afs_inet_ntoa_r(host->host,hoststr),ntohs(host->port)));
+       ViceLog(8,
+               ("BVCB: volume call back for host %s:%d failed\n",
+                afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
        if (ShowProblems) {
-           ViceLog(0, ("CB: volume callback for host %s:%d failed\n",
-                   afs_inet_ntoa_r(host->host,hoststr), ntohs(host->port)));
+           ViceLog(0,
+                   ("CB: volume callback for host %s:%d failed\n",
+                    afs_inet_ntoa_r(host->host, hoststr),
+                    ntohs(host->port)));
        }
-       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 */
+       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 */
        h_Unlock_r(host);
-       return 0; /* release hold */
+       return 0;               /* release hold */
     }
     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
-    */
-    if ( parms->ncbas  == MAX_CB_HOSTS ) {
-      struct AFSCBFids tf;
-
-      tf.AFSCBFids_len = 1;
-      tf.AFSCBFids_val = parms->fid;
-      
-                               /* this releases all the hosts */
-      MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf, 0 /* xhost */);
-
-      parms->ncbas = 0;
-    }  
+     ** because it would prematurely release the hold on the host
+     */
+    if (parms->ncbas == MAX_CB_HOSTS) {
+       struct AFSCBFids tf;
+
+       tf.AFSCBFids_len = 1;
+       tf.AFSCBFids_val = parms->fid;
+
+       /* this releases all the hosts */
+       MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf, 0 /* xhost */ );
+
+       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. */
+    return 1;                  /* DON'T release hold, because we still need it. */
 }
 
 /*
 ** 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)
+static int
+MultiBreakVolumeCallBack(struct host *host, int isheld,
+                        struct VCBParams *parms)
 {
     int retval;
-    H_LOCK
+    H_LOCK;
     retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 1);
-    H_UNLOCK
+    H_UNLOCK;
     return retval;
 }
 
@@ -1200,13 +1277,14 @@ static int MultiBreakVolumeCallBack(struct host *host, int isheld,
 ** 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)
+static int
+MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
+                             struct VCBParams *parms)
 {
     int retval;
-    H_LOCK
+    H_LOCK;
     retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 0);
-    H_UNLOCK
+    H_UNLOCK;
     return retval;
 }
 
@@ -1224,7 +1302,8 @@ static int MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
  * a delayed callback.  Resets will be forced if the host is
  * determined to be down before the RPC is executed.
  */
-int BreakVolumeCallBacks(afs_uint32 volume)
+int
+BreakVolumeCallBacks(afs_uint32 volume)
 {
     struct AFSFid fid;
     int hash;
@@ -1233,12 +1312,12 @@ int BreakVolumeCallBacks(afs_uint32 volume)
     struct FileEntry *fe;
     struct host *host;
     struct VCBParams henumParms;
-    afs_uint32 tthead = 0;  /* zero is illegal value */
+    afs_uint32 tthead = 0;     /* zero is illegal value */
 
-    H_LOCK
+    H_LOCK;
     fid.Volume = volume, fid.Vnode = fid.Unique = 0;
-    for (hash=0; hash<VHASH; hash++) {
-       for (feip = &HashTable[hash]; (fe = itofe(*feip)); ) {
+    for (hash = 0; hash < VHASH; hash++) {
+       for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
            if (fe->volid == volume) {
                register struct CallBack *cbnext;
                for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
@@ -1246,7 +1325,7 @@ int BreakVolumeCallBacks(afs_uint32 volume)
                    h_Hold_r(host);
                    cbnext = itocb(cb->cnext);
                    if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
-                     tthead = cb->thead;
+                       tthead = cb->thead;
                    }
                    TDel(cb);
                    HDel(cb);
@@ -1262,28 +1341,26 @@ int BreakVolumeCallBacks(afs_uint32 volume)
     }
 
     if (!tthead) {
-      /* didn't find any callbacks, so return right away. */
-      H_UNLOCK
-      return 0;
+       /* 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
+    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;
 }
 
@@ -1293,7 +1370,8 @@ extern pthread_cond_t fsync_cond;
 extern char fsync_wait[];
 #endif
 
-int BreakVolumeCallBacksLater(afs_uint32 volume)
+int
+BreakVolumeCallBacksLater(afs_uint32 volume)
 {
     int hash;
     afs_int32 *feip;
@@ -1303,27 +1381,26 @@ int BreakVolumeCallBacksLater(afs_uint32 volume)
     int found = 0;
 
     ViceLog(25, ("Setting later on volume %u\n", volume));
-    H_LOCK
-    for (hash=0; hash<VHASH; hash++) {
-       for (feip = &HashTable[hash]; fe = itofe(*feip); ) {
+    H_LOCK;
+    for (hash = 0; hash < VHASH; hash++) {
+       for (feip = &HashTable[hash]; fe = itofe(*feip);) {
            if (fe->volid == volume) {
                register struct CallBack *cbnext;
-                for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
+               for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
                    host = h_itoh(cb->hhead);
                    host->hostFlags |= HFE_LATER;
                    cb->status = CB_DELAYED;
-                    cbnext = itocb(cb->cnext);
-                }
-               FSYNC_LOCK
+                   cbnext = itocb(cb->cnext);
+               }
+               FSYNC_LOCK;
                fe->status |= FE_LATER;
-               FSYNC_UNLOCK
+               FSYNC_UNLOCK;
                found++;
            }
            feip = &fe->fnext;
        }
     }
-    H_UNLOCK
-
+    H_UNLOCK;
     if (!found) {
        /* didn't find any callbacks, so return right away. */
        return 0;
@@ -1331,14 +1408,17 @@ int BreakVolumeCallBacksLater(afs_uint32 volume)
 
     ViceLog(25, ("Fsync thread wakeup\n"));
 #ifdef AFS_PTHREAD_ENV
+    FSYNC_LOCK;
     assert(pthread_cond_broadcast(&fsync_cond) == 0);
+    FSYNC_UNLOCK;
 #else
-    LWP_NoYieldSignal(&fsync_wait);
+    LWP_NoYieldSignal(fsync_wait);
 #endif
     return 0;
 }
 
-int BreakLaterCallBacks(void)
+int
+BreakLaterCallBacks(void)
 {
     struct AFSFid fid;
     int hash;
@@ -1348,52 +1428,64 @@ int BreakLaterCallBacks(void)
     struct FileEntry *myfe = NULL;
     struct host *host;
     struct VCBParams henumParms;
-    unsigned short tthead = 0;  /* zero is illegal value */
-    
+    unsigned short tthead = 0; /* zero is illegal value */
+    char hoststr[16];
+
     /* Unchain first */
     ViceLog(25, ("Looking for FileEntries to unchain\n"));
-    H_LOCK
-
+    H_LOCK;
+    FSYNC_LOCK;
     /* Pick the first volume we see to clean up */
     fid.Volume = fid.Vnode = fid.Unique = 0;
 
-    for (hash=0; hash<VHASH; hash++) {
-       for (feip = &HashTable[hash]; fe = itofe(*feip); ) {
-           if (fe && (fe->status & FE_LATER) && 
-               (fid.Volume == 0 || fid.Volume == fe->volid)) {
-               ViceLog(125, ("Unchaining for %u:%u:%u\n", fe->vnode, 
-                             fe->unique, fe->volid));
+    for (hash = 0; hash < VHASH; hash++) {
+       for (feip = &HashTable[hash]; fe = itofe(*feip);) {
+           if (fe && (fe->status & FE_LATER)
+               && (fid.Volume == 0 || fid.Volume == fe->volid)) {
+               /* 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));
                fid.Volume = fe->volid;
                *feip = fe->fnext;
                /* Works since volid is deeper than the largest pointer */
-               ((struct object *)fe)->next = (struct object *)myfe;
+               tmpfe = (struct object *)fe;
+               tmpfe->next = (struct object *)myfe;
                myfe = fe;
-           } else 
+           } else
                feip = &fe->fnext;
        }
     }
-    
+    FSYNC_UNLOCK;
+
     if (!myfe) {
-       H_UNLOCK
+       H_UNLOCK;
        return 0;
     }
 
     /* loop over FEs from myfe and free/break */
-    FSYNC_UNLOCK
     tthead = 0;
-    for (fe = myfe; fe; ) {
+    for (fe = myfe; fe;) {
        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;
+           host = h_itoh(cb->hhead);
+           if (cb->status == CB_DELAYED) {
+               h_Hold_r(host);
+               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 */
+           } else {
+               ViceLog(125,
+                       ("Found host %s:%d non-DELAYED cb for %u:%u:%u\n", 
+                        afs_inet_ntoa_r(host->host, hoststr),
+                        ntohs(host->port), fe->vnode, fe->unique, fe->volid));
            }
-           TDel(cb);
-           HDel(cb);
-           CDel(cb, 0); /* Don't let CDel clean up the fe */
-           /* leave hold for MultiBreakVolumeCallBack to clear */
        }
        myfe = fe;
        fe = (struct FileEntry *)((struct object *)fe)->next;
@@ -1405,21 +1497,19 @@ int BreakLaterCallBacks(void)
        henumParms.ncbas = 0;
        henumParms.fid = &fid;
        henumParms.thead = tthead;
-       H_UNLOCK
-       h_Enumerate(MultiBreakVolumeLaterCallBack, (char *) &henumParms);
-       H_LOCK
-           
-       if (henumParms.ncbas) {    /* do left-overs */
+       H_UNLOCK;
+       h_Enumerate(MultiBreakVolumeLaterCallBack, (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 );
+
+           MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf, 0);
            henumParms.ncbas = 0;
        }
-    }  
-    FSYNC_LOCK
-    H_UNLOCK
+    }
+    H_UNLOCK;
 
     /* Arrange to be called again */
     return 1;
@@ -1429,14 +1519,17 @@ int BreakLaterCallBacks(void)
  * Delete all timed-out call back entries (to be called periodically by file
  * server)
  */
-int CleanupTimedOutCallBacks(void)
+int
+CleanupTimedOutCallBacks(void)
 {
-    H_LOCK
+    H_LOCK;
     CleanupTimedOutCallBacks_r();
-    H_UNLOCK
+    H_UNLOCK;
+    return 0;
 }
 
-int CleanupTimedOutCallBacks_r(void)
+int
+CleanupTimedOutCallBacks_r(void)
 {
     afs_uint32 now = CBtime(FT_ApproxTime());
     register afs_uint32 *thead;
@@ -1451,17 +1544,18 @@ int CleanupTimedOutCallBacks_r(void)
            do {
                cb = itocb(cbi);
                cbi = cb->tnext;
-               ViceLog(8,("CCB: deleting timed out call back %s:%d, (%u,%u,%u)\n",
-                       afs_inet_ntoa_r(h_itoh(cb->hhead)->host, hoststr), h_itoh(cb->hhead)->port, 
-                       itofe(cb->fhead)->volid, itofe(cb->fhead)->vnode,
-                       itofe(cb->fhead)->unique));
+               ViceLog(8,
+                       ("CCB: deleting timed out call back %s:%d, (%u,%u,%u)\n",
+                        afs_inet_ntoa_r(h_itoh(cb->hhead)->host, hoststr),
+                        h_itoh(cb->hhead)->port, 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();
-                 ShutDownAndCore(PANIC);
+                   ViceLog(0, ("CCB: Internal Error -- shutting down...\n"));
+                   DumpCallBackState();
+                   ShutDownAndCore(PANIC);
                }
            } while (cbi != *thead);
            *thead = 0;
@@ -1469,19 +1563,26 @@ int CleanupTimedOutCallBacks_r(void)
        tfirst++;
     }
     cbstuff.CBsTimedOut += ntimedout;
-    ViceLog(7,("CCB: deleted %d timed out callbacks\n", ntimedout));
+    ViceLog(7, ("CCB: deleted %d timed out callbacks\n", ntimedout));
     return (ntimedout > 0);
 }
 
 static struct host *lih_host;
+static int lih_host_held;
 
-static int lih_r(register struct host *host, register int held, 
-       register struct host *hostp)
+static int
+lih_r(register struct host *host, register int held,
+      register struct host *hostp)
 {
     if (host->cblist
-          && ((hostp && host != hostp) || (!held && !h_OtherHolds_r(host)))
-           && (!lih_host || host->ActiveCall < lih_host->ActiveCall) ) {
+       && ((hostp && host != hostp) || (!held && !h_OtherHolds_r(host)))
+       && (!lih_host || host->ActiveCall < lih_host->ActiveCall)) {
+       if (lih_host != NULL && lih_host_held) {
+           h_Release_r(lih_host);
+       }
        lih_host = host;
+       lih_host_held = !held;
+       held = 1;
     }
     return held;
 }
@@ -1491,13 +1592,15 @@ static int lih_r(register struct host *host, register int held,
 /* second pass: find the oldest host who isn't "me" */
 /* always called with hostp unlocked */
 extern struct host *hostList;
-static int GetSomeSpace_r(struct host *hostp, int locked)
+static int
+GetSomeSpace_r(struct host *hostp, int locked)
 {
     register struct host *hp, *hp1 = (struct host *)0, *hp2 = hostList;
-    int i=0;
+    int i = 0;
 
     cbstuff.GotSomeSpaces++;
-    ViceLog(5,("GSS: First looking for timed out call backs via CleanupCallBacks\n"));
+    ViceLog(5,
+           ("GSS: First looking for timed out call backs via CleanupCallBacks\n"));
     if (CleanupTimedOutCallBacks_r()) {
        cbstuff.GSS3++;
        return 0;
@@ -1507,15 +1610,24 @@ static int GetSomeSpace_r(struct host *hostp, int locked)
        h_Enumerate_r(lih_r, hp2, (char *)hp1);
        hp = lih_host;
        if (hp) {
+           /* set in lih_r! private copy before giving up H_LOCK */
+           int lih_host_held2=lih_host_held;   
            cbstuff.GSS4++;
-           if ( ! ClearHostCallbacks_r(hp, 0 /* not locked or held */) )
+           if (!ClearHostCallbacks_r(hp, 0 /* not locked or held */ )) {
+               if (lih_host_held2)
+                   h_Release_r(hp);
                return 0;
+           }
+           if (lih_host_held2)
+               h_Release_r(hp);
            hp2 = hp->next;
        } else {
            hp2 = hostList;
            hp1 = hostp;
            cbstuff.GSS1++;
-           ViceLog(5,("GSS: Try harder for longest inactive host cnt= %d\n", i));
+           ViceLog(5,
+                   ("GSS: Try harder for longest inactive host cnt= %d\n",
+                    i));
            /*
             * Next time try getting callbacks from any host even if
             * it's deleted (that's actually great since we can freely
@@ -1533,47 +1645,50 @@ static int GetSomeSpace_r(struct host *hostp, int locked)
      * previously passed over. */
     cbstuff.GSS2++;
     if (!locked) {
-      h_Lock_r(hostp);
+       h_Lock_r(hostp);
     }
-    ClearHostCallbacks_r(hostp, 1/*already locked*/);
+    ClearHostCallbacks_r(hostp, 1 /*already locked */ );
     if (!locked) {
-      h_Unlock_r(hostp);
+       h_Unlock_r(hostp);
     }
     return 0;
 }
 
 /* locked - set if caller has already locked the host */
-static int ClearHostCallbacks_r(struct host *hp, int locked)
+static int
+ClearHostCallbacks_r(struct host *hp, int locked)
 {
-    struct interfaceAddr interf;
     int code;
     int held = 0;
     char hoststr[16];
+    struct rx_connection *cb_conn = NULL;
 
-    ViceLog(5,("GSS: Delete longest inactive host %s\n", afs_inet_ntoa_r(hp->host,hoststr)));
-    if ( !(held = h_Held_r(hp)) )
+    ViceLog(5,
+           ("GSS: Delete longest inactive host %s\n",
+            afs_inet_ntoa_r(hp->host, hoststr)));
+    if (!(held = h_Held_r(hp)))
        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)) {
+           if (!held)
+               h_Release_r(hp);
+           return 1;
+       }
     }
     if (hp->Console & 2) {
        /*
         * If the special console field is set it means that a thread
-         * is waiting in AddCallBack1 after it set pointers to the
-         * file entry and/or callback entry. Because of the bogus
-         * usage of h_hold it won't prevent from another thread, this
-         * one, to remove all the callbacks so just to be safe we keep
-         * a reference. NOTE, on the last phase we'll free the calling
-         * host's callbacks but that's ok...
-         */
+        * is waiting in AddCallBack1 after it set pointers to the
+        * file entry and/or callback entry. Because of the bogus
+        * usage of h_hold it won't prevent from another thread, this
+        * one, to remove all the callbacks so just to be safe we keep
+        * a reference. NOTE, on the last phase we'll free the calling
+        * host's callbacks but that's ok...
+        */
        cbstuff.GSS5++;
     }
     DeleteAllCallBacks_r(hp, 1);
@@ -1581,19 +1696,22 @@ static int ClearHostCallbacks_r(struct host *hp, int locked)
        hp->hostFlags &= ~RESETDONE;    /* remember that we must do a reset */
     } else {
        /* host is up, try a call */
-       hp->hostFlags &= ~ALTADDR; /* alternate addresses are invalid */
+       hp->hostFlags &= ~ALTADDR;      /* alternate addresses are invalid */
+       cb_conn = hp->callback_rxcon;
+       rx_GetConnection(hp->callback_rxcon);
        if (hp->interface) {
-           H_UNLOCK
-           code = RXAFSCB_InitCallBackState3(hp->callback_rxcon, &FS_HostUUID);
-           H_LOCK
+           H_UNLOCK;
+           code =
+               RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
        } else {
-           H_UNLOCK
-           code = RXAFSCB_InitCallBackState(hp->callback_rxcon);
-           H_LOCK
+           H_UNLOCK;
+           code = RXAFSCB_InitCallBackState(cb_conn);
        }
-       hp->hostFlags |= ALTADDR; /* alternate addresses are valid */
-       if (code)
-        {
+       rx_PutConnection(cb_conn);
+       cb_conn = NULL;
+       H_LOCK;
+       hp->hostFlags |= ALTADDR;       /* alternate addresses are valid */
+       if (code) {
            /* failed, mark host down and need reset */
            hp->hostFlags |= VENUSDOWN;
            hp->hostFlags &= ~RESETDONE;
@@ -1603,9 +1721,9 @@ static int ClearHostCallbacks_r(struct host *hp, int locked)
        }
     }
     if (!locked) {
-       h_Unlock_r(hp);
+       h_Unlock_r(hp);
     }
-    if ( !held )
+    if (!held)
        h_Release_r(hp);
 
     return 0;
@@ -1613,45 +1731,52 @@ static int ClearHostCallbacks_r(struct host *hp, int locked)
 #endif /* INTERPRET_DUMP */
 
 
-int PrintCallBackStats(void)
+int
+PrintCallBackStats(void)
 {
-    fprintf(stderr, "%d add CB, %d break CB, %d del CB, %d del FE, %d CB's timed out, %d space reclaim, %d del host\n",
-           cbstuff.AddCallBacks, cbstuff.BreakCallBacks, cbstuff.DeleteCallBacks,
-           cbstuff.DeleteFiles, cbstuff.CBsTimedOut, cbstuff.GotSomeSpaces,
-           cbstuff.DeleteAllCallBacks);
+    fprintf(stderr,
+           "%d add CB, %d break CB, %d del CB, %d del FE, %d CB's timed out, %d space reclaim, %d del host\n",
+           cbstuff.AddCallBacks, cbstuff.BreakCallBacks,
+           cbstuff.DeleteCallBacks, cbstuff.DeleteFiles, cbstuff.CBsTimedOut,
+           cbstuff.GotSomeSpaces, cbstuff.DeleteAllCallBacks);
     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);
+           cbstuff.nCBs, cbstuff.nFEs, cbstuff.nCBs + cbstuff.nFEs,
+           cbstuff.nblks);
 
     return 0;
 }
 
-#define MAGIC 0x12345678    /* To check byte ordering of dump when it is read in */
+#define MAGIC 0x12345678       /* To check byte ordering of dump when it is read in */
 
 #ifndef INTERPRET_DUMP
 
-int DumpCallBackState(void)
+int
+DumpCallBackState(void)
 {
     int fd;
     afs_uint32 magic = MAGIC, now = FT_ApproxTime(), freelisthead;
 
-    fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, O_WRONLY|O_CREAT|O_TRUNC, 0666);
+    fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, O_WRONLY | O_CREAT | O_TRUNC,
+             0666);
     if (fd < 0) {
-       ViceLog(0, ("Couldn't create callback dump file %s\n", AFSDIR_SERVER_CBKDUMP_FILEPATH));
+       ViceLog(0,
+               ("Couldn't create callback dump file %s\n",
+                AFSDIR_SERVER_CBKDUMP_FILEPATH));
        return 0;
     }
-    (void) write(fd, &magic, sizeof(magic));
-    (void) write(fd, &now, sizeof(now));
-    (void) write(fd, &cbstuff, sizeof(cbstuff));
-    (void) write(fd, TimeOuts, sizeof(TimeOuts));
-    (void) write(fd, timeout, sizeof(timeout));
-    (void) write(fd, &tfirst, sizeof(tfirst));
-    freelisthead = cbtoi((struct CallBack *) CBfree);
-    (void) write(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
-    freelisthead = fetoi((struct FileEntry *) FEfree);
-    (void) write(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
-    (void) write(fd, HashTable, sizeof(HashTable));
-    (void) write(fd, &CB[1], sizeof(CB[1])*cbstuff.nblks); /* CB stuff */
-    (void) write(fd, &FE[1], sizeof(FE[1])*cbstuff.nblks); /* FE stuff */
+    (void)write(fd, &magic, sizeof(magic));
+    (void)write(fd, &now, sizeof(now));
+    (void)write(fd, &cbstuff, sizeof(cbstuff));
+    (void)write(fd, TimeOuts, sizeof(TimeOuts));
+    (void)write(fd, timeout, sizeof(timeout));
+    (void)write(fd, &tfirst, sizeof(tfirst));
+    freelisthead = cbtoi((struct CallBack *)CBfree);
+    (void)write(fd, &freelisthead, sizeof(freelisthead));      /* This is a pointer */
+    freelisthead = fetoi((struct FileEntry *)FEfree);
+    (void)write(fd, &freelisthead, sizeof(freelisthead));      /* This is a pointer */
+    (void)write(fd, HashTable, sizeof(HashTable));
+    (void)write(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks);    /* CB stuff */
+    (void)write(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks);    /* FE stuff */
     close(fd);
 
     return 0;
@@ -1663,7 +1788,8 @@ int DumpCallBackState(void)
 
 /* This is only compiled in for the callback analyzer program */
 /* Returns the time of the dump */
-time_t ReadDump(char *file)
+time_t
+ReadDump(char *file)
 {
     int fd;
     afs_uint32 magic, freelisthead;
@@ -1676,7 +1802,8 @@ time_t ReadDump(char *file)
     }
     read(fd, &magic, sizeof(magic));
     if (magic != MAGIC) {
-       fprintf(stderr, "Magic number of %s is invalid.  You might be trying to\n",
+       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");
@@ -1688,14 +1815,16 @@ time_t ReadDump(char *file)
     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;
-    FE = ((struct FileEntry *)(calloc(cbstuff.nblks,sizeof(struct FileEntry))))-1;
-    CBfree = (struct CallBack *) itocb(freelisthead);
+    CB = ((struct CallBack
+          *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
+    FE = ((struct FileEntry
+          *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
+    CBfree = (struct CallBack *)itocb(freelisthead);
     read(fd, &freelisthead, sizeof(freelisthead));
-    FEfree = (struct FileEntry *) itofe(freelisthead);
+    FEfree = (struct FileEntry *)itofe(freelisthead);
     read(fd, HashTable, sizeof(HashTable));
-    read(fd, &CB[1], sizeof(CB[1])*cbstuff.nblks); /* CB stuff */
-    read(fd, &FE[1], sizeof(FE[1])*cbstuff.nblks); /* FE stuff */
+    read(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks);   /* CB stuff */
+    read(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks);   /* FE stuff */
     if (close(fd)) {
        perror("Error reading dumpfile");
        exit(1);
@@ -1705,7 +1834,8 @@ time_t ReadDump(char *file)
 
 #include "AFS_component_version_number.c"
 
-int main(int argc, char **argv)
+int
+main(int argc, char **argv)
 {
     int err = 0, cbi = 0, stats = 0, noptions = 0, all = 0, vol = 0, raw = 0;
     static AFSFid fid;
@@ -1714,19 +1844,19 @@ int main(int argc, char **argv)
     time_t now;
 
     memset(&fid, 0, sizeof(fid));
-    argc--; argv++;
+    argc--;
+    argv++;
     while (argc && **argv == '-') {
        noptions++;
-        argc--;
-        if (!strcmp(*argv, "-host")) {
+       argc--;
+       if (!strcmp(*argv, "-host")) {
            if (argc < 1) {
                err++;
                break;
            }
            argc--;
            cbi = atoi(*++argv);
-        }
-       else if (!strcmp(*argv, "-fid")) {
+       } else if (!strcmp(*argv, "-fid")) {
            if (argc < 2) {
                err++;
                break;
@@ -1735,43 +1865,40 @@ int main(int argc, char **argv)
            fid.Volume = atoi(*++argv);
            fid.Vnode = atoi(*++argv);
            fid.Unique = atoi(*++argv);
-       }
-       else if (!strcmp(*argv, "-time")) {
+       } else if (!strcmp(*argv, "-time")) {
            fprintf(stderr, "-time not supported\n");
            exit(1);
-       }
-       else if (!strcmp(*argv, "-stats")) {
+       } else if (!strcmp(*argv, "-stats")) {
            stats = 1;
-       }
-       else if (!strcmp(*argv, "-all")) {
+       } else if (!strcmp(*argv, "-all")) {
            all = 1;
-       }
-       else if (!strcmp(*argv, "-raw")) {
+       } else if (!strcmp(*argv, "-raw")) {
            raw = 1;
-       }
-        else if (!strcmp(*argv, "-volume")) {
+       } else if (!strcmp(*argv, "-volume")) {
            if (argc < 1) {
                err++;
                break;
            }
            argc--;
            vol = atoi(*++argv);
-        }
-       else err++;
-        argv++;
+       } else
+           err++;
+       argv++;
     }
     if (err || argc != 1) {
        fprintf(stderr,
                "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] callbackdumpfile\n");
-       fprintf(stderr, "[cbid is shown for each host in the hosts.dump file]\n");
+       fprintf(stderr,
+               "[cbid is shown for each host in the hosts.dump file]\n");
        exit(1);
     }
     now = ReadDump(*argv);
     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, ctime(&uxtfirst));
-        PrintCallBackStats();
+       printf("The last time cleanup ran was %u %s", uxtfirst,
+              ctime(&uxtfirst));
+       PrintCallBackStats();
     }
     if (all || vol) {
        register hash;
@@ -1779,12 +1906,12 @@ int main(int argc, char **argv)
        register struct CallBack *cb;
        register struct FileEntry *fe;
 
-       for (hash=0; hash<VHASH; hash++) {
-           for (feip = &HashTable[hash]; fe = itofe(*feip); ) {
+       for (hash = 0; hash < VHASH; hash++) {
+           for (feip = &HashTable[hash]; fe = itofe(*feip);) {
                if (!vol || (fe->volid == vol)) {
                    register struct CallBack *cbnext;
                    for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
-                       PrintCB(cb,now);
+                       PrintCB(cb, now);
                        cbnext = itocb(cb->cnext);
                    }
                    *feip = fe->fnext;
@@ -1798,7 +1925,7 @@ int main(int argc, char **argv)
        afs_uint32 cfirst = cbi;
        do {
            cb = itocb(cbi);
-           PrintCB(cb,now);
+           PrintCB(cb, now);
            cbi = cb->hnext;
        } while (cbi != cfirst);
     }
@@ -1817,21 +1944,25 @@ int main(int argc, char **argv)
     if (raw) {
        struct FileEntry *fe;
        afs_int32 *p, i;
-       for (i=1; i < cbstuff.nblks; i++) {
-           p = (afs_int32 *) &FE[i];
+       for (i = 1; i < cbstuff.nblks; i++) {
+           p = (afs_int32 *) & FE[i];
            printf("%d:%12x%12x%12x%12x\n", i, p[0], p[1], p[2], p[3]);
        }
     }
 }
 
-int PrintCB(register struct CallBack *cb, afs_uint32 now)
+int
+PrintCB(register struct CallBack *cb, afs_uint32 now)
 {
     struct FileEntry *fe = itofe(cb->fhead);
     time_t expires = TIndexToTime(cb->thead);
 
-    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));
+    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));
 }
 
 #endif
@@ -1841,185 +1972,227 @@ int PrintCB(register struct CallBack *cb, afs_uint32 now)
 ** try breaking calbacks on afidp from host. Use multi_rx.
 ** return 0 on success, non-zero on failure
 */
-int MultiBreakCallBackAlternateAddress(struct host *host, struct AFSCBFids *afidp)
+int
+MultiBreakCallBackAlternateAddress(struct host *host, struct AFSCBFids *afidp)
 {
     int retVal;
-    H_LOCK
+    H_LOCK;
     retVal = MultiBreakCallBackAlternateAddress_r(host, afidp);
-    H_UNLOCK
+    H_UNLOCK;
     return retVal;
 }
 
-int MultiBreakCallBackAlternateAddress_r(struct host *host, struct AFSCBFids *afidp)
+int
+MultiBreakCallBackAlternateAddress_r(struct host *host,
+                                    struct AFSCBFids *afidp)
 {
-       int i,j;
-       struct rx_connection**  conns;
-       struct rx_connection*   connSuccess = 0;
-       afs_int32               *addr;
-       static struct rx_securityClass *sc = 0;
-       static struct AFSCBs tc = {0,0};
-       char hoststr[16];
-
-       /* nothing more can be done */
-       if ( !host->interface ) return 1;       /* failure */
-
-       assert(host->interface->numberOfInterfaces > 0 );
-
-       /* the only address is the primary interface */
-       if ( host->interface->numberOfInterfaces == 1 )
-               return 1;                       /* failure */
-
-       /* initialise a security object only once */
-       if ( !sc )
-           sc = rxnull_NewClientSecurityObject();
-
-       i = host->interface->numberOfInterfaces;
-       addr = calloc(i, sizeof(afs_int32));
-       conns = calloc(i, sizeof(struct rx_connection *));
-       if (!addr || !conns) {
-           ViceLog(0, ("Failed malloc in MultiBreakCallBackAlternateAddress_r\n"));
-           assert(0);
-       }
+    int i, j;
+    struct rx_connection **conns;
+    struct rx_connection *connSuccess = 0;
+    struct AddrPort *interfaces;
+    static struct rx_securityClass *sc = 0;
+    static struct AFSCBs tc = { 0, 0 };
+    char hoststr[16];
 
-       /* initialize alternate rx connections */
-       for ( i=0,j=0; i < host->interface->numberOfInterfaces; i++)
-       {
-               /* this is the current primary address */
-               if ( host->host == host->interface->addr[i] )
-                       continue;       
-
-               addr[j]    = host->interface->addr[i];
-               conns[j] =  rx_NewConnection (host->interface->addr[i],
-                               host->port, 1, sc, 0);
-               rx_SetConnDeadTime(conns[j], 2); 
-               rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME); 
-               j++;
-       }
+    /* nothing more can be done */
+    if (!host->interface)
+       return 1;               /* failure */
 
-       assert(j);  /* at least one alternate address */
-       ViceLog(125,("Starting multibreakcall back on all addr for host %s\n",
-                       afs_inet_ntoa_r(host->host,hoststr)));
-       H_UNLOCK
-       multi_Rx(conns, j)
-       {
-               multi_RXAFSCB_CallBack(afidp, &tc);     
-               if ( !multi_error )
-               {
-                       /* first success */
-                       H_LOCK
-                       if ( host->callback_rxcon )
-                               rx_DestroyConnection(host->callback_rxcon);
-                       host->callback_rxcon = conns[multi_i];
-                       host->host           = addr[multi_i];
-                       connSuccess          = conns[multi_i];
-                       rx_SetConnDeadTime(host->callback_rxcon, 50);
-                       rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
-                       ViceLog(125,("multibreakcall success with addr %s\n",
-                                       afs_inet_ntoa_r(addr[multi_i],hoststr)));
-                       H_UNLOCK
-                       multi_Abort; 
-               }
-       } multi_End_Ignore;
-       H_LOCK
+    assert(host->interface->numberOfInterfaces > 0);
+
+    /* the only address is the primary interface */
+    if (host->interface->numberOfInterfaces == 1)
+       return 1;               /* failure */
+
+    /* initialise a security object only once */
+    if (!sc)
+       sc = rxnull_NewClientSecurityObject();
 
-       /* Destroy all connections except the one on which we succeeded */
-       for ( i=0; i < j; i++)
-               if ( conns[i] != connSuccess )
-                       rx_DestroyConnection(conns[i] );
+    i = host->interface->numberOfInterfaces;
+    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);
+    }
 
-       free(addr);
-       free(conns);
+    /* initialize alternate rx connections */
+    for (i = 0, j = 0; i < host->interface->numberOfInterfaces; i++) {
+       /* this is the current primary address */
+       if (host->host == host->interface->interface[i].addr &&
+           host->port == host->interface->interface[i].port)
+           continue;
+
+       interfaces[j] = host->interface->interface[i];
+       conns[j] =
+           rx_NewConnection(interfaces[j].addr, 
+                            interfaces[j].port, 1, sc, 0);
+       rx_SetConnDeadTime(conns[j], 2);
+       rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
+       j++;
+    }
 
-       if ( connSuccess ) return 0;    /* success */
-               else return 1;          /* failure */
+    assert(j);                 /* at least one alternate address */
+    ViceLog(125,
+           ("Starting multibreakcall back on all addr for host %s\n",
+            afs_inet_ntoa_r(host->host, hoststr)));
+    H_UNLOCK;
+    multi_Rx(conns, j) {
+       multi_RXAFSCB_CallBack(afidp, &tc);
+       if (!multi_error) {
+           /* first success */
+           H_LOCK;
+           if (host->callback_rxcon)
+               rx_DestroyConnection(host->callback_rxcon);
+           host->callback_rxcon = conns[multi_i];
+           host->host = interfaces[multi_i].addr;
+           host->port = interfaces[multi_i].port;
+           connSuccess = conns[multi_i];
+           rx_SetConnDeadTime(host->callback_rxcon, 50);
+           rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
+           ViceLog(125,
+                   ("multibreakcall success with addr %s\n",
+                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr)));
+           H_UNLOCK;
+           multi_Abort;
+       }
+    }
+    multi_End_Ignore;
+    H_LOCK;
+    /* Destroy all connections except the one on which we succeeded */
+    for (i = 0; i < j; i++)
+       if (conns[i] != connSuccess)
+           rx_DestroyConnection(conns[i]);
+
+    free(interfaces);
+    free(conns);
+
+    if (connSuccess)
+       return 0;               /* success */
+    else
+       return 1;               /* failure */
 }
 
 
 /*
-** try multiRX probes to host. 
-** return 0 on success, non-zero on failure
+** try multi_RX probes to host. 
+** return 0 on success, non-0 on failure
 */
-int MultiProbeAlternateAddress_r(struct host *host)
+int
+MultiProbeAlternateAddress_r(struct host *host)
 {
-       int i,j;
-       struct rx_connection**  conns;
-       struct rx_connection*   connSuccess = 0;
-       afs_int32               *addr;
-       static struct rx_securityClass *sc = 0;
-       char hoststr[16];
-
-       /* nothing more can be done */
-       if ( !host->interface ) return 1;       /* failure */
-
-       assert(host->interface->numberOfInterfaces > 0 );
-
-       /* the only address is the primary interface */
-       if ( host->interface->numberOfInterfaces == 1 )
-               return 1;                       /* failure */
-
-       /* initialise a security object only once */
-       if ( !sc )
-           sc = rxnull_NewClientSecurityObject();
-
-       i = host->interface->numberOfInterfaces;
-       addr = calloc(i, sizeof(afs_int32));    
-       conns = calloc(i, sizeof(struct rx_connection *));
-       if (!addr || !conns) {
-           ViceLog(0, ("Failed malloc in MultiProbeAlternateAddress_r\n"));
-           assert(0);
-       }
+    int i, j;
+    struct rx_connection **conns;
+    struct rx_connection *connSuccess = 0;
+    struct AddrPort *interfaces;
+    static struct rx_securityClass *sc = 0;
+    char hoststr[16];
 
-       /* initialize alternate rx connections */
-       for ( i=0,j=0; i < host->interface->numberOfInterfaces; i++)
-       {
-               /* this is the current primary address */
-               if ( host->host == host->interface->addr[i] )
-                       continue;       
-
-               addr[j]    = host->interface->addr[i];
-               conns[j] =  rx_NewConnection (host->interface->addr[i],
-                               host->port, 1, sc, 0);
-               rx_SetConnDeadTime(conns[j], 2); 
-               rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME); 
-               j++;
-       }
+    /* nothing more can be done */
+    if (!host->interface)
+       return 1;               /* failure */
 
-       assert(j);  /* at least one alternate address */
-       ViceLog(125,("Starting multiprobe on all addr for host %s\n",
-                       afs_inet_ntoa_r(host->host,hoststr)));
-       H_UNLOCK
-       multi_Rx(conns, j)
-       {
-               multi_RXAFSCB_ProbeUuid(&host->interface->uuid);        
-               if ( !multi_error )
-               {
-                       /* first success */
-                       H_LOCK
-                       if ( host->callback_rxcon )
-                               rx_DestroyConnection(host->callback_rxcon);
-                       host->callback_rxcon = conns[multi_i];
-                       host->host           = addr[multi_i];
-                       connSuccess          = conns[multi_i];
-                       rx_SetConnDeadTime(host->callback_rxcon, 50);
-                       rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
-                       ViceLog(125,("multiprobe success with addr %s\n",
-                                       afs_inet_ntoa_r(addr[multi_i],hoststr)));
-                       H_UNLOCK
-                       multi_Abort; 
-               }
-       } multi_End_Ignore;
-       H_LOCK
+    assert(host->interface->numberOfInterfaces > 0);
+
+    /* the only address is the primary interface */
+    if (host->interface->numberOfInterfaces == 1)
+       return 1;               /* failure */
+
+    /* initialise a security object only once */
+    if (!sc)
+       sc = rxnull_NewClientSecurityObject();
 
-       /* Destroy all connections except the one on which we succeeded */
-       for ( i=0; i < j; i++)
-               if ( conns[i] != connSuccess )
-                       rx_DestroyConnection(conns[i] );
+    i = host->interface->numberOfInterfaces;
+    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);
+    }
 
-       free(addr);
-       free(conns);
+    /* initialize alternate rx connections */
+    for (i = 0, j = 0; i < host->interface->numberOfInterfaces; i++) {
+       /* this is the current primary address */
+       if (host->host == host->interface->interface[i].addr &&
+           host->port == host->interface->interface[i].port)
+           continue;
+
+       interfaces[j] = host->interface->interface[i];
+       conns[j] =
+           rx_NewConnection(interfaces[i].addr, 
+                            interfaces[i].port, 1, sc, 0);
+       rx_SetConnDeadTime(conns[j], 2);
+       rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
+       j++;
+    }
 
-       if ( connSuccess ) return 0;    /* success */
-               else return 1;          /* failure */
+    assert(j);                 /* at least one alternate address */
+    ViceLog(125,
+           ("Starting multiprobe on all addr for host %s\n",
+            afs_inet_ntoa_r(host->host, hoststr)));
+    H_UNLOCK;
+    multi_Rx(conns, j) {
+       multi_RXAFSCB_ProbeUuid(&host->interface->uuid);
+       if (!multi_error) {
+           /* first success */
+           H_LOCK;
+           if (host->callback_rxcon)
+               rx_DestroyConnection(host->callback_rxcon);
+           host->callback_rxcon = conns[multi_i];
+           host->host = interfaces[multi_i].addr;
+           host->port = interfaces[multi_i].port;
+           connSuccess = conns[multi_i];
+           rx_SetConnDeadTime(host->callback_rxcon, 50);
+           rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
+           ViceLog(125,
+                   ("multiprobe success with addr %s\n",
+                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr)));
+           H_UNLOCK;
+           multi_Abort;
+       } else {
+           ViceLog(125,
+                   ("multiprobe failure with addr %s\n",
+                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr)));
+            
+            /* 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 
+             * is wrong.   We should attempt to find the correct
+             * Uuid and fix the host tables.
+             */
+            if (multi_error == 1) {
+                struct host * newhost;
+
+                /* remove the current alternate address from this host */
+                H_LOCK;
+                for (i = 0, j = 0; i < host->interface->numberOfInterfaces; i++) {
+                    if (interfaces[multi_i].addr != host->interface->interface[i].addr &&
+                       interfaces[multi_i].port != host->interface->interface[i].port) {
+                        host->interface->interface[j] = host->interface->interface[i];
+                        j++;
+                    }
+                }
+                host->interface->numberOfInterfaces--;
+                H_UNLOCK;
+            }
+        }
+    }
+    multi_End_Ignore;
+    H_LOCK;
+    /* Destroy all connections except the one on which we succeeded */
+    for (i = 0; i < j; i++)
+       if (conns[i] != connSuccess)
+           rx_DestroyConnection(conns[i]);
+
+    free(interfaces);
+    free(conns);
+
+    if (connSuccess)
+       return 0;               /* success */
+    else
+       return 1;               /* failure */
 }
 
 #endif /* !defined(INTERPRET_DUMP) */