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
12 #include <afsconfig.h>
13 #include <afs/param.h>
27 #include <netinet/in.h>
32 #include <afs/assert.h>
35 #include <afs/afsint.h>
36 #include <afs/rxgen_consts.h>
38 #include <afs/errors.h>
39 #include <afs/ihandle.h>
40 #include <afs/vnode.h>
41 #include <afs/volume.h>
42 #ifdef AFS_ATHENA_STDENV
46 #include <afs/ptclient.h>
47 #include <afs/ptuser.h>
48 #include <afs/prs_fs.h>
50 #include <afs/afsutil.h>
51 #include <afs/com_err.h>
53 #include <afs/cellconfig.h>
55 #include "viced_prototypes.h"
59 #ifdef AFS_DEMAND_ATTACH_FS
60 #include "../util/afsutil_prototypes.h"
61 #include "../tviced/serialize_state.h"
62 #endif /* AFS_DEMAND_ATTACH_FS */
64 #ifdef AFS_PTHREAD_ENV
65 pthread_mutex_t host_glock_mutex;
66 #endif /* AFS_PTHREAD_ENV */
69 extern int CurrentConnections;
71 extern int AnonymousID;
72 extern prlist AnonCPS;
74 extern struct afsconf_dir *confDir; /* config dir object */
75 extern int lwps; /* the max number of server threads */
76 extern afsUUID FS_HostUUID;
79 int CEs = 0; /* active clients */
80 int CEBlocks = 0; /* number of blocks of CEs */
81 struct client *CEFree = 0; /* first free client */
82 struct host *hostList = 0; /* linked list of all hosts */
83 int hostCount = 0; /* number of hosts in hostList */
87 static struct rx_securityClass *sc = NULL;
89 static void h_SetupCallbackConn_r(struct host * host);
91 #define CESPERBLOCK 73
92 struct CEBlock { /* block of CESPERBLOCK file entries */
93 struct client entry[CESPERBLOCK];
96 static void h_TossStuff_r(register struct host *host);
99 * Make sure the subnet macros have been defined.
102 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
105 #ifndef IN_CLASSA_SUBNET
106 #define IN_CLASSA_SUBNET 0xffff0000
110 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
113 #ifndef IN_CLASSB_SUBNET
114 #define IN_CLASSB_SUBNET 0xffffff00
118 /* get a new block of CEs and chain it on CEFree */
122 register struct CEBlock *block;
125 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
127 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
128 ShutDownAndCore(PANIC);
131 for (i = 0; i < (CESPERBLOCK - 1); i++) {
132 Lock_Init(&block->entry[i].lock);
133 block->entry[i].next = &(block->entry[i + 1]);
135 block->entry[CESPERBLOCK - 1].next = 0;
136 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
137 CEFree = (struct client *)block;
143 /* get the next available CE */
144 static struct client *
147 register struct client *entry;
152 ViceLog(0, ("CEFree NULL in GetCE\n"));
153 ShutDownAndCore(PANIC);
157 CEFree = entry->next;
159 memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
165 /* return an entry to the free list */
167 FreeCE(register struct client *entry)
169 entry->VenusEpoch = 0;
171 entry->next = CEFree;
178 * The HTs and HTBlocks variables were formerly static, but they are
179 * now referenced elsewhere in the FileServer.
181 int HTs = 0; /* active file entries */
182 int HTBlocks = 0; /* number of blocks of HTs */
183 static struct host *HTFree = 0; /* first free file entry */
186 * Hash tables of host pointers. We need two tables, one
187 * to map IP addresses onto host pointers, and another
188 * to map host UUIDs onto host pointers.
190 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
191 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
192 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
193 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
195 struct HTBlock { /* block of HTSPERBLOCK file entries */
196 struct host entry[h_HTSPERBLOCK];
200 /* get a new block of HTs and chain it on HTFree */
204 register struct HTBlock *block;
206 static int index = 0;
208 if (HTBlocks == h_MAXHOSTTABLES) {
209 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
210 ShutDownAndCore(PANIC);
213 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
215 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
216 ShutDownAndCore(PANIC);
218 #ifdef AFS_PTHREAD_ENV
219 for (i = 0; i < (h_HTSPERBLOCK); i++)
220 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
221 #endif /* AFS_PTHREAD_ENV */
222 for (i = 0; i < (h_HTSPERBLOCK); i++)
223 Lock_Init(&block->entry[i].lock);
224 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
225 block->entry[i].next = &(block->entry[i + 1]);
226 for (i = 0; i < (h_HTSPERBLOCK); i++)
227 block->entry[i].index = index++;
228 block->entry[h_HTSPERBLOCK - 1].next = 0;
229 HTFree = (struct host *)block;
230 hosttableptrs[HTBlocks++] = block->entry;
235 /* get the next available HT */
239 register struct host *entry;
243 assert(HTFree != NULL);
245 HTFree = entry->next;
247 memset((char *)entry, 0, HOST_TO_ZERO(entry));
253 /* return an entry to the free list */
255 FreeHT(register struct host *entry)
257 entry->next = HTFree;
264 hpr_Initialize(struct ubik_client **uclient)
267 struct rx_connection *serverconns[MAXSERVERS];
268 struct rx_securityClass *sc[3];
269 struct afsconf_dir *tdir;
270 char tconfDir[100] = "";
272 struct ktc_token ttoken;
274 struct afsconf_cell info;
278 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
280 ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
284 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
286 ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]", code));
291 code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
293 ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s", cellstr, confDir, AFSDIR_CELLSERVDB_FILE));
300 ViceLog(0, ("hpr_Initialize: Could not initialize rx."));
309 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
310 * to force use of the KeyFile. secLevel == 0 implies -noauth was
312 if ((afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
313 code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
315 ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s (so trying noauth)", code, afs_error_message(code)));
317 scIndex = 0; /* use noauth */
319 /* if there was a problem, an unauthenticated conn is returned */
322 struct ktc_principal sname;
323 strcpy(sname.cell, info.name);
324 sname.instance[0] = 0;
325 strcpy(sname.name, "afs");
326 code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
330 if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
331 /* this is a kerberos ticket, set scIndex accordingly */
334 ViceLog(0, ("hpr_Initialize: funny kvno (%d) in ticket, proceeding", ttoken.kvno));
338 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
339 ttoken.kvno, ttoken.ticketLen,
343 if ((scIndex == 0) && (sc[0] == 0))
344 sc[0] = rxnull_NewClientSecurityObject();
346 ViceLog(0, ("hpr_Initialize: Could not get afs tokens, running unauthenticated. [%d]", code));
348 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
349 for (i = 0; i < info.numServers; i++) {
351 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
352 info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
356 code = ubik_ClientInit(serverconns, uclient);
358 ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
361 code = rxs_Release(sc[scIndex]);
366 hpr_End(struct ubik_client *uclient)
371 code = ubik_ClientDestroy(uclient);
377 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
379 #ifdef AFS_PTHREAD_ENV
380 register afs_int32 code;
382 struct ubik_client *uclient =
383 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
386 code = hpr_Initialize(&uclient);
388 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
394 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
395 if (code != PRSUCCESS)
398 /* do something about this, probably make a new call */
399 /* don't forget there's a hard limit in the interface */
401 "membership list for host id %d exceeds display limit\n",
406 return pr_GetHostCPS(host, CPS);
411 hpr_NameToId(namelist *names, idlist *ids)
413 #ifdef AFS_PTHREAD_ENV
414 register afs_int32 code;
415 register afs_int32 i;
416 struct ubik_client *uclient =
417 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
420 code = hpr_Initialize(&uclient);
422 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
427 for (i = 0; i < names->namelist_len; i++)
428 stolower(names->namelist_val[i]);
429 code = ubik_PR_NameToID(uclient, 0, names, ids);
432 return pr_NameToId(names, ids);
437 hpr_IdToName(idlist *ids, namelist *names)
439 #ifdef AFS_PTHREAD_ENV
440 register afs_int32 code;
441 struct ubik_client *uclient =
442 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
445 code = hpr_Initialize(&uclient);
447 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
452 code = ubik_PR_IDToName(uclient, 0, ids, names);
455 return pr_IdToName(ids, names);
460 hpr_GetCPS(afs_int32 id, prlist *CPS)
462 #ifdef AFS_PTHREAD_ENV
463 register afs_int32 code;
465 struct ubik_client *uclient =
466 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
469 code = hpr_Initialize(&uclient);
471 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
477 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
478 if (code != PRSUCCESS)
481 /* do something about this, probably make a new call */
482 /* don't forget there's a hard limit in the interface */
483 fprintf(stderr, "membership list for id %d exceeds display limit\n",
488 return pr_GetCPS(id, CPS);
492 static short consolePort = 0;
495 h_Release(register struct host *host)
504 * If this thread does not have a hold on this host AND
505 * if other threads also dont have any holds on this host AND
506 * If either the HOSTDELETED or CLIENTDELETED flags are set
510 h_Release_r(register struct host *host)
513 if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
514 if (!h_OtherHolds_r(host)) {
515 /* must avoid masking this until after h_OtherHolds_r runs
516 * but it should be run before h_TossStuff_r */
517 (host)->holds[h_holdSlot()] &= ~h_holdbit();
518 if ((host->hostFlags & HOSTDELETED)
519 || (host->hostFlags & CLIENTDELETED)) {
523 (host)->holds[h_holdSlot()] &= ~h_holdbit();
525 (host)->holds[h_holdSlot()] &= ~h_holdbit();
531 h_OtherHolds_r(register struct host *host)
533 register int i, bit, slot;
536 for (i = 0; i < h_maxSlots; i++) {
537 if (host->holds[i] != ((i == slot) ? bit : 0)) {
545 h_Lock_r(register struct host *host)
555 * returns 1 if already locked
556 * else returns locks and returns 0
560 h_NBLock_r(register struct host *host)
562 struct Lock *hostLock = &host->lock;
567 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
568 hostLock->excl_locked = WRITE_LOCK;
572 LOCK_UNLOCK(hostLock);
581 #if FS_STATS_DETAILED
582 /*------------------------------------------------------------------------
583 * PRIVATE h_AddrInSameNetwork
586 * Given a target IP address and a candidate IP address (both
587 * in host byte order), return a non-zero value (1) if the
588 * candidate address is in a different network from the target
592 * a_targetAddr : Target address.
593 * a_candAddr : Candidate address.
596 * 1 if the candidate address is in the same net as the target,
600 * The target and candidate addresses are both in host byte
601 * order, NOT network byte order, when passed in. We return
602 * our value as a character, since that's the type of field in
603 * the host structure, where this info will be stored.
607 *------------------------------------------------------------------------*/
610 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
611 { /*h_AddrInSameNetwork */
613 afs_uint32 targetNet;
617 * Pull out the network and subnetwork numbers from the target
618 * and candidate addresses. We can short-circuit this whole
619 * affair if the target and candidate addresses are not of the
622 if (IN_CLASSA(a_targetAddr)) {
623 if (!(IN_CLASSA(a_candAddr))) {
626 targetNet = a_targetAddr & IN_CLASSA_NET;
627 candNet = a_candAddr & IN_CLASSA_NET;
628 } else if (IN_CLASSB(a_targetAddr)) {
629 if (!(IN_CLASSB(a_candAddr))) {
632 targetNet = a_targetAddr & IN_CLASSB_NET;
633 candNet = a_candAddr & IN_CLASSB_NET;
634 } /*Class B target */
635 else if (IN_CLASSC(a_targetAddr)) {
636 if (!(IN_CLASSC(a_candAddr))) {
639 targetNet = a_targetAddr & IN_CLASSC_NET;
640 candNet = a_candAddr & IN_CLASSC_NET;
641 } /*Class C target */
643 targetNet = a_targetAddr;
644 candNet = a_candAddr;
645 } /*Class D address */
648 * Now, simply compare the extracted net values for the two addresses
649 * (which at this point are known to be of the same class)
651 if (targetNet == candNet)
656 } /*h_AddrInSameNetwork */
657 #endif /* FS_STATS_DETAILED */
660 /* Assumptions: called with held host */
662 h_gethostcps_r(register struct host *host, register afs_int32 now)
667 /* wait if somebody else is already doing the getCPS call */
668 while (host->hostFlags & HCPS_INPROGRESS) {
669 slept = 1; /* I did sleep */
670 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
671 #ifdef AFS_PTHREAD_ENV
672 pthread_cond_wait(&host->cond, &host_glock_mutex);
673 #else /* AFS_PTHREAD_ENV */
674 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
675 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
676 #endif /* AFS_PTHREAD_ENV */
680 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
681 if (host->hcps.prlist_val)
682 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
683 host->hcps.prlist_val = NULL;
684 host->hcps.prlist_len = 0;
685 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
688 code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
693 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
694 * and reevaluates things if no sync server or quorum is found we could still end up
695 * with one of these errors. In such case we would like to reevaluate the rpc call to
696 * find if there's cps for this guy. We treat other errors (except network failures
697 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
698 * we could like to deal this problem the other way around (i.e. if code == NOCPS
699 * ignore else retry next time) but the problem is that there're other errors (i.e.
700 * EPERM) for which we don't want to retry and we don't know the whole code list!
702 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
704 * We would have preferred to use a while loop and try again since ops in protected
705 * acls for this host will fail now but they'll be reevaluated on any subsequent
706 * call. The attempt to wait for a quorum/sync site or network error won't work
707 * since this problems really should only occurs during a complete fileserver
708 * restart. Since the fileserver will start before the ptservers (and thus before
709 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
711 host->hcpsfailed = 1;
713 ("Warning: GetHostCPS failed (%d) for %x (%s:%d); will retry\n",
714 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
716 host->hcpsfailed = 0;
718 ("gethost: GetHostCPS failed (%d) for %x (%s:%d); ignored\n",
719 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
721 if (host->hcps.prlist_val)
722 free(host->hcps.prlist_val);
723 host->hcps.prlist_val = NULL;
724 host->hcps.prlist_len = 0; /* Make sure it's zero */
726 host->hcpsfailed = 0;
728 host->hostFlags &= ~HCPS_INPROGRESS;
729 /* signal all who are waiting */
730 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
731 host->hostFlags &= ~HCPS_WAITING;
732 #ifdef AFS_PTHREAD_ENV
733 assert(pthread_cond_broadcast(&host->cond) == 0);
734 #else /* AFS_PTHREAD_ENV */
735 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
736 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
737 #endif /* AFS_PTHREAD_ENV */
741 /* args in net byte order */
743 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
745 register struct host *host;
749 host = h_Lookup_r(hostaddr, hport, &held);
751 host->hcpsfailed = 1;
761 * Allocate a host. It will be identified by the peer (ip,port) info in the
762 * rx connection provided. The host is returned held and locked
764 #define DEF_ROPCONS 2115
767 h_Alloc_r(register struct rx_connection *r_con)
769 struct servent *serverentry;
772 #if FS_STATS_DETAILED
773 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
774 #endif /* FS_STATS_DETAILED */
778 host->host = rxr_HostOf(r_con);
779 host->port = rxr_PortOf(r_con);
781 h_AddHostToAddrHashTable_r(host->host, host->port, host);
783 if (consolePort == 0) { /* find the portal number for console */
784 #if defined(AFS_OSF_ENV)
785 serverentry = getservbyname("ropcons", "");
787 serverentry = getservbyname("ropcons", 0);
790 consolePort = serverentry->s_port;
792 consolePort = htons(DEF_ROPCONS); /* Use a default */
794 if (host->port == consolePort)
796 /* Make a callback channel even for the console, on the off chance that it
797 * makes a request that causes a break call back. It shouldn't. */
798 h_SetupCallbackConn_r(host);
799 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
801 host->hcps.prlist_val = NULL;
802 host->hcps.prlist_len = 0;
803 host->interface = NULL;
805 host->hcpsfailed = 0; /* save cycles */
806 h_gethostcps(host); /* do this under host hold/lock */
808 host->FirstClient = NULL;
811 h_InsertList_r(host); /* update global host List */
812 #if FS_STATS_DETAILED
814 * Compare the new host's IP address (in host byte order) with ours
815 * (the File Server's), remembering if they are in the same network.
817 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
818 host->InSameNetwork =
819 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
820 #endif /* FS_STATS_DETAILED */
827 /* Make a callback channel even for the console, on the off chance that it
828 * makes a request that causes a break call back. It shouldn't. */
830 h_SetupCallbackConn_r(struct host * host)
833 sc = rxnull_NewClientSecurityObject();
834 host->callback_rxcon =
835 rx_NewConnection(host->host, host->port, 1, sc, 0);
836 rx_SetConnDeadTime(host->callback_rxcon, 50);
837 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
840 /* Lookup a host given an IP address and UDP port number. */
841 /* hostaddr and hport are in network order */
842 /* Note: host should be released by caller if 0 == *heldp and non-null */
843 /* hostaddr and hport are in network order */
845 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
848 struct host *host = NULL;
849 struct h_AddrHashChain *chain;
850 int index = h_HashIndex(haddr);
851 extern int hostaclRefresh;
854 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
855 host = chain->hostPtr;
857 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
858 && chain->port == hport) {
859 *heldp = h_Held_r(host);
863 if (host->hostFlags & HOSTDELETED) {
870 now = FT_ApproxTime(); /* always evaluate "now" */
871 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
873 * Every hostaclRefresh period (def 2 hrs) get the new
874 * membership list for the host. Note this could be the
875 * first time that the host is added to a group. Also
876 * here we also retry on previous legitimate hcps failures.
878 * If we get here we still have a host hold.
880 h_gethostcps_r(host, now);
890 /* Lookup a host given its UUID. */
892 h_LookupUuid_r(afsUUID * uuidp)
894 struct host *host = 0;
895 struct h_UuidHashChain *chain;
896 int index = h_UuidHashIndex(uuidp);
898 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
899 host = chain->hostPtr;
901 if (!(host->hostFlags & HOSTDELETED) && host->interface
902 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
913 * h_Hold_r: Establish a hold by the current LWP on this host--the host
914 * or its clients will not be physically deleted until all holds have
916 * NOTE: h_Hold_r is a macro defined in host.h.
919 /* h_TossStuff_r: Toss anything in the host structure (the host or
920 * clients marked for deletion. Called from h_Release_r ONLY.
921 * To be called, there must be no holds, and either host->deleted
922 * or host->clientDeleted must be set.
925 h_TossStuff_r(register struct host *host)
927 register struct client **cp, *client;
930 /* if somebody still has this host held */
931 for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
935 /* if somebody still has this host locked */
936 if (h_NBLock_r(host) != 0) {
939 ("Warning: h_TossStuff_r failed; Host %x (%s:%d) was locked.\n",
940 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
946 /* ASSUMPTION: rxi_FreeConnection() does not yield */
947 for (cp = &host->FirstClient; (client = *cp);) {
948 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
950 ObtainWriteLockNoBlock(&client->lock, code);
954 ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x was locked.\n",
955 host, afs_inet_ntoa_r(host->host, hoststr),
956 ntohs(host->port), client));
960 if (client->refCount) {
963 ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x refcount %d.\n",
964 host, afs_inet_ntoa_r(host->host, hoststr),
965 ntohs(host->port), client, client->refCount));
966 /* This is the same thing we do if the host is locked */
967 ReleaseWriteLock(&client->lock);
970 client->CPS.prlist_len = 0;
971 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
972 free(client->CPS.prlist_val);
973 client->CPS.prlist_val = NULL;
974 CurrentConnections--;
976 ReleaseWriteLock(&client->lock);
982 /* We've just cleaned out all the deleted clients; clear the flag */
983 host->hostFlags &= ~CLIENTDELETED;
985 if (host->hostFlags & HOSTDELETED) {
986 register struct h_AddrHashChain **ahp, *ath;
987 register struct rx_connection *rxconn;
989 struct AddrPort hostAddrPort;
992 if (host->Console & 1)
994 if ((rxconn = host->callback_rxcon)) {
995 host->callback_rxcon = (struct rx_connection *)0;
996 rx_DestroyConnection(rxconn);
998 if (host->hcps.prlist_val)
999 free(host->hcps.prlist_val);
1000 host->hcps.prlist_val = NULL;
1001 host->hcps.prlist_len = 0;
1002 DeleteAllCallBacks_r(host, 1);
1003 host->hostFlags &= ~RESETDONE; /* just to be safe */
1005 /* if alternate addresses do not exist */
1006 if (!(host->interface)) {
1007 for (ahp = &hostAddrHashTable[h_HashIndex(host->host)]; (ath = *ahp);
1009 assert(ath->hostPtr);
1010 if (ath->hostPtr == host) {
1017 register struct h_UuidHashChain **uhp, *uth;
1018 /* delete the hash entry for the UUID */
1019 uuidp = &host->interface->uuid;
1020 for (uhp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (uth = *uhp);
1022 assert(uth->hostPtr);
1023 if (uth->hostPtr == host) {
1029 /* delete the hash entry for each alternate addresses */
1030 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
1031 hostAddrPort = host->interface->interface[i];
1033 if (!hostAddrPort.valid)
1036 for (ahp = &hostAddrHashTable[h_HashIndex(hostAddrPort.addr)]; (ath = *ahp);
1038 assert(ath->hostPtr);
1039 if (ath->hostPtr == host) {
1046 free(host->interface);
1047 host->interface = NULL;
1048 } /* if alternate address exists */
1050 h_DeleteList_r(host); /* remove host from global host List */
1053 } /*h_TossStuff_r */
1056 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
1057 * system at the start of the enumeration (perhaps more). Hosts may be deleted
1058 * (have delete flag set); ditto for clients. (*proc) is always called with
1059 * host h_held(). The hold state of the host with respect to this lwp is passed
1060 * to (*proc) as the param held. The proc should return 0 if the host should be
1061 * released, 1 if it should be held after enumeration.
1064 h_Enumerate(int (*proc) (), char *param)
1066 register struct host *host, **list;
1068 register int i, count;
1071 if (hostCount == 0) {
1075 list = (struct host **)malloc(hostCount * sizeof(struct host *));
1077 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
1080 held = (int *)malloc(hostCount * sizeof(int));
1082 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
1085 for (count = 0, host = hostList; host; host = host->next, count++) {
1087 if (!(held[count] = h_Held_r(host)))
1090 if (count != hostCount) {
1091 ViceLog(0, ("h_Enumerate found %d of %d hosts\n", count, hostCount));
1093 assert(count <= hostCount);
1095 for (i = 0; i < count; i++) {
1096 held[i] = (*proc) (list[i], held[i], param);
1097 if (!H_ENUMERATE_ISSET_HELD(held[i]))
1098 h_Release(list[i]); /* this might free up the host */
1099 /* bail out of the enumeration early */
1100 if (H_ENUMERATE_ISSET_BAIL(held[i]))
1107 /* h_Enumerate_r (revised):
1108 * Calls (*proc)(host, held, param) for each host in hostList, starting
1110 * Hosts may be deleted (have delete flag set); ditto for clients.
1111 * (*proc) is always called with
1112 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
1113 * host with respect to this lwp is passed to (*proc) as the param held.
1114 * The proc should return 0 if the host should be released, 1 if it should
1115 * be held after enumeration.
1118 h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
1120 register struct host *host, *next;
1121 register int held, nheld;
1123 if (hostCount == 0) {
1126 if (enumstart && !(held = h_Held_r(enumstart)))
1127 h_Hold_r(enumstart);
1128 for (host = enumstart; host; host = next, held = nheld) {
1130 if (next && !(nheld = h_Held_r(next)) && !H_ENUMERATE_ISSET_BAIL(held))
1132 held = (*proc) (host, held, param);
1133 if (!H_ENUMERATE_ISSET_HELD(held))
1134 h_Release_r(host); /* this might free up the host */
1135 if (H_ENUMERATE_ISSET_BAIL(held)) {
1136 if (!H_ENUMERATE_ISSET_HELD(nheld))
1137 h_Release_r(next); /* this might free up the host */
1141 } /*h_Enumerate_r */
1143 /* inserts a new HashChain structure corresponding to this UUID */
1145 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1148 struct h_UuidHashChain *chain;
1150 /* hash into proper bucket */
1151 index = h_UuidHashIndex(uuid);
1153 /* don't add the same entry multiple times */
1154 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1155 if (host->interface && afs_uuid_equal(&host->interface->uuid, uuid))
1159 /* insert into beginning of list for this bucket */
1160 chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
1162 ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1166 chain->hostPtr = host;
1167 chain->next = hostUuidHashTable[index];
1168 hostUuidHashTable[index] = chain;
1172 /* inserts a new HashChain structure corresponding to this address */
1174 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1177 struct h_AddrHashChain *chain;
1181 /* hash into proper bucket */
1182 index = h_HashIndex(addr);
1184 /* don't add the same entry multiple times */
1185 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1186 if (chain->addr == addr && chain->port == port) {
1187 if (chain->hostPtr == host)
1189 else if (!(host->hostFlags & HOSTDELETED))
1190 ViceLog(125, ("Addr %s:%d assigned to %x and %x.\n",
1191 afs_inet_ntoa_r(addr, hoststr), ntohs(port),
1199 /* insert into beginning of list for this bucket */
1200 chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
1202 ViceLog(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1205 chain->hostPtr = host;
1206 chain->next = hostAddrHashTable[index];
1209 hostAddrHashTable[index] = chain;
1213 * This is called with host locked and held. At this point, the
1214 * hostAddrHashTable should not have entries for the alternate
1215 * interfaces. This function has to insert these entries in the
1216 * hostAddrHashTable.
1218 * All addresses are in network byte order.
1221 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1226 struct Interface *interface;
1227 char hoststr[16], hoststr2[16];
1230 assert(host->interface);
1233 * Make sure this address is on the list of known addresses
1236 number = host->interface->numberOfInterfaces;
1237 for (i = 0, found = 0; i < number && !found; i++) {
1238 if (host->interface->interface[i].addr == addr &&
1239 host->interface->interface[i].port == port) {
1241 host->interface->interface[i].valid = 1;
1245 ViceLog(125, ("addInterfaceAddr : host %s:%d addr %s:%d : found:%d\n",
1246 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1247 afs_inet_ntoa_r(addr, hoststr2), ntohs(port), found));
1250 interface = (struct Interface *)
1251 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1253 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
1256 interface->numberOfInterfaces = number + 1;
1257 interface->uuid = host->interface->uuid;
1258 for (i = 0; i < number; i++)
1259 interface->interface[i] = host->interface->interface[i];
1260 interface->interface[number].addr = addr;
1261 interface->interface[number].port = port;
1262 interface->interface[number].valid = 1;
1263 free(host->interface);
1264 host->interface = interface;
1272 * This is called with host locked and held. At this point, the
1273 * hostAddrHashTable should not be having entries for the alternate
1274 * interfaces. This function has to insert these entries in the
1275 * hostAddrHashTable.
1277 * All addresses are in network byte order.
1280 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1285 struct Interface *interface;
1286 char hoststr[16], hoststr2[16];
1289 assert(host->interface);
1291 ViceLog(125, ("removeInterfaceAddr : host %s:%d addr %s:%d\n",
1292 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1293 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
1296 * Make sure this address is on the list of known addresses
1299 interface = host->interface;
1300 number = host->interface->numberOfInterfaces;
1301 for (i = 0, found = 0; i < number && !found; i++) {
1302 if (interface->interface[i].addr == addr &&
1303 interface->interface[i].port == port) {
1305 interface->interface[i].valid = 0;
1310 for (; i < number; i++) {
1311 interface->interface[i] = interface->interface[i+1];
1313 interface->numberOfInterfaces = number;
1317 * Remove the hash table entry for this address
1319 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1325 * This is called with host locked and held. This function differs
1326 * from removeInterfaceAddr_r in that it is called when the address
1327 * is being removed from the host regardless of whether or not there
1328 * is an interface list for the host. This function will delete the
1329 * host if there are no addresses left on it.
1331 * All addresses are in network byte order.
1334 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1337 char hoststr[16], hoststr2[16];
1339 if (!host->interface) {
1340 if (host->host == addr && host->port == port) {
1342 ("Removing only address for host %x (%s:%d), deleting host.\n",
1343 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1344 host->hostFlags |= HOSTDELETED;
1347 removeInterfaceAddr_r(host, host->host, host->port);
1348 if (host->interface->numberOfInterfaces == 0) {
1350 ("Removed only address for host %x (%s:%d), no alternate interfaces, deleting host.\n",
1351 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1352 host->hostFlags |= HOSTDELETED;
1354 struct rx_connection *rxconn;
1356 rxconn = host->callback_rxcon;
1357 host->callback_rxcon = NULL;
1360 struct client *client;
1362 * If rx_DestroyConnection calls h_FreeConnection we will
1363 * deadlock on the host_glock_mutex. Work around the problem
1364 * by unhooking the client from the connection before
1365 * destroying the connection.
1367 client = rx_GetSpecific(rxconn, rxcon_client_key);
1368 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1369 rx_DestroyConnection(rxconn);
1372 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1373 if (host->interface->interface[i].valid) {
1375 ("Removed address for host %x (%s:%d), new primary interface %s:%d.\n",
1376 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1377 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2),
1378 ntohs(host->interface->interface[i].port)));
1379 host->host = host->interface->interface[i].addr;
1380 host->port = host->interface->interface[i].port;
1381 h_AddHostToAddrHashTable_r(host->host, host->port, host);
1386 if (i == host->interface->numberOfInterfaces) {
1388 ("Removed only address for host %x (%s:%d), no valid alternate interfaces, deleting host.\n",
1389 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1390 host->hostFlags |= HOSTDELETED;
1393 sc = rxnull_NewClientSecurityObject();
1394 host->callback_rxcon =
1395 rx_NewConnection(host->host, host->port, 1, sc, 0);
1396 rx_SetConnDeadTime(host->callback_rxcon, 50);
1397 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
1405 h_threadquota(int waiting)
1410 } else if (lwps > 32) {
1413 } else if (lwps > 16) {
1423 /* Host is returned held */
1425 h_GetHost_r(struct rx_connection *tcon)
1428 struct host *oldHost;
1431 struct interfaceAddr interf;
1432 int interfValid = 0;
1433 struct Identity *identP = NULL;
1436 char hoststr[16], hoststr2[16];
1438 struct rx_connection *cb_conn = NULL;
1440 caps.Capabilities_val = NULL;
1442 haddr = rxr_HostOf(tcon);
1443 hport = rxr_PortOf(tcon);
1445 if (caps.Capabilities_val)
1446 free(caps.Capabilities_val);
1447 caps.Capabilities_val = NULL;
1448 caps.Capabilities_len = 0;
1451 host = h_Lookup_r(haddr, hport, &held);
1452 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1453 if (host && !identP && !(host->Console & 1)) {
1454 /* This is a new connection, and we already have a host
1455 * structure for this address. Verify that the identity
1456 * of the caller matches the identity in the host structure.
1458 if ((host->hostFlags & HWHO_INPROGRESS) &&
1459 h_threadquota(host->lock.num_waiting)) {
1465 if (!(host->hostFlags & ALTADDR)) {
1466 host->hostFlags &= ~HWHO_INPROGRESS;
1467 /* Another thread is doing initialization */
1472 ("Host %x (%s:%d) starting h_Lookup again\n",
1473 host, afs_inet_ntoa_r(host->host, hoststr),
1474 ntohs(host->port)));
1477 host->hostFlags &= ~ALTADDR;
1478 cb_conn = host->callback_rxcon;
1479 rx_GetConnection(cb_conn);
1482 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1483 if (code == RXGEN_OPCODE)
1484 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1485 rx_PutConnection(cb_conn);
1488 if ((code == RXGEN_OPCODE) ||
1489 (afs_uuid_equal(&interf.uuid, &nulluuid))) {
1490 identP = (struct Identity *)malloc(sizeof(struct Identity));
1492 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1496 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1497 /* The host on this connection was unable to respond to
1498 * the WhoAreYou. We will treat this as a new connection
1499 * from the existing host. The worst that can happen is
1500 * that we maintain some extra callback state information */
1501 if (host->interface) {
1503 ("Host %x (%s:%d) used to support WhoAreYou, deleting.\n",
1505 afs_inet_ntoa_r(host->host, hoststr),
1506 ntohs(host->port)));
1507 host->hostFlags |= HOSTDELETED;
1508 host->hostFlags &= ~HWHO_INPROGRESS;
1515 } else if (code == 0) {
1517 identP = (struct Identity *)malloc(sizeof(struct Identity));
1519 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1523 identP->uuid = interf.uuid;
1524 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1525 /* Check whether the UUID on this connection matches
1526 * the UUID in the host structure. If they don't match
1527 * then this is not the same host as before. */
1528 if (!host->interface
1529 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1531 ("Uuid doesn't match host %x (%s:%d).\n",
1532 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1534 removeAddress_r(host, host->host, host->port);
1535 host->hostFlags &= ~HWHO_INPROGRESS;
1544 ("CB: WhoAreYou failed for host %x (%s:%d), error %d\n",
1545 host, afs_inet_ntoa_r(host->host, hoststr),
1546 ntohs(host->port), code));
1547 host->hostFlags |= VENUSDOWN;
1549 if (caps.Capabilities_val
1550 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1551 host->hostFlags |= HERRORTRANS;
1553 host->hostFlags &= ~(HERRORTRANS);
1554 host->hostFlags |= ALTADDR;
1555 host->hostFlags &= ~HWHO_INPROGRESS;
1558 if (!(host->hostFlags & ALTADDR)) {
1559 /* another thread is doing the initialisation */
1561 ("Host %x (%s:%d) waiting for host-init to complete\n",
1562 host, afs_inet_ntoa_r(host->host, hoststr),
1563 ntohs(host->port)));
1565 host->hostFlags &= ~HWHO_INPROGRESS;
1570 ("Host %x (%s:%d) starting h_Lookup again\n",
1571 host, afs_inet_ntoa_r(host->host, hoststr),
1572 ntohs(host->port)));
1575 /* We need to check whether the identity in the host structure
1576 * matches the identity on the connection. If they don't match
1577 * then treat this a new host. */
1578 if (!(host->Console & 1)
1579 && ((!identP->valid && host->interface)
1580 || (identP->valid && !host->interface)
1582 && !afs_uuid_equal(&identP->uuid,
1583 &host->interface->uuid)))) {
1584 char uuid1[128], uuid2[128];
1586 afsUUID_to_string(&identP->uuid, uuid1, 127);
1587 if (host->interface)
1588 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1590 ("CB: new identity for host %x (%s:%d), deleting(%x %x %s %s)\n",
1591 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1592 identP->valid, host->interface,
1593 identP->valid ? uuid1 : "no_uuid",
1594 host->interface ? uuid2 : "no_uuid"));
1596 /* The host in the cache is not the host for this connection */
1598 host->hostFlags |= HOSTDELETED;
1599 host->hostFlags &= ~HWHO_INPROGRESS;
1606 host = h_Alloc_r(tcon); /* returned held and locked */
1607 h_gethostcps_r(host, FT_ApproxTime());
1608 if (!(host->Console & 1)) {
1610 cb_conn = host->callback_rxcon;
1611 rx_GetConnection(cb_conn);
1614 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1615 if (code == RXGEN_OPCODE)
1616 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1617 rx_PutConnection(cb_conn);
1620 if ((code == RXGEN_OPCODE) ||
1621 afs_uuid_equal(&interf.uuid, &nulluuid)) {
1624 (struct Identity *)malloc(sizeof(struct Identity));
1629 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1634 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1636 ("Host %x (%s:%d) does not support WhoAreYou.\n",
1637 host, afs_inet_ntoa_r(host->host, hoststr),
1638 ntohs(host->port)));
1640 } else if (code == 0) {
1643 (struct Identity *)malloc(sizeof(struct Identity));
1648 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1653 identP->uuid = interf.uuid;
1655 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1657 ("WhoAreYou success on host %x (%s:%d)\n",
1658 host, afs_inet_ntoa_r(host->host, hoststr),
1659 ntohs(host->port)));
1661 if (code == 0 && !identP->valid) {
1662 cb_conn = host->callback_rxcon;
1663 rx_GetConnection(cb_conn);
1665 code = RXAFSCB_InitCallBackState(cb_conn);
1666 rx_PutConnection(cb_conn);
1669 } else if (code == 0) {
1670 oldHost = h_LookupUuid_r(&identP->uuid);
1674 if (!h_Held_r(oldHost))
1677 oldHost->hostFlags |= HWHO_INPROGRESS;
1679 if (oldHost->interface) {
1681 afsUUID uuid = oldHost->interface->uuid;
1682 cb_conn = oldHost->callback_rxcon;
1683 rx_GetConnection(cb_conn);
1684 rx_SetConnDeadTime(cb_conn, 2);
1685 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1687 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1689 rx_SetConnDeadTime(cb_conn, 50);
1690 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1691 rx_PutConnection(cb_conn);
1694 /* The primary address is either not responding or
1695 * is not the client we are looking for.
1696 * MultiProbeAlternateAddress_r() will remove the
1697 * alternate interfaces that do not have the same
1699 ViceLog(0,("CB: ProbeUuid for host %x (%s:%d) failed %d\n",
1701 afs_inet_ntoa_r(oldHost->host, hoststr),
1702 ntohs(oldHost->port),code2));
1703 MultiProbeAlternateAddress_r(oldHost);
1710 /* This is a new address for an existing host. Update
1711 * the list of interfaces for the existing host and
1712 * delete the host structure we just allocated. */
1714 /* prevent warnings while manipulating interface lists */
1715 host->hostFlags |= HOSTDELETED;
1717 if (oldHost->host != haddr || oldHost->port != hport) {
1718 struct rx_connection *rxconn;
1721 ("CB: Host %x (%s:%d) has new addr %s:%d\n",
1723 afs_inet_ntoa_r(oldHost->host, hoststr2),
1724 ntohs(oldHost->port),
1725 afs_inet_ntoa_r(haddr, hoststr),
1727 if (probefail || oldHost->host == haddr) {
1728 /* The probe failed which means that the old address is
1729 * either unreachable or is not the same host we were just
1730 * contacted by. We will also remove addresses if only
1731 * the port has changed because that indicates the client
1734 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
1737 struct Interface *interface = oldHost->interface;
1738 int number = oldHost->interface->numberOfInterfaces;
1739 for (i = 0, found = 0; i < number; i++) {
1740 if (interface->interface[i].addr == haddr &&
1741 interface->interface[i].port != hport) {
1747 /* We have just been contacted by a client that has been
1748 * seen from behind a NAT and at least one other address.
1750 removeInterfaceAddr_r(oldHost, haddr, interface->interface[i].port);
1753 addInterfaceAddr_r(oldHost, haddr, hport);
1754 oldHost->host = haddr;
1755 oldHost->port = hport;
1756 rxconn = oldHost->callback_rxcon;
1757 oldHost->callback_rxcon = host->callback_rxcon;
1758 host->callback_rxcon = NULL;
1761 struct client *client;
1763 * If rx_DestroyConnection calls h_FreeConnection we will
1764 * deadlock on the host_glock_mutex. Work around the problem
1765 * by unhooking the client from the connection before
1766 * destroying the connection.
1768 client = rx_GetSpecific(rxconn, rxcon_client_key);
1769 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1770 rx_DestroyConnection(rxconn);
1773 host->hostFlags &= ~HWHO_INPROGRESS;
1775 /* release host because it was allocated by h_Alloc_r */
1778 /* the new host is held and locked */
1780 /* This really is a new host */
1781 h_AddHostToUuidHashTable_r(&identP->uuid, host);
1782 cb_conn = host->callback_rxcon;
1783 rx_GetConnection(cb_conn);
1786 RXAFSCB_InitCallBackState3(cb_conn,
1788 rx_PutConnection(cb_conn);
1793 ("InitCallBackState3 success on host %x (%s:%d)\n",
1794 host, afs_inet_ntoa_r(host->host, hoststr),
1795 ntohs(host->port)));
1796 assert(interfValid == 1);
1797 initInterfaceAddr_r(host, &interf);
1803 ("CB: RCallBackConnectBack failed for %x (%s:%d)\n",
1804 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1805 host->hostFlags |= VENUSDOWN;
1808 ("CB: RCallBackConnectBack succeeded for %x (%s:%d)\n",
1809 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1810 host->hostFlags |= RESETDONE;
1813 if (caps.Capabilities_val
1814 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1815 host->hostFlags |= HERRORTRANS;
1817 host->hostFlags &= ~(HERRORTRANS);
1818 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1819 host->hostFlags &= ~HWHO_INPROGRESS;
1822 if (caps.Capabilities_val)
1823 free(caps.Capabilities_val);
1824 caps.Capabilities_val = NULL;
1825 caps.Capabilities_len = 0;
1831 static char localcellname[PR_MAXNAMELEN + 1];
1832 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1833 int num_lrealms = -1;
1839 memset(&nulluuid, 0, sizeof(afsUUID));
1840 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1841 if (num_lrealms == -1) {
1843 for (i=0; i<AFS_NUM_LREALMS; i++) {
1844 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1850 ("afs_krb_get_lrealm failed, using %s.\n",
1852 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1858 /* initialize the rest of the local realms to nullstring for debugging */
1859 for (; i<AFS_NUM_LREALMS; i++)
1860 local_realms[i][0] = '\0';
1862 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1863 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1864 #ifdef AFS_PTHREAD_ENV
1865 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1866 #endif /* AFS_PTHREAD_ENV */
1870 MapName_r(char *aname, char *acell, afs_int32 * aval)
1875 afs_int32 anamelen, cnamelen;
1879 anamelen = strlen(aname);
1880 if (anamelen >= PR_MAXNAMELEN)
1881 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1883 lnames.namelist_len = 1;
1884 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1885 lids.idlist_len = 0;
1886 lids.idlist_val = NULL;
1888 cnamelen = strlen(acell);
1890 if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
1892 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1893 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1894 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1896 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1898 *aval = AnonymousID;
1901 foreign = 1; /* attempt cross-cell authentication */
1902 tname = (char *)malloc(PR_MAXNAMELEN);
1904 ViceLog(0, ("Failed malloc in MapName_r\n"));
1907 strcpy(tname, aname);
1908 tname[anamelen] = '@';
1909 strcpy(tname + anamelen + 1, acell);
1910 lnames.namelist_val = (prname *) tname;
1915 code = hpr_NameToId(&lnames, &lids);
1918 if (lids.idlist_val) {
1919 *aval = lids.idlist_val[0];
1920 if (*aval == AnonymousID) {
1922 ("MapName: NameToId on %s returns anonymousID\n",
1923 lnames.namelist_val));
1925 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1928 ("MapName: NameToId on '%s' is unknown\n",
1929 lnames.namelist_val));
1935 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1943 /* NOTE: this returns the client with a Write lock and a refCount */
1945 h_ID2Client(afs_int32 vid)
1947 register struct client *client;
1948 register struct host *host;
1951 for (host = hostList; host; host = host->next) {
1952 if (host->hostFlags & HOSTDELETED)
1954 for (client = host->FirstClient; client; client = client->next) {
1955 if (!client->deleted && client->ViceId == vid) {
1958 ObtainWriteLock(&client->lock);
1969 * Called by the server main loop. Returns a h_Held client, which must be
1970 * released later the main loop. Allocates a client if the matching one
1971 * isn't around. The client is returned with its reference count incremented
1972 * by one. The caller must call h_ReleaseClient_r when finished with
1975 * the client->host is returned held. h_ReleaseClient_r does not release
1976 * the hold on client->host.
1979 h_FindClient_r(struct rx_connection *tcon)
1981 register struct client *client;
1982 register struct host *host;
1983 struct client *oldClient;
1988 #if (64-MAXKTCNAMELEN)
1989 ticket name length != 64
1993 char uname[PR_MAXNAMELEN];
1994 char tcell[MAXKTCREALMLEN];
1998 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1999 if (client && client->sid == rxr_CidOf(tcon)
2000 && client->VenusEpoch == rxr_GetEpoch(tcon)) {
2002 h_Hold_r(client->host);
2003 if (!client->deleted && client->prfail != 2) {
2004 /* Could add shared lock on client here */
2005 /* note that we don't have to lock entry in this path to
2006 * ensure CPS is initialized, since we don't call rx_SetSpecific
2007 * until initialization is done, and we only get here if
2008 * rx_GetSpecific located the client structure.
2013 ObtainWriteLock(&client->lock); /* released at end */
2019 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
2021 ("FindClient: authenticating connection: authClass=%d\n",
2023 if (authClass == 1) {
2024 /* A bcrypt tickets, no longer supported */
2025 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2026 viceid = AnonymousID;
2027 expTime = 0x7fffffff;
2028 } else if (authClass == 2) {
2031 /* kerberos ticket */
2032 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
2033 tname, tinst, tcell, &kvno);
2035 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2036 viceid = AnonymousID;
2037 expTime = 0x7fffffff;
2039 int ilen = strlen(tinst);
2041 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2042 tname, tinst, tcell, expTime, kvno));
2043 strncpy(uname, tname, sizeof(uname));
2045 if (strlen(uname) + 1 + ilen >= sizeof(uname))
2048 strcat(uname, tinst);
2050 /* translate the name to a vice id */
2051 code = MapName_r(uname, tcell, &viceid);
2055 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
2058 viceid = AnonymousID;
2059 expTime = 0x7fffffff;
2063 viceid = AnonymousID; /* unknown security class */
2064 expTime = 0x7fffffff;
2067 if (!client) { /* loop */
2068 host = h_GetHost_r(tcon); /* Returns it h_Held */
2074 /* First try to find the client structure */
2075 for (client = host->FirstClient; client; client = client->next) {
2076 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2077 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2080 ObtainWriteLock(&client->lock);
2086 /* Still no client structure - get one */
2089 /* Retry to find the client structure */
2090 for (client = host->FirstClient; client; client = client->next) {
2091 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2092 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2094 goto retryfirstclient;
2099 ObtainWriteLock(&client->lock);
2100 client->refCount = 1;
2101 client->host = host;
2102 #if FS_STATS_DETAILED
2103 client->InSameNetwork = host->InSameNetwork;
2104 #endif /* FS_STATS_DETAILED */
2105 client->ViceId = viceid;
2106 client->expTime = expTime; /* rx only */
2107 client->authClass = authClass; /* rx only */
2108 client->sid = rxr_CidOf(tcon);
2109 client->VenusEpoch = rxr_GetEpoch(tcon);
2110 client->CPS.prlist_val = NULL;
2111 client->CPS.prlist_len = 0;
2115 client->prfail = fail;
2117 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
2118 client->CPS.prlist_len = 0;
2119 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
2120 free(client->CPS.prlist_val);
2121 client->CPS.prlist_val = NULL;
2122 client->ViceId = viceid;
2123 client->expTime = expTime;
2125 if (viceid == ANONYMOUSID) {
2126 client->CPS.prlist_len = AnonCPS.prlist_len;
2127 client->CPS.prlist_val = AnonCPS.prlist_val;
2130 code = hpr_GetCPS(viceid, &client->CPS);
2135 ("pr_GetCPS failed(%d) for user %d, host %x (%s:%d)\n",
2136 code, viceid, client->host,
2137 afs_inet_ntoa_r(client->host->host,hoststr),
2138 ntohs(client->host->port)));
2140 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2141 * all protection servers and reevaluates things if no
2142 * sync server or quorum is found we could still end up
2143 * with one of these errors. In such case we would like to
2144 * reevaluate the rpc call to find if there's cps for this
2145 * guy. We treat other errors (except network failures
2146 * ones - i.e. code < 0) as an indication that there is no
2147 * CPS for this host. Ideally we could like to deal this
2148 * problem the other way around (i.e. if code == NOCPS
2149 * ignore else retry next time) but the problem is that
2150 * there're other errors (i.e. EPERM) for which we don't
2151 * want to retry and we don't know the whole code list!
2153 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2157 /* the disabling of system:administrators is so iffy and has so many
2158 * possible failure modes that we will disable it again */
2159 /* Turn off System:Administrator for safety
2160 * if (AL_IsAMember(SystemId, client->CPS) == 0)
2161 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
2164 /* Now, tcon may already be set to a rock, since we blocked with no host
2165 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2166 * required). So, before setting the RPC's rock, we should disconnect
2167 * the RPC from the other client structure's rock.
2169 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2170 if (oldClient && oldClient != client && oldClient->sid == rxr_CidOf(tcon)
2171 && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
2173 if (!oldClient->deleted) {
2174 /* if we didn't create it, it's not ours to put back */
2176 ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n",
2177 client, client->sid, tcon,
2178 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2179 ntohs(rxr_PortOf(tcon)),
2180 oldClient, oldClient->sid));
2181 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2182 free(client->CPS.prlist_val);
2183 client->CPS.prlist_val = NULL;
2184 client->CPS.prlist_len = 0;
2186 /* We should perhaps check for 0 here */
2188 ReleaseWriteLock(&client->lock);
2193 oldClient->refCount++;
2195 ObtainWriteLock(&oldClient->lock);
2199 ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
2200 oldClient, oldClient->sid, tcon,
2201 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2202 ntohs(rxr_PortOf(tcon)),
2203 client, client->sid));
2204 /* rx_SetSpecific will be done immediately below */
2207 /* Avoid chaining in more than once. */
2210 client->next = host->FirstClient;
2211 host->FirstClient = client;
2213 CurrentConnections++; /* increment number of connections */
2215 rx_SetSpecific(tcon, rxcon_client_key, client);
2216 ReleaseWriteLock(&client->lock);
2220 } /*h_FindClient_r */
2223 h_ReleaseClient_r(struct client *client)
2225 assert(client->refCount > 0);
2232 * Sigh: this one is used to get the client AGAIN within the individual
2233 * server routines. This does not bother h_Holding the host, since
2234 * this is assumed already have been done by the server main loop.
2235 * It does check tokens, since only the server routines can return the
2236 * VICETOKENDEAD error code
2239 GetClient(struct rx_connection *tcon, struct client **cp)
2241 register struct client *client;
2246 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2247 if (client == NULL) {
2249 ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
2250 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2251 ntohs(rxr_PortOf(tcon))));
2255 if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
2257 ("GetClient: tcon %x tcon sid %d client sid %d\n",
2258 tcon, rxr_CidOf(tcon), client->sid));
2262 if (client && client->LastCall > client->expTime && client->expTime) {
2264 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2265 afs_inet_ntoa_r(client->host->host, hoststr),
2266 ntohs(client->host->port), client->expTime));
2268 return VICETOKENDEAD;
2278 PutClient(struct client **cp)
2284 h_ReleaseClient_r(*cp);
2291 /* Client user name for short term use. Note that this is NOT inexpensive */
2293 h_UserName(struct client *client)
2295 static char User[PR_MAXNAMELEN + 1];
2299 lids.idlist_len = 1;
2300 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
2301 if (!lids.idlist_val) {
2302 ViceLog(0, ("Failed malloc in h_UserName\n"));
2305 lnames.namelist_len = 0;
2306 lnames.namelist_val = (prname *) 0;
2307 lids.idlist_val[0] = client->ViceId;
2308 if (hpr_IdToName(&lids, &lnames)) {
2309 /* We need to free id we alloced above! */
2310 free(lids.idlist_val);
2311 return "*UNKNOWN USER NAME*";
2313 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2314 free(lids.idlist_val);
2315 free(lnames.namelist_val);
2324 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2325 CEs, CEBlocks, HTs, HTBlocks));
2331 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
2333 register struct client *client;
2338 time_t LastCall, expTime;
2341 LastCall = host->LastCall;
2342 if (host->hostFlags & HOSTDELETED) {
2346 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2347 "Host %s:%d down = %d, LastCall %s",
2348 afs_inet_ntoa_r(host->host, hoststr),
2349 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2350 afs_ctime(&LastCall, tbuffer,
2352 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2353 for (client = host->FirstClient; client; client = client->next) {
2354 if (!client->deleted) {
2355 expTime = client->expTime;
2356 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2357 " user id=%d, name=%s, sl=%s till %s",
2358 client->ViceId, h_UserName(client),
2360 authClass ? "Authenticated" :
2361 "Not authenticated",
2363 authClass ? afs_ctime(&expTime, tbuffer,
2366 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2367 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2368 client->CPS.prlist_len);
2369 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2370 if (client->CPS.prlist_val) {
2371 for (i = 0; i > client->CPS.prlist_len; i++) {
2372 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2373 client->CPS.prlist_val[i]);
2374 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2377 sprintf(tmpStr, "]\n");
2378 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2384 } /*h_PrintClient */
2389 * Print a list of clients, with last security level and token value seen,
2399 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2403 ("Couldn't create client dump file %s\n",
2404 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2407 now = FT_ApproxTime();
2408 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
2409 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2410 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2411 h_Enumerate(h_PrintClient, (char *)file);
2412 STREAM_REALLYCLOSE(file);
2413 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2420 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
2427 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2428 "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
2429 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
2430 host->cblist, CheckLock(&host->lock), host->LastCall,
2431 host->ActiveCall, (host->hostFlags & VENUSDOWN),
2432 host->hostFlags & HOSTDELETED, host->Console,
2433 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2435 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2436 if (host->hcps.prlist_val)
2437 for (i = 0; i < host->hcps.prlist_len; i++) {
2438 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2439 host->hcps.prlist_val[i]);
2440 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2442 sprintf(tmpStr, "] [");
2443 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2444 if (host->interface)
2445 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2447 sprintf(tmpStr, " %s:%d",
2448 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2449 ntohs(host->interface->interface[i].port));
2450 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2452 sprintf(tmpStr, "] holds: ");
2453 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2455 for (i = 0; i < h_maxSlots; i++) {
2456 sprintf(tmpStr, "%04x", host->holds[i]);
2457 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2459 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
2460 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2472 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2478 ("Couldn't create host dump file %s\n",
2479 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2482 now = FT_ApproxTime();
2483 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
2484 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2485 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2486 h_Enumerate(h_DumpHost, (char *)file);
2487 STREAM_REALLYCLOSE(file);
2488 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2492 #ifdef AFS_DEMAND_ATTACH_FS
2495 * host state serialization
2497 static int h_stateFillHeader(struct host_state_header * hdr);
2498 static int h_stateCheckHeader(struct host_state_header * hdr);
2499 static int h_stateAllocMap(struct fs_dump_state * state);
2500 static int h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state);
2501 static int h_stateRestoreHost(struct fs_dump_state * state);
2502 static int h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state);
2503 static int h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state);
2504 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port);
2505 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2506 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2507 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2510 /* this procedure saves all host state to disk for fast startup */
2512 h_stateSave(struct fs_dump_state * state)
2514 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2517 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2519 /* invalidate host state header */
2520 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2522 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2523 sizeof(struct host_state_header))) {
2528 fs_stateIncEOF(state, sizeof(struct host_state_header));
2530 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2535 h_stateFillHeader(state->h_hdr);
2537 /* write the real header to disk */
2538 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2539 sizeof(struct host_state_header));
2546 * host state serialization
2548 * this procedure restores all host state from a disk for fast startup
2551 h_stateRestore(struct fs_dump_state * state)
2555 /* seek to the right position and read in the host state header */
2556 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2557 sizeof(struct host_state_header))) {
2562 /* check the validity of the header */
2563 if (h_stateCheckHeader(state->h_hdr)) {
2568 records = state->h_hdr->records;
2570 if (h_stateAllocMap(state)) {
2575 /* iterate over records restoring host state */
2576 for (i=0; i < records; i++) {
2577 if (h_stateRestoreHost(state) != 0) {
2588 h_stateRestoreIndices(struct fs_dump_state * state)
2590 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
2595 h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state)
2597 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
2598 return H_ENUMERATE_BAIL(held);
2604 h_stateVerify(struct fs_dump_state * state)
2606 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
2611 h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state)
2616 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
2617 return H_ENUMERATE_BAIL(held);
2621 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
2622 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
2623 h->interface->interface[i].port)) {
2627 if (h_stateVerifyUuidHash(state, h)) {
2630 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port)) {
2634 if (cb_stateVerifyHCBList(state, h)) {
2643 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port)
2645 int ret = 0, found = 0;
2646 struct host *host = NULL;
2647 struct h_AddrHashChain *chain;
2648 int index = h_HashIndex(addr);
2652 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
2653 host = chain->hostPtr;
2655 afs_inet_ntoa_r(addr, tmp);
2656 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
2660 if ((chain->addr == addr) && (chain->port == port)) {
2662 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry points to different host struct (%d, %d)\n",
2663 h->index, host->index));
2664 state->flags.warnings_generated = 1;
2669 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
2670 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2671 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
2679 afs_inet_ntoa_r(addr, tmp);
2680 if (state->mode == FS_STATE_LOAD_MODE) {
2681 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s not found in hash\n", tmp));
2685 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s not found in hash\n", tmp));
2686 state->flags.warnings_generated = 1;
2695 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
2697 int ret = 0, found = 0;
2698 struct host *host = NULL;
2699 struct h_UuidHashChain *chain;
2700 afsUUID * uuidp = &h->interface->uuid;
2701 int index = h_UuidHashIndex(uuidp);
2705 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
2706 host = chain->hostPtr;
2708 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2709 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
2713 if (host->interface &&
2714 afs_uuid_equal(&host->interface->uuid, uuidp)) {
2716 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
2717 h->index, host->index));
2718 state->flags.warnings_generated = 1;
2723 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
2724 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2725 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
2733 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2734 if (state->mode == FS_STATE_LOAD_MODE) {
2735 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
2739 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
2740 state->flags.warnings_generated = 1;
2748 /* create the host state header structure */
2750 h_stateFillHeader(struct host_state_header * hdr)
2752 hdr->stamp.magic = HOST_STATE_MAGIC;
2753 hdr->stamp.version = HOST_STATE_VERSION;
2756 /* check the contents of the host state header structure */
2758 h_stateCheckHeader(struct host_state_header * hdr)
2762 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
2763 ViceLog(0, ("check_host_state_header: invalid state header\n"));
2766 else if (hdr->stamp.version != HOST_STATE_VERSION) {
2767 ViceLog(0, ("check_host_state_header: unknown version number\n"));
2773 /* allocate the host id mapping table */
2775 h_stateAllocMap(struct fs_dump_state * state)
2777 state->h_map.len = state->h_hdr->index_max + 1;
2778 state->h_map.entries = (struct idx_map_entry_t *)
2779 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
2780 return (state->h_map.entries != NULL) ? 0 : 1;
2783 /* function called by h_Enumerate to save a host to disk */
2785 h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state)
2787 int i, if_len=0, hcps_len=0;
2788 struct hostDiskEntry hdsk;
2789 struct host_state_entry_header hdr;
2790 struct Interface * ifp = NULL;
2791 afs_int32 * hcps = NULL;
2792 struct iovec iov[4];
2795 memset(&hdr, 0, sizeof(hdr));
2797 if (state->h_hdr->index_max < host->index) {
2798 state->h_hdr->index_max = host->index;
2801 h_hostToDiskEntry_r(host, &hdsk);
2802 if (host->interface) {
2803 if_len = sizeof(struct Interface) +
2804 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
2805 ifp = (struct Interface *) malloc(if_len);
2806 assert(ifp != NULL);
2807 memcpy(ifp, host->interface, if_len);
2808 hdr.interfaces = host->interface->numberOfInterfaces;
2809 iov[iovcnt].iov_base = (char *) ifp;
2810 iov[iovcnt].iov_len = if_len;
2813 if (host->hcps.prlist_val) {
2814 hdr.hcps = host->hcps.prlist_len;
2815 hcps_len = hdr.hcps * sizeof(afs_int32);
2816 hcps = (afs_int32 *) malloc(hcps_len);
2817 assert(hcps != NULL);
2818 memcpy(hcps, host->hcps.prlist_val, hcps_len);
2819 iov[iovcnt].iov_base = (char *) hcps;
2820 iov[iovcnt].iov_len = hcps_len;
2824 if (hdsk.index > state->h_hdr->index_max)
2825 state->h_hdr->index_max = hdsk.index;
2827 hdr.len = sizeof(struct host_state_entry_header) +
2828 sizeof(struct hostDiskEntry) + if_len + hcps_len;
2829 hdr.magic = HOST_STATE_ENTRY_MAGIC;
2831 iov[0].iov_base = (char *) &hdr;
2832 iov[0].iov_len = sizeof(hdr);
2833 iov[1].iov_base = (char *) &hdsk;
2834 iov[1].iov_len = sizeof(struct hostDiskEntry);
2836 if (fs_stateWriteV(state, iov, iovcnt)) {
2837 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
2841 fs_stateIncEOF(state, hdr.len);
2843 state->h_hdr->records++;
2851 return H_ENUMERATE_BAIL(held);
2856 /* restores a host from disk */
2858 h_stateRestoreHost(struct fs_dump_state * state)
2860 int ifp_len=0, hcps_len=0, bail=0;
2861 struct host_state_entry_header hdr;
2862 struct hostDiskEntry hdsk;
2863 struct host *host = NULL;
2864 struct Interface *ifp = NULL;
2865 afs_int32 * hcps = NULL;
2866 struct iovec iov[3];
2869 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
2870 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
2876 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
2877 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
2883 iov[0].iov_base = (char *) &hdsk;
2884 iov[0].iov_len = sizeof(struct hostDiskEntry);
2886 if (hdr.interfaces) {
2887 ifp_len = sizeof(struct Interface) +
2888 ((hdr.interfaces-1) * sizeof(struct AddrPort));
2889 ifp = (struct Interface *) malloc(ifp_len);
2890 assert(ifp != NULL);
2891 iov[iovcnt].iov_base = (char *) ifp;
2892 iov[iovcnt].iov_len = ifp_len;
2896 hcps_len = hdr.hcps * sizeof(afs_int32);
2897 hcps = (afs_int32 *) malloc(hcps_len);
2898 assert(hcps != NULL);
2899 iov[iovcnt].iov_base = (char *) hcps;
2900 iov[iovcnt].iov_len = hcps_len;
2904 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
2905 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
2910 if (fs_stateReadV(state, iov, iovcnt)) {
2911 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
2916 if (!hdr.hcps && hdsk.hcps_valid) {
2917 /* valid, zero-length host cps ; does this ever happen? */
2918 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
2919 assert(hcps != NULL);
2923 assert(host != NULL);
2926 host->interface = ifp;
2929 host->hcps.prlist_val = hcps;
2930 host->hcps.prlist_len = hdr.hcps;
2933 h_diskEntryToHost_r(&hdsk, host);
2934 h_SetupCallbackConn_r(host);
2938 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
2940 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
2942 h_AddHostToAddrHashTable_r(host->host, host->port, host);
2943 h_InsertList_r(host);
2945 /* setup host id map entry */
2946 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
2947 state->h_map.entries[hdsk.index].new_idx = host->index;
2959 /* serialize a host structure to disk */
2961 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
2963 out->host = in->host;
2964 out->port = in->port;
2965 out->hostFlags = in->hostFlags;
2966 out->Console = in->Console;
2967 out->hcpsfailed = in->hcpsfailed;
2968 out->LastCall = in->LastCall;
2969 out->ActiveCall = in->ActiveCall;
2970 out->cpsCall = in->cpsCall;
2971 out->cblist = in->cblist;
2972 #ifdef FS_STATS_DETAILED
2973 out->InSameNetwork = in->InSameNetwork;
2976 /* special fields we save, but are not memcpy'd back on restore */
2977 out->index = in->index;
2978 out->hcps_len = in->hcps.prlist_len;
2979 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
2982 /* restore a host structure from disk */
2984 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
2986 out->host = in->host;
2987 out->port = in->port;
2988 out->hostFlags = in->hostFlags;
2989 out->Console = in->Console;
2990 out->hcpsfailed = in->hcpsfailed;
2991 out->LastCall = in->LastCall;
2992 out->ActiveCall = in->ActiveCall;
2993 out->cpsCall = in->cpsCall;
2994 out->cblist = in->cblist;
2995 #ifdef FS_STATS_DETAILED
2996 out->InSameNetwork = in->InSameNetwork;
3000 /* index translation routines */
3002 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
3006 /* hosts use a zero-based index, so old==0 is valid */
3008 if (old >= state->h_map.len) {
3009 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
3011 } else if (state->h_map.entries[old].old_idx != old) { /* sanity check */
3012 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
3015 *new = state->h_map.entries[old].new_idx;
3021 #endif /* AFS_DEMAND_ATTACH_FS */
3025 * This counts the number of workstations, the number of active workstations,
3026 * and the number of workstations declared "down" (i.e. not heard from
3027 * recently). An active workstation has received a call since the cutoff
3028 * time argument passed.
3031 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3033 register struct host *host;
3034 register int num = 0, active = 0, del = 0;
3037 for (host = hostList; host; host = host->next) {
3038 if (!(host->hostFlags & HOSTDELETED)) {
3040 if (host->ActiveCall > cutofftime)
3042 if (host->hostFlags & VENUSDOWN)
3054 } /*h_GetWorkStats */
3057 /*------------------------------------------------------------------------
3058 * PRIVATE h_ClassifyAddress
3061 * Given a target IP address and a candidate IP address (both
3062 * in host byte order), classify the candidate into one of three
3063 * buckets in relation to the target by bumping the counters passed
3067 * a_targetAddr : Target address.
3068 * a_candAddr : Candidate address.
3069 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3070 * addresses are either in the same network
3071 * or the same subnet.
3072 * a_diffSubnetP : ...when the candidate is in a different
3074 * a_diffNetworkP : ...when the candidate is in a different
3081 * The target and candidate addresses are both in host byte
3082 * order, NOT network byte order, when passed in.
3086 *------------------------------------------------------------------------*/
3089 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3090 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3091 afs_int32 * a_diffNetworkP)
3092 { /*h_ClassifyAddress */
3094 afs_uint32 targetNet;
3095 afs_uint32 targetSubnet;
3097 afs_uint32 candSubnet;
3100 * Put bad values into the subnet info to start with.
3102 targetSubnet = (afs_uint32) 0;
3103 candSubnet = (afs_uint32) 0;
3106 * Pull out the network and subnetwork numbers from the target
3107 * and candidate addresses. We can short-circuit this whole
3108 * affair if the target and candidate addresses are not of the
3111 if (IN_CLASSA(a_targetAddr)) {
3112 if (!(IN_CLASSA(a_candAddr))) {
3113 (*a_diffNetworkP)++;
3116 targetNet = a_targetAddr & IN_CLASSA_NET;
3117 candNet = a_candAddr & IN_CLASSA_NET;
3118 if (IN_SUBNETA(a_targetAddr))
3119 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
3120 if (IN_SUBNETA(a_candAddr))
3121 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
3122 } else if (IN_CLASSB(a_targetAddr)) {
3123 if (!(IN_CLASSB(a_candAddr))) {
3124 (*a_diffNetworkP)++;
3127 targetNet = a_targetAddr & IN_CLASSB_NET;
3128 candNet = a_candAddr & IN_CLASSB_NET;
3129 if (IN_SUBNETB(a_targetAddr))
3130 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
3131 if (IN_SUBNETB(a_candAddr))
3132 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
3133 } /*Class B target */
3134 else if (IN_CLASSC(a_targetAddr)) {
3135 if (!(IN_CLASSC(a_candAddr))) {
3136 (*a_diffNetworkP)++;
3139 targetNet = a_targetAddr & IN_CLASSC_NET;
3140 candNet = a_candAddr & IN_CLASSC_NET;
3143 * Note that class C addresses can't have subnets,
3144 * so we leave the defaults untouched.
3146 } /*Class C target */
3148 targetNet = a_targetAddr;
3149 candNet = a_candAddr;
3150 } /*Class D address */
3153 * Now, simply compare the extracted net and subnet values for
3154 * the two addresses (which at this point are known to be of the
3157 if (targetNet == candNet) {
3158 if (targetSubnet == candSubnet)
3159 (*a_sameNetOrSubnetP)++;
3163 (*a_diffNetworkP)++;
3165 } /*h_ClassifyAddress */
3168 /*------------------------------------------------------------------------
3169 * EXPORTED h_GetHostNetStats
3172 * Iterate through the host table, and classify each (non-deleted)
3173 * host entry into ``proximity'' categories (same net or subnet,
3174 * different subnet, different network).
3177 * a_numHostsP : Set to total number of (non-deleted) hosts.
3178 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
3179 * a_diffSubnetP : Set to # hosts on diff subnet as server.
3180 * a_diffNetworkP : Set to # hosts on diff network as server.
3186 * We only count non-deleted hosts. The storage pointed to by our
3187 * parameters is zeroed upon entry.
3191 *------------------------------------------------------------------------*/
3194 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
3195 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
3196 { /*h_GetHostNetStats */
3198 register struct host *hostP; /*Ptr to current host entry */
3199 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
3202 * Clear out the storage pointed to by our parameters.
3204 *a_numHostsP = (afs_int32) 0;
3205 *a_sameNetOrSubnetP = (afs_int32) 0;
3206 *a_diffSubnetP = (afs_int32) 0;
3207 *a_diffNetworkP = (afs_int32) 0;
3210 for (hostP = hostList; hostP; hostP = hostP->next) {
3211 if (!(hostP->hostFlags & HOSTDELETED)) {
3213 * Bump the number of undeleted host entries found.
3214 * In classifying the current entry's address, make
3215 * sure to first convert to host byte order.
3218 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
3219 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
3220 a_sameNetOrSubnetP, a_diffSubnetP,
3222 } /*Only look at non-deleted hosts */
3223 } /*For each host record hashed to this index */
3225 } /*h_GetHostNetStats */
3227 static afs_uint32 checktime;
3228 static afs_uint32 clientdeletetime;
3229 static struct AFSFid zerofid;
3233 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
3234 * Since it can serialize them, and pile up, it should be a separate LWP
3235 * from other events.
3238 CheckHost(register struct host *host, int held)
3240 register struct client *client;
3241 struct rx_connection *cb_conn = NULL;
3244 #ifdef AFS_DEMAND_ATTACH_FS
3245 /* kill the checkhost lwp ASAP during shutdown */
3247 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3249 return H_ENUMERATE_BAIL(held);
3254 /* Host is held by h_Enumerate */
3256 for (client = host->FirstClient; client; client = client->next) {
3257 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3258 client->deleted = 1;
3259 host->hostFlags |= CLIENTDELETED;
3262 if (host->LastCall < checktime) {
3264 host->hostFlags |= HWHO_INPROGRESS;
3265 if (!(host->hostFlags & HOSTDELETED)) {
3266 cb_conn = host->callback_rxcon;
3267 rx_GetConnection(cb_conn);
3268 if (host->LastCall < clientdeletetime) {
3269 host->hostFlags |= HOSTDELETED;
3270 if (!(host->hostFlags & VENUSDOWN)) {
3271 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3272 if (host->interface) {
3275 RXAFSCB_InitCallBackState3(cb_conn,
3281 RXAFSCB_InitCallBackState(cb_conn);
3284 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3287 (void)afs_inet_ntoa_r(host->host, hoststr);
3289 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3290 hoststr, ntohs(host->port)));
3291 host->hostFlags |= VENUSDOWN;
3293 /* Note: it's safe to delete hosts even if they have call
3294 * back state, because break delayed callbacks (called when a
3295 * message is received from the workstation) will always send a
3296 * break all call backs to the workstation if there is no
3301 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3303 (void)afs_inet_ntoa_r(host->host, hoststr);
3304 if (host->interface) {
3305 afsUUID uuid = host->interface->uuid;
3307 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3310 if (MultiProbeAlternateAddress_r(host)) {
3311 ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
3312 hoststr, ntohs(host->port), code));
3313 host->hostFlags |= VENUSDOWN;
3318 code = RXAFSCB_Probe(cb_conn);
3322 ("CheckHost: Probe failed for host %s:%d, code %d\n",
3323 hoststr, ntohs(host->port), code));
3324 host->hostFlags |= VENUSDOWN;
3330 rx_PutConnection(cb_conn);
3334 host->hostFlags &= ~HWHO_INPROGRESS;
3343 CheckHost_r(register struct host *host, int held, char *dummy)
3345 register struct client *client;
3346 struct rx_connection *cb_conn = NULL;
3349 #ifdef AFS_DEMAND_ATTACH_FS
3350 /* kill the checkhost lwp ASAP during shutdown */
3352 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3354 return H_ENUMERATE_BAIL(held);
3359 /* Host is held by h_Enumerate_r */
3360 for (client = host->FirstClient; client; client = client->next) {
3361 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3362 client->deleted = 1;
3363 host->hostFlags |= CLIENTDELETED;
3366 if (host->LastCall < checktime) {
3368 if (!(host->hostFlags & HOSTDELETED)) {
3369 cb_conn = host->callback_rxcon;
3370 rx_GetConnection(cb_conn);
3371 if (host->LastCall < clientdeletetime) {
3372 host->hostFlags |= HOSTDELETED;
3373 if (!(host->hostFlags & VENUSDOWN)) {
3374 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3375 if (host->interface) {
3378 RXAFSCB_InitCallBackState3(cb_conn,
3384 RXAFSCB_InitCallBackState(cb_conn);
3387 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3390 (void)afs_inet_ntoa_r(host->host, hoststr);
3392 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3393 hoststr, ntohs(host->port)));
3394 host->hostFlags |= VENUSDOWN;
3396 /* Note: it's safe to delete hosts even if they have call
3397 * back state, because break delayed callbacks (called when a
3398 * message is received from the workstation) will always send a
3399 * break all call backs to the workstation if there is no
3404 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3406 (void)afs_inet_ntoa_r(host->host, hoststr);
3407 if (host->interface) {
3408 afsUUID uuid = host->interface->uuid;
3410 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3413 if (MultiProbeAlternateAddress_r(host)) {
3414 ViceLog(0,("CheckHost_r: Probing all interfaces of host %s:%d failed, code %d\n",
3415 hoststr, ntohs(host->port), code));
3416 host->hostFlags |= VENUSDOWN;
3421 code = RXAFSCB_Probe(cb_conn);
3425 ("CheckHost_r: Probe failed for host %s:%d, code %d\n",
3426 hoststr, ntohs(host->port), code));
3427 host->hostFlags |= VENUSDOWN;
3433 rx_PutConnection(cb_conn);
3445 * Set VenusDown for any hosts that have not had a call in 15 minutes and
3446 * don't respond to a probe. Note that VenusDown can only be cleared if
3447 * a message is received from the host (see ServerLWP in file.c).
3448 * Delete hosts that have not had any calls in 1 hour, clients that
3449 * have not had any calls in 15 minutes.
3451 * This routine is called roughly every 5 minutes.
3456 afs_uint32 now = FT_ApproxTime();
3458 memset((char *)&zerofid, 0, sizeof(zerofid));
3460 * Send a probe to the workstation if it hasn't been heard from in
3463 checktime = now - 15 * 60;
3464 clientdeletetime = now - 120 * 60; /* 2 hours ago */
3467 h_Enumerate_r(CheckHost_r, hostList, NULL);
3472 * This is called with host locked and held. At this point, the
3473 * hostAddrHashTable should not have any entries for the alternate
3474 * interfaces. This function has to insert these entries in the
3475 * hostAddrHashTable.
3477 * The addresses in the interfaceAddr list are in host byte order.
3480 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
3487 struct Interface *interface;
3490 afs_uint16 port7001 = htons(7001);
3495 number = interf->numberOfInterfaces;
3496 myAddr = host->host; /* current interface address */
3497 myPort = host->port; /* current port */
3500 ("initInterfaceAddr : host %s:%d numAddr %d\n",
3501 afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
3503 /* validation checks */
3504 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
3506 ("Invalid number of alternate addresses is %d\n", number));
3511 * The client's notion of its own IP addresses is not reliable.
3513 * 1. The client list might contain private address ranges which
3514 * are likely to be re-used by many clients allocated addresses
3517 * 2. The client list will not include any public addresses that
3518 * are hidden by a NAT.
3520 * 3. Private address ranges that are exposed to the server will
3521 * be obtained from the rx connections that use them.
3523 * 4. Lists provided by the client are not necessarily truthful.
3524 * Many existing clients (UNIX) do not refresh the IP address
3525 * list as the actual assigned addresses change. The end result
3526 * is that they report the initial address list for the lifetime
3527 * of the process. In other words, a client can report addresses
3528 * that they are in fact not using. Adding these addresses to
3529 * the host interface list without verification is not only
3530 * pointless, it is downright dangerous.
3532 * We therefore do not add alternate addresses to the addr hash table.
3533 * We only use them for multi-rx callback breaks.
3537 * Convert IP addresses to network byte order, and remove
3538 * duplicate IP addresses from the interface list, and
3539 * determine whether or not the incoming addr/port is
3540 * listed. Note that if the address matches it is not
3541 * truly a match because the port number for the entries
3542 * in the interface list are port 7001 and the port number
3543 * for this connection might not be 7001.
3545 for (i = 0, count = 0, found = 0; i < number; i++) {
3546 interf->addr_in[i] = htonl(interf->addr_in[i]);
3547 for (j = 0; j < count; j++) {
3548 if (interf->addr_in[j] == interf->addr_in[i])
3552 interf->addr_in[count] = interf->addr_in[i];
3553 if (interf->addr_in[count] == myAddr &&
3561 * Allocate and initialize an interface structure for this host.
3564 interface = (struct Interface *)
3565 malloc(sizeof(struct Interface) +
3566 (sizeof(struct AddrPort) * (count - 1)));
3568 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
3571 interface->numberOfInterfaces = count;
3573 interface = (struct Interface *)
3574 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
3576 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
3579 interface->numberOfInterfaces = count + 1;
3580 interface->interface[count].addr = myAddr;
3581 interface->interface[count].port = myPort;
3582 interface->interface[count].valid = 1;
3585 for (i = 0; i < count; i++) {
3587 interface->interface[i].addr = interf->addr_in[i];
3588 /* We store the port as 7001 because the addresses reported by
3589 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
3590 * are coming from fully connected hosts (no NAT/PATs)
3592 interface->interface[i].port = port7001;
3593 interface->interface[i].valid = 1; /* valid until a conflict is found */
3596 interface->uuid = interf->uuid;
3598 assert(!host->interface);
3599 host->interface = interface;
3601 afsUUID_to_string(&interface->uuid, uuidstr, 127);
3603 ViceLog(125, ("--- uuid %s\n", uuidstr));
3604 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
3605 ViceLog(125, ("--- alt address %s:%d\n",
3606 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3607 ntohs(host->interface->interface[i].port)));
3613 /* deleted a HashChain structure for this address and host */
3614 /* returns 1 on success */
3616 h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
3619 register struct h_AddrHashChain **hp, *th;
3621 for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);) {
3622 assert(th->hostPtr);
3623 if (th->hostPtr == host && th->addr == addr && th->port == port) {
3637 ** prints out all alternate interface address for the host. The 'level'
3638 ** parameter indicates what level of debugging sets this output
3641 printInterfaceAddr(struct host *host, int level)
3646 if (host->interface) {
3647 /* check alternate addresses */
3648 number = host->interface->numberOfInterfaces;
3650 ViceLog(level, ("no-addresses "));
3652 for (i = 0; i < number; i++)
3653 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3654 ntohs(host->interface->interface[i].port)));
3657 ViceLog(level, ("\n"));