2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
9 * Portions Copyright (c) 2006 Sine Nomine Associates
13 * NEW callback package callback.c (replaces vicecb.c)
14 * Updated call back routines, NOW with:
16 * Faster DeleteVenus (Now called DeleteAllCallBacks)
17 * Call back breaking for volumes
18 * Adaptive timeouts on call backs
19 * Architected for Multi RPC
20 * No locks (currently implicit vnode locks--these will go, to)
21 * Delayed call back when rpc connection down.
22 * Bulk break of delayed call backs when rpc connection
24 * Strict limit on number of call backs.
26 * InitCallBack(nblocks)
27 * Initialize: nblocks is max number # of file entries + # of callback entries
28 * nblocks must be < 65536
29 * Space used is nblocks*16 bytes
30 * Note that space will be reclaimed by breaking callbacks of old hosts
32 * time = AddCallBack(host, fid)
34 * Returns the expiration time at the workstation.
36 * BreakCallBack(host, fid)
37 * Break all call backs for fid, except for the specified host.
40 * BreakVolumeCallBacksLater(volume)
41 * Break all call backs on volume, using single call to each host
42 * Delete all the call backs.
44 * DeleteCallBack(host,fid)
45 * Delete (do not break) single call back for fid.
47 * DeleteFileCallBacks(fid)
48 * Delete (do not break) all call backs for fid.
50 * DeleteAllCallBacks(host)
51 * Delete (do not break) all call backs for host.
53 * CleanupTimedOutCallBacks()
54 * Delete all timed out call back entries
55 * Must be called periodically by file server.
57 * BreakDelayedCallBacks(host)
58 * Break all delayed call backs for host.
59 * Returns 1: one or more failed, 0: success.
61 * PrintCallBackStats()
62 * Print statistics about call backs to stdout.
64 * DumpCallBacks() ---wishful thinking---
65 * Dump call back state to /tmp/callback.state.
66 * This is separately interpretable by the program pcb.
68 * Notes: In general, if a call back to a host doesn't get through,
69 * then HostDown, supplied elsewhere, is called. BreakDelayedCallBacks,
70 * however, does not call HostDown, but instead returns an indication of
71 * success if all delayed call backs were finally broken.
73 * BreakDelayedCallBacks MUST be called at the first sign of activity
74 * from the host after HostDown has been called (or a previous
75 * BreakDelayedCallBacks failed). The BreakDelayedCallBacks must be
76 * allowed to complete before any requests from that host are handled.
77 * If BreakDelayedCallBacks fails, then the host should remain
78 * down (and the request should be failed).
80 * CleanupCallBacks MUST be called periodically by the file server for
81 * this package to work correctly. Every 5 minutes is suggested.
84 #include <afsconfig.h>
85 #include <afs/param.h>
90 #include <stdlib.h> /* for malloc() */
91 #include <time.h> /* ANSI standard location for time stuff */
101 #include <afs/afs_assert.h>
103 #include <afs/stds.h>
105 #include <afs/nfs.h> /* yuck. This is an abomination. */
108 #include <afs/afscbint.h>
109 #include <afs/afsutil.h>
111 #include <afs/ihandle.h>
112 #include <afs/vnode.h>
113 #include <afs/volume.h>
114 #include "viced_prototypes.h"
117 #include <afs/ptclient.h> /* need definition of prlist for host.h */
119 #include "callback.h"
120 #ifdef AFS_DEMAND_ATTACH_FS
121 #include "../tviced/serialize_state.h"
122 #endif /* AFS_DEMAND_ATTACH_FS */
125 extern afsUUID FS_HostUUID;
126 extern int hostCount;
128 #ifndef INTERPRET_DUMP
129 static int ShowProblems = 1;
132 struct cbcounters cbstuff;
134 static struct FileEntry * FE = NULL; /* don't use FE[0] */
135 static struct CallBack * CB = NULL; /* don't use CB[0] */
137 static struct CallBack * CBfree = NULL;
138 static struct FileEntry * FEfree = NULL;
141 /* Time to live for call backs depends upon number of users of the file.
142 * TimeOuts is indexed by this number/8 (using TimeOut macro). Times
143 * in this table are for the workstation; server timeouts, add
146 static int TimeOuts[] = {
147 /* Note: don't make the first entry larger than 4 hours (see above) */
148 4 * 60 * 60, /* 0-7 users */
149 1 * 60 * 60, /* 8-15 users */
150 30 * 60, /* 16-23 users */
151 15 * 60, /* 24-31 users */
152 15 * 60, /* 32-39 users */
153 10 * 60, /* 40-47 users */
154 10 * 60, /* 48-55 users */
155 10 * 60, /* 56-63 users */
156 }; /* Anything more: MinTimeOut */
158 /* minimum time given for a call back */
159 #ifndef INTERPRET_DUMP
160 static int MinTimeOut = (7 * 60);
163 /* Heads of CB queues; a timeout index is 1+index into this array */
164 static afs_uint32 timeout[CB_NUM_TIMEOUT_QUEUES];
166 static afs_int32 tfirst; /* cbtime of oldest unexpired call back time queue */
169 /* 16 byte object get/free routines */
174 /* Prototypes for static routines */
175 static struct FileEntry *FindFE(AFSFid * fid);
177 #ifndef INTERPRET_DUMP
178 static struct CallBack *iGetCB(int *nused);
179 static int iFreeCB(struct CallBack *cb, int *nused);
180 static struct FileEntry *iGetFE(int *nused);
181 static int iFreeFE(struct FileEntry *fe, int *nused);
182 static int TAdd(struct CallBack *cb, afs_uint32 * thead);
183 static int TDel(struct CallBack *cb);
184 static int HAdd(struct CallBack *cb, struct host *host);
185 static int HDel(struct CallBack *cb);
186 static int CDel(struct CallBack *cb, int deletefe);
187 static int CDelPtr(struct FileEntry *fe, afs_uint32 * cbp,
189 static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host);
190 static int FDel(struct FileEntry *fe);
191 static int AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead,
192 int type, int locked);
193 static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
194 struct AFSCBFids *afidp, struct host *xhost);
195 static int MultiBreakVolumeCallBack_r(struct host *host, int isheld,
196 struct VCBParams *parms, int deletefe);
197 static int MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
199 static int GetSomeSpace_r(struct host *hostp, int locked);
200 static int ClearHostCallbacks_r(struct host *hp, int locked);
201 static int DumpCallBackState_r(void);
204 #define GetCB() ((struct CallBack *)iGetCB(&cbstuff.nCBs))
205 #define GetFE() ((struct FileEntry *)iGetFE(&cbstuff.nFEs))
206 #define FreeCB(cb) iFreeCB((struct CallBack *)cb, &cbstuff.nCBs)
207 #define FreeFE(fe) iFreeFE((struct FileEntry *)fe, &cbstuff.nFEs)
210 /* Other protos - move out sometime */
211 void PrintCB(struct CallBack *cb, afs_uint32 now);
213 static afs_uint32 HashTable[FEHASH_SIZE]; /* File entry hash table */
215 static struct FileEntry *
220 struct FileEntry *fe;
222 hash = FEHash(fid->Volume, fid->Unique);
223 for (fei = HashTable[hash]; fei; fei = fe->fnext) {
225 if (fe->volid == fid->Volume && fe->unique == fid->Unique
226 && fe->vnode == fid->Vnode && (fe->status & FE_LATER) != FE_LATER)
232 #ifndef INTERPRET_DUMP
234 static struct CallBack *
237 struct CallBack *ret;
239 if ((ret = CBfree)) {
240 CBfree = (struct CallBack *)(((struct object *)ret)->next);
247 iFreeCB(struct CallBack *cb, int *nused)
249 ((struct object *)cb)->next = (struct object *)CBfree;
255 static struct FileEntry *
258 struct FileEntry *ret;
260 if ((ret = FEfree)) {
261 FEfree = (struct FileEntry *)(((struct object *)ret)->next);
268 iFreeFE(struct FileEntry *fe, int *nused)
270 ((struct object *)fe)->next = (struct object *)FEfree;
276 /* Add cb to end of specified timeout list */
278 TAdd(struct CallBack *cb, afs_uint32 * thead)
281 (*thead) = cb->tnext = cb->tprev = cbtoi(cb);
283 struct CallBack *thp = itocb(*thead);
285 cb->tprev = thp->tprev;
289 thp->tprev = (itocb(thp->tprev)->tnext = cbtoi(cb));
291 thp->tprev = cbtoi(cb);
294 cb->thead = ttoi(thead);
298 /* Delete call back entry from timeout list */
300 TDel(struct CallBack *cb)
302 afs_uint32 *thead = itot(cb->thead);
304 if (*thead == cbtoi(cb))
305 *thead = (*thead == cb->tnext ? 0 : cb->tnext);
306 if (itocb(cb->tprev))
307 itocb(cb->tprev)->tnext = cb->tnext;
308 if (itocb(cb->tnext))
309 itocb(cb->tnext)->tprev = cb->tprev;
313 /* Add cb to end of specified host list */
315 HAdd(struct CallBack *cb, struct host *host)
317 cb->hhead = h_htoi(host);
319 host->cblist = cb->hnext = cb->hprev = cbtoi(cb);
321 struct CallBack *fcb = itocb(host->cblist);
323 cb->hprev = fcb->hprev;
324 cb->hnext = cbtoi(fcb);
325 fcb->hprev = (itocb(fcb->hprev)->hnext = cbtoi(cb));
330 /* Delete call back entry from host list */
332 HDel(struct CallBack *cb)
334 afs_uint32 *hhead = &h_itoh(cb->hhead)->cblist;
336 if (*hhead == cbtoi(cb))
337 *hhead = (*hhead == cb->hnext ? 0 : cb->hnext);
338 itocb(cb->hprev)->hnext = cb->hnext;
339 itocb(cb->hnext)->hprev = cb->hprev;
343 /* Delete call back entry from fid's chain of cb's */
344 /* N.B. This one also deletes the CB, and also possibly parent FE, so
345 * make sure that it is not on any other list before calling this
348 CDel(struct CallBack *cb, int deletefe)
351 struct FileEntry *fe = itofe(cb->fhead);
355 for (safety = 0, cbp = &fe->firstcb; *cbp && *cbp != cbi;
356 cbp = &itocb(*cbp)->cnext, safety++) {
357 if (safety > cbstuff.nblks + 10) {
358 osi_Panic("CDel: Internal Error -- shutting down: wanted %d from %d, now at %d\n",
359 cbi, fe->firstcb, *cbp);
361 ("CDel: Internal Error -- shutting down: wanted %d from %d, now at %d\n",
362 cbi, fe->firstcb, *cbp));
363 DumpCallBackState_r();
364 ShutDownAndCore(PANIC);
367 CDelPtr(fe, cbp, deletefe);
371 /* Same as CDel, but pointer to parent pointer to CB entry is passed,
372 * as well as file entry */
373 /* N.B. This one also deletes the CB, and also possibly parent FE, so
374 * make sure that it is not on any other list before calling this
376 static int Ccdelpt = 0, CcdelB = 0;
379 CDelPtr(struct FileEntry *fe, afs_uint32 * cbp,
392 if ((--fe->ncbs == 0) && deletefe)
398 FindCBPtr(struct FileEntry *fe, struct host *host)
400 afs_uint32 hostindex = h_htoi(host);
405 for (safety = 0, cbp = &fe->firstcb; *cbp; cbp = &cb->cnext, safety++) {
406 if (safety > cbstuff.nblks) {
407 ViceLog(0, ("FindCBPtr: Internal Error -- shutting down.\n"));
408 DumpCallBackState_r();
409 ShutDownAndCore(PANIC);
412 if (cb->hhead == hostindex)
418 /* Delete file entry from hash table */
420 FDel(struct FileEntry *fe)
423 afs_uint32 *p = &HashTable[FEHash(fe->volid, fe->unique)];
425 while (*p && *p != fei)
426 p = &itofe(*p)->fnext;
433 /* initialize the callback package */
435 InitCallBack(int nblks)
438 tfirst = CBtime(FT_ApproxTime());
439 /* N.B. The "-1", below, is because
440 * FE[0] and CB[0] are not used--and not allocated */
441 FE = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry))));
443 ViceLog(0, ("Failed malloc in InitCallBack\n"));
444 osi_Panic("Failed malloc in InitCallBack\n");
446 FE--; /* FE[0] is supposed to point to junk */
447 cbstuff.nFEs = nblks;
449 FreeFE(&FE[cbstuff.nFEs]); /* This is correct */
450 CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))));
452 ViceLog(0, ("Failed malloc in InitCallBack\n"));
453 osi_Panic("Failed malloc in InitCallBack\n");
455 CB--; /* CB[0] is supposed to point to junk */
456 cbstuff.nCBs = nblks;
458 FreeCB(&CB[cbstuff.nCBs]); /* This is correct */
459 cbstuff.nblks = nblks;
460 cbstuff.nbreakers = 0;
466 XCallBackBulk_r(struct host * ahost, struct AFSFid * fids, afs_int32 nfids)
468 struct AFSCallBack tcbs[AFSCBMAX];
474 struct rx_connection *cb_conn = NULL;
477 rx_SetConnDeadTime(ahost->callback_rxcon, 4);
478 rx_SetConnHardDeadTime(ahost->callback_rxcon, AFS_HARDDEADTIME);
485 for (i = 0; i < nfids && i < AFSCBMAX; i++) {
486 tcbs[i].CallBackVersion = CALLBACK_VERSION;
487 tcbs[i].ExpirationTime = 0;
488 tcbs[i].CallBackType = CB_DROPPED;
490 tf.AFSCBFids_len = i;
491 tf.AFSCBFids_val = &(fids[j]);
495 tc.AFSCBs_val = tcbs;
497 cb_conn = ahost->callback_rxcon;
498 rx_GetConnection(cb_conn);
500 code |= RXAFSCB_CallBack(cb_conn, &tf, &tc);
501 rx_PutConnection(cb_conn);
509 /* the locked flag tells us if the host entry has already been locked
510 * by our parent. I don't think anybody actually calls us with the
511 * host locked, but here's how to make that work: GetSomeSpace has to
512 * change so that it doesn't attempt to lock any hosts < "host". That
513 * means that it might be unable to free any objects, so it has to
514 * return an exit status. If it fails, then AddCallBack1 might fail,
515 * as well. If so, the host->ResetDone should probably be set to 0,
516 * and we probably don't want to return a callback promise to the
517 * cache manager, either. */
519 AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
527 if (!(host->hostFlags & HOSTDELETED))
528 retVal = AddCallBack1_r(host, fid, thead, type, 1);
538 AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
541 struct FileEntry *fe;
542 struct CallBack *cb = 0, *lastcb = 0;
543 struct FileEntry *newfe = 0;
544 afs_uint32 time_out = 0;
545 afs_uint32 *Thead = thead;
546 struct CallBack *newcb = 0;
549 cbstuff.AddCallBacks++;
553 /* allocate these guys first, since we can't call the allocator with
554 * the host structure locked -- or we might deadlock. However, we have
555 * to avoid races with FindFE... */
556 while (!(newcb = GetCB())) {
557 GetSomeSpace_r(host, locked);
559 while (!(newfe = GetFE())) { /* Get it now, so we don't have to call */
560 /* GetSomeSpace with the host locked, later. This might turn out to */
561 /* have been unneccessary, but that's actually kind of unlikely, since */
562 /* most files are not shared. */
563 GetSomeSpace_r(host, locked);
567 h_Lock_r(host); /* this can yield, so do it before we get any */
569 if (host->hostFlags & HOSTDELETED) {
577 if (type == CB_NORMAL) {
579 TimeCeiling(FT_ApproxTime() + TimeOut(fe ? fe->ncbs : 0) +
581 Thead = THead(CBtime(time_out));
582 } else if (type == CB_VOLUME) {
583 time_out = TimeCeiling((60 * 120 + FT_ApproxTime()) + ServerBias);
584 Thead = THead(CBtime(time_out));
585 } else if (type == CB_BULK) {
586 /* bulk status can get so many callbacks all at once, and most of them
587 * are probably not for things that will be used for long.
590 TimeCeiling(FT_ApproxTime() + ServerBias +
591 TimeOut(22 + (fe ? fe->ncbs : 0)));
592 Thead = THead(CBtime(time_out));
603 fe->volid = fid->Volume;
604 fe->vnode = fid->Vnode;
605 fe->unique = fid->Unique;
608 hash = FEHash(fid->Volume, fid->Unique);
609 fe->fnext = HashTable[hash];
610 HashTable[hash] = fetoi(fe);
612 for (safety = 0, lastcb = cb = itocb(fe->firstcb); cb;
613 lastcb = cb, cb = itocb(cb->cnext), safety++) {
614 if (safety > cbstuff.nblks) {
615 ViceLog(0, ("AddCallBack1: Internal Error -- shutting down.\n"));
616 DumpCallBackState_r();
617 ShutDownAndCore(PANIC);
619 if (cb->hhead == h_htoi(host))
622 if (cb) { /* Already have call back: move to new timeout list */
623 /* don't change delayed callbacks back to normal ones */
624 if (cb->status != CB_DELAYED)
626 /* Only move if new timeout is longer */
627 if (TNorm(ttoi(Thead)) > TNorm(cb->thead)) {
631 if (newfe == NULL) { /* we are using the new FE */
632 fe->firstcb = cbtoi(cb);
634 cb->fhead = fetoi(fe);
639 *(lastcb ? &lastcb->cnext : &fe->firstcb) = cbtoi(cb);
642 cb->fhead = fetoi(fe);
648 /* now free any still-unused callback or host entries */
654 if (!locked) /* freecb and freefe might(?) yield */
657 if (type == CB_NORMAL || type == CB_VOLUME || type == CB_BULK)
658 return time_out - ServerBias; /* Expires sooner at workstation */
664 CompareCBA(const void *e1, const void *e2)
666 const struct cbstruct *cba1 = (const struct cbstruct *)e1;
667 const struct cbstruct *cba2 = (const struct cbstruct *)e2;
668 return ((cba1->hp)->index - (cba2->hp)->index);
671 /* Take an array full of hosts, all held. Break callbacks to them, and
672 * release the holds once you're done, except don't release xhost. xhost
673 * may be NULL. Currently only works for a single Fid in afidp array.
674 * If you want to make this work with multiple fids, you need to fix
675 * the error handling. One approach would be to force a reset if a
676 * multi-fid call fails, or you could add delayed callbacks for each
677 * fid. You probably also need to sort and remove duplicate hosts.
678 * When this is called from the BreakVolumeCallBacks path, it does NOT
679 * force a reset if the RPC fails, it just marks the host down and tries
680 * to create a delayed callback. */
681 /* N.B. be sure that code works when ncbas == 0 */
682 /* N.B. requires all the cba[*].hp pointers to be valid... */
683 /* This routine does not hold a lock on the host for the duration of
684 * the BreakCallBack RPC, which is a significant deviation from tradition.
685 * It _does_ get a lock on the host before setting VenusDown = 1,
686 * which is sufficient only if VenusDown = 0 only happens when the
687 * lock is held over the RPC and the subsequent VenusDown == 0
688 * wherever that is done. */
690 MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
691 struct AFSCBFids *afidp, struct host *xhost)
694 struct rx_connection *conns[MAX_CB_HOSTS];
695 static struct AFSCBs tc = { 0, 0 };
696 int multi_to_cba_map[MAX_CB_HOSTS];
698 osi_Assert(ncbas <= MAX_CB_HOSTS);
700 /* sort cba list to avoid makecall issues */
701 qsort(cba, ncbas, sizeof(struct cbstruct), CompareCBA);
703 /* set up conns for multi-call */
704 for (i = 0, j = 0; i < ncbas; i++) {
705 struct host *thishost = cba[i].hp;
706 if (!thishost || (thishost->hostFlags & HOSTDELETED)) {
709 rx_GetConnection(thishost->callback_rxcon);
710 multi_to_cba_map[j] = i;
711 conns[j++] = thishost->callback_rxcon;
714 rx_SetConnDeadTime(thishost->callback_rxcon, 4);
715 rx_SetConnHardDeadTime(thishost->callback_rxcon, AFS_HARDDEADTIME);
719 if (j) { /* who knows what multi would do with 0 conns? */
723 multi_RXAFSCB_CallBack(afidp, &tc);
729 i = multi_to_cba_map[multi_i];
735 ("BCB: INTERNAL ERROR: hp=%p, cba=%p, thead=%u\n",
739 ** try breaking callbacks on alternate interface addresses
741 if (MultiBreakCallBackAlternateAddress(hp, afidp)) {
744 ("BCB: Failed on file %u.%u.%u, "
745 "Host %p (%s:%d) is down\n",
746 afidp->AFSCBFids_val->Volume,
747 afidp->AFSCBFids_val->Vnode,
748 afidp->AFSCBFids_val->Unique,
750 afs_inet_ntoa_r(hp->host, hoststr),
756 if (!(hp->hostFlags & HOSTDELETED)) {
757 hp->hostFlags |= VENUSDOWN;
759 * We always go into AddCallBack1_r with the host locked
761 AddCallBack1_r(hp, afidp->AFSCBFids_val, itot(idx),
775 for (i = 0; i < ncbas; i++) {
778 if (hp && xhost != hp) {
783 /* H_UNLOCK around this so h_FreeConnection does not deadlock.
784 h_FreeConnection should *never* be called on a callback connection,
785 but on 10/27/04 a deadlock occurred where it was, when we know why,
786 this should be reverted. -- shadow */
788 for (i = 0; i < j; i++) {
789 rx_PutConnection(conns[i]);
797 * Break all call backs for fid, except for the specified host (unless flag
798 * is true, in which case all get a callback message. Assumption: the specified
799 * host is h_Held, by the caller; the others aren't.
800 * Specified host may be bogus, that's ok. This used to check to see if the
801 * host was down in two places, once right after the host was h_held, and
802 * again after it was locked. That race condition is incredibly rare and
803 * relatively harmless even when it does occur, so we don't check for it now.
805 /* if flag is true, send a break callback msg to "host", too */
807 BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
809 struct FileEntry *fe;
810 struct CallBack *cb, *nextcb;
811 struct cbstruct cba[MAX_CB_HOSTS];
818 ("BCB: BreakCallBack(Host %p all but %s:%d, (%u,%u,%u))\n",
819 xhost, afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
820 fid->Volume, fid->Vnode, fid->Unique));
823 cbstuff.BreakCallBacks++;
828 hostindex = h_htoi(xhost);
829 cb = itocb(fe->firstcb);
830 if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
831 /* the most common case is what follows the || */
834 tf.AFSCBFids_len = 1;
835 tf.AFSCBFids_val = fid;
838 for (ncbas = 0; cb && ncbas < MAX_CB_HOSTS; cb = nextcb) {
839 nextcb = itocb(cb->cnext);
840 if ((cb->hhead != hostindex || flag)
841 && (cb->status == CB_BULK || cb->status == CB_NORMAL
842 || cb->status == CB_VOLUME)) {
843 struct host *thishost = h_itoh(cb->hhead);
845 ViceLog(0, ("BCB: BOGUS! cb->hhead is NULL!\n"));
846 } else if (thishost->hostFlags & VENUSDOWN) {
848 ("BCB: %p (%s:%d) is down; delaying break call back\n",
849 thishost, afs_inet_ntoa_r(thishost->host, hoststr),
850 ntohs(thishost->port)));
851 cb->status = CB_DELAYED;
853 if (!(thishost->hostFlags & HOSTDELETED)) {
855 cba[ncbas].hp = thishost;
856 cba[ncbas].thead = cb->thead;
861 CDel(cb, 1); /* Usually first; so this delete
862 * is reasonably inexpensive */
868 MultiBreakCallBack_r(cba, ncbas, &tf, xhost);
870 /* we need to to all these initializations again because MultiBreakCallBack may block */
875 cb = itocb(fe->firstcb);
876 if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
877 /* the most common case is what follows the || */
888 /* Delete (do not break) single call back for fid */
890 DeleteCallBack(struct host *host, AFSFid * fid)
892 struct FileEntry *fe;
897 cbstuff.DeleteCallBacks++;
900 /* do not care if the host has been HOSTDELETED */
906 ("DCB: No call backs for fid (%u, %u, %u)\n", fid->Volume,
907 fid->Vnode, fid->Unique));
910 pcb = FindCBPtr(fe, host);
913 ("DCB: No call back for host %p (%s:%d), (%u, %u, %u)\n",
914 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
915 fid->Volume, fid->Vnode, fid->Unique));
929 * Delete (do not break) all call backs for fid. This call doesn't
930 * set all of the various host locks, but it shouldn't really matter
931 * since we're not adding callbacks, but deleting them. I'm not sure
932 * why it doesn't set the lock, however; perhaps it should.
935 DeleteFileCallBacks(AFSFid * fid)
937 struct FileEntry *fe;
943 cbstuff.DeleteFiles++;
948 ("DF: No fid (%u,%u,%u) to delete\n", fid->Volume, fid->Vnode,
952 for (n = 0, cbi = fe->firstcb; cbi; n++) {
965 /* Delete (do not break) all call backs for host. The host should be
968 DeleteAllCallBacks_r(struct host *host, int deletefe)
973 cbstuff.DeleteAllCallBacks++;
974 cbi = first = host->cblist;
976 ViceLog(8, ("DV: no call backs\n"));
984 } while (cbi != first);
990 * Break all delayed call backs for host. Returns 1 if all call backs
991 * successfully broken; 0 otherwise. Assumes host is h_Held and h_Locked.
992 * Must be called with VenusDown set for this host
995 BreakDelayedCallBacks(struct host *host)
999 retVal = BreakDelayedCallBacks_r(host);
1005 BreakDelayedCallBacks_r(struct host *host)
1007 struct AFSFid fids[AFSCBMAX];
1008 int cbi, first, nfids;
1009 struct CallBack *cb;
1012 struct rx_connection *cb_conn;
1014 cbstuff.nbreakers++;
1015 if (!(host->hostFlags & RESETDONE) && !(host->hostFlags & HOSTDELETED)) {
1016 host->hostFlags &= ~ALTADDR; /* alternate addresses are invalid */
1017 cb_conn = host->callback_rxcon;
1018 rx_GetConnection(cb_conn);
1019 if (host->interface) {
1022 RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
1025 code = RXAFSCB_InitCallBackState(cb_conn);
1027 rx_PutConnection(cb_conn);
1030 host->hostFlags |= ALTADDR; /* alternate addresses are valid */
1034 ("CB: Call back connect back failed (in break delayed) "
1035 "for Host %p (%s:%d)\n",
1036 host, afs_inet_ntoa_r(host->host, hoststr),
1037 ntohs(host->port)));
1039 host->hostFlags |= VENUSDOWN;
1042 ("InitCallBackState success on %p (%s:%d)\n",
1043 host, afs_inet_ntoa_r(host->host, hoststr),
1044 ntohs(host->port)));
1045 /* reset was done successfully */
1046 host->hostFlags |= RESETDONE;
1047 host->hostFlags &= ~VENUSDOWN;
1050 while (!(host->hostFlags & HOSTDELETED)) {
1052 host->hostFlags &= ~VENUSDOWN; /* presume up */
1053 cbi = first = host->cblist;
1057 first = host->cblist;
1060 if (cb->status == CB_DELAYED) {
1061 struct FileEntry *fe = itofe(cb->fhead);
1062 fids[nfids].Volume = fe->volid;
1063 fids[nfids].Vnode = fe->vnode;
1064 fids[nfids].Unique = fe->unique;
1070 } while (cbi && cbi != first && nfids < AFSCBMAX);
1076 if (XCallBackBulk_r(host, fids, nfids)) {
1077 /* Failed, again: put them back, probably with old
1082 ("CB: XCallBackBulk failed, Host %p (%s:%d); "
1083 "callback list follows:\n",
1084 host, afs_inet_ntoa_r(host->host, hoststr),
1085 ntohs(host->port)));
1087 for (i = 0; i < nfids; i++) {
1090 ("CB: Host %p (%s:%d), file %u.%u.%u "
1091 "(part of bulk callback)\n",
1092 host, afs_inet_ntoa_r(host->host, hoststr),
1093 ntohs(host->port), fids[i].Volume,
1094 fids[i].Vnode, fids[i].Unique));
1097 * AddCallBack1_r(host, &fids[i], itot(thead[i]), CB_DELAYED, 1);
1098 * * but it turns out to cause too many tricky locking problems.
1099 * * now, if break delayed fails, screw it. */
1101 host->hostFlags |= VENUSDOWN; /* Failed */
1102 ClearHostCallbacks_r(host, 1 /* locked */ );
1106 if (nfids < AFSCBMAX)
1110 cbstuff.nbreakers--;
1111 /* If we succeeded it's always ok to unset HFE_LATER */
1112 if (!(host->hostFlags & VENUSDOWN))
1113 host->hostFlags &= ~HFE_LATER;
1114 return (host->hostFlags & VENUSDOWN);
1118 ** isheld is 0 if the host is held in h_Enumerate
1119 ** isheld is 1 if the host is held in BreakVolumeCallBacks
1122 MultiBreakVolumeCallBack_r(struct host *host, int isheld,
1123 struct VCBParams *parms, int deletefe)
1127 if (host->hostFlags & HOSTDELETED)
1128 return 0; /* host is deleted, release hold */
1130 if (!(host->hostFlags & HCBREAK))
1131 return 0; /* host is not flagged to notify */
1133 if (host->hostFlags & VENUSDOWN) {
1135 /* Do not care if the host is now HOSTDELETED */
1138 ("BVCB: volume callback for Host %p (%s:%d) failed\n",
1139 host, afs_inet_ntoa_r(host->host, hoststr),
1140 ntohs(host->port)));
1142 DeleteAllCallBacks_r(host, deletefe); /* Delete all callback state
1143 * rather than attempting to
1144 * selectively remember to
1145 * delete the volume callbacks
1147 host->hostFlags &= ~(RESETDONE|HCBREAK); /* Do InitCallBackState when host returns */
1149 return 0; /* parent will release hold */
1151 osi_Assert(parms->ncbas <= MAX_CB_HOSTS);
1153 /* Do not call MultiBreakCallBack on the current host structure
1154 ** because it would prematurely release the hold on the host
1156 if (parms->ncbas == MAX_CB_HOSTS) {
1157 struct AFSCBFids tf;
1159 tf.AFSCBFids_len = 1;
1160 tf.AFSCBFids_val = parms->fid;
1162 /* this releases all the hosts */
1163 MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf, 0 /* xhost */ );
1167 parms->cba[parms->ncbas].hp = host;
1168 parms->cba[(parms->ncbas)++].thead = parms->thead;
1169 host->hostFlags &= ~HCBREAK;
1170 return 1; /* parent shouldn't release hold, more work to do */
1174 ** isheld is 0 if the host is held in h_Enumerate
1175 ** isheld is 1 if the host is held in BreakVolumeCallBacks
1178 MultiBreakVolumeLaterCallBack(struct host *host, int isheld, void *rock)
1180 struct VCBParams *parms = (struct VCBParams *)rock;
1183 retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 0);
1189 * Break all call backs on a single volume. Don't call this with any
1190 * hosts h_held. Note that this routine clears the callbacks before
1191 * actually breaking them, and that the vnode isn't locked during this
1192 * operation, so that people might see temporary callback loss while
1193 * this function is executing. It is just a temporary state, however,
1194 * since the callback will be broken later by this same function.
1196 * Now uses multi-RX for CallBack RPC in a different thread,
1197 * only marking them here.
1199 #ifdef AFS_PTHREAD_ENV
1200 extern pthread_cond_t fsync_cond;
1202 extern char fsync_wait[];
1206 BreakVolumeCallBacksLater(afs_uint32 volume)
1210 struct FileEntry *fe;
1211 struct CallBack *cb;
1215 ViceLog(25, ("Setting later on volume %u\n", volume));
1217 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1218 for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
1219 if (fe->volid == volume) {
1220 struct CallBack *cbnext;
1221 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1222 host = h_itoh(cb->hhead);
1223 host->hostFlags |= HFE_LATER;
1224 cb->status = CB_DELAYED;
1225 cbnext = itocb(cb->cnext);
1228 fe->status |= FE_LATER;
1237 /* didn't find any callbacks, so return right away. */
1241 ViceLog(25, ("Fsync thread wakeup\n"));
1242 #ifdef AFS_PTHREAD_ENV
1244 CV_BROADCAST(&fsync_cond);
1247 LWP_NoYieldSignal(fsync_wait);
1253 BreakLaterCallBacks(void)
1258 struct CallBack *cb;
1259 struct FileEntry *fe = NULL;
1260 struct FileEntry *myfe = NULL;
1262 struct VCBParams henumParms;
1263 unsigned short tthead = 0; /* zero is illegal value */
1267 ViceLog(25, ("Looking for FileEntries to unchain\n"));
1270 /* Pick the first volume we see to clean up */
1271 fid.Volume = fid.Vnode = fid.Unique = 0;
1273 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1274 for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
1275 if (fe && (fe->status & FE_LATER)
1276 && (fid.Volume == 0 || fid.Volume == fe->volid)) {
1277 /* Ugly, but used to avoid left side casting */
1278 struct object *tmpfe;
1280 ("Unchaining for %u:%u:%u\n", fe->vnode, fe->unique,
1282 fid.Volume = fe->volid;
1284 fe->status &= ~FE_LATER; /* not strictly needed */
1285 /* Works since volid is deeper than the largest pointer */
1286 tmpfe = (struct object *)fe;
1287 tmpfe->next = (struct object *)myfe;
1300 /* loop over FEs from myfe and free/break */
1302 for (fe = myfe; fe;) {
1303 struct CallBack *cbnext;
1304 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1305 cbnext = itocb(cb->cnext);
1306 host = h_itoh(cb->hhead);
1307 if (cb->status == CB_DELAYED) {
1308 if (!(host->hostFlags & HOSTDELETED)) {
1309 /* mark this host for notification */
1310 host->hostFlags |= HCBREAK;
1311 if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
1317 CDel(cb, 0); /* Don't let CDel clean up the fe */
1318 /* leave flag for MultiBreakVolumeCallBack to clear */
1321 ("Found host %p (%s:%d) non-DELAYED cb for %u:%u:%u\n",
1322 host, afs_inet_ntoa_r(host->host, hoststr),
1323 ntohs(host->port), fe->vnode, fe->unique, fe->volid));
1327 fe = (struct FileEntry *)((struct object *)fe)->next;
1332 ViceLog(125, ("Breaking volume %u\n", fid.Volume));
1333 henumParms.ncbas = 0;
1334 henumParms.fid = &fid;
1335 henumParms.thead = tthead;
1337 h_Enumerate(MultiBreakVolumeLaterCallBack, (char *)&henumParms);
1339 if (henumParms.ncbas) { /* do left-overs */
1340 struct AFSCBFids tf;
1341 tf.AFSCBFids_len = 1;
1342 tf.AFSCBFids_val = &fid;
1344 MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf, 0);
1345 henumParms.ncbas = 0;
1350 /* Arrange to be called again */
1355 * Delete all timed-out call back entries (to be called periodically by file
1359 CleanupTimedOutCallBacks(void)
1362 CleanupTimedOutCallBacks_r();
1368 CleanupTimedOutCallBacks_r(void)
1370 afs_uint32 now = CBtime(FT_ApproxTime());
1372 struct CallBack *cb;
1376 while (tfirst <= now) {
1378 cbi = *(thead = THead(tfirst));
1384 ("CCB: deleting timed out call back %x (%s:%d), (%u,%u,%u)\n",
1385 h_itoh(cb->hhead)->host,
1386 afs_inet_ntoa_r(h_itoh(cb->hhead)->host, hoststr),
1387 h_itoh(cb->hhead)->port, itofe(cb->fhead)->volid,
1388 itofe(cb->fhead)->vnode, itofe(cb->fhead)->unique));
1392 if (ntimedout > cbstuff.nblks) {
1393 ViceLog(0, ("CCB: Internal Error -- shutting down...\n"));
1394 DumpCallBackState_r();
1395 ShutDownAndCore(PANIC);
1397 } while (cbi != *thead);
1402 cbstuff.CBsTimedOut += ntimedout;
1403 ViceLog(7, ("CCB: deleted %d timed out callbacks\n", ntimedout));
1404 return (ntimedout > 0);
1408 * parameters to pass to lih*_r from h_Enumerate_r when trying to find a host
1409 * from which to clear callbacks.
1413 * Points to the least interesting host found; try to clear callbacks on
1414 * this host after h_Enumerate_r(lih*_r)'ing.
1419 * The last host we got from lih*_r, but we couldn't clear its callbacks
1420 * for some reason. Choose the next-best host after this one (with the
1421 * current lih*_r, this means to only select hosts that have an ActiveCall
1422 * newer than lastlih).
1424 struct host *lastlih;
1427 /* Value of host->refCount that allows us to reliably infer that
1428 * host may be held by some other thread */
1429 #define OTHER_MUSTHOLD_LIH 2
1431 /* This version does not allow 'host' to be selected unless its ActiveCall
1432 * is newer than 'params->lastlih' which is the host with the oldest
1433 * ActiveCall from the last pass (if it is provided). We filter out any hosts
1434 * that are are held by other threads.
1436 * There is a small problem here, but it may not be easily fixable. Say we
1437 * select some host A, and give it back to GetSomeSpace_r. GSS_r for some
1438 * reason cannot clear the callbacks on A, and so calls us again with
1439 * lastlih = A. Suppose there is another host B that has the same ActiveCall
1440 * time as A. We will now skip over host B, since
1441 * 'hostB->ActiveCall > hostA->ActiveCall' is not true. This could result in
1442 * us prematurely going to the GSS_r 2nd or 3rd pass, and making us a little
1443 * inefficient. This should be pretty rare, though, except perhaps in cases
1444 * with very small numbers of hosts.
1446 * Also filter out any hosts with HOSTDELETED set. h_Enumerate_r should in
1447 * theory not give these to us anyway, but be paranoid.
1450 lih0_r(struct host *host, int flags, void *rock)
1452 struct lih_params *params = (struct lih_params *)rock;
1454 /* OTHER_MUSTHOLD_LIH is because the h_Enum loop holds us once */
1456 && (!(host->hostFlags & HOSTDELETED))
1457 && (host->refCount < OTHER_MUSTHOLD_LIH)
1458 && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
1459 && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
1462 h_Release_r(params->lih); /* release prev host */
1471 /* same as lih0_r, except we do not prevent held hosts from being selected. */
1473 lih1_r(struct host *host, int flags, void *rock)
1475 struct lih_params *params = (struct lih_params *)rock;
1478 && (!(host->hostFlags & HOSTDELETED))
1479 && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
1480 && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
1483 h_Release_r(params->lih); /* release prev host */
1492 /* This could be upgraded to get more space each time */
1493 /* first pass: sequentially find the oldest host which isn't held by
1494 anyone for which we can clear callbacks;
1496 /* second pass: sequentially find the oldest host regardless of
1497 whether or not the host is held; skipping 'hostp' */
1498 /* third pass: attempt to clear callbacks from 'hostp' */
1499 /* always called with hostp unlocked */
1501 /* Note: hostlist is ordered most recently created host first and
1502 * its order has no relationship to the most recently used. */
1503 extern struct host *hostList;
1505 GetSomeSpace_r(struct host *hostp, int locked)
1508 struct lih_params params;
1511 cbstuff.GotSomeSpaces++;
1513 ("GSS: First looking for timed out call backs via CleanupCallBacks\n"));
1514 if (CleanupTimedOutCallBacks_r()) {
1520 params.lastlih = NULL;
1525 h_Enumerate_r(i == 0 ? lih0_r : lih1_r, hostList, ¶ms);
1528 if (params.lastlih) {
1529 h_Release_r(params.lastlih);
1530 params.lastlih = NULL;
1534 /* note that 'hp' was held by lih*_r; we will need to release it */
1536 if ((hp != hostp) && !ClearHostCallbacks_r(hp, 0 /* not locked or held */ )) {
1541 params.lastlih = hp;
1542 /* params.lastlih will be released on the next iteration, after
1547 * Next time try getting callbacks from any host even if
1548 * it's held, since the only other option is starvation for
1549 * the file server (i.e. until the callback timeout arrives).
1552 params.lastlih = NULL;
1555 ("GSS: Try harder for longest inactive host cnt= %d\n",
1560 /* Could not obtain space from other hosts, clear hostp's callback state */
1565 ClearHostCallbacks_r(hostp, 1 /*already locked */ );
1572 /* locked - set if caller has already locked the host */
1574 ClearHostCallbacks_r(struct host *hp, int locked)
1578 struct rx_connection *cb_conn = NULL;
1581 ("GSS: Delete longest inactive host %p (%s:%d)\n",
1582 hp, afs_inet_ntoa_r(hp->host, hoststr), ntohs(hp->port)));
1584 if ((hp->hostFlags & HOSTDELETED)) {
1585 /* hp could go away after reacquiring H_LOCK in h_NBLock_r, so we can't
1586 * really use it; its callbacks will get cleared anyway when
1587 * h_TossStuff_r gets its hands on it */
1593 /** Try a non-blocking lock. If the lock is already held return
1594 * after releasing hold on hp
1597 if (h_NBLock_r(hp)) {
1602 if (hp->Console & 2) {
1604 * If the special console field is set it means that a thread
1605 * is waiting in AddCallBack1 after it set pointers to the
1606 * file entry and/or callback entry. Because of the bogus
1607 * usage of h_hold it won't prevent from another thread, this
1608 * one, to remove all the callbacks so just to be safe we keep
1609 * a reference. NOTE, on the last phase we'll free the calling
1610 * host's callbacks but that's ok...
1614 DeleteAllCallBacks_r(hp, 1);
1615 if (hp->hostFlags & VENUSDOWN) {
1616 hp->hostFlags &= ~RESETDONE; /* remember that we must do a reset */
1617 } else if (!(hp->hostFlags & HOSTDELETED)) {
1618 /* host is up, try a call */
1619 hp->hostFlags &= ~ALTADDR; /* alternate addresses are invalid */
1620 cb_conn = hp->callback_rxcon;
1621 rx_GetConnection(hp->callback_rxcon);
1622 if (hp->interface) {
1625 RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
1628 code = RXAFSCB_InitCallBackState(cb_conn);
1630 rx_PutConnection(cb_conn);
1633 hp->hostFlags |= ALTADDR; /* alternate addresses are valid */
1635 /* failed, mark host down and need reset */
1636 hp->hostFlags |= VENUSDOWN;
1637 hp->hostFlags &= ~RESETDONE;
1639 /* reset succeeded, we're done */
1640 hp->hostFlags |= RESETDONE;
1649 #endif /* INTERPRET_DUMP */
1653 PrintCallBackStats(void)
1656 "%d add CB, %d break CB, %d del CB, %d del FE, %d CB's timed out, %d space reclaim, %d del host\n",
1657 cbstuff.AddCallBacks, cbstuff.BreakCallBacks,
1658 cbstuff.DeleteCallBacks, cbstuff.DeleteFiles, cbstuff.CBsTimedOut,
1659 cbstuff.GotSomeSpaces, cbstuff.DeleteAllCallBacks);
1660 fprintf(stderr, "%d CBs, %d FEs, (%d of total of %d 16-byte blocks)\n",
1661 cbstuff.nCBs, cbstuff.nFEs, cbstuff.nCBs + cbstuff.nFEs,
1663 fprintf(stderr, "%d GSS1, %d GSS2, %d GSS3, %d GSS4, %d GSS5 (internal counters)\n",
1664 cbstuff.GSS1, cbstuff.GSS2, cbstuff.GSS3, cbstuff.GSS4, cbstuff.GSS5);
1669 #define MAGIC 0x12345678 /* To check byte ordering of dump when it is read in */
1670 #define MAGICV2 0x12345679 /* To check byte ordering & version of dump when it is read in */
1673 #ifndef INTERPRET_DUMP
1675 #ifdef AFS_DEMAND_ATTACH_FS
1678 * callback state serialization
1680 static int cb_stateSaveTimeouts(struct fs_dump_state * state);
1681 static int cb_stateSaveFEHash(struct fs_dump_state * state);
1682 static int cb_stateSaveFEs(struct fs_dump_state * state);
1683 static int cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe);
1684 static int cb_stateRestoreTimeouts(struct fs_dump_state * state);
1685 static int cb_stateRestoreFEHash(struct fs_dump_state * state);
1686 static int cb_stateRestoreFEs(struct fs_dump_state * state);
1687 static int cb_stateRestoreFE(struct fs_dump_state * state);
1688 static int cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
1689 struct iovec * iov, int niovecs);
1691 static int cb_stateVerifyFEHash(struct fs_dump_state * state);
1692 static int cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe);
1693 static int cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe);
1694 static int cb_stateVerifyTimeoutQueues(struct fs_dump_state * state);
1696 static int cb_stateFEToDiskEntry(struct FileEntry *, struct FEDiskEntry *);
1697 static int cb_stateDiskEntryToFE(struct fs_dump_state * state,
1698 struct FEDiskEntry *, struct FileEntry *);
1700 static int cb_stateCBToDiskEntry(struct CallBack *, struct CBDiskEntry *);
1701 static int cb_stateDiskEntryToCB(struct fs_dump_state * state,
1702 struct CBDiskEntry *, struct CallBack *);
1704 static int cb_stateFillHeader(struct callback_state_header * hdr);
1705 static int cb_stateCheckHeader(struct callback_state_header * hdr);
1707 static int cb_stateAllocMap(struct fs_dump_state * state);
1710 cb_stateSave(struct fs_dump_state * state)
1714 AssignInt64(state->eof_offset, &state->hdr->cb_offset);
1716 /* invalidate callback state header */
1717 memset(state->cb_hdr, 0, sizeof(struct callback_state_header));
1718 if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1719 sizeof(struct callback_state_header))) {
1724 fs_stateIncEOF(state, sizeof(struct callback_state_header));
1726 /* dump timeout state */
1727 if (cb_stateSaveTimeouts(state)) {
1732 /* dump fe hashtable state */
1733 if (cb_stateSaveFEHash(state)) {
1738 /* dump callback state */
1739 if (cb_stateSaveFEs(state)) {
1744 /* write the callback state header to disk */
1745 cb_stateFillHeader(state->cb_hdr);
1746 if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1747 sizeof(struct callback_state_header))) {
1757 cb_stateRestore(struct fs_dump_state * state)
1761 if (fs_stateReadHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1762 sizeof(struct callback_state_header))) {
1767 if (cb_stateCheckHeader(state->cb_hdr)) {
1772 if (cb_stateAllocMap(state)) {
1777 if (cb_stateRestoreTimeouts(state)) {
1782 if (cb_stateRestoreFEHash(state)) {
1787 /* restore FEs and CBs from disk */
1788 if (cb_stateRestoreFEs(state)) {
1793 /* restore the timeout queue heads */
1794 tfirst = state->cb_hdr->tfirst;
1801 cb_stateRestoreIndices(struct fs_dump_state * state)
1804 struct FileEntry * fe;
1805 struct CallBack * cb;
1807 /* restore indices in the FileEntry structures */
1808 for (i = 1; i < state->fe_map.len; i++) {
1809 if (state->fe_map.entries[i].new_idx) {
1810 fe = itofe(state->fe_map.entries[i].new_idx);
1812 /* restore the fe->fnext entry */
1813 if (fe_OldToNew(state, fe->fnext, &fe->fnext)) {
1818 /* restore the fe->firstcb entry */
1819 if (cb_OldToNew(state, fe->firstcb, &fe->firstcb)) {
1826 /* restore indices in the CallBack structures */
1827 for (i = 1; i < state->cb_map.len; i++) {
1828 if (state->cb_map.entries[i].new_idx) {
1829 cb = itocb(state->cb_map.entries[i].new_idx);
1831 /* restore the cb->cnext entry */
1832 if (cb_OldToNew(state, cb->cnext, &cb->cnext)) {
1837 /* restore the cb->fhead entry */
1838 if (fe_OldToNew(state, cb->fhead, &cb->fhead)) {
1843 /* restore the cb->hhead entry */
1844 if (h_OldToNew(state, cb->hhead, &cb->hhead)) {
1849 /* restore the cb->tprev entry */
1850 if (cb_OldToNew(state, cb->tprev, &cb->tprev)) {
1855 /* restore the cb->tnext entry */
1856 if (cb_OldToNew(state, cb->tnext, &cb->tnext)) {
1861 /* restore the cb->hprev entry */
1862 if (cb_OldToNew(state, cb->hprev, &cb->hprev)) {
1867 /* restore the cb->hnext entry */
1868 if (cb_OldToNew(state, cb->hnext, &cb->hnext)) {
1875 /* restore the timeout queue head indices */
1876 for (i = 0; i < state->cb_timeout_hdr->records; i++) {
1877 if (cb_OldToNew(state, timeout[i], &timeout[i])) {
1883 /* restore the FE hash table queue heads */
1884 for (i = 0; i < state->cb_fehash_hdr->records; i++) {
1885 if (fe_OldToNew(state, HashTable[i], &HashTable[i])) {
1896 cb_stateVerify(struct fs_dump_state * state)
1900 if (cb_stateVerifyFEHash(state)) {
1904 if (cb_stateVerifyTimeoutQueues(state)) {
1912 cb_stateVerifyFEHash(struct fs_dump_state * state)
1915 struct FileEntry * fe;
1916 afs_uint32 fei, chain_len;
1918 for (i = 0; i < FEHASH_SIZE; i++) {
1920 for (fei = HashTable[i], fe = itofe(fei);
1922 fei = fe->fnext, fe = itofe(fei)) {
1923 if (fei > cbstuff.nblks) {
1924 ViceLog(0, ("cb_stateVerifyFEHash: error: index out of range (fei=%d)\n", fei));
1928 if (cb_stateVerifyFE(state, fe)) {
1931 if (chain_len > FS_STATE_FE_MAX_HASH_CHAIN_LEN) {
1932 ViceLog(0, ("cb_stateVerifyFEHash: error: hash chain %d length exceeds %d; assuming there's a loop\n",
1933 i, FS_STATE_FE_MAX_HASH_CHAIN_LEN));
1945 cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe)
1949 if ((fe->firstcb && !fe->ncbs) ||
1950 (!fe->firstcb && fe->ncbs)) {
1951 ViceLog(0, ("cb_stateVerifyFE: error: fe->firstcb does not agree with fe->ncbs (fei=%lu, fe->firstcb=%lu, fe->ncbs=%lu)\n",
1952 afs_printable_uint32_lu(fetoi(fe)),
1953 afs_printable_uint32_lu(fe->firstcb),
1954 afs_printable_uint32_lu(fe->ncbs)));
1957 if (cb_stateVerifyFCBList(state, fe)) {
1958 ViceLog(0, ("cb_stateVerifyFE: error: FCBList failed verification (fei=%lu)\n",
1959 afs_printable_uint32_lu(fetoi(fe))));
1967 cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe)
1970 afs_uint32 cbi, fei, chain_len = 0;
1971 struct CallBack * cb;
1975 for (cbi = fe->firstcb, cb = itocb(cbi);
1977 cbi = cb->cnext, cb = itocb(cbi)) {
1978 if (cbi > cbstuff.nblks) {
1979 ViceLog(0, ("cb_stateVerifyFCBList: error: list index out of range (cbi=%d, ncbs=%d)\n",
1980 cbi, cbstuff.nblks));
1984 if (cb->fhead != fei) {
1985 ViceLog(0, ("cb_stateVerifyFCBList: error: cb->fhead != fei (fei=%d, cb->fhead=%d)\n",
1989 if (chain_len > FS_STATE_FCB_MAX_LIST_LEN) {
1990 ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (fei=%d); assuming there's a loop\n",
1991 FS_STATE_FCB_MAX_LIST_LEN, fei));
1998 if (fe->ncbs != chain_len) {
1999 ViceLog(0, ("cb_stateVerifyFCBList: error: list length mismatch (len=%d, fe->ncbs=%d)\n",
2000 chain_len, fe->ncbs));
2009 cb_stateVerifyHCBList(struct fs_dump_state * state, struct host * host)
2012 afs_uint32 hi, chain_len, cbi;
2013 struct CallBack *cb, *ncb;
2018 for (cbi = host->cblist, cb = itocb(cbi);
2020 cbi = cb->hnext, cb = ncb) {
2021 if (chain_len && (host->cblist == cbi)) {
2022 /* we've wrapped around the circular list, and everything looks ok */
2025 if (cb->hhead != hi) {
2026 ViceLog(0, ("cb_stateVerifyHCBList: error: incorrect cb->hhead (cbi=%d, h->index=%d, cb->hhead=%d)\n",
2027 cbi, hi, cb->hhead));
2030 if (!cb->hprev || !cb->hnext) {
2031 ViceLog(0, ("cb_stateVerifyHCBList: error: null index in circular list (cbi=%d, h->index=%d)\n",
2036 if ((cb->hprev > cbstuff.nblks) ||
2037 (cb->hnext > cbstuff.nblks)) {
2038 ViceLog(0, ("cb_stateVerifyHCBList: error: list index out of range (cbi=%d, h->index=%d, cb->hprev=%d, cb->hnext=%d, nCBs=%d)\n",
2039 cbi, hi, cb->hprev, cb->hnext, cbstuff.nblks));
2043 ncb = itocb(cb->hnext);
2044 if (cbi != ncb->hprev) {
2045 ViceLog(0, ("cb_stateVerifyHCBList: error: corrupt linked list (cbi=%d, h->index=%d)\n",
2050 if (chain_len > FS_STATE_HCB_MAX_LIST_LEN) {
2051 ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (h->index=%d); assuming there's a loop\n",
2052 FS_STATE_HCB_MAX_LIST_LEN, hi));
2064 cb_stateVerifyTimeoutQueues(struct fs_dump_state * state)
2067 afs_uint32 cbi, chain_len;
2068 struct CallBack *cb, *ncb;
2070 for (i = 0; i < CB_NUM_TIMEOUT_QUEUES; i++) {
2072 for (cbi = timeout[i], cb = itocb(cbi);
2074 cbi = cb->tnext, cb = ncb) {
2075 if (chain_len && (cbi == timeout[i])) {
2076 /* we've wrapped around the circular list, and everything looks ok */
2079 if (cbi > cbstuff.nblks) {
2080 ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: list index out of range (cbi=%d, tindex=%d)\n",
2085 if (itot(cb->thead) != &timeout[i]) {
2086 ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: cb->thead points to wrong timeout queue (tindex=%d, cbi=%d, cb->thead=%d)\n",
2087 i, cbi, cb->thead));
2090 if (!cb->tprev || !cb->tnext) {
2091 ViceLog(0, ("cb_stateVerifyTimeoutQueues: null index in circular list (cbi=%d, tindex=%d)\n",
2096 if ((cb->tprev > cbstuff.nblks) ||
2097 (cb->tnext > cbstuff.nblks)) {
2098 ViceLog(0, ("cb_stateVerifyTimeoutQueues: list index out of range (cbi=%d, tindex=%d, cb->tprev=%d, cb->tnext=%d, nCBs=%d)\n",
2099 cbi, i, cb->tprev, cb->tnext, cbstuff.nblks));
2103 ncb = itocb(cb->tnext);
2104 if (cbi != ncb->tprev) {
2105 ViceLog(0, ("cb_stateVerifyTimeoutQueues: corrupt linked list (cbi=%d, tindex=%d)\n",
2110 if (chain_len > FS_STATE_TCB_MAX_LIST_LEN) {
2111 ViceLog(0, ("cb_stateVerifyTimeoutQueues: list length exceeds %d (tindex=%d); assuming there's a loop\n",
2112 FS_STATE_TCB_MAX_LIST_LEN, i));
2124 cb_stateSaveTimeouts(struct fs_dump_state * state)
2127 struct iovec iov[2];
2129 AssignInt64(state->eof_offset, &state->cb_hdr->timeout_offset);
2131 memset(state->cb_timeout_hdr, 0, sizeof(struct callback_state_fehash_header));
2132 state->cb_timeout_hdr->magic = CALLBACK_STATE_TIMEOUT_MAGIC;
2133 state->cb_timeout_hdr->records = CB_NUM_TIMEOUT_QUEUES;
2134 state->cb_timeout_hdr->len = sizeof(struct callback_state_timeout_header) +
2135 (state->cb_timeout_hdr->records * sizeof(afs_uint32));
2137 iov[0].iov_base = (char *)state->cb_timeout_hdr;
2138 iov[0].iov_len = sizeof(struct callback_state_timeout_header);
2139 iov[1].iov_base = (char *)timeout;
2140 iov[1].iov_len = sizeof(timeout);
2142 if (fs_stateSeek(state, &state->cb_hdr->timeout_offset)) {
2147 if (fs_stateWriteV(state, iov, 2)) {
2152 fs_stateIncEOF(state, state->cb_timeout_hdr->len);
2159 cb_stateRestoreTimeouts(struct fs_dump_state * state)
2163 if (fs_stateReadHeader(state, &state->cb_hdr->timeout_offset,
2164 state->cb_timeout_hdr,
2165 sizeof(struct callback_state_timeout_header))) {
2170 if (state->cb_timeout_hdr->magic != CALLBACK_STATE_TIMEOUT_MAGIC) {
2174 if (state->cb_timeout_hdr->records != CB_NUM_TIMEOUT_QUEUES) {
2179 len = state->cb_timeout_hdr->records * sizeof(afs_uint32);
2181 if (state->cb_timeout_hdr->len !=
2182 (sizeof(struct callback_state_timeout_header) + len)) {
2187 if (fs_stateRead(state, timeout, len)) {
2197 cb_stateSaveFEHash(struct fs_dump_state * state)
2200 struct iovec iov[2];
2202 AssignInt64(state->eof_offset, &state->cb_hdr->fehash_offset);
2204 memset(state->cb_fehash_hdr, 0, sizeof(struct callback_state_fehash_header));
2205 state->cb_fehash_hdr->magic = CALLBACK_STATE_FEHASH_MAGIC;
2206 state->cb_fehash_hdr->records = FEHASH_SIZE;
2207 state->cb_fehash_hdr->len = sizeof(struct callback_state_fehash_header) +
2208 (state->cb_fehash_hdr->records * sizeof(afs_uint32));
2210 iov[0].iov_base = (char *)state->cb_fehash_hdr;
2211 iov[0].iov_len = sizeof(struct callback_state_fehash_header);
2212 iov[1].iov_base = (char *)HashTable;
2213 iov[1].iov_len = sizeof(HashTable);
2215 if (fs_stateSeek(state, &state->cb_hdr->fehash_offset)) {
2220 if (fs_stateWriteV(state, iov, 2)) {
2225 fs_stateIncEOF(state, state->cb_fehash_hdr->len);
2232 cb_stateRestoreFEHash(struct fs_dump_state * state)
2236 if (fs_stateReadHeader(state, &state->cb_hdr->fehash_offset,
2237 state->cb_fehash_hdr,
2238 sizeof(struct callback_state_fehash_header))) {
2243 if (state->cb_fehash_hdr->magic != CALLBACK_STATE_FEHASH_MAGIC) {
2247 if (state->cb_fehash_hdr->records != FEHASH_SIZE) {
2252 len = state->cb_fehash_hdr->records * sizeof(afs_uint32);
2254 if (state->cb_fehash_hdr->len !=
2255 (sizeof(struct callback_state_fehash_header) + len)) {
2260 if (fs_stateRead(state, HashTable, len)) {
2270 cb_stateSaveFEs(struct fs_dump_state * state)
2274 struct FileEntry *fe;
2276 AssignInt64(state->eof_offset, &state->cb_hdr->fe_offset);
2278 for (hash = 0; hash < FEHASH_SIZE ; hash++) {
2279 for (fei = HashTable[hash]; fei; fei = fe->fnext) {
2281 if (cb_stateSaveFE(state, fe)) {
2293 cb_stateRestoreFEs(struct fs_dump_state * state)
2295 int count, nFEs, ret = 0;
2297 nFEs = state->cb_hdr->nFEs;
2299 for (count = 0; count < nFEs; count++) {
2300 if (cb_stateRestoreFE(state)) {
2311 cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
2313 int ret = 0, iovcnt, cbi, written = 0;
2315 struct callback_state_entry_header hdr;
2316 struct FEDiskEntry fedsk;
2317 struct CBDiskEntry cbdsk[16];
2318 struct iovec iov[16];
2319 struct CallBack *cb;
2322 if (fei > state->cb_hdr->fe_max) {
2323 state->cb_hdr->fe_max = fei;
2326 memset(&hdr, 0, sizeof(struct callback_state_entry_header));
2328 if (cb_stateFEToDiskEntry(fe, &fedsk)) {
2333 iov[0].iov_base = (char *)&hdr;
2334 iov[0].iov_len = sizeof(hdr);
2335 iov[1].iov_base = (char *)&fedsk;
2336 iov[1].iov_len = sizeof(struct FEDiskEntry);
2339 for (cbi = fe->firstcb, cb = itocb(cbi);
2341 cbi = cb->cnext, cb = itocb(cbi), hdr.nCBs++) {
2342 if (cbi > state->cb_hdr->cb_max) {
2343 state->cb_hdr->cb_max = cbi;
2345 if (cb_stateCBToDiskEntry(cb, &cbdsk[iovcnt])) {
2349 cbdsk[iovcnt].index = cbi;
2350 iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
2351 iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
2353 if ((iovcnt == 16) || (!cb->cnext)) {
2354 if (fs_stateWriteV(state, iov, iovcnt)) {
2363 hdr.magic = CALLBACK_STATE_ENTRY_MAGIC;
2364 hdr.len = sizeof(hdr) + sizeof(struct FEDiskEntry) +
2365 (hdr.nCBs * sizeof(struct CBDiskEntry));
2368 if (fs_stateWriteV(state, iov, iovcnt)) {
2373 if (fs_stateWriteHeader(state, &state->eof_offset, &hdr, sizeof(hdr))) {
2379 fs_stateIncEOF(state, hdr.len);
2382 if (fs_stateSeek(state, &state->eof_offset)) {
2388 state->cb_hdr->nFEs++;
2389 state->cb_hdr->nCBs += hdr.nCBs;
2396 cb_stateRestoreFE(struct fs_dump_state * state)
2398 int ret = 0, iovcnt, nCBs;
2399 struct callback_state_entry_header hdr;
2400 struct FEDiskEntry fedsk;
2401 struct CBDiskEntry cbdsk[16];
2402 struct iovec iov[16];
2403 struct FileEntry * fe;
2405 iov[0].iov_base = (char *)&hdr;
2406 iov[0].iov_len = sizeof(hdr);
2407 iov[1].iov_base = (char *)&fedsk;
2408 iov[1].iov_len = sizeof(fedsk);
2411 if (fs_stateReadV(state, iov, iovcnt)) {
2416 if (hdr.magic != CALLBACK_STATE_ENTRY_MAGIC) {
2423 ViceLog(0, ("cb_stateRestoreFE: ran out of free FileEntry structures\n"));
2428 if (cb_stateDiskEntryToFE(state, &fedsk, fe)) {
2434 for (iovcnt = 0, nCBs = 0;
2437 iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
2438 iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
2440 if ((iovcnt == 16) || (nCBs == hdr.nCBs - 1)) {
2441 if (fs_stateReadV(state, iov, iovcnt)) {
2445 if (cb_stateRestoreCBs(state, fe, iov, iovcnt)) {
2459 cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
2460 struct iovec * iov, int niovecs)
2463 struct CallBack * cb;
2464 struct CBDiskEntry * cbdsk;
2466 for (idx = 0; idx < niovecs; idx++) {
2467 cbdsk = (struct CBDiskEntry *) iov[idx].iov_base;
2468 if ((cb = GetCB()) == NULL) {
2469 ViceLog(0, ("cb_stateRestoreCBs: ran out of free CallBack structures\n"));
2473 if (cb_stateDiskEntryToCB(state, cbdsk, cb)) {
2474 ViceLog(0, ("cb_stateRestoreCBs: corrupt CallBack disk entry\n"));
2486 cb_stateFillHeader(struct callback_state_header * hdr)
2488 hdr->stamp.magic = CALLBACK_STATE_MAGIC;
2489 hdr->stamp.version = CALLBACK_STATE_VERSION;
2490 hdr->tfirst = tfirst;
2495 cb_stateCheckHeader(struct callback_state_header * hdr)
2499 if (hdr->stamp.magic != CALLBACK_STATE_MAGIC) {
2501 } else if (hdr->stamp.version != CALLBACK_STATE_VERSION) {
2503 } else if ((hdr->nFEs > cbstuff.nblks) || (hdr->nCBs > cbstuff.nblks)) {
2504 ViceLog(0, ("cb_stateCheckHeader: saved callback state larger than callback memory allocation\n"));
2510 /* disk entry conversion routines */
2512 cb_stateFEToDiskEntry(struct FileEntry * in, struct FEDiskEntry * out)
2514 memcpy(&out->fe, in, sizeof(struct FileEntry));
2515 out->index = fetoi(in);
2520 cb_stateDiskEntryToFE(struct fs_dump_state * state,
2521 struct FEDiskEntry * in, struct FileEntry * out)
2525 memcpy(out, &in->fe, sizeof(struct FileEntry));
2527 /* setup FE map entry */
2528 if (!in->index || (in->index >= state->fe_map.len)) {
2529 ViceLog(0, ("cb_stateDiskEntryToFE: index (%d) out of range",
2534 state->fe_map.entries[in->index].old_idx = in->index;
2535 state->fe_map.entries[in->index].new_idx = fetoi(out);
2542 cb_stateCBToDiskEntry(struct CallBack * in, struct CBDiskEntry * out)
2544 memcpy(&out->cb, in, sizeof(struct CallBack));
2545 out->index = cbtoi(in);
2550 cb_stateDiskEntryToCB(struct fs_dump_state * state,
2551 struct CBDiskEntry * in, struct CallBack * out)
2555 memcpy(out, &in->cb, sizeof(struct CallBack));
2557 /* setup CB map entry */
2558 if (!in->index || (in->index >= state->cb_map.len)) {
2559 ViceLog(0, ("cb_stateDiskEntryToCB: index (%d) out of range\n",
2564 state->cb_map.entries[in->index].old_idx = in->index;
2565 state->cb_map.entries[in->index].new_idx = cbtoi(out);
2571 /* index map routines */
2573 cb_stateAllocMap(struct fs_dump_state * state)
2575 state->fe_map.len = state->cb_hdr->fe_max + 1;
2576 state->cb_map.len = state->cb_hdr->cb_max + 1;
2577 state->fe_map.entries = (struct idx_map_entry_t *)
2578 calloc(state->fe_map.len, sizeof(struct idx_map_entry_t));
2579 state->cb_map.entries = (struct idx_map_entry_t *)
2580 calloc(state->cb_map.len, sizeof(struct idx_map_entry_t));
2581 return ((state->fe_map.entries != NULL) && (state->cb_map.entries != NULL)) ? 0 : 1;
2585 fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2589 /* FEs use a one-based indexing system, so old==0 implies no mapping */
2595 if (old >= state->fe_map.len) {
2596 ViceLog(0, ("fe_OldToNew: index %d is out of range\n", old));
2598 } else if (state->fe_map.entries[old].old_idx != old) { /* sanity check */
2599 ViceLog(0, ("fe_OldToNew: index %d points to an invalid FileEntry record\n", old));
2602 *new = state->fe_map.entries[old].new_idx;
2610 cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2614 /* CBs use a one-based indexing system, so old==0 implies no mapping */
2620 if (old >= state->cb_map.len) {
2621 ViceLog(0, ("cb_OldToNew: index %d is out of range\n", old));
2623 } else if (state->cb_map.entries[old].old_idx != old) { /* sanity check */
2624 ViceLog(0, ("cb_OldToNew: index %d points to an invalid CallBack record\n", old));
2627 *new = state->cb_map.entries[old].new_idx;
2633 #endif /* AFS_DEMAND_ATTACH_FS */
2636 DumpCallBackState_r(void)
2639 afs_uint32 magic = MAGICV2, now = (afs_int32) FT_ApproxTime(), freelisthead;
2641 oflag = O_WRONLY | O_CREAT | O_TRUNC;
2645 fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, oflag, 0666);
2648 ("Couldn't create callback dump file %s\n",
2649 AFSDIR_SERVER_CBKDUMP_FILEPATH));
2652 (void)write(fd, &magic, sizeof(magic));
2653 (void)write(fd, &now, sizeof(now));
2654 (void)write(fd, &cbstuff, sizeof(cbstuff));
2655 (void)write(fd, TimeOuts, sizeof(TimeOuts));
2656 (void)write(fd, timeout, sizeof(timeout));
2657 (void)write(fd, &tfirst, sizeof(tfirst));
2658 freelisthead = cbtoi((struct CallBack *)CBfree);
2659 (void)write(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
2660 freelisthead = fetoi((struct FileEntry *)FEfree);
2661 (void)write(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
2662 (void)write(fd, HashTable, sizeof(HashTable));
2663 (void)write(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks); /* CB stuff */
2664 (void)write(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks); /* FE stuff */
2671 DumpCallBackState(void) {
2675 rc = DumpCallBackState_r();
2681 #endif /* !INTERPRET_DUMP */
2683 #ifdef INTERPRET_DUMP
2685 /* This is only compiled in for the callback analyzer program */
2686 /* Returns the time of the dump */
2688 ReadDump(char *file, int timebits)
2691 afs_uint32 magic, freelisthead;
2699 fd = open(file, oflag);
2701 fprintf(stderr, "Couldn't read dump file %s\n", file);
2704 read(fd, &magic, sizeof(magic));
2705 if (magic == MAGICV2) {
2708 if (magic != MAGIC) {
2710 "Magic number of %s is invalid. You might be trying to\n",
2713 "run this program on a machine type with a different byte ordering.\n");
2717 if (timebits == 64) {
2718 read(fd, &now64, sizeof(afs_int64));
2719 now = (afs_int32) now64;
2721 read(fd, &now, sizeof(afs_int32));
2723 read(fd, &cbstuff, sizeof(cbstuff));
2724 read(fd, TimeOuts, sizeof(TimeOuts));
2725 read(fd, timeout, sizeof(timeout));
2726 read(fd, &tfirst, sizeof(tfirst));
2727 read(fd, &freelisthead, sizeof(freelisthead));
2728 CB = ((struct CallBack
2729 *)(calloc(cbstuff.nblks, sizeof(struct CallBack)))) - 1;
2730 FE = ((struct FileEntry
2731 *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
2732 CBfree = (struct CallBack *)itocb(freelisthead);
2733 read(fd, &freelisthead, sizeof(freelisthead));
2734 FEfree = (struct FileEntry *)itofe(freelisthead);
2735 read(fd, HashTable, sizeof(HashTable));
2736 read(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks); /* CB stuff */
2737 read(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks); /* FE stuff */
2739 perror("Error reading dumpfile");
2746 #include "AFS_component_version_number.h"
2748 #include "AFS_component_version_number.c"
2751 static afs_uint32 *cbTrack;
2754 main(int argc, char **argv)
2756 int err = 0, cbi = 0, stats = 0, noptions = 0, all = 0, vol = 0, raw = 0;
2758 struct FileEntry *fe;
2759 struct CallBack *cb;
2763 memset(&fid, 0, sizeof(fid));
2766 while (argc && **argv == '-') {
2769 if (!strcmp(*argv, "-host")) {
2775 cbi = atoi(*++argv);
2776 } else if (!strcmp(*argv, "-fid")) {
2782 fid.Volume = atoi(*++argv);
2783 fid.Vnode = atoi(*++argv);
2784 fid.Unique = atoi(*++argv);
2785 } else if (!strcmp(*argv, "-time")) {
2786 fprintf(stderr, "-time not supported\n");
2788 } else if (!strcmp(*argv, "-stats")) {
2790 } else if (!strcmp(*argv, "-all")) {
2792 } else if (!strcmp(*argv, "-raw")) {
2794 } else if (!strcmp(*argv, "-timebits")) {
2800 timebits = atoi(*++argv);
2801 if ((timebits != 32)
2805 } else if (!strcmp(*argv, "-volume")) {
2811 vol = atoi(*++argv);
2816 if (err || argc != 1) {
2818 "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] [-timebits 32"
2820 "] callbackdumpfile\n");
2822 "[cbid is shown for each host in the hosts.dump file]\n");
2825 now = ReadDump(*argv, timebits);
2826 if (stats || noptions == 0) {
2827 time_t uxtfirst = UXtime(tfirst), tnow = now;
2828 printf("The time of the dump was %u %s", (unsigned int) now, ctime(&tnow));
2829 printf("The last time cleanup ran was %u %s", (unsigned int) uxtfirst,
2831 PrintCallBackStats();
2834 cbTrack = calloc(cbstuff.nblks, sizeof(cbTrack[0]));
2839 struct CallBack *cb;
2840 struct FileEntry *fe;
2842 for (hash = 0; hash < FEHASH_SIZE; hash++) {
2843 for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
2844 if (!vol || (fe->volid == vol)) {
2845 afs_uint32 fe_i = fetoi(fe);
2847 for (cb = itocb(fe->firstcb); cb; cb = itocb(cb->cnext)) {
2848 afs_uint32 cb_i = cbtoi(cb);
2850 if (cb_i > cbstuff.nblks) {
2851 printf("CB index out of range (%u > %d), stopped for this FE\n",
2852 cb_i, cbstuff.nblks);
2856 if (cbTrack[cb_i]) {
2857 printf("CB entry already claimed for FE[%u] (this is FE[%u]), stopped\n",
2858 cbTrack[cb_i], fe_i);
2861 cbTrack[cb_i] = fe_i;
2873 afs_uint32 cfirst = cbi;
2878 } while (cbi != cfirst);
2883 printf("No callback entries for %u.%u\n", fid.Volume, fid.Vnode);
2886 cb = itocb(fe->firstcb);
2889 cb = itocb(cb->cnext);
2894 for (i = 1; i < cbstuff.nblks; i++) {
2895 p = (afs_int32 *) & FE[i];
2896 printf("%d:%12x%12x%12x%12x\n", i, p[0], p[1], p[2], p[3]);
2905 PrintCB(struct CallBack *cb, afs_uint32 now)
2907 struct FileEntry *fe = itofe(cb->fhead);
2908 time_t expires = TIndexToTime(cb->thead);
2913 printf("vol=%u vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %lu secs at %s",
2914 fe->volid, fe->vnode, fe->ncbs, cb->hhead, cb->status, fe->status,
2915 expires - now, ctime(&expires));
2920 #if !defined(INTERPRET_DUMP)
2922 ** try breaking calbacks on afidp from host. Use multi_rx.
2923 ** return 0 on success, non-zero on failure
2926 MultiBreakCallBackAlternateAddress(struct host *host, struct AFSCBFids *afidp)
2930 retVal = MultiBreakCallBackAlternateAddress_r(host, afidp);
2936 MultiBreakCallBackAlternateAddress_r(struct host *host,
2937 struct AFSCBFids *afidp)
2940 struct rx_connection **conns;
2941 struct rx_connection *connSuccess = 0;
2942 struct AddrPort *interfaces;
2943 static struct rx_securityClass *sc = 0;
2944 static struct AFSCBs tc = { 0, 0 };
2947 /* nothing more can be done */
2948 if (!host->interface)
2949 return 1; /* failure */
2951 /* the only address is the primary interface */
2952 if (host->interface->numberOfInterfaces <= 1)
2953 return 1; /* failure */
2955 /* initialise a security object only once */
2957 sc = rxnull_NewClientSecurityObject();
2959 i = host->interface->numberOfInterfaces;
2960 interfaces = calloc(i, sizeof(struct AddrPort));
2961 conns = calloc(i, sizeof(struct rx_connection *));
2962 if (!interfaces || !conns) {
2964 ("Failed malloc in MultiBreakCallBackAlternateAddress_r\n"));
2965 osi_Panic("Failed malloc in MultiBreakCallBackAlternateAddress_r\n");
2968 /* initialize alternate rx connections */
2969 for (i = 0, j = 0; i < host->interface->numberOfInterfaces; i++) {
2970 /* this is the current primary address */
2971 if (host->host == host->interface->interface[i].addr &&
2972 host->port == host->interface->interface[i].port)
2975 interfaces[j] = host->interface->interface[i];
2977 rx_NewConnection(interfaces[j].addr,
2978 interfaces[j].port, 1, sc, 0);
2979 rx_SetConnDeadTime(conns[j], 2);
2980 rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
2984 osi_Assert(j); /* at least one alternate address */
2986 ("Starting multibreakcall back on all addr for host %p (%s:%d)\n",
2987 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
2989 multi_Rx(conns, j) {
2990 multi_RXAFSCB_CallBack(afidp, &tc);
2994 if (host->callback_rxcon)
2995 rx_DestroyConnection(host->callback_rxcon);
2996 host->callback_rxcon = conns[multi_i];
2997 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
2998 host->host = interfaces[multi_i].addr;
2999 host->port = interfaces[multi_i].port;
3000 h_AddHostToAddrHashTable_r(host->host, host->port, host);
3001 connSuccess = conns[multi_i];
3002 rx_SetConnDeadTime(host->callback_rxcon, 50);
3003 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
3005 ("multibreakcall success with addr %s:%d\n",
3006 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3007 ntohs(interfaces[multi_i].port)));
3014 /* Destroy all connections except the one on which we succeeded */
3015 for (i = 0; i < j; i++)
3016 if (conns[i] != connSuccess)
3017 rx_DestroyConnection(conns[i]);
3023 return 0; /* success */
3025 return 1; /* failure */
3030 ** try multi_RX probes to host.
3031 ** return 0 on success, non-0 on failure
3034 MultiProbeAlternateAddress_r(struct host *host)
3037 struct rx_connection **conns;
3038 struct rx_connection *connSuccess = 0;
3039 struct AddrPort *interfaces;
3040 static struct rx_securityClass *sc = 0;
3043 /* nothing more can be done */
3044 if (!host->interface)
3045 return 1; /* failure */
3047 /* the only address is the primary interface */
3048 if (host->interface->numberOfInterfaces <= 1)
3049 return 1; /* failure */
3051 /* initialise a security object only once */
3053 sc = rxnull_NewClientSecurityObject();
3055 i = host->interface->numberOfInterfaces;
3056 interfaces = calloc(i, sizeof(struct AddrPort));
3057 conns = calloc(i, sizeof(struct rx_connection *));
3058 if (!interfaces || !conns) {
3059 ViceLog(0, ("Failed malloc in MultiProbeAlternateAddress_r\n"));
3060 osi_Panic("Failed malloc in MultiProbeAlternateAddress_r\n");
3063 /* initialize alternate rx connections */
3064 for (i = 0, j = 0; i < host->interface->numberOfInterfaces; i++) {
3065 /* this is the current primary address */
3066 if (host->host == host->interface->interface[i].addr &&
3067 host->port == host->interface->interface[i].port)
3070 interfaces[j] = host->interface->interface[i];
3072 rx_NewConnection(interfaces[j].addr,
3073 interfaces[j].port, 1, sc, 0);
3074 rx_SetConnDeadTime(conns[j], 2);
3075 rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
3079 osi_Assert(j); /* at least one alternate address */
3081 ("Starting multiprobe on all addr for host %p (%s:%d)\n",
3082 host, afs_inet_ntoa_r(host->host, hoststr),
3083 ntohs(host->port)));
3085 multi_Rx(conns, j) {
3086 multi_RXAFSCB_ProbeUuid(&host->interface->uuid);
3090 if (host->callback_rxcon)
3091 rx_DestroyConnection(host->callback_rxcon);
3092 host->callback_rxcon = conns[multi_i];
3093 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
3094 host->host = interfaces[multi_i].addr;
3095 host->port = interfaces[multi_i].port;
3096 h_AddHostToAddrHashTable_r(host->host, host->port, host);
3097 connSuccess = conns[multi_i];
3098 rx_SetConnDeadTime(host->callback_rxcon, 50);
3099 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
3101 ("multiprobe success with addr %s:%d\n",
3102 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3103 ntohs(interfaces[multi_i].port)));
3108 ("multiprobe failure with addr %s:%d\n",
3109 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3110 ntohs(interfaces[multi_i].port)));
3112 /* This is less than desirable but its the best we can do.
3113 * The AFS Cache Manager will return either 0 for a Uuid
3114 * match and a 1 for a non-match. If the error is 1 we
3115 * therefore know that our mapping of IP address to Uuid
3116 * is wrong. We should attempt to find the correct
3117 * Uuid and fix the host tables.
3119 if (multi_error == 1) {
3120 /* remove the current alternate address from this host */
3122 removeInterfaceAddr_r(host, interfaces[multi_i].addr, interfaces[multi_i].port);
3126 #ifdef AFS_DEMAND_ATTACH_FS
3127 /* try to bail ASAP if the fileserver is shutting down */
3129 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3138 /* Destroy all connections except the one on which we succeeded */
3139 for (i = 0; i < j; i++)
3140 if (conns[i] != connSuccess)
3141 rx_DestroyConnection(conns[i]);
3147 return 0; /* success */
3149 return 1; /* failure */
3152 #endif /* !defined(INTERPRET_DUMP) */