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 #ifdef HAVE_SYS_FILE_H
94 #include <afs/nfs.h> /* yuck. This is an abomination. */
97 #include <afs/afscbint.h>
98 #include <afs/afsutil.h>
100 #include <afs/ihandle.h>
101 #include <afs/vnode.h>
102 #include <afs/volume.h>
103 #include "viced_prototypes.h"
106 #include <afs/ptclient.h> /* need definition of prlist for host.h */
108 #include "callback.h"
109 #ifdef AFS_DEMAND_ATTACH_FS
110 #include "../tviced/serialize_state.h"
111 #endif /* AFS_DEMAND_ATTACH_FS */
114 extern afsUUID FS_HostUUID;
115 extern int hostCount;
117 #ifndef INTERPRET_DUMP
118 static int ShowProblems = 1;
121 struct cbcounters cbstuff;
123 static struct FileEntry * FE = NULL; /* don't use FE[0] */
124 static struct CallBack * CB = NULL; /* don't use CB[0] */
126 static struct CallBack * CBfree = NULL;
127 static struct FileEntry * FEfree = NULL;
130 /* Time to live for call backs depends upon number of users of the file.
131 * TimeOuts is indexed by this number/8 (using TimeOut macro). Times
132 * in this table are for the workstation; server timeouts, add
135 static int TimeOuts[] = {
136 /* Note: don't make the first entry larger than 4 hours (see above) */
137 4 * 60 * 60, /* 0-7 users */
138 1 * 60 * 60, /* 8-15 users */
139 30 * 60, /* 16-23 users */
140 15 * 60, /* 24-31 users */
141 15 * 60, /* 32-39 users */
142 10 * 60, /* 40-47 users */
143 10 * 60, /* 48-55 users */
144 10 * 60, /* 56-63 users */
145 }; /* Anything more: MinTimeOut */
147 /* minimum time given for a call back */
148 #ifndef INTERPRET_DUMP
149 static int MinTimeOut = (7 * 60);
152 /* Heads of CB queues; a timeout index is 1+index into this array */
153 static afs_uint32 timeout[CB_NUM_TIMEOUT_QUEUES];
155 static afs_int32 tfirst; /* cbtime of oldest unexpired call back time queue */
158 /* 16 byte object get/free routines */
163 /* Prototypes for static routines */
164 static struct FileEntry *FindFE(AFSFid * fid);
166 #ifndef INTERPRET_DUMP
167 static struct CallBack *iGetCB(int *nused);
168 static int iFreeCB(struct CallBack *cb, int *nused);
169 static struct FileEntry *iGetFE(int *nused);
170 static int iFreeFE(struct FileEntry *fe, int *nused);
171 static int TAdd(struct CallBack *cb, afs_uint32 * thead);
172 static int TDel(struct CallBack *cb);
173 static int HAdd(struct CallBack *cb, struct host *host);
174 static int HDel(struct CallBack *cb);
175 static int CDel(struct CallBack *cb, int deletefe);
176 static int CDelPtr(struct FileEntry *fe, afs_uint32 * cbp,
178 static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host);
179 static int FDel(struct FileEntry *fe);
180 static int AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead,
181 int type, int locked);
182 static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
183 struct AFSCBFids *afidp);
184 static int MultiBreakVolumeCallBack_r(struct host *host,
185 struct VCBParams *parms, int deletefe);
186 static int MultiBreakVolumeLaterCallBack(struct host *host, void *rock);
187 static int GetSomeSpace_r(struct host *hostp, int locked);
188 static int ClearHostCallbacks_r(struct host *hp, int locked);
189 static int DumpCallBackState_r(void);
192 #define GetCB() ((struct CallBack *)iGetCB(&cbstuff.nCBs))
193 #define GetFE() ((struct FileEntry *)iGetFE(&cbstuff.nFEs))
194 #define FreeCB(cb) iFreeCB((struct CallBack *)cb, &cbstuff.nCBs)
195 #define FreeFE(fe) iFreeFE((struct FileEntry *)fe, &cbstuff.nFEs)
198 /* Other protos - move out sometime */
199 void PrintCB(struct CallBack *cb, afs_uint32 now);
201 static afs_uint32 HashTable[FEHASH_SIZE]; /* File entry hash table */
203 static struct FileEntry *
208 struct FileEntry *fe;
210 hash = FEHash(fid->Volume, fid->Unique);
211 for (fei = HashTable[hash]; fei; fei = fe->fnext) {
213 if (fe->volid == fid->Volume && fe->unique == fid->Unique
214 && fe->vnode == fid->Vnode && (fe->status & FE_LATER) != FE_LATER)
220 #ifndef INTERPRET_DUMP
222 static struct CallBack *
225 struct CallBack *ret;
227 if ((ret = CBfree)) {
228 CBfree = (struct CallBack *)(((struct object *)ret)->next);
235 iFreeCB(struct CallBack *cb, int *nused)
237 ((struct object *)cb)->next = (struct object *)CBfree;
243 static struct FileEntry *
246 struct FileEntry *ret;
248 if ((ret = FEfree)) {
249 FEfree = (struct FileEntry *)(((struct object *)ret)->next);
256 iFreeFE(struct FileEntry *fe, int *nused)
258 ((struct object *)fe)->next = (struct object *)FEfree;
264 /* Add cb to end of specified timeout list */
266 TAdd(struct CallBack *cb, afs_uint32 * thead)
269 (*thead) = cb->tnext = cb->tprev = cbtoi(cb);
271 struct CallBack *thp = itocb(*thead);
273 cb->tprev = thp->tprev;
277 thp->tprev = (itocb(thp->tprev)->tnext = cbtoi(cb));
279 thp->tprev = cbtoi(cb);
282 cb->thead = ttoi(thead);
286 /* Delete call back entry from timeout list */
288 TDel(struct CallBack *cb)
290 afs_uint32 *thead = itot(cb->thead);
292 if (*thead == cbtoi(cb))
293 *thead = (*thead == cb->tnext ? 0 : cb->tnext);
294 if (itocb(cb->tprev))
295 itocb(cb->tprev)->tnext = cb->tnext;
296 if (itocb(cb->tnext))
297 itocb(cb->tnext)->tprev = cb->tprev;
301 /* Add cb to end of specified host list */
303 HAdd(struct CallBack *cb, struct host *host)
305 cb->hhead = h_htoi(host);
307 host->cblist = cb->hnext = cb->hprev = cbtoi(cb);
309 struct CallBack *fcb = itocb(host->cblist);
311 cb->hprev = fcb->hprev;
312 cb->hnext = cbtoi(fcb);
313 fcb->hprev = (itocb(fcb->hprev)->hnext = cbtoi(cb));
318 /* Delete call back entry from host list */
320 HDel(struct CallBack *cb)
322 afs_uint32 *hhead = &h_itoh(cb->hhead)->cblist;
324 if (*hhead == cbtoi(cb))
325 *hhead = (*hhead == cb->hnext ? 0 : cb->hnext);
326 itocb(cb->hprev)->hnext = cb->hnext;
327 itocb(cb->hnext)->hprev = cb->hprev;
331 /* Delete call back entry from fid's chain of cb's */
332 /* N.B. This one also deletes the CB, and also possibly parent FE, so
333 * make sure that it is not on any other list before calling this
336 CDel(struct CallBack *cb, int deletefe)
339 struct FileEntry *fe = itofe(cb->fhead);
343 for (safety = 0, cbp = &fe->firstcb; *cbp && *cbp != cbi;
344 cbp = &itocb(*cbp)->cnext, safety++) {
345 if (safety > cbstuff.nblks + 10) {
346 ViceLogThenPanic(0, ("CDel: Internal Error -- shutting down: "
347 "wanted %d from %d, now at %d\n",
348 cbi, fe->firstcb, *cbp));
349 DumpCallBackState_r();
350 ShutDownAndCore(PANIC);
353 CDelPtr(fe, cbp, deletefe);
357 /* Same as CDel, but pointer to parent pointer to CB entry is passed,
358 * as well as file entry */
359 /* N.B. This one also deletes the CB, and also possibly parent FE, so
360 * make sure that it is not on any other list before calling this
362 static int Ccdelpt = 0, CcdelB = 0;
365 CDelPtr(struct FileEntry *fe, afs_uint32 * cbp,
378 if ((--fe->ncbs == 0) && deletefe)
384 FindCBPtr(struct FileEntry *fe, struct host *host)
386 afs_uint32 hostindex = h_htoi(host);
391 for (safety = 0, cbp = &fe->firstcb; *cbp; cbp = &cb->cnext, safety++) {
392 if (safety > cbstuff.nblks) {
393 ViceLog(0, ("FindCBPtr: Internal Error -- shutting down.\n"));
394 DumpCallBackState_r();
395 ShutDownAndCore(PANIC);
398 if (cb->hhead == hostindex)
404 /* Delete file entry from hash table */
406 FDel(struct FileEntry *fe)
409 afs_uint32 *p = &HashTable[FEHash(fe->volid, fe->unique)];
411 while (*p && *p != fei)
412 p = &itofe(*p)->fnext;
419 /* initialize the callback package */
421 InitCallBack(int nblks)
424 tfirst = CBtime(FT_ApproxTime());
425 /* N.B. The "-1", below, is because
426 * FE[0] and CB[0] are not used--and not allocated */
427 FE = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry))));
429 ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
431 FE--; /* FE[0] is supposed to point to junk */
432 cbstuff.nFEs = nblks;
434 FreeFE(&FE[cbstuff.nFEs]); /* This is correct */
435 CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))));
437 ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
439 CB--; /* CB[0] is supposed to point to junk */
440 cbstuff.nCBs = nblks;
442 FreeCB(&CB[cbstuff.nCBs]); /* This is correct */
443 cbstuff.nblks = nblks;
444 cbstuff.nbreakers = 0;
450 XCallBackBulk_r(struct host * ahost, struct AFSFid * fids, afs_int32 nfids)
452 struct AFSCallBack tcbs[AFSCBMAX];
458 struct rx_connection *cb_conn = NULL;
461 rx_SetConnDeadTime(ahost->callback_rxcon, 4);
462 rx_SetConnHardDeadTime(ahost->callback_rxcon, AFS_HARDDEADTIME);
469 for (i = 0; i < nfids && i < AFSCBMAX; i++) {
470 tcbs[i].CallBackVersion = CALLBACK_VERSION;
471 tcbs[i].ExpirationTime = 0;
472 tcbs[i].CallBackType = CB_DROPPED;
474 tf.AFSCBFids_len = i;
475 tf.AFSCBFids_val = &(fids[j]);
479 tc.AFSCBs_val = tcbs;
481 cb_conn = ahost->callback_rxcon;
482 rx_GetConnection(cb_conn);
484 code |= RXAFSCB_CallBack(cb_conn, &tf, &tc);
485 rx_PutConnection(cb_conn);
493 /* the locked flag tells us if the host entry has already been locked
494 * by our parent. I don't think anybody actually calls us with the
495 * host locked, but here's how to make that work: GetSomeSpace has to
496 * change so that it doesn't attempt to lock any hosts < "host". That
497 * means that it might be unable to free any objects, so it has to
498 * return an exit status. If it fails, then AddCallBack1 might fail,
499 * as well. If so, the host->ResetDone should probably be set to 0,
500 * and we probably don't want to return a callback promise to the
501 * cache manager, either. */
503 AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
511 if (!(host->hostFlags & HOSTDELETED))
512 retVal = AddCallBack1_r(host, fid, thead, type, 1);
522 AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
525 struct FileEntry *fe;
526 struct CallBack *cb = 0, *lastcb = 0;
527 struct FileEntry *newfe = 0;
528 afs_uint32 time_out = 0;
529 afs_uint32 *Thead = thead;
530 struct CallBack *newcb = 0;
533 cbstuff.AddCallBacks++;
537 /* allocate these guys first, since we can't call the allocator with
538 * the host structure locked -- or we might deadlock. However, we have
539 * to avoid races with FindFE... */
540 while (!(newcb = GetCB())) {
541 GetSomeSpace_r(host, locked);
543 while (!(newfe = GetFE())) { /* Get it now, so we don't have to call */
544 /* GetSomeSpace with the host locked, later. This might turn out to */
545 /* have been unneccessary, but that's actually kind of unlikely, since */
546 /* most files are not shared. */
547 GetSomeSpace_r(host, locked);
551 h_Lock_r(host); /* this can yield, so do it before we get any */
553 if (host->hostFlags & HOSTDELETED) {
561 if (type == CB_NORMAL) {
563 TimeCeiling(FT_ApproxTime() + TimeOut(fe ? fe->ncbs : 0) +
565 Thead = THead(CBtime(time_out));
566 } else if (type == CB_VOLUME) {
567 time_out = TimeCeiling((60 * 120 + FT_ApproxTime()) + ServerBias);
568 Thead = THead(CBtime(time_out));
569 } else if (type == CB_BULK) {
570 /* bulk status can get so many callbacks all at once, and most of them
571 * are probably not for things that will be used for long.
574 TimeCeiling(FT_ApproxTime() + ServerBias +
575 TimeOut(22 + (fe ? fe->ncbs : 0)));
576 Thead = THead(CBtime(time_out));
587 fe->volid = fid->Volume;
588 fe->vnode = fid->Vnode;
589 fe->unique = fid->Unique;
592 hash = FEHash(fid->Volume, fid->Unique);
593 fe->fnext = HashTable[hash];
594 HashTable[hash] = fetoi(fe);
596 for (safety = 0, lastcb = cb = itocb(fe->firstcb); cb;
597 lastcb = cb, cb = itocb(cb->cnext), safety++) {
598 if (safety > cbstuff.nblks) {
599 ViceLog(0, ("AddCallBack1: Internal Error -- shutting down.\n"));
600 DumpCallBackState_r();
601 ShutDownAndCore(PANIC);
603 if (cb->hhead == h_htoi(host))
606 if (cb) { /* Already have call back: move to new timeout list */
607 /* don't change delayed callbacks back to normal ones */
608 if (cb->status != CB_DELAYED)
610 /* Only move if new timeout is longer */
611 if (TNorm(ttoi(Thead)) > TNorm(cb->thead)) {
615 if (newfe == NULL) { /* we are using the new FE */
616 fe->firstcb = cbtoi(cb);
618 cb->fhead = fetoi(fe);
623 *(lastcb ? &lastcb->cnext : &fe->firstcb) = cbtoi(cb);
626 cb->fhead = fetoi(fe);
632 /* now free any still-unused callback or host entries */
638 if (!locked) /* freecb and freefe might(?) yield */
641 if (type == CB_NORMAL || type == CB_VOLUME || type == CB_BULK)
642 return time_out - ServerBias; /* Expires sooner at workstation */
648 CompareCBA(const void *e1, const void *e2)
650 const struct cbstruct *cba1 = (const struct cbstruct *)e1;
651 const struct cbstruct *cba2 = (const struct cbstruct *)e2;
652 return ((cba1->hp)->index - (cba2->hp)->index);
655 /* Take an array full of hosts, all held. Break callbacks to them, and
656 * release the holds once you're done.
657 * Currently only works for a single Fid in afidp array.
658 * If you want to make this work with multiple fids, you need to fix
659 * the error handling. One approach would be to force a reset if a
660 * multi-fid call fails, or you could add delayed callbacks for each
661 * fid. You probably also need to sort and remove duplicate hosts.
662 * When this is called from the BreakVolumeCallBacks path, it does NOT
663 * force a reset if the RPC fails, it just marks the host down and tries
664 * to create a delayed callback. */
665 /* N.B. be sure that code works when ncbas == 0 */
666 /* N.B. requires all the cba[*].hp pointers to be valid... */
667 /* This routine does not hold a lock on the host for the duration of
668 * the BreakCallBack RPC, which is a significant deviation from tradition.
669 * It _does_ get a lock on the host before setting VenusDown = 1,
670 * which is sufficient only if VenusDown = 0 only happens when the
671 * lock is held over the RPC and the subsequent VenusDown == 0
672 * wherever that is done. */
674 MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
675 struct AFSCBFids *afidp)
678 struct rx_connection *conns[MAX_CB_HOSTS];
679 static struct AFSCBs tc = { 0, 0 };
680 int multi_to_cba_map[MAX_CB_HOSTS];
682 osi_Assert(ncbas <= MAX_CB_HOSTS);
684 /* sort cba list to avoid makecall issues */
685 qsort(cba, ncbas, sizeof(struct cbstruct), CompareCBA);
687 /* set up conns for multi-call */
688 for (i = 0, j = 0; i < ncbas; i++) {
689 struct host *thishost = cba[i].hp;
690 if (!thishost || (thishost->hostFlags & HOSTDELETED)) {
693 rx_GetConnection(thishost->callback_rxcon);
694 multi_to_cba_map[j] = i;
695 conns[j++] = thishost->callback_rxcon;
698 rx_SetConnDeadTime(thishost->callback_rxcon, 4);
699 rx_SetConnHardDeadTime(thishost->callback_rxcon, AFS_HARDDEADTIME);
703 if (j) { /* who knows what multi would do with 0 conns? */
707 multi_RXAFSCB_CallBack(afidp, &tc);
713 i = multi_to_cba_map[multi_i];
719 ("BCB: INTERNAL ERROR: hp=%p, cba=%p, thead=%u\n",
723 ** try breaking callbacks on alternate interface addresses
725 if (MultiBreakCallBackAlternateAddress(hp, afidp)) {
728 ("BCB: Failed on file %u.%u.%u, "
729 "Host %p (%s:%d) is down\n",
730 afidp->AFSCBFids_val->Volume,
731 afidp->AFSCBFids_val->Vnode,
732 afidp->AFSCBFids_val->Unique,
734 afs_inet_ntoa_r(hp->host, hoststr),
740 if (!(hp->hostFlags & HOSTDELETED)) {
741 hp->hostFlags |= VENUSDOWN;
743 * We always go into AddCallBack1_r with the host locked
745 AddCallBack1_r(hp, afidp->AFSCBFids_val, itot(idx),
759 for (i = 0; i < ncbas; i++) {
767 /* H_UNLOCK around this so h_FreeConnection does not deadlock.
768 h_FreeConnection should *never* be called on a callback connection,
769 but on 10/27/04 a deadlock occurred where it was, when we know why,
770 this should be reverted. -- shadow */
772 for (i = 0; i < j; i++) {
773 rx_PutConnection(conns[i]);
781 * Break all call backs for fid, except for the specified host (unless flag
782 * is true, in which case all get a callback message. Assumption: the specified
783 * host is h_Held, by the caller; the others aren't.
784 * Specified host may be bogus, that's ok. This used to check to see if the
785 * host was down in two places, once right after the host was h_held, and
786 * again after it was locked. That race condition is incredibly rare and
787 * relatively harmless even when it does occur, so we don't check for it now.
789 /* if flag is true, send a break callback msg to "host", too */
791 BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
793 struct FileEntry *fe;
794 struct CallBack *cb, *nextcb;
795 struct cbstruct cbaDef[MAX_CB_HOSTS], *cba = cbaDef;
796 unsigned int ncbas, cbaAlloc = MAX_CB_HOSTS;
802 ("BCB: BreakCallBack(Host %p all but %s:%d, (%u,%u,%u))\n",
803 xhost, afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
804 fid->Volume, fid->Vnode, fid->Unique));
807 cbstuff.BreakCallBacks++;
812 hostindex = h_htoi(xhost);
813 cb = itocb(fe->firstcb);
814 if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
815 /* the most common case is what follows the || */
818 tf.AFSCBFids_len = 1;
819 tf.AFSCBFids_val = fid;
821 for (ncbas = 0; cb ; cb = nextcb) {
822 nextcb = itocb(cb->cnext);
823 if ((cb->hhead != hostindex || flag)
824 && (cb->status == CB_BULK || cb->status == CB_NORMAL
825 || cb->status == CB_VOLUME)) {
826 struct host *thishost = h_itoh(cb->hhead);
828 ViceLog(0, ("BCB: BOGUS! cb->hhead is NULL!\n"));
829 } else if (thishost->hostFlags & VENUSDOWN) {
831 ("BCB: %p (%s:%d) is down; delaying break call back\n",
832 thishost, afs_inet_ntoa_r(thishost->host, hoststr),
833 ntohs(thishost->port)));
834 cb->status = CB_DELAYED;
836 if (!(thishost->hostFlags & HOSTDELETED)) {
838 if (ncbas == cbaAlloc) { /* Need more space */
839 int curLen = cbaAlloc*sizeof(cba[0]);
840 struct cbstruct *cbaOld = (cba == cbaDef) ? NULL : cba;
842 /* There are logical contraints elsewhere that the number of hosts
843 (i.e. h_HTSPERBLOCK*h_MAXHOSTTABLES) remains in the realm of a signed "int".
844 cbaAlloc is defined unsigned int hence doubling below cannot overflow
846 cbaAlloc = cbaAlloc<<1; /* double */
847 cba = realloc(cbaOld, cbaAlloc * sizeof(cba[0]));
849 if (cbaOld == NULL) { /* realloc wouldn't have copied from cbaDef */
850 memcpy(cba, cbaDef, curLen);
853 cba[ncbas].hp = thishost;
854 cba[ncbas].thead = cb->thead;
859 CDel(cb, 1); /* Usually first; so this delete
860 * is reasonably inexpensive */
866 struct cbstruct *cba2;
869 for (cba2 = cba, num = ncbas; ncbas > 0; cba2 += num, ncbas -= num) {
870 num = (ncbas > MAX_CB_HOSTS) ? MAX_CB_HOSTS : ncbas;
871 MultiBreakCallBack_r(cba2, num, &tf);
875 if (cba != cbaDef) free(cba);
882 /* Delete (do not break) single call back for fid */
884 DeleteCallBack(struct host *host, AFSFid * fid)
886 struct FileEntry *fe;
891 cbstuff.DeleteCallBacks++;
894 /* do not care if the host has been HOSTDELETED */
900 ("DCB: No call backs for fid (%u, %u, %u)\n", fid->Volume,
901 fid->Vnode, fid->Unique));
904 pcb = FindCBPtr(fe, host);
907 ("DCB: No call back for host %p (%s:%d), (%u, %u, %u)\n",
908 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
909 fid->Volume, fid->Vnode, fid->Unique));
923 * Delete (do not break) all call backs for fid. This call doesn't
924 * set all of the various host locks, but it shouldn't really matter
925 * since we're not adding callbacks, but deleting them. I'm not sure
926 * why it doesn't set the lock, however; perhaps it should.
929 DeleteFileCallBacks(AFSFid * fid)
931 struct FileEntry *fe;
937 cbstuff.DeleteFiles++;
942 ("DF: No fid (%u,%u,%u) to delete\n", fid->Volume, fid->Vnode,
946 for (n = 0, cbi = fe->firstcb; cbi; n++) {
959 /* Delete (do not break) all call backs for host. The host should be
962 DeleteAllCallBacks_r(struct host *host, int deletefe)
967 cbstuff.DeleteAllCallBacks++;
968 cbi = first = host->cblist;
970 ViceLog(8, ("DV: no call backs\n"));
978 } while (cbi != first);
984 * Break all delayed call backs for host. Returns 1 if all call backs
985 * successfully broken; 0 otherwise. Assumes host is h_Held and h_Locked.
986 * Must be called with VenusDown set for this host
989 BreakDelayedCallBacks(struct host *host)
993 retVal = BreakDelayedCallBacks_r(host);
999 BreakDelayedCallBacks_r(struct host *host)
1001 struct AFSFid fids[AFSCBMAX];
1002 int cbi, first, nfids;
1003 struct CallBack *cb;
1006 struct rx_connection *cb_conn;
1008 cbstuff.nbreakers++;
1009 if (!(host->hostFlags & RESETDONE) && !(host->hostFlags & HOSTDELETED)) {
1010 host->hostFlags &= ~ALTADDR; /* alternate addresses are invalid */
1011 cb_conn = host->callback_rxcon;
1012 rx_GetConnection(cb_conn);
1013 if (host->interface) {
1016 RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
1019 code = RXAFSCB_InitCallBackState(cb_conn);
1021 rx_PutConnection(cb_conn);
1024 host->hostFlags |= ALTADDR; /* alternate addresses are valid */
1028 ("CB: Call back connect back failed (in break delayed) "
1029 "for Host %p (%s:%d)\n",
1030 host, afs_inet_ntoa_r(host->host, hoststr),
1031 ntohs(host->port)));
1033 host->hostFlags |= VENUSDOWN;
1036 ("InitCallBackState success on %p (%s:%d)\n",
1037 host, afs_inet_ntoa_r(host->host, hoststr),
1038 ntohs(host->port)));
1039 /* reset was done successfully */
1040 host->hostFlags |= RESETDONE;
1041 host->hostFlags &= ~VENUSDOWN;
1044 while (!(host->hostFlags & HOSTDELETED)) {
1046 host->hostFlags &= ~VENUSDOWN; /* presume up */
1047 cbi = first = host->cblist;
1051 first = host->cblist;
1054 if (cb->status == CB_DELAYED) {
1055 struct FileEntry *fe = itofe(cb->fhead);
1056 fids[nfids].Volume = fe->volid;
1057 fids[nfids].Vnode = fe->vnode;
1058 fids[nfids].Unique = fe->unique;
1064 } while (cbi && cbi != first && nfids < AFSCBMAX);
1070 if (XCallBackBulk_r(host, fids, nfids)) {
1071 /* Failed, again: put them back, probably with old
1076 ("CB: XCallBackBulk failed, Host %p (%s:%d); "
1077 "callback list follows:\n",
1078 host, afs_inet_ntoa_r(host->host, hoststr),
1079 ntohs(host->port)));
1081 for (i = 0; i < nfids; i++) {
1084 ("CB: Host %p (%s:%d), file %u.%u.%u "
1085 "(part of bulk callback)\n",
1086 host, afs_inet_ntoa_r(host->host, hoststr),
1087 ntohs(host->port), fids[i].Volume,
1088 fids[i].Vnode, fids[i].Unique));
1091 * AddCallBack1_r(host, &fids[i], itot(thead[i]), CB_DELAYED, 1);
1092 * * but it turns out to cause too many tricky locking problems.
1093 * * now, if break delayed fails, screw it. */
1095 host->hostFlags |= VENUSDOWN; /* Failed */
1096 ClearHostCallbacks_r(host, 1 /* locked */ );
1100 if (nfids < AFSCBMAX)
1104 cbstuff.nbreakers--;
1105 /* If we succeeded it's always ok to unset HFE_LATER */
1106 if (!(host->hostFlags & VENUSDOWN))
1107 host->hostFlags &= ~HFE_LATER;
1108 return (host->hostFlags & VENUSDOWN);
1112 MultiBreakVolumeCallBack_r(struct host *host,
1113 struct VCBParams *parms, int deletefe)
1117 if (host->hostFlags & HOSTDELETED)
1120 if (!(host->hostFlags & HCBREAK))
1121 return 0; /* host is not flagged to notify */
1123 if (host->hostFlags & VENUSDOWN) {
1125 /* Do not care if the host is now HOSTDELETED */
1128 ("BVCB: volume callback for Host %p (%s:%d) failed\n",
1129 host, afs_inet_ntoa_r(host->host, hoststr),
1130 ntohs(host->port)));
1132 DeleteAllCallBacks_r(host, deletefe); /* Delete all callback state
1133 * rather than attempting to
1134 * selectively remember to
1135 * delete the volume callbacks
1137 host->hostFlags &= ~(RESETDONE|HCBREAK); /* Do InitCallBackState when host returns */
1141 osi_Assert(parms->ncbas <= MAX_CB_HOSTS);
1143 /* Do not call MultiBreakCallBack on the current host structure
1144 ** because it would prematurely release the hold on the host
1146 if (parms->ncbas == MAX_CB_HOSTS) {
1147 struct AFSCBFids tf;
1149 tf.AFSCBFids_len = 1;
1150 tf.AFSCBFids_val = parms->fid;
1152 /* this releases all the hosts */
1153 MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf);
1157 parms->cba[parms->ncbas].hp = host;
1158 parms->cba[(parms->ncbas)++].thead = parms->thead;
1159 host->hostFlags &= ~HCBREAK;
1161 /* we have more work to do on this host, so make sure we keep a reference
1169 MultiBreakVolumeLaterCallBack(struct host *host, void *rock)
1171 struct VCBParams *parms = (struct VCBParams *)rock;
1174 retval = MultiBreakVolumeCallBack_r(host, parms, 0);
1180 * Break all call backs on a single volume. Don't call this with any
1181 * hosts h_held. Note that this routine clears the callbacks before
1182 * actually breaking them, and that the vnode isn't locked during this
1183 * operation, so that people might see temporary callback loss while
1184 * this function is executing. It is just a temporary state, however,
1185 * since the callback will be broken later by this same function.
1187 * Now uses multi-RX for CallBack RPC in a different thread,
1188 * only marking them here.
1190 #ifdef AFS_PTHREAD_ENV
1191 extern pthread_cond_t fsync_cond;
1193 extern char fsync_wait[];
1197 BreakVolumeCallBacksLater(afs_uint32 volume)
1201 struct FileEntry *fe;
1202 struct CallBack *cb;
1206 ViceLog(25, ("Setting later on volume %u\n", volume));
1208 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1209 for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
1210 if (fe->volid == volume) {
1211 struct CallBack *cbnext;
1212 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1213 host = h_itoh(cb->hhead);
1214 host->hostFlags |= HFE_LATER;
1215 cb->status = CB_DELAYED;
1216 cbnext = itocb(cb->cnext);
1219 fe->status |= FE_LATER;
1228 /* didn't find any callbacks, so return right away. */
1232 ViceLog(25, ("Fsync thread wakeup\n"));
1233 #ifdef AFS_PTHREAD_ENV
1235 CV_BROADCAST(&fsync_cond);
1238 LWP_NoYieldSignal(fsync_wait);
1244 BreakLaterCallBacks(void)
1249 struct CallBack *cb;
1250 struct FileEntry *fe = NULL;
1251 struct FileEntry *myfe = NULL;
1253 struct VCBParams henumParms;
1254 unsigned short tthead = 0; /* zero is illegal value */
1258 ViceLog(25, ("Looking for FileEntries to unchain\n"));
1261 /* Pick the first volume we see to clean up */
1262 fid.Volume = fid.Vnode = fid.Unique = 0;
1264 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1265 for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
1266 if (fe && (fe->status & FE_LATER)
1267 && (fid.Volume == 0 || fid.Volume == fe->volid)) {
1268 /* Ugly, but used to avoid left side casting */
1269 struct object *tmpfe;
1271 ("Unchaining for %u:%u:%u\n", fe->vnode, fe->unique,
1273 fid.Volume = fe->volid;
1275 fe->status &= ~FE_LATER; /* not strictly needed */
1276 /* Works since volid is deeper than the largest pointer */
1277 tmpfe = (struct object *)fe;
1278 tmpfe->next = (struct object *)myfe;
1291 /* loop over FEs from myfe and free/break */
1293 for (fe = myfe; fe;) {
1294 struct CallBack *cbnext;
1295 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1296 cbnext = itocb(cb->cnext);
1297 host = h_itoh(cb->hhead);
1298 if (cb->status == CB_DELAYED) {
1299 if (!(host->hostFlags & HOSTDELETED)) {
1300 /* mark this host for notification */
1301 host->hostFlags |= HCBREAK;
1302 if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
1308 CDel(cb, 0); /* Don't let CDel clean up the fe */
1309 /* leave flag for MultiBreakVolumeCallBack to clear */
1312 ("Found host %p (%s:%d) non-DELAYED cb for %u:%u:%u\n",
1313 host, afs_inet_ntoa_r(host->host, hoststr),
1314 ntohs(host->port), fe->vnode, fe->unique, fe->volid));
1318 fe = (struct FileEntry *)((struct object *)fe)->next;
1323 ViceLog(125, ("Breaking volume %u\n", fid.Volume));
1324 henumParms.ncbas = 0;
1325 henumParms.fid = &fid;
1326 henumParms.thead = tthead;
1328 h_Enumerate(MultiBreakVolumeLaterCallBack, (char *)&henumParms);
1330 if (henumParms.ncbas) { /* do left-overs */
1331 struct AFSCBFids tf;
1332 tf.AFSCBFids_len = 1;
1333 tf.AFSCBFids_val = &fid;
1335 MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf);
1336 henumParms.ncbas = 0;
1341 /* Arrange to be called again */
1346 * Delete all timed-out call back entries (to be called periodically by file
1350 CleanupTimedOutCallBacks(void)
1353 CleanupTimedOutCallBacks_r();
1359 CleanupTimedOutCallBacks_r(void)
1361 afs_uint32 now = CBtime(FT_ApproxTime());
1363 struct CallBack *cb;
1367 while (tfirst <= now) {
1369 cbi = *(thead = THead(tfirst));
1375 ("CCB: deleting timed out call back %x (%s:%d), (%u,%u,%u)\n",
1376 h_itoh(cb->hhead)->host,
1377 afs_inet_ntoa_r(h_itoh(cb->hhead)->host, hoststr),
1378 h_itoh(cb->hhead)->port, itofe(cb->fhead)->volid,
1379 itofe(cb->fhead)->vnode, itofe(cb->fhead)->unique));
1383 if (ntimedout > cbstuff.nblks) {
1384 ViceLog(0, ("CCB: Internal Error -- shutting down...\n"));
1385 DumpCallBackState_r();
1386 ShutDownAndCore(PANIC);
1388 } while (cbi != *thead);
1393 cbstuff.CBsTimedOut += ntimedout;
1394 ViceLog(7, ("CCB: deleted %d timed out callbacks\n", ntimedout));
1395 return (ntimedout > 0);
1399 * parameters to pass to lih*_r from h_Enumerate_r when trying to find a host
1400 * from which to clear callbacks.
1404 * Points to the least interesting host found; try to clear callbacks on
1405 * this host after h_Enumerate_r(lih*_r)'ing.
1410 * The last host we got from lih*_r, but we couldn't clear its callbacks
1411 * for some reason. Choose the next-best host after this one (with the
1412 * current lih*_r, this means to only select hosts that have an ActiveCall
1413 * newer than lastlih).
1415 struct host *lastlih;
1418 /* Value of host->refCount that allows us to reliably infer that
1419 * host may be held by some other thread */
1420 #define OTHER_MUSTHOLD_LIH 2
1422 /* This version does not allow 'host' to be selected unless its ActiveCall
1423 * is newer than 'params->lastlih' which is the host with the oldest
1424 * ActiveCall from the last pass (if it is provided). We filter out any hosts
1425 * that are are held by other threads.
1427 * There is a small problem here, but it may not be easily fixable. Say we
1428 * select some host A, and give it back to GetSomeSpace_r. GSS_r for some
1429 * reason cannot clear the callbacks on A, and so calls us again with
1430 * lastlih = A. Suppose there is another host B that has the same ActiveCall
1431 * time as A. We will now skip over host B, since
1432 * 'hostB->ActiveCall > hostA->ActiveCall' is not true. This could result in
1433 * us prematurely going to the GSS_r 2nd or 3rd pass, and making us a little
1434 * inefficient. This should be pretty rare, though, except perhaps in cases
1435 * with very small numbers of hosts.
1437 * Also filter out any hosts with HOSTDELETED set. h_Enumerate_r should in
1438 * theory not give these to us anyway, but be paranoid.
1441 lih0_r(struct host *host, void *rock)
1443 struct lih_params *params = (struct lih_params *)rock;
1445 /* OTHER_MUSTHOLD_LIH is because the h_Enum loop holds us once */
1447 && (!(host->hostFlags & HOSTDELETED))
1448 && (host->refCount < OTHER_MUSTHOLD_LIH)
1449 && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
1450 && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
1453 h_Release_r(params->lih); /* release prev host */
1462 /* same as lih0_r, except we do not prevent held hosts from being selected. */
1464 lih1_r(struct host *host, void *rock)
1466 struct lih_params *params = (struct lih_params *)rock;
1469 && (!(host->hostFlags & HOSTDELETED))
1470 && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
1471 && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
1474 h_Release_r(params->lih); /* release prev host */
1483 /* This could be upgraded to get more space each time */
1484 /* first pass: sequentially find the oldest host which isn't held by
1485 anyone for which we can clear callbacks;
1487 /* second pass: sequentially find the oldest host regardless of
1488 whether or not the host is held; skipping 'hostp' */
1489 /* third pass: attempt to clear callbacks from 'hostp' */
1490 /* always called with hostp unlocked */
1492 /* Note: hostlist is ordered most recently created host first and
1493 * its order has no relationship to the most recently used. */
1494 extern struct host *hostList;
1496 GetSomeSpace_r(struct host *hostp, int locked)
1499 struct lih_params params;
1502 cbstuff.GotSomeSpaces++;
1504 ("GSS: First looking for timed out call backs via CleanupCallBacks\n"));
1505 if (CleanupTimedOutCallBacks_r()) {
1511 params.lastlih = NULL;
1516 h_Enumerate_r(i == 0 ? lih0_r : lih1_r, hostList, ¶ms);
1519 if (params.lastlih) {
1520 h_Release_r(params.lastlih);
1521 params.lastlih = NULL;
1525 /* note that 'hp' was held by lih*_r; we will need to release it */
1527 if ((hp != hostp) && !ClearHostCallbacks_r(hp, 0 /* not locked or held */ )) {
1532 params.lastlih = hp;
1533 /* params.lastlih will be released on the next iteration, after
1538 * Next time try getting callbacks from any host even if
1539 * it's held, since the only other option is starvation for
1540 * the file server (i.e. until the callback timeout arrives).
1543 params.lastlih = NULL;
1546 ("GSS: Try harder for longest inactive host cnt= %d\n",
1551 /* Could not obtain space from other hosts, clear hostp's callback state */
1556 ClearHostCallbacks_r(hostp, 1 /*already locked */ );
1563 /* locked - set if caller has already locked the host */
1565 ClearHostCallbacks_r(struct host *hp, int locked)
1569 struct rx_connection *cb_conn = NULL;
1572 ("GSS: Delete longest inactive host %p (%s:%d)\n",
1573 hp, afs_inet_ntoa_r(hp->host, hoststr), ntohs(hp->port)));
1575 if ((hp->hostFlags & HOSTDELETED)) {
1576 /* hp could go away after reacquiring H_LOCK in h_NBLock_r, so we can't
1577 * really use it; its callbacks will get cleared anyway when
1578 * h_TossStuff_r gets its hands on it */
1584 /** Try a non-blocking lock. If the lock is already held return
1585 * after releasing hold on hp
1588 if (h_NBLock_r(hp)) {
1593 if (hp->Console & 2) {
1595 * If the special console field is set it means that a thread
1596 * is waiting in AddCallBack1 after it set pointers to the
1597 * file entry and/or callback entry. Because of the bogus
1598 * usage of h_hold it won't prevent from another thread, this
1599 * one, to remove all the callbacks so just to be safe we keep
1600 * a reference. NOTE, on the last phase we'll free the calling
1601 * host's callbacks but that's ok...
1605 DeleteAllCallBacks_r(hp, 1);
1606 if (hp->hostFlags & VENUSDOWN) {
1607 hp->hostFlags &= ~RESETDONE; /* remember that we must do a reset */
1608 } else if (!(hp->hostFlags & HOSTDELETED)) {
1609 /* host is up, try a call */
1610 hp->hostFlags &= ~ALTADDR; /* alternate addresses are invalid */
1611 cb_conn = hp->callback_rxcon;
1612 rx_GetConnection(hp->callback_rxcon);
1613 if (hp->interface) {
1616 RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
1619 code = RXAFSCB_InitCallBackState(cb_conn);
1621 rx_PutConnection(cb_conn);
1624 hp->hostFlags |= ALTADDR; /* alternate addresses are valid */
1626 /* failed, mark host down and need reset */
1627 hp->hostFlags |= VENUSDOWN;
1628 hp->hostFlags &= ~RESETDONE;
1630 /* reset succeeded, we're done */
1631 hp->hostFlags |= RESETDONE;
1640 #endif /* INTERPRET_DUMP */
1644 PrintCallBackStats(void)
1647 "%d add CB, %d break CB, %d del CB, %d del FE, %d CB's timed out, %d space reclaim, %d del host\n",
1648 cbstuff.AddCallBacks, cbstuff.BreakCallBacks,
1649 cbstuff.DeleteCallBacks, cbstuff.DeleteFiles, cbstuff.CBsTimedOut,
1650 cbstuff.GotSomeSpaces, cbstuff.DeleteAllCallBacks);
1651 fprintf(stderr, "%d CBs, %d FEs, (%d of total of %d 16-byte blocks)\n",
1652 cbstuff.nCBs, cbstuff.nFEs, cbstuff.nCBs + cbstuff.nFEs,
1654 fprintf(stderr, "%d GSS1, %d GSS2, %d GSS3, %d GSS4, %d GSS5 (internal counters)\n",
1655 cbstuff.GSS1, cbstuff.GSS2, cbstuff.GSS3, cbstuff.GSS4, cbstuff.GSS5);
1660 #define MAGIC 0x12345678 /* To check byte ordering of dump when it is read in */
1661 #define MAGICV2 0x12345679 /* To check byte ordering & version of dump when it is read in */
1664 #ifndef INTERPRET_DUMP
1666 #ifdef AFS_DEMAND_ATTACH_FS
1669 * callback state serialization
1671 static int cb_stateSaveTimeouts(struct fs_dump_state * state);
1672 static int cb_stateSaveFEHash(struct fs_dump_state * state);
1673 static int cb_stateSaveFEs(struct fs_dump_state * state);
1674 static int cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe);
1675 static int cb_stateRestoreTimeouts(struct fs_dump_state * state);
1676 static int cb_stateRestoreFEHash(struct fs_dump_state * state);
1677 static int cb_stateRestoreFEs(struct fs_dump_state * state);
1678 static int cb_stateRestoreFE(struct fs_dump_state * state);
1679 static int cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
1680 struct iovec * iov, int niovecs);
1682 static int cb_stateVerifyFEHash(struct fs_dump_state * state);
1683 static int cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe);
1684 static int cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe);
1685 static int cb_stateVerifyTimeoutQueues(struct fs_dump_state * state);
1687 static int cb_stateFEToDiskEntry(struct FileEntry *, struct FEDiskEntry *);
1688 static int cb_stateDiskEntryToFE(struct fs_dump_state * state,
1689 struct FEDiskEntry *, struct FileEntry *);
1691 static int cb_stateCBToDiskEntry(struct CallBack *, struct CBDiskEntry *);
1692 static int cb_stateDiskEntryToCB(struct fs_dump_state * state,
1693 struct CBDiskEntry *, struct CallBack *);
1695 static int cb_stateFillHeader(struct callback_state_header * hdr);
1696 static int cb_stateCheckHeader(struct callback_state_header * hdr);
1698 static int cb_stateAllocMap(struct fs_dump_state * state);
1701 cb_stateSave(struct fs_dump_state * state)
1705 AssignInt64(state->eof_offset, &state->hdr->cb_offset);
1707 /* invalidate callback state header */
1708 memset(state->cb_hdr, 0, sizeof(struct callback_state_header));
1709 if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1710 sizeof(struct callback_state_header))) {
1715 fs_stateIncEOF(state, sizeof(struct callback_state_header));
1717 /* dump timeout state */
1718 if (cb_stateSaveTimeouts(state)) {
1723 /* dump fe hashtable state */
1724 if (cb_stateSaveFEHash(state)) {
1729 /* dump callback state */
1730 if (cb_stateSaveFEs(state)) {
1735 /* write the callback state header to disk */
1736 cb_stateFillHeader(state->cb_hdr);
1737 if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1738 sizeof(struct callback_state_header))) {
1748 cb_stateRestore(struct fs_dump_state * state)
1752 if (fs_stateReadHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1753 sizeof(struct callback_state_header))) {
1758 if (cb_stateCheckHeader(state->cb_hdr)) {
1763 if (cb_stateAllocMap(state)) {
1768 if (cb_stateRestoreTimeouts(state)) {
1773 if (cb_stateRestoreFEHash(state)) {
1778 /* restore FEs and CBs from disk */
1779 if (cb_stateRestoreFEs(state)) {
1784 /* restore the timeout queue heads */
1785 tfirst = state->cb_hdr->tfirst;
1792 cb_stateRestoreIndices(struct fs_dump_state * state)
1795 struct FileEntry * fe;
1796 struct CallBack * cb;
1798 /* restore indices in the FileEntry structures */
1799 for (i = 1; i < state->fe_map.len; i++) {
1800 if (state->fe_map.entries[i].new_idx) {
1801 fe = itofe(state->fe_map.entries[i].new_idx);
1803 /* restore the fe->fnext entry */
1804 if (fe_OldToNew(state, fe->fnext, &fe->fnext)) {
1809 /* restore the fe->firstcb entry */
1810 if (cb_OldToNew(state, fe->firstcb, &fe->firstcb)) {
1817 /* restore indices in the CallBack structures */
1818 for (i = 1; i < state->cb_map.len; i++) {
1819 if (state->cb_map.entries[i].new_idx) {
1820 cb = itocb(state->cb_map.entries[i].new_idx);
1822 /* restore the cb->cnext entry */
1823 if (cb_OldToNew(state, cb->cnext, &cb->cnext)) {
1828 /* restore the cb->fhead entry */
1829 if (fe_OldToNew(state, cb->fhead, &cb->fhead)) {
1834 /* restore the cb->hhead entry */
1835 if (h_OldToNew(state, cb->hhead, &cb->hhead)) {
1840 /* restore the cb->tprev entry */
1841 if (cb_OldToNew(state, cb->tprev, &cb->tprev)) {
1846 /* restore the cb->tnext entry */
1847 if (cb_OldToNew(state, cb->tnext, &cb->tnext)) {
1852 /* restore the cb->hprev entry */
1853 if (cb_OldToNew(state, cb->hprev, &cb->hprev)) {
1858 /* restore the cb->hnext entry */
1859 if (cb_OldToNew(state, cb->hnext, &cb->hnext)) {
1866 /* restore the timeout queue head indices */
1867 for (i = 0; i < state->cb_timeout_hdr->records; i++) {
1868 if (cb_OldToNew(state, timeout[i], &timeout[i])) {
1874 /* restore the FE hash table queue heads */
1875 for (i = 0; i < state->cb_fehash_hdr->records; i++) {
1876 if (fe_OldToNew(state, HashTable[i], &HashTable[i])) {
1887 cb_stateVerify(struct fs_dump_state * state)
1891 if (cb_stateVerifyFEHash(state)) {
1895 if (cb_stateVerifyTimeoutQueues(state)) {
1903 cb_stateVerifyFEHash(struct fs_dump_state * state)
1906 struct FileEntry * fe;
1907 afs_uint32 fei, chain_len;
1909 for (i = 0; i < FEHASH_SIZE; i++) {
1911 for (fei = HashTable[i], fe = itofe(fei);
1913 fei = fe->fnext, fe = itofe(fei)) {
1914 if (fei > cbstuff.nblks) {
1915 ViceLog(0, ("cb_stateVerifyFEHash: error: index out of range (fei=%d)\n", fei));
1919 if (cb_stateVerifyFE(state, fe)) {
1922 if (chain_len > FS_STATE_FE_MAX_HASH_CHAIN_LEN) {
1923 ViceLog(0, ("cb_stateVerifyFEHash: error: hash chain %d length exceeds %d; assuming there's a loop\n",
1924 i, FS_STATE_FE_MAX_HASH_CHAIN_LEN));
1936 cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe)
1940 if ((fe->firstcb && !fe->ncbs) ||
1941 (!fe->firstcb && fe->ncbs)) {
1942 ViceLog(0, ("cb_stateVerifyFE: error: fe->firstcb does not agree with fe->ncbs (fei=%lu, fe->firstcb=%lu, fe->ncbs=%lu)\n",
1943 afs_printable_uint32_lu(fetoi(fe)),
1944 afs_printable_uint32_lu(fe->firstcb),
1945 afs_printable_uint32_lu(fe->ncbs)));
1948 if (cb_stateVerifyFCBList(state, fe)) {
1949 ViceLog(0, ("cb_stateVerifyFE: error: FCBList failed verification (fei=%lu)\n",
1950 afs_printable_uint32_lu(fetoi(fe))));
1958 cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe)
1961 afs_uint32 cbi, fei, chain_len = 0;
1962 struct CallBack * cb;
1966 for (cbi = fe->firstcb, cb = itocb(cbi);
1968 cbi = cb->cnext, cb = itocb(cbi)) {
1969 if (cbi > cbstuff.nblks) {
1970 ViceLog(0, ("cb_stateVerifyFCBList: error: list index out of range (cbi=%d, ncbs=%d)\n",
1971 cbi, cbstuff.nblks));
1975 if (cb->fhead != fei) {
1976 ViceLog(0, ("cb_stateVerifyFCBList: error: cb->fhead != fei (fei=%d, cb->fhead=%d)\n",
1980 if (chain_len > FS_STATE_FCB_MAX_LIST_LEN) {
1981 ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (fei=%d); assuming there's a loop\n",
1982 FS_STATE_FCB_MAX_LIST_LEN, fei));
1989 if (fe->ncbs != chain_len) {
1990 ViceLog(0, ("cb_stateVerifyFCBList: error: list length mismatch (len=%d, fe->ncbs=%d)\n",
1991 chain_len, fe->ncbs));
2000 cb_stateVerifyHCBList(struct fs_dump_state * state, struct host * host)
2003 afs_uint32 hi, chain_len, cbi;
2004 struct CallBack *cb, *ncb;
2009 for (cbi = host->cblist, cb = itocb(cbi);
2011 cbi = cb->hnext, cb = ncb) {
2012 if (chain_len && (host->cblist == cbi)) {
2013 /* we've wrapped around the circular list, and everything looks ok */
2016 if (cb->hhead != hi) {
2017 ViceLog(0, ("cb_stateVerifyHCBList: error: incorrect cb->hhead (cbi=%d, h->index=%d, cb->hhead=%d)\n",
2018 cbi, hi, cb->hhead));
2021 if (!cb->hprev || !cb->hnext) {
2022 ViceLog(0, ("cb_stateVerifyHCBList: error: null index in circular list (cbi=%d, h->index=%d)\n",
2027 if ((cb->hprev > cbstuff.nblks) ||
2028 (cb->hnext > cbstuff.nblks)) {
2029 ViceLog(0, ("cb_stateVerifyHCBList: error: list index out of range (cbi=%d, h->index=%d, cb->hprev=%d, cb->hnext=%d, nCBs=%d)\n",
2030 cbi, hi, cb->hprev, cb->hnext, cbstuff.nblks));
2034 ncb = itocb(cb->hnext);
2035 if (cbi != ncb->hprev) {
2036 ViceLog(0, ("cb_stateVerifyHCBList: error: corrupt linked list (cbi=%d, h->index=%d)\n",
2041 if (chain_len > FS_STATE_HCB_MAX_LIST_LEN) {
2042 ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (h->index=%d); assuming there's a loop\n",
2043 FS_STATE_HCB_MAX_LIST_LEN, hi));
2055 cb_stateVerifyTimeoutQueues(struct fs_dump_state * state)
2058 afs_uint32 cbi, chain_len;
2059 struct CallBack *cb, *ncb;
2061 for (i = 0; i < CB_NUM_TIMEOUT_QUEUES; i++) {
2063 for (cbi = timeout[i], cb = itocb(cbi);
2065 cbi = cb->tnext, cb = ncb) {
2066 if (chain_len && (cbi == timeout[i])) {
2067 /* we've wrapped around the circular list, and everything looks ok */
2070 if (cbi > cbstuff.nblks) {
2071 ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: list index out of range (cbi=%d, tindex=%d)\n",
2076 if (itot(cb->thead) != &timeout[i]) {
2077 ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: cb->thead points to wrong timeout queue (tindex=%d, cbi=%d, cb->thead=%d)\n",
2078 i, cbi, cb->thead));
2081 if (!cb->tprev || !cb->tnext) {
2082 ViceLog(0, ("cb_stateVerifyTimeoutQueues: null index in circular list (cbi=%d, tindex=%d)\n",
2087 if ((cb->tprev > cbstuff.nblks) ||
2088 (cb->tnext > cbstuff.nblks)) {
2089 ViceLog(0, ("cb_stateVerifyTimeoutQueues: list index out of range (cbi=%d, tindex=%d, cb->tprev=%d, cb->tnext=%d, nCBs=%d)\n",
2090 cbi, i, cb->tprev, cb->tnext, cbstuff.nblks));
2094 ncb = itocb(cb->tnext);
2095 if (cbi != ncb->tprev) {
2096 ViceLog(0, ("cb_stateVerifyTimeoutQueues: corrupt linked list (cbi=%d, tindex=%d)\n",
2101 if (chain_len > FS_STATE_TCB_MAX_LIST_LEN) {
2102 ViceLog(0, ("cb_stateVerifyTimeoutQueues: list length exceeds %d (tindex=%d); assuming there's a loop\n",
2103 FS_STATE_TCB_MAX_LIST_LEN, i));
2115 cb_stateSaveTimeouts(struct fs_dump_state * state)
2118 struct iovec iov[2];
2120 AssignInt64(state->eof_offset, &state->cb_hdr->timeout_offset);
2122 memset(state->cb_timeout_hdr, 0, sizeof(struct callback_state_fehash_header));
2123 state->cb_timeout_hdr->magic = CALLBACK_STATE_TIMEOUT_MAGIC;
2124 state->cb_timeout_hdr->records = CB_NUM_TIMEOUT_QUEUES;
2125 state->cb_timeout_hdr->len = sizeof(struct callback_state_timeout_header) +
2126 (state->cb_timeout_hdr->records * sizeof(afs_uint32));
2128 iov[0].iov_base = (char *)state->cb_timeout_hdr;
2129 iov[0].iov_len = sizeof(struct callback_state_timeout_header);
2130 iov[1].iov_base = (char *)timeout;
2131 iov[1].iov_len = sizeof(timeout);
2133 if (fs_stateSeek(state, &state->cb_hdr->timeout_offset)) {
2138 if (fs_stateWriteV(state, iov, 2)) {
2143 fs_stateIncEOF(state, state->cb_timeout_hdr->len);
2150 cb_stateRestoreTimeouts(struct fs_dump_state * state)
2154 if (fs_stateReadHeader(state, &state->cb_hdr->timeout_offset,
2155 state->cb_timeout_hdr,
2156 sizeof(struct callback_state_timeout_header))) {
2161 if (state->cb_timeout_hdr->magic != CALLBACK_STATE_TIMEOUT_MAGIC) {
2165 if (state->cb_timeout_hdr->records != CB_NUM_TIMEOUT_QUEUES) {
2170 len = state->cb_timeout_hdr->records * sizeof(afs_uint32);
2172 if (state->cb_timeout_hdr->len !=
2173 (sizeof(struct callback_state_timeout_header) + len)) {
2178 if (fs_stateRead(state, timeout, len)) {
2188 cb_stateSaveFEHash(struct fs_dump_state * state)
2191 struct iovec iov[2];
2193 AssignInt64(state->eof_offset, &state->cb_hdr->fehash_offset);
2195 memset(state->cb_fehash_hdr, 0, sizeof(struct callback_state_fehash_header));
2196 state->cb_fehash_hdr->magic = CALLBACK_STATE_FEHASH_MAGIC;
2197 state->cb_fehash_hdr->records = FEHASH_SIZE;
2198 state->cb_fehash_hdr->len = sizeof(struct callback_state_fehash_header) +
2199 (state->cb_fehash_hdr->records * sizeof(afs_uint32));
2201 iov[0].iov_base = (char *)state->cb_fehash_hdr;
2202 iov[0].iov_len = sizeof(struct callback_state_fehash_header);
2203 iov[1].iov_base = (char *)HashTable;
2204 iov[1].iov_len = sizeof(HashTable);
2206 if (fs_stateSeek(state, &state->cb_hdr->fehash_offset)) {
2211 if (fs_stateWriteV(state, iov, 2)) {
2216 fs_stateIncEOF(state, state->cb_fehash_hdr->len);
2223 cb_stateRestoreFEHash(struct fs_dump_state * state)
2227 if (fs_stateReadHeader(state, &state->cb_hdr->fehash_offset,
2228 state->cb_fehash_hdr,
2229 sizeof(struct callback_state_fehash_header))) {
2234 if (state->cb_fehash_hdr->magic != CALLBACK_STATE_FEHASH_MAGIC) {
2238 if (state->cb_fehash_hdr->records != FEHASH_SIZE) {
2243 len = state->cb_fehash_hdr->records * sizeof(afs_uint32);
2245 if (state->cb_fehash_hdr->len !=
2246 (sizeof(struct callback_state_fehash_header) + len)) {
2251 if (fs_stateRead(state, HashTable, len)) {
2261 cb_stateSaveFEs(struct fs_dump_state * state)
2265 struct FileEntry *fe;
2267 AssignInt64(state->eof_offset, &state->cb_hdr->fe_offset);
2269 for (hash = 0; hash < FEHASH_SIZE ; hash++) {
2270 for (fei = HashTable[hash]; fei; fei = fe->fnext) {
2272 if (cb_stateSaveFE(state, fe)) {
2284 cb_stateRestoreFEs(struct fs_dump_state * state)
2286 int count, nFEs, ret = 0;
2288 nFEs = state->cb_hdr->nFEs;
2290 for (count = 0; count < nFEs; count++) {
2291 if (cb_stateRestoreFE(state)) {
2302 cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
2304 int ret = 0, iovcnt, cbi, written = 0;
2306 struct callback_state_entry_header hdr;
2307 struct FEDiskEntry fedsk;
2308 struct CBDiskEntry cbdsk[16];
2309 struct iovec iov[16];
2310 struct CallBack *cb;
2313 if (fei > state->cb_hdr->fe_max) {
2314 state->cb_hdr->fe_max = fei;
2317 memset(&hdr, 0, sizeof(struct callback_state_entry_header));
2319 if (cb_stateFEToDiskEntry(fe, &fedsk)) {
2324 iov[0].iov_base = (char *)&hdr;
2325 iov[0].iov_len = sizeof(hdr);
2326 iov[1].iov_base = (char *)&fedsk;
2327 iov[1].iov_len = sizeof(struct FEDiskEntry);
2330 for (cbi = fe->firstcb, cb = itocb(cbi);
2332 cbi = cb->cnext, cb = itocb(cbi), hdr.nCBs++) {
2333 if (cbi > state->cb_hdr->cb_max) {
2334 state->cb_hdr->cb_max = cbi;
2336 if (cb_stateCBToDiskEntry(cb, &cbdsk[iovcnt])) {
2340 cbdsk[iovcnt].index = cbi;
2341 iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
2342 iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
2344 if ((iovcnt == 16) || (!cb->cnext)) {
2345 if (fs_stateWriteV(state, iov, iovcnt)) {
2354 hdr.magic = CALLBACK_STATE_ENTRY_MAGIC;
2355 hdr.len = sizeof(hdr) + sizeof(struct FEDiskEntry) +
2356 (hdr.nCBs * sizeof(struct CBDiskEntry));
2359 if (fs_stateWriteV(state, iov, iovcnt)) {
2364 if (fs_stateWriteHeader(state, &state->eof_offset, &hdr, sizeof(hdr))) {
2370 fs_stateIncEOF(state, hdr.len);
2373 if (fs_stateSeek(state, &state->eof_offset)) {
2379 state->cb_hdr->nFEs++;
2380 state->cb_hdr->nCBs += hdr.nCBs;
2387 cb_stateRestoreFE(struct fs_dump_state * state)
2389 int ret = 0, iovcnt, nCBs;
2390 struct callback_state_entry_header hdr;
2391 struct FEDiskEntry fedsk;
2392 struct CBDiskEntry cbdsk[16];
2393 struct iovec iov[16];
2394 struct FileEntry * fe;
2396 iov[0].iov_base = (char *)&hdr;
2397 iov[0].iov_len = sizeof(hdr);
2398 iov[1].iov_base = (char *)&fedsk;
2399 iov[1].iov_len = sizeof(fedsk);
2402 if (fs_stateReadV(state, iov, iovcnt)) {
2407 if (hdr.magic != CALLBACK_STATE_ENTRY_MAGIC) {
2414 ViceLog(0, ("cb_stateRestoreFE: ran out of free FileEntry structures\n"));
2419 if (cb_stateDiskEntryToFE(state, &fedsk, fe)) {
2425 for (iovcnt = 0, nCBs = 0;
2428 iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
2429 iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
2431 if ((iovcnt == 16) || (nCBs == hdr.nCBs - 1)) {
2432 if (fs_stateReadV(state, iov, iovcnt)) {
2436 if (cb_stateRestoreCBs(state, fe, iov, iovcnt)) {
2450 cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
2451 struct iovec * iov, int niovecs)
2454 struct CallBack * cb;
2455 struct CBDiskEntry * cbdsk;
2457 for (idx = 0; idx < niovecs; idx++) {
2458 cbdsk = (struct CBDiskEntry *) iov[idx].iov_base;
2460 if (cbdsk->cb.hhead < state->h_map.len &&
2461 state->h_map.entries[cbdsk->cb.hhead].valid == FS_STATE_IDX_SKIPPED) {
2465 if ((cb = GetCB()) == NULL) {
2466 ViceLog(0, ("cb_stateRestoreCBs: ran out of free CallBack structures\n"));
2470 if (cb_stateDiskEntryToCB(state, cbdsk, cb)) {
2471 ViceLog(0, ("cb_stateRestoreCBs: corrupt CallBack disk entry\n"));
2483 cb_stateFillHeader(struct callback_state_header * hdr)
2485 hdr->stamp.magic = CALLBACK_STATE_MAGIC;
2486 hdr->stamp.version = CALLBACK_STATE_VERSION;
2487 hdr->tfirst = tfirst;
2492 cb_stateCheckHeader(struct callback_state_header * hdr)
2496 if (hdr->stamp.magic != CALLBACK_STATE_MAGIC) {
2498 } else if (hdr->stamp.version != CALLBACK_STATE_VERSION) {
2500 } else if ((hdr->nFEs > cbstuff.nblks) || (hdr->nCBs > cbstuff.nblks)) {
2501 ViceLog(0, ("cb_stateCheckHeader: saved callback state larger than callback memory allocation\n"));
2507 /* disk entry conversion routines */
2509 cb_stateFEToDiskEntry(struct FileEntry * in, struct FEDiskEntry * out)
2511 memcpy(&out->fe, in, sizeof(struct FileEntry));
2512 out->index = fetoi(in);
2517 cb_stateDiskEntryToFE(struct fs_dump_state * state,
2518 struct FEDiskEntry * in, struct FileEntry * out)
2522 memcpy(out, &in->fe, sizeof(struct FileEntry));
2524 /* setup FE map entry */
2525 if (!in->index || (in->index >= state->fe_map.len)) {
2526 ViceLog(0, ("cb_stateDiskEntryToFE: index (%d) out of range",
2531 state->fe_map.entries[in->index].valid = FS_STATE_IDX_VALID;
2532 state->fe_map.entries[in->index].old_idx = in->index;
2533 state->fe_map.entries[in->index].new_idx = fetoi(out);
2540 cb_stateCBToDiskEntry(struct CallBack * in, struct CBDiskEntry * out)
2542 memcpy(&out->cb, in, sizeof(struct CallBack));
2543 out->index = cbtoi(in);
2548 cb_stateDiskEntryToCB(struct fs_dump_state * state,
2549 struct CBDiskEntry * in, struct CallBack * out)
2553 memcpy(out, &in->cb, sizeof(struct CallBack));
2555 /* setup CB map entry */
2556 if (!in->index || (in->index >= state->cb_map.len)) {
2557 ViceLog(0, ("cb_stateDiskEntryToCB: index (%d) out of range\n",
2562 state->cb_map.entries[in->index].valid = FS_STATE_IDX_VALID;
2563 state->cb_map.entries[in->index].old_idx = in->index;
2564 state->cb_map.entries[in->index].new_idx = cbtoi(out);
2570 /* index map routines */
2572 cb_stateAllocMap(struct fs_dump_state * state)
2574 state->fe_map.len = state->cb_hdr->fe_max + 1;
2575 state->cb_map.len = state->cb_hdr->cb_max + 1;
2576 state->fe_map.entries = (struct idx_map_entry_t *)
2577 calloc(state->fe_map.len, sizeof(struct idx_map_entry_t));
2578 state->cb_map.entries = (struct idx_map_entry_t *)
2579 calloc(state->cb_map.len, sizeof(struct idx_map_entry_t));
2580 return ((state->fe_map.entries != NULL) && (state->cb_map.entries != NULL)) ? 0 : 1;
2584 fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2588 /* FEs use a one-based indexing system, so old==0 implies no mapping */
2594 if (old >= state->fe_map.len) {
2595 ViceLog(0, ("fe_OldToNew: index %d is out of range\n", old));
2597 } else if (state->fe_map.entries[old].valid != FS_STATE_IDX_VALID ||
2598 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].valid != FS_STATE_IDX_VALID ||
2624 state->cb_map.entries[old].old_idx != old) { /* sanity check */
2625 ViceLog(0, ("cb_OldToNew: index %d points to an invalid CallBack record\n", old));
2628 *new = state->cb_map.entries[old].new_idx;
2634 #endif /* AFS_DEMAND_ATTACH_FS */
2637 DumpCallBackState_r(void)
2640 afs_uint32 magic = MAGICV2, now = (afs_int32) FT_ApproxTime(), freelisthead;
2642 oflag = O_WRONLY | O_CREAT | O_TRUNC;
2646 fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, oflag, 0666);
2649 ("Couldn't create callback dump file %s\n",
2650 AFSDIR_SERVER_CBKDUMP_FILEPATH));
2653 (void)write(fd, &magic, sizeof(magic));
2654 (void)write(fd, &now, sizeof(now));
2655 (void)write(fd, &cbstuff, sizeof(cbstuff));
2656 (void)write(fd, TimeOuts, sizeof(TimeOuts));
2657 (void)write(fd, timeout, sizeof(timeout));
2658 (void)write(fd, &tfirst, sizeof(tfirst));
2659 freelisthead = cbtoi((struct CallBack *)CBfree);
2660 (void)write(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
2661 freelisthead = fetoi((struct FileEntry *)FEfree);
2662 (void)write(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
2663 (void)write(fd, HashTable, sizeof(HashTable));
2664 (void)write(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks); /* CB stuff */
2665 (void)write(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks); /* FE stuff */
2672 DumpCallBackState(void) {
2676 rc = DumpCallBackState_r();
2682 #endif /* !INTERPRET_DUMP */
2684 #ifdef INTERPRET_DUMP
2686 /* This is only compiled in for the callback analyzer program */
2687 /* Returns the time of the dump */
2689 ReadDump(char *file, int timebits)
2692 afs_uint32 magic, freelisthead;
2700 fd = open(file, oflag);
2702 fprintf(stderr, "Couldn't read dump file %s\n", file);
2705 read(fd, &magic, sizeof(magic));
2706 if (magic == MAGICV2) {
2709 if (magic != MAGIC) {
2711 "Magic number of %s is invalid. You might be trying to\n",
2714 "run this program on a machine type with a different byte ordering.\n");
2718 if (timebits == 64) {
2719 read(fd, &now64, sizeof(afs_int64));
2720 now = (afs_int32) now64;
2722 read(fd, &now, sizeof(afs_int32));
2724 read(fd, &cbstuff, sizeof(cbstuff));
2725 read(fd, TimeOuts, sizeof(TimeOuts));
2726 read(fd, timeout, sizeof(timeout));
2727 read(fd, &tfirst, sizeof(tfirst));
2728 read(fd, &freelisthead, sizeof(freelisthead));
2729 CB = ((struct CallBack
2730 *)(calloc(cbstuff.nblks, sizeof(struct CallBack)))) - 1;
2731 FE = ((struct FileEntry
2732 *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
2733 CBfree = (struct CallBack *)itocb(freelisthead);
2734 read(fd, &freelisthead, sizeof(freelisthead));
2735 FEfree = (struct FileEntry *)itofe(freelisthead);
2736 read(fd, HashTable, sizeof(HashTable));
2737 read(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks); /* CB stuff */
2738 read(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks); /* FE stuff */
2740 perror("Error reading dumpfile");
2747 #include "AFS_component_version_number.h"
2749 #include "AFS_component_version_number.c"
2752 static afs_uint32 *cbTrack;
2755 main(int argc, char **argv)
2757 int err = 0, cbi = 0, stats = 0, noptions = 0, all = 0, vol = 0, raw = 0;
2759 struct FileEntry *fe;
2760 struct CallBack *cb;
2764 memset(&fid, 0, sizeof(fid));
2767 while (argc && **argv == '-') {
2770 if (!strcmp(*argv, "-host")) {
2776 cbi = atoi(*++argv);
2777 } else if (!strcmp(*argv, "-fid")) {
2783 fid.Volume = atoi(*++argv);
2784 fid.Vnode = atoi(*++argv);
2785 fid.Unique = atoi(*++argv);
2786 } else if (!strcmp(*argv, "-time")) {
2787 fprintf(stderr, "-time not supported\n");
2789 } else if (!strcmp(*argv, "-stats")) {
2791 } else if (!strcmp(*argv, "-all")) {
2793 } else if (!strcmp(*argv, "-raw")) {
2795 } else if (!strcmp(*argv, "-timebits")) {
2801 timebits = atoi(*++argv);
2802 if ((timebits != 32)
2806 } else if (!strcmp(*argv, "-volume")) {
2812 vol = atoi(*++argv);
2817 if (err || argc != 1) {
2819 "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] [-timebits 32"
2821 "] callbackdumpfile\n");
2823 "[cbid is shown for each host in the hosts.dump file]\n");
2826 now = ReadDump(*argv, timebits);
2827 if (stats || noptions == 0) {
2828 time_t uxtfirst = UXtime(tfirst), tnow = now;
2829 printf("The time of the dump was %u %s", (unsigned int) now, ctime(&tnow));
2830 printf("The last time cleanup ran was %u %s", (unsigned int) uxtfirst,
2832 PrintCallBackStats();
2835 cbTrack = calloc(cbstuff.nblks, sizeof(cbTrack[0]));
2840 struct CallBack *cb;
2841 struct FileEntry *fe;
2843 for (hash = 0; hash < FEHASH_SIZE; hash++) {
2844 for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
2845 if (!vol || (fe->volid == vol)) {
2846 afs_uint32 fe_i = fetoi(fe);
2848 for (cb = itocb(fe->firstcb); cb; cb = itocb(cb->cnext)) {
2849 afs_uint32 cb_i = cbtoi(cb);
2851 if (cb_i > cbstuff.nblks) {
2852 printf("CB index out of range (%u > %d), stopped for this FE\n",
2853 cb_i, cbstuff.nblks);
2857 if (cbTrack[cb_i]) {
2858 printf("CB entry already claimed for FE[%u] (this is FE[%u]), stopped\n",
2859 cbTrack[cb_i], fe_i);
2862 cbTrack[cb_i] = fe_i;
2874 afs_uint32 cfirst = cbi;
2879 } while (cbi != cfirst);
2884 printf("No callback entries for %u.%u\n", fid.Volume, fid.Vnode);
2887 cb = itocb(fe->firstcb);
2890 cb = itocb(cb->cnext);
2895 for (i = 1; i < cbstuff.nblks; i++) {
2896 p = (afs_int32 *) & FE[i];
2897 printf("%d:%12x%12x%12x%12x\n", i, p[0], p[1], p[2], p[3]);
2906 PrintCB(struct CallBack *cb, afs_uint32 now)
2908 struct FileEntry *fe = itofe(cb->fhead);
2909 time_t expires = TIndexToTime(cb->thead);
2914 printf("vol=%u vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %lu secs at %s",
2915 fe->volid, fe->vnode, fe->ncbs, cb->hhead, cb->status, fe->status,
2916 expires - now, ctime(&expires));
2921 #if !defined(INTERPRET_DUMP)
2923 ** try breaking calbacks on afidp from host. Use multi_rx.
2924 ** return 0 on success, non-zero on failure
2927 MultiBreakCallBackAlternateAddress(struct host *host, struct AFSCBFids *afidp)
2931 retVal = MultiBreakCallBackAlternateAddress_r(host, afidp);
2937 MultiBreakCallBackAlternateAddress_r(struct host *host,
2938 struct AFSCBFids *afidp)
2941 struct rx_connection **conns;
2942 struct rx_connection *connSuccess = 0;
2943 struct AddrPort *interfaces;
2944 static struct rx_securityClass *sc = 0;
2945 static struct AFSCBs tc = { 0, 0 };
2948 /* nothing more can be done */
2949 if (!host->interface)
2950 return 1; /* failure */
2952 /* the only address is the primary interface */
2953 if (host->interface->numberOfInterfaces <= 1)
2954 return 1; /* failure */
2956 /* initialise a security object only once */
2958 sc = rxnull_NewClientSecurityObject();
2960 i = host->interface->numberOfInterfaces;
2961 interfaces = calloc(i, sizeof(struct AddrPort));
2962 conns = calloc(i, sizeof(struct rx_connection *));
2963 if (!interfaces || !conns) {
2964 ViceLogThenPanic(0, ("Failed malloc in "
2965 "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 ViceLogThenPanic(0, ("Failed malloc in "
3060 "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) */