e803d1f40b26a3902606ca2f81ae75e16c9cf719
[openafs.git] / src / ubik / beacon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <sys/types.h>
16 #include <string.h>
17 #include <stdarg.h>
18 #include <errno.h>
19
20 #ifdef AFS_NT40_ENV
21 #include <winsock2.h>
22 #include <time.h>
23 #else
24 #include <sys/file.h>
25 #include <sys/time.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <netdb.h>
29 #endif
30
31 #include <lock.h>
32 #include <rx/xdr.h>
33 #include <rx/rx.h>
34 #include <rx/rx_multi.h>
35 #include <afs/cellconfig.h>
36 #ifndef AFS_NT40_ENV
37 #include <afs/afsutil.h>
38 #include <afs/netutils.h>
39 #endif
40
41 #define UBIK_INTERNALS
42 #include "ubik.h"
43 #include "ubik_int.h"
44
45 /*! \name statics used to determine if we're the sync site */
46 static afs_int32 syncSiteUntil = 0;     /*!< valid only if amSyncSite */
47 int ubik_amSyncSite = 0;        /*!< flag telling if I'm sync site */
48 static int nServers;            /*!< total number of servers */
49 static char amIMagic = 0;       /*!< is this host the magic host */
50 char amIClone = 0;              /*!< is this a clone which doesn't vote */
51 static char ubik_singleServer = 0;
52 /*\}*/
53 int (*ubik_CRXSecurityProc) (void *rock, struct rx_securityClass **,
54                              afs_int32 *);
55 void *ubik_CRXSecurityRock;
56 afs_int32 ubikSecIndex;
57 struct rx_securityClass *ubikSecClass;
58 static int ubeacon_InitServerListCommon(afs_uint32 ame,
59                                         struct afsconf_cell *info,
60                                         char clones[],
61                                         afs_uint32 aservers[]);
62 static int verifyInterfaceAddress(afs_uint32 *ame, struct afsconf_cell *info,
63                                   afs_uint32 aservers[]);
64 static int updateUbikNetworkAddress(afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR]);
65
66
67 /*! \file
68  * Module responsible for both deciding if we're currently the sync site,
69  * and keeping collecting votes so as to stay sync site.
70  *
71  * The basic module contacts all of the servers it can, trying to get them to vote
72  * for this server for sync site.  The vote request message (called a beacon message)
73  * also specifies until which time this site claims to be the sync site, if at all, thus enabling
74  * receiving sites to know how long the sync site guarantee is made for.
75  *
76  * Each  of these beacon messages is thus both a declaration of how long this site will
77  * remain sync site, and an attempt to extend that time by collecting votes for a later
78  * sync site extension.
79  *
80  * The voting module is responsible for choosing a reasonable time until which it promises
81  * not to vote for someone else.  This parameter (BIG seconds) is not actually passed in
82  * the interface (perhaps it should be?) but is instead a compile time constant that both
83  * sides know about.
84
85  * The beacon and vote modules work intimately together; the vote module decides how long
86  * it should promise the beacon module its vote, and the beacon module takes all of these
87  * votes and decides for how long it is the synchronization site.
88  */
89
90 /*! \brief procedure called from debug rpc call to get this module's state for debugging */
91 void
92 ubeacon_Debug(struct ubik_debug *aparm)
93 {
94     /* fill in beacon's state fields in the ubik_debug structure */
95     aparm->syncSiteUntil = syncSiteUntil;
96     aparm->nServers = nServers;
97 }
98
99 /*!
100  * \brief Procedure that determines whether this site has enough current votes to remain sync site.
101  *
102  * Called from higher-level modules (everything but the vote module).
103  *
104  * If we're the sync site, check that our guarantees, obtained by the ubeacon_Interact()
105  * light-weight process, haven't expired.  We're sync site as long as a majority of the
106  * servers in existence have promised us unexpired guarantees.  The variable #ubik_syncSiteUntil
107  * contains the time at which the latest of the majority of the sync site guarantees expires
108  * (if the variable #ubik_amSyncSite is true)
109  * This module also calls up to the recovery module if it thinks that the recovery module
110  * may have to pick up a new database (which offucr sif [sic] we lose the sync site votes).
111  *
112  * \return 1 if local site is the sync site
113  * \return 0 if sync site is elsewhere
114  */
115 int
116 ubeacon_AmSyncSite(void)
117 {
118     afs_int32 now;
119     afs_int32 rcode;
120
121     /* special case for fast startup */
122     if (nServers == 1 && !amIClone) {
123         return 1;               /* one guy is always the sync site */
124     }
125
126     if (ubik_amSyncSite == 0 || amIClone)
127         rcode = 0;              /* if I don't think I'm the sync site, say so */
128     else {
129         now = FT_ApproxTime();
130         if (syncSiteUntil <= now) {     /* if my votes have expired, say so */
131             if (ubik_amSyncSite)
132                 ubik_dprint("Ubik: I am no longer the sync site\n");
133             ubik_amSyncSite = 0;
134             rcode = 0;
135         } else {
136             rcode = 1;          /* otherwise still have the required votes */
137         }
138     }
139     if (rcode == 0)
140         urecovery_ResetState(); /* force recovery to re-execute */
141     ubik_dprint("beacon: amSyncSite is %d\n", rcode);
142     return rcode;
143 }
144
145 /*!
146  * \see ubeacon_InitServerListCommon()
147  */
148 int
149 ubeacon_InitServerListByInfo(afs_uint32 ame, struct afsconf_cell *info,
150                              char clones[])
151 {
152     afs_int32 code;
153
154     code = ubeacon_InitServerListCommon(ame, info, clones, 0);
155     return code;
156 }
157
158 /*!
159  * \param ame "address of me"
160  * \param aservers list of other servers
161  *
162  * \see ubeacon_InitServerListCommon()
163  */
164 int
165 ubeacon_InitServerList(afs_uint32 ame, afs_uint32 aservers[])
166 {
167     afs_int32 code;
168
169     code =
170         ubeacon_InitServerListCommon(ame, (struct afsconf_cell *)0, 0,
171                                      aservers);
172     return code;
173 }
174
175 /*!
176  * \brief setup server list
177  *
178  * \param ame "address of me"
179  * \param aservers list of other servers
180  *
181  * called only at initialization to set up the list of servers to
182  * contact for votes.  Just creates the server structure.
183  *
184  * The "magic" host is the one with the lowest internet address.  It is
185  * magic because its vote counts epsilon more than the others.  This acts
186  * as a tie-breaker when we have an even number of hosts in the system.
187  * For example, if the "magic" host is up in a 2 site system, then it
188  * is sync site.  Without the magic host hack, if anyone crashed in a 2
189  * site system, we'd be out of business.
190  *
191  * \note There are two connections in every server structure, one for
192  * vote calls (which must always go through quickly) and one for database
193  * operations, which are subject to waiting for locks.  If we used only
194  * one, the votes would sometimes get held up behind database operations,
195  * and the sync site guarantees would timeout even though the host would be
196  * up for communication.
197  *
198  * \see ubeacon_InitServerList(), ubeacon_InitServerListByInfo()
199  */
200 int
201 ubeacon_InitServerListCommon(afs_uint32 ame, struct afsconf_cell *info,
202                              char clones[], afs_uint32 aservers[])
203 {
204     struct ubik_server *ts;
205     afs_int32 me = -1;
206     afs_int32 servAddr;
207     afs_int32 i, code;
208     afs_int32 magicHost;
209     struct ubik_server *magicServer;
210
211     /* verify that the addresses passed in are correct */
212     if ((code = verifyInterfaceAddress(&ame, info, aservers)))
213         return code;
214
215     /* get the security index to use, if we can */
216     if (ubik_CRXSecurityProc) {
217         i = (*ubik_CRXSecurityProc) (ubik_CRXSecurityRock, &ubikSecClass,
218                                      &ubikSecIndex);
219     } else
220         i = 1;
221     if (i) {
222         /* don't have sec module yet */
223         ubikSecIndex = 0;
224         ubikSecClass = rxnull_NewClientSecurityObject();
225     }
226     magicHost = ntohl(ame);     /* do comparisons in host order */
227     magicServer = (struct ubik_server *)0;
228
229     if (info) {
230         for (i = 0; i < info->numServers; i++) {
231             if (ntohl((afs_uint32) info->hostAddr[i].sin_addr.s_addr) ==
232                 ntohl((afs_uint32) ame)) {
233                 me = i;
234                 if (clones[i]) {
235                     amIClone = 1;
236                     magicHost = 0;
237                 }
238             }
239         }
240         nServers = 0;
241         for (i = 0; i < info->numServers; i++) {
242             if (i == me)
243                 continue;
244             ts = (struct ubik_server *)malloc(sizeof(struct ubik_server));
245             memset(ts, 0, sizeof(struct ubik_server));
246             ts->next = ubik_servers;
247             ubik_servers = ts;
248             ts->addr[0] = info->hostAddr[i].sin_addr.s_addr;
249             if (clones[i]) {
250                 ts->isClone = 1;
251             } else {
252                 if (!magicHost
253                     || ntohl((afs_uint32) ts->addr[0]) <
254                     (afs_uint32) magicHost) {
255                     magicHost = ntohl(ts->addr[0]);
256                     magicServer = ts;
257                 }
258                 ++nServers;
259             }
260             /* for vote reqs */
261             ts->vote_rxcid =
262                 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
263                                  ubik_callPortal, VOTE_SERVICE_ID,
264                                  ubikSecClass, ubikSecIndex);
265             /* for disk reqs */
266             ts->disk_rxcid =
267                 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
268                                  ubik_callPortal, DISK_SERVICE_ID,
269                                  ubikSecClass, ubikSecIndex);
270             ts->up = 1;
271         }
272     } else {
273         i = 0;
274         while ((servAddr = *aservers++)) {
275             if (i >= MAXSERVERS)
276                 return UNHOSTS; /* too many hosts */
277             ts = (struct ubik_server *)malloc(sizeof(struct ubik_server));
278             memset(ts, 0, sizeof(struct ubik_server));
279             ts->next = ubik_servers;
280             ubik_servers = ts;
281             ts->addr[0] = servAddr;     /* primary address in  net byte order */
282             ts->vote_rxcid = rx_NewConnection(servAddr, ubik_callPortal, VOTE_SERVICE_ID, ubikSecClass, ubikSecIndex);  /* for vote reqs */
283             ts->disk_rxcid = rx_NewConnection(servAddr, ubik_callPortal, DISK_SERVICE_ID, ubikSecClass, ubikSecIndex);  /* for disk reqs */
284             ts->isClone = 0;    /* don't know about clones */
285             ts->up = 1;
286             if (ntohl((afs_uint32) servAddr) < (afs_uint32) magicHost) {
287                 magicHost = ntohl(servAddr);
288                 magicServer = ts;
289             }
290             i++;
291         }
292     }
293     if (magicServer)
294         magicServer->magic = 1; /* remember for when counting votes */
295
296     if (!amIClone && !magicServer)
297         amIMagic = 1;
298     if (info) {
299         if (!amIClone)
300             ++nServers;         /* count this server as well as the remotes */
301     } else
302         nServers = i + 1;       /* count this server as well as the remotes */
303
304     ubik_quorum = (nServers >> 1) + 1;  /* compute the majority figure */
305     /* send addrs to all other servers */
306     code = updateUbikNetworkAddress(ubik_host);
307     if (code)
308         return code;
309
310 /* Shoud we set some defaults for RX??
311     r_retryInterval = 2;
312     r_nRetries = (RPCTIMEOUT/r_retryInterval);
313 */
314     if (info) {
315         if (!ubik_servers)      /* special case 1 server */
316             ubik_singleServer = 1;
317         if (nServers == 1 && !amIClone) {
318             ubik_amSyncSite = 1;        /* let's start as sync site */
319             syncSiteUntil = 0x7fffffff; /* and be it quite a while */
320         }
321     } else {
322         if (nServers == 1)      /* special case 1 server */
323             ubik_singleServer = 1;
324     }
325
326     if (ubik_singleServer) {
327         if (!ubik_amSyncSite)
328             ubik_dprint("Ubik: I am the sync site - 1 server\n");
329         ubik_amSyncSite = 1;
330         syncSiteUntil = 0x7fffffff;     /* quite a while */
331     }
332     return 0;
333 }
334
335 /*!
336  * \brief main lwp loop for code that sends out beacons.
337  *
338  * This code only runs while we're sync site or we want to be the sync site.
339  * It runs in its very own light-weight process.
340  */
341 void *
342 ubeacon_Interact(void *dummy)
343 {
344     afs_int32 code;
345     struct timeval tt;
346     struct rx_connection *connections[MAXSERVERS];
347     struct ubik_server *servers[MAXSERVERS];
348     afs_int32 i;
349     struct ubik_server *ts;
350     afs_int32 temp, yesVotes, lastWakeupTime, oldestYesVote, syncsite;
351     struct ubik_tid ttid;
352     afs_int32 startTime;
353
354     /* loop forever getting votes */
355     lastWakeupTime = 0;         /* keep track of time we last started a vote collection */
356     while (1) {
357
358         /* don't wakeup more than every POLLTIME seconds */
359         temp = (lastWakeupTime + POLLTIME) - FT_ApproxTime();
360         /* don't sleep if last collection phase took too long (probably timed someone out ) */
361         if (temp > 0) {
362             if (temp > POLLTIME)
363                 temp = POLLTIME;
364             tt.tv_sec = temp;
365             tt.tv_usec = 0;
366 #ifdef AFS_PTHREAD_ENV
367             code = select(0, 0, 0, 0, &tt);
368 #else
369             code = IOMGR_Select(0, 0, 0, 0, &tt);
370 #endif
371         } else
372             code = 0;
373
374         lastWakeupTime = FT_ApproxTime();       /* started a new collection phase */
375
376         if (ubik_singleServer)
377             continue;           /* special-case 1 server for speedy startup */
378
379         if (!uvote_ShouldIRun())
380             continue;           /* if voter has heard from a better candidate than us, don't bother running */
381
382         /* otherwise we should run for election, or we're the sync site (and have already won);
383          * send out the beacon packets */
384         /* build list of all up hosts (noticing dead hosts are running again
385          * is a task for the recovery module, not the beacon module), and
386          * prepare to send them an r multi-call containing the beacon message */
387         i = 0;                  /* collect connections */
388         for (ts = ubik_servers; ts; ts = ts->next) {
389             if (ts->up && ts->addr[0] != ubik_host[0]) {
390                 servers[i] = ts;
391                 connections[i++] = ts->vote_rxcid;
392             }
393         }
394         servers[i] = (struct ubik_server *)0;   /* end of list */
395         /* note that we assume in the vote module that we'll always get at least BIGTIME
396          * seconds of vote from anyone who votes for us, which means we can conservatively
397          * assume we'll be fine until SMALLTIME seconds after we start collecting votes */
398         /* this next is essentially an expansion of rgen's ServBeacon routine */
399
400         ttid.epoch = ubik_epochTime;
401         if (ubik_dbase->flags & DBWRITING) {
402             /*
403              * if a write is in progress, we have to send the writeTidCounter
404              * which holds the tid counter of the write transaction , and not
405              * send the tidCounter value which holds the tid counter of the
406              * last transaction.
407              */
408             ttid.counter = ubik_dbase->writeTidCounter;
409         } else
410             ttid.counter = ubik_dbase->tidCounter + 1;
411 #if defined(UBIK_PAUSE)
412         ubik_dbase->flags |= DBVOTING;
413 #endif /* UBIK_PAUSE */
414
415         /* now analyze return codes, counting up our votes */
416         yesVotes = 0;           /* count how many to ensure we have quorum */
417         oldestYesVote = 0x7fffffff;     /* time quorum expires */
418         syncsite = ubeacon_AmSyncSite();
419         startTime = FT_ApproxTime();
420         /*
421          * Don't waste time using mult Rx calls if there are no connections out there
422          */
423         if (i > 0) {
424             char hoststr[16];
425             multi_Rx(connections, i) {
426                 multi_VOTE_Beacon(syncsite, startTime, &ubik_dbase->version,
427                                   &ttid);
428                 temp = FT_ApproxTime(); /* now, more or less */
429                 ts = servers[multi_i];
430                 ts->lastBeaconSent = temp;
431                 code = multi_error;
432                 /* note that the vote time (the return code) represents the time
433                  * the vote was computed, *not* the time the vote expires.  We compute
434                  * the latter down below if we got enough votes to go with */
435                 if (code > 0) {
436                     ts->lastVoteTime = code;
437                     if (code < oldestYesVote)
438                         oldestYesVote = code;
439                     ts->lastVote = 1;
440                     if (!ts->isClone)
441                         yesVotes += 2;
442                     if (ts->magic)
443                         yesVotes++;     /* the extra epsilon */
444                     ts->up = 1; /* server is up (not really necessary: recovery does this for real) */
445                     ts->beaconSinceDown = 1;
446                     ubik_dprint("yes vote from host %s\n",
447                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
448                 } else if (code == 0) {
449                     ts->lastVoteTime = temp;
450                     ts->lastVote = 0;
451                     ts->beaconSinceDown = 1;
452                     ubik_dprint("no vote from %s\n",
453                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
454                 } else if (code < 0) {
455                     ts->up = 0;
456                     ts->beaconSinceDown = 0;
457                     urecovery_LostServer();
458                     ubik_dprint("time out from %s\n",
459                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
460                 }
461             }
462             multi_End;
463         }
464         /* now call our own voter module to see if we'll vote for ourself.  Note that
465          * the same restrictions apply for our voting for ourself as for our voting
466          * for anyone else. */
467         i = SVOTE_Beacon((struct rx_call *)0, ubeacon_AmSyncSite(), startTime,
468                          &ubik_dbase->version, &ttid);
469         if (i) {
470             yesVotes += 2;
471             if (amIMagic)
472                 yesVotes++;     /* extra epsilon */
473             if (i < oldestYesVote)
474                 oldestYesVote = i;
475         }
476 #if defined(UBIK_PAUSE)
477         ubik_dbase->flags &= ~DBVOTING;
478 #endif /* UBIK_PAUSE */
479
480         /* now decide if we have enough votes to become sync site.
481          * Note that we can still get enough votes even if we didn't for ourself. */
482         if (yesVotes > nServers) {      /* yesVotes is bumped by 2 or 3 for each site */
483             if (!ubik_amSyncSite)
484                 ubik_dprint("Ubik: I am the sync site\n");
485             ubik_amSyncSite = 1;
486             syncSiteUntil = oldestYesVote + SMALLTIME;
487 #ifndef AFS_PTHREAD_ENV
488                 /* I did not find a corresponding LWP_WaitProcess(&ubik_amSyncSite) --
489                    this may be a spurious signal call -- sjenkins */
490                 LWP_NoYieldSignal(&ubik_amSyncSite);
491 #endif
492         } else {
493             if (ubik_amSyncSite)
494                 ubik_dprint("Ubik: I am no longer the sync site\n");
495             ubik_amSyncSite = 0;
496             urecovery_ResetState();     /* tell recovery we're no longer the sync site */
497         }
498
499     }                           /* while loop */
500     return NULL;
501 }
502
503 /*!
504  * \brief Verify that a given IP addresses does actually exist on this machine.
505  *
506  * \param ame      the pointer to my IP address specified in the
507  *                 CellServDB file.
508  * \param aservers an array containing IP
509  *                 addresses of remote ubik servers. The array is
510  *                 terminated by a zero address.
511  *
512  * Algorithm     : Verify that my IP addresses \p ame does actually exist
513  *                 on this machine.  If any of my IP addresses are there
514  *                 in the remote server list \p aserver, remove them from
515  *                 this list.  Update global variable \p ubik_host[] with
516  *                 my IP addresses.
517  *
518  * \return 0 on success, non-zero on failure
519  */
520 static int
521 verifyInterfaceAddress(afs_uint32 *ame, struct afsconf_cell *info,
522                        afs_uint32 aservers[]) {
523     afs_uint32 myAddr[UBIK_MAX_INTERFACE_ADDR], *servList, tmpAddr;
524     afs_uint32 myAddr2[UBIK_MAX_INTERFACE_ADDR];
525     char hoststr[16];
526     int tcount, count, found, i, j, totalServers, start, end, usednetfiles =
527         0;
528
529     if (info)
530         totalServers = info->numServers;
531     else {                      /* count the number of servers */
532         for (totalServers = 0, servList = aservers; *servList; servList++)
533             totalServers++;
534     }
535
536     if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || AFSDIR_SERVER_NETINFO_FILEPATH) {
537         /*
538          * Find addresses we are supposed to register as per the netrestrict file
539          * if it exists, else just register all the addresses we find on this
540          * host as returned by rx_getAllAddr (in NBO)
541          */
542         char reason[1024];
543         count =
544             parseNetFiles(myAddr, NULL, NULL, UBIK_MAX_INTERFACE_ADDR, reason,
545                           AFSDIR_SERVER_NETINFO_FILEPATH,
546                           AFSDIR_SERVER_NETRESTRICT_FILEPATH);
547         if (count < 0) {
548             ubik_print("ubik: Can't register any valid addresses:%s\n",
549                        reason);
550             ubik_print("Aborting..\n");
551             return UBADHOST;
552         }
553         usednetfiles++;
554     } else {
555         /* get all my interface addresses in net byte order */
556         count = rx_getAllAddr(myAddr, UBIK_MAX_INTERFACE_ADDR);
557     }
558
559     if (count <= 0) {           /* no address found */
560         ubik_print("ubik: No network addresses found, aborting..");
561         return UBADHOST;
562     }
563
564     /* verify that the My-address passed in by ubik is correct */
565     for (j = 0, found = 0; j < count; j++) {
566         if (*ame == myAddr[j]) {        /* both in net byte order */
567             found = 1;
568             break;
569         }
570     }
571
572     if (!found) {
573         ubik_print("ubik: primary address %s does not exist\n",
574                    afs_inet_ntoa_r(*ame, hoststr));
575         /* if we had the result of rx_getAllAddr already, avoid subverting
576          * the "is gethostbyname(gethostname()) us" check. If we're
577          * using NetInfo/NetRestrict, we assume they have enough clue
578          * to avoid that big hole in their foot from the loaded gun. */
579         if (usednetfiles) {
580             /* take the address we did get, then see if ame was masked */
581             *ame = myAddr[0];
582             tcount = rx_getAllAddr(myAddr2, UBIK_MAX_INTERFACE_ADDR);
583             if (tcount <= 0) {  /* no address found */
584                 ubik_print("ubik: No network addresses found, aborting..");
585                 return UBADHOST;
586             }
587
588             /* verify that the My-address passed in by ubik is correct */
589             for (j = 0, found = 0; j < tcount; j++) {
590                 if (*ame == myAddr2[j]) {       /* both in net byte order */
591                     found = 1;
592                     break;
593                 }
594             }
595         }
596         if (!found)
597             return UBADHOST;
598     }
599
600     /* if any of my addresses are there in serverList, then
601      ** use that as my primary addresses : the higher level
602      ** application screwed up in dealing with multihomed concepts
603      */
604     for (j = 0, found = 0; j < count; j++) {
605         for (i = 0; i < totalServers; i++) {
606             if (info)
607                 tmpAddr = (afs_uint32) info->hostAddr[i].sin_addr.s_addr;
608             else
609                 tmpAddr = aservers[i];
610             if (myAddr[j] == tmpAddr) {
611                 *ame = tmpAddr;
612                 if (!info)
613                     aservers[i] = 0;
614                 found = 1;
615             }
616         }
617     }
618     if (found)
619         ubik_print("Using %s as my primary address\n", afs_inet_ntoa_r(*ame, hoststr));
620
621     if (!info) {
622         /* get rid of servers which were purged because all
623          ** those interface addresses are myself
624          */
625         for (start = 0, end = totalServers - 1; (start < end); start++, end--) {
626             /* find the first zero entry from the beginning */
627             for (; (start < end) && (aservers[start]); start++);
628
629             /* find the last non-zero entry from the end */
630             for (; (end >= 0) && (!aservers[end]); end--);
631
632             /* if there is nothing more to purge, exit from loop */
633             if (start >= end)
634                 break;
635
636             /* move the entry */
637             aservers[start] = aservers[end];
638             aservers[end] = 0;  /* this entry was moved */
639         }
640     }
641
642     /* update all my addresses in ubik_host in such a way
643      ** that ubik_host[0] has the primary address
644      */
645     ubik_host[0] = *ame;
646     for (j = 0, i = 1; j < count; j++)
647         if (*ame != myAddr[j])
648             ubik_host[i++] = myAddr[j];
649
650     return 0;                   /* return success */
651 }
652
653
654 /*!
655  * \brief Exchange IP address information with remote servers.
656  *
657  * \param ubik_host an array containing all my IP addresses.
658  *
659  * Algorithm     : Do an RPC to all remote ubik servers infroming them
660  *                 about my IP addresses. Get their IP addresses and
661  *                 update my linked list of ubik servers \p ubik_servers
662  *
663  * \return 0 on success, non-zero on failure
664  */
665 static int
666 updateUbikNetworkAddress(afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR])
667 {
668     int j, count, code = 0;
669     UbikInterfaceAddr inAddr, outAddr;
670     struct rx_connection *conns[MAXSERVERS];
671     struct ubik_server *ts, *server[MAXSERVERS];
672     char buffer[32];
673     char hoststr[16];
674
675     for (count = 0, ts = ubik_servers; ts; count++, ts = ts->next) {
676         conns[count] = ts->disk_rxcid;
677         server[count] = ts;
678     }
679
680
681     /* inform all other servers only if there are more than one
682      * database servers in the cell */
683
684     if (count > 0) {
685
686         for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
687             inAddr.hostAddr[j] = ntohl(ubik_host[j]);
688
689
690         /* do the multi-RX RPC to all other servers */
691         multi_Rx(conns, count) {
692             multi_DISK_UpdateInterfaceAddr(&inAddr, &outAddr);
693             ts = server[multi_i];       /* reply received from this server */
694             if (!multi_error) {
695                 if (ts->addr[0] != htonl(outAddr.hostAddr[0])) {
696                     code = UBADHOST;
697                     strcpy(buffer, afs_inet_ntoa_r(ts->addr[0], hoststr));
698                     ubik_print("ubik:Two primary addresses for same server \
699                     %s %s\n", buffer,
700                     afs_inet_ntoa_r(htonl(outAddr.hostAddr[0]), hoststr));
701                 } else {
702                     for (j = 1; j < UBIK_MAX_INTERFACE_ADDR; j++)
703                         ts->addr[j] = htonl(outAddr.hostAddr[j]);
704                 }
705             } else if (multi_error == RXGEN_OPCODE) {   /* pre 3.5 remote server */
706                 ubik_print
707                     ("ubik server %s does not support UpdateInterfaceAddr RPC\n",
708                      afs_inet_ntoa_r(ts->addr[0], hoststr));
709             } else if (multi_error == UBADHOST) {
710                 code = UBADHOST;        /* remote CellServDB inconsistency */
711                 ubik_print("Inconsistent Cell Info on server: ");
712                 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR && ts->addr[j]; j++)
713                     ubik_print("%s ", afs_inet_ntoa_r(ts->addr[j], hoststr));
714                 ubik_print("\n");
715             } else {
716                 ts->up = 0;     /* mark the remote server as down */
717             }
718         }
719         multi_End;
720     }
721     return code;
722 }