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 * BreakVolumeCallBacks(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>
89 #include <stdlib.h> /* for malloc() */
90 #include <time.h> /* ANSI standard location for time stuff */
100 #include <afs/assert.h>
102 #include <afs/stds.h>
104 #include <afs/nfs.h> /* yuck. This is an abomination. */
107 #include <afs/afscbint.h>
108 #include <afs/afsutil.h>
110 #include <afs/ihandle.h>
111 #include <afs/vnode.h>
112 #include <afs/volume.h>
113 #include "viced_prototypes.h"
116 #include <afs/ptclient.h> /* need definition of prlist for host.h */
118 #include "callback.h"
119 #ifdef AFS_DEMAND_ATTACH_FS
120 #include "../tviced/serialize_state.h"
121 #endif /* AFS_DEMAND_ATTACH_FS */
124 extern afsUUID FS_HostUUID;
125 extern int hostCount;
127 #ifndef INTERPRET_DUMP
128 static int ShowProblems = 1;
131 struct cbcounters cbstuff;
133 static struct FileEntry * FE = NULL; /* don't use FE[0] */
134 static struct CallBack * CB = NULL; /* don't use CB[0] */
136 static struct CallBack * CBfree = NULL;
137 static struct FileEntry * FEfree = NULL;
140 /* Time to live for call backs depends upon number of users of the file.
141 * TimeOuts is indexed by this number/8 (using TimeOut macro). Times
142 * in this table are for the workstation; server timeouts, add
145 static int TimeOuts[] = {
146 /* Note: don't make the first entry larger than 4 hours (see above) */
147 4 * 60 * 60, /* 0-7 users */
148 1 * 60 * 60, /* 8-15 users */
149 30 * 60, /* 16-23 users */
150 15 * 60, /* 24-31 users */
151 15 * 60, /* 32-39 users */
152 10 * 60, /* 40-47 users */
153 10 * 60, /* 48-55 users */
154 10 * 60, /* 56-63 users */
155 }; /* Anything more: MinTimeOut */
157 /* minimum time given for a call back */
158 #ifndef INTERPRET_DUMP
159 static int MinTimeOut = (7 * 60);
162 /* Heads of CB queues; a timeout index is 1+index into this array */
163 static afs_uint32 timeout[CB_NUM_TIMEOUT_QUEUES];
165 static afs_int32 tfirst; /* cbtime of oldest unexpired call back time queue */
168 /* 16 byte object get/free routines */
173 /* Prototypes for static routines */
174 static struct FileEntry *FindFE(register AFSFid * fid);
176 #ifndef INTERPRET_DUMP
177 static struct CallBack *iGetCB(register int *nused);
178 static int iFreeCB(register struct CallBack *cb, register int *nused);
179 static struct FileEntry *iGetFE(register int *nused);
180 static int iFreeFE(register struct FileEntry *fe, register int *nused);
181 static int TAdd(register struct CallBack *cb, register afs_uint32 * thead);
182 static int TDel(register struct CallBack *cb);
183 static int HAdd(register struct CallBack *cb, register struct host *host);
184 static int HDel(register struct CallBack *cb);
185 static int CDel(struct CallBack *cb, int deletefe);
186 static int CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
188 static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host);
189 static int FDel(register struct FileEntry *fe);
190 static int AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead,
191 int type, int locked);
192 static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
193 struct AFSCBFids *afidp, struct host *xhost);
194 static int MultiBreakVolumeCallBack_r(struct host *host, int isheld,
195 struct VCBParams *parms, int deletefe);
196 static int MultiBreakVolumeCallBack(struct host *host, int isheld,
198 static int MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
200 static int GetSomeSpace_r(struct host *hostp, int locked);
201 static int ClearHostCallbacks_r(struct host *hp, int locked);
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(register struct CallBack *cb, afs_uint32 now);
213 static afs_uint32 HashTable[FEHASH_SIZE]; /* File entry hash table */
215 static struct FileEntry *
216 FindFE(register AFSFid * fid)
220 register 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 *
235 iGetCB(register int *nused)
237 register struct CallBack *ret;
239 if ((ret = CBfree)) {
240 CBfree = (struct CallBack *)(((struct object *)ret)->next);
247 iFreeCB(register struct CallBack *cb, register int *nused)
249 ((struct object *)cb)->next = (struct object *)CBfree;
255 static struct FileEntry *
256 iGetFE(register int *nused)
258 register struct FileEntry *ret;
260 if ((ret = FEfree)) {
261 FEfree = (struct FileEntry *)(((struct object *)ret)->next);
268 iFreeFE(register struct FileEntry *fe, register int *nused)
270 ((struct object *)fe)->next = (struct object *)FEfree;
276 /* Add cb to end of specified timeout list */
278 TAdd(register struct CallBack *cb, register afs_uint32 * thead)
281 (*thead) = cb->tnext = cb->tprev = cbtoi(cb);
283 register 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(register struct CallBack *cb)
302 register 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(register struct CallBack *cb, register struct host *host)
317 cb->hhead = h_htoi(host);
319 host->cblist = cb->hnext = cb->hprev = cbtoi(cb);
321 register 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(register struct CallBack *cb)
334 register 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);
352 register afs_uint32 *cbp;
355 for (safety = 0, cbp = &fe->firstcb; *cbp && *cbp != cbi;
356 cbp = &itocb(*cbp)->cnext, safety++) {
357 if (safety > cbstuff.nblks + 10) {
360 ("CDel: Internal Error -- shutting down: wanted %d from %d, now at %d\n",
361 cbi, fe->firstcb, *cbp));
363 ShutDownAndCore(PANIC);
366 CDelPtr(fe, cbp, deletefe);
370 /* Same as CDel, but pointer to parent pointer to CB entry is passed,
371 * as well as file entry */
372 /* N.B. This one also deletes the CB, and also possibly parent FE, so
373 * make sure that it is not on any other list before calling this
375 static int Ccdelpt = 0, CcdelB = 0;
378 CDelPtr(register struct FileEntry *fe, register afs_uint32 * cbp,
381 register struct CallBack *cb;
391 if ((--fe->ncbs == 0) && deletefe)
397 FindCBPtr(struct FileEntry *fe, struct host *host)
399 register afs_uint32 hostindex = h_htoi(host);
400 register struct CallBack *cb;
401 register afs_uint32 *cbp;
404 for (safety = 0, cbp = &fe->firstcb; *cbp; cbp = &cb->cnext, safety++) {
405 if (safety > cbstuff.nblks) {
406 ViceLog(0, ("FindCBPtr: Internal Error -- shutting down.\n"));
408 ShutDownAndCore(PANIC);
411 if (cb->hhead == hostindex)
417 /* Delete file entry from hash table */
419 FDel(register struct FileEntry *fe)
421 register int fei = fetoi(fe);
422 register afs_uint32 *p = &HashTable[FEHash(fe->volid, fe->unique)];
424 while (*p && *p != fei)
425 p = &itofe(*p)->fnext;
432 /* initialize the callback package */
434 InitCallBack(int nblks)
437 tfirst = CBtime(FT_ApproxTime());
438 /* N.B. The "-1", below, is because
439 * FE[0] and CB[0] are not used--and not allocated */
440 FE = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry))));
442 ViceLog(0, ("Failed malloc in InitCallBack\n"));
445 FE--; /* FE[0] is supposed to point to junk */
446 cbstuff.nFEs = nblks;
448 FreeFE(&FE[cbstuff.nFEs]); /* This is correct */
449 CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))));
451 ViceLog(0, ("Failed malloc in InitCallBack\n"));
454 CB--; /* CB[0] is supposed to point to junk */
455 cbstuff.nCBs = nblks;
457 FreeCB(&CB[cbstuff.nCBs]); /* This is correct */
458 cbstuff.nblks = nblks;
459 cbstuff.nbreakers = 0;
465 XCallBackBulk_r(struct host * ahost, struct AFSFid * fids, afs_int32 nfids)
467 struct AFSCallBack tcbs[AFSCBMAX];
473 struct rx_connection *cb_conn = NULL;
476 rx_SetConnDeadTime(ahost->callback_rxcon, 4);
477 rx_SetConnHardDeadTime(ahost->callback_rxcon, AFS_HARDDEADTIME);
484 for (i = 0; i < nfids && i < AFSCBMAX; i++) {
485 tcbs[i].CallBackVersion = CALLBACK_VERSION;
486 tcbs[i].ExpirationTime = 0;
487 tcbs[i].CallBackType = CB_DROPPED;
489 tf.AFSCBFids_len = i;
490 tf.AFSCBFids_val = &(fids[j]);
494 tc.AFSCBs_val = tcbs;
496 cb_conn = ahost->callback_rxcon;
497 rx_GetConnection(cb_conn);
499 code |= RXAFSCB_CallBack(cb_conn, &tf, &tc);
500 rx_PutConnection(cb_conn);
508 /* the locked flag tells us if the host entry has already been locked
509 * by our parent. I don't think anybody actually calls us with the
510 * host locked, but here's how to make that work: GetSomeSpace has to
511 * change so that it doesn't attempt to lock any hosts < "host". That
512 * means that it might be unable to free any objects, so it has to
513 * return an exit status. If it fails, then AddCallBack1 might fail,
514 * as well. If so, the host->ResetDone should probably be set to 0,
515 * and we probably don't want to return a callback promise to the
516 * cache manager, either. */
518 AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
526 if (!(host->hostFlags & HOSTDELETED))
527 retVal = AddCallBack1_r(host, fid, thead, type, 1);
537 AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
540 struct FileEntry *fe;
541 struct CallBack *cb = 0, *lastcb = 0;
542 struct FileEntry *newfe = 0;
543 afs_uint32 time_out = 0;
544 afs_uint32 *Thead = thead;
545 struct CallBack *newcb = 0;
548 cbstuff.AddCallBacks++;
552 /* allocate these guys first, since we can't call the allocator with
553 * the host structure locked -- or we might deadlock. However, we have
554 * to avoid races with FindFE... */
555 while (!(newcb = GetCB())) {
556 GetSomeSpace_r(host, locked);
558 while (!(newfe = GetFE())) { /* Get it now, so we don't have to call */
559 /* GetSomeSpace with the host locked, later. This might turn out to */
560 /* have been unneccessary, but that's actually kind of unlikely, since */
561 /* most files are not shared. */
562 GetSomeSpace_r(host, locked);
566 h_Lock_r(host); /* this can yield, so do it before we get any */
568 if (host->hostFlags & HOSTDELETED) {
576 if (type == CB_NORMAL) {
578 TimeCeiling(FT_ApproxTime() + TimeOut(fe ? fe->ncbs : 0) +
580 Thead = THead(CBtime(time_out));
581 } else if (type == CB_VOLUME) {
582 time_out = TimeCeiling((60 * 120 + FT_ApproxTime()) + ServerBias);
583 Thead = THead(CBtime(time_out));
584 } else if (type == CB_BULK) {
585 /* bulk status can get so many callbacks all at once, and most of them
586 * are probably not for things that will be used for long.
589 TimeCeiling(FT_ApproxTime() + ServerBias +
590 TimeOut(22 + (fe ? fe->ncbs : 0)));
591 Thead = THead(CBtime(time_out));
597 register afs_uint32 hash;
602 fe->volid = fid->Volume;
603 fe->vnode = fid->Vnode;
604 fe->unique = fid->Unique;
607 hash = FEHash(fid->Volume, fid->Unique);
608 fe->fnext = HashTable[hash];
609 HashTable[hash] = fetoi(fe);
611 for (safety = 0, lastcb = cb = itocb(fe->firstcb); cb;
612 lastcb = cb, cb = itocb(cb->cnext), safety++) {
613 if (safety > cbstuff.nblks) {
614 ViceLog(0, ("AddCallBack1: Internal Error -- shutting down.\n"));
616 ShutDownAndCore(PANIC);
618 if (cb->hhead == h_htoi(host))
621 if (cb) { /* Already have call back: move to new timeout list */
622 /* don't change delayed callbacks back to normal ones */
623 if (cb->status != CB_DELAYED)
625 /* Only move if new timeout is longer */
626 if (TNorm(ttoi(Thead)) > TNorm(cb->thead)) {
630 if (newfe == NULL) { /* we are using the new FE */
631 fe->firstcb = cbtoi(cb);
633 cb->fhead = fetoi(fe);
638 *(lastcb ? &lastcb->cnext : &fe->firstcb) = cbtoi(cb);
641 cb->fhead = fetoi(fe);
647 /* now free any still-unused callback or host entries */
653 if (!locked) /* freecb and freefe might(?) yield */
656 if (type == CB_NORMAL || type == CB_VOLUME || type == CB_BULK)
657 return time_out - ServerBias; /* Expires sooner at workstation */
663 CompareCBA(const void *e1, const void *e2)
665 const struct cbstruct *cba1 = (const struct cbstruct *)e1;
666 const struct cbstruct *cba2 = (const struct cbstruct *)e2;
667 return ((cba1->hp)->index - (cba2->hp)->index);
670 /* Take an array full of hosts, all held. Break callbacks to them, and
671 * release the holds once you're done, except don't release xhost. xhost
672 * may be NULL. Currently only works for a single Fid in afidp array.
673 * If you want to make this work with multiple fids, you need to fix
674 * the error handling. One approach would be to force a reset if a
675 * multi-fid call fails, or you could add delayed callbacks for each
676 * fid. You probably also need to sort and remove duplicate hosts.
677 * When this is called from the BreakVolumeCallBacks path, it does NOT
678 * force a reset if the RPC fails, it just marks the host down and tries
679 * to create a delayed callback. */
680 /* N.B. be sure that code works when ncbas == 0 */
681 /* N.B. requires all the cba[*].hp pointers to be valid... */
682 /* This routine does not hold a lock on the host for the duration of
683 * the BreakCallBack RPC, which is a significant deviation from tradition.
684 * It _does_ get a lock on the host before setting VenusDown = 1,
685 * which is sufficient only if VenusDown = 0 only happens when the
686 * lock is held over the RPC and the subsequent VenusDown == 0
687 * wherever that is done. */
689 MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
690 struct AFSCBFids *afidp, struct host *xhost)
693 struct rx_connection *conns[MAX_CB_HOSTS];
694 static struct AFSCBs tc = { 0, 0 };
695 int multi_to_cba_map[MAX_CB_HOSTS];
697 assert(ncbas <= MAX_CB_HOSTS);
699 /* sort cba list to avoid makecall issues */
700 qsort(cba, ncbas, sizeof(struct cbstruct), CompareCBA);
702 /* set up conns for multi-call */
703 for (i = 0, j = 0; i < ncbas; i++) {
704 struct host *thishost = cba[i].hp;
705 if (!thishost || (thishost->hostFlags & HOSTDELETED)) {
708 rx_GetConnection(thishost->callback_rxcon);
709 multi_to_cba_map[j] = i;
710 conns[j++] = thishost->callback_rxcon;
713 rx_SetConnDeadTime(thishost->callback_rxcon, 4);
714 rx_SetConnHardDeadTime(thishost->callback_rxcon, AFS_HARDDEADTIME);
718 if (j) { /* who knows what multi would do with 0 conns? */
722 multi_RXAFSCB_CallBack(afidp, &tc);
728 i = multi_to_cba_map[multi_i];
734 ("BCB: INTERNAL ERROR: hp=%p, cba=%p, thead=%u\n",
738 ** try breaking callbacks on alternate interface addresses
740 if (MultiBreakCallBackAlternateAddress(hp, afidp)) {
743 ("BCB: Failed on file %u.%u.%u, "
744 "Host %p (%s:%d) is down\n",
745 afidp->AFSCBFids_val->Volume,
746 afidp->AFSCBFids_val->Vnode,
747 afidp->AFSCBFids_val->Unique,
749 afs_inet_ntoa_r(hp->host, hoststr),
755 if (!(hp->hostFlags & HOSTDELETED)) {
756 hp->hostFlags |= VENUSDOWN;
758 * We always go into AddCallBack1_r with the host locked
760 AddCallBack1_r(hp, afidp->AFSCBFids_val, itot(idx),
774 for (i = 0; i < ncbas; i++) {
777 if (hp && xhost != hp) {
782 /* H_UNLOCK around this so h_FreeConnection does not deadlock.
783 h_FreeConnection should *never* be called on a callback connection,
784 but on 10/27/04 a deadlock occurred where it was, when we know why,
785 this should be reverted. -- shadow */
787 for (i = 0; i < j; i++) {
788 rx_PutConnection(conns[i]);
796 * Break all call backs for fid, except for the specified host (unless flag
797 * is true, in which case all get a callback message. Assumption: the specified
798 * host is h_Held, by the caller; the others aren't.
799 * Specified host may be bogus, that's ok. This used to check to see if the
800 * host was down in two places, once right after the host was h_held, and
801 * again after it was locked. That race condition is incredibly rare and
802 * relatively harmless even when it does occur, so we don't check for it now.
804 /* if flag is true, send a break callback msg to "host", too */
806 BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
808 struct FileEntry *fe;
809 struct CallBack *cb, *nextcb;
810 struct cbstruct cba[MAX_CB_HOSTS];
817 ("BCB: BreakCallBack(Host %p all but %s:%d, (%u,%u,%u))\n",
818 xhost, afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
819 fid->Volume, fid->Vnode, fid->Unique));
822 cbstuff.BreakCallBacks++;
827 hostindex = h_htoi(xhost);
828 cb = itocb(fe->firstcb);
829 if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
830 /* the most common case is what follows the || */
833 tf.AFSCBFids_len = 1;
834 tf.AFSCBFids_val = fid;
837 for (ncbas = 0; cb && ncbas < MAX_CB_HOSTS; cb = nextcb) {
838 nextcb = itocb(cb->cnext);
839 if ((cb->hhead != hostindex || flag)
840 && (cb->status == CB_BULK || cb->status == CB_NORMAL
841 || cb->status == CB_VOLUME)) {
842 struct host *thishost = h_itoh(cb->hhead);
844 ViceLog(0, ("BCB: BOGUS! cb->hhead is NULL!\n"));
845 } else if (thishost->hostFlags & VENUSDOWN) {
847 ("BCB: %p (%s:%d) is down; delaying break call back\n",
848 thishost, afs_inet_ntoa_r(thishost->host, hoststr),
849 ntohs(thishost->port)));
850 cb->status = CB_DELAYED;
852 if (!(thishost->hostFlags & HOSTDELETED)) {
854 cba[ncbas].hp = thishost;
855 cba[ncbas].thead = cb->thead;
860 CDel(cb, 1); /* Usually first; so this delete
861 * is reasonably inexpensive */
867 MultiBreakCallBack_r(cba, ncbas, &tf, xhost);
869 /* we need to to all these initializations again because MultiBreakCallBack may block */
874 cb = itocb(fe->firstcb);
875 if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
876 /* the most common case is what follows the || */
887 /* Delete (do not break) single call back for fid */
889 DeleteCallBack(struct host *host, AFSFid * fid)
891 register struct FileEntry *fe;
892 register afs_uint32 *pcb;
896 cbstuff.DeleteCallBacks++;
899 /* do not care if the host has been HOSTDELETED */
905 ("DCB: No call backs for fid (%u, %u, %u)\n", fid->Volume,
906 fid->Vnode, fid->Unique));
909 pcb = FindCBPtr(fe, host);
912 ("DCB: No call back for host %p (%s:%d), (%u, %u, %u)\n",
913 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
914 fid->Volume, fid->Vnode, fid->Unique));
928 * Delete (do not break) all call backs for fid. This call doesn't
929 * set all of the various host locks, but it shouldn't really matter
930 * since we're not adding callbacks, but deleting them. I'm not sure
931 * why it doesn't set the lock, however; perhaps it should.
934 DeleteFileCallBacks(AFSFid * fid)
936 register struct FileEntry *fe;
937 register struct CallBack *cb;
938 register afs_uint32 cbi;
942 cbstuff.DeleteFiles++;
947 ("DF: No fid (%u,%u,%u) to delete\n", fid->Volume, fid->Vnode,
951 for (n = 0, cbi = fe->firstcb; cbi; n++) {
964 /* Delete (do not break) all call backs for host. The host should be
967 DeleteAllCallBacks_r(struct host *host, int deletefe)
969 register struct CallBack *cb;
970 register int cbi, first;
972 cbstuff.DeleteAllCallBacks++;
973 cbi = first = host->cblist;
975 ViceLog(8, ("DV: no call backs\n"));
983 } while (cbi != first);
989 * Break all delayed call backs for host. Returns 1 if all call backs
990 * successfully broken; 0 otherwise. Assumes host is h_Held and h_Locked.
991 * Must be called with VenusDown set for this host
994 BreakDelayedCallBacks(struct host *host)
998 retVal = BreakDelayedCallBacks_r(host);
1004 BreakDelayedCallBacks_r(struct host *host)
1006 struct AFSFid fids[AFSCBMAX];
1007 u_byte thead[AFSCBMAX]; /* This should match thead in struct Callback */
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 register struct FileEntry *fe = itofe(cb->fhead);
1062 thead[nfids] = cb->thead;
1063 fids[nfids].Volume = fe->volid;
1064 fids[nfids].Vnode = fe->vnode;
1065 fids[nfids].Unique = fe->unique;
1071 } while (cbi && cbi != first && nfids < AFSCBMAX);
1077 if (XCallBackBulk_r(host, fids, nfids)) {
1078 /* Failed, again: put them back, probably with old
1083 ("CB: XCallBackBulk failed, Host %p (%s:%d); "
1084 "callback list follows:\n",
1085 host, afs_inet_ntoa_r(host->host, hoststr),
1086 ntohs(host->port)));
1088 for (i = 0; i < nfids; i++) {
1091 ("CB: Host %p (%s:%d), file %u.%u.%u "
1092 "(part of bulk callback)\n",
1093 host, afs_inet_ntoa_r(host->host, hoststr),
1094 ntohs(host->port), fids[i].Volume,
1095 fids[i].Vnode, fids[i].Unique));
1098 * AddCallBack1_r(host, &fids[i], itot(thead[i]), CB_DELAYED, 1);
1099 * * but it turns out to cause too many tricky locking problems.
1100 * * now, if break delayed fails, screw it. */
1102 host->hostFlags |= VENUSDOWN; /* Failed */
1103 ClearHostCallbacks_r(host, 1 /* locked */ );
1107 if (nfids < AFSCBMAX)
1111 cbstuff.nbreakers--;
1112 /* If we succeeded it's always ok to unset HFE_LATER */
1113 if (!(host->hostFlags & VENUSDOWN))
1114 host->hostFlags &= ~HFE_LATER;
1115 return (host->hostFlags & VENUSDOWN);
1119 ** isheld is 0 if the host is held in h_Enumerate
1120 ** isheld is 1 if the host is held in BreakVolumeCallBacks
1123 MultiBreakVolumeCallBack_r(struct host *host, int isheld,
1124 struct VCBParams *parms, int deletefe)
1129 return isheld; /* host is held only by h_Enumerate, do nothing */
1130 if (host->hostFlags & HOSTDELETED)
1131 return 0; /* host is deleted, release hold */
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; /* Do InitCallBackState when host returns */
1149 return 0; /* release hold */
1151 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 return 1; /* DON'T release hold, because we still need it. */
1173 ** isheld is 0 if the host is held in h_Enumerate
1174 ** isheld is 1 if the host is held in BreakVolumeCallBacks
1177 MultiBreakVolumeCallBack(struct host *host, int isheld, void *rock)
1179 struct VCBParams *parms = (struct VCBParams *) rock;
1183 retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 1);
1189 ** isheld is 0 if the host is held in h_Enumerate
1190 ** isheld is 1 if the host is held in BreakVolumeCallBacks
1193 MultiBreakVolumeLaterCallBack(struct host *host, int isheld, void *rock)
1195 struct VCBParams *parms = (struct VCBParams *)rock;
1198 retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 0);
1204 * Break all call backs on a single volume. Don't call this with any
1205 * hosts h_held. Note that this routine clears the callbacks before
1206 * actually breaking them, and that the vnode isn't locked during this
1207 * operation, so that people might see temporary callback loss while
1208 * this function is executing. It is just a temporary state, however,
1209 * since the callback will be broken later by this same function.
1211 * Now uses multi-RX for CallBack RPC. Note that the
1212 * multiBreakCallBacks routine does not force a reset if the RPC
1213 * fails, unlike the previous version of this routine, but does create
1214 * a delayed callback. Resets will be forced if the host is
1215 * determined to be down before the RPC is executed.
1218 BreakVolumeCallBacks(afs_uint32 volume)
1223 struct CallBack *cb;
1224 struct FileEntry *fe;
1226 struct VCBParams henumParms;
1227 afs_uint32 tthead = 0; /* zero is illegal value */
1230 fid.Volume = volume, fid.Vnode = fid.Unique = 0;
1231 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1232 for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
1233 if (fe->volid == volume) {
1234 register struct CallBack *cbnext;
1235 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1236 host = h_itoh(cb->hhead);
1238 if (!(host->hostFlags & HOSTDELETED)) {
1240 if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
1244 cbnext = itocb(cb->cnext);
1248 /* leave hold for MultiBreakVolumeCallBack to clear */
1259 /* didn't find any callbacks, so return right away. */
1263 henumParms.ncbas = 0;
1264 henumParms.fid = &fid;
1265 henumParms.thead = tthead;
1267 h_Enumerate(MultiBreakVolumeCallBack, &henumParms);
1269 if (henumParms.ncbas) { /* do left-overs */
1270 struct AFSCBFids tf;
1271 tf.AFSCBFids_len = 1;
1272 tf.AFSCBFids_val = &fid;
1274 MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf, 0);
1276 henumParms.ncbas = 0;
1282 #ifdef AFS_PTHREAD_ENV
1283 extern pthread_cond_t fsync_cond;
1285 extern char fsync_wait[];
1289 BreakVolumeCallBacksLater(afs_uint32 volume)
1293 struct FileEntry *fe;
1294 struct CallBack *cb;
1298 ViceLog(25, ("Setting later on volume %u\n", volume));
1300 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1301 for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
1302 if (fe->volid == volume) {
1303 register struct CallBack *cbnext;
1304 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1305 host = h_itoh(cb->hhead);
1306 host->hostFlags |= HFE_LATER;
1307 cb->status = CB_DELAYED;
1308 cbnext = itocb(cb->cnext);
1311 fe->status |= FE_LATER;
1320 /* didn't find any callbacks, so return right away. */
1324 ViceLog(25, ("Fsync thread wakeup\n"));
1325 #ifdef AFS_PTHREAD_ENV
1327 assert(pthread_cond_broadcast(&fsync_cond) == 0);
1330 LWP_NoYieldSignal(fsync_wait);
1336 BreakLaterCallBacks(void)
1341 struct CallBack *cb;
1342 struct FileEntry *fe = NULL;
1343 struct FileEntry *myfe = NULL;
1345 struct VCBParams henumParms;
1346 unsigned short tthead = 0; /* zero is illegal value */
1350 ViceLog(25, ("Looking for FileEntries to unchain\n"));
1353 /* Pick the first volume we see to clean up */
1354 fid.Volume = fid.Vnode = fid.Unique = 0;
1356 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1357 for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
1358 if (fe && (fe->status & FE_LATER)
1359 && (fid.Volume == 0 || fid.Volume == fe->volid)) {
1360 /* Ugly, but used to avoid left side casting */
1361 struct object *tmpfe;
1363 ("Unchaining for %u:%u:%u\n", fe->vnode, fe->unique,
1365 fid.Volume = fe->volid;
1367 fe->status &= ~FE_LATER; /* not strictly needed */
1368 /* Works since volid is deeper than the largest pointer */
1369 tmpfe = (struct object *)fe;
1370 tmpfe->next = (struct object *)myfe;
1383 /* loop over FEs from myfe and free/break */
1385 for (fe = myfe; fe;) {
1386 register struct CallBack *cbnext;
1387 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1388 cbnext = itocb(cb->cnext);
1389 host = h_itoh(cb->hhead);
1390 if (cb->status == CB_DELAYED) {
1391 if (!(host->hostFlags & HOSTDELETED)) {
1393 if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
1399 CDel(cb, 0); /* Don't let CDel clean up the fe */
1400 /* leave hold for MultiBreakVolumeCallBack to clear */
1403 ("Found host %p (%s:%d) non-DELAYED cb for %u:%u:%u\n",
1404 host, afs_inet_ntoa_r(host->host, hoststr),
1405 ntohs(host->port), fe->vnode, fe->unique, fe->volid));
1409 fe = (struct FileEntry *)((struct object *)fe)->next;
1414 ViceLog(125, ("Breaking volume %u\n", fid.Volume));
1415 henumParms.ncbas = 0;
1416 henumParms.fid = &fid;
1417 henumParms.thead = tthead;
1419 h_Enumerate(MultiBreakVolumeLaterCallBack, (char *)&henumParms);
1421 if (henumParms.ncbas) { /* do left-overs */
1422 struct AFSCBFids tf;
1423 tf.AFSCBFids_len = 1;
1424 tf.AFSCBFids_val = &fid;
1426 MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf, 0);
1427 henumParms.ncbas = 0;
1432 /* Arrange to be called again */
1437 * Delete all timed-out call back entries (to be called periodically by file
1441 CleanupTimedOutCallBacks(void)
1444 CleanupTimedOutCallBacks_r();
1450 CleanupTimedOutCallBacks_r(void)
1452 afs_uint32 now = CBtime(FT_ApproxTime());
1453 register afs_uint32 *thead;
1454 register struct CallBack *cb;
1455 register int ntimedout = 0;
1458 while (tfirst <= now) {
1460 cbi = *(thead = THead(tfirst));
1466 ("CCB: deleting timed out call back %x (%s:%d), (%u,%u,%u)\n",
1467 h_itoh(cb->hhead)->host,
1468 afs_inet_ntoa_r(h_itoh(cb->hhead)->host, hoststr),
1469 h_itoh(cb->hhead)->port, itofe(cb->fhead)->volid,
1470 itofe(cb->fhead)->vnode, itofe(cb->fhead)->unique));
1474 if (ntimedout > cbstuff.nblks) {
1475 ViceLog(0, ("CCB: Internal Error -- shutting down...\n"));
1476 DumpCallBackState();
1477 ShutDownAndCore(PANIC);
1479 } while (cbi != *thead);
1484 cbstuff.CBsTimedOut += ntimedout;
1485 ViceLog(7, ("CCB: deleted %d timed out callbacks\n", ntimedout));
1486 return (ntimedout > 0);
1490 * parameters to pass to lih*_r from h_Enumerate_r when trying to find a host
1491 * from which to clear callbacks.
1495 * Points to the least interesting host found; try to clear callbacks on
1496 * this host after h_Enumerate_r(lih*_r)'ing.
1501 * The last host we got from lih*_r, but we couldn't clear its callbacks
1502 * for some reason. Choose the next-best host after this one (with the
1503 * current lih*_r, this means to only select hosts that have an ActiveCall
1504 * newer than lastlih).
1506 struct host *lastlih;
1509 /* Value of host->refCount that allows us to reliably infer that
1510 * host may be held by some other thread */
1511 #define OTHER_MUSTHOLD_LIH 2
1513 /* This version does not allow 'host' to be selected unless its ActiveCall
1514 * is newer than 'params->lastlih' which is the host with the oldest
1515 * ActiveCall from the last pass (if it is provided). We filter out any hosts
1516 * that are are held by other threads.
1518 * There is a small problem here, but it may not be easily fixable. Say we
1519 * select some host A, and give it back to GetSomeSpace_r. GSS_r for some
1520 * reason cannot clear the callbacks on A, and so calls us again with
1521 * lastlih = A. Suppose there is another host B that has the same ActiveCall
1522 * time as A. We will now skip over host B, since
1523 * 'hostB->ActiveCall > hostA->ActiveCall' is not true. This could result in
1524 * us prematurely going to the GSS_r 2nd or 3rd pass, and making us a little
1525 * inefficient. This should be pretty rare, though, except perhaps in cases
1526 * with very small numbers of hosts.
1528 * Also filter out any hosts with HOSTDELETED set. h_Enumerate_r should in
1529 * theory not give these to us anyway, but be paranoid.
1532 lih0_r(register struct host *host, register int flags, void *rock)
1534 struct lih_params *params = (struct lih_params *)rock;
1536 /* OTHER_MUSTHOLD_LIH is because the h_Enum loop holds us once */
1538 && (!(host->hostFlags & HOSTDELETED))
1539 && (host->refCount < OTHER_MUSTHOLD_LIH)
1540 && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
1541 && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
1544 h_Release_r(params->lih); /* release prev host */
1553 /* same as lih0_r, except we do not prevent held hosts from being selected. */
1555 lih1_r(register struct host *host, register int flags, void *rock)
1557 struct lih_params *params = (struct lih_params *)rock;
1560 && (!(host->hostFlags & HOSTDELETED))
1561 && (!params->lih || host->ActiveCall < params->lih->ActiveCall)
1562 && (!params->lastlih || host->ActiveCall > params->lastlih->ActiveCall)) {
1565 h_Release_r(params->lih); /* release prev host */
1574 /* This could be upgraded to get more space each time */
1575 /* first pass: sequentially find the oldest host which isn't held by
1576 anyone for which we can clear callbacks;
1578 /* second pass: sequentially find the oldest host regardless of
1579 whether or not the host is held; skipping 'hostp' */
1580 /* third pass: attempt to clear callbacks from 'hostp' */
1581 /* always called with hostp unlocked */
1583 /* Note: hostlist is ordered most recently created host first and
1584 * its order has no relationship to the most recently used. */
1585 extern struct host *hostList;
1587 GetSomeSpace_r(struct host *hostp, int locked)
1590 struct lih_params params;
1593 cbstuff.GotSomeSpaces++;
1595 ("GSS: First looking for timed out call backs via CleanupCallBacks\n"));
1596 if (CleanupTimedOutCallBacks_r()) {
1602 params.lastlih = NULL;
1607 h_Enumerate_r(i == 0 ? lih0_r : lih1_r, hostList, ¶ms);
1610 if (params.lastlih) {
1611 h_Release_r(params.lastlih);
1612 params.lastlih = NULL;
1616 /* note that 'hp' was held by lih*_r; we will need to release it */
1618 if ((hp != hostp) && !ClearHostCallbacks_r(hp, 0 /* not locked or held */ )) {
1623 params.lastlih = hp;
1624 /* params.lastlih will be released on the next iteration, after
1629 * Next time try getting callbacks from any host even if
1630 * it's held, since the only other option is starvation for
1631 * the file server (i.e. until the callback timeout arrives).
1634 params.lastlih = NULL;
1637 ("GSS: Try harder for longest inactive host cnt= %d\n",
1642 /* Could not obtain space from other hosts, clear hostp's callback state */
1647 ClearHostCallbacks_r(hostp, 1 /*already locked */ );
1654 /* locked - set if caller has already locked the host */
1656 ClearHostCallbacks_r(struct host *hp, int locked)
1660 struct rx_connection *cb_conn = NULL;
1663 ("GSS: Delete longest inactive host %p (%s:%d)\n",
1664 hp, afs_inet_ntoa_r(hp->host, hoststr), ntohs(hp->port)));
1666 if ((hp->hostFlags & HOSTDELETED)) {
1667 /* hp could go away after reacquiring H_LOCK in h_NBLock_r, so we can't
1668 * really use it; its callbacks will get cleared anyway when
1669 * h_TossStuff_r gets its hands on it */
1675 /** Try a non-blocking lock. If the lock is already held return
1676 * after releasing hold on hp
1679 if (h_NBLock_r(hp)) {
1684 if (hp->Console & 2) {
1686 * If the special console field is set it means that a thread
1687 * is waiting in AddCallBack1 after it set pointers to the
1688 * file entry and/or callback entry. Because of the bogus
1689 * usage of h_hold it won't prevent from another thread, this
1690 * one, to remove all the callbacks so just to be safe we keep
1691 * a reference. NOTE, on the last phase we'll free the calling
1692 * host's callbacks but that's ok...
1696 DeleteAllCallBacks_r(hp, 1);
1697 if (hp->hostFlags & VENUSDOWN) {
1698 hp->hostFlags &= ~RESETDONE; /* remember that we must do a reset */
1699 } else if (!(hp->hostFlags & HOSTDELETED)) {
1700 /* host is up, try a call */
1701 hp->hostFlags &= ~ALTADDR; /* alternate addresses are invalid */
1702 cb_conn = hp->callback_rxcon;
1703 rx_GetConnection(hp->callback_rxcon);
1704 if (hp->interface) {
1707 RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
1710 code = RXAFSCB_InitCallBackState(cb_conn);
1712 rx_PutConnection(cb_conn);
1715 hp->hostFlags |= ALTADDR; /* alternate addresses are valid */
1717 /* failed, mark host down and need reset */
1718 hp->hostFlags |= VENUSDOWN;
1719 hp->hostFlags &= ~RESETDONE;
1721 /* reset succeeded, we're done */
1722 hp->hostFlags |= RESETDONE;
1731 #endif /* INTERPRET_DUMP */
1735 PrintCallBackStats(void)
1738 "%d add CB, %d break CB, %d del CB, %d del FE, %d CB's timed out, %d space reclaim, %d del host\n",
1739 cbstuff.AddCallBacks, cbstuff.BreakCallBacks,
1740 cbstuff.DeleteCallBacks, cbstuff.DeleteFiles, cbstuff.CBsTimedOut,
1741 cbstuff.GotSomeSpaces, cbstuff.DeleteAllCallBacks);
1742 fprintf(stderr, "%d CBs, %d FEs, (%d of total of %d 16-byte blocks)\n",
1743 cbstuff.nCBs, cbstuff.nFEs, cbstuff.nCBs + cbstuff.nFEs,
1749 #define MAGIC 0x12345678 /* To check byte ordering of dump when it is read in */
1750 #define MAGICV2 0x12345679 /* To check byte ordering & version of dump when it is read in */
1753 #ifndef INTERPRET_DUMP
1755 #ifdef AFS_DEMAND_ATTACH_FS
1758 * callback state serialization
1760 static int cb_stateSaveTimeouts(struct fs_dump_state * state);
1761 static int cb_stateSaveFEHash(struct fs_dump_state * state);
1762 static int cb_stateSaveFEs(struct fs_dump_state * state);
1763 static int cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe);
1764 static int cb_stateRestoreTimeouts(struct fs_dump_state * state);
1765 static int cb_stateRestoreFEHash(struct fs_dump_state * state);
1766 static int cb_stateRestoreFEs(struct fs_dump_state * state);
1767 static int cb_stateRestoreFE(struct fs_dump_state * state);
1768 static int cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
1769 struct iovec * iov, int niovecs);
1771 static int cb_stateVerifyFEHash(struct fs_dump_state * state);
1772 static int cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe);
1773 static int cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe);
1774 static int cb_stateVerifyTimeoutQueues(struct fs_dump_state * state);
1776 static int cb_stateFEToDiskEntry(struct FileEntry *, struct FEDiskEntry *);
1777 static int cb_stateDiskEntryToFE(struct fs_dump_state * state,
1778 struct FEDiskEntry *, struct FileEntry *);
1780 static int cb_stateCBToDiskEntry(struct CallBack *, struct CBDiskEntry *);
1781 static int cb_stateDiskEntryToCB(struct fs_dump_state * state,
1782 struct CBDiskEntry *, struct CallBack *);
1784 static int cb_stateFillHeader(struct callback_state_header * hdr);
1785 static int cb_stateCheckHeader(struct callback_state_header * hdr);
1787 static int cb_stateAllocMap(struct fs_dump_state * state);
1790 cb_stateSave(struct fs_dump_state * state)
1794 AssignInt64(state->eof_offset, &state->hdr->cb_offset);
1796 /* invalidate callback state header */
1797 memset(state->cb_hdr, 0, sizeof(struct callback_state_header));
1798 if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1799 sizeof(struct callback_state_header))) {
1804 fs_stateIncEOF(state, sizeof(struct callback_state_header));
1806 /* dump timeout state */
1807 if (cb_stateSaveTimeouts(state)) {
1812 /* dump fe hashtable state */
1813 if (cb_stateSaveFEHash(state)) {
1818 /* dump callback state */
1819 if (cb_stateSaveFEs(state)) {
1824 /* write the callback state header to disk */
1825 cb_stateFillHeader(state->cb_hdr);
1826 if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1827 sizeof(struct callback_state_header))) {
1837 cb_stateRestore(struct fs_dump_state * state)
1841 if (fs_stateReadHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1842 sizeof(struct callback_state_header))) {
1847 if (cb_stateCheckHeader(state->cb_hdr)) {
1852 if (cb_stateAllocMap(state)) {
1857 if (cb_stateRestoreTimeouts(state)) {
1862 if (cb_stateRestoreFEHash(state)) {
1867 /* restore FEs and CBs from disk */
1868 if (cb_stateRestoreFEs(state)) {
1873 /* restore the timeout queue heads */
1874 tfirst = state->cb_hdr->tfirst;
1881 cb_stateRestoreIndices(struct fs_dump_state * state)
1884 struct FileEntry * fe;
1885 struct CallBack * cb;
1887 /* restore indices in the FileEntry structures */
1888 for (i = 1; i < state->fe_map.len; i++) {
1889 if (state->fe_map.entries[i].new_idx) {
1890 fe = itofe(state->fe_map.entries[i].new_idx);
1892 /* restore the fe->fnext entry */
1893 if (fe_OldToNew(state, fe->fnext, &fe->fnext)) {
1898 /* restore the fe->firstcb entry */
1899 if (cb_OldToNew(state, fe->firstcb, &fe->firstcb)) {
1906 /* restore indices in the CallBack structures */
1907 for (i = 1; i < state->cb_map.len; i++) {
1908 if (state->cb_map.entries[i].new_idx) {
1909 cb = itocb(state->cb_map.entries[i].new_idx);
1911 /* restore the cb->cnext entry */
1912 if (cb_OldToNew(state, cb->cnext, &cb->cnext)) {
1917 /* restore the cb->fhead entry */
1918 if (fe_OldToNew(state, cb->fhead, &cb->fhead)) {
1923 /* restore the cb->hhead entry */
1924 if (h_OldToNew(state, cb->hhead, &cb->hhead)) {
1929 /* restore the cb->tprev entry */
1930 if (cb_OldToNew(state, cb->tprev, &cb->tprev)) {
1935 /* restore the cb->tnext entry */
1936 if (cb_OldToNew(state, cb->tnext, &cb->tnext)) {
1941 /* restore the cb->hprev entry */
1942 if (cb_OldToNew(state, cb->hprev, &cb->hprev)) {
1947 /* restore the cb->hnext entry */
1948 if (cb_OldToNew(state, cb->hnext, &cb->hnext)) {
1955 /* restore the timeout queue head indices */
1956 for (i = 0; i < state->cb_timeout_hdr->records; i++) {
1957 if (cb_OldToNew(state, timeout[i], &timeout[i])) {
1963 /* restore the FE hash table queue heads */
1964 for (i = 0; i < state->cb_fehash_hdr->records; i++) {
1965 if (fe_OldToNew(state, HashTable[i], &HashTable[i])) {
1976 cb_stateVerify(struct fs_dump_state * state)
1980 if (cb_stateVerifyFEHash(state)) {
1984 if (cb_stateVerifyTimeoutQueues(state)) {
1992 cb_stateVerifyFEHash(struct fs_dump_state * state)
1995 struct FileEntry * fe;
1996 afs_uint32 fei, chain_len;
1998 for (i = 0; i < FEHASH_SIZE; i++) {
2000 for (fei = HashTable[i], fe = itofe(fei);
2002 fei = fe->fnext, fe = itofe(fei)) {
2003 if (fei > cbstuff.nblks) {
2004 ViceLog(0, ("cb_stateVerifyFEHash: error: index out of range (fei=%d)\n", fei));
2008 if (cb_stateVerifyFE(state, fe)) {
2011 if (chain_len > FS_STATE_FE_MAX_HASH_CHAIN_LEN) {
2012 ViceLog(0, ("cb_stateVerifyFEHash: error: hash chain %d length exceeds %d; assuming there's a loop\n",
2013 i, FS_STATE_FE_MAX_HASH_CHAIN_LEN));
2025 cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe)
2029 if ((fe->firstcb && !fe->ncbs) ||
2030 (!fe->firstcb && fe->ncbs)) {
2031 ViceLog(0, ("cb_stateVerifyFE: error: fe->firstcb does not agree with fe->ncbs (fei=%lu, fe->firstcb=%lu, fe->ncbs=%lu)\n",
2032 afs_printable_uint32_lu(fetoi(fe)),
2033 afs_printable_uint32_lu(fe->firstcb),
2034 afs_printable_uint32_lu(fe->ncbs)));
2037 if (cb_stateVerifyFCBList(state, fe)) {
2038 ViceLog(0, ("cb_stateVerifyFE: error: FCBList failed verification (fei=%lu)\n",
2039 afs_printable_uint32_lu(fetoi(fe))));
2047 cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe)
2050 afs_uint32 cbi, fei, chain_len = 0;
2051 struct CallBack * cb;
2055 for (cbi = fe->firstcb, cb = itocb(cbi);
2057 cbi = cb->cnext, cb = itocb(cbi)) {
2058 if (cbi > cbstuff.nblks) {
2059 ViceLog(0, ("cb_stateVerifyFCBList: error: list index out of range (cbi=%d, ncbs=%d)\n",
2060 cbi, cbstuff.nblks));
2064 if (cb->fhead != fei) {
2065 ViceLog(0, ("cb_stateVerifyFCBList: error: cb->fhead != fei (fei=%d, cb->fhead=%d)\n",
2069 if (chain_len > FS_STATE_FCB_MAX_LIST_LEN) {
2070 ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (fei=%d); assuming there's a loop\n",
2071 FS_STATE_FCB_MAX_LIST_LEN, fei));
2078 if (fe->ncbs != chain_len) {
2079 ViceLog(0, ("cb_stateVerifyFCBList: error: list length mismatch (len=%d, fe->ncbs=%d)\n",
2080 chain_len, fe->ncbs));
2089 cb_stateVerifyHCBList(struct fs_dump_state * state, struct host * host)
2092 afs_uint32 hi, chain_len, cbi;
2093 struct CallBack *cb, *ncb;
2098 for (cbi = host->cblist, cb = itocb(cbi);
2100 cbi = cb->hnext, cb = ncb) {
2101 if (chain_len && (host->cblist == cbi)) {
2102 /* we've wrapped around the circular list, and everything looks ok */
2105 if (cb->hhead != hi) {
2106 ViceLog(0, ("cb_stateVerifyHCBList: error: incorrect cb->hhead (cbi=%d, h->index=%d, cb->hhead=%d)\n",
2107 cbi, hi, cb->hhead));
2110 if (!cb->hprev || !cb->hnext) {
2111 ViceLog(0, ("cb_stateVerifyHCBList: error: null index in circular list (cbi=%d, h->index=%d)\n",
2116 if ((cb->hprev > cbstuff.nblks) ||
2117 (cb->hnext > cbstuff.nblks)) {
2118 ViceLog(0, ("cb_stateVerifyHCBList: error: list index out of range (cbi=%d, h->index=%d, cb->hprev=%d, cb->hnext=%d, nCBs=%d)\n",
2119 cbi, hi, cb->hprev, cb->hnext, cbstuff.nblks));
2123 ncb = itocb(cb->hnext);
2124 if (cbi != ncb->hprev) {
2125 ViceLog(0, ("cb_stateVerifyHCBList: error: corrupt linked list (cbi=%d, h->index=%d)\n",
2130 if (chain_len > FS_STATE_HCB_MAX_LIST_LEN) {
2131 ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (h->index=%d); assuming there's a loop\n",
2132 FS_STATE_HCB_MAX_LIST_LEN, hi));
2144 cb_stateVerifyTimeoutQueues(struct fs_dump_state * state)
2147 afs_uint32 cbi, chain_len;
2148 struct CallBack *cb, *ncb;
2150 for (i = 0; i < CB_NUM_TIMEOUT_QUEUES; i++) {
2152 for (cbi = timeout[i], cb = itocb(cbi);
2154 cbi = cb->tnext, cb = ncb) {
2155 if (chain_len && (cbi == timeout[i])) {
2156 /* we've wrapped around the circular list, and everything looks ok */
2159 if (cbi > cbstuff.nblks) {
2160 ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: list index out of range (cbi=%d, tindex=%d)\n",
2165 if (itot(cb->thead) != &timeout[i]) {
2166 ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: cb->thead points to wrong timeout queue (tindex=%d, cbi=%d, cb->thead=%d)\n",
2167 i, cbi, cb->thead));
2170 if (!cb->tprev || !cb->tnext) {
2171 ViceLog(0, ("cb_stateVerifyTimeoutQueues: null index in circular list (cbi=%d, tindex=%d)\n",
2176 if ((cb->tprev > cbstuff.nblks) ||
2177 (cb->tnext > cbstuff.nblks)) {
2178 ViceLog(0, ("cb_stateVerifyTimeoutQueues: list index out of range (cbi=%d, tindex=%d, cb->tprev=%d, cb->tnext=%d, nCBs=%d)\n",
2179 cbi, i, cb->tprev, cb->tnext, cbstuff.nblks));
2183 ncb = itocb(cb->tnext);
2184 if (cbi != ncb->tprev) {
2185 ViceLog(0, ("cb_stateVerifyTimeoutQueues: corrupt linked list (cbi=%d, tindex=%d)\n",
2190 if (chain_len > FS_STATE_TCB_MAX_LIST_LEN) {
2191 ViceLog(0, ("cb_stateVerifyTimeoutQueues: list length exceeds %d (tindex=%d); assuming there's a loop\n",
2192 FS_STATE_TCB_MAX_LIST_LEN, i));
2204 cb_stateSaveTimeouts(struct fs_dump_state * state)
2207 struct iovec iov[2];
2209 AssignInt64(state->eof_offset, &state->cb_hdr->timeout_offset);
2211 memset(state->cb_timeout_hdr, 0, sizeof(struct callback_state_fehash_header));
2212 state->cb_timeout_hdr->magic = CALLBACK_STATE_TIMEOUT_MAGIC;
2213 state->cb_timeout_hdr->records = CB_NUM_TIMEOUT_QUEUES;
2214 state->cb_timeout_hdr->len = sizeof(struct callback_state_timeout_header) +
2215 (state->cb_timeout_hdr->records * sizeof(afs_uint32));
2217 iov[0].iov_base = (char *)state->cb_timeout_hdr;
2218 iov[0].iov_len = sizeof(struct callback_state_timeout_header);
2219 iov[1].iov_base = (char *)timeout;
2220 iov[1].iov_len = sizeof(timeout);
2222 if (fs_stateSeek(state, &state->cb_hdr->timeout_offset)) {
2227 if (fs_stateWriteV(state, iov, 2)) {
2232 fs_stateIncEOF(state, state->cb_timeout_hdr->len);
2239 cb_stateRestoreTimeouts(struct fs_dump_state * state)
2243 if (fs_stateReadHeader(state, &state->cb_hdr->timeout_offset,
2244 state->cb_timeout_hdr,
2245 sizeof(struct callback_state_timeout_header))) {
2250 if (state->cb_timeout_hdr->magic != CALLBACK_STATE_TIMEOUT_MAGIC) {
2254 if (state->cb_timeout_hdr->records != CB_NUM_TIMEOUT_QUEUES) {
2259 len = state->cb_timeout_hdr->records * sizeof(afs_uint32);
2261 if (state->cb_timeout_hdr->len !=
2262 (sizeof(struct callback_state_timeout_header) + len)) {
2267 if (fs_stateRead(state, timeout, len)) {
2277 cb_stateSaveFEHash(struct fs_dump_state * state)
2280 struct iovec iov[2];
2282 AssignInt64(state->eof_offset, &state->cb_hdr->fehash_offset);
2284 memset(state->cb_fehash_hdr, 0, sizeof(struct callback_state_fehash_header));
2285 state->cb_fehash_hdr->magic = CALLBACK_STATE_FEHASH_MAGIC;
2286 state->cb_fehash_hdr->records = FEHASH_SIZE;
2287 state->cb_fehash_hdr->len = sizeof(struct callback_state_fehash_header) +
2288 (state->cb_fehash_hdr->records * sizeof(afs_uint32));
2290 iov[0].iov_base = (char *)state->cb_fehash_hdr;
2291 iov[0].iov_len = sizeof(struct callback_state_fehash_header);
2292 iov[1].iov_base = (char *)HashTable;
2293 iov[1].iov_len = sizeof(HashTable);
2295 if (fs_stateSeek(state, &state->cb_hdr->fehash_offset)) {
2300 if (fs_stateWriteV(state, iov, 2)) {
2305 fs_stateIncEOF(state, state->cb_fehash_hdr->len);
2312 cb_stateRestoreFEHash(struct fs_dump_state * state)
2316 if (fs_stateReadHeader(state, &state->cb_hdr->fehash_offset,
2317 state->cb_fehash_hdr,
2318 sizeof(struct callback_state_fehash_header))) {
2323 if (state->cb_fehash_hdr->magic != CALLBACK_STATE_FEHASH_MAGIC) {
2327 if (state->cb_fehash_hdr->records != FEHASH_SIZE) {
2332 len = state->cb_fehash_hdr->records * sizeof(afs_uint32);
2334 if (state->cb_fehash_hdr->len !=
2335 (sizeof(struct callback_state_fehash_header) + len)) {
2340 if (fs_stateRead(state, HashTable, len)) {
2350 cb_stateSaveFEs(struct fs_dump_state * state)
2353 register int fei, hash;
2354 register struct FileEntry *fe;
2356 AssignInt64(state->eof_offset, &state->cb_hdr->fe_offset);
2358 for (hash = 0; hash < FEHASH_SIZE ; hash++) {
2359 for (fei = HashTable[hash]; fei; fei = fe->fnext) {
2361 if (cb_stateSaveFE(state, fe)) {
2373 cb_stateRestoreFEs(struct fs_dump_state * state)
2375 int count, nFEs, ret = 0;
2377 nFEs = state->cb_hdr->nFEs;
2379 for (count = 0; count < nFEs; count++) {
2380 if (cb_stateRestoreFE(state)) {
2391 cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
2393 int ret = 0, iovcnt, cbi, written = 0;
2395 struct callback_state_entry_header hdr;
2396 struct FEDiskEntry fedsk;
2397 struct CBDiskEntry cbdsk[16];
2398 struct iovec iov[16];
2399 struct CallBack *cb;
2402 if (fei > state->cb_hdr->fe_max) {
2403 state->cb_hdr->fe_max = fei;
2406 memset(&hdr, 0, sizeof(struct callback_state_entry_header));
2408 if (cb_stateFEToDiskEntry(fe, &fedsk)) {
2413 iov[0].iov_base = (char *)&hdr;
2414 iov[0].iov_len = sizeof(hdr);
2415 iov[1].iov_base = (char *)&fedsk;
2416 iov[1].iov_len = sizeof(struct FEDiskEntry);
2419 for (cbi = fe->firstcb, cb = itocb(cbi);
2421 cbi = cb->cnext, cb = itocb(cbi), hdr.nCBs++) {
2422 if (cbi > state->cb_hdr->cb_max) {
2423 state->cb_hdr->cb_max = cbi;
2425 if (cb_stateCBToDiskEntry(cb, &cbdsk[iovcnt])) {
2429 cbdsk[iovcnt].index = cbi;
2430 iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
2431 iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
2433 if ((iovcnt == 16) || (!cb->cnext)) {
2434 if (fs_stateWriteV(state, iov, iovcnt)) {
2443 hdr.magic = CALLBACK_STATE_ENTRY_MAGIC;
2444 hdr.len = sizeof(hdr) + sizeof(struct FEDiskEntry) +
2445 (hdr.nCBs * sizeof(struct CBDiskEntry));
2448 if (fs_stateWriteV(state, iov, iovcnt)) {
2453 if (fs_stateWriteHeader(state, &state->eof_offset, &hdr, sizeof(hdr))) {
2459 fs_stateIncEOF(state, hdr.len);
2462 if (fs_stateSeek(state, &state->eof_offset)) {
2468 state->cb_hdr->nFEs++;
2469 state->cb_hdr->nCBs += hdr.nCBs;
2476 cb_stateRestoreFE(struct fs_dump_state * state)
2478 int ret = 0, iovcnt, nCBs;
2479 struct callback_state_entry_header hdr;
2480 struct FEDiskEntry fedsk;
2481 struct CBDiskEntry cbdsk[16];
2482 struct iovec iov[16];
2483 struct FileEntry * fe;
2485 iov[0].iov_base = (char *)&hdr;
2486 iov[0].iov_len = sizeof(hdr);
2487 iov[1].iov_base = (char *)&fedsk;
2488 iov[1].iov_len = sizeof(fedsk);
2491 if (fs_stateReadV(state, iov, iovcnt)) {
2496 if (hdr.magic != CALLBACK_STATE_ENTRY_MAGIC) {
2503 ViceLog(0, ("cb_stateRestoreFE: ran out of free FileEntry structures\n"));
2508 if (cb_stateDiskEntryToFE(state, &fedsk, fe)) {
2514 for (iovcnt = 0, nCBs = 0;
2517 iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
2518 iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
2520 if ((iovcnt == 16) || (nCBs == hdr.nCBs - 1)) {
2521 if (fs_stateReadV(state, iov, iovcnt)) {
2525 if (cb_stateRestoreCBs(state, fe, iov, iovcnt)) {
2539 cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
2540 struct iovec * iov, int niovecs)
2543 register struct CallBack * cb;
2544 struct CBDiskEntry * cbdsk;
2549 for (idx = 0; idx < niovecs; idx++) {
2550 cbdsk = (struct CBDiskEntry *) iov[idx].iov_base;
2551 if ((cb = GetCB()) == NULL) {
2552 ViceLog(0, ("cb_stateRestoreCBs: ran out of free CallBack structures\n"));
2556 if (cb_stateDiskEntryToCB(state, cbdsk, cb)) {
2557 ViceLog(0, ("cb_stateRestoreCBs: corrupt CallBack disk entry\n"));
2569 cb_stateFillHeader(struct callback_state_header * hdr)
2571 hdr->stamp.magic = CALLBACK_STATE_MAGIC;
2572 hdr->stamp.version = CALLBACK_STATE_VERSION;
2573 hdr->tfirst = tfirst;
2578 cb_stateCheckHeader(struct callback_state_header * hdr)
2582 if (hdr->stamp.magic != CALLBACK_STATE_MAGIC) {
2584 } else if (hdr->stamp.version != CALLBACK_STATE_VERSION) {
2586 } else if ((hdr->nFEs > cbstuff.nblks) || (hdr->nCBs > cbstuff.nblks)) {
2587 ViceLog(0, ("cb_stateCheckHeader: saved callback state larger than callback memory allocation\n"));
2593 /* disk entry conversion routines */
2595 cb_stateFEToDiskEntry(struct FileEntry * in, struct FEDiskEntry * out)
2597 memcpy(&out->fe, in, sizeof(struct FileEntry));
2598 out->index = fetoi(in);
2603 cb_stateDiskEntryToFE(struct fs_dump_state * state,
2604 struct FEDiskEntry * in, struct FileEntry * out)
2608 memcpy(out, &in->fe, sizeof(struct FileEntry));
2610 /* setup FE map entry */
2611 if (!in->index || (in->index >= state->fe_map.len)) {
2612 ViceLog(0, ("cb_stateDiskEntryToFE: index (%d) out of range",
2617 state->fe_map.entries[in->index].old_idx = in->index;
2618 state->fe_map.entries[in->index].new_idx = fetoi(out);
2625 cb_stateCBToDiskEntry(struct CallBack * in, struct CBDiskEntry * out)
2627 memcpy(&out->cb, in, sizeof(struct CallBack));
2628 out->index = cbtoi(in);
2633 cb_stateDiskEntryToCB(struct fs_dump_state * state,
2634 struct CBDiskEntry * in, struct CallBack * out)
2638 memcpy(out, &in->cb, sizeof(struct CallBack));
2640 /* setup CB map entry */
2641 if (!in->index || (in->index >= state->cb_map.len)) {
2642 ViceLog(0, ("cb_stateDiskEntryToCB: index (%d) out of range\n",
2647 state->cb_map.entries[in->index].old_idx = in->index;
2648 state->cb_map.entries[in->index].new_idx = cbtoi(out);
2654 /* index map routines */
2656 cb_stateAllocMap(struct fs_dump_state * state)
2658 state->fe_map.len = state->cb_hdr->fe_max + 1;
2659 state->cb_map.len = state->cb_hdr->cb_max + 1;
2660 state->fe_map.entries = (struct idx_map_entry_t *)
2661 calloc(state->fe_map.len, sizeof(struct idx_map_entry_t));
2662 state->cb_map.entries = (struct idx_map_entry_t *)
2663 calloc(state->cb_map.len, sizeof(struct idx_map_entry_t));
2664 return ((state->fe_map.entries != NULL) && (state->cb_map.entries != NULL)) ? 0 : 1;
2668 fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2672 /* FEs use a one-based indexing system, so old==0 implies no mapping */
2678 if (old >= state->fe_map.len) {
2679 ViceLog(0, ("fe_OldToNew: index %d is out of range\n", old));
2681 } else if (state->fe_map.entries[old].old_idx != old) { /* sanity check */
2682 ViceLog(0, ("fe_OldToNew: index %d points to an invalid FileEntry record\n", old));
2685 *new = state->fe_map.entries[old].new_idx;
2693 cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2697 /* CBs use a one-based indexing system, so old==0 implies no mapping */
2703 if (old >= state->cb_map.len) {
2704 ViceLog(0, ("cb_OldToNew: index %d is out of range\n", old));
2706 } else if (state->cb_map.entries[old].old_idx != old) { /* sanity check */
2707 ViceLog(0, ("cb_OldToNew: index %d points to an invalid CallBack record\n", old));
2710 *new = state->cb_map.entries[old].new_idx;
2716 #endif /* AFS_DEMAND_ATTACH_FS */
2719 DumpCallBackState(void)
2722 afs_uint32 magic = MAGICV2, now = (afs_int32) FT_ApproxTime(), freelisthead;
2724 oflag = O_WRONLY | O_CREAT | O_TRUNC;
2728 fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, oflag, 0666);
2731 ("Couldn't create callback dump file %s\n",
2732 AFSDIR_SERVER_CBKDUMP_FILEPATH));
2735 (void)write(fd, &magic, sizeof(magic));
2736 (void)write(fd, &now, sizeof(now));
2737 (void)write(fd, &cbstuff, sizeof(cbstuff));
2738 (void)write(fd, TimeOuts, sizeof(TimeOuts));
2739 (void)write(fd, timeout, sizeof(timeout));
2740 (void)write(fd, &tfirst, sizeof(tfirst));
2741 freelisthead = cbtoi((struct CallBack *)CBfree);
2742 (void)write(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
2743 freelisthead = fetoi((struct FileEntry *)FEfree);
2744 (void)write(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
2745 (void)write(fd, HashTable, sizeof(HashTable));
2746 (void)write(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks); /* CB stuff */
2747 (void)write(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks); /* FE stuff */
2753 #endif /* !INTERPRET_DUMP */
2755 #ifdef INTERPRET_DUMP
2757 /* This is only compiled in for the callback analyzer program */
2758 /* Returns the time of the dump */
2760 ReadDump(char *file, int timebits)
2763 afs_uint32 magic, freelisthead;
2765 #ifdef AFS_64BIT_ENV
2773 fd = open(file, oflag);
2775 fprintf(stderr, "Couldn't read dump file %s\n", file);
2778 read(fd, &magic, sizeof(magic));
2779 if (magic == MAGICV2) {
2782 if (magic != MAGIC) {
2784 "Magic number of %s is invalid. You might be trying to\n",
2787 "run this program on a machine type with a different byte ordering.\n");
2791 #ifdef AFS_64BIT_ENV
2792 if (timebits == 64) {
2793 read(fd, &now64, sizeof(afs_int64));
2794 now = (afs_int32) now64;
2797 read(fd, &now, sizeof(afs_int32));
2798 read(fd, &cbstuff, sizeof(cbstuff));
2799 read(fd, TimeOuts, sizeof(TimeOuts));
2800 read(fd, timeout, sizeof(timeout));
2801 read(fd, &tfirst, sizeof(tfirst));
2802 read(fd, &freelisthead, sizeof(freelisthead));
2803 CB = ((struct CallBack
2804 *)(calloc(cbstuff.nblks, sizeof(struct CallBack)))) - 1;
2805 FE = ((struct FileEntry
2806 *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
2807 CBfree = (struct CallBack *)itocb(freelisthead);
2808 read(fd, &freelisthead, sizeof(freelisthead));
2809 FEfree = (struct FileEntry *)itofe(freelisthead);
2810 read(fd, HashTable, sizeof(HashTable));
2811 read(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks); /* CB stuff */
2812 read(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks); /* FE stuff */
2814 perror("Error reading dumpfile");
2821 #include "AFS_component_version_number.h"
2823 #include "AFS_component_version_number.c"
2827 main(int argc, char **argv)
2829 int err = 0, cbi = 0, stats = 0, noptions = 0, all = 0, vol = 0, raw = 0;
2831 register struct FileEntry *fe;
2832 register struct CallBack *cb;
2836 memset(&fid, 0, sizeof(fid));
2839 while (argc && **argv == '-') {
2842 if (!strcmp(*argv, "-host")) {
2848 cbi = atoi(*++argv);
2849 } else if (!strcmp(*argv, "-fid")) {
2855 fid.Volume = atoi(*++argv);
2856 fid.Vnode = atoi(*++argv);
2857 fid.Unique = atoi(*++argv);
2858 } else if (!strcmp(*argv, "-time")) {
2859 fprintf(stderr, "-time not supported\n");
2861 } else if (!strcmp(*argv, "-stats")) {
2863 } else if (!strcmp(*argv, "-all")) {
2865 } else if (!strcmp(*argv, "-raw")) {
2867 } else if (!strcmp(*argv, "-timebits")) {
2873 timebits = atoi(*++argv);
2874 if ((timebits != 32)
2875 #ifdef AFS_64BIT_ENV
2880 } else if (!strcmp(*argv, "-volume")) {
2886 vol = atoi(*++argv);
2891 if (err || argc != 1) {
2893 "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] [-timebits 32"
2894 #ifdef AFS_64BIT_ENV
2897 "] callbackdumpfile\n");
2899 "[cbid is shown for each host in the hosts.dump file]\n");
2902 now = ReadDump(*argv, timebits);
2903 if (stats || noptions == 0) {
2904 time_t uxtfirst = UXtime(tfirst), tnow = now;
2905 printf("The time of the dump was %u %s", (unsigned int) now, ctime(&tnow));
2906 printf("The last time cleanup ran was %u %s", (unsigned int) uxtfirst,
2908 PrintCallBackStats();
2913 struct CallBack *cb;
2914 struct FileEntry *fe;
2916 for (hash = 0; hash < FEHASH_SIZE; hash++) {
2917 for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
2918 if (!vol || (fe->volid == vol)) {
2919 register struct CallBack *cbnext;
2920 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
2922 cbnext = itocb(cb->cnext);
2932 afs_uint32 cfirst = cbi;
2937 } while (cbi != cfirst);
2942 printf("No callback entries for %u.%u\n", fid.Volume, fid.Vnode);
2945 cb = itocb(fe->firstcb);
2948 cb = itocb(cb->cnext);
2953 for (i = 1; i < cbstuff.nblks; i++) {
2954 p = (afs_int32 *) & FE[i];
2955 printf("%d:%12x%12x%12x%12x\n", i, p[0], p[1], p[2], p[3]);
2962 PrintCB(register struct CallBack *cb, afs_uint32 now)
2964 struct FileEntry *fe = itofe(cb->fhead);
2965 time_t expires = TIndexToTime(cb->thead);
2970 printf("vol=%u vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %lu secs at %s",
2971 fe->volid, fe->vnode, fe->ncbs, cb->hhead, cb->status, fe->status,
2972 expires - now, ctime(&expires));
2977 #if !defined(INTERPRET_DUMP)
2979 ** try breaking calbacks on afidp from host. Use multi_rx.
2980 ** return 0 on success, non-zero on failure
2983 MultiBreakCallBackAlternateAddress(struct host *host, struct AFSCBFids *afidp)
2987 retVal = MultiBreakCallBackAlternateAddress_r(host, afidp);
2993 MultiBreakCallBackAlternateAddress_r(struct host *host,
2994 struct AFSCBFids *afidp)
2997 struct rx_connection **conns;
2998 struct rx_connection *connSuccess = 0;
2999 struct AddrPort *interfaces;
3000 static struct rx_securityClass *sc = 0;
3001 static struct AFSCBs tc = { 0, 0 };
3004 /* nothing more can be done */
3005 if (!host->interface)
3006 return 1; /* failure */
3008 /* the only address is the primary interface */
3009 if (host->interface->numberOfInterfaces <= 1)
3010 return 1; /* failure */
3012 /* initialise a security object only once */
3014 sc = rxnull_NewClientSecurityObject();
3016 i = host->interface->numberOfInterfaces;
3017 interfaces = calloc(i, sizeof(struct AddrPort));
3018 conns = calloc(i, sizeof(struct rx_connection *));
3019 if (!interfaces || !conns) {
3021 ("Failed malloc in MultiBreakCallBackAlternateAddress_r\n"));
3025 /* initialize alternate rx connections */
3026 for (i = 0, j = 0; i < host->interface->numberOfInterfaces; i++) {
3027 /* this is the current primary address */
3028 if (host->host == host->interface->interface[i].addr &&
3029 host->port == host->interface->interface[i].port)
3032 interfaces[j] = host->interface->interface[i];
3034 rx_NewConnection(interfaces[j].addr,
3035 interfaces[j].port, 1, sc, 0);
3036 rx_SetConnDeadTime(conns[j], 2);
3037 rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
3041 assert(j); /* at least one alternate address */
3043 ("Starting multibreakcall back on all addr for host %p (%s:%d)\n",
3044 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
3046 multi_Rx(conns, j) {
3047 multi_RXAFSCB_CallBack(afidp, &tc);
3051 if (host->callback_rxcon)
3052 rx_DestroyConnection(host->callback_rxcon);
3053 host->callback_rxcon = conns[multi_i];
3054 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
3055 host->host = interfaces[multi_i].addr;
3056 host->port = interfaces[multi_i].port;
3057 h_AddHostToAddrHashTable_r(host->host, host->port, host);
3058 connSuccess = conns[multi_i];
3059 rx_SetConnDeadTime(host->callback_rxcon, 50);
3060 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
3062 ("multibreakcall success with addr %s:%d\n",
3063 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3064 ntohs(interfaces[multi_i].port)));
3071 /* Destroy all connections except the one on which we succeeded */
3072 for (i = 0; i < j; i++)
3073 if (conns[i] != connSuccess)
3074 rx_DestroyConnection(conns[i]);
3080 return 0; /* success */
3082 return 1; /* failure */
3087 ** try multi_RX probes to host.
3088 ** return 0 on success, non-0 on failure
3091 MultiProbeAlternateAddress_r(struct host *host)
3094 struct rx_connection **conns;
3095 struct rx_connection *connSuccess = 0;
3096 struct AddrPort *interfaces;
3097 static struct rx_securityClass *sc = 0;
3100 /* nothing more can be done */
3101 if (!host->interface)
3102 return 1; /* failure */
3104 /* the only address is the primary interface */
3105 if (host->interface->numberOfInterfaces <= 1)
3106 return 1; /* failure */
3108 /* initialise a security object only once */
3110 sc = rxnull_NewClientSecurityObject();
3112 i = host->interface->numberOfInterfaces;
3113 interfaces = calloc(i, sizeof(struct AddrPort));
3114 conns = calloc(i, sizeof(struct rx_connection *));
3115 if (!interfaces || !conns) {
3116 ViceLog(0, ("Failed malloc in MultiProbeAlternateAddress_r\n"));
3120 /* initialize alternate rx connections */
3121 for (i = 0, j = 0; i < host->interface->numberOfInterfaces; i++) {
3122 /* this is the current primary address */
3123 if (host->host == host->interface->interface[i].addr &&
3124 host->port == host->interface->interface[i].port)
3127 interfaces[j] = host->interface->interface[i];
3129 rx_NewConnection(interfaces[j].addr,
3130 interfaces[j].port, 1, sc, 0);
3131 rx_SetConnDeadTime(conns[j], 2);
3132 rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
3136 assert(j); /* at least one alternate address */
3138 ("Starting multiprobe on all addr for host %p (%s:%d)\n",
3139 host, afs_inet_ntoa_r(host->host, hoststr),
3140 ntohs(host->port)));
3142 multi_Rx(conns, j) {
3143 multi_RXAFSCB_ProbeUuid(&host->interface->uuid);
3147 if (host->callback_rxcon)
3148 rx_DestroyConnection(host->callback_rxcon);
3149 host->callback_rxcon = conns[multi_i];
3150 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
3151 host->host = interfaces[multi_i].addr;
3152 host->port = interfaces[multi_i].port;
3153 h_AddHostToAddrHashTable_r(host->host, host->port, host);
3154 connSuccess = conns[multi_i];
3155 rx_SetConnDeadTime(host->callback_rxcon, 50);
3156 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
3158 ("multiprobe success with addr %s:%d\n",
3159 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3160 ntohs(interfaces[multi_i].port)));
3165 ("multiprobe failure with addr %s:%d\n",
3166 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3167 ntohs(interfaces[multi_i].port)));
3169 /* This is less than desirable but its the best we can do.
3170 * The AFS Cache Manager will return either 0 for a Uuid
3171 * match and a 1 for a non-match. If the error is 1 we
3172 * therefore know that our mapping of IP address to Uuid
3173 * is wrong. We should attempt to find the correct
3174 * Uuid and fix the host tables.
3176 if (multi_error == 1) {
3177 /* remove the current alternate address from this host */
3179 removeInterfaceAddr_r(host, interfaces[multi_i].addr, interfaces[multi_i].port);
3183 #ifdef AFS_DEMAND_ATTACH_FS
3184 /* try to bail ASAP if the fileserver is shutting down */
3186 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3195 /* Destroy all connections except the one on which we succeeded */
3196 for (i = 0; i < j; i++)
3197 if (conns[i] != connSuccess)
3198 rx_DestroyConnection(conns[i]);
3204 return 0; /* success */
3206 return 1; /* failure */
3209 #endif /* !defined(INTERPRET_DUMP) */