Allow GetSomeSpace_r to select an optimal host
[openafs.git] / src / viced / callback.c
index 2c5716e..b5006a1 100644 (file)
@@ -5,6 +5,8 @@
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
+ *
+ * Portions Copyright (c) 2006 Sine Nomine Associates
  */
 
 /*
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
 #include <stdio.h>
 #include <stdlib.h>            /* for malloc() */
 #include <time.h>              /* ANSI standard location for time stuff */
+#include <string.h>
 #ifdef AFS_NT40_ENV
 #include <fcntl.h>
 #include <io.h>
 #else
 #include <sys/time.h>
 #include <sys/file.h>
-#endif
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
+#include <unistd.h>
 #endif
 #include <afs/assert.h>
 
@@ -109,7 +104,7 @@ RCSID
 #include <afs/nfs.h>           /* yuck.  This is an abomination. */
 #include <lwp.h>
 #include <rx/rx.h>
-#include <afscbint.h>
+#include <afs/afscbint.h>
 #include <afs/afsutil.h>
 #include <lock.h>
 #include <afs/ihandle.h>
@@ -120,94 +115,27 @@ RCSID
 
 #include <afs/ptclient.h>      /* need definition of prlist for host.h */
 #include "host.h"
+#include "callback.h"
+#ifdef AFS_DEMAND_ATTACH_FS
+#include "../tviced/serialize_state.h"
+#endif /* AFS_DEMAND_ATTACH_FS */
+
 
 extern afsUUID FS_HostUUID;
 extern int hostCount;
-int ShowProblems = 1;
-
-/* Maximum number of call backs to break at once, single fid */
-/* There is some debate as to just how large this value should be */
-/* Ideally, it would be very very large, but I am afraid that the */
-/* cache managers will all send in their responses simultaneously, */
-/* thereby swamping the file server.  As a result, something like */
-/* 10 or 15 might be a better bet. */
-#define MAX_CB_HOSTS   10
 
-/* max time to break a callback, otherwise client is dead or net is hosed */
-#define MAXCBT 25
-
-#define u_byte unsigned char
+#ifndef INTERPRET_DUMP
+static int ShowProblems = 1;
+#endif
 
 struct cbcounters cbstuff;
 
-struct cbstruct {
-    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] */
-#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] */
-
-/* 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 */
-
-/* call back indices to pointers, and vice-versa */
-#define itocb(i)    ((i)?CB+(i):0)
-#define cbtoi(cbp)  (!(cbp)?0:(cbp)-CB)
-
-/* file entry indices to pointers, and vice-versa */
-#define itofe(i)    ((i)?FE+(i):0)
-#define fetoi(fep)  (!(fep)?0:(fep)-FE)
-
-/* Timeouts:  there are 128 possible timeout values in effect at any
- * given time.  Each timeout represents timeouts in an interval of 128
- * seconds.  So the maximum timeout for a call back is 128*128=16384
- * seconds, or 4 1/2 hours.  The timeout cleanup stuff is called only
- * if space runs out or by the file server every 5 minutes.  This 5
- * minute slack should be allowed for--so a maximum time of 4 hours
- * is safer.
- *
- * Timeouts must be chosen to correspond to an exact multiple
- * of 128, because all times are truncated to a 128 multiple, and
- * timed out if the current truncated time is <= to the truncated time
- * corresponding to the timeout queue.
- */
+static struct FileEntry * FE = NULL;    /* don't use FE[0] */
+static struct CallBack * CB = NULL;     /* don't use CB[0] */
 
-/* Unix time to Call Back time, and vice-versa.  Call back time is
-   in units of 128 seconds, corresponding to time queues. */
-#define CBtime(uxtime) ((uxtime)>>7)
-#define UXtime(cbtime) ((cbtime)<<7)
+static struct CallBack * CBfree = NULL;
+static struct FileEntry * FEfree = NULL;
 
-/* Given a Unix time, compute the closest Unix time that corresponds to
-   a time queue, rounding up */
-#define TimeCeiling(uxtime)    (((uxtime)+127)&~127)
 
 /* Time to live for call backs depends upon number of users of the file.
  * TimeOuts is indexed by this number/8 (using TimeOut macro).  Times
@@ -227,56 +155,25 @@ static int TimeOuts[] = {
 };                             /* Anything more: MinTimeOut */
 
 /* minimum time given for a call back */
+#ifndef INTERPRET_DUMP
 static int MinTimeOut = (7 * 60);
-
-#define TimeOutCutoff   ((sizeof(TimeOuts)/sizeof(TimeOuts[0]))*8)
-#define TimeOut(nusers)  ((nusers)>=TimeOutCutoff? MinTimeOut: TimeOuts[(nusers)>>3])
-
-/* time out at server is 3 minutes more than ws */
-#define ServerBias       (3*60)
+#endif
 
 /* Heads of CB queues; a timeout index is 1+index into this array */
-static afs_uint32 timeout[128];
-
-/* Convert cbtime to timeout queue index */
-#define TIndex(cbtime)  (((cbtime)&127)+1)
-
-/* Convert cbtime to pointer to timeout queue head */
-#define THead(cbtime)  (&timeout[TIndex(cbtime)-1])
+static afs_uint32 timeout[CB_NUM_TIMEOUT_QUEUES];
 
 static afs_int32 tfirst;       /* cbtime of oldest unexpired call back time queue */
 
-/* Normalize index into timeout array so that two such indices will be
-   ordered correctly, so that they can be compared to see which times
-   sooner, or so that the difference in time out times between them
-   can be computed. */
-#define TNorm(index)   ((index)<TIndex(tfirst)?(index)+128:(index))
-
-/* This converts a timeout index into the actual time it will expire */
-#define TIndexToTime(index) (UXtime(TNorm(index) - TIndex(tfirst) + tfirst))
-
-
-/* Convert pointer to timeout queue head to index, and vice versa */
-#define ttoi(t)                ((t-timeout)+1)
-#define itot(i)                ((timeout)+(i-1))
 
 /* 16 byte object get/free routines */
 struct object {
     struct object *next;
 };
 
-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 CallBack *CBfree = 0;
-struct FileEntry *FEfree = 0;
-
 /* Prototypes for static routines */
 static struct FileEntry *FindFE(register AFSFid * fid);
+
+#ifndef INTERPRET_DUMP
 static struct CallBack *iGetCB(register int *nused);
 static int iFreeCB(register struct CallBack *cb, register int *nused);
 static struct FileEntry *iGetFE(register int *nused);
@@ -297,25 +194,23 @@ static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
 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);
+                                   void *rock);
 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);
+                                        void *rock);
 static int GetSomeSpace_r(struct host *hostp, int locked);
 static int ClearHostCallbacks_r(struct host *hp, int locked);
+#endif
 
 #define GetCB() ((struct CallBack *)iGetCB(&cbstuff.nCBs))
 #define GetFE() ((struct FileEntry *)iGetFE(&cbstuff.nFEs))
 #define FreeCB(cb) iFreeCB((struct CallBack *)cb, &cbstuff.nCBs)
 #define FreeFE(fe) iFreeFE((struct FileEntry *)fe, &cbstuff.nFEs)
 
+
 /* Other protos - move out sometime */
-extern void ShutDown();
+void PrintCB(register struct CallBack *cb, afs_uint32 now);
 
-#define VHASH 512              /* Power of 2 */
-static afs_uint32 HashTable[VHASH];    /* File entry hash table */
-#define VHash(volume, unique) (((volume)+(unique))&(VHASH-1))
+static afs_uint32 HashTable[FEHASH_SIZE];      /* File entry hash table */
 
 static struct FileEntry *
 FindFE(register AFSFid * fid)
@@ -324,7 +219,7 @@ FindFE(register AFSFid * fid)
     register int fei;
     register struct FileEntry *fe;
 
-    hash = VHash(fid->Volume, fid->Unique);
+    hash = FEHash(fid->Volume, fid->Unique);
     for (fei = HashTable[hash]; fei; fei = fe->fnext) {
        fe = itofe(fei);
        if (fe->volid == fid->Volume && fe->unique == fid->Unique
@@ -423,11 +318,11 @@ HAdd(register struct CallBack *cb, register struct host *host)
     if (!host->cblist) {
        host->cblist = cb->hnext = cb->hprev = cbtoi(cb);
     } else {
-       register struct CallBack *hhp = itocb(host->cblist);
+       register struct CallBack *fcb = itocb(host->cblist);
 
-       cb->hprev = hhp->hprev;
-       cb->hnext = host->cblist;
-       hhp->hprev = (itocb(hhp->hprev)->hnext = cbtoi(cb));
+       cb->hprev = fcb->hprev;
+       cb->hnext = cbtoi(fcb);
+       fcb->hprev = (itocb(fcb->hprev)->hnext = cbtoi(cb));
     }
     return 0;
 }
@@ -477,7 +372,7 @@ 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;
+static int Ccdelpt = 0, CcdelB = 0;
 
 static int
 CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
@@ -493,7 +388,7 @@ CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
        CcdelB++;
     *cbp = cb->cnext;
     FreeCB(cb);
-    if (deletefe && (--fe->ncbs == 0))
+    if ((--fe->ncbs == 0) && deletefe)
        FDel(fe);
     return 0;
 }
@@ -524,7 +419,7 @@ static int
 FDel(register struct FileEntry *fe)
 {
     register int fei = fetoi(fe);
-    register afs_uint32 *p = &HashTable[VHash(fe->volid, fe->unique)];
+    register afs_uint32 *p = &HashTable[FEHash(fe->volid, fe->unique)];
 
     while (*p && *p != fei)
        p = &itofe(*p)->fnext;
@@ -534,6 +429,7 @@ FDel(register struct FileEntry *fe)
     return 0;
 }
 
+/* initialize the callback package */
 int
 InitCallBack(int nblks)
 {
@@ -541,19 +437,21 @@ InitCallBack(int nblks)
     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 = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry))));
     if (!FE) {
        ViceLog(0, ("Failed malloc in InitCallBack\n"));
        assert(0);
     }
+    FE--;  /* FE[0] is supposed to point to junk */
     cbstuff.nFEs = nblks;
     while (cbstuff.nFEs)
        FreeFE(&FE[cbstuff.nFEs]);      /* This is correct */
-    CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack)))) - 1;
+    CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))));
     if (!CB) {
        ViceLog(0, ("Failed malloc in InitCallBack\n"));
        assert(0);
     }
+    CB--;  /* CB[0] is supposed to point to junk */
     cbstuff.nCBs = nblks;
     while (cbstuff.nCBs)
        FreeCB(&CB[cbstuff.nCBs]);      /* This is correct */
@@ -620,12 +518,13 @@ int
 AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
             int locked)
 {
-    int retVal;
+    int retVal = 0;
     H_LOCK;
     if (!locked) {
        h_Lock_r(host);
     }
-    retVal = AddCallBack1_r(host, fid, thead, type, 1);
+    if (!(host->hostFlags & HOSTDELETED))
+        retVal = AddCallBack1_r(host, fid, thead, type, 1);
 
     if (!locked) {
        h_Unlock_r(host);
@@ -641,11 +540,13 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
     struct FileEntry *fe;
     struct CallBack *cb = 0, *lastcb = 0;
     struct FileEntry *newfe = 0;
-    afs_uint32 time_out;
+    afs_uint32 time_out = 0;
     afs_uint32 *Thead = thead;
     struct CallBack *newcb = 0;
     int safety;
 
+    cbstuff.AddCallBacks++;
+
     host->Console |= 2;
 
     /* allocate these guys first, since we can't call the allocator with
@@ -664,6 +565,11 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
     if (!locked) {
        h_Lock_r(host);         /* this can yield, so do it before we get any */
        /* fragile info */
+        if (host->hostFlags & HOSTDELETED) {
+            host->Console &= ~2;
+            h_Unlock_r(host);
+            return 0;
+        }
     }
 
     fe = FindFE(fid);
@@ -698,7 +604,7 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
        fe->unique = fid->Unique;
        fe->ncbs = 0;
        fe->status = 0;
-       hash = VHash(fid->Volume, fid->Unique);
+       hash = FEHash(fid->Volume, fid->Unique);
        fe->fnext = HashTable[hash];
        HashTable[hash] = fetoi(fe);
     }
@@ -753,6 +659,14 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
     return 0;
 }
 
+static int
+CompareCBA(const void *e1, const void *e2)
+{
+    const struct cbstruct *cba1 = (const struct cbstruct *)e1;
+    const struct cbstruct *cba2 = (const struct cbstruct *)e2;
+    return ((cba1->hp)->index - (cba2->hp)->index);
+}
+
 /* Take an array full of hosts, all held.  Break callbacks to them, and 
  * release the holds once you're done, except don't release xhost.  xhost 
  * may be NULL.  Currently only works for a single Fid in afidp array.
@@ -778,9 +692,13 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
     int i, j;
     struct rx_connection *conns[MAX_CB_HOSTS];
     static struct AFSCBs tc = { 0, 0 };
+    int multi_to_cba_map[MAX_CB_HOSTS];
 
     assert(ncbas <= MAX_CB_HOSTS);
 
+    /* sort cba list to avoid makecall issues */
+    qsort(cba, ncbas, sizeof(struct cbstruct), CompareCBA);
+
     /* set up conns for multi-call */
     for (i = 0, j = 0; i < ncbas; i++) {
        struct host *thishost = cba[i].hp;
@@ -788,6 +706,7 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
            continue;
        }
        rx_GetConnection(thishost->callback_rxcon);
+       multi_to_cba_map[j] = i;
        conns[j++] = thishost->callback_rxcon;
 
 #ifdef ADAPT_MTU
@@ -806,28 +725,14 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
                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;
-                   }
-               }
+               i = multi_to_cba_map[multi_i];
+               hp = cba[i].hp;
+               idx = cba[i].thead;
 
-               if (!hp) {
+               if (!hp || !idx) {
                    ViceLog(0,
-                           ("BCB: INTERNAL ERROR: hp=%x, cba=%x\n", hp,
-                            cba));
+                           ("BCB: INTERNAL ERROR: hp=%p, cba=%p, thead=%u\n",
+                            hp, cba, idx));
                } else {
                    /* 
                     ** try breaking callbacks on alternate interface addresses
@@ -835,22 +740,26 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
                    if (MultiBreakCallBackAlternateAddress(hp, afidp)) {
                        if (ShowProblems) {
                            ViceLog(7,
-                                   ("BCB: Failed on file %u.%u.%u, host %s:%d is down\n",
+                                   ("BCB: Failed on file %u.%u.%u, "
+                                    "Host %p (%s:%d) is down\n",
                                     afidp->AFSCBFids_val->Volume,
                                     afidp->AFSCBFids_val->Vnode,
                                     afidp->AFSCBFids_val->Unique,
+                                     hp,
                                     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);
+                        if (!(hp->hostFlags & HOSTDELETED)) {
+                            hp->hostFlags |= VENUSDOWN;
+                            /**
+                             * We always go into AddCallBack1_r with the host locked
+                             */
+                            AddCallBack1_r(hp, afidp->AFSCBFids_val, itot(idx),
+                                           CB_DELAYED, 1);
+                        }
                        h_Unlock_r(hp); 
                        H_UNLOCK;
                    }
@@ -905,8 +814,8 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
     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),
+           ("BCB: BreakCallBack(Host %p all but %s:%d, (%u,%u,%u))\n",
+            xhost, afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
             fid->Volume, fid->Vnode, fid->Unique));
 
     H_LOCK;
@@ -935,15 +844,17 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
                    ViceLog(0, ("BCB: BOGUS! cb->hhead is NULL!\n"));
                } else if (thishost->hostFlags & VENUSDOWN) {
                    ViceLog(7,
-                           ("BCB: %s:%d is down; delaying break call back\n",
-                            afs_inet_ntoa_r(thishost->host, hoststr),
+                           ("BCB: %p (%s:%d) is down; delaying break call back\n",
+                            thishost, afs_inet_ntoa_r(thishost->host, hoststr),
                             ntohs(thishost->port)));
                    cb->status = CB_DELAYED;
                } else {
-                   h_Hold_r(thishost);
-                   cba[ncbas].hp = thishost;
-                   cba[ncbas].thead = cb->thead;
-                   ncbas++;
+                   if (!(thishost->hostFlags & HOSTDELETED)) {
+                       h_Hold_r(thishost);
+                       cba[ncbas].hp = thishost;
+                       cba[ncbas].thead = cb->thead;
+                       ncbas++;
+                   }
                    TDel(cb);
                    HDel(cb);
                    CDel(cb, 1);        /* Usually first; so this delete 
@@ -981,10 +892,11 @@ DeleteCallBack(struct host *host, AFSFid * fid)
     register afs_uint32 *pcb;
     char hoststr[16];
 
+    H_LOCK;
     cbstuff.DeleteCallBacks++;
 
-    H_LOCK;
     h_Lock_r(host);
+    /* do not care if the host has been HOSTDELETED */
     fe = FindFE(fid);
     if (!fe) {
        h_Unlock_r(host);
@@ -997,8 +909,8 @@ DeleteCallBack(struct host *host, AFSFid * fid)
     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),
+               ("DCB: No call back for host %p (%s:%d), (%u, %u, %u)\n",
+                host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
                 fid->Volume, fid->Vnode, fid->Unique));
        h_Unlock_r(host);
        H_UNLOCK;
@@ -1042,6 +954,7 @@ DeleteFileCallBacks(AFSFid * fid)
        TDel(cb);
        HDel(cb);
        FreeCB(cb);
+       fe->ncbs--;
     }
     FDel(fe);
     H_UNLOCK;
@@ -1100,7 +1013,7 @@ BreakDelayedCallBacks_r(struct host *host)
 
     cbstuff.nbreakers++;
     if (!(host->hostFlags & RESETDONE) && !(host->hostFlags & HOSTDELETED)) {
-       host->hostFlags &= ~ALTADDR;    /* alterrnate addresses are invalid */
+       host->hostFlags &= ~ALTADDR;    /* alternate addresses are invalid */
        cb_conn = host->callback_rxcon;
        rx_GetConnection(cb_conn);
        if (host->interface) {
@@ -1118,15 +1031,17 @@ BreakDelayedCallBacks_r(struct host *host)
        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),
+                       ("CB: Call back connect back failed (in break delayed) "
+                        "for Host %p (%s:%d)\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port)));
            }
            host->hostFlags |= VENUSDOWN;
        } else {
            ViceLog(25,
-                   ("InitCallBackState success on %s\n",
-                    afs_inet_ntoa_r(host->host, hoststr)));
+                   ("InitCallBackState success on %p (%s:%d)\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
+                    ntohs(host->port)));
            /* reset was done successfully */
            host->hostFlags |= RESETDONE;
            host->hostFlags &= ~VENUSDOWN;
@@ -1165,15 +1080,17 @@ BreakDelayedCallBacks_r(struct host *host)
                int i;
                if (ShowProblems) {
                    ViceLog(0,
-                           ("CB: XCallBackBulk failed, host=%s:%d; callback list follows:\n",
-                            afs_inet_ntoa_r(host->host, hoststr),
+                           ("CB: XCallBackBulk failed, Host %p (%s:%d); "
+                            "callback list follows:\n",
+                             host, afs_inet_ntoa_r(host->host, hoststr),
                             ntohs(host->port)));
                }
                for (i = 0; i < nfids; i++) {
                    if (ShowProblems) {
                        ViceLog(0,
-                               ("CB: Host %s:%d, file %u.%u.%u (part of bulk callback)\n",
-                                afs_inet_ntoa_r(host->host, hoststr),
+                               ("CB: Host %p (%s:%d), file %u.%u.%u "
+                                "(part of bulk callback)\n",
+                                host, afs_inet_ntoa_r(host->host, hoststr),
                                 ntohs(host->port), fids[i].Volume,
                                 fids[i].Vnode, fids[i].Unique));
                    }
@@ -1193,7 +1110,7 @@ BreakDelayedCallBacks_r(struct host *host)
 
     cbstuff.nbreakers--;
     /* If we succeeded it's always ok to unset HFE_LATER */
-    if (!host->hostFlags & VENUSDOWN)
+    if (!(host->hostFlags & VENUSDOWN))
        host->hostFlags &= ~HFE_LATER;
     return (host->hostFlags & VENUSDOWN);
 }
@@ -1215,17 +1132,11 @@ MultiBreakVolumeCallBack_r(struct host *host, int isheld,
 
     if (host->hostFlags & VENUSDOWN) {
        h_Lock_r(host);
-       if (host->hostFlags & HOSTDELETED) {
-           h_Unlock_r(host);
-           return 0;           /* Release hold */
-       }
-       ViceLog(8,
-               ("BVCB: volume call back for host %s:%d failed\n",
-                afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
+        /* Do not care if the host is now HOSTDELETED */
        if (ShowProblems) {
            ViceLog(0,
-                   ("CB: volume callback for host %s:%d failed\n",
-                    afs_inet_ntoa_r(host->host, hoststr),
+                   ("BVCB: volume callback for Host %p (%s:%d) failed\n",
+                    host, afs_inet_ntoa_r(host->host, hoststr),
                     ntohs(host->port)));
        }
        DeleteAllCallBacks_r(host, deletefe);   /* Delete all callback state 
@@ -1263,9 +1174,10 @@ MultiBreakVolumeCallBack_r(struct host *host, int isheld,
 ** isheld is 1 if the host is held in BreakVolumeCallBacks
 */
 static int
-MultiBreakVolumeCallBack(struct host *host, int isheld,
-                        struct VCBParams *parms)
+MultiBreakVolumeCallBack(struct host *host, int isheld, void *rock)
 {
+    struct VCBParams *parms = (struct VCBParams *) rock;
+    
     int retval;
     H_LOCK;
     retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 1);
@@ -1278,9 +1190,9 @@ MultiBreakVolumeCallBack(struct host *host, int isheld,
 ** isheld is 1 if the host is held in BreakVolumeCallBacks
 */
 static int
-MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
-                             struct VCBParams *parms)
+MultiBreakVolumeLaterCallBack(struct host *host, int isheld, void *rock)
 {
+    struct VCBParams *parms = (struct VCBParams *)rock;
     int retval;
     H_LOCK;
     retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 0);
@@ -1316,17 +1228,20 @@ BreakVolumeCallBacks(afs_uint32 volume)
 
     H_LOCK;
     fid.Volume = volume, fid.Vnode = fid.Unique = 0;
-    for (hash = 0; hash < VHASH; hash++) {
+    for (hash = 0; hash < FEHASH_SIZE; hash++) {
        for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
            if (fe->volid == volume) {
                register struct CallBack *cbnext;
                for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
                    host = h_itoh(cb->hhead);
-                   h_Hold_r(host);
-                   cbnext = itocb(cb->cnext);
-                   if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
-                       tthead = cb->thead;
+
+                   if (!(host->hostFlags & HOSTDELETED)) {
+                       h_Hold_r(host);
+                       if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
+                           tthead = cb->thead;
+                       }
                    }
+                   cbnext = itocb(cb->cnext);
                    TDel(cb);
                    HDel(cb);
                    FreeCB(cb);
@@ -1349,7 +1264,7 @@ BreakVolumeCallBacks(afs_uint32 volume)
     henumParms.fid = &fid;
     henumParms.thead = tthead;
     H_UNLOCK;
-    h_Enumerate(MultiBreakVolumeCallBack, (char *)&henumParms);
+    h_Enumerate(MultiBreakVolumeCallBack, &henumParms);
     H_LOCK;
     if (henumParms.ncbas) {    /* do left-overs */
        struct AFSCBFids tf;
@@ -1374,7 +1289,7 @@ int
 BreakVolumeCallBacksLater(afs_uint32 volume)
 {
     int hash;
-    afs_int32 *feip;
+    afs_uint32 *feip;
     struct FileEntry *fe;
     struct CallBack *cb;
     struct host *host;
@@ -1382,8 +1297,8 @@ BreakVolumeCallBacksLater(afs_uint32 volume)
 
     ViceLog(25, ("Setting later on volume %u\n", volume));
     H_LOCK;
-    for (hash = 0; hash < VHASH; hash++) {
-       for (feip = &HashTable[hash]; fe = itofe(*feip);) {
+    for (hash = 0; hash < FEHASH_SIZE; hash++) {
+       for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
            if (fe->volid == volume) {
                register struct CallBack *cbnext;
                for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
@@ -1395,7 +1310,7 @@ BreakVolumeCallBacksLater(afs_uint32 volume)
                FSYNC_LOCK;
                fe->status |= FE_LATER;
                FSYNC_UNLOCK;
-               found++;
+               found = 1;
            }
            feip = &fe->fnext;
        }
@@ -1422,7 +1337,7 @@ BreakLaterCallBacks(void)
 {
     struct AFSFid fid;
     int hash;
-    afs_int32 *feip;
+    afs_uint32 *feip;
     struct CallBack *cb;
     struct FileEntry *fe = NULL;
     struct FileEntry *myfe = NULL;
@@ -1438,8 +1353,8 @@ BreakLaterCallBacks(void)
     /* 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);) {
+    for (hash = 0; hash < FEHASH_SIZE; hash++) {
+       for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
            if (fe && (fe->status & FE_LATER)
                && (fid.Volume == 0 || fid.Volume == fe->volid)) {
                /* Ugly, but used to avoid left side casting */
@@ -1449,6 +1364,7 @@ BreakLaterCallBacks(void)
                         fe->volid));
                fid.Volume = fe->volid;
                *feip = fe->fnext;
+               fe->status &= ~FE_LATER; /* not strictly needed */
                /* Works since volid is deeper than the largest pointer */
                tmpfe = (struct object *)fe;
                tmpfe->next = (struct object *)myfe;
@@ -1472,9 +1388,11 @@ BreakLaterCallBacks(void)
            cbnext = itocb(cb->cnext);
            host = h_itoh(cb->hhead);
            if (cb->status == CB_DELAYED) {
-               h_Hold_r(host);
-               if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
-                   tthead = cb->thead;
+               if (!(host->hostFlags & HOSTDELETED)) {
+                   h_Hold_r(host);
+                   if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
+                       tthead = cb->thead;
+                   }
                }
                TDel(cb);
                HDel(cb);
@@ -1482,8 +1400,8 @@ BreakLaterCallBacks(void)
                /* 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),
+                       ("Found host %p (%s:%d) non-DELAYED cb for %u:%u:%u\n",
+                        host, afs_inet_ntoa_r(host->host, hoststr),
                         ntohs(host->port), fe->vnode, fe->unique, fe->volid));
            }
        }
@@ -1545,8 +1463,9 @@ CleanupTimedOutCallBacks_r(void)
                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),
+                       ("CCB: deleting timed out call back %x (%s:%d), (%u,%u,%u)\n",
+                         h_itoh(cb->hhead)->host,
+                         afs_inet_ntoa_r(h_itoh(cb->hhead)->host, hoststr),
                         h_itoh(cb->hhead)->port, itofe(cb->fhead)->volid,
                         itofe(cb->fhead)->vnode, itofe(cb->fhead)->unique));
                HDel(cb);
@@ -1567,35 +1486,107 @@ CleanupTimedOutCallBacks_r(void)
     return (ntimedout > 0);
 }
 
-static struct host *lih_host;
-static int lih_host_held;
+/**
+ * parameters to pass to lih*_r from h_Enumerate_r when trying to find a host
+ * from which to clear callbacks.
+ */
+struct lih_params {
+    /**
+     * Points to the least interesting host found; try to clear callbacks on
+     * this host after h_Enumerate_r(lih*_r)'ing.
+     */
+    struct host *lih;
+
+    /**
+     * The last host we got from lih*_r, but we couldn't clear its callbacks
+     * for some reason. Choose the next-best host after this one (with the
+     * current lih*_r, this means to only select hosts that have an ActiveCall
+     * newer than lastlih).
+     */
+    struct host *lastlih;
+};
+
+/* Value of host->refCount that allows us to reliably infer that
+ * host may be held by some other thread */
+#define OTHER_MUSTHOLD_LIH 2
+
+/* This version does not allow 'host' to be selected unless its ActiveCall 
+ * is newer than 'params->lastlih' which is the host with the oldest
+ * ActiveCall from the last pass (if it is provided).  We filter out any hosts
+ * that are are held by other threads.
+ *
+ * There is a small problem here, but it may not be easily fixable. Say we
+ * select some host A, and give it back to GetSomeSpace_r. GSS_r for some
+ * reason cannot clear the callbacks on A, and so calls us again with
+ * lastlih = A. Suppose there is another host B that has the same ActiveCall
+ * time as A. We will now skip over host B, since
+ * 'hostB->ActiveCall > hostA->ActiveCall' is not true. This could result in
+ * us prematurely going to the GSS_r 2nd or 3rd pass, and making us a little
+ * inefficient. This should be pretty rare, though, except perhaps in cases
+ * with very small numbers of hosts.
+ *
+ * Also filter out any hosts with HOSTDELETED set. h_Enumerate_r should in
+ * theory not give these to us anyway, but be paranoid.
+ */
+static int
+lih0_r(register struct host *host, register int flags, void *rock)
+{
+    struct lih_params *params = (struct lih_params *)rock;
+
+    if (host->cblist
+       && (!(host->hostFlags & HOSTDELETED))
+       && (host->refCount < OTHER_MUSTHOLD_LIH)
+       && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
+       && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
+
+       if (params->lih) {
+           h_Release_r(params->lih); /* release prev host */
+       }
+
+       h_Hold_r(host);
+       params->lih = host;
+    }
+    return flags;
+}
 
+/* same as lih0_r, except we do not prevent held hosts from being selected. */
 static int
-lih_r(register struct host *host, register int held,
-      register struct host *hostp)
+lih1_r(register struct host *host, register int flags, void *rock)
 {
+    struct lih_params *params = (struct lih_params *)rock;
+
     if (host->cblist
-       && ((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);
+       && (!(host->hostFlags & HOSTDELETED))
+       && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
+       && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
+
+       if (params->lih) {
+           h_Release_r(params->lih); /* release prev host */
        }
-       lih_host = host;
-       lih_host_held = !held;
-       held = 1;
+
+       h_Hold_r(host);
+       params->lih = host;
     }
-    return held;
+    return flags;
 }
 
 /* This could be upgraded to get more space each time */
-/* first pass: find the oldest host which isn't held by anyone */
-/* second pass: find the oldest host who isn't "me" */
+/* first pass: sequentially find the oldest host which isn't held by
+               anyone for which we can clear callbacks;
+              skipping 'hostp' */
+/* second pass: sequentially find the oldest host regardless of 
+               whether or not the host is held; skipping 'hostp' */
+/* third pass: attempt to clear callbacks from 'hostp' */
 /* always called with hostp unlocked */
+
+/* Note: hostlist is ordered most recently created host first and 
+ * its order has no relationship to the most recently used. */
 extern struct host *hostList;
 static int
 GetSomeSpace_r(struct host *hostp, int locked)
 {
-    register struct host *hp, *hp1 = (struct host *)0, *hp2 = hostList;
+    struct host *hp;
+    struct lih_params params;
     int i = 0;
 
     cbstuff.GotSomeSpaces++;
@@ -1605,44 +1596,49 @@ GetSomeSpace_r(struct host *hostp, int locked)
        cbstuff.GSS3++;
        return 0;
     }
+
+    i = 0;
+    params.lastlih = NULL;
+
     do {
-       lih_host = 0;
-       h_Enumerate_r(lih_r, hp2, (char *)hp1);
-       hp = lih_host;
+       params.lih = NULL;
+
+       h_Enumerate_r(i == 0 ? lih0_r : lih1_r, hostList, &params);
+
+       hp = params.lih;
+       if (params.lastlih) {
+           h_Release_r(params.lastlih);
+           params.lastlih = NULL;
+       }
+
        if (hp) {
-           /* set in lih_r! private copy before giving up H_LOCK */
-           int lih_host_held2=lih_host_held;   
+           /* note that 'hp' was held by lih*_r; we will need to release it */
            cbstuff.GSS4++;
-           if (!ClearHostCallbacks_r(hp, 0 /* not locked or held */ )) {
-               if (lih_host_held2)
-                   h_Release_r(hp);
+           if ((hp != hostp) && !ClearHostCallbacks_r(hp, 0 /* not locked or held */ )) {
+                h_Release_r(hp);
                return 0;
            }
-           if (lih_host_held2)
-               h_Release_r(hp);
-           hp2 = hp->next;
+
+           params.lastlih = hp;
+           /* params.lastlih will be released on the next iteration, after
+            * h_Enumerate_r */
+
        } else {
-           hp2 = hostList;
-           hp1 = hostp;
-           cbstuff.GSS1++;
-           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
-            * remove its callbacks) or it's held since the only other
-            * option is starvation for the file server (i.e. until the
-            * callback timeout arrives).
+            * it's held, since the only other option is starvation for
+            * the file server (i.e. until the callback timeout arrives).
             */
            i++;
+           params.lastlih = NULL;
+           cbstuff.GSS1++;
+           ViceLog(5,
+                   ("GSS: Try harder for longest inactive host cnt= %d\n",
+                    i));
        }
     } while (i < 2);
-    /*
-     * No choice to clear this host's callback state
-     */
-    /* third pass: we still haven't gotten any space, so we free what we had
-     * previously passed over. */
+
+    /* Could not obtain space from other hosts, clear hostp's callback state */
     cbstuff.GSS2++;
     if (!locked) {
        h_Lock_r(hostp);
@@ -1659,25 +1655,30 @@ static int
 ClearHostCallbacks_r(struct host *hp, int locked)
 {
     int code;
-    int held = 0;
     char hoststr[16];
     struct rx_connection *cb_conn = NULL;
 
     ViceLog(5,
-           ("GSS: Delete longest inactive host %s\n",
-            afs_inet_ntoa_r(hp->host, hoststr)));
-    if (!(held = h_Held_r(hp)))
-       h_Hold_r(hp);
+           ("GSS: Delete longest inactive host %p (%s:%d)\n",
+             hp, afs_inet_ntoa_r(hp->host, hoststr), ntohs(hp->port)));
+
+    if ((hp->hostFlags & HOSTDELETED)) {
+       /* hp could go away after reacquiring H_LOCK in h_NBLock_r, so we can't
+        * really use it; its callbacks will get cleared anyway when
+        * h_TossStuff_r gets its hands on it */
+       return 1;
+    }
+
+    h_Hold_r(hp);
 
     /** Try a non-blocking lock. If the lock is already held return
       * after releasing hold on hp
       */
     if (!locked) {
-       if (h_NBLock_r(hp)) {
-           if (!held)
-               h_Release_r(hp);
-           return 1;
-       }
+        if (h_NBLock_r(hp)) {
+            h_Release_r(hp);
+            return 1;
+        }
     }
     if (hp->Console & 2) {
        /*
@@ -1694,7 +1695,7 @@ ClearHostCallbacks_r(struct host *hp, int locked)
     DeleteAllCallBacks_r(hp, 1);
     if (hp->hostFlags & VENUSDOWN) {
        hp->hostFlags &= ~RESETDONE;    /* remember that we must do a reset */
-    } else {
+    } else if (!(hp->hostFlags & HOSTDELETED)) {
        /* host is up, try a call */
        hp->hostFlags &= ~ALTADDR;      /* alternate addresses are invalid */
        cb_conn = hp->callback_rxcon;
@@ -1720,11 +1721,9 @@ ClearHostCallbacks_r(struct host *hp, int locked)
            hp->hostFlags |= RESETDONE;
        }
     }
-    if (!locked) {
-       h_Unlock_r(hp);
-    }
-    if (!held)
-       h_Release_r(hp);
+    if (!locked)
+        h_Unlock_r(hp);
+    h_Release_r(hp);
 
     return 0;
 }
@@ -1747,167 +1746,1174 @@ PrintCallBackStats(void)
 }
 
 #define MAGIC 0x12345678       /* To check byte ordering of dump when it is read in */
+#define MAGICV2 0x12345679      /* To check byte ordering & version of dump when it is read in */
+
 
 #ifndef INTERPRET_DUMP
 
+#ifdef AFS_DEMAND_ATTACH_FS
+/*
+ * demand attach fs
+ * callback state serialization
+ */
+static int cb_stateSaveTimeouts(struct fs_dump_state * state);
+static int cb_stateSaveFEHash(struct fs_dump_state * state);
+static int cb_stateSaveFEs(struct fs_dump_state * state);
+static int cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe);
+static int cb_stateRestoreTimeouts(struct fs_dump_state * state);
+static int cb_stateRestoreFEHash(struct fs_dump_state * state);
+static int cb_stateRestoreFEs(struct fs_dump_state * state);
+static int cb_stateRestoreFE(struct fs_dump_state * state);
+static int cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe, 
+                             struct iovec * iov, int niovecs);
+
+static int cb_stateVerifyFEHash(struct fs_dump_state * state);
+static int cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe);
+static int cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe);
+static int cb_stateVerifyTimeoutQueues(struct fs_dump_state * state);
+
+static int cb_stateFEToDiskEntry(struct FileEntry *, struct FEDiskEntry *);
+static int cb_stateDiskEntryToFE(struct fs_dump_state * state,
+                                struct FEDiskEntry *, struct FileEntry *);
+
+static int cb_stateCBToDiskEntry(struct CallBack *, struct CBDiskEntry *);
+static int cb_stateDiskEntryToCB(struct fs_dump_state * state,
+                                struct CBDiskEntry *, struct CallBack *);
+
+static int cb_stateFillHeader(struct callback_state_header * hdr);
+static int cb_stateCheckHeader(struct callback_state_header * hdr);
+
+static int cb_stateAllocMap(struct fs_dump_state * state);
+
 int
-DumpCallBackState(void)
+cb_stateSave(struct fs_dump_state * state)
 {
-    int fd;
-    afs_uint32 magic = MAGIC, now = FT_ApproxTime(), freelisthead;
+    int ret = 0;
 
-    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));
-       return 0;
+    AssignInt64(state->eof_offset, &state->hdr->cb_offset);
+
+    /* invalidate callback state header */
+    memset(state->cb_hdr, 0, sizeof(struct callback_state_header));
+    if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
+                           sizeof(struct callback_state_header))) {
+       ret = 1;
+       goto done;
     }
-    (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;
-}
+    fs_stateIncEOF(state, sizeof(struct callback_state_header));
 
-#endif
+    /* dump timeout state */
+    if (cb_stateSaveTimeouts(state)) {
+       ret = 1;
+       goto done;
+    }
 
-#ifdef INTERPRET_DUMP
+    /* dump fe hashtable state */
+    if (cb_stateSaveFEHash(state)) {
+       ret = 1;
+       goto done;
+    }
 
-/* This is only compiled in for the callback analyzer program */
-/* Returns the time of the dump */
-time_t
-ReadDump(char *file)
+    /* dump callback state */
+    if (cb_stateSaveFEs(state)) {
+       ret = 1;
+       goto done;
+    }
+
+    /* write the callback state header to disk */
+    cb_stateFillHeader(state->cb_hdr);
+    if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
+                           sizeof(struct callback_state_header))) {
+       ret = 1;
+       goto done;
+    }
+    
+ done:
+    return ret;
+}
+
+int
+cb_stateRestore(struct fs_dump_state * state)
 {
-    int fd;
-    afs_uint32 magic, freelisthead;
-    time_t now;
+    int ret = 0;
 
-    fd = open(file, O_RDONLY);
-    if (fd < 0) {
-       fprintf(stderr, "Couldn't read dump file %s\n", file);
-       exit(1);
+    if (fs_stateReadHeader(state, &state->hdr->cb_offset, state->cb_hdr,
+                          sizeof(struct callback_state_header))) {
+       ret = 1;
+       goto done;
     }
-    read(fd, &magic, sizeof(magic));
-    if (magic != MAGIC) {
-       fprintf(stderr,
-               "Magic number of %s is invalid.  You might be trying to\n",
-               file);
-       fprintf(stderr,
-               "run this program on a machine type with a different byte ordering.\n");
-       exit(1);
+
+    if (cb_stateCheckHeader(state->cb_hdr)) {
+       ret = 1;
+       goto done;
     }
-    read(fd, &now, sizeof(now));
-    read(fd, &cbstuff, sizeof(cbstuff));
-    read(fd, TimeOuts, sizeof(TimeOuts));
-    read(fd, timeout, sizeof(timeout));
-    read(fd, &tfirst, sizeof(tfirst));
-    read(fd, &freelisthead, sizeof(freelisthead));
-    CB = ((struct CallBack
-          *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
-    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);
-    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 */
-    if (close(fd)) {
-       perror("Error reading dumpfile");
-       exit(1);
+
+    if (cb_stateAllocMap(state)) {
+       ret = 1;
+       goto done;
     }
-    return now;
-}
 
-#include "AFS_component_version_number.c"
+    if (cb_stateRestoreTimeouts(state)) {
+       ret = 1;
+       goto done;
+    }
+
+    if (cb_stateRestoreFEHash(state)) {
+       ret = 1;
+       goto done;
+    }
+
+    /* restore FEs and CBs from disk */
+    if (cb_stateRestoreFEs(state)) {
+       ret = 1;
+       goto done;
+    }
+
+    /* restore the timeout queue heads */
+    tfirst = state->cb_hdr->tfirst;
+
+ done:
+    return ret;
+}
 
 int
-main(int argc, char **argv)
+cb_stateRestoreIndices(struct fs_dump_state * state)
 {
-    int err = 0, cbi = 0, stats = 0, noptions = 0, all = 0, vol = 0, raw = 0;
-    static AFSFid fid;
-    register struct FileEntry *fe;
-    register struct CallBack *cb;
-    time_t now;
+    int i, ret = 0;
+    struct FileEntry * fe;
+    struct CallBack * cb;
+
+    /* restore indices in the FileEntry structures */
+    for (i = 1; i < state->fe_map.len; i++) {
+       if (state->fe_map.entries[i].new_idx) {
+           fe = itofe(state->fe_map.entries[i].new_idx);
+
+           /* restore the fe->fnext entry */
+           if (fe_OldToNew(state, fe->fnext, &fe->fnext)) {
+               ret = 1;
+               goto done;
+           }
 
-    memset(&fid, 0, sizeof(fid));
-    argc--;
-    argv++;
-    while (argc && **argv == '-') {
-       noptions++;
-       argc--;
-       if (!strcmp(*argv, "-host")) {
-           if (argc < 1) {
-               err++;
-               break;
+           /* restore the fe->firstcb entry */
+           if (cb_OldToNew(state, fe->firstcb, &fe->firstcb)) {
+               ret = 1;
+               goto done;
            }
-           argc--;
-           cbi = atoi(*++argv);
-       } else if (!strcmp(*argv, "-fid")) {
-           if (argc < 2) {
-               err++;
-               break;
+       }
+    }
+    
+    /* restore indices in the CallBack structures */
+    for (i = 1; i < state->cb_map.len; i++) {
+       if (state->cb_map.entries[i].new_idx) {
+           cb = itocb(state->cb_map.entries[i].new_idx);
+
+           /* restore the cb->cnext entry */
+           if (cb_OldToNew(state, cb->cnext, &cb->cnext)) {
+               ret = 1;
+               goto done;
            }
-           argc -= 3;
-           fid.Volume = atoi(*++argv);
-           fid.Vnode = atoi(*++argv);
-           fid.Unique = atoi(*++argv);
-       } else if (!strcmp(*argv, "-time")) {
-           fprintf(stderr, "-time not supported\n");
-           exit(1);
-       } else if (!strcmp(*argv, "-stats")) {
-           stats = 1;
-       } else if (!strcmp(*argv, "-all")) {
-           all = 1;
-       } else if (!strcmp(*argv, "-raw")) {
-           raw = 1;
-       } else if (!strcmp(*argv, "-volume")) {
-           if (argc < 1) {
-               err++;
-               break;
+           
+           /* restore the cb->fhead entry */
+           if (fe_OldToNew(state, cb->fhead, &cb->fhead)) {
+               ret = 1;
+               goto done;
            }
-           argc--;
-           vol = atoi(*++argv);
-       } else
-           err++;
-       argv++;
-    }
+
+           /* restore the cb->hhead entry */
+           if (h_OldToNew(state, cb->hhead, &cb->hhead)) {
+               ret = 1;
+               goto done;
+           }
+
+           /* restore the cb->tprev entry */
+           if (cb_OldToNew(state, cb->tprev, &cb->tprev)) {
+               ret = 1;
+               goto done;
+           }
+
+           /* restore the cb->tnext entry */
+           if (cb_OldToNew(state, cb->tnext, &cb->tnext)) {
+               ret = 1;
+               goto done;
+           }
+
+           /* restore the cb->hprev entry */
+           if (cb_OldToNew(state, cb->hprev, &cb->hprev)) {
+               ret = 1;
+               goto done;
+           }
+
+           /* restore the cb->hnext entry */
+           if (cb_OldToNew(state, cb->hnext, &cb->hnext)) {
+               ret = 1;
+               goto done;
+           }
+       }
+    }
+
+    /* restore the timeout queue head indices */
+    for (i = 0; i < state->cb_timeout_hdr->records; i++) {
+       if (cb_OldToNew(state, timeout[i], &timeout[i])) {
+           ret = 1;
+           goto done;
+       }
+    }
+
+    /* restore the FE hash table queue heads */
+    for (i = 0; i < state->cb_fehash_hdr->records; i++) {
+       if (fe_OldToNew(state, HashTable[i], &HashTable[i])) {
+           ret = 1;
+           goto done;
+       }
+    }
+
+ done:
+    return ret;
+}
+
+int
+cb_stateVerify(struct fs_dump_state * state)
+{
+    int ret = 0;
+
+    if (cb_stateVerifyFEHash(state)) {
+       ret = 1;
+    }
+
+    if (cb_stateVerifyTimeoutQueues(state)) {
+       ret = 1;
+    }
+
+    return ret;
+}
+
+static int
+cb_stateVerifyFEHash(struct fs_dump_state * state)
+{
+    int ret = 0, i;
+    struct FileEntry * fe;
+    afs_uint32 fei, chain_len;
+
+    for (i = 0; i < FEHASH_SIZE; i++) {
+       chain_len = 0;
+       for (fei = HashTable[i], fe = itofe(fei);
+            fe;
+            fei = fe->fnext, fe = itofe(fei)) {
+           if (fei > cbstuff.nblks) {
+               ViceLog(0, ("cb_stateVerifyFEHash: error: index out of range (fei=%d)\n", fei));
+               ret = 1;
+               break;
+           }
+           if (cb_stateVerifyFE(state, fe)) {
+               ret = 1;
+           }
+           if (chain_len > FS_STATE_FE_MAX_HASH_CHAIN_LEN) {
+               ViceLog(0, ("cb_stateVerifyFEHash: error: hash chain %d length exceeds %d; assuming there's a loop\n",
+                           i, FS_STATE_FE_MAX_HASH_CHAIN_LEN));
+               ret = 1;
+               break;
+           }
+           chain_len++;
+       }
+    }
+
+    return ret;
+}
+
+static int
+cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe)
+{
+    int ret = 0;
+
+    if ((fe->firstcb && !fe->ncbs) ||
+       (!fe->firstcb && fe->ncbs)) {
+       ViceLog(0, ("cb_stateVerifyFE: error: fe->firstcb does not agree with fe->ncbs (fei=%lu, fe->firstcb=%lu, fe->ncbs=%lu)\n",
+                   afs_printable_uint32_lu(fetoi(fe)),
+                   afs_printable_uint32_lu(fe->firstcb),
+                   afs_printable_uint32_lu(fe->ncbs)));
+       ret = 1;
+    }
+    if (cb_stateVerifyFCBList(state, fe)) {
+       ViceLog(0, ("cb_stateVerifyFE: error: FCBList failed verification (fei=%lu)\n",
+                   afs_printable_uint32_lu(fetoi(fe))));
+       ret = 1;
+    }
+
+    return ret;
+}
+
+static int
+cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe)
+{
+    int ret = 0;
+    afs_uint32 cbi, fei, chain_len = 0;
+    struct CallBack * cb;
+
+    fei = fetoi(fe);
+
+    for (cbi = fe->firstcb, cb = itocb(cbi);
+        cb;
+        cbi = cb->cnext, cb = itocb(cbi)) {
+       if (cbi > cbstuff.nblks) {
+           ViceLog(0, ("cb_stateVerifyFCBList: error: list index out of range (cbi=%d, ncbs=%d)\n",
+                       cbi, cbstuff.nblks));
+           ret = 1;
+           goto done;
+       }
+       if (cb->fhead != fei) {
+           ViceLog(0, ("cb_stateVerifyFCBList: error: cb->fhead != fei (fei=%d, cb->fhead=%d)\n",
+                       fei, cb->fhead));
+           ret = 1;
+       }
+       if (chain_len > FS_STATE_FCB_MAX_LIST_LEN) {
+           ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (fei=%d); assuming there's a loop\n",
+                       FS_STATE_FCB_MAX_LIST_LEN, fei));
+           ret = 1;
+           goto done;
+       }
+       chain_len++;
+    }
+
+    if (fe->ncbs != chain_len) {
+       ViceLog(0, ("cb_stateVerifyFCBList: error: list length mismatch (len=%d, fe->ncbs=%d)\n",
+                   chain_len, fe->ncbs));
+       ret = 1;
+    }
+
+ done:
+    return ret;
+}
+
+int
+cb_stateVerifyHCBList(struct fs_dump_state * state, struct host * host)
+{
+    int ret = 0;
+    afs_uint32 hi, chain_len, cbi;
+    struct CallBack *cb, *ncb;
+
+    hi = h_htoi(host);
+    chain_len = 0;
+
+    for (cbi = host->cblist, cb = itocb(cbi);
+        cb;
+        cbi = cb->hnext, cb = ncb) {
+       if (chain_len && (host->cblist == cbi)) {
+           /* we've wrapped around the circular list, and everything looks ok */
+           break;
+       }
+       if (cb->hhead != hi) {
+           ViceLog(0, ("cb_stateVerifyHCBList: error: incorrect cb->hhead (cbi=%d, h->index=%d, cb->hhead=%d)\n",
+                       cbi, hi, cb->hhead));
+           ret = 1;
+       }
+       if (!cb->hprev || !cb->hnext) {
+           ViceLog(0, ("cb_stateVerifyHCBList: error: null index in circular list (cbi=%d, h->index=%d)\n",
+                       cbi, hi));
+           ret = 1;
+           goto done;
+       }
+       if ((cb->hprev > cbstuff.nblks) ||
+           (cb->hnext > cbstuff.nblks)) {
+           ViceLog(0, ("cb_stateVerifyHCBList: error: list index out of range (cbi=%d, h->index=%d, cb->hprev=%d, cb->hnext=%d, nCBs=%d)\n",
+                       cbi, hi, cb->hprev, cb->hnext, cbstuff.nblks));
+           ret = 1;
+           goto done;
+       }
+       ncb = itocb(cb->hnext);
+       if (cbi != ncb->hprev) {
+           ViceLog(0, ("cb_stateVerifyHCBList: error: corrupt linked list (cbi=%d, h->index=%d)\n",
+                       cbi, hi));
+           ret = 1;
+           goto done;
+       }
+       if (chain_len > FS_STATE_HCB_MAX_LIST_LEN) {
+           ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (h->index=%d); assuming there's a loop\n",
+                       FS_STATE_HCB_MAX_LIST_LEN, hi));
+           ret = 1;
+           goto done;
+       }
+       chain_len++;
+    }
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateVerifyTimeoutQueues(struct fs_dump_state * state)
+{
+    int ret = 0, i;
+    afs_uint32 cbi, chain_len;
+    struct CallBack *cb, *ncb;
+
+    for (i = 0; i < CB_NUM_TIMEOUT_QUEUES; i++) {
+       chain_len = 0;
+       for (cbi = timeout[i], cb = itocb(cbi);
+            cb;
+            cbi = cb->tnext, cb = ncb) {
+           if (chain_len && (cbi == timeout[i])) {
+               /* we've wrapped around the circular list, and everything looks ok */
+               break;
+           }
+           if (cbi > cbstuff.nblks) {
+               ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: list index out of range (cbi=%d, tindex=%d)\n",
+                           cbi, i));
+               ret = 1;
+               break;
+           }
+           if (itot(cb->thead) != &timeout[i]) {
+               ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: cb->thead points to wrong timeout queue (tindex=%d, cbi=%d, cb->thead=%d)\n",
+                           i, cbi, cb->thead));
+               ret = 1;
+           }
+           if (!cb->tprev || !cb->tnext) {
+               ViceLog(0, ("cb_stateVerifyTimeoutQueues: null index in circular list (cbi=%d, tindex=%d)\n",
+                           cbi, i));
+               ret = 1;
+               break;
+           }
+           if ((cb->tprev > cbstuff.nblks) ||
+               (cb->tnext > cbstuff.nblks)) {
+               ViceLog(0, ("cb_stateVerifyTimeoutQueues: list index out of range (cbi=%d, tindex=%d, cb->tprev=%d, cb->tnext=%d, nCBs=%d)\n",
+                           cbi, i, cb->tprev, cb->tnext, cbstuff.nblks));
+               ret = 1;
+               break;
+           }
+           ncb = itocb(cb->tnext);
+           if (cbi != ncb->tprev) {
+               ViceLog(0, ("cb_stateVerifyTimeoutQueues: corrupt linked list (cbi=%d, tindex=%d)\n",
+                           cbi, i));
+               ret = 1;
+               break;
+           }
+           if (chain_len > FS_STATE_TCB_MAX_LIST_LEN) {
+               ViceLog(0, ("cb_stateVerifyTimeoutQueues: list length exceeds %d (tindex=%d); assuming there's a loop\n",
+                           FS_STATE_TCB_MAX_LIST_LEN, i));
+               ret = 1;
+               break;
+           }
+           chain_len++;
+       }
+    }
+
+    return ret;
+}
+
+static int
+cb_stateSaveTimeouts(struct fs_dump_state * state)
+{
+    int ret = 0;
+    struct iovec iov[2];
+
+    AssignInt64(state->eof_offset, &state->cb_hdr->timeout_offset);
+
+    memset(state->cb_timeout_hdr, 0, sizeof(struct callback_state_fehash_header));
+    state->cb_timeout_hdr->magic = CALLBACK_STATE_TIMEOUT_MAGIC;
+    state->cb_timeout_hdr->records = CB_NUM_TIMEOUT_QUEUES;
+    state->cb_timeout_hdr->len = sizeof(struct callback_state_timeout_header) +
+       (state->cb_timeout_hdr->records * sizeof(afs_uint32));
+
+    iov[0].iov_base = (char *)state->cb_timeout_hdr;
+    iov[0].iov_len = sizeof(struct callback_state_timeout_header);
+    iov[1].iov_base = (char *)timeout;
+    iov[1].iov_len = sizeof(timeout);
+
+    if (fs_stateSeek(state, &state->cb_hdr->timeout_offset)) {
+       ret = 1;
+       goto done;
+    }
+
+    if (fs_stateWriteV(state, iov, 2)) {
+       ret = 1;
+       goto done;
+    }
+
+    fs_stateIncEOF(state, state->cb_timeout_hdr->len);
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateRestoreTimeouts(struct fs_dump_state * state)
+{
+    int ret = 0, len;
+
+    if (fs_stateReadHeader(state, &state->cb_hdr->timeout_offset,
+                          state->cb_timeout_hdr, 
+                          sizeof(struct callback_state_timeout_header))) {
+       ret = 1;
+       goto done;
+    }
+
+    if (state->cb_timeout_hdr->magic != CALLBACK_STATE_TIMEOUT_MAGIC) {
+       ret = 1;
+       goto done;
+    }
+    if (state->cb_timeout_hdr->records != CB_NUM_TIMEOUT_QUEUES) {
+       ret = 1;
+       goto done;
+    }
+
+    len = state->cb_timeout_hdr->records * sizeof(afs_uint32);
+
+    if (state->cb_timeout_hdr->len !=
+       (sizeof(struct callback_state_timeout_header) + len)) {
+       ret = 1;
+       goto done;
+    }
+
+    if (fs_stateRead(state, timeout, len)) {
+       ret = 1;
+       goto done;
+    }
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateSaveFEHash(struct fs_dump_state * state)
+{
+    int ret = 0;
+    struct iovec iov[2];
+
+    AssignInt64(state->eof_offset, &state->cb_hdr->fehash_offset);
+
+    memset(state->cb_fehash_hdr, 0, sizeof(struct callback_state_fehash_header));
+    state->cb_fehash_hdr->magic = CALLBACK_STATE_FEHASH_MAGIC;
+    state->cb_fehash_hdr->records = FEHASH_SIZE;
+    state->cb_fehash_hdr->len = sizeof(struct callback_state_fehash_header) +
+       (state->cb_fehash_hdr->records * sizeof(afs_uint32));
+
+    iov[0].iov_base = (char *)state->cb_fehash_hdr;
+    iov[0].iov_len = sizeof(struct callback_state_fehash_header);
+    iov[1].iov_base = (char *)HashTable;
+    iov[1].iov_len = sizeof(HashTable);
+
+    if (fs_stateSeek(state, &state->cb_hdr->fehash_offset)) {
+       ret = 1;
+       goto done;
+    }
+
+    if (fs_stateWriteV(state, iov, 2)) {
+       ret = 1;
+       goto done;
+    }
+
+    fs_stateIncEOF(state, state->cb_fehash_hdr->len);
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateRestoreFEHash(struct fs_dump_state * state)
+{
+    int ret = 0, len;
+
+    if (fs_stateReadHeader(state, &state->cb_hdr->fehash_offset,
+                          state->cb_fehash_hdr, 
+                          sizeof(struct callback_state_fehash_header))) {
+       ret = 1;
+       goto done;
+    }
+
+    if (state->cb_fehash_hdr->magic != CALLBACK_STATE_FEHASH_MAGIC) {
+       ret = 1;
+       goto done;
+    }
+    if (state->cb_fehash_hdr->records != FEHASH_SIZE) {
+       ret = 1;
+       goto done;
+    }
+
+    len = state->cb_fehash_hdr->records * sizeof(afs_uint32);
+
+    if (state->cb_fehash_hdr->len !=
+       (sizeof(struct callback_state_fehash_header) + len)) {
+       ret = 1;
+       goto done;
+    }
+
+    if (fs_stateRead(state, HashTable, len)) {
+       ret = 1;
+       goto done;
+    }
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateSaveFEs(struct fs_dump_state * state)
+{
+    int ret = 0;
+    register int fei, hash;
+    register struct FileEntry *fe;
+
+    AssignInt64(state->eof_offset, &state->cb_hdr->fe_offset);
+
+    for (hash = 0; hash < FEHASH_SIZE ; hash++) {
+       for (fei = HashTable[hash]; fei; fei = fe->fnext) {
+           fe = itofe(fei);
+           if (cb_stateSaveFE(state, fe)) {
+               ret = 1;
+               goto done;
+           }
+       }
+    }
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateRestoreFEs(struct fs_dump_state * state)
+{
+    int count, nFEs, ret = 0;
+
+    nFEs = state->cb_hdr->nFEs;
+
+    for (count = 0; count < nFEs; count++) {
+       if (cb_stateRestoreFE(state)) {
+           ret = 1;
+           goto done;
+       }
+    }
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
+{
+    int ret = 0, iovcnt, cbi, written = 0;
+    afs_uint32 fei;
+    struct callback_state_entry_header hdr;
+    struct FEDiskEntry fedsk;
+    struct CBDiskEntry cbdsk[16];
+    struct iovec iov[16];
+    struct CallBack *cb;
+
+    fei = fetoi(fe);
+    if (fei > state->cb_hdr->fe_max) {
+       state->cb_hdr->fe_max = fei;
+    }
+
+    memset(&hdr, 0, sizeof(struct callback_state_entry_header));
+
+    if (cb_stateFEToDiskEntry(fe, &fedsk)) {
+       ret = 1;
+       goto done;
+    }
+
+    iov[0].iov_base = (char *)&hdr;
+    iov[0].iov_len = sizeof(hdr);
+    iov[1].iov_base = (char *)&fedsk;
+    iov[1].iov_len = sizeof(struct FEDiskEntry);
+    iovcnt = 2;
+
+    for (cbi = fe->firstcb, cb = itocb(cbi); 
+        cb != NULL; 
+        cbi = cb->cnext, cb = itocb(cbi), hdr.nCBs++) {
+       if (cbi > state->cb_hdr->cb_max) {
+           state->cb_hdr->cb_max = cbi;
+       }
+       if (cb_stateCBToDiskEntry(cb, &cbdsk[iovcnt])) {
+           ret = 1;
+           goto done;
+       }
+       cbdsk[iovcnt].index = cbi;
+       iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
+       iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
+       iovcnt++;
+       if ((iovcnt == 16) || (!cb->cnext)) {
+           if (fs_stateWriteV(state, iov, iovcnt)) {
+               ret = 1;
+               goto done;
+           }
+           written = 1;
+           iovcnt = 0;
+       }
+    }
+
+    hdr.magic = CALLBACK_STATE_ENTRY_MAGIC;
+    hdr.len = sizeof(hdr) + sizeof(struct FEDiskEntry) + 
+       (hdr.nCBs * sizeof(struct CBDiskEntry));
+
+    if (!written) {
+       if (fs_stateWriteV(state, iov, iovcnt)) {
+           ret = 1;
+           goto done;
+       }
+    } else {
+       if (fs_stateWriteHeader(state, &state->eof_offset, &hdr, sizeof(hdr))) {
+           ret = 1;
+           goto done;
+       }
+    }
+
+    fs_stateIncEOF(state, hdr.len);
+
+    if (written) {
+       if (fs_stateSeek(state, &state->eof_offset)) {
+           ret = 1;
+           goto done;
+       }
+    }
+
+    state->cb_hdr->nFEs++;
+    state->cb_hdr->nCBs += hdr.nCBs;
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateRestoreFE(struct fs_dump_state * state)
+{
+    int ret = 0, iovcnt, nCBs;
+    struct callback_state_entry_header hdr;
+    struct FEDiskEntry fedsk;
+    struct CBDiskEntry cbdsk[16];
+    struct iovec iov[16];
+    struct FileEntry * fe;
+
+    iov[0].iov_base = (char *)&hdr;
+    iov[0].iov_len = sizeof(hdr);
+    iov[1].iov_base = (char *)&fedsk;
+    iov[1].iov_len = sizeof(fedsk);
+    iovcnt = 2;
+
+    if (fs_stateReadV(state, iov, iovcnt)) {
+       ret = 1;
+       goto done;
+    }
+
+    if (hdr.magic != CALLBACK_STATE_ENTRY_MAGIC) {
+       ret = 1;
+       goto done;
+    }
+
+    fe = GetFE();
+    if (fe == NULL) {
+       ViceLog(0, ("cb_stateRestoreFE: ran out of free FileEntry structures\n"));
+       ret = 1;
+       goto done;
+    }
+
+    if (cb_stateDiskEntryToFE(state, &fedsk, fe)) {
+       ret = 1;
+       goto done;
+    }
+
+    if (hdr.nCBs) {
+       for (iovcnt = 0, nCBs = 0;
+            nCBs < hdr.nCBs;
+            nCBs++) {
+           iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
+           iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
+           iovcnt++;
+           if ((iovcnt == 16) || (nCBs == hdr.nCBs - 1)) {
+               if (fs_stateReadV(state, iov, iovcnt)) {
+                   ret = 1;
+                   goto done;
+               }
+               if (cb_stateRestoreCBs(state, fe, iov, iovcnt)) {
+                   ret = 1;
+                   goto done;
+               }
+               iovcnt = 0;
+           }
+       }
+    }
+    
+ done:
+    return ret;
+}
+
+static int
+cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe, 
+                  struct iovec * iov, int niovecs)
+{
+    int ret = 0, idx;
+    register struct CallBack * cb;
+    struct CBDiskEntry * cbdsk;
+    afs_uint32 fei;
+
+    fei = fetoi(fe);
+
+    for (idx = 0; idx < niovecs; idx++) {
+       cbdsk = (struct CBDiskEntry *) iov[idx].iov_base;
+       if ((cb = GetCB()) == NULL) {
+           ViceLog(0, ("cb_stateRestoreCBs: ran out of free CallBack structures\n"));
+           ret = 1;
+           goto done;
+       }
+       if (cb_stateDiskEntryToCB(state, cbdsk, cb)) {
+           ViceLog(0, ("cb_stateRestoreCBs: corrupt CallBack disk entry\n"));
+           ret = 1;
+           goto done;
+       }
+    }
+
+ done:
+    return ret;
+}
+
+
+static int
+cb_stateFillHeader(struct callback_state_header * hdr)
+{
+    hdr->stamp.magic = CALLBACK_STATE_MAGIC;
+    hdr->stamp.version = CALLBACK_STATE_VERSION;
+    hdr->tfirst = tfirst;
+    return 0;
+}
+
+static int
+cb_stateCheckHeader(struct callback_state_header * hdr)
+{
+    int ret = 0;
+
+    if (hdr->stamp.magic != CALLBACK_STATE_MAGIC) {
+       ret = 1;
+    } else if (hdr->stamp.version != CALLBACK_STATE_VERSION) {
+       ret = 1;
+    } else if ((hdr->nFEs > cbstuff.nblks) || (hdr->nCBs > cbstuff.nblks)) {
+       ViceLog(0, ("cb_stateCheckHeader: saved callback state larger than callback memory allocation\n"));
+       ret = 1;
+    }
+    return ret;
+}
+
+/* disk entry conversion routines */
+static int
+cb_stateFEToDiskEntry(struct FileEntry * in, struct FEDiskEntry * out)
+{
+    memcpy(&out->fe, in, sizeof(struct FileEntry));
+    out->index = fetoi(in);
+    return 0;
+}
+
+static int
+cb_stateDiskEntryToFE(struct fs_dump_state * state, 
+                     struct FEDiskEntry * in, struct FileEntry * out)
+{
+    int ret = 0;
+
+    memcpy(out, &in->fe, sizeof(struct FileEntry));
+
+    /* setup FE map entry */
+    if (!in->index || (in->index >= state->fe_map.len)) {
+       ViceLog(0, ("cb_stateDiskEntryToFE: index (%d) out of range",
+                   in->index));
+       ret = 1;
+       goto done;
+    }
+    state->fe_map.entries[in->index].old_idx = in->index;
+    state->fe_map.entries[in->index].new_idx = fetoi(out);
+
+ done:
+    return ret;
+}
+
+static int
+cb_stateCBToDiskEntry(struct CallBack * in, struct CBDiskEntry * out)
+{
+    memcpy(&out->cb, in, sizeof(struct CallBack));
+    out->index = cbtoi(in);
+    return 0;
+}
+
+static int
+cb_stateDiskEntryToCB(struct fs_dump_state * state,
+                     struct CBDiskEntry * in, struct CallBack * out)
+{
+    int ret = 0;
+
+    memcpy(out, &in->cb, sizeof(struct CallBack));
+
+    /* setup CB map entry */
+    if (!in->index || (in->index >= state->cb_map.len)) {
+       ViceLog(0, ("cb_stateDiskEntryToCB: index (%d) out of range\n",
+                   in->index));
+       ret = 1;
+       goto done;
+    }
+    state->cb_map.entries[in->index].old_idx = in->index;
+    state->cb_map.entries[in->index].new_idx = cbtoi(out);
+
+ done:
+    return ret;
+}
+
+/* index map routines */
+static int
+cb_stateAllocMap(struct fs_dump_state * state)
+{
+    state->fe_map.len = state->cb_hdr->fe_max + 1;
+    state->cb_map.len = state->cb_hdr->cb_max + 1;
+    state->fe_map.entries = (struct idx_map_entry_t *)
+       calloc(state->fe_map.len, sizeof(struct idx_map_entry_t));
+    state->cb_map.entries = (struct idx_map_entry_t *)
+       calloc(state->cb_map.len, sizeof(struct idx_map_entry_t));
+    return ((state->fe_map.entries != NULL) && (state->cb_map.entries != NULL)) ? 0 : 1;
+}
+
+int
+fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
+{
+    int ret = 0;
+
+    /* FEs use a one-based indexing system, so old==0 implies no mapping */
+    if (!old) {
+       *new = 0;
+       goto done;
+    }
+
+    if (old >= state->fe_map.len) {
+       ViceLog(0, ("fe_OldToNew: index %d is out of range\n", old));
+       ret = 1;
+    } else if (state->fe_map.entries[old].old_idx != old) { /* sanity check */
+       ViceLog(0, ("fe_OldToNew: index %d points to an invalid FileEntry record\n", old));
+       ret = 1;
+    } else {
+       *new = state->fe_map.entries[old].new_idx;
+    }
+
+ done:
+    return ret;
+}
+
+int
+cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
+{
+    int ret = 0;
+
+    /* CBs use a one-based indexing system, so old==0 implies no mapping */
+    if (!old) {
+       *new = 0;
+       goto done;
+    }
+
+    if (old >= state->cb_map.len) {
+       ViceLog(0, ("cb_OldToNew: index %d is out of range\n", old));
+       ret = 1;
+    } else if (state->cb_map.entries[old].old_idx != old) { /* sanity check */
+       ViceLog(0, ("cb_OldToNew: index %d points to an invalid CallBack record\n", old));
+       ret = 1;
+    } else {
+       *new = state->cb_map.entries[old].new_idx;
+    }
+
+ done:
+    return ret;
+}
+#endif /* AFS_DEMAND_ATTACH_FS */
+
+int
+DumpCallBackState(void)
+{
+    int fd, oflag;
+    afs_uint32 magic = MAGICV2, now = (afs_int32) FT_ApproxTime(), freelisthead;
+
+    oflag = O_WRONLY | O_CREAT | O_TRUNC;
+#ifdef AFS_NT40_ENV
+    oflag |= O_BINARY;
+#endif
+    fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, oflag, 0666);
+    if (fd < 0) {
+       ViceLog(0,
+               ("Couldn't create callback dump file %s\n",
+                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 */
+    close(fd);
+
+    return 0;
+}
+
+#endif /* !INTERPRET_DUMP */
+
+#ifdef INTERPRET_DUMP
+
+/* This is only compiled in for the callback analyzer program */
+/* Returns the time of the dump */
+time_t
+ReadDump(char *file, int timebits)
+{
+    int fd, oflag;
+    afs_uint32 magic, freelisthead;
+    afs_uint32 now;
+#ifdef AFS_64BIT_ENV
+    afs_int64 now64;
+#endif
+
+    oflag = O_RDONLY;
+#ifdef AFS_NT40_ENV
+    oflag |= O_BINARY;
+#endif
+    fd = open(file, oflag);
+    if (fd < 0) {
+       fprintf(stderr, "Couldn't read dump file %s\n", file);
+       exit(1);
+    }
+    read(fd, &magic, sizeof(magic));
+    if (magic == MAGICV2) {
+       timebits = 32;
+    } else {
+       if (magic != MAGIC) {
+           fprintf(stderr,
+                   "Magic number of %s is invalid.  You might be trying to\n",
+                   file);
+           fprintf(stderr,
+                   "run this program on a machine type with a different byte ordering.\n");
+           exit(1);
+       }
+    }
+#ifdef AFS_64BIT_ENV
+    if (timebits == 64) {
+       read(fd, &now64, sizeof(afs_int64));
+       now = (afs_int32) now64;
+    } else
+#endif
+       read(fd, &now, sizeof(afs_int32));
+    read(fd, &cbstuff, sizeof(cbstuff));
+    read(fd, TimeOuts, sizeof(TimeOuts));
+    read(fd, timeout, sizeof(timeout));
+    read(fd, &tfirst, sizeof(tfirst));
+    read(fd, &freelisthead, sizeof(freelisthead));
+    CB = ((struct CallBack
+          *)(calloc(cbstuff.nblks, sizeof(struct CallBack)))) - 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);
+    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 */
+    if (close(fd)) {
+       perror("Error reading dumpfile");
+       exit(1);
+    }
+    return now;
+}
+
+#ifdef AFS_NT40_ENV
+#include "AFS_component_version_number.h"
+#else
+#include "AFS_component_version_number.c"
+#endif
+
+int
+main(int argc, char **argv)
+{
+    int err = 0, cbi = 0, stats = 0, noptions = 0, all = 0, vol = 0, raw = 0;
+    static AFSFid fid;
+    register struct FileEntry *fe;
+    register struct CallBack *cb;
+    afs_int32 now;
+    int timebits = 32;
+    
+    memset(&fid, 0, sizeof(fid));
+    argc--;
+    argv++;
+    while (argc && **argv == '-') {
+       noptions++;
+       argc--;
+       if (!strcmp(*argv, "-host")) {
+           if (argc < 1) {
+               err++;
+               break;
+           }
+           argc--;
+           cbi = atoi(*++argv);
+       } else if (!strcmp(*argv, "-fid")) {
+           if (argc < 2) {
+               err++;
+               break;
+           }
+           argc -= 3;
+           fid.Volume = atoi(*++argv);
+           fid.Vnode = atoi(*++argv);
+           fid.Unique = atoi(*++argv);
+       } else if (!strcmp(*argv, "-time")) {
+           fprintf(stderr, "-time not supported\n");
+           exit(1);
+       } else if (!strcmp(*argv, "-stats")) {
+           stats = 1;
+       } else if (!strcmp(*argv, "-all")) {
+           all = 1;
+       } else if (!strcmp(*argv, "-raw")) {
+           raw = 1;
+       } else if (!strcmp(*argv, "-timebits")) {
+           if (argc < 1) {
+               err++;
+               break;
+           }
+           argc--;
+           timebits = atoi(*++argv);
+           if ((timebits != 32)
+#ifdef AFS_64BIT_ENV
+               && (timebits != 64)
+#endif
+               ) 
+               err++;
+       } else if (!strcmp(*argv, "-volume")) {
+           if (argc < 1) {
+               err++;
+               break;
+           }
+           argc--;
+           vol = atoi(*++argv);
+       } else
+           err++;
+       argv++;
+    }
     if (err || argc != 1) {
        fprintf(stderr,
-               "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] callbackdumpfile\n");
+               "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] [-timebits 32"
+#ifdef AFS_64BIT_ENV
+               "|64"
+#endif
+               "] callbackdumpfile\n");
        fprintf(stderr,
                "[cbid is shown for each host in the hosts.dump file]\n");
        exit(1);
     }
-    now = ReadDump(*argv);
+    now = ReadDump(*argv, timebits);
     if (stats || noptions == 0) {
-       time_t uxtfirst = UXtime(tfirst);
-       printf("The time of the dump was %u %s", now, ctime(&now));
-       printf("The last time cleanup ran was %u %s", uxtfirst,
+       time_t uxtfirst = UXtime(tfirst), tnow = now;
+       printf("The time of the dump was %u %s", (unsigned int) now, ctime(&tnow));
+       printf("The last time cleanup ran was %u %s", (unsigned int) uxtfirst,
               ctime(&uxtfirst));
        PrintCallBackStats();
     }
     if (all || vol) {
-       register hash;
-       register afs_uint32 *feip;
-       register struct CallBack *cb;
-       register struct FileEntry *fe;
+       int hash;
+       afs_uint32 *feip;
+       struct CallBack *cb;
+       struct FileEntry *fe;
 
-       for (hash = 0; hash < VHASH; hash++) {
-           for (feip = &HashTable[hash]; fe = itofe(*feip);) {
+       for (hash = 0; hash < FEHASH_SIZE; 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) {
@@ -1942,22 +2948,25 @@ 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];
            printf("%d:%12x%12x%12x%12x\n", i, p[0], p[1], p[2], p[3]);
        }
     }
+    exit(0);
 }
 
-int
+void
 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",
+    if (fe == NULL)
+       return;
+
+    printf("vol=%u vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %lu secs at %s",
           fe->volid, fe->vnode, fe->ncbs, cb->hhead, cb->status, fe->status,
           expires - now, ctime(&expires));
 }
@@ -1995,10 +3004,8 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
     if (!host->interface)
        return 1;               /* failure */
 
-    assert(host->interface->numberOfInterfaces > 0);
-
     /* the only address is the primary interface */
-    if (host->interface->numberOfInterfaces == 1)
+    if (host->interface->numberOfInterfaces <= 1)
        return 1;               /* failure */
 
     /* initialise a security object only once */
@@ -2032,8 +3039,8 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
 
     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)));
+           ("Starting multibreakcall back on all addr for host %p (%s:%d)\n",
+             host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
     H_UNLOCK;
     multi_Rx(conns, j) {
        multi_RXAFSCB_CallBack(afidp, &tc);
@@ -2043,14 +3050,17 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
            if (host->callback_rxcon)
                rx_DestroyConnection(host->callback_rxcon);
            host->callback_rxcon = conns[multi_i];
+            h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
            host->host = interfaces[multi_i].addr;
            host->port = interfaces[multi_i].port;
+            h_AddHostToAddrHashTable_r(host->host, host->port, host);
            connSuccess = conns[multi_i];
            rx_SetConnDeadTime(host->callback_rxcon, 50);
            rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
            ViceLog(125,
-                   ("multibreakcall success with addr %s\n",
-                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr)));
+                   ("multibreakcall success with addr %s:%d\n",
+                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
+                     ntohs(interfaces[multi_i].port)));
            H_UNLOCK;
            multi_Abort;
        }
@@ -2090,11 +3100,9 @@ MultiProbeAlternateAddress_r(struct host *host)
     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 */
+    if (host->interface->numberOfInterfaces <= 1)
+        return 1;               /* failure */
 
     /* initialise a security object only once */
     if (!sc)
@@ -2117,8 +3125,8 @@ MultiProbeAlternateAddress_r(struct host *host)
 
        interfaces[j] = host->interface->interface[i];
        conns[j] =
-           rx_NewConnection(interfaces[i].addr, 
-                            interfaces[i].port, 1, sc, 0);
+           rx_NewConnection(interfaces[j].addr, 
+                            interfaces[j].port, 1, sc, 0);
        rx_SetConnDeadTime(conns[j], 2);
        rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
        j++;
@@ -2126,8 +3134,9 @@ MultiProbeAlternateAddress_r(struct host *host)
 
     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)));
+           ("Starting multiprobe on all addr for host %p (%s:%d)\n",
+             host, afs_inet_ntoa_r(host->host, hoststr),
+             ntohs(host->port)));
     H_UNLOCK;
     multi_Rx(conns, j) {
        multi_RXAFSCB_ProbeUuid(&host->interface->uuid);
@@ -2137,20 +3146,24 @@ MultiProbeAlternateAddress_r(struct host *host)
            if (host->callback_rxcon)
                rx_DestroyConnection(host->callback_rxcon);
            host->callback_rxcon = conns[multi_i];
+            h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
            host->host = interfaces[multi_i].addr;
            host->port = interfaces[multi_i].port;
+            h_AddHostToAddrHashTable_r(host->host, host->port, host);
            connSuccess = conns[multi_i];
            rx_SetConnDeadTime(host->callback_rxcon, 50);
            rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
            ViceLog(125,
-                   ("multiprobe success with addr %s\n",
-                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr)));
+                   ("multiprobe success with addr %s:%d\n",
+                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
+                     ntohs(interfaces[multi_i].port)));
            H_UNLOCK;
            multi_Abort;
        } else {
            ViceLog(125,
-                   ("multiprobe failure with addr %s\n",
-                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr)));
+                   ("multiprobe failure with addr %s:%d\n",
+                    afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
+                     ntohs(interfaces[multi_i].port)));
             
             /* This is less than desirable but its the best we can do.
              * The AFS Cache Manager will return either 0 for a Uuid  
@@ -2160,21 +3173,21 @@ MultiProbeAlternateAddress_r(struct host *host)
              * 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--;
+                removeInterfaceAddr_r(host, interfaces[multi_i].addr, interfaces[multi_i].port);
                 H_UNLOCK;
             }
         }
+#ifdef AFS_DEMAND_ATTACH_FS
+       /* try to bail ASAP if the fileserver is shutting down */
+       FS_STATE_RDLOCK;
+       if (fs_state.mode == FS_MODE_SHUTDOWN) {
+           FS_STATE_UNLOCK;
+           multi_Abort;
+       }
+       FS_STATE_UNLOCK;
+#endif
     }
     multi_End_Ignore;
     H_LOCK;