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);
387 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
391 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
392 if (code != PRSUCCESS)
395 /* do something about this, probably make a new call */
396 /* don't forget there's a hard limit in the interface */
398 "membership list for host id %d exceeds display limit\n",
403 return pr_GetHostCPS(host, CPS);
408 hpr_NameToId(namelist *names, idlist *ids)
410 #ifdef AFS_PTHREAD_ENV
411 register afs_int32 code;
412 register afs_int32 i;
413 struct ubik_client *uclient =
414 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
417 code = hpr_Initialize(&uclient);
418 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
421 for (i = 0; i < names->namelist_len; i++)
422 stolower(names->namelist_val[i]);
423 code = ubik_PR_NameToID(uclient, 0, names, ids);
426 return pr_NameToId(names, ids);
431 hpr_IdToName(idlist *ids, namelist *names)
433 #ifdef AFS_PTHREAD_ENV
434 register afs_int32 code;
435 struct ubik_client *uclient =
436 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
439 code = hpr_Initialize(&uclient);
440 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
443 code = ubik_PR_IDToName(uclient, 0, ids, names);
446 return pr_IdToName(ids, names);
451 hpr_GetCPS(afs_int32 id, prlist *CPS)
453 #ifdef AFS_PTHREAD_ENV
454 register afs_int32 code;
456 struct ubik_client *uclient =
457 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
460 code = hpr_Initialize(&uclient);
461 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
465 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
466 if (code != PRSUCCESS)
469 /* do something about this, probably make a new call */
470 /* don't forget there's a hard limit in the interface */
471 fprintf(stderr, "membership list for id %d exceeds display limit\n",
476 return pr_GetCPS(id, CPS);
480 static short consolePort = 0;
483 h_Release(register struct host *host)
492 * If this thread does not have a hold on this host AND
493 * if other threads also dont have any holds on this host AND
494 * If either the HOSTDELETED or CLIENTDELETED flags are set
498 h_Release_r(register struct host *host)
501 if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
502 if (!h_OtherHolds_r(host)) {
503 /* must avoid masking this until after h_OtherHolds_r runs
504 * but it should be run before h_TossStuff_r */
505 (host)->holds[h_holdSlot()] &= ~h_holdbit();
506 if ((host->hostFlags & HOSTDELETED)
507 || (host->hostFlags & CLIENTDELETED)) {
511 (host)->holds[h_holdSlot()] &= ~h_holdbit();
513 (host)->holds[h_holdSlot()] &= ~h_holdbit();
519 h_OtherHolds_r(register struct host *host)
521 register int i, bit, slot;
524 for (i = 0; i < h_maxSlots; i++) {
525 if (host->holds[i] != ((i == slot) ? bit : 0)) {
533 h_Lock_r(register struct host *host)
543 * returns 1 if already locked
544 * else returns locks and returns 0
548 h_NBLock_r(register struct host *host)
550 struct Lock *hostLock = &host->lock;
555 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
556 hostLock->excl_locked = WRITE_LOCK;
560 LOCK_UNLOCK(hostLock);
569 #if FS_STATS_DETAILED
570 /*------------------------------------------------------------------------
571 * PRIVATE h_AddrInSameNetwork
574 * Given a target IP address and a candidate IP address (both
575 * in host byte order), return a non-zero value (1) if the
576 * candidate address is in a different network from the target
580 * a_targetAddr : Target address.
581 * a_candAddr : Candidate address.
584 * 1 if the candidate address is in the same net as the target,
588 * The target and candidate addresses are both in host byte
589 * order, NOT network byte order, when passed in. We return
590 * our value as a character, since that's the type of field in
591 * the host structure, where this info will be stored.
595 *------------------------------------------------------------------------*/
598 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
599 { /*h_AddrInSameNetwork */
601 afs_uint32 targetNet;
605 * Pull out the network and subnetwork numbers from the target
606 * and candidate addresses. We can short-circuit this whole
607 * affair if the target and candidate addresses are not of the
610 if (IN_CLASSA(a_targetAddr)) {
611 if (!(IN_CLASSA(a_candAddr))) {
614 targetNet = a_targetAddr & IN_CLASSA_NET;
615 candNet = a_candAddr & IN_CLASSA_NET;
616 } else if (IN_CLASSB(a_targetAddr)) {
617 if (!(IN_CLASSB(a_candAddr))) {
620 targetNet = a_targetAddr & IN_CLASSB_NET;
621 candNet = a_candAddr & IN_CLASSB_NET;
622 } /*Class B target */
623 else if (IN_CLASSC(a_targetAddr)) {
624 if (!(IN_CLASSC(a_candAddr))) {
627 targetNet = a_targetAddr & IN_CLASSC_NET;
628 candNet = a_candAddr & IN_CLASSC_NET;
629 } /*Class C target */
631 targetNet = a_targetAddr;
632 candNet = a_candAddr;
633 } /*Class D address */
636 * Now, simply compare the extracted net values for the two addresses
637 * (which at this point are known to be of the same class)
639 if (targetNet == candNet)
644 } /*h_AddrInSameNetwork */
645 #endif /* FS_STATS_DETAILED */
648 /* Assumptions: called with held host */
650 h_gethostcps_r(register struct host *host, register afs_int32 now)
655 /* wait if somebody else is already doing the getCPS call */
656 while (host->hostFlags & HCPS_INPROGRESS) {
657 slept = 1; /* I did sleep */
658 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
659 #ifdef AFS_PTHREAD_ENV
660 pthread_cond_wait(&host->cond, &host_glock_mutex);
661 #else /* AFS_PTHREAD_ENV */
662 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
663 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
664 #endif /* AFS_PTHREAD_ENV */
668 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
669 if (host->hcps.prlist_val)
670 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
671 host->hcps.prlist_val = NULL;
672 host->hcps.prlist_len = 0;
673 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
676 code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
681 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
682 * and reevaluates things if no sync server or quorum is found we could still end up
683 * with one of these errors. In such case we would like to reevaluate the rpc call to
684 * find if there's cps for this guy. We treat other errors (except network failures
685 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
686 * we could like to deal this problem the other way around (i.e. if code == NOCPS
687 * ignore else retry next time) but the problem is that there're other errors (i.e.
688 * EPERM) for which we don't want to retry and we don't know the whole code list!
690 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
692 * We would have preferred to use a while loop and try again since ops in protected
693 * acls for this host will fail now but they'll be reevaluated on any subsequent
694 * call. The attempt to wait for a quorum/sync site or network error won't work
695 * since this problems really should only occurs during a complete fileserver
696 * restart. Since the fileserver will start before the ptservers (and thus before
697 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
699 host->hcpsfailed = 1;
701 ("Warning: GetHostCPS failed (%d) for %x (%s:%d); will retry\n",
702 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
704 host->hcpsfailed = 0;
706 ("gethost: GetHostCPS failed (%d) for %x (%s:%d); ignored\n",
707 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
709 if (host->hcps.prlist_val)
710 free(host->hcps.prlist_val);
711 host->hcps.prlist_val = NULL;
712 host->hcps.prlist_len = 0; /* Make sure it's zero */
714 host->hcpsfailed = 0;
716 host->hostFlags &= ~HCPS_INPROGRESS;
717 /* signal all who are waiting */
718 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
719 host->hostFlags &= ~HCPS_WAITING;
720 #ifdef AFS_PTHREAD_ENV
721 assert(pthread_cond_broadcast(&host->cond) == 0);
722 #else /* AFS_PTHREAD_ENV */
723 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
724 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
725 #endif /* AFS_PTHREAD_ENV */
729 /* args in net byte order */
731 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
733 register struct host *host;
737 host = h_Lookup_r(hostaddr, hport, &held);
739 host->hcpsfailed = 1;
749 * Allocate a host. It will be identified by the peer (ip,port) info in the
750 * rx connection provided. The host is returned held and locked
752 #define DEF_ROPCONS 2115
755 h_Alloc_r(register struct rx_connection *r_con)
757 struct servent *serverentry;
760 #if FS_STATS_DETAILED
761 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
762 #endif /* FS_STATS_DETAILED */
766 host->host = rxr_HostOf(r_con);
767 host->port = rxr_PortOf(r_con);
769 h_AddHostToAddrHashTable_r(host->host, host->port, host);
771 if (consolePort == 0) { /* find the portal number for console */
772 #if defined(AFS_OSF_ENV)
773 serverentry = getservbyname("ropcons", "");
775 serverentry = getservbyname("ropcons", 0);
778 consolePort = serverentry->s_port;
780 consolePort = htons(DEF_ROPCONS); /* Use a default */
782 if (host->port == consolePort)
784 /* Make a callback channel even for the console, on the off chance that it
785 * makes a request that causes a break call back. It shouldn't. */
786 h_SetupCallbackConn_r(host);
787 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
789 host->hcps.prlist_val = NULL;
790 host->hcps.prlist_len = 0;
791 host->interface = NULL;
793 host->hcpsfailed = 0; /* save cycles */
794 h_gethostcps(host); /* do this under host hold/lock */
796 host->FirstClient = NULL;
799 h_InsertList_r(host); /* update global host List */
800 #if FS_STATS_DETAILED
802 * Compare the new host's IP address (in host byte order) with ours
803 * (the File Server's), remembering if they are in the same network.
805 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
806 host->InSameNetwork =
807 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
808 #endif /* FS_STATS_DETAILED */
815 /* Make a callback channel even for the console, on the off chance that it
816 * makes a request that causes a break call back. It shouldn't. */
818 h_SetupCallbackConn_r(struct host * host)
821 sc = rxnull_NewClientSecurityObject();
822 host->callback_rxcon =
823 rx_NewConnection(host->host, host->port, 1, sc, 0);
824 rx_SetConnDeadTime(host->callback_rxcon, 50);
825 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
828 /* Lookup a host given an IP address and UDP port number. */
829 /* hostaddr and hport are in network order */
830 /* Note: host should be released by caller if 0 == *heldp and non-null */
831 /* hostaddr and hport are in network order */
833 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
836 struct host *host = NULL;
837 struct h_AddrHashChain *chain;
838 int index = h_HashIndex(haddr);
839 extern int hostaclRefresh;
842 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
843 host = chain->hostPtr;
845 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
846 && chain->port == hport) {
847 *heldp = h_Held_r(host);
851 if (host->hostFlags & HOSTDELETED) {
858 now = FT_ApproxTime(); /* always evaluate "now" */
859 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
861 * Every hostaclRefresh period (def 2 hrs) get the new
862 * membership list for the host. Note this could be the
863 * first time that the host is added to a group. Also
864 * here we also retry on previous legitimate hcps failures.
866 * If we get here we still have a host hold.
868 h_gethostcps_r(host, now);
878 /* Lookup a host given its UUID. */
880 h_LookupUuid_r(afsUUID * uuidp)
882 struct host *host = 0;
883 struct h_UuidHashChain *chain;
884 int index = h_UuidHashIndex(uuidp);
886 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
887 host = chain->hostPtr;
889 if (!(host->hostFlags & HOSTDELETED) && host->interface
890 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
901 * h_Hold_r: Establish a hold by the current LWP on this host--the host
902 * or its clients will not be physically deleted until all holds have
904 * NOTE: h_Hold_r is a macro defined in host.h.
907 /* h_TossStuff_r: Toss anything in the host structure (the host or
908 * clients marked for deletion. Called from h_Release_r ONLY.
909 * To be called, there must be no holds, and either host->deleted
910 * or host->clientDeleted must be set.
913 h_TossStuff_r(register struct host *host)
915 register struct client **cp, *client;
918 /* if somebody still has this host held */
919 for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
923 /* if somebody still has this host locked */
924 if (h_NBLock_r(host) != 0) {
927 ("Warning: h_TossStuff_r failed; Host %x (%s:%d) was locked.\n",
928 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
934 /* ASSUMPTION: rxi_FreeConnection() does not yield */
935 for (cp = &host->FirstClient; (client = *cp);) {
936 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
938 ObtainWriteLockNoBlock(&client->lock, code);
942 ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x was locked.\n",
943 host, afs_inet_ntoa_r(host->host, hoststr),
944 ntohs(host->port), client));
948 if (client->refCount) {
951 ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x refcount %d.\n",
952 host, afs_inet_ntoa_r(host->host, hoststr),
953 ntohs(host->port), client, client->refCount));
954 /* This is the same thing we do if the host is locked */
955 ReleaseWriteLock(&client->lock);
958 client->CPS.prlist_len = 0;
959 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
960 free(client->CPS.prlist_val);
961 client->CPS.prlist_val = NULL;
962 CurrentConnections--;
964 ReleaseWriteLock(&client->lock);
970 /* We've just cleaned out all the deleted clients; clear the flag */
971 host->hostFlags &= ~CLIENTDELETED;
973 if (host->hostFlags & HOSTDELETED) {
974 register struct h_AddrHashChain **ahp, *ath;
975 register struct rx_connection *rxconn;
977 struct AddrPort hostAddrPort;
980 if (host->Console & 1)
982 if ((rxconn = host->callback_rxcon)) {
983 host->callback_rxcon = (struct rx_connection *)0;
984 rx_DestroyConnection(rxconn);
986 if (host->hcps.prlist_val)
987 free(host->hcps.prlist_val);
988 host->hcps.prlist_val = NULL;
989 host->hcps.prlist_len = 0;
990 DeleteAllCallBacks_r(host, 1);
991 host->hostFlags &= ~RESETDONE; /* just to be safe */
993 /* if alternate addresses do not exist */
994 if (!(host->interface)) {
995 for (ahp = &hostAddrHashTable[h_HashIndex(host->host)]; (ath = *ahp);
997 assert(ath->hostPtr);
998 if (ath->hostPtr == host) {
1005 register struct h_UuidHashChain **uhp, *uth;
1006 /* delete the hash entry for the UUID */
1007 uuidp = &host->interface->uuid;
1008 for (uhp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (uth = *uhp);
1010 assert(uth->hostPtr);
1011 if (uth->hostPtr == host) {
1017 /* delete the hash entry for each alternate addresses */
1018 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
1019 hostAddrPort = host->interface->interface[i];
1021 if (!hostAddrPort.valid)
1024 for (ahp = &hostAddrHashTable[h_HashIndex(hostAddrPort.addr)]; (ath = *ahp);
1026 assert(ath->hostPtr);
1027 if (ath->hostPtr == host) {
1034 free(host->interface);
1035 host->interface = NULL;
1036 } /* if alternate address exists */
1038 h_DeleteList_r(host); /* remove host from global host List */
1041 } /*h_TossStuff_r */
1043 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
1044 * system at the start of the enumeration (perhaps more). Hosts may be deleted
1045 * (have delete flag set); ditto for clients. (*proc) is always called with
1046 * host h_held(). The hold state of the host with respect to this lwp is passed
1047 * to (*proc) as the param held. The proc should return 0 if the host should be
1048 * released, 1 if it should be held after enumeration.
1051 h_Enumerate(int (*proc) (), char *param)
1053 register struct host *host, **list;
1055 register int i, count;
1058 if (hostCount == 0) {
1062 list = (struct host **)malloc(hostCount * sizeof(struct host *));
1064 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
1067 held = (int *)malloc(hostCount * sizeof(int));
1069 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
1072 for (count = 0, host = hostList; host; host = host->next, count++) {
1074 if (!(held[count] = h_Held_r(host)))
1077 assert(count == hostCount);
1079 for (i = 0; i < count; i++) {
1080 held[i] = (*proc) (list[i], held[i], param);
1081 if (!H_ENUMERATE_ISSET_HELD(held[i]))
1082 h_Release(list[i]); /* this might free up the host */
1083 /* bail out of the enumeration early */
1084 if (H_ENUMERATE_ISSET_BAIL(held[i]))
1091 /* h_Enumerate_r (revised):
1092 * Calls (*proc)(host, held, param) for each host in hostList, starting
1094 * Hosts may be deleted (have delete flag set); ditto for clients.
1095 * (*proc) is always called with
1096 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
1097 * host with respect to this lwp is passed to (*proc) as the param held.
1098 * The proc should return 0 if the host should be released, 1 if it should
1099 * be held after enumeration.
1102 h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
1104 register struct host *host, *next;
1105 register int held, nheld;
1107 if (hostCount == 0) {
1110 if (enumstart && !(held = h_Held_r(enumstart)))
1111 h_Hold_r(enumstart);
1112 for (host = enumstart; host; host = next, held = nheld) {
1114 if (next && !(nheld = h_Held_r(next)) && !H_ENUMERATE_ISSET_BAIL(held))
1116 held = (*proc) (host, held, param);
1117 if (!H_ENUMERATE_ISSET_HELD(held))
1118 h_Release_r(host); /* this might free up the host */
1119 if (H_ENUMERATE_ISSET_BAIL(held)) {
1120 if (!H_ENUMERATE_ISSET_HELD(nheld))
1121 h_Release_r(next); /* this might free up the host */
1125 } /*h_Enumerate_r */
1127 /* inserts a new HashChain structure corresponding to this UUID */
1129 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1132 struct h_UuidHashChain *chain;
1134 /* hash into proper bucket */
1135 index = h_UuidHashIndex(uuid);
1137 /* don't add the same entry multiple times */
1138 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1139 if (host->interface && afs_uuid_equal(&host->interface->uuid, uuid))
1143 /* insert into beginning of list for this bucket */
1144 chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
1146 ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1150 chain->hostPtr = host;
1151 chain->next = hostUuidHashTable[index];
1152 hostUuidHashTable[index] = chain;
1156 /* inserts a new HashChain structure corresponding to this address */
1158 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1161 struct h_AddrHashChain *chain;
1165 /* hash into proper bucket */
1166 index = h_HashIndex(addr);
1168 /* don't add the same entry multiple times */
1169 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1170 if (chain->addr == addr && chain->port == port) {
1171 if (chain->hostPtr == host)
1173 else if (!(host->hostFlags & HOSTDELETED))
1174 ViceLog(125, ("Addr %s:%d assigned to %x and %x.\n",
1175 afs_inet_ntoa_r(addr, hoststr), ntohs(port),
1183 /* insert into beginning of list for this bucket */
1184 chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
1186 ViceLog(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1189 chain->hostPtr = host;
1190 chain->next = hostAddrHashTable[index];
1193 hostAddrHashTable[index] = chain;
1197 * This is called with host locked and held. At this point, the
1198 * hostAddrHashTable should not have entries for the alternate
1199 * interfaces. This function has to insert these entries in the
1200 * hostAddrHashTable.
1202 * All addresses are in network byte order.
1205 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1210 struct Interface *interface;
1211 char hoststr[16], hoststr2[16];
1214 assert(host->interface);
1217 * Make sure this address is on the list of known addresses
1220 number = host->interface->numberOfInterfaces;
1221 for (i = 0, found = 0; i < number && !found; i++) {
1222 if (host->interface->interface[i].addr == addr &&
1223 host->interface->interface[i].port == port) {
1225 host->interface->interface[i].valid = 1;
1229 ViceLog(125, ("addInterfaceAddr : host %s:%d addr %s:%d : found:%d\n",
1230 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1231 afs_inet_ntoa_r(addr, hoststr2), ntohs(port), found));
1234 interface = (struct Interface *)
1235 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1237 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
1240 interface->numberOfInterfaces = number + 1;
1241 interface->uuid = host->interface->uuid;
1242 for (i = 0; i < number; i++)
1243 interface->interface[i] = host->interface->interface[i];
1244 interface->interface[number].addr = addr;
1245 interface->interface[number].port = port;
1246 interface->interface[number].valid = 1;
1247 free(host->interface);
1248 host->interface = interface;
1256 * This is called with host locked and held. At this point, the
1257 * hostAddrHashTable should not be having entries for the alternate
1258 * interfaces. This function has to insert these entries in the
1259 * hostAddrHashTable.
1261 * All addresses are in network byte order.
1264 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1269 struct Interface *interface;
1270 char hoststr[16], hoststr2[16];
1273 assert(host->interface);
1275 ViceLog(125, ("removeInterfaceAddr : host %s:%d addr %s:%d\n",
1276 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1277 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
1280 * Make sure this address is on the list of known addresses
1283 interface = host->interface;
1284 number = host->interface->numberOfInterfaces;
1285 for (i = 0, found = 0; i < number && !found; i++) {
1286 if (interface->interface[i].addr == addr &&
1287 interface->interface[i].port == port) {
1289 interface->interface[i].valid = 0;
1294 for (; i < number; i++) {
1295 interface->interface[i] = interface->interface[i+1];
1297 interface->numberOfInterfaces = number;
1301 * Remove the hash table entry for this address
1303 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1309 * This is called with host locked and held. This function differs
1310 * from removeInterfaceAddr_r in that it is called when the address
1311 * is being removed from the host regardless of whether or not there
1312 * is an interface list for the host. This function will delete the
1313 * host if there are no addresses left on it.
1315 * All addresses are in network byte order.
1318 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1321 char hoststr[16], hoststr2[16];
1323 if (!host->interface) {
1324 if (host->host == addr && host->port == port) {
1326 ("Removing only address for host %x (%s:%d), deleting host.\n",
1327 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1328 host->hostFlags |= HOSTDELETED;
1331 removeInterfaceAddr_r(host, host->host, host->port);
1332 if (host->interface->numberOfInterfaces == 0) {
1334 ("Removed only address for host %x (%s:%d), no alternate interfaces, deleting host.\n",
1335 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1336 host->hostFlags |= HOSTDELETED;
1338 struct rx_connection *rxconn;
1340 rxconn = host->callback_rxcon;
1341 host->callback_rxcon = NULL;
1344 struct client *client;
1346 * If rx_DestroyConnection calls h_FreeConnection we will
1347 * deadlock on the host_glock_mutex. Work around the problem
1348 * by unhooking the client from the connection before
1349 * destroying the connection.
1351 client = rx_GetSpecific(rxconn, rxcon_client_key);
1352 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1353 rx_DestroyConnection(rxconn);
1356 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1357 if (host->interface->interface[i].valid) {
1359 ("Removed address for host %x (%s:%d), new primary interface %s:%d.\n",
1360 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1361 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2),
1362 ntohs(host->interface->interface[i].port)));
1363 host->host = host->interface->interface[i].addr;
1364 host->port = host->interface->interface[i].port;
1365 h_AddHostToAddrHashTable_r(host->host, host->port, host);
1370 if (i == host->interface->numberOfInterfaces) {
1372 ("Removed only address for host %x (%s:%d), no valid alternate interfaces, deleting host.\n",
1373 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1374 host->hostFlags |= HOSTDELETED;
1377 sc = rxnull_NewClientSecurityObject();
1378 host->callback_rxcon =
1379 rx_NewConnection(host->host, host->port, 1, sc, 0);
1380 rx_SetConnDeadTime(host->callback_rxcon, 50);
1381 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
1389 h_threadquota(int waiting)
1394 } else if (lwps > 32) {
1397 } else if (lwps > 16) {
1407 /* Host is returned held */
1409 h_GetHost_r(struct rx_connection *tcon)
1412 struct host *oldHost;
1415 struct interfaceAddr interf;
1416 int interfValid = 0;
1417 struct Identity *identP = NULL;
1420 char hoststr[16], hoststr2[16];
1422 struct rx_connection *cb_conn = NULL;
1424 caps.Capabilities_val = NULL;
1426 haddr = rxr_HostOf(tcon);
1427 hport = rxr_PortOf(tcon);
1429 if (caps.Capabilities_val)
1430 free(caps.Capabilities_val);
1431 caps.Capabilities_val = NULL;
1432 caps.Capabilities_len = 0;
1435 host = h_Lookup_r(haddr, hport, &held);
1436 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1437 if (host && !identP && !(host->Console & 1)) {
1438 /* This is a new connection, and we already have a host
1439 * structure for this address. Verify that the identity
1440 * of the caller matches the identity in the host structure.
1442 if ((host->hostFlags & HWHO_INPROGRESS) &&
1443 h_threadquota(host->lock.num_waiting))
1446 if (!(host->hostFlags & ALTADDR)) {
1447 host->hostFlags &= ~HWHO_INPROGRESS;
1448 /* Another thread is doing initialization */
1453 ("Host %x (%s:%d) starting h_Lookup again\n",
1454 host, afs_inet_ntoa_r(host->host, hoststr),
1455 ntohs(host->port)));
1458 host->hostFlags &= ~ALTADDR;
1459 cb_conn = host->callback_rxcon;
1460 rx_GetConnection(cb_conn);
1463 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1464 if (code == RXGEN_OPCODE)
1465 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1466 rx_PutConnection(cb_conn);
1469 if ((code == RXGEN_OPCODE) ||
1470 (afs_uuid_equal(&interf.uuid, &nulluuid))) {
1471 identP = (struct Identity *)malloc(sizeof(struct Identity));
1473 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1477 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1478 /* The host on this connection was unable to respond to
1479 * the WhoAreYou. We will treat this as a new connection
1480 * from the existing host. The worst that can happen is
1481 * that we maintain some extra callback state information */
1482 if (host->interface) {
1484 ("Host %x (%s:%d) used to support WhoAreYou, deleting.\n",
1486 afs_inet_ntoa_r(host->host, hoststr),
1487 ntohs(host->port)));
1488 host->hostFlags |= HOSTDELETED;
1489 host->hostFlags &= ~HWHO_INPROGRESS;
1496 } else if (code == 0) {
1498 identP = (struct Identity *)malloc(sizeof(struct Identity));
1500 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1504 identP->uuid = interf.uuid;
1505 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1506 /* Check whether the UUID on this connection matches
1507 * the UUID in the host structure. If they don't match
1508 * then this is not the same host as before. */
1509 if (!host->interface
1510 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1512 ("Uuid doesn't match host %x (%s:%d).\n",
1513 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1515 removeAddress_r(host, host->host, host->port);
1516 host->hostFlags &= ~HWHO_INPROGRESS;
1525 ("CB: WhoAreYou failed for host %x (%s:%d), error %d\n",
1526 host, afs_inet_ntoa_r(host->host, hoststr),
1527 ntohs(host->port), code));
1528 host->hostFlags |= VENUSDOWN;
1530 if (caps.Capabilities_val
1531 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1532 host->hostFlags |= HERRORTRANS;
1534 host->hostFlags &= ~(HERRORTRANS);
1535 host->hostFlags |= ALTADDR;
1536 host->hostFlags &= ~HWHO_INPROGRESS;
1539 if (!(host->hostFlags & ALTADDR)) {
1540 /* another thread is doing the initialisation */
1542 ("Host %x (%s:%d) waiting for host-init to complete\n",
1543 host, afs_inet_ntoa_r(host->host, hoststr),
1544 ntohs(host->port)));
1546 host->hostFlags &= ~HWHO_INPROGRESS;
1551 ("Host %x (%s:%d) starting h_Lookup again\n",
1552 host, afs_inet_ntoa_r(host->host, hoststr),
1553 ntohs(host->port)));
1556 /* We need to check whether the identity in the host structure
1557 * matches the identity on the connection. If they don't match
1558 * then treat this a new host. */
1559 if (!(host->Console & 1)
1560 && ((!identP->valid && host->interface)
1561 || (identP->valid && !host->interface)
1563 && !afs_uuid_equal(&identP->uuid,
1564 &host->interface->uuid)))) {
1565 char uuid1[128], uuid2[128];
1567 afsUUID_to_string(&identP->uuid, uuid1, 127);
1568 if (host->interface)
1569 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1571 ("CB: new identity for host %x (%s:%d), deleting(%x %x %s %s)\n",
1572 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1573 identP->valid, host->interface,
1574 identP->valid ? uuid1 : "no_uuid",
1575 host->interface ? uuid2 : "no_uuid"));
1577 /* The host in the cache is not the host for this connection */
1578 host->hostFlags |= HOSTDELETED;
1579 host->hostFlags &= ~HWHO_INPROGRESS;
1586 host = h_Alloc_r(tcon); /* returned held and locked */
1587 h_gethostcps_r(host, FT_ApproxTime());
1588 if (!(host->Console & 1)) {
1590 cb_conn = host->callback_rxcon;
1591 rx_GetConnection(cb_conn);
1594 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1595 if (code == RXGEN_OPCODE)
1596 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1597 rx_PutConnection(cb_conn);
1600 if ((code == RXGEN_OPCODE) ||
1601 afs_uuid_equal(&interf.uuid, &nulluuid)) {
1604 (struct Identity *)malloc(sizeof(struct Identity));
1609 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1614 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1616 ("Host %x (%s:%d) does not support WhoAreYou.\n",
1617 host, afs_inet_ntoa_r(host->host, hoststr),
1618 ntohs(host->port)));
1620 } else if (code == 0) {
1623 (struct Identity *)malloc(sizeof(struct Identity));
1628 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1633 identP->uuid = interf.uuid;
1635 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1637 ("WhoAreYou success on host %x (%s:%d)\n",
1638 host, afs_inet_ntoa_r(host->host, hoststr),
1639 ntohs(host->port)));
1641 if (code == 0 && !identP->valid) {
1642 cb_conn = host->callback_rxcon;
1643 rx_GetConnection(cb_conn);
1645 code = RXAFSCB_InitCallBackState(cb_conn);
1646 rx_PutConnection(cb_conn);
1649 } else if (code == 0) {
1650 oldHost = h_LookupUuid_r(&identP->uuid);
1654 if (!h_Held_r(oldHost))
1657 oldHost->hostFlags |= HWHO_INPROGRESS;
1659 if (oldHost->interface) {
1661 afsUUID uuid = oldHost->interface->uuid;
1662 cb_conn = oldHost->callback_rxcon;
1663 rx_GetConnection(cb_conn);
1664 rx_SetConnDeadTime(cb_conn, 2);
1665 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1667 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1669 rx_SetConnDeadTime(cb_conn, 50);
1670 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1671 rx_PutConnection(cb_conn);
1674 /* The primary address is either not responding or
1675 * is not the client we are looking for.
1676 * MultiProbeAlternateAddress_r() will remove the
1677 * alternate interfaces that do not have the same
1679 ViceLog(0,("CB: ProbeUuid for host %x (%s:%d) failed %d\n",
1681 afs_inet_ntoa_r(oldHost->host, hoststr),
1682 ntohs(oldHost->port),code2));
1683 MultiProbeAlternateAddress_r(oldHost);
1690 /* This is a new address for an existing host. Update
1691 * the list of interfaces for the existing host and
1692 * delete the host structure we just allocated. */
1694 /* prevent warnings while manipulating interface lists */
1695 host->hostFlags |= HOSTDELETED;
1697 if (oldHost->host != haddr || oldHost->port != hport) {
1698 struct rx_connection *rxconn;
1701 ("CB: Host %x (%s:%d) has new addr %s:%d\n",
1703 afs_inet_ntoa_r(oldHost->host, hoststr2),
1704 ntohs(oldHost->port),
1705 afs_inet_ntoa_r(haddr, hoststr),
1707 if (probefail || oldHost->host == haddr) {
1708 /* The probe failed which means that the old address is
1709 * either unreachable or is not the same host we were just
1710 * contacted by. We will also remove addresses if only
1711 * the port has changed because that indicates the client
1714 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
1717 struct Interface *interface = oldHost->interface;
1718 int number = oldHost->interface->numberOfInterfaces;
1719 for (i = 0, found = 0; i < number; i++) {
1720 if (interface->interface[i].addr == haddr &&
1721 interface->interface[i].port != hport) {
1727 /* We have just been contacted by a client that has been
1728 * seen from behind a NAT and at least one other address.
1730 removeInterfaceAddr_r(oldHost, haddr, interface->interface[i].port);
1733 addInterfaceAddr_r(oldHost, haddr, hport);
1734 oldHost->host = haddr;
1735 oldHost->port = hport;
1736 rxconn = oldHost->callback_rxcon;
1737 oldHost->callback_rxcon = host->callback_rxcon;
1738 host->callback_rxcon = NULL;
1741 struct client *client;
1743 * If rx_DestroyConnection calls h_FreeConnection we will
1744 * deadlock on the host_glock_mutex. Work around the problem
1745 * by unhooking the client from the connection before
1746 * destroying the connection.
1748 client = rx_GetSpecific(rxconn, rxcon_client_key);
1749 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1750 rx_DestroyConnection(rxconn);
1753 host->hostFlags &= ~HWHO_INPROGRESS;
1755 /* release host because it was allocated by h_Alloc_r */
1758 /* the new host is held and locked */
1760 /* This really is a new host */
1761 h_AddHostToUuidHashTable_r(&identP->uuid, host);
1762 cb_conn = host->callback_rxcon;
1763 rx_GetConnection(cb_conn);
1766 RXAFSCB_InitCallBackState3(cb_conn,
1768 rx_PutConnection(cb_conn);
1772 ("InitCallBackState3 success on host %x (%s:%d)\n",
1773 host, afs_inet_ntoa_r(host->host, hoststr),
1774 ntohs(host->port)));
1775 assert(interfValid == 1);
1776 initInterfaceAddr_r(host, &interf);
1782 ("CB: RCallBackConnectBack failed for %x (%s:%d)\n",
1783 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1784 host->hostFlags |= VENUSDOWN;
1787 ("CB: RCallBackConnectBack succeeded for %x (%s:%d)\n",
1788 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1789 host->hostFlags |= RESETDONE;
1792 if (caps.Capabilities_val
1793 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1794 host->hostFlags |= HERRORTRANS;
1796 host->hostFlags &= ~(HERRORTRANS);
1797 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1798 host->hostFlags &= ~HWHO_INPROGRESS;
1801 if (caps.Capabilities_val)
1802 free(caps.Capabilities_val);
1803 caps.Capabilities_val = NULL;
1804 caps.Capabilities_len = 0;
1810 static char localcellname[PR_MAXNAMELEN + 1];
1811 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1812 int num_lrealms = -1;
1818 memset(&nulluuid, 0, sizeof(afsUUID));
1819 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1820 if (num_lrealms == -1) {
1822 for (i=0; i<AFS_NUM_LREALMS; i++) {
1823 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1829 ("afs_krb_get_lrealm failed, using %s.\n",
1831 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1837 /* initialize the rest of the local realms to nullstring for debugging */
1838 for (; i<AFS_NUM_LREALMS; i++)
1839 local_realms[i][0] = '\0';
1841 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1842 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1843 #ifdef AFS_PTHREAD_ENV
1844 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1845 #endif /* AFS_PTHREAD_ENV */
1849 MapName_r(char *aname, char *acell, afs_int32 * aval)
1854 afs_int32 anamelen, cnamelen;
1858 anamelen = strlen(aname);
1859 if (anamelen >= PR_MAXNAMELEN)
1860 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1862 lnames.namelist_len = 1;
1863 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1864 lids.idlist_len = 0;
1865 lids.idlist_val = NULL;
1867 cnamelen = strlen(acell);
1869 if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
1871 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1872 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1873 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1875 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1877 *aval = AnonymousID;
1880 foreign = 1; /* attempt cross-cell authentication */
1881 tname = (char *)malloc(PR_MAXNAMELEN);
1883 ViceLog(0, ("Failed malloc in MapName_r\n"));
1886 strcpy(tname, aname);
1887 tname[anamelen] = '@';
1888 strcpy(tname + anamelen + 1, acell);
1889 lnames.namelist_val = (prname *) tname;
1894 code = hpr_NameToId(&lnames, &lids);
1897 if (lids.idlist_val) {
1898 *aval = lids.idlist_val[0];
1899 if (*aval == AnonymousID) {
1901 ("MapName: NameToId on %s returns anonymousID\n",
1902 lnames.namelist_val));
1904 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1907 ("MapName: NameToId on '%s' is unknown\n",
1908 lnames.namelist_val));
1914 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1922 /* NOTE: this returns the client with a Write lock and a refCount */
1924 h_ID2Client(afs_int32 vid)
1926 register struct client *client;
1927 register struct host *host;
1930 for (host = hostList; host; host = host->next) {
1931 if (host->hostFlags & HOSTDELETED)
1933 for (client = host->FirstClient; client; client = client->next) {
1934 if (!client->deleted && client->ViceId == vid) {
1937 ObtainWriteLock(&client->lock);
1948 * Called by the server main loop. Returns a h_Held client, which must be
1949 * released later the main loop. Allocates a client if the matching one
1950 * isn't around. The client is returned with its reference count incremented
1951 * by one. The caller must call h_ReleaseClient_r when finished with
1954 * the client->host is returned held. h_ReleaseClient_r does not release
1955 * the hold on client->host.
1958 h_FindClient_r(struct rx_connection *tcon)
1960 register struct client *client;
1961 register struct host *host;
1962 struct client *oldClient;
1967 #if (64-MAXKTCNAMELEN)
1968 ticket name length != 64
1972 char uname[PR_MAXNAMELEN];
1973 char tcell[MAXKTCREALMLEN];
1977 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1978 if (client && client->sid == rxr_CidOf(tcon)
1979 && client->VenusEpoch == rxr_GetEpoch(tcon)) {
1981 h_Hold_r(client->host);
1982 if (!client->deleted && client->prfail != 2) {
1983 /* Could add shared lock on client here */
1984 /* note that we don't have to lock entry in this path to
1985 * ensure CPS is initialized, since we don't call rx_SetSpecific
1986 * until initialization is done, and we only get here if
1987 * rx_GetSpecific located the client structure.
1992 ObtainWriteLock(&client->lock); /* released at end */
1998 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
2000 ("FindClient: authenticating connection: authClass=%d\n",
2002 if (authClass == 1) {
2003 /* A bcrypt tickets, no longer supported */
2004 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2005 viceid = AnonymousID;
2006 expTime = 0x7fffffff;
2007 } else if (authClass == 2) {
2010 /* kerberos ticket */
2011 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
2012 tname, tinst, tcell, &kvno);
2014 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2015 viceid = AnonymousID;
2016 expTime = 0x7fffffff;
2018 int ilen = strlen(tinst);
2020 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2021 tname, tinst, tcell, expTime, kvno));
2022 strncpy(uname, tname, sizeof(uname));
2024 if (strlen(uname) + 1 + ilen >= sizeof(uname))
2027 strcat(uname, tinst);
2029 /* translate the name to a vice id */
2030 code = MapName_r(uname, tcell, &viceid);
2034 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
2037 viceid = AnonymousID;
2038 expTime = 0x7fffffff;
2042 viceid = AnonymousID; /* unknown security class */
2043 expTime = 0x7fffffff;
2046 if (!client) { /* loop */
2047 host = h_GetHost_r(tcon); /* Returns it h_Held */
2053 /* First try to find the client structure */
2054 for (client = host->FirstClient; client; client = client->next) {
2055 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2056 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2059 ObtainWriteLock(&client->lock);
2065 /* Still no client structure - get one */
2068 /* Retry to find the client structure */
2069 for (client = host->FirstClient; client; client = client->next) {
2070 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2071 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2073 goto retryfirstclient;
2078 ObtainWriteLock(&client->lock);
2079 client->refCount = 1;
2080 client->host = host;
2081 #if FS_STATS_DETAILED
2082 client->InSameNetwork = host->InSameNetwork;
2083 #endif /* FS_STATS_DETAILED */
2084 client->ViceId = viceid;
2085 client->expTime = expTime; /* rx only */
2086 client->authClass = authClass; /* rx only */
2087 client->sid = rxr_CidOf(tcon);
2088 client->VenusEpoch = rxr_GetEpoch(tcon);
2089 client->CPS.prlist_val = NULL;
2090 client->CPS.prlist_len = 0;
2094 client->prfail = fail;
2096 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
2097 client->CPS.prlist_len = 0;
2098 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
2099 free(client->CPS.prlist_val);
2100 client->CPS.prlist_val = NULL;
2101 client->ViceId = viceid;
2102 client->expTime = expTime;
2104 if (viceid == ANONYMOUSID) {
2105 client->CPS.prlist_len = AnonCPS.prlist_len;
2106 client->CPS.prlist_val = AnonCPS.prlist_val;
2109 code = hpr_GetCPS(viceid, &client->CPS);
2114 ("pr_GetCPS failed(%d) for user %d, host %x (%s:%d)\n",
2115 code, viceid, client->host,
2116 afs_inet_ntoa_r(client->host->host,hoststr),
2117 ntohs(client->host->port)));
2119 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2120 * all protection servers and reevaluates things if no
2121 * sync server or quorum is found we could still end up
2122 * with one of these errors. In such case we would like to
2123 * reevaluate the rpc call to find if there's cps for this
2124 * guy. We treat other errors (except network failures
2125 * ones - i.e. code < 0) as an indication that there is no
2126 * CPS for this host. Ideally we could like to deal this
2127 * problem the other way around (i.e. if code == NOCPS
2128 * ignore else retry next time) but the problem is that
2129 * there're other errors (i.e. EPERM) for which we don't
2130 * want to retry and we don't know the whole code list!
2132 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2136 /* the disabling of system:administrators is so iffy and has so many
2137 * possible failure modes that we will disable it again */
2138 /* Turn off System:Administrator for safety
2139 * if (AL_IsAMember(SystemId, client->CPS) == 0)
2140 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
2143 /* Now, tcon may already be set to a rock, since we blocked with no host
2144 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2145 * required). So, before setting the RPC's rock, we should disconnect
2146 * the RPC from the other client structure's rock.
2148 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2149 if (oldClient && oldClient != client && oldClient->sid == rxr_CidOf(tcon)
2150 && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
2152 if (!oldClient->deleted) {
2153 /* if we didn't create it, it's not ours to put back */
2155 ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n",
2156 client, client->sid, tcon,
2157 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2158 ntohs(rxr_PortOf(tcon)),
2159 oldClient, oldClient->sid));
2160 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2161 free(client->CPS.prlist_val);
2162 client->CPS.prlist_val = NULL;
2163 client->CPS.prlist_len = 0;
2165 /* We should perhaps check for 0 here */
2167 ReleaseWriteLock(&client->lock);
2172 oldClient->refCount++;
2174 ObtainWriteLock(&oldClient->lock);
2178 ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
2179 oldClient, oldClient->sid, tcon,
2180 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2181 ntohs(rxr_PortOf(tcon)),
2182 client, client->sid));
2183 /* rx_SetSpecific will be done immediately below */
2186 /* Avoid chaining in more than once. */
2189 client->next = host->FirstClient;
2190 host->FirstClient = client;
2192 CurrentConnections++; /* increment number of connections */
2194 rx_SetSpecific(tcon, rxcon_client_key, client);
2195 ReleaseWriteLock(&client->lock);
2199 } /*h_FindClient_r */
2202 h_ReleaseClient_r(struct client *client)
2204 assert(client->refCount > 0);
2211 * Sigh: this one is used to get the client AGAIN within the individual
2212 * server routines. This does not bother h_Holding the host, since
2213 * this is assumed already have been done by the server main loop.
2214 * It does check tokens, since only the server routines can return the
2215 * VICETOKENDEAD error code
2218 GetClient(struct rx_connection *tcon, struct client **cp)
2220 register struct client *client;
2225 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2226 if (client == NULL) {
2228 ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
2229 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2230 ntohs(rxr_PortOf(tcon))));
2234 if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
2236 ("GetClient: tcon %x tcon sid %d client sid %d\n",
2237 tcon, rxr_CidOf(tcon), client->sid));
2241 if (client && client->LastCall > client->expTime && client->expTime) {
2243 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2244 afs_inet_ntoa_r(client->host->host, hoststr),
2245 ntohs(client->host->port), client->expTime));
2247 return VICETOKENDEAD;
2257 PutClient(struct client **cp)
2263 h_ReleaseClient_r(*cp);
2270 /* Client user name for short term use. Note that this is NOT inexpensive */
2272 h_UserName(struct client *client)
2274 static char User[PR_MAXNAMELEN + 1];
2278 lids.idlist_len = 1;
2279 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
2280 if (!lids.idlist_val) {
2281 ViceLog(0, ("Failed malloc in h_UserName\n"));
2284 lnames.namelist_len = 0;
2285 lnames.namelist_val = (prname *) 0;
2286 lids.idlist_val[0] = client->ViceId;
2287 if (hpr_IdToName(&lids, &lnames)) {
2288 /* We need to free id we alloced above! */
2289 free(lids.idlist_val);
2290 return "*UNKNOWN USER NAME*";
2292 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2293 free(lids.idlist_val);
2294 free(lnames.namelist_val);
2303 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2304 CEs, CEBlocks, HTs, HTBlocks));
2310 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
2312 register struct client *client;
2317 time_t LastCall, expTime;
2320 LastCall = host->LastCall;
2321 if (host->hostFlags & HOSTDELETED) {
2325 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2326 "Host %s:%d down = %d, LastCall %s",
2327 afs_inet_ntoa_r(host->host, hoststr),
2328 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2329 afs_ctime(&LastCall, tbuffer,
2331 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2332 for (client = host->FirstClient; client; client = client->next) {
2333 if (!client->deleted) {
2334 expTime = client->expTime;
2335 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2336 " user id=%d, name=%s, sl=%s till %s",
2337 client->ViceId, h_UserName(client),
2339 authClass ? "Authenticated" :
2340 "Not authenticated",
2342 authClass ? afs_ctime(&expTime, tbuffer,
2345 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2346 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2347 client->CPS.prlist_len);
2348 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2349 if (client->CPS.prlist_val) {
2350 for (i = 0; i > client->CPS.prlist_len; i++) {
2351 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2352 client->CPS.prlist_val[i]);
2353 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2356 sprintf(tmpStr, "]\n");
2357 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2363 } /*h_PrintClient */
2368 * Print a list of clients, with last security level and token value seen,
2378 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2382 ("Couldn't create client dump file %s\n",
2383 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2386 now = FT_ApproxTime();
2387 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
2388 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2389 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2390 h_Enumerate(h_PrintClient, (char *)file);
2391 STREAM_REALLYCLOSE(file);
2392 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2399 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
2406 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2407 "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 [",
2408 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
2409 host->cblist, CheckLock(&host->lock), host->LastCall,
2410 host->ActiveCall, (host->hostFlags & VENUSDOWN),
2411 host->hostFlags & HOSTDELETED, host->Console,
2412 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2414 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2415 if (host->hcps.prlist_val)
2416 for (i = 0; i < host->hcps.prlist_len; i++) {
2417 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2418 host->hcps.prlist_val[i]);
2419 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2421 sprintf(tmpStr, "] [");
2422 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2423 if (host->interface)
2424 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2426 sprintf(tmpStr, " %s:%d",
2427 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2428 ntohs(host->interface->interface[i].port));
2429 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2431 sprintf(tmpStr, "] holds: ");
2432 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2434 for (i = 0; i < h_maxSlots; i++) {
2435 sprintf(tmpStr, "%04x", host->holds[i]);
2436 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2438 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
2439 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2451 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2457 ("Couldn't create host dump file %s\n",
2458 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2461 now = FT_ApproxTime();
2462 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
2463 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2464 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2465 h_Enumerate(h_DumpHost, (char *)file);
2466 STREAM_REALLYCLOSE(file);
2467 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2471 #ifdef AFS_DEMAND_ATTACH_FS
2474 * host state serialization
2476 static int h_stateFillHeader(struct host_state_header * hdr);
2477 static int h_stateCheckHeader(struct host_state_header * hdr);
2478 static int h_stateAllocMap(struct fs_dump_state * state);
2479 static int h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state);
2480 static int h_stateRestoreHost(struct fs_dump_state * state);
2481 static int h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state);
2482 static int h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state);
2483 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port);
2484 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2485 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2486 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2489 /* this procedure saves all host state to disk for fast startup */
2491 h_stateSave(struct fs_dump_state * state)
2493 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2496 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2498 /* invalidate host state header */
2499 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2501 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2502 sizeof(struct host_state_header))) {
2507 fs_stateIncEOF(state, sizeof(struct host_state_header));
2509 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2514 h_stateFillHeader(state->h_hdr);
2516 /* write the real header to disk */
2517 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2518 sizeof(struct host_state_header));
2525 * host state serialization
2527 * this procedure restores all host state from a disk for fast startup
2530 h_stateRestore(struct fs_dump_state * state)
2534 /* seek to the right position and read in the host state header */
2535 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2536 sizeof(struct host_state_header))) {
2541 /* check the validity of the header */
2542 if (h_stateCheckHeader(state->h_hdr)) {
2547 records = state->h_hdr->records;
2549 if (h_stateAllocMap(state)) {
2554 /* iterate over records restoring host state */
2555 for (i=0; i < records; i++) {
2556 if (h_stateRestoreHost(state) != 0) {
2567 h_stateRestoreIndices(struct fs_dump_state * state)
2569 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
2574 h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state)
2576 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
2577 return H_ENUMERATE_BAIL(held);
2583 h_stateVerify(struct fs_dump_state * state)
2585 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
2590 h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state)
2595 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
2596 return H_ENUMERATE_BAIL(held);
2600 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
2601 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
2602 h->interface->interface[i].port)) {
2606 if (h_stateVerifyUuidHash(state, h)) {
2609 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port)) {
2613 if (cb_stateVerifyHCBList(state, h)) {
2622 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port)
2624 int ret = 0, found = 0;
2625 struct host *host = NULL;
2626 struct h_AddrHashChain *chain;
2627 int index = h_HashIndex(addr);
2631 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
2632 host = chain->hostPtr;
2634 afs_inet_ntoa_r(addr, tmp);
2635 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
2639 if ((chain->addr == addr) && (chain->port == port)) {
2641 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry points to different host struct (%d, %d)\n",
2642 h->index, host->index));
2643 state->flags.warnings_generated = 1;
2648 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
2649 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2650 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
2658 afs_inet_ntoa_r(addr, tmp);
2659 if (state->mode == FS_STATE_LOAD_MODE) {
2660 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s not found in hash\n", tmp));
2664 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s not found in hash\n", tmp));
2665 state->flags.warnings_generated = 1;
2674 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
2676 int ret = 0, found = 0;
2677 struct host *host = NULL;
2678 struct h_UuidHashChain *chain;
2679 afsUUID * uuidp = &h->interface->uuid;
2680 int index = h_UuidHashIndex(uuidp);
2684 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
2685 host = chain->hostPtr;
2687 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2688 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
2692 if (host->interface &&
2693 afs_uuid_equal(&host->interface->uuid, uuidp)) {
2695 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
2696 h->index, host->index));
2697 state->flags.warnings_generated = 1;
2702 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
2703 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2704 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
2712 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2713 if (state->mode == FS_STATE_LOAD_MODE) {
2714 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
2718 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
2719 state->flags.warnings_generated = 1;
2727 /* create the host state header structure */
2729 h_stateFillHeader(struct host_state_header * hdr)
2731 hdr->stamp.magic = HOST_STATE_MAGIC;
2732 hdr->stamp.version = HOST_STATE_VERSION;
2735 /* check the contents of the host state header structure */
2737 h_stateCheckHeader(struct host_state_header * hdr)
2741 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
2742 ViceLog(0, ("check_host_state_header: invalid state header\n"));
2745 else if (hdr->stamp.version != HOST_STATE_VERSION) {
2746 ViceLog(0, ("check_host_state_header: unknown version number\n"));
2752 /* allocate the host id mapping table */
2754 h_stateAllocMap(struct fs_dump_state * state)
2756 state->h_map.len = state->h_hdr->index_max + 1;
2757 state->h_map.entries = (struct idx_map_entry_t *)
2758 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
2759 return (state->h_map.entries != NULL) ? 0 : 1;
2762 /* function called by h_Enumerate to save a host to disk */
2764 h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state)
2766 int i, if_len=0, hcps_len=0;
2767 struct hostDiskEntry hdsk;
2768 struct host_state_entry_header hdr;
2769 struct Interface * ifp = NULL;
2770 afs_int32 * hcps = NULL;
2771 struct iovec iov[4];
2774 memset(&hdr, 0, sizeof(hdr));
2776 if (state->h_hdr->index_max < host->index) {
2777 state->h_hdr->index_max = host->index;
2780 h_hostToDiskEntry_r(host, &hdsk);
2781 if (host->interface) {
2782 if_len = sizeof(struct Interface) +
2783 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
2784 ifp = (struct Interface *) malloc(if_len);
2785 assert(ifp != NULL);
2786 memcpy(ifp, host->interface, if_len);
2787 hdr.interfaces = host->interface->numberOfInterfaces;
2788 iov[iovcnt].iov_base = (char *) ifp;
2789 iov[iovcnt].iov_len = if_len;
2792 if (host->hcps.prlist_val) {
2793 hdr.hcps = host->hcps.prlist_len;
2794 hcps_len = hdr.hcps * sizeof(afs_int32);
2795 hcps = (afs_int32 *) malloc(hcps_len);
2796 assert(hcps != NULL);
2797 memcpy(hcps, host->hcps.prlist_val, hcps_len);
2798 iov[iovcnt].iov_base = (char *) hcps;
2799 iov[iovcnt].iov_len = hcps_len;
2803 if (hdsk.index > state->h_hdr->index_max)
2804 state->h_hdr->index_max = hdsk.index;
2806 hdr.len = sizeof(struct host_state_entry_header) +
2807 sizeof(struct hostDiskEntry) + if_len + hcps_len;
2808 hdr.magic = HOST_STATE_ENTRY_MAGIC;
2810 iov[0].iov_base = (char *) &hdr;
2811 iov[0].iov_len = sizeof(hdr);
2812 iov[1].iov_base = (char *) &hdsk;
2813 iov[1].iov_len = sizeof(struct hostDiskEntry);
2815 if (fs_stateWriteV(state, iov, iovcnt)) {
2816 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
2820 fs_stateIncEOF(state, hdr.len);
2822 state->h_hdr->records++;
2830 return H_ENUMERATE_BAIL(held);
2835 /* restores a host from disk */
2837 h_stateRestoreHost(struct fs_dump_state * state)
2839 int ifp_len=0, hcps_len=0, bail=0;
2840 struct host_state_entry_header hdr;
2841 struct hostDiskEntry hdsk;
2842 struct host *host = NULL;
2843 struct Interface *ifp = NULL;
2844 afs_int32 * hcps = NULL;
2845 struct iovec iov[3];
2848 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
2849 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
2855 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
2856 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
2862 iov[0].iov_base = (char *) &hdsk;
2863 iov[0].iov_len = sizeof(struct hostDiskEntry);
2865 if (hdr.interfaces) {
2866 ifp_len = sizeof(struct Interface) +
2867 ((hdr.interfaces-1) * sizeof(struct AddrPort));
2868 ifp = (struct Interface *) malloc(ifp_len);
2869 assert(ifp != NULL);
2870 iov[iovcnt].iov_base = (char *) ifp;
2871 iov[iovcnt].iov_len = ifp_len;
2875 hcps_len = hdr.hcps * sizeof(afs_int32);
2876 hcps = (afs_int32 *) malloc(hcps_len);
2877 assert(hcps != NULL);
2878 iov[iovcnt].iov_base = (char *) hcps;
2879 iov[iovcnt].iov_len = hcps_len;
2883 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
2884 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
2889 if (fs_stateReadV(state, iov, iovcnt)) {
2890 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
2895 if (!hdr.hcps && hdsk.hcps_valid) {
2896 /* valid, zero-length host cps ; does this ever happen? */
2897 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
2898 assert(hcps != NULL);
2902 assert(host != NULL);
2905 host->interface = ifp;
2908 host->hcps.prlist_val = hcps;
2909 host->hcps.prlist_len = hdr.hcps;
2912 h_diskEntryToHost_r(&hdsk, host);
2913 h_SetupCallbackConn_r(host);
2917 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
2919 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
2921 h_AddHostToAddrHashTable_r(host->host, host->port, host);
2922 h_InsertList_r(host);
2924 /* setup host id map entry */
2925 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
2926 state->h_map.entries[hdsk.index].new_idx = host->index;
2938 /* serialize a host structure to disk */
2940 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
2942 out->host = in->host;
2943 out->port = in->port;
2944 out->hostFlags = in->hostFlags;
2945 out->Console = in->Console;
2946 out->hcpsfailed = in->hcpsfailed;
2947 out->LastCall = in->LastCall;
2948 out->ActiveCall = in->ActiveCall;
2949 out->cpsCall = in->cpsCall;
2950 out->cblist = in->cblist;
2951 #ifdef FS_STATS_DETAILED
2952 out->InSameNetwork = in->InSameNetwork;
2955 /* special fields we save, but are not memcpy'd back on restore */
2956 out->index = in->index;
2957 out->hcps_len = in->hcps.prlist_len;
2958 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
2961 /* restore a host structure from disk */
2963 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
2965 out->host = in->host;
2966 out->port = in->port;
2967 out->hostFlags = in->hostFlags;
2968 out->Console = in->Console;
2969 out->hcpsfailed = in->hcpsfailed;
2970 out->LastCall = in->LastCall;
2971 out->ActiveCall = in->ActiveCall;
2972 out->cpsCall = in->cpsCall;
2973 out->cblist = in->cblist;
2974 #ifdef FS_STATS_DETAILED
2975 out->InSameNetwork = in->InSameNetwork;
2979 /* index translation routines */
2981 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2985 /* hosts use a zero-based index, so old==0 is valid */
2987 if (old >= state->h_map.len) {
2988 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
2990 } else if (state->h_map.entries[old].old_idx != old) { /* sanity check */
2991 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
2994 *new = state->h_map.entries[old].new_idx;
3000 #endif /* AFS_DEMAND_ATTACH_FS */
3004 * This counts the number of workstations, the number of active workstations,
3005 * and the number of workstations declared "down" (i.e. not heard from
3006 * recently). An active workstation has received a call since the cutoff
3007 * time argument passed.
3010 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3012 register struct host *host;
3013 register int num = 0, active = 0, del = 0;
3016 for (host = hostList; host; host = host->next) {
3017 if (!(host->hostFlags & HOSTDELETED)) {
3019 if (host->ActiveCall > cutofftime)
3021 if (host->hostFlags & VENUSDOWN)
3033 } /*h_GetWorkStats */
3036 /*------------------------------------------------------------------------
3037 * PRIVATE h_ClassifyAddress
3040 * Given a target IP address and a candidate IP address (both
3041 * in host byte order), classify the candidate into one of three
3042 * buckets in relation to the target by bumping the counters passed
3046 * a_targetAddr : Target address.
3047 * a_candAddr : Candidate address.
3048 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3049 * addresses are either in the same network
3050 * or the same subnet.
3051 * a_diffSubnetP : ...when the candidate is in a different
3053 * a_diffNetworkP : ...when the candidate is in a different
3060 * The target and candidate addresses are both in host byte
3061 * order, NOT network byte order, when passed in.
3065 *------------------------------------------------------------------------*/
3068 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3069 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3070 afs_int32 * a_diffNetworkP)
3071 { /*h_ClassifyAddress */
3073 afs_uint32 targetNet;
3074 afs_uint32 targetSubnet;
3076 afs_uint32 candSubnet;
3079 * Put bad values into the subnet info to start with.
3081 targetSubnet = (afs_uint32) 0;
3082 candSubnet = (afs_uint32) 0;
3085 * Pull out the network and subnetwork numbers from the target
3086 * and candidate addresses. We can short-circuit this whole
3087 * affair if the target and candidate addresses are not of the
3090 if (IN_CLASSA(a_targetAddr)) {
3091 if (!(IN_CLASSA(a_candAddr))) {
3092 (*a_diffNetworkP)++;
3095 targetNet = a_targetAddr & IN_CLASSA_NET;
3096 candNet = a_candAddr & IN_CLASSA_NET;
3097 if (IN_SUBNETA(a_targetAddr))
3098 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
3099 if (IN_SUBNETA(a_candAddr))
3100 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
3101 } else if (IN_CLASSB(a_targetAddr)) {
3102 if (!(IN_CLASSB(a_candAddr))) {
3103 (*a_diffNetworkP)++;
3106 targetNet = a_targetAddr & IN_CLASSB_NET;
3107 candNet = a_candAddr & IN_CLASSB_NET;
3108 if (IN_SUBNETB(a_targetAddr))
3109 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
3110 if (IN_SUBNETB(a_candAddr))
3111 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
3112 } /*Class B target */
3113 else if (IN_CLASSC(a_targetAddr)) {
3114 if (!(IN_CLASSC(a_candAddr))) {
3115 (*a_diffNetworkP)++;
3118 targetNet = a_targetAddr & IN_CLASSC_NET;
3119 candNet = a_candAddr & IN_CLASSC_NET;
3122 * Note that class C addresses can't have subnets,
3123 * so we leave the defaults untouched.
3125 } /*Class C target */
3127 targetNet = a_targetAddr;
3128 candNet = a_candAddr;
3129 } /*Class D address */
3132 * Now, simply compare the extracted net and subnet values for
3133 * the two addresses (which at this point are known to be of the
3136 if (targetNet == candNet) {
3137 if (targetSubnet == candSubnet)
3138 (*a_sameNetOrSubnetP)++;
3142 (*a_diffNetworkP)++;
3144 } /*h_ClassifyAddress */
3147 /*------------------------------------------------------------------------
3148 * EXPORTED h_GetHostNetStats
3151 * Iterate through the host table, and classify each (non-deleted)
3152 * host entry into ``proximity'' categories (same net or subnet,
3153 * different subnet, different network).
3156 * a_numHostsP : Set to total number of (non-deleted) hosts.
3157 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
3158 * a_diffSubnetP : Set to # hosts on diff subnet as server.
3159 * a_diffNetworkP : Set to # hosts on diff network as server.
3165 * We only count non-deleted hosts. The storage pointed to by our
3166 * parameters is zeroed upon entry.
3170 *------------------------------------------------------------------------*/
3173 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
3174 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
3175 { /*h_GetHostNetStats */
3177 register struct host *hostP; /*Ptr to current host entry */
3178 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
3181 * Clear out the storage pointed to by our parameters.
3183 *a_numHostsP = (afs_int32) 0;
3184 *a_sameNetOrSubnetP = (afs_int32) 0;
3185 *a_diffSubnetP = (afs_int32) 0;
3186 *a_diffNetworkP = (afs_int32) 0;
3189 for (hostP = hostList; hostP; hostP = hostP->next) {
3190 if (!(hostP->hostFlags & HOSTDELETED)) {
3192 * Bump the number of undeleted host entries found.
3193 * In classifying the current entry's address, make
3194 * sure to first convert to host byte order.
3197 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
3198 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
3199 a_sameNetOrSubnetP, a_diffSubnetP,
3201 } /*Only look at non-deleted hosts */
3202 } /*For each host record hashed to this index */
3204 } /*h_GetHostNetStats */
3206 static afs_uint32 checktime;
3207 static afs_uint32 clientdeletetime;
3208 static struct AFSFid zerofid;
3212 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
3213 * Since it can serialize them, and pile up, it should be a separate LWP
3214 * from other events.
3217 CheckHost(register struct host *host, int held)
3219 register struct client *client;
3220 struct rx_connection *cb_conn = NULL;
3223 #ifdef AFS_DEMAND_ATTACH_FS
3224 /* kill the checkhost lwp ASAP during shutdown */
3226 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3228 return H_ENUMERATE_BAIL(held);
3233 /* Host is held by h_Enumerate */
3235 for (client = host->FirstClient; client; client = client->next) {
3236 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3237 client->deleted = 1;
3238 host->hostFlags |= CLIENTDELETED;
3241 if (host->LastCall < checktime) {
3243 host->hostFlags |= HWHO_INPROGRESS;
3244 if (!(host->hostFlags & HOSTDELETED)) {
3245 cb_conn = host->callback_rxcon;
3246 rx_GetConnection(cb_conn);
3247 if (host->LastCall < clientdeletetime) {
3248 host->hostFlags |= HOSTDELETED;
3249 if (!(host->hostFlags & VENUSDOWN)) {
3250 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3251 if (host->interface) {
3254 RXAFSCB_InitCallBackState3(cb_conn,
3260 RXAFSCB_InitCallBackState(cb_conn);
3263 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3266 (void)afs_inet_ntoa_r(host->host, hoststr);
3268 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3269 hoststr, ntohs(host->port)));
3270 host->hostFlags |= VENUSDOWN;
3272 /* Note: it's safe to delete hosts even if they have call
3273 * back state, because break delayed callbacks (called when a
3274 * message is received from the workstation) will always send a
3275 * break all call backs to the workstation if there is no
3280 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3282 (void)afs_inet_ntoa_r(host->host, hoststr);
3283 if (host->interface) {
3284 afsUUID uuid = host->interface->uuid;
3286 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3289 if (MultiProbeAlternateAddress_r(host)) {
3290 ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
3291 hoststr, ntohs(host->port), code));
3292 host->hostFlags |= VENUSDOWN;
3297 code = RXAFSCB_Probe(cb_conn);
3301 ("CheckHost: Probe failed for host %s:%d, code %d\n",
3302 hoststr, ntohs(host->port), code));
3303 host->hostFlags |= VENUSDOWN;
3309 rx_PutConnection(cb_conn);
3313 host->hostFlags &= ~HWHO_INPROGRESS;
3322 CheckHost_r(register struct host *host, int held, char *dummy)
3324 register struct client *client;
3325 struct rx_connection *cb_conn = NULL;
3328 /* Host is held by h_Enumerate_r */
3329 for (client = host->FirstClient; client; client = client->next) {
3330 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3331 client->deleted = 1;
3332 host->hostFlags |= CLIENTDELETED;
3335 if (host->LastCall < checktime) {
3337 if (!(host->hostFlags & HOSTDELETED)) {
3338 cb_conn = host->callback_rxcon;
3339 rx_GetConnection(cb_conn);
3340 if (host->LastCall < clientdeletetime) {
3341 host->hostFlags |= HOSTDELETED;
3342 if (!(host->hostFlags & VENUSDOWN)) {
3343 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3344 if (host->interface) {
3347 RXAFSCB_InitCallBackState3(cb_conn,
3353 RXAFSCB_InitCallBackState(cb_conn);
3356 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3359 (void)afs_inet_ntoa_r(host->host, hoststr);
3361 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3362 hoststr, ntohs(host->port)));
3363 host->hostFlags |= VENUSDOWN;
3365 /* Note: it's safe to delete hosts even if they have call
3366 * back state, because break delayed callbacks (called when a
3367 * message is received from the workstation) will always send a
3368 * break all call backs to the workstation if there is no
3373 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3375 (void)afs_inet_ntoa_r(host->host, hoststr);
3376 if (host->interface) {
3377 afsUUID uuid = host->interface->uuid;
3379 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3382 if (MultiProbeAlternateAddress_r(host)) {
3383 ViceLog(0,("CheckHost_r: Probing all interfaces of host %s:%d failed, code %d\n",
3384 hoststr, ntohs(host->port), code));
3385 host->hostFlags |= VENUSDOWN;
3390 code = RXAFSCB_Probe(cb_conn);
3394 ("CheckHost_r: Probe failed for host %s:%d, code %d\n",
3395 hoststr, ntohs(host->port), code));
3396 host->hostFlags |= VENUSDOWN;
3402 rx_PutConnection(cb_conn);
3414 * Set VenusDown for any hosts that have not had a call in 15 minutes and
3415 * don't respond to a probe. Note that VenusDown can only be cleared if
3416 * a message is received from the host (see ServerLWP in file.c).
3417 * Delete hosts that have not had any calls in 1 hour, clients that
3418 * have not had any calls in 15 minutes.
3420 * This routine is called roughly every 5 minutes.
3425 afs_uint32 now = FT_ApproxTime();
3427 memset((char *)&zerofid, 0, sizeof(zerofid));
3429 * Send a probe to the workstation if it hasn't been heard from in
3432 checktime = now - 15 * 60;
3433 clientdeletetime = now - 120 * 60; /* 2 hours ago */
3436 h_Enumerate_r(CheckHost_r, hostList, NULL);
3441 * This is called with host locked and held. At this point, the
3442 * hostAddrHashTable should not have any entries for the alternate
3443 * interfaces. This function has to insert these entries in the
3444 * hostAddrHashTable.
3446 * The addresses in the interfaceAddr list are in host byte order.
3449 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
3456 struct Interface *interface;
3459 afs_uint16 port7001 = htons(7001);
3464 number = interf->numberOfInterfaces;
3465 myAddr = host->host; /* current interface address */
3466 myPort = host->port; /* current port */
3469 ("initInterfaceAddr : host %s:%d numAddr %d\n",
3470 afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
3472 /* validation checks */
3473 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
3475 ("Invalid number of alternate addresses is %d\n", number));
3480 * The client's notion of its own IP addresses is not reliable.
3482 * 1. The client list might contain private address ranges which
3483 * are likely to be re-used by many clients allocated addresses
3486 * 2. The client list will not include any public addresses that
3487 * are hidden by a NAT.
3489 * 3. Private address ranges that are exposed to the server will
3490 * be obtained from the rx connections that use them.
3492 * 4. Lists provided by the client are not necessarily truthful.
3493 * Many existing clients (UNIX) do not refresh the IP address
3494 * list as the actual assigned addresses change. The end result
3495 * is that they report the initial address list for the lifetime
3496 * of the process. In other words, a client can report addresses
3497 * that they are in fact not using. Adding these addresses to
3498 * the host interface list without verification is not only
3499 * pointless, it is downright dangerous.
3501 * We therefore do not add alternate addresses to the addr hash table.
3502 * We only use them for multi-rx callback breaks.
3506 * Convert IP addresses to network byte order, and remove
3507 * duplicate IP addresses from the interface list, and
3508 * determine whether or not the incoming addr/port is
3509 * listed. Note that if the address matches it is not
3510 * truly a match because the port number for the entries
3511 * in the interface list are port 7001 and the port number
3512 * for this connection might not be 7001.
3514 for (i = 0, count = 0, found = 0; i < number; i++) {
3515 interf->addr_in[i] = htonl(interf->addr_in[i]);
3516 for (j = 0; j < count; j++) {
3517 if (interf->addr_in[j] == interf->addr_in[i])
3521 interf->addr_in[count] = interf->addr_in[i];
3522 if (interf->addr_in[count] == myAddr &&
3530 * Allocate and initialize an interface structure for this host.
3533 interface = (struct Interface *)
3534 malloc(sizeof(struct Interface) +
3535 (sizeof(struct AddrPort) * (count - 1)));
3537 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
3540 interface->numberOfInterfaces = count;
3542 interface = (struct Interface *)
3543 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
3545 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
3548 interface->numberOfInterfaces = count + 1;
3549 interface->interface[count].addr = myAddr;
3550 interface->interface[count].port = myPort;
3551 interface->interface[count].valid = 1;
3554 for (i = 0; i < count; i++) {
3556 interface->interface[i].addr = interf->addr_in[i];
3557 /* We store the port as 7001 because the addresses reported by
3558 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
3559 * are coming from fully connected hosts (no NAT/PATs)
3561 interface->interface[i].port = port7001;
3562 interface->interface[i].valid = 1; /* valid until a conflict is found */
3565 interface->uuid = interf->uuid;
3567 assert(!host->interface);
3568 host->interface = interface;
3570 afsUUID_to_string(&interface->uuid, uuidstr, 127);
3572 ViceLog(125, ("--- uuid %s\n", uuidstr));
3573 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
3574 ViceLog(125, ("--- alt address %s:%d\n",
3575 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3576 ntohs(host->interface->interface[i].port)));
3582 /* deleted a HashChain structure for this address and host */
3583 /* returns 1 on success */
3585 h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
3588 register struct h_AddrHashChain **hp, *th;
3590 for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);) {
3591 assert(th->hostPtr);
3592 if (th->hostPtr == host && th->addr == addr && th->port == port) {
3606 ** prints out all alternate interface address for the host. The 'level'
3607 ** parameter indicates what level of debugging sets this output
3610 printInterfaceAddr(struct host *host, int level)
3615 if (host->interface) {
3616 /* check alternate addresses */
3617 number = host->interface->numberOfInterfaces;
3619 ViceLog(level, ("no-addresses "));
3621 for (i = 0; i < number; i++)
3622 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3623 ntohs(host->interface->interface[i].port)));
3626 ViceLog(level, ("\n"));