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
96 #include <afs/nfs.h> /* yuck. This is an abomination. */
98 #include <rx/rx_queue.h>
99 #include <afs/afscbint.h>
100 #include <afs/afsutil.h>
101 #include <afs/ihandle.h>
102 #include <afs/partition.h>
103 #include <afs/vnode.h>
104 #include <afs/volume.h>
105 #include "viced_prototypes.h"
108 #include <afs/ptclient.h> /* need definition of prlist for host.h */
110 #include "callback.h"
111 #ifdef AFS_DEMAND_ATTACH_FS
112 #include "serialize_state.h"
113 #endif /* AFS_DEMAND_ATTACH_FS */
116 extern afsUUID FS_HostUUID;
117 extern int hostCount;
119 #ifndef INTERPRET_DUMP
120 static int ShowProblems = 1;
123 struct cbcounters cbstuff;
125 static struct FileEntry * FE = NULL; /* don't use FE[0] */
126 static struct CallBack * CB = NULL; /* don't use CB[0] */
128 static struct CallBack * CBfree = NULL;
129 static struct FileEntry * FEfree = NULL;
132 /* Time to live for call backs depends upon number of users of the file.
133 * TimeOuts is indexed by this number/8 (using TimeOut macro). Times
134 * in this table are for the workstation; server timeouts, add
137 static int TimeOuts[] = {
138 /* Note: don't make the first entry larger than 4 hours (see above) */
139 4 * 60 * 60, /* 0-7 users */
140 1 * 60 * 60, /* 8-15 users */
141 30 * 60, /* 16-23 users */
142 15 * 60, /* 24-31 users */
143 15 * 60, /* 32-39 users */
144 10 * 60, /* 40-47 users */
145 10 * 60, /* 48-55 users */
146 10 * 60, /* 56-63 users */
147 }; /* Anything more: MinTimeOut */
149 /* minimum time given for a call back */
150 #ifndef INTERPRET_DUMP
151 static int MinTimeOut = (7 * 60);
154 /* Heads of CB queues; a timeout index is 1+index into this array */
155 static afs_uint32 timeout[CB_NUM_TIMEOUT_QUEUES];
157 static afs_int32 tfirst; /* cbtime of oldest unexpired call back time queue */
160 /* 16 byte object get/free routines */
165 /* Prototypes for static routines */
166 static struct FileEntry *FindFE(AFSFid * fid);
168 #ifndef INTERPRET_DUMP
169 static struct CallBack *iGetCB(int *nused);
170 static int iFreeCB(struct CallBack *cb, int *nused);
171 static struct FileEntry *iGetFE(int *nused);
172 static int iFreeFE(struct FileEntry *fe, int *nused);
173 static int TAdd(struct CallBack *cb, afs_uint32 * thead);
174 static int TDel(struct CallBack *cb);
175 static int HAdd(struct CallBack *cb, struct host *host);
176 static int HDel(struct CallBack *cb);
177 static int CDel(struct CallBack *cb, int deletefe);
178 static int CDelPtr(struct FileEntry *fe, afs_uint32 * cbp,
180 static afs_uint32 *FindCBPtr(struct FileEntry *fe, struct host *host);
181 static int FDel(struct FileEntry *fe);
182 static int AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead,
183 int type, int locked);
184 static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
185 struct AFSCBFids *afidp);
186 static int MultiBreakVolumeCallBack_r(struct host *host,
187 struct VCBParams *parms, int deletefe);
188 static int MultiBreakVolumeLaterCallBack(struct host *host, void *rock);
189 static int GetSomeSpace_r(struct host *hostp, int locked);
190 static int ClearHostCallbacks_r(struct host *hp, int locked);
191 static int DumpCallBackState_r(void);
194 #define GetCB() ((struct CallBack *)iGetCB(&cbstuff.nCBs))
195 #define GetFE() ((struct FileEntry *)iGetFE(&cbstuff.nFEs))
196 #define FreeCB(cb) iFreeCB((struct CallBack *)cb, &cbstuff.nCBs)
197 #define FreeFE(fe) iFreeFE((struct FileEntry *)fe, &cbstuff.nFEs)
200 /* Other protos - move out sometime */
201 void PrintCB(struct CallBack *cb, afs_uint32 now);
203 static afs_uint32 HashTable[FEHASH_SIZE]; /* File entry hash table */
205 static struct FileEntry *
210 struct FileEntry *fe;
212 hash = FEHash(fid->Volume, fid->Unique);
213 for (fei = HashTable[hash]; fei; fei = fe->fnext) {
215 if (fe->volid == fid->Volume && fe->unique == fid->Unique
216 && fe->vnode == fid->Vnode && (fe->status & FE_LATER) != FE_LATER)
222 #ifndef INTERPRET_DUMP
224 static struct CallBack *
227 struct CallBack *ret;
229 if ((ret = CBfree)) {
230 CBfree = (struct CallBack *)(((struct object *)ret)->next);
237 iFreeCB(struct CallBack *cb, int *nused)
239 ((struct object *)cb)->next = (struct object *)CBfree;
245 static struct FileEntry *
248 struct FileEntry *ret;
250 if ((ret = FEfree)) {
251 FEfree = (struct FileEntry *)(((struct object *)ret)->next);
258 iFreeFE(struct FileEntry *fe, int *nused)
260 ((struct object *)fe)->next = (struct object *)FEfree;
266 /* Add cb to end of specified timeout list */
268 TAdd(struct CallBack *cb, afs_uint32 * thead)
271 (*thead) = cb->tnext = cb->tprev = cbtoi(cb);
273 struct CallBack *thp = itocb(*thead);
275 cb->tprev = thp->tprev;
279 thp->tprev = (itocb(thp->tprev)->tnext = cbtoi(cb));
281 thp->tprev = cbtoi(cb);
284 cb->thead = ttoi(thead);
288 /* Delete call back entry from timeout list */
290 TDel(struct CallBack *cb)
292 afs_uint32 *thead = itot(cb->thead);
294 if (*thead == cbtoi(cb))
295 *thead = (*thead == cb->tnext ? 0 : cb->tnext);
296 if (itocb(cb->tprev))
297 itocb(cb->tprev)->tnext = cb->tnext;
298 if (itocb(cb->tnext))
299 itocb(cb->tnext)->tprev = cb->tprev;
303 /* Add cb to end of specified host list */
305 HAdd(struct CallBack *cb, struct host *host)
307 cb->hhead = h_htoi(host);
308 if (!host->z.cblist) {
309 host->z.cblist = cb->hnext = cb->hprev = cbtoi(cb);
311 struct CallBack *fcb = itocb(host->z.cblist);
313 cb->hprev = fcb->hprev;
314 cb->hnext = cbtoi(fcb);
315 fcb->hprev = (itocb(fcb->hprev)->hnext = cbtoi(cb));
320 /* Delete call back entry from host list */
322 HDel(struct CallBack *cb)
324 afs_uint32 *hhead = &h_itoh(cb->hhead)->z.cblist;
326 if (*hhead == cbtoi(cb))
327 *hhead = (*hhead == cb->hnext ? 0 : cb->hnext);
328 itocb(cb->hprev)->hnext = cb->hnext;
329 itocb(cb->hnext)->hprev = cb->hprev;
333 /* Delete call back entry from fid's chain of cb's */
334 /* N.B. This one also deletes the CB, and also possibly parent FE, so
335 * make sure that it is not on any other list before calling this
338 CDel(struct CallBack *cb, int deletefe)
341 struct FileEntry *fe = itofe(cb->fhead);
345 for (safety = 0, cbp = &fe->firstcb; *cbp && *cbp != cbi;
346 cbp = &itocb(*cbp)->cnext, safety++) {
347 if (safety > cbstuff.nblks + 10) {
348 ViceLogThenPanic(0, ("CDel: Internal Error -- shutting down: "
349 "wanted %d from %d, now at %d\n",
350 cbi, fe->firstcb, *cbp));
351 DumpCallBackState_r();
352 ShutDownAndCore(PANIC);
355 CDelPtr(fe, cbp, deletefe);
359 /* Same as CDel, but pointer to parent pointer to CB entry is passed,
360 * as well as file entry */
361 /* N.B. This one also deletes the CB, and also possibly parent FE, so
362 * make sure that it is not on any other list before calling this
364 static int Ccdelpt = 0, CcdelB = 0;
367 CDelPtr(struct FileEntry *fe, afs_uint32 * cbp,
380 if ((--fe->ncbs == 0) && deletefe)
386 FindCBPtr(struct FileEntry *fe, struct host *host)
388 afs_uint32 hostindex = h_htoi(host);
393 for (safety = 0, cbp = &fe->firstcb; *cbp; cbp = &cb->cnext, safety++) {
394 if (safety > cbstuff.nblks) {
395 ViceLog(0, ("FindCBPtr: Internal Error -- shutting down.\n"));
396 DumpCallBackState_r();
397 ShutDownAndCore(PANIC);
400 if (cb->hhead == hostindex)
406 /* Delete file entry from hash table */
408 FDel(struct FileEntry *fe)
411 afs_uint32 *p = &HashTable[FEHash(fe->volid, fe->unique)];
413 while (*p && *p != fei)
414 p = &itofe(*p)->fnext;
421 /* initialize the callback package */
423 InitCallBack(int nblks)
425 opr_Assert(nblks > 0);
428 tfirst = CBtime(time(NULL));
429 /* N.B. The "-1", below, is because
430 * FE[0] and CB[0] are not used--and not allocated */
431 FE = calloc(nblks, sizeof(struct FileEntry));
433 ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
435 FE--; /* FE[0] is supposed to point to junk */
436 cbstuff.nFEs = nblks;
438 FreeFE(&FE[cbstuff.nFEs]); /* This is correct */
439 CB = calloc(nblks, sizeof(struct CallBack));
441 ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
443 CB--; /* CB[0] is supposed to point to junk */
444 cbstuff.nCBs = nblks;
446 FreeCB(&CB[cbstuff.nCBs]); /* This is correct */
447 cbstuff.nblks = nblks;
448 cbstuff.nbreakers = 0;
454 XCallBackBulk_r(struct host * ahost, struct AFSFid * fids, afs_int32 nfids)
456 struct AFSCallBack tcbs[AFSCBMAX];
462 struct rx_connection *cb_conn = NULL;
464 rx_SetConnDeadTime(ahost->z.callback_rxcon, 4);
465 rx_SetConnHardDeadTime(ahost->z.callback_rxcon, AFS_HARDDEADTIME);
471 for (i = 0; i < nfids && i < AFSCBMAX; i++) {
472 tcbs[i].CallBackVersion = CALLBACK_VERSION;
473 tcbs[i].ExpirationTime = 0;
474 tcbs[i].CallBackType = CB_DROPPED;
476 tf.AFSCBFids_len = i;
477 tf.AFSCBFids_val = &(fids[j]);
481 tc.AFSCBs_val = tcbs;
483 cb_conn = ahost->z.callback_rxcon;
484 rx_GetConnection(cb_conn);
486 code |= RXAFSCB_CallBack(cb_conn, &tf, &tc);
487 rx_PutConnection(cb_conn);
495 /* the locked flag tells us if the host entry has already been locked
496 * by our parent. I don't think anybody actually calls us with the
497 * host locked, but here's how to make that work: GetSomeSpace has to
498 * change so that it doesn't attempt to lock any hosts < "host". That
499 * means that it might be unable to free any objects, so it has to
500 * return an exit status. If it fails, then AddCallBack1 might fail,
501 * as well. If so, the host->ResetDone should probably be set to 0,
502 * and we probably don't want to return a callback promise to the
503 * cache manager, either. */
505 AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
513 if (!(host->z.hostFlags & HOSTDELETED))
514 retVal = AddCallBack1_r(host, fid, thead, type, 1);
524 AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
527 struct FileEntry *fe;
528 struct CallBack *cb = 0, *lastcb = 0;
529 struct FileEntry *newfe = 0;
530 afs_uint32 time_out = 0;
531 afs_uint32 *Thead = thead;
532 struct CallBack *newcb = 0;
535 cbstuff.AddCallBacks++;
537 host->z.Console |= 2;
539 /* allocate these guys first, since we can't call the allocator with
540 * the host structure locked -- or we might deadlock. However, we have
541 * to avoid races with FindFE... */
542 while (!(newcb = GetCB())) {
543 GetSomeSpace_r(host, locked);
545 while (!(newfe = GetFE())) { /* Get it now, so we don't have to call */
546 /* GetSomeSpace with the host locked, later. This might turn out to */
547 /* have been unneccessary, but that's actually kind of unlikely, since */
548 /* most files are not shared. */
549 GetSomeSpace_r(host, locked);
553 h_Lock_r(host); /* this can yield, so do it before we get any */
555 if (host->z.hostFlags & HOSTDELETED) {
556 host->z.Console &= ~2;
563 if (type == CB_NORMAL) {
565 TimeCeiling(time(NULL) + TimeOut(fe ? fe->ncbs : 0) +
567 Thead = THead(CBtime(time_out));
568 } else if (type == CB_VOLUME) {
569 time_out = TimeCeiling((60 * 120 + time(NULL)) + ServerBias);
570 Thead = THead(CBtime(time_out));
571 } else if (type == CB_BULK) {
572 /* bulk status can get so many callbacks all at once, and most of them
573 * are probably not for things that will be used for long.
576 TimeCeiling(time(NULL) + ServerBias +
577 TimeOut(22 + (fe ? fe->ncbs : 0)));
578 Thead = THead(CBtime(time_out));
581 host->z.Console &= ~2;
589 fe->volid = fid->Volume;
590 fe->vnode = fid->Vnode;
591 fe->unique = fid->Unique;
594 hash = FEHash(fid->Volume, fid->Unique);
595 fe->fnext = HashTable[hash];
596 HashTable[hash] = fetoi(fe);
598 for (safety = 0, lastcb = cb = itocb(fe->firstcb); cb;
599 lastcb = cb, cb = itocb(cb->cnext), safety++) {
600 if (safety > cbstuff.nblks) {
601 ViceLog(0, ("AddCallBack1: Internal Error -- shutting down.\n"));
602 DumpCallBackState_r();
603 ShutDownAndCore(PANIC);
605 if (cb->hhead == h_htoi(host))
608 if (cb) { /* Already have call back: move to new timeout list */
609 /* don't change delayed callbacks back to normal ones */
610 if (cb->status != CB_DELAYED)
612 /* Only move if new timeout is longer */
613 if (TNorm(ttoi(Thead)) > TNorm(cb->thead)) {
617 if (newfe == NULL) { /* we are using the new FE */
618 fe->firstcb = cbtoi(cb);
620 cb->fhead = fetoi(fe);
625 *(lastcb ? &lastcb->cnext : &fe->firstcb) = cbtoi(cb);
628 cb->fhead = fetoi(fe);
635 /* now free any still-unused callback or host entries */
641 if (!locked) /* freecb and freefe might(?) yield */
644 if (type == CB_NORMAL || type == CB_VOLUME || type == CB_BULK)
645 return time_out - ServerBias; /* Expires sooner at workstation */
651 CompareCBA(const void *e1, const void *e2)
653 const struct cbstruct *cba1 = (const struct cbstruct *)e1;
654 const struct cbstruct *cba2 = (const struct cbstruct *)e2;
655 return ((cba1->hp)->index - (cba2->hp)->index);
658 /* Take an array full of hosts, all held. Break callbacks to them, and
659 * release the holds once you're done.
660 * Currently only works for a single Fid in afidp array.
661 * If you want to make this work with multiple fids, you need to fix
662 * the error handling. One approach would be to force a reset if a
663 * multi-fid call fails, or you could add delayed callbacks for each
664 * fid. You probably also need to sort and remove duplicate hosts.
665 * When this is called from the BreakVolumeCallBacks path, it does NOT
666 * force a reset if the RPC fails, it just marks the host down and tries
667 * to create a delayed callback. */
668 /* N.B. be sure that code works when ncbas == 0 */
669 /* N.B. requires all the cba[*].hp pointers to be valid... */
670 /* This routine does not hold a lock on the host for the duration of
671 * the BreakCallBack RPC, which is a significant deviation from tradition.
672 * It _does_ get a lock on the host before setting VenusDown = 1,
673 * which is sufficient only if VenusDown = 0 only happens when the
674 * lock is held over the RPC and the subsequent VenusDown == 0
675 * wherever that is done. */
677 MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
678 struct AFSCBFids *afidp)
681 struct rx_connection *conns[MAX_CB_HOSTS];
682 static struct AFSCBs tc = { 0, 0 };
683 int multi_to_cba_map[MAX_CB_HOSTS];
685 opr_Assert(ncbas <= MAX_CB_HOSTS);
688 * When we issue a multi_Rx callback break, we must rx_NewCall a call for
689 * each host before we do anything. If there are no call channels
690 * available on the conn, we must wait for one of the existing calls to
691 * finish. If another thread is breaking callbacks at the same time, it is
692 * possible for us to be waiting on NewCall for one of their multi_Rx
693 * CallBack calls to finish, but they are waiting on NewCall for one of
694 * our calls to finish. So we deadlock.
696 * This can be thought of as similar to obtaining multiple locks at the
697 * same time. So if we establish an ordering, the possibility of deadlock
698 * goes away. Here we provide such an ordering, by sorting our CBAs
699 * according to CompareCBA.
701 qsort(cba, ncbas, sizeof(struct cbstruct), CompareCBA);
703 /* set up conns for multi-call */
704 for (i = 0, j = 0; i < ncbas; i++) {
705 struct host *thishost = cba[i].hp;
706 if (!thishost || (thishost->z.hostFlags & HOSTDELETED)) {
709 rx_GetConnection(thishost->z.callback_rxcon);
710 multi_to_cba_map[j] = i;
711 conns[j++] = thishost->z.callback_rxcon;
713 rx_SetConnDeadTime(thishost->z.callback_rxcon, 4);
714 rx_SetConnHardDeadTime(thishost->z.callback_rxcon, AFS_HARDDEADTIME);
717 if (j) { /* who knows what multi would do with 0 conns? */
721 multi_RXAFSCB_CallBack(afidp, &tc);
727 i = multi_to_cba_map[multi_i];
733 ("BCB: INTERNAL ERROR: hp=%p, cba=%p, thead=%u\n",
737 ** try breaking callbacks on alternate interface addresses
739 if (MultiBreakCallBackAlternateAddress(hp, afidp)) {
742 ("BCB: Failed on file %u.%u.%u, "
743 "Host %p (%s:%d) is down\n",
744 afidp->AFSCBFids_val->Volume,
745 afidp->AFSCBFids_val->Vnode,
746 afidp->AFSCBFids_val->Unique,
748 afs_inet_ntoa_r(hp->z.host, hoststr),
754 if (!(hp->z.hostFlags & HOSTDELETED)) {
755 hp->z.hostFlags |= VENUSDOWN;
757 * We always go into AddCallBack1_r with the host locked
759 AddCallBack1_r(hp, afidp->AFSCBFids_val, itot(idx),
773 for (i = 0; i < ncbas; i++) {
781 /* H_UNLOCK around this so h_FreeConnection does not deadlock.
782 h_FreeConnection should *never* be called on a callback connection,
783 but on 10/27/04 a deadlock occurred where it was, when we know why,
784 this should be reverted. -- shadow */
786 for (i = 0; i < j; i++) {
787 rx_PutConnection(conns[i]);
795 * Break all call backs for fid, except for the specified host (unless flag
796 * is true, in which case all get a callback message. Assumption: the specified
797 * host is h_Held, by the caller; the others aren't.
798 * Specified host may be bogus, that's ok. This used to check to see if the
799 * host was down in two places, once right after the host was h_held, and
800 * again after it was locked. That race condition is incredibly rare and
801 * relatively harmless even when it does occur, so we don't check for it now.
803 /* if flag is true, send a break callback msg to "host", too */
805 BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
807 struct FileEntry *fe;
808 struct CallBack *cb, *nextcb;
809 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->z.host, hoststr), ntohs(xhost->z.port),
819 fid->Volume, fid->Vnode, fid->Unique));
822 ("BCB: BreakCallBack(No Host, (%u,%u,%u))\n",
823 fid->Volume, fid->Vnode, fid->Unique));
826 cbstuff.BreakCallBacks++;
831 hostindex = xhost ? h_htoi(xhost) : 0;
832 cb = itocb(fe->firstcb);
833 if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
834 /* the most common case is what follows the || */
837 tf.AFSCBFids_len = 1;
838 tf.AFSCBFids_val = fid;
840 /* Set CBFLAG_BREAKING flag on all CBs we're looking at. We do this so we
841 * can loop through all relevant CBs while dropping H_LOCK, and not lose
842 * track of which CBs we want to look at. If we look at all CBs over and
843 * over again, we can loop indefinitely as new CBs are added. */
844 for (; cb; cb = nextcb) {
845 nextcb = itocb(cb->cnext);
847 if ((cb->hhead != hostindex || flag)
848 && (cb->status == CB_BULK || cb->status == CB_NORMAL
849 || cb->status == CB_VOLUME)) {
850 cb->flags |= CBFLAG_BREAKING;
854 cb = itocb(fe->firstcb);
857 /* loop through all CBs, only looking at ones with the CBFLAG_BREAKING
860 for (ncbas = 0; cb && ncbas < MAX_CB_HOSTS; cb = nextcb) {
861 nextcb = itocb(cb->cnext);
862 if ((cb->flags & CBFLAG_BREAKING)) {
863 struct host *thishost = h_itoh(cb->hhead);
864 cb->flags &= ~CBFLAG_BREAKING;
866 ViceLog(0, ("BCB: BOGUS! cb->hhead is NULL!\n"));
867 } else if (thishost->z.hostFlags & VENUSDOWN) {
869 ("BCB: %p (%s:%d) is down; delaying break call back\n",
870 thishost, afs_inet_ntoa_r(thishost->z.host, hoststr),
871 ntohs(thishost->z.port)));
872 cb->status = CB_DELAYED;
874 if (!(thishost->z.hostFlags & HOSTDELETED)) {
876 cba[ncbas].hp = thishost;
877 cba[ncbas].thead = cb->thead;
882 CDel(cb, 1); /* Usually first; so this delete
883 * is reasonably inexpensive */
889 MultiBreakCallBack_r(cba, ncbas, &tf);
891 /* we need to to all these initializations again because MultiBreakCallBack may block */
896 cb = itocb(fe->firstcb);
897 if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
898 /* the most common case is what follows the || */
909 /* Delete (do not break) single call back for fid */
911 DeleteCallBack(struct host *host, AFSFid * fid)
913 struct FileEntry *fe;
918 cbstuff.DeleteCallBacks++;
921 /* do not care if the host has been HOSTDELETED */
927 ("DCB: No call backs for fid (%u, %u, %u)\n", fid->Volume,
928 fid->Vnode, fid->Unique));
931 pcb = FindCBPtr(fe, host);
934 ("DCB: No call back for host %p (%s:%d), (%u, %u, %u)\n",
935 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port),
936 fid->Volume, fid->Vnode, fid->Unique));
950 * Delete (do not break) all call backs for fid. This call doesn't
951 * set all of the various host locks, but it shouldn't really matter
952 * since we're not adding callbacks, but deleting them. I'm not sure
953 * why it doesn't set the lock, however; perhaps it should.
956 DeleteFileCallBacks(AFSFid * fid)
958 struct FileEntry *fe;
964 cbstuff.DeleteFiles++;
969 ("DF: No fid (%u,%u,%u) to delete\n", fid->Volume, fid->Vnode,
973 for (n = 0, cbi = fe->firstcb; cbi; n++) {
986 /* Delete (do not break) all call backs for host. The host should be
989 DeleteAllCallBacks_r(struct host *host, int deletefe)
994 cbstuff.DeleteAllCallBacks++;
995 cbi = first = host->z.cblist;
997 ViceLog(8, ("DV: no call backs\n"));
1005 } while (cbi != first);
1011 * Break all delayed call backs for host. Returns 1 if all call backs
1012 * successfully broken; 0 otherwise. Assumes host is h_Held and h_Locked.
1013 * Must be called with VenusDown set for this host
1016 BreakDelayedCallBacks(struct host *host)
1020 retVal = BreakDelayedCallBacks_r(host);
1026 BreakDelayedCallBacks_r(struct host *host)
1028 struct AFSFid fids[AFSCBMAX];
1029 int cbi, first, nfids;
1030 struct CallBack *cb;
1033 struct rx_connection *cb_conn;
1035 cbstuff.nbreakers++;
1036 if (!(host->z.hostFlags & RESETDONE) && !(host->z.hostFlags & HOSTDELETED)) {
1037 host->z.hostFlags &= ~ALTADDR; /* alternate addresses are invalid */
1038 host->z.hostFlags |= HWHO_INPROGRESS; /* ICBS(3) invokes thread quota */
1039 cb_conn = host->z.callback_rxcon;
1040 rx_GetConnection(cb_conn);
1041 if (host->z.interface) {
1044 RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
1047 code = RXAFSCB_InitCallBackState(cb_conn);
1049 rx_PutConnection(cb_conn);
1052 host->z.hostFlags |= ALTADDR; /* alternate addresses are valid */
1053 host->z.hostFlags &= ~HWHO_INPROGRESS;
1057 ("CB: Call back connect back failed (in break delayed) "
1058 "for Host %p (%s:%d)\n",
1059 host, afs_inet_ntoa_r(host->z.host, hoststr),
1060 ntohs(host->z.port)));
1062 host->z.hostFlags |= VENUSDOWN;
1065 ("InitCallBackState success on %p (%s:%d)\n",
1066 host, afs_inet_ntoa_r(host->z.host, hoststr),
1067 ntohs(host->z.port)));
1068 /* reset was done successfully */
1069 host->z.hostFlags |= RESETDONE;
1070 host->z.hostFlags &= ~VENUSDOWN;
1073 while (!(host->z.hostFlags & HOSTDELETED)) {
1075 host->z.hostFlags &= ~VENUSDOWN; /* presume up */
1076 cbi = first = host->z.cblist;
1080 first = host->z.cblist;
1083 if (cb->status == CB_DELAYED) {
1084 struct FileEntry *fe = itofe(cb->fhead);
1085 fids[nfids].Volume = fe->volid;
1086 fids[nfids].Vnode = fe->vnode;
1087 fids[nfids].Unique = fe->unique;
1093 } while (cbi && cbi != first && nfids < AFSCBMAX);
1099 if (XCallBackBulk_r(host, fids, nfids)) {
1100 /* Failed, again: put them back, probably with old
1105 ("CB: XCallBackBulk failed, Host %p (%s:%d); "
1106 "callback list follows:\n",
1107 host, afs_inet_ntoa_r(host->z.host, hoststr),
1108 ntohs(host->z.port)));
1110 for (i = 0; i < nfids; i++) {
1113 ("CB: Host %p (%s:%d), file %u.%u.%u "
1114 "(part of bulk callback)\n",
1115 host, afs_inet_ntoa_r(host->z.host, hoststr),
1116 ntohs(host->z.port), fids[i].Volume,
1117 fids[i].Vnode, fids[i].Unique));
1120 * AddCallBack1_r(host, &fids[i], itot(thead[i]), CB_DELAYED, 1);
1121 * * but it turns out to cause too many tricky locking problems.
1122 * * now, if break delayed fails, screw it. */
1124 host->z.hostFlags |= VENUSDOWN; /* Failed */
1125 ClearHostCallbacks_r(host, 1 /* locked */ );
1128 if (nfids < AFSCBMAX)
1132 cbstuff.nbreakers--;
1133 /* If we succeeded it's always ok to unset HFE_LATER */
1134 if (!(host->z.hostFlags & VENUSDOWN))
1135 host->z.hostFlags &= ~HFE_LATER;
1136 return (host->z.hostFlags & VENUSDOWN);
1140 MultiBreakVolumeCallBack_r(struct host *host,
1141 struct VCBParams *parms, int deletefe)
1145 if (host->z.hostFlags & HOSTDELETED)
1148 if (!(host->z.hostFlags & HCBREAK))
1149 return 0; /* host is not flagged to notify */
1151 if (host->z.hostFlags & VENUSDOWN) {
1153 /* Do not care if the host is now HOSTDELETED */
1156 ("BVCB: volume callback for Host %p (%s:%d) failed\n",
1157 host, afs_inet_ntoa_r(host->z.host, hoststr),
1158 ntohs(host->z.port)));
1160 DeleteAllCallBacks_r(host, deletefe); /* Delete all callback state
1161 * rather than attempting to
1162 * selectively remember to
1163 * delete the volume callbacks
1165 host->z.hostFlags &= ~(RESETDONE|HCBREAK); /* Do InitCallBackState when host returns */
1169 opr_Assert(parms->ncbas <= MAX_CB_HOSTS);
1171 /* Do not call MultiBreakCallBack on the current host structure
1172 ** because it would prematurely release the hold on the host
1174 if (parms->ncbas == MAX_CB_HOSTS) {
1175 struct AFSCBFids tf;
1177 tf.AFSCBFids_len = 1;
1178 tf.AFSCBFids_val = parms->fid;
1180 /* this releases all the hosts */
1181 MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf);
1185 parms->cba[parms->ncbas].hp = host;
1186 parms->cba[(parms->ncbas)++].thead = parms->thead;
1187 host->z.hostFlags &= ~HCBREAK;
1189 /* we have more work to do on this host, so make sure we keep a reference
1197 MultiBreakVolumeLaterCallBack(struct host *host, void *rock)
1199 struct VCBParams *parms = (struct VCBParams *)rock;
1202 retval = MultiBreakVolumeCallBack_r(host, parms, 0);
1208 * Break all call backs on a single volume. Don't call this with any
1209 * hosts h_held. Note that this routine clears the callbacks before
1210 * actually breaking them, and that the vnode isn't locked during this
1211 * operation, so that people might see temporary callback loss while
1212 * this function is executing. It is just a temporary state, however,
1213 * since the callback will be broken later by this same function.
1215 * Now uses multi-RX for CallBack RPC in a different thread,
1216 * only marking them here.
1218 extern pthread_cond_t fsync_cond;
1221 BreakVolumeCallBacksLater(VolumeId volume)
1225 struct FileEntry *fe;
1226 struct CallBack *cb;
1230 ViceLog(25, ("Setting later on volume %" AFS_VOLID_FMT "\n",
1231 afs_printable_VolumeId_lu(volume)));
1233 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1234 for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
1235 if (fe->volid == volume) {
1236 struct CallBack *cbnext;
1237 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1238 host = h_itoh(cb->hhead);
1239 host->z.hostFlags |= HFE_LATER;
1240 cb->status = CB_DELAYED;
1241 cbnext = itocb(cb->cnext);
1244 fe->status |= FE_LATER;
1253 /* didn't find any callbacks, so return right away. */
1257 ViceLog(25, ("Fsync thread wakeup\n"));
1259 opr_cv_broadcast(&fsync_cond);
1265 BreakLaterCallBacks(void)
1270 struct CallBack *cb;
1271 struct FileEntry *fe = NULL;
1272 struct FileEntry *myfe = NULL;
1274 struct VCBParams henumParms;
1275 unsigned short tthead = 0; /* zero is illegal value */
1279 ViceLog(25, ("Looking for FileEntries to unchain\n"));
1282 /* Pick the first volume we see to clean up */
1283 fid.Volume = fid.Vnode = fid.Unique = 0;
1285 for (hash = 0; hash < FEHASH_SIZE; hash++) {
1286 for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
1287 if (fe && (fe->status & FE_LATER)
1288 && (fid.Volume == 0 || fid.Volume == fe->volid)) {
1289 /* Ugly, but used to avoid left side casting */
1290 struct object *tmpfe;
1292 ("Unchaining for %u:%u:%" AFS_VOLID_FMT "\n", fe->vnode,
1293 fe->unique, afs_printable_VolumeId_lu(fe->volid)));
1294 fid.Volume = fe->volid;
1296 fe->status &= ~FE_LATER; /* not strictly needed */
1297 /* Works since volid is deeper than the largest pointer */
1298 tmpfe = (struct object *)fe;
1299 tmpfe->next = (struct object *)myfe;
1312 /* loop over FEs from myfe and free/break */
1314 for (fe = myfe; fe;) {
1315 struct CallBack *cbnext;
1316 for (cb = itocb(fe->firstcb); cb; cb = cbnext) {
1317 cbnext = itocb(cb->cnext);
1318 host = h_itoh(cb->hhead);
1319 if (cb->status == CB_DELAYED) {
1320 if (!(host->z.hostFlags & HOSTDELETED)) {
1321 /* mark this host for notification */
1322 host->z.hostFlags |= HCBREAK;
1323 if (!tthead || (TNorm(tthead) < TNorm(cb->thead))) {
1329 CDel(cb, 0); /* Don't let CDel clean up the fe */
1330 /* leave flag for MultiBreakVolumeCallBack to clear */
1333 ("Found host %p (%s:%d) non-DELAYED cb for %u:%u:%" AFS_VOLID_FMT "\n",
1334 host, afs_inet_ntoa_r(host->z.host, hoststr),
1335 ntohs(host->z.port), fe->vnode, fe->unique,
1336 afs_printable_VolumeId_lu(fe->volid)));
1340 fe = (struct FileEntry *)((struct object *)fe)->next;
1345 ViceLog(125, ("Breaking volume %u\n", fid.Volume));
1346 henumParms.ncbas = 0;
1347 henumParms.fid = &fid;
1348 henumParms.thead = tthead;
1350 h_Enumerate(MultiBreakVolumeLaterCallBack, (char *)&henumParms);
1352 if (henumParms.ncbas) { /* do left-overs */
1353 struct AFSCBFids tf;
1354 tf.AFSCBFids_len = 1;
1355 tf.AFSCBFids_val = &fid;
1357 MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf);
1358 henumParms.ncbas = 0;
1363 /* Arrange to be called again */
1368 * Delete all timed-out call back entries (to be called periodically by file
1372 CleanupTimedOutCallBacks(void)
1377 code = CleanupTimedOutCallBacks_r();
1383 CleanupTimedOutCallBacks_r(void)
1385 afs_uint32 now = CBtime(time(NULL));
1387 struct CallBack *cb;
1391 while (tfirst <= now) {
1393 cbi = *(thead = THead(tfirst));
1399 ("CCB: deleting timed out call back %x (%s:%d), (%" AFS_VOLID_FMT ",%u,%u)\n",
1400 h_itoh(cb->hhead)->z.host,
1401 afs_inet_ntoa_r(h_itoh(cb->hhead)->z.host, hoststr),
1402 h_itoh(cb->hhead)->z.port,
1403 afs_printable_VolumeId_lu(itofe(cb->fhead)->volid),
1404 itofe(cb->fhead)->vnode, itofe(cb->fhead)->unique));
1408 if (ntimedout > cbstuff.nblks) {
1409 ViceLog(0, ("CCB: Internal Error -- shutting down...\n"));
1410 DumpCallBackState_r();
1411 ShutDownAndCore(PANIC);
1413 } while (cbi != *thead);
1418 cbstuff.CBsTimedOut += ntimedout;
1419 ViceLog(7, ("CCB: deleted %d timed out callbacks\n", ntimedout));
1420 return (ntimedout > 0);
1424 * parameters to pass to lih*_r from h_Enumerate_r when trying to find a host
1425 * from which to clear callbacks.
1429 * Points to the least interesting host found; try to clear callbacks on
1430 * this host after h_Enumerate_r(lih*_r)'ing.
1435 * The last host we got from lih*_r, but we couldn't clear its callbacks
1436 * for some reason. Choose the next-best host after this one (with the
1437 * current lih*_r, this means to only select hosts that have an ActiveCall
1438 * newer than lastlih).
1440 struct host *lastlih;
1443 /* Value of host->z.refCount that allows us to reliably infer that
1444 * host may be held by some other thread */
1445 #define OTHER_MUSTHOLD_LIH 2
1447 /* This version does not allow 'host' to be selected unless its ActiveCall
1448 * is newer than 'params->lastlih' which is the host with the oldest
1449 * ActiveCall from the last pass (if it is provided). We filter out any hosts
1450 * that are are held by other threads.
1452 * There is a small problem here, but it may not be easily fixable. Say we
1453 * select some host A, and give it back to GetSomeSpace_r. GSS_r for some
1454 * reason cannot clear the callbacks on A, and so calls us again with
1455 * lastlih = A. Suppose there is another host B that has the same ActiveCall
1456 * time as A. We will now skip over host B, since
1457 * 'hostB->z.ActiveCall > hostA->z.ActiveCall' is not true. This could result in
1458 * us prematurely going to the GSS_r 2nd or 3rd pass, and making us a little
1459 * inefficient. This should be pretty rare, though, except perhaps in cases
1460 * with very small numbers of hosts.
1462 * Also filter out any hosts with HOSTDELETED set. h_Enumerate_r should in
1463 * theory not give these to us anyway, but be paranoid.
1466 lih0_r(struct host *host, void *rock)
1468 struct lih_params *params = (struct lih_params *)rock;
1470 /* OTHER_MUSTHOLD_LIH is because the h_Enum loop holds us once */
1472 && (!(host->z.hostFlags & HOSTDELETED))
1473 && (host->z.refCount < OTHER_MUSTHOLD_LIH)
1474 && (!params->lih || host->z.ActiveCall < params->lih->z.ActiveCall)
1475 && (!params->lastlih || host->z.ActiveCall > params->lastlih->z.ActiveCall)) {
1478 h_Release_r(params->lih); /* release prev host */
1487 /* same as lih0_r, except we do not prevent held hosts from being selected. */
1489 lih1_r(struct host *host, void *rock)
1491 struct lih_params *params = (struct lih_params *)rock;
1494 && (!(host->z.hostFlags & HOSTDELETED))
1495 && (!params->lih || host->z.ActiveCall < params->lih->z.ActiveCall)
1496 && (!params->lastlih || host->z.ActiveCall > params->lastlih->z.ActiveCall)) {
1499 h_Release_r(params->lih); /* release prev host */
1508 /* This could be upgraded to get more space each time */
1509 /* first pass: sequentially find the oldest host which isn't held by
1510 anyone for which we can clear callbacks;
1512 /* second pass: sequentially find the oldest host regardless of
1513 whether or not the host is held; skipping 'hostp' */
1514 /* third pass: attempt to clear callbacks from 'hostp' */
1515 /* always called with hostp unlocked */
1517 /* Note: hostlist is ordered most recently created host first and
1518 * its order has no relationship to the most recently used. */
1519 extern struct host *hostList;
1521 GetSomeSpace_r(struct host *hostp, int locked)
1524 struct lih_params params;
1527 if (cbstuff.GotSomeSpaces == 0) {
1528 /* only log this once; if GSS is getting called constantly, that's not
1529 * good but don't make things worse by spamming the log. */
1530 ViceLog(0, ("We have run out of callback space; forcing callback revocation. "
1531 "This suggests the fileserver is configured with insufficient "
1532 "callbacks; you probably want to increase the -cb fileserver "
1533 "parameter (current setting: %u). The fileserver will continue "
1534 "to operate, but this may indicate a severe performance problem\n",
1536 ViceLog(0, ("This message is logged at most once; for more information "
1537 "see the OpenAFS documentation and fileserver xstat collection 3\n"));
1540 cbstuff.GotSomeSpaces++;
1542 ("GSS: First looking for timed out call backs via CleanupCallBacks\n"));
1543 if (CleanupTimedOutCallBacks_r()) {
1549 params.lastlih = NULL;
1554 h_Enumerate_r(i == 0 ? lih0_r : lih1_r, hostList, ¶ms);
1557 if (params.lastlih) {
1558 h_Release_r(params.lastlih);
1559 params.lastlih = NULL;
1563 /* note that 'hp' was held by lih*_r; we will need to release it */
1565 if ((hp != hostp) && !ClearHostCallbacks_r(hp, 0 /* not locked or held */ )) {
1570 params.lastlih = hp;
1571 /* params.lastlih will be released on the next iteration, after
1576 * Next time try getting callbacks from any host even if
1577 * it's held, since the only other option is starvation for
1578 * the file server (i.e. until the callback timeout arrives).
1581 params.lastlih = NULL;
1584 ("GSS: Try harder for longest inactive host cnt= %d\n",
1589 /* Could not obtain space from other hosts, clear hostp's callback state */
1594 ClearHostCallbacks_r(hostp, 1 /*already locked */ );
1601 /* locked - set if caller has already locked the host */
1603 ClearHostCallbacks_r(struct host *hp, int locked)
1607 struct rx_connection *cb_conn = NULL;
1610 ("GSS: Delete longest inactive host %p (%s:%d)\n",
1611 hp, afs_inet_ntoa_r(hp->z.host, hoststr), ntohs(hp->z.port)));
1613 if ((hp->z.hostFlags & HOSTDELETED)) {
1614 /* hp could go away after reacquiring H_LOCK in h_NBLock_r, so we can't
1615 * really use it; its callbacks will get cleared anyway when
1616 * h_TossStuff_r gets its hands on it */
1622 /** Try a non-blocking lock. If the lock is already held return
1623 * after releasing hold on hp
1626 if (h_NBLock_r(hp)) {
1631 if (hp->z.Console & 2) {
1633 * If the special console field is set it means that a thread
1634 * is waiting in AddCallBack1 after it set pointers to the
1635 * file entry and/or callback entry. Because of the bogus
1636 * usage of h_hold it won't prevent from another thread, this
1637 * one, to remove all the callbacks so just to be safe we keep
1638 * a reference. NOTE, on the last phase we'll free the calling
1639 * host's callbacks but that's ok...
1643 DeleteAllCallBacks_r(hp, 1);
1644 if (hp->z.hostFlags & VENUSDOWN) {
1645 hp->z.hostFlags &= ~RESETDONE; /* remember that we must do a reset */
1646 } else if (!(hp->z.hostFlags & HOSTDELETED)) {
1647 /* host is up, try a call */
1648 hp->z.hostFlags &= ~ALTADDR; /* alternate addresses are invalid */
1649 hp->z.hostFlags |= HWHO_INPROGRESS; /* enable host thread quota enforcement */
1650 cb_conn = hp->z.callback_rxcon;
1651 rx_GetConnection(hp->z.callback_rxcon);
1652 if (hp->z.interface) {
1655 RXAFSCB_InitCallBackState3(cb_conn, &FS_HostUUID);
1658 code = RXAFSCB_InitCallBackState(cb_conn);
1660 rx_PutConnection(cb_conn);
1663 hp->z.hostFlags |= ALTADDR; /* alternate addresses are valid */
1664 hp->z.hostFlags &= ~HWHO_INPROGRESS;
1666 /* failed, mark host down and need reset */
1667 hp->z.hostFlags |= VENUSDOWN;
1668 hp->z.hostFlags &= ~RESETDONE;
1670 /* reset succeeded, we're done */
1671 hp->z.hostFlags |= RESETDONE;
1680 #endif /* INTERPRET_DUMP */
1684 PrintCallBackStats(void)
1687 "%d add CB, %d break CB, %d del CB, %d del FE, %d CB's timed out, %d space reclaim, %d del host\n",
1688 cbstuff.AddCallBacks, cbstuff.BreakCallBacks,
1689 cbstuff.DeleteCallBacks, cbstuff.DeleteFiles, cbstuff.CBsTimedOut,
1690 cbstuff.GotSomeSpaces, cbstuff.DeleteAllCallBacks);
1691 fprintf(stderr, "%d CBs, %d FEs, (%d of total of %d %d-byte blocks)\n",
1692 cbstuff.nCBs, cbstuff.nFEs, cbstuff.nCBs + cbstuff.nFEs,
1693 cbstuff.nblks, (int) sizeof(struct CallBack));
1694 fprintf(stderr, "%d GSS1, %d GSS2, %d GSS3, %d GSS4, %d GSS5 (internal counters)\n",
1695 cbstuff.GSS1, cbstuff.GSS2, cbstuff.GSS3, cbstuff.GSS4, cbstuff.GSS5);
1700 #define MAGIC 0x12345678 /* To check byte ordering of dump when it is read in */
1701 #define MAGICV2 0x12345679 /* To check byte ordering & version of dump when it is read in */
1704 #ifndef INTERPRET_DUMP
1706 #ifdef AFS_DEMAND_ATTACH_FS
1709 * callback state serialization
1711 static int cb_stateSaveTimeouts(struct fs_dump_state * state);
1712 static int cb_stateSaveFEHash(struct fs_dump_state * state);
1713 static int cb_stateSaveFEs(struct fs_dump_state * state);
1714 static int cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe);
1715 static int cb_stateRestoreTimeouts(struct fs_dump_state * state);
1716 static int cb_stateRestoreFEHash(struct fs_dump_state * state);
1717 static int cb_stateRestoreFEs(struct fs_dump_state * state);
1718 static int cb_stateRestoreFE(struct fs_dump_state * state);
1719 static int cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
1720 struct iovec * iov, int niovecs);
1722 static int cb_stateVerifyFEHash(struct fs_dump_state * state);
1723 static int cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe);
1724 static int cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe);
1725 static int cb_stateVerifyTimeoutQueues(struct fs_dump_state * state);
1727 static int cb_stateFEToDiskEntry(struct FileEntry *, struct FEDiskEntry *);
1728 static int cb_stateDiskEntryToFE(struct fs_dump_state * state,
1729 struct FEDiskEntry *, struct FileEntry *);
1731 static int cb_stateCBToDiskEntry(struct CallBack *, struct CBDiskEntry *);
1732 static int cb_stateDiskEntryToCB(struct fs_dump_state * state,
1733 struct CBDiskEntry *, struct CallBack *);
1735 static int cb_stateFillHeader(struct callback_state_header * hdr);
1736 static int cb_stateCheckHeader(struct callback_state_header * hdr);
1738 static int cb_stateAllocMap(struct fs_dump_state * state);
1741 cb_stateSave(struct fs_dump_state * state)
1745 AssignInt64(state->eof_offset, &state->hdr->cb_offset);
1747 /* invalidate callback state header */
1748 memset(state->cb_hdr, 0, sizeof(struct callback_state_header));
1749 if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1750 sizeof(struct callback_state_header))) {
1755 fs_stateIncEOF(state, sizeof(struct callback_state_header));
1757 /* dump timeout state */
1758 if (cb_stateSaveTimeouts(state)) {
1763 /* dump fe hashtable state */
1764 if (cb_stateSaveFEHash(state)) {
1769 /* dump callback state */
1770 if (cb_stateSaveFEs(state)) {
1775 /* write the callback state header to disk */
1776 cb_stateFillHeader(state->cb_hdr);
1777 if (fs_stateWriteHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1778 sizeof(struct callback_state_header))) {
1788 cb_stateRestore(struct fs_dump_state * state)
1792 if (fs_stateReadHeader(state, &state->hdr->cb_offset, state->cb_hdr,
1793 sizeof(struct callback_state_header))) {
1798 if (cb_stateCheckHeader(state->cb_hdr)) {
1803 if (cb_stateAllocMap(state)) {
1808 if (cb_stateRestoreTimeouts(state)) {
1813 if (cb_stateRestoreFEHash(state)) {
1818 /* restore FEs and CBs from disk */
1819 if (cb_stateRestoreFEs(state)) {
1824 /* restore the timeout queue heads */
1825 tfirst = state->cb_hdr->tfirst;
1832 cb_stateRestoreIndices(struct fs_dump_state * state)
1835 struct FileEntry * fe;
1836 struct CallBack * cb;
1838 /* restore indices in the FileEntry structures */
1839 for (i = 1; i < state->fe_map.len; i++) {
1840 if (state->fe_map.entries[i].new_idx) {
1841 fe = itofe(state->fe_map.entries[i].new_idx);
1843 /* restore the fe->fnext entry */
1844 if (fe_OldToNew(state, fe->fnext, &fe->fnext)) {
1849 /* restore the fe->firstcb entry */
1850 if (cb_OldToNew(state, fe->firstcb, &fe->firstcb)) {
1857 /* restore indices in the CallBack structures */
1858 for (i = 1; i < state->cb_map.len; i++) {
1859 if (state->cb_map.entries[i].new_idx) {
1860 cb = itocb(state->cb_map.entries[i].new_idx);
1862 /* restore the cb->cnext entry */
1863 if (cb_OldToNew(state, cb->cnext, &cb->cnext)) {
1868 /* restore the cb->fhead entry */
1869 if (fe_OldToNew(state, cb->fhead, &cb->fhead)) {
1874 /* restore the cb->hhead entry */
1875 if (h_OldToNew(state, cb->hhead, &cb->hhead)) {
1880 /* restore the cb->tprev entry */
1881 if (cb_OldToNew(state, cb->tprev, &cb->tprev)) {
1886 /* restore the cb->tnext entry */
1887 if (cb_OldToNew(state, cb->tnext, &cb->tnext)) {
1892 /* restore the cb->hprev entry */
1893 if (cb_OldToNew(state, cb->hprev, &cb->hprev)) {
1898 /* restore the cb->hnext entry */
1899 if (cb_OldToNew(state, cb->hnext, &cb->hnext)) {
1906 /* restore the timeout queue head indices */
1907 for (i = 0; i < state->cb_timeout_hdr->records; i++) {
1908 if (cb_OldToNew(state, timeout[i], &timeout[i])) {
1914 /* restore the FE hash table queue heads */
1915 for (i = 0; i < state->cb_fehash_hdr->records; i++) {
1916 if (fe_OldToNew(state, HashTable[i], &HashTable[i])) {
1927 cb_stateVerify(struct fs_dump_state * state)
1931 if (cb_stateVerifyFEHash(state)) {
1935 if (cb_stateVerifyTimeoutQueues(state)) {
1943 cb_stateVerifyFEHash(struct fs_dump_state * state)
1946 struct FileEntry * fe;
1947 afs_uint32 fei, chain_len;
1949 for (i = 0; i < FEHASH_SIZE; i++) {
1951 for (fei = HashTable[i], fe = itofe(fei);
1953 fei = fe->fnext, fe = itofe(fei)) {
1954 if (fei > cbstuff.nblks) {
1955 ViceLog(0, ("cb_stateVerifyFEHash: error: index out of range (fei=%d)\n", fei));
1959 if (cb_stateVerifyFE(state, fe)) {
1962 if (chain_len > FS_STATE_FE_MAX_HASH_CHAIN_LEN) {
1963 ViceLog(0, ("cb_stateVerifyFEHash: error: hash chain %d length exceeds %d; assuming there's a loop\n",
1964 i, FS_STATE_FE_MAX_HASH_CHAIN_LEN));
1976 cb_stateVerifyFE(struct fs_dump_state * state, struct FileEntry * fe)
1980 if ((fe->firstcb && !fe->ncbs) ||
1981 (!fe->firstcb && fe->ncbs)) {
1982 ViceLog(0, ("cb_stateVerifyFE: error: fe->firstcb does not agree with fe->ncbs (fei=%lu, fe->firstcb=%lu, fe->ncbs=%lu)\n",
1983 afs_printable_uint32_lu(fetoi(fe)),
1984 afs_printable_uint32_lu(fe->firstcb),
1985 afs_printable_uint32_lu(fe->ncbs)));
1988 if (cb_stateVerifyFCBList(state, fe)) {
1989 ViceLog(0, ("cb_stateVerifyFE: error: FCBList failed verification (fei=%lu)\n",
1990 afs_printable_uint32_lu(fetoi(fe))));
1998 cb_stateVerifyFCBList(struct fs_dump_state * state, struct FileEntry * fe)
2001 afs_uint32 cbi, fei, chain_len = 0;
2002 struct CallBack * cb;
2006 for (cbi = fe->firstcb, cb = itocb(cbi);
2008 cbi = cb->cnext, cb = itocb(cbi)) {
2009 if (cbi > cbstuff.nblks) {
2010 ViceLog(0, ("cb_stateVerifyFCBList: error: list index out of range (cbi=%d, ncbs=%d)\n",
2011 cbi, cbstuff.nblks));
2015 if (cb->fhead != fei) {
2016 ViceLog(0, ("cb_stateVerifyFCBList: error: cb->fhead != fei (fei=%d, cb->fhead=%d)\n",
2020 if (chain_len > FS_STATE_FCB_MAX_LIST_LEN) {
2021 ViceLog(0, ("cb_stateVerifyFCBList: error: list length exceeds %d (fei=%d); assuming there's a loop\n",
2022 FS_STATE_FCB_MAX_LIST_LEN, fei));
2029 if (fe->ncbs != chain_len) {
2030 ViceLog(0, ("cb_stateVerifyFCBList: error: list length mismatch (len=%d, fe->ncbs=%d)\n",
2031 chain_len, fe->ncbs));
2040 cb_stateVerifyHCBList(struct fs_dump_state * state, struct host * host)
2043 afs_uint32 hi, chain_len, cbi;
2044 struct CallBack *cb, *ncb;
2049 for (cbi = host->z.cblist, cb = itocb(cbi);
2051 cbi = cb->hnext, cb = ncb) {
2052 if (chain_len && (host->z.cblist == cbi)) {
2053 /* we've wrapped around the circular list, and everything looks ok */
2056 if (cb->hhead != hi) {
2057 ViceLog(0, ("cb_stateVerifyHCBList: error: incorrect cb->hhead (cbi=%d, h->index=%d, cb->hhead=%d)\n",
2058 cbi, hi, cb->hhead));
2061 if (!cb->hprev || !cb->hnext) {
2062 ViceLog(0, ("cb_stateVerifyHCBList: error: null index in circular list (cbi=%d, h->index=%d)\n",
2067 if ((cb->hprev > cbstuff.nblks) ||
2068 (cb->hnext > cbstuff.nblks)) {
2069 ViceLog(0, ("cb_stateVerifyHCBList: error: list index out of range (cbi=%d, h->index=%d, cb->hprev=%d, cb->hnext=%d, nCBs=%d)\n",
2070 cbi, hi, cb->hprev, cb->hnext, cbstuff.nblks));
2074 ncb = itocb(cb->hnext);
2075 if (cbi != ncb->hprev) {
2076 ViceLog(0, ("cb_stateVerifyHCBList: error: corrupt linked list (cbi=%d, h->index=%d)\n",
2081 if (chain_len > FS_STATE_HCB_MAX_LIST_LEN) {
2082 ViceLog(0, ("cb_stateVerifyHCBList: error: list length exceeds %d (h->index=%d); assuming there's a loop\n",
2083 FS_STATE_HCB_MAX_LIST_LEN, hi));
2095 cb_stateVerifyTimeoutQueues(struct fs_dump_state * state)
2098 afs_uint32 cbi, chain_len;
2099 struct CallBack *cb, *ncb;
2101 for (i = 0; i < CB_NUM_TIMEOUT_QUEUES; i++) {
2103 for (cbi = timeout[i], cb = itocb(cbi);
2105 cbi = cb->tnext, cb = ncb) {
2106 if (chain_len && (cbi == timeout[i])) {
2107 /* we've wrapped around the circular list, and everything looks ok */
2110 if (cbi > cbstuff.nblks) {
2111 ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: list index out of range (cbi=%d, tindex=%d)\n",
2116 if (itot(cb->thead) != &timeout[i]) {
2117 ViceLog(0, ("cb_stateVerifyTimeoutQueues: error: cb->thead points to wrong timeout queue (tindex=%d, cbi=%d, cb->thead=%d)\n",
2118 i, cbi, cb->thead));
2121 if (!cb->tprev || !cb->tnext) {
2122 ViceLog(0, ("cb_stateVerifyTimeoutQueues: null index in circular list (cbi=%d, tindex=%d)\n",
2127 if ((cb->tprev > cbstuff.nblks) ||
2128 (cb->tnext > cbstuff.nblks)) {
2129 ViceLog(0, ("cb_stateVerifyTimeoutQueues: list index out of range (cbi=%d, tindex=%d, cb->tprev=%d, cb->tnext=%d, nCBs=%d)\n",
2130 cbi, i, cb->tprev, cb->tnext, cbstuff.nblks));
2134 ncb = itocb(cb->tnext);
2135 if (cbi != ncb->tprev) {
2136 ViceLog(0, ("cb_stateVerifyTimeoutQueues: corrupt linked list (cbi=%d, tindex=%d)\n",
2141 if (chain_len > FS_STATE_TCB_MAX_LIST_LEN) {
2142 ViceLog(0, ("cb_stateVerifyTimeoutQueues: list length exceeds %d (tindex=%d); assuming there's a loop\n",
2143 FS_STATE_TCB_MAX_LIST_LEN, i));
2155 cb_stateSaveTimeouts(struct fs_dump_state * state)
2158 struct iovec iov[2];
2160 AssignInt64(state->eof_offset, &state->cb_hdr->timeout_offset);
2162 memset(state->cb_timeout_hdr, 0, sizeof(struct callback_state_fehash_header));
2163 state->cb_timeout_hdr->magic = CALLBACK_STATE_TIMEOUT_MAGIC;
2164 state->cb_timeout_hdr->records = CB_NUM_TIMEOUT_QUEUES;
2165 state->cb_timeout_hdr->len = sizeof(struct callback_state_timeout_header) +
2166 (state->cb_timeout_hdr->records * sizeof(afs_uint32));
2168 iov[0].iov_base = (char *)state->cb_timeout_hdr;
2169 iov[0].iov_len = sizeof(struct callback_state_timeout_header);
2170 iov[1].iov_base = (char *)timeout;
2171 iov[1].iov_len = sizeof(timeout);
2173 if (fs_stateSeek(state, &state->cb_hdr->timeout_offset)) {
2178 if (fs_stateWriteV(state, iov, 2)) {
2183 fs_stateIncEOF(state, state->cb_timeout_hdr->len);
2190 cb_stateRestoreTimeouts(struct fs_dump_state * state)
2194 if (fs_stateReadHeader(state, &state->cb_hdr->timeout_offset,
2195 state->cb_timeout_hdr,
2196 sizeof(struct callback_state_timeout_header))) {
2201 if (state->cb_timeout_hdr->magic != CALLBACK_STATE_TIMEOUT_MAGIC) {
2205 if (state->cb_timeout_hdr->records != CB_NUM_TIMEOUT_QUEUES) {
2210 len = state->cb_timeout_hdr->records * sizeof(afs_uint32);
2212 if (state->cb_timeout_hdr->len !=
2213 (sizeof(struct callback_state_timeout_header) + len)) {
2218 if (fs_stateRead(state, timeout, len)) {
2228 cb_stateSaveFEHash(struct fs_dump_state * state)
2231 struct iovec iov[2];
2233 AssignInt64(state->eof_offset, &state->cb_hdr->fehash_offset);
2235 memset(state->cb_fehash_hdr, 0, sizeof(struct callback_state_fehash_header));
2236 state->cb_fehash_hdr->magic = CALLBACK_STATE_FEHASH_MAGIC;
2237 state->cb_fehash_hdr->records = FEHASH_SIZE;
2238 state->cb_fehash_hdr->len = sizeof(struct callback_state_fehash_header) +
2239 (state->cb_fehash_hdr->records * sizeof(afs_uint32));
2241 iov[0].iov_base = (char *)state->cb_fehash_hdr;
2242 iov[0].iov_len = sizeof(struct callback_state_fehash_header);
2243 iov[1].iov_base = (char *)HashTable;
2244 iov[1].iov_len = sizeof(HashTable);
2246 if (fs_stateSeek(state, &state->cb_hdr->fehash_offset)) {
2251 if (fs_stateWriteV(state, iov, 2)) {
2256 fs_stateIncEOF(state, state->cb_fehash_hdr->len);
2263 cb_stateRestoreFEHash(struct fs_dump_state * state)
2267 if (fs_stateReadHeader(state, &state->cb_hdr->fehash_offset,
2268 state->cb_fehash_hdr,
2269 sizeof(struct callback_state_fehash_header))) {
2274 if (state->cb_fehash_hdr->magic != CALLBACK_STATE_FEHASH_MAGIC) {
2278 if (state->cb_fehash_hdr->records != FEHASH_SIZE) {
2283 len = state->cb_fehash_hdr->records * sizeof(afs_uint32);
2285 if (state->cb_fehash_hdr->len !=
2286 (sizeof(struct callback_state_fehash_header) + len)) {
2291 if (fs_stateRead(state, HashTable, len)) {
2301 cb_stateSaveFEs(struct fs_dump_state * state)
2305 struct FileEntry *fe;
2307 AssignInt64(state->eof_offset, &state->cb_hdr->fe_offset);
2309 for (hash = 0; hash < FEHASH_SIZE ; hash++) {
2310 for (fei = HashTable[hash]; fei; fei = fe->fnext) {
2312 if (cb_stateSaveFE(state, fe)) {
2324 cb_stateRestoreFEs(struct fs_dump_state * state)
2326 int count, nFEs, ret = 0;
2328 nFEs = state->cb_hdr->nFEs;
2330 for (count = 0; count < nFEs; count++) {
2331 if (cb_stateRestoreFE(state)) {
2342 cb_stateSaveFE(struct fs_dump_state * state, struct FileEntry * fe)
2344 int ret = 0, iovcnt, cbi, written = 0;
2346 struct callback_state_entry_header hdr;
2347 struct FEDiskEntry fedsk;
2348 struct CBDiskEntry cbdsk[16];
2349 struct iovec iov[16];
2350 struct CallBack *cb;
2353 if (fei > state->cb_hdr->fe_max) {
2354 state->cb_hdr->fe_max = fei;
2357 memset(&hdr, 0, sizeof(struct callback_state_entry_header));
2359 if (cb_stateFEToDiskEntry(fe, &fedsk)) {
2364 iov[0].iov_base = (char *)&hdr;
2365 iov[0].iov_len = sizeof(hdr);
2366 iov[1].iov_base = (char *)&fedsk;
2367 iov[1].iov_len = sizeof(struct FEDiskEntry);
2370 for (cbi = fe->firstcb, cb = itocb(cbi);
2372 cbi = cb->cnext, cb = itocb(cbi), hdr.nCBs++) {
2373 if (cbi > state->cb_hdr->cb_max) {
2374 state->cb_hdr->cb_max = cbi;
2376 if (cb_stateCBToDiskEntry(cb, &cbdsk[iovcnt])) {
2380 cbdsk[iovcnt].index = cbi;
2381 iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
2382 iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
2384 if ((iovcnt == 16) || (!cb->cnext)) {
2385 if (fs_stateWriteV(state, iov, iovcnt)) {
2394 hdr.magic = CALLBACK_STATE_ENTRY_MAGIC;
2395 hdr.len = sizeof(hdr) + sizeof(struct FEDiskEntry) +
2396 (hdr.nCBs * sizeof(struct CBDiskEntry));
2399 if (fs_stateWriteV(state, iov, iovcnt)) {
2404 if (fs_stateWriteHeader(state, &state->eof_offset, &hdr, sizeof(hdr))) {
2410 fs_stateIncEOF(state, hdr.len);
2413 if (fs_stateSeek(state, &state->eof_offset)) {
2419 state->cb_hdr->nFEs++;
2420 state->cb_hdr->nCBs += hdr.nCBs;
2427 cb_stateRestoreFE(struct fs_dump_state * state)
2429 int ret = 0, iovcnt, nCBs;
2430 struct callback_state_entry_header hdr;
2431 struct FEDiskEntry fedsk;
2432 struct CBDiskEntry cbdsk[16];
2433 struct iovec iov[16];
2434 struct FileEntry * fe;
2436 iov[0].iov_base = (char *)&hdr;
2437 iov[0].iov_len = sizeof(hdr);
2438 iov[1].iov_base = (char *)&fedsk;
2439 iov[1].iov_len = sizeof(fedsk);
2442 if (fs_stateReadV(state, iov, iovcnt)) {
2447 if (hdr.magic != CALLBACK_STATE_ENTRY_MAGIC) {
2454 ViceLog(0, ("cb_stateRestoreFE: ran out of free FileEntry structures\n"));
2459 if (cb_stateDiskEntryToFE(state, &fedsk, fe)) {
2465 for (iovcnt = 0, nCBs = 0;
2468 iov[iovcnt].iov_base = (char *)&cbdsk[iovcnt];
2469 iov[iovcnt].iov_len = sizeof(struct CBDiskEntry);
2471 if ((iovcnt == 16) || (nCBs == hdr.nCBs - 1)) {
2472 if (fs_stateReadV(state, iov, iovcnt)) {
2476 if (cb_stateRestoreCBs(state, fe, iov, iovcnt)) {
2490 cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
2491 struct iovec * iov, int niovecs)
2494 struct CallBack * cb;
2495 struct CBDiskEntry * cbdsk;
2497 for (idx = 0; idx < niovecs; idx++) {
2498 cbdsk = (struct CBDiskEntry *) iov[idx].iov_base;
2500 if (cbdsk->cb.hhead < state->h_map.len &&
2501 state->h_map.entries[cbdsk->cb.hhead].valid == FS_STATE_IDX_SKIPPED) {
2505 if ((cb = GetCB()) == NULL) {
2506 ViceLog(0, ("cb_stateRestoreCBs: ran out of free CallBack structures\n"));
2510 if (cb_stateDiskEntryToCB(state, cbdsk, cb)) {
2511 ViceLog(0, ("cb_stateRestoreCBs: corrupt CallBack disk entry\n"));
2523 cb_stateFillHeader(struct callback_state_header * hdr)
2525 hdr->stamp.magic = CALLBACK_STATE_MAGIC;
2526 hdr->stamp.version = CALLBACK_STATE_VERSION;
2527 hdr->tfirst = tfirst;
2532 cb_stateCheckHeader(struct callback_state_header * hdr)
2536 if (hdr->stamp.magic != CALLBACK_STATE_MAGIC) {
2538 } else if (hdr->stamp.version != CALLBACK_STATE_VERSION) {
2540 } else if ((hdr->nFEs > cbstuff.nblks) || (hdr->nCBs > cbstuff.nblks)) {
2541 ViceLog(0, ("cb_stateCheckHeader: saved callback state larger than callback memory allocation\n"));
2547 /* disk entry conversion routines */
2549 cb_stateFEToDiskEntry(struct FileEntry * in, struct FEDiskEntry * out)
2551 memcpy(&out->fe, in, sizeof(struct FileEntry));
2552 out->index = fetoi(in);
2557 cb_stateDiskEntryToFE(struct fs_dump_state * state,
2558 struct FEDiskEntry * in, struct FileEntry * out)
2562 memcpy(out, &in->fe, sizeof(struct FileEntry));
2564 /* setup FE map entry */
2565 if (!in->index || (in->index >= state->fe_map.len)) {
2566 ViceLog(0, ("cb_stateDiskEntryToFE: index (%d) out of range\n",
2571 state->fe_map.entries[in->index].valid = FS_STATE_IDX_VALID;
2572 state->fe_map.entries[in->index].old_idx = in->index;
2573 state->fe_map.entries[in->index].new_idx = fetoi(out);
2580 cb_stateCBToDiskEntry(struct CallBack * in, struct CBDiskEntry * out)
2582 memcpy(&out->cb, in, sizeof(struct CallBack));
2583 out->index = cbtoi(in);
2588 cb_stateDiskEntryToCB(struct fs_dump_state * state,
2589 struct CBDiskEntry * in, struct CallBack * out)
2593 memcpy(out, &in->cb, sizeof(struct CallBack));
2595 /* setup CB map entry */
2596 if (!in->index || (in->index >= state->cb_map.len)) {
2597 ViceLog(0, ("cb_stateDiskEntryToCB: index (%d) out of range\n",
2602 state->cb_map.entries[in->index].valid = FS_STATE_IDX_VALID;
2603 state->cb_map.entries[in->index].old_idx = in->index;
2604 state->cb_map.entries[in->index].new_idx = cbtoi(out);
2610 /* index map routines */
2612 cb_stateAllocMap(struct fs_dump_state * state)
2614 state->fe_map.len = state->cb_hdr->fe_max + 1;
2615 state->cb_map.len = state->cb_hdr->cb_max + 1;
2616 state->fe_map.entries = (struct idx_map_entry_t *)
2617 calloc(state->fe_map.len, sizeof(struct idx_map_entry_t));
2618 state->cb_map.entries = (struct idx_map_entry_t *)
2619 calloc(state->cb_map.len, sizeof(struct idx_map_entry_t));
2620 return ((state->fe_map.entries != NULL) && (state->cb_map.entries != NULL)) ? 0 : 1;
2624 fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2628 /* FEs use a one-based indexing system, so old==0 implies no mapping */
2634 if (old >= state->fe_map.len) {
2635 ViceLog(0, ("fe_OldToNew: index %d is out of range\n", old));
2637 } else if (state->fe_map.entries[old].valid != FS_STATE_IDX_VALID ||
2638 state->fe_map.entries[old].old_idx != old) { /* sanity check */
2639 ViceLog(0, ("fe_OldToNew: index %d points to an invalid FileEntry record\n", old));
2642 *new = state->fe_map.entries[old].new_idx;
2650 cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2654 /* CBs use a one-based indexing system, so old==0 implies no mapping */
2660 if (old >= state->cb_map.len) {
2661 ViceLog(0, ("cb_OldToNew: index %d is out of range\n", old));
2663 } else if (state->cb_map.entries[old].valid != FS_STATE_IDX_VALID ||
2664 state->cb_map.entries[old].old_idx != old) { /* sanity check */
2665 ViceLog(0, ("cb_OldToNew: index %d points to an invalid CallBack record\n", old));
2668 *new = state->cb_map.entries[old].new_idx;
2674 #endif /* AFS_DEMAND_ATTACH_FS */
2676 #define DumpBytes(fd,buf,req) if (write(fd, buf, req) < 0) {} /* don't care */
2679 DumpCallBackState_r(void)
2682 afs_uint32 magic = MAGICV2, now = (afs_int32) time(NULL), freelisthead;
2684 oflag = O_WRONLY | O_CREAT | O_TRUNC;
2688 fd = open(AFSDIR_SERVER_CBKDUMP_FILEPATH, oflag, 0666);
2691 ("Couldn't create callback dump file %s\n",
2692 AFSDIR_SERVER_CBKDUMP_FILEPATH));
2696 * Collect but ignoring the return value of write(2) here,
2697 * to avoid compiler warnings on some platforms.
2699 DumpBytes(fd, &magic, sizeof(magic));
2700 DumpBytes(fd, &now, sizeof(now));
2701 DumpBytes(fd, &cbstuff, sizeof(cbstuff));
2702 DumpBytes(fd, TimeOuts, sizeof(TimeOuts));
2703 DumpBytes(fd, timeout, sizeof(timeout));
2704 DumpBytes(fd, &tfirst, sizeof(tfirst));
2705 freelisthead = cbtoi((struct CallBack *)CBfree);
2706 DumpBytes(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
2707 freelisthead = fetoi((struct FileEntry *)FEfree);
2708 DumpBytes(fd, &freelisthead, sizeof(freelisthead)); /* This is a pointer */
2709 DumpBytes(fd, HashTable, sizeof(HashTable));
2710 DumpBytes(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks); /* CB stuff */
2711 DumpBytes(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks); /* FE stuff */
2718 DumpCallBackState(void) {
2722 rc = DumpCallBackState_r();
2728 #endif /* !INTERPRET_DUMP */
2730 #ifdef INTERPRET_DUMP
2733 ReadBytes(int fd, void *buf, size_t req)
2737 count = read(fd, buf, req);
2741 } else if (count != req) {
2742 fprintf(stderr, "read: premature EOF (expected %lu, got %lu)\n",
2743 (unsigned long)req, (unsigned long)count);
2748 /* This is only compiled in for the callback analyzer program */
2749 /* Returns the time of the dump */
2751 ReadDump(char *file, int timebits)
2754 afs_uint32 magic, freelisthead;
2762 fd = open(file, oflag);
2764 fprintf(stderr, "Couldn't read dump file %s\n", file);
2767 ReadBytes(fd, &magic, sizeof(magic));
2768 if (magic == MAGICV2) {
2771 if (magic != MAGIC) {
2773 "Magic number of %s is invalid. You might be trying to\n",
2776 "run this program on a machine type with a different byte ordering.\n");
2780 if (timebits == 64) {
2781 ReadBytes(fd, &now64, sizeof(afs_int64));
2782 now = (afs_int32) now64;
2784 ReadBytes(fd, &now, sizeof(afs_int32));
2786 ReadBytes(fd, &cbstuff, sizeof(cbstuff));
2787 ReadBytes(fd, TimeOuts, sizeof(TimeOuts));
2788 ReadBytes(fd, timeout, sizeof(timeout));
2789 ReadBytes(fd, &tfirst, sizeof(tfirst));
2790 ReadBytes(fd, &freelisthead, sizeof(freelisthead));
2791 CB = ((struct CallBack
2792 *)(calloc(cbstuff.nblks, sizeof(struct CallBack)))) - 1;
2793 FE = ((struct FileEntry
2794 *)(calloc(cbstuff.nblks, sizeof(struct FileEntry)))) - 1;
2795 CBfree = (struct CallBack *)itocb(freelisthead);
2796 ReadBytes(fd, &freelisthead, sizeof(freelisthead));
2797 FEfree = (struct FileEntry *)itofe(freelisthead);
2798 ReadBytes(fd, HashTable, sizeof(HashTable));
2799 ReadBytes(fd, &CB[1], sizeof(CB[1]) * cbstuff.nblks); /* CB stuff */
2800 ReadBytes(fd, &FE[1], sizeof(FE[1]) * cbstuff.nblks); /* FE stuff */
2802 perror("Error reading dumpfile");
2809 #include "AFS_component_version_number.h"
2811 #include "AFS_component_version_number.c"
2814 static afs_uint32 *cbTrack;
2817 main(int argc, char **argv)
2819 int err = 0, cbi = 0, stats = 0, noptions = 0, all = 0, vol = 0, raw = 0;
2821 struct FileEntry *fe;
2822 struct CallBack *cb;
2826 memset(&fid, 0, sizeof(fid));
2829 while (argc && **argv == '-') {
2832 if (!strcmp(*argv, "-host")) {
2838 cbi = atoi(*++argv);
2839 } else if (!strcmp(*argv, "-fid")) {
2845 fid.Volume = atoi(*++argv);
2846 fid.Vnode = atoi(*++argv);
2847 fid.Unique = atoi(*++argv);
2848 } else if (!strcmp(*argv, "-time")) {
2849 fprintf(stderr, "-time not supported\n");
2851 } else if (!strcmp(*argv, "-stats")) {
2853 } else if (!strcmp(*argv, "-all")) {
2855 } else if (!strcmp(*argv, "-raw")) {
2857 } else if (!strcmp(*argv, "-timebits")) {
2863 timebits = atoi(*++argv);
2864 if ((timebits != 32)
2868 } else if (!strcmp(*argv, "-volume")) {
2874 vol = atoi(*++argv);
2879 if (err || argc != 1) {
2881 "Usage: cbd [-host cbid] [-fid volume vnode] [-stats] [-all] [-timebits 32"
2883 "] callbackdumpfile\n");
2885 "[cbid is shown for each host in the hosts.dump file]\n");
2888 now = ReadDump(*argv, timebits);
2889 if (stats || noptions == 0) {
2890 time_t uxtfirst = UXtime(tfirst), tnow = now;
2891 printf("The time of the dump was %u %s", (unsigned int) now, ctime(&tnow));
2892 printf("The last time cleanup ran was %u %s", (unsigned int) uxtfirst,
2894 PrintCallBackStats();
2897 cbTrack = calloc(cbstuff.nblks, sizeof(cbTrack[0]));
2902 struct CallBack *cb;
2903 struct FileEntry *fe;
2905 for (hash = 0; hash < FEHASH_SIZE; hash++) {
2906 for (feip = &HashTable[hash]; (fe = itofe(*feip));) {
2907 if (!vol || (fe->volid == vol)) {
2908 afs_uint32 fe_i = fetoi(fe);
2910 for (cb = itocb(fe->firstcb); cb; cb = itocb(cb->cnext)) {
2911 afs_uint32 cb_i = cbtoi(cb);
2913 if (cb_i > cbstuff.nblks) {
2914 printf("CB index out of range (%u > %d), stopped for this FE\n",
2915 cb_i, cbstuff.nblks);
2919 if (cbTrack[cb_i]) {
2920 printf("CB entry already claimed for FE[%u] (this is FE[%u]), stopped\n",
2921 cbTrack[cb_i], fe_i);
2924 cbTrack[cb_i] = fe_i;
2936 afs_uint32 cfirst = cbi;
2941 } while (cbi != cfirst);
2946 printf("No callback entries for %u.%u\n", fid.Volume, fid.Vnode);
2949 cb = itocb(fe->firstcb);
2952 cb = itocb(cb->cnext);
2957 for (i = 1; i < cbstuff.nblks; i++) {
2958 p = (afs_int32 *) & FE[i];
2959 printf("%d:%12x%12x%12x%12x\n", i, p[0], p[1], p[2], p[3]);
2968 PrintCB(struct CallBack *cb, afs_uint32 now)
2970 struct FileEntry *fe = itofe(cb->fhead);
2971 time_t expires = TIndexToTime(cb->thead);
2976 printf("vol=%" AFS_VOLID_FMT " vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %lu secs at %s",
2977 afs_printable_VolumeId_lu(fe->volid), fe->vnode, fe->ncbs,
2978 cb->hhead, cb->status, fe->status, (unsigned long)(expires - now),
2984 #if !defined(INTERPRET_DUMP)
2986 ** try breaking calbacks on afidp from host. Use multi_rx.
2987 ** return 0 on success, non-zero on failure
2990 MultiBreakCallBackAlternateAddress(struct host *host, struct AFSCBFids *afidp)
2994 retVal = MultiBreakCallBackAlternateAddress_r(host, afidp);
3000 MultiBreakCallBackAlternateAddress_r(struct host *host,
3001 struct AFSCBFids *afidp)
3004 struct rx_connection **conns;
3005 struct rx_connection *connSuccess = 0;
3006 struct AddrPort *interfaces;
3007 static struct rx_securityClass *sc = 0;
3008 static struct AFSCBs tc = { 0, 0 };
3011 /* nothing more can be done */
3012 if (!host->z.interface)
3013 return 1; /* failure */
3015 /* the only address is the primary interface */
3016 if (host->z.interface->numberOfInterfaces <= 1)
3017 return 1; /* failure */
3019 /* initialise a security object only once */
3021 sc = rxnull_NewClientSecurityObject();
3023 i = host->z.interface->numberOfInterfaces;
3024 interfaces = calloc(i, sizeof(struct AddrPort));
3025 conns = calloc(i, sizeof(struct rx_connection *));
3026 if (!interfaces || !conns) {
3027 ViceLogThenPanic(0, ("Failed malloc in "
3028 "MultiBreakCallBackAlternateAddress_r\n"));
3031 /* initialize alternate rx connections */
3032 for (i = 0, j = 0; i < host->z.interface->numberOfInterfaces; i++) {
3033 /* this is the current primary address */
3034 if (host->z.host == host->z.interface->interface[i].addr &&
3035 host->z.port == host->z.interface->interface[i].port)
3038 interfaces[j] = host->z.interface->interface[i];
3040 rx_NewConnection(interfaces[j].addr,
3041 interfaces[j].port, 1, sc, 0);
3042 rx_SetConnDeadTime(conns[j], 2);
3043 rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
3047 opr_Assert(j); /* at least one alternate address */
3049 ("Starting multibreakcall back on all addr for host %p (%s:%d)\n",
3050 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
3052 multi_Rx(conns, j) {
3053 multi_RXAFSCB_CallBack(afidp, &tc);
3057 if (host->z.callback_rxcon)
3058 rx_DestroyConnection(host->z.callback_rxcon);
3059 host->z.callback_rxcon = conns[multi_i];
3060 /* add then remove */
3061 addInterfaceAddr_r(host, interfaces[multi_i].addr,
3062 interfaces[multi_i].port);
3063 removeInterfaceAddr_r(host, host->z.host, host->z.port);
3064 host->z.host = interfaces[multi_i].addr;
3065 host->z.port = interfaces[multi_i].port;
3066 connSuccess = conns[multi_i];
3067 rx_SetConnDeadTime(host->z.callback_rxcon, 50);
3068 rx_SetConnHardDeadTime(host->z.callback_rxcon, AFS_HARDDEADTIME);
3070 ("multibreakcall success with addr %s:%d\n",
3071 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3072 ntohs(interfaces[multi_i].port)));
3079 /* Destroy all connections except the one on which we succeeded */
3080 for (i = 0; i < j; i++)
3081 if (conns[i] != connSuccess)
3082 rx_DestroyConnection(conns[i]);
3088 return 0; /* success */
3090 return 1; /* failure */
3095 ** try multi_RX probes to host.
3096 ** return 0 on success, non-0 on failure
3099 MultiProbeAlternateAddress_r(struct host *host)
3102 struct rx_connection **conns;
3103 struct rx_connection *connSuccess = 0;
3104 struct AddrPort *interfaces;
3105 static struct rx_securityClass *sc = 0;
3108 /* nothing more can be done */
3109 if (!host->z.interface)
3110 return 1; /* failure */
3112 /* the only address is the primary interface */
3113 if (host->z.interface->numberOfInterfaces <= 1)
3114 return 1; /* failure */
3116 /* initialise a security object only once */
3118 sc = rxnull_NewClientSecurityObject();
3120 i = host->z.interface->numberOfInterfaces;
3121 interfaces = calloc(i, sizeof(struct AddrPort));
3122 conns = calloc(i, sizeof(struct rx_connection *));
3123 if (!interfaces || !conns) {
3124 ViceLogThenPanic(0, ("Failed malloc in "
3125 "MultiProbeAlternateAddress_r\n"));
3128 /* initialize alternate rx connections */
3129 for (i = 0, j = 0; i < host->z.interface->numberOfInterfaces; i++) {
3130 /* this is the current primary address */
3131 if (host->z.host == host->z.interface->interface[i].addr &&
3132 host->z.port == host->z.interface->interface[i].port)
3135 interfaces[j] = host->z.interface->interface[i];
3137 rx_NewConnection(interfaces[j].addr,
3138 interfaces[j].port, 1, sc, 0);
3139 rx_SetConnDeadTime(conns[j], 2);
3140 rx_SetConnHardDeadTime(conns[j], AFS_HARDDEADTIME);
3144 opr_Assert(j); /* at least one alternate address */
3146 ("Starting multiprobe on all addr for host %p (%s:%d)\n",
3147 host, afs_inet_ntoa_r(host->z.host, hoststr),
3148 ntohs(host->z.port)));
3150 multi_Rx(conns, j) {
3151 multi_RXAFSCB_ProbeUuid(&host->z.interface->uuid);
3155 if (host->z.callback_rxcon)
3156 rx_DestroyConnection(host->z.callback_rxcon);
3157 host->z.callback_rxcon = conns[multi_i];
3158 /* add then remove */
3159 addInterfaceAddr_r(host, interfaces[multi_i].addr,
3160 interfaces[multi_i].port);
3161 removeInterfaceAddr_r(host, host->z.host, host->z.port);
3162 host->z.host = interfaces[multi_i].addr;
3163 host->z.port = interfaces[multi_i].port;
3164 connSuccess = conns[multi_i];
3165 rx_SetConnDeadTime(host->z.callback_rxcon, 50);
3166 rx_SetConnHardDeadTime(host->z.callback_rxcon, AFS_HARDDEADTIME);
3168 ("multiprobe success with addr %s:%d\n",
3169 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3170 ntohs(interfaces[multi_i].port)));
3175 ("multiprobe failure with addr %s:%d\n",
3176 afs_inet_ntoa_r(interfaces[multi_i].addr, hoststr),
3177 ntohs(interfaces[multi_i].port)));
3179 /* This is less than desirable but its the best we can do.
3180 * The AFS Cache Manager will return either 0 for a Uuid
3181 * match and a 1 for a non-match. If the error is 1 we
3182 * therefore know that our mapping of IP address to Uuid
3183 * is wrong. We should attempt to find the correct
3184 * Uuid and fix the host tables.
3186 if (multi_error == 1) {
3187 /* remove the current alternate address from this host */
3189 removeInterfaceAddr_r(host, interfaces[multi_i].addr, interfaces[multi_i].port);
3193 #ifdef AFS_DEMAND_ATTACH_FS
3194 /* try to bail ASAP if the fileserver is shutting down */
3196 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3205 /* Destroy all connections except the one on which we succeeded */
3206 for (i = 0; i < j; i++)
3207 if (conns[i] != connSuccess)
3208 rx_DestroyConnection(conns[i]);
3214 return 0; /* success */
3216 return 1; /* failure */
3219 #endif /* !defined(INTERPRET_DUMP) */