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>
26 #include <netinet/in.h>
39 #include <afs/assert.h>
42 #include <afs/afsint.h>
43 #include <afs/rxgen_consts.h>
45 #include <afs/errors.h>
46 #include <afs/ihandle.h>
47 #include <afs/vnode.h>
48 #include <afs/volume.h>
49 #ifdef AFS_ATHENA_STDENV
53 #include <afs/ptclient.h>
54 #include <afs/ptuser.h>
55 #include <afs/prs_fs.h>
57 #include <afs/afsutil.h>
58 #include <afs/com_err.h>
60 #include <afs/cellconfig.h>
62 #include "viced_prototypes.h"
66 #ifdef AFS_DEMAND_ATTACH_FS
67 #include "../util/afsutil_prototypes.h"
68 #include "../tviced/serialize_state.h"
69 #endif /* AFS_DEMAND_ATTACH_FS */
71 #ifdef AFS_PTHREAD_ENV
72 pthread_mutex_t host_glock_mutex;
73 #endif /* AFS_PTHREAD_ENV */
76 extern int CurrentConnections;
78 extern int AnonymousID;
79 extern prlist AnonCPS;
81 extern struct afsconf_dir *confDir; /* config dir object */
82 extern int lwps; /* the max number of server threads */
83 extern afsUUID FS_HostUUID;
86 int CEs = 0; /* active clients */
87 int CEBlocks = 0; /* number of blocks of CEs */
88 struct client *CEFree = 0; /* first free client */
89 struct host *hostList = 0; /* linked list of all hosts */
90 int hostCount = 0; /* number of hosts in hostList */
94 static struct rx_securityClass *sc = NULL;
96 static void h_SetupCallbackConn_r(struct host * host);
98 #define CESPERBLOCK 73
99 struct CEBlock { /* block of CESPERBLOCK file entries */
100 struct client entry[CESPERBLOCK];
103 static void h_TossStuff_r(register struct host *host);
106 * Make sure the subnet macros have been defined.
109 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
112 #ifndef IN_CLASSA_SUBNET
113 #define IN_CLASSA_SUBNET 0xffff0000
117 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
120 #ifndef IN_CLASSB_SUBNET
121 #define IN_CLASSB_SUBNET 0xffffff00
125 /* get a new block of CEs and chain it on CEFree */
129 register struct CEBlock *block;
132 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
134 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
135 ShutDownAndCore(PANIC);
138 for (i = 0; i < (CESPERBLOCK - 1); i++) {
139 Lock_Init(&block->entry[i].lock);
140 block->entry[i].next = &(block->entry[i + 1]);
142 block->entry[CESPERBLOCK - 1].next = 0;
143 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
144 CEFree = (struct client *)block;
150 /* get the next available CE */
151 static struct client *
154 register struct client *entry;
159 ViceLog(0, ("CEFree NULL in GetCE\n"));
160 ShutDownAndCore(PANIC);
164 CEFree = entry->next;
166 memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
172 /* return an entry to the free list */
174 FreeCE(register struct client *entry)
176 entry->VenusEpoch = 0;
178 entry->next = CEFree;
185 * The HTs and HTBlocks variables were formerly static, but they are
186 * now referenced elsewhere in the FileServer.
188 int HTs = 0; /* active file entries */
189 int HTBlocks = 0; /* number of blocks of HTs */
190 static struct host *HTFree = 0; /* first free file entry */
193 * Hash tables of host pointers. We need two tables, one
194 * to map IP addresses onto host pointers, and another
195 * to map host UUIDs onto host pointers.
197 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
198 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
199 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
200 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
202 struct HTBlock { /* block of HTSPERBLOCK file entries */
203 struct host entry[h_HTSPERBLOCK];
207 /* get a new block of HTs and chain it on HTFree */
211 register struct HTBlock *block;
213 static int index = 0;
215 if (HTBlocks == h_MAXHOSTTABLES) {
216 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
217 ShutDownAndCore(PANIC);
220 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
222 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
223 ShutDownAndCore(PANIC);
225 #ifdef AFS_PTHREAD_ENV
226 for (i = 0; i < (h_HTSPERBLOCK); i++)
227 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
228 #endif /* AFS_PTHREAD_ENV */
229 for (i = 0; i < (h_HTSPERBLOCK); i++)
230 Lock_Init(&block->entry[i].lock);
231 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
232 block->entry[i].next = &(block->entry[i + 1]);
233 for (i = 0; i < (h_HTSPERBLOCK); i++)
234 block->entry[i].index = index++;
235 block->entry[h_HTSPERBLOCK - 1].next = 0;
236 HTFree = (struct host *)block;
237 hosttableptrs[HTBlocks++] = block->entry;
242 /* get the next available HT */
246 register struct host *entry;
250 assert(HTFree != NULL);
252 HTFree = entry->next;
254 memset((char *)entry, 0, HOST_TO_ZERO(entry));
260 /* return an entry to the free list */
262 FreeHT(register struct host *entry)
264 entry->next = HTFree;
271 hpr_Initialize(struct ubik_client **uclient)
274 struct rx_connection *serverconns[MAXSERVERS];
275 struct rx_securityClass *sc[3];
276 struct afsconf_dir *tdir;
277 char tconfDir[100] = "";
279 struct ktc_token ttoken;
281 struct afsconf_cell info;
285 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
287 ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
291 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
293 ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]", code));
298 code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
300 ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s", cellstr, confDir, AFSDIR_CELLSERVDB_FILE));
307 ViceLog(0, ("hpr_Initialize: Could not initialize rx."));
316 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
317 * to force use of the KeyFile. secLevel == 0 implies -noauth was
319 if ((afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
320 code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
322 ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s (so trying noauth)", code, afs_error_message(code)));
324 scIndex = 0; /* use noauth */
326 /* if there was a problem, an unauthenticated conn is returned */
329 struct ktc_principal sname;
330 strcpy(sname.cell, info.name);
331 sname.instance[0] = 0;
332 strcpy(sname.name, "afs");
333 code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
337 if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
338 /* this is a kerberos ticket, set scIndex accordingly */
341 ViceLog(0, ("hpr_Initialize: funny kvno (%d) in ticket, proceeding", ttoken.kvno));
345 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
346 ttoken.kvno, ttoken.ticketLen,
350 if ((scIndex == 0) && (sc[0] == 0))
351 sc[0] = rxnull_NewClientSecurityObject();
353 ViceLog(0, ("hpr_Initialize: Could not get afs tokens, running unauthenticated. [%d]", code));
355 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
356 for (i = 0; i < info.numServers; i++) {
358 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
359 info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
363 code = ubik_ClientInit(serverconns, uclient);
365 ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
368 code = rxs_Release(sc[scIndex]);
373 hpr_End(struct ubik_client *uclient)
378 code = ubik_ClientDestroy(uclient);
384 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
386 #ifdef AFS_PTHREAD_ENV
387 register afs_int32 code;
389 struct ubik_client *uclient =
390 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
393 code = hpr_Initialize(&uclient);
394 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
398 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
399 if (code != PRSUCCESS)
402 /* do something about this, probably make a new call */
403 /* don't forget there's a hard limit in the interface */
405 "membership list for host id %d exceeds display limit\n",
410 return pr_GetHostCPS(host, CPS);
415 hpr_NameToId(namelist *names, idlist *ids)
417 #ifdef AFS_PTHREAD_ENV
418 register afs_int32 code;
419 register afs_int32 i;
420 struct ubik_client *uclient =
421 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
424 code = hpr_Initialize(&uclient);
425 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
428 for (i = 0; i < names->namelist_len; i++)
429 stolower(names->namelist_val[i]);
430 code = ubik_PR_NameToID(uclient, 0, names, ids);
433 return pr_NameToId(names, ids);
438 hpr_IdToName(idlist *ids, namelist *names)
440 #ifdef AFS_PTHREAD_ENV
441 register afs_int32 code;
442 struct ubik_client *uclient =
443 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
446 code = hpr_Initialize(&uclient);
447 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
450 code = ubik_PR_IDToName(uclient, 0, ids, names);
453 return pr_IdToName(ids, names);
458 hpr_GetCPS(afs_int32 id, prlist *CPS)
460 #ifdef AFS_PTHREAD_ENV
461 register afs_int32 code;
463 struct ubik_client *uclient =
464 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
467 code = hpr_Initialize(&uclient);
468 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
472 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
473 if (code != PRSUCCESS)
476 /* do something about this, probably make a new call */
477 /* don't forget there's a hard limit in the interface */
478 fprintf(stderr, "membership list for id %d exceeds display limit\n",
483 return pr_GetCPS(id, CPS);
487 static short consolePort = 0;
490 h_Release(register struct host *host)
499 * If this thread does not have a hold on this host AND
500 * if other threads also dont have any holds on this host AND
501 * If either the HOSTDELETED or CLIENTDELETED flags are set
505 h_Release_r(register struct host *host)
508 if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
509 if (!h_OtherHolds_r(host)) {
510 /* must avoid masking this until after h_OtherHolds_r runs
511 * but it should be run before h_TossStuff_r */
512 (host)->holds[h_holdSlot()] &= ~h_holdbit();
513 if ((host->hostFlags & HOSTDELETED)
514 || (host->hostFlags & CLIENTDELETED)) {
518 (host)->holds[h_holdSlot()] &= ~h_holdbit();
520 (host)->holds[h_holdSlot()] &= ~h_holdbit();
526 h_OtherHolds_r(register struct host *host)
528 register int i, bit, slot;
531 for (i = 0; i < h_maxSlots; i++) {
532 if (host->holds[i] != ((i == slot) ? bit : 0)) {
540 h_Lock_r(register struct host *host)
550 * returns 1 if already locked
551 * else returns locks and returns 0
555 h_NBLock_r(register struct host *host)
557 struct Lock *hostLock = &host->lock;
562 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
563 hostLock->excl_locked = WRITE_LOCK;
567 LOCK_UNLOCK(hostLock);
576 #if FS_STATS_DETAILED
577 /*------------------------------------------------------------------------
578 * PRIVATE h_AddrInSameNetwork
581 * Given a target IP address and a candidate IP address (both
582 * in host byte order), return a non-zero value (1) if the
583 * candidate address is in a different network from the target
587 * a_targetAddr : Target address.
588 * a_candAddr : Candidate address.
591 * 1 if the candidate address is in the same net as the target,
595 * The target and candidate addresses are both in host byte
596 * order, NOT network byte order, when passed in. We return
597 * our value as a character, since that's the type of field in
598 * the host structure, where this info will be stored.
602 *------------------------------------------------------------------------*/
605 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
606 { /*h_AddrInSameNetwork */
608 afs_uint32 targetNet;
612 * Pull out the network and subnetwork numbers from the target
613 * and candidate addresses. We can short-circuit this whole
614 * affair if the target and candidate addresses are not of the
617 if (IN_CLASSA(a_targetAddr)) {
618 if (!(IN_CLASSA(a_candAddr))) {
621 targetNet = a_targetAddr & IN_CLASSA_NET;
622 candNet = a_candAddr & IN_CLASSA_NET;
623 } else if (IN_CLASSB(a_targetAddr)) {
624 if (!(IN_CLASSB(a_candAddr))) {
627 targetNet = a_targetAddr & IN_CLASSB_NET;
628 candNet = a_candAddr & IN_CLASSB_NET;
629 } /*Class B target */
630 else if (IN_CLASSC(a_targetAddr)) {
631 if (!(IN_CLASSC(a_candAddr))) {
634 targetNet = a_targetAddr & IN_CLASSC_NET;
635 candNet = a_candAddr & IN_CLASSC_NET;
636 } /*Class C target */
638 targetNet = a_targetAddr;
639 candNet = a_candAddr;
640 } /*Class D address */
643 * Now, simply compare the extracted net values for the two addresses
644 * (which at this point are known to be of the same class)
646 if (targetNet == candNet)
651 } /*h_AddrInSameNetwork */
652 #endif /* FS_STATS_DETAILED */
655 /* Assumptions: called with held host */
657 h_gethostcps_r(register struct host *host, register afs_int32 now)
662 /* wait if somebody else is already doing the getCPS call */
663 while (host->hostFlags & HCPS_INPROGRESS) {
664 slept = 1; /* I did sleep */
665 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
666 #ifdef AFS_PTHREAD_ENV
667 pthread_cond_wait(&host->cond, &host_glock_mutex);
668 #else /* AFS_PTHREAD_ENV */
669 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
670 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
671 #endif /* AFS_PTHREAD_ENV */
675 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
676 if (host->hcps.prlist_val)
677 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
678 host->hcps.prlist_val = NULL;
679 host->hcps.prlist_len = 0;
680 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
683 code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
688 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
689 * and reevaluates things if no sync server or quorum is found we could still end up
690 * with one of these errors. In such case we would like to reevaluate the rpc call to
691 * find if there's cps for this guy. We treat other errors (except network failures
692 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
693 * we could like to deal this problem the other way around (i.e. if code == NOCPS
694 * ignore else retry next time) but the problem is that there're other errors (i.e.
695 * EPERM) for which we don't want to retry and we don't know the whole code list!
697 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
699 * We would have preferred to use a while loop and try again since ops in protected
700 * acls for this host will fail now but they'll be reevaluated on any subsequent
701 * call. The attempt to wait for a quorum/sync site or network error won't work
702 * since this problems really should only occurs during a complete fileserver
703 * restart. Since the fileserver will start before the ptservers (and thus before
704 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
706 host->hcpsfailed = 1;
708 ("Warning: GetHostCPS failed (%d) for %x (%s:%d); will retry\n",
709 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
711 host->hcpsfailed = 0;
713 ("gethost: GetHostCPS failed (%d) for %x (%s:%d); ignored\n",
714 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
716 if (host->hcps.prlist_val)
717 free(host->hcps.prlist_val);
718 host->hcps.prlist_val = NULL;
719 host->hcps.prlist_len = 0; /* Make sure it's zero */
721 host->hcpsfailed = 0;
723 host->hostFlags &= ~HCPS_INPROGRESS;
724 /* signal all who are waiting */
725 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
726 host->hostFlags &= ~HCPS_WAITING;
727 #ifdef AFS_PTHREAD_ENV
728 assert(pthread_cond_broadcast(&host->cond) == 0);
729 #else /* AFS_PTHREAD_ENV */
730 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
731 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
732 #endif /* AFS_PTHREAD_ENV */
736 /* args in net byte order */
738 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
740 register struct host *host;
744 host = h_Lookup_r(hostaddr, hport, &held);
746 host->hcpsfailed = 1;
756 * Allocate a host. It will be identified by the peer (ip,port) info in the
757 * rx connection provided. The host is returned held and locked
759 #define DEF_ROPCONS 2115
762 h_Alloc_r(register struct rx_connection *r_con)
764 struct servent *serverentry;
767 #if FS_STATS_DETAILED
768 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
769 #endif /* FS_STATS_DETAILED */
773 host->host = rxr_HostOf(r_con);
774 host->port = rxr_PortOf(r_con);
776 h_AddHostToAddrHashTable_r(host->host, host->port, host);
778 if (consolePort == 0) { /* find the portal number for console */
779 #if defined(AFS_OSF_ENV)
780 serverentry = getservbyname("ropcons", "");
782 serverentry = getservbyname("ropcons", 0);
785 consolePort = serverentry->s_port;
787 consolePort = htons(DEF_ROPCONS); /* Use a default */
789 if (host->port == consolePort)
791 /* Make a callback channel even for the console, on the off chance that it
792 * makes a request that causes a break call back. It shouldn't. */
793 h_SetupCallbackConn_r(host);
794 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
796 host->hcps.prlist_val = NULL;
797 host->hcps.prlist_len = 0;
798 host->interface = NULL;
800 host->hcpsfailed = 0; /* save cycles */
801 h_gethostcps(host); /* do this under host hold/lock */
803 host->FirstClient = NULL;
806 h_InsertList_r(host); /* update global host List */
807 #if FS_STATS_DETAILED
809 * Compare the new host's IP address (in host byte order) with ours
810 * (the File Server's), remembering if they are in the same network.
812 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
813 host->InSameNetwork =
814 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
815 #endif /* FS_STATS_DETAILED */
822 /* Make a callback channel even for the console, on the off chance that it
823 * makes a request that causes a break call back. It shouldn't. */
825 h_SetupCallbackConn_r(struct host * host)
828 sc = rxnull_NewClientSecurityObject();
829 host->callback_rxcon =
830 rx_NewConnection(host->host, host->port, 1, sc, 0);
831 rx_SetConnDeadTime(host->callback_rxcon, 50);
832 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
835 /* Lookup a host given an IP address and UDP port number. */
836 /* hostaddr and hport are in network order */
837 /* Note: host should be released by caller if 0 == *heldp and non-null */
838 /* hostaddr and hport are in network order */
840 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
843 struct host *host = NULL;
844 struct h_AddrHashChain *chain;
845 int index = h_HashIndex(haddr);
846 extern int hostaclRefresh;
849 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
850 host = chain->hostPtr;
852 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
853 && chain->port == hport) {
854 *heldp = h_Held_r(host);
858 if (host->hostFlags & HOSTDELETED) {
865 now = FT_ApproxTime(); /* always evaluate "now" */
866 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
868 * Every hostaclRefresh period (def 2 hrs) get the new
869 * membership list for the host. Note this could be the
870 * first time that the host is added to a group. Also
871 * here we also retry on previous legitimate hcps failures.
873 * If we get here we still have a host hold.
875 h_gethostcps_r(host, now);
885 /* Lookup a host given its UUID. */
887 h_LookupUuid_r(afsUUID * uuidp)
889 struct host *host = 0;
890 struct h_UuidHashChain *chain;
891 int index = h_UuidHashIndex(uuidp);
893 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
894 host = chain->hostPtr;
896 if (!(host->hostFlags & HOSTDELETED) && host->interface
897 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
908 * h_Hold_r: Establish a hold by the current LWP on this host--the host
909 * or its clients will not be physically deleted until all holds have
911 * NOTE: h_Hold_r is a macro defined in host.h.
914 /* h_TossStuff_r: Toss anything in the host structure (the host or
915 * clients marked for deletion. Called from h_Release_r ONLY.
916 * To be called, there must be no holds, and either host->deleted
917 * or host->clientDeleted must be set.
920 h_TossStuff_r(register struct host *host)
922 register struct client **cp, *client;
925 /* if somebody still has this host held */
926 for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
930 /* if somebody still has this host locked */
931 if (h_NBLock_r(host) != 0) {
934 ("Warning: h_TossStuff_r failed; Host %x (%s:%d) was locked.\n",
935 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
941 /* ASSUMPTION: rxi_FreeConnection() does not yield */
942 for (cp = &host->FirstClient; (client = *cp);) {
943 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
945 ObtainWriteLockNoBlock(&client->lock, code);
949 ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x was locked.\n",
950 host, afs_inet_ntoa_r(host->host, hoststr),
951 ntohs(host->port), client));
955 if (client->refCount) {
958 ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x refcount %d.\n",
959 host, afs_inet_ntoa_r(host->host, hoststr),
960 ntohs(host->port), client, client->refCount));
961 /* This is the same thing we do if the host is locked */
962 ReleaseWriteLock(&client->lock);
965 client->CPS.prlist_len = 0;
966 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
967 free(client->CPS.prlist_val);
968 client->CPS.prlist_val = NULL;
969 CurrentConnections--;
971 ReleaseWriteLock(&client->lock);
977 /* We've just cleaned out all the deleted clients; clear the flag */
978 host->hostFlags &= ~CLIENTDELETED;
980 if (host->hostFlags & HOSTDELETED) {
981 register struct h_AddrHashChain **ahp, *ath;
982 register struct rx_connection *rxconn;
984 struct AddrPort hostAddrPort;
987 if (host->Console & 1)
989 if ((rxconn = host->callback_rxcon)) {
990 host->callback_rxcon = (struct rx_connection *)0;
991 rx_DestroyConnection(rxconn);
993 if (host->hcps.prlist_val)
994 free(host->hcps.prlist_val);
995 host->hcps.prlist_val = NULL;
996 host->hcps.prlist_len = 0;
997 DeleteAllCallBacks_r(host, 1);
998 host->hostFlags &= ~RESETDONE; /* just to be safe */
1000 /* if alternate addresses do not exist */
1001 if (!(host->interface)) {
1002 for (ahp = &hostAddrHashTable[h_HashIndex(host->host)]; (ath = *ahp);
1004 assert(ath->hostPtr);
1005 if (ath->hostPtr == host) {
1012 register struct h_UuidHashChain **uhp, *uth;
1013 /* delete the hash entry for the UUID */
1014 uuidp = &host->interface->uuid;
1015 for (uhp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (uth = *uhp);
1017 assert(uth->hostPtr);
1018 if (uth->hostPtr == host) {
1024 /* delete the hash entry for each alternate addresses */
1025 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
1026 hostAddrPort = host->interface->interface[i];
1028 if (!hostAddrPort.valid)
1031 for (ahp = &hostAddrHashTable[h_HashIndex(hostAddrPort.addr)]; (ath = *ahp);
1033 assert(ath->hostPtr);
1034 if (ath->hostPtr == host) {
1041 free(host->interface);
1042 host->interface = NULL;
1043 } /* if alternate address exists */
1045 h_DeleteList_r(host); /* remove host from global host List */
1048 } /*h_TossStuff_r */
1050 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
1051 * system at the start of the enumeration (perhaps more). Hosts may be deleted
1052 * (have delete flag set); ditto for clients. (*proc) is always called with
1053 * host h_held(). The hold state of the host with respect to this lwp is passed
1054 * to (*proc) as the param held. The proc should return 0 if the host should be
1055 * released, 1 if it should be held after enumeration.
1058 h_Enumerate(int (*proc) (), char *param)
1060 register struct host *host, **list;
1062 register int i, count;
1065 if (hostCount == 0) {
1069 list = (struct host **)malloc(hostCount * sizeof(struct host *));
1071 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
1074 held = (int *)malloc(hostCount * sizeof(int));
1076 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
1079 for (count = 0, host = hostList; host; host = host->next, count++) {
1081 if (!(held[count] = h_Held_r(host)))
1084 assert(count == hostCount);
1086 for (i = 0; i < count; i++) {
1087 held[i] = (*proc) (list[i], held[i], param);
1088 if (!H_ENUMERATE_ISSET_HELD(held[i]))
1089 h_Release(list[i]); /* this might free up the host */
1090 /* bail out of the enumeration early */
1091 if (H_ENUMERATE_ISSET_BAIL(held[i]))
1098 /* h_Enumerate_r (revised):
1099 * Calls (*proc)(host, held, param) for each host in hostList, starting
1101 * Hosts may be deleted (have delete flag set); ditto for clients.
1102 * (*proc) is always called with
1103 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
1104 * host with respect to this lwp is passed to (*proc) as the param held.
1105 * The proc should return 0 if the host should be released, 1 if it should
1106 * be held after enumeration.
1109 h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
1111 register struct host *host, *next;
1112 register int held, nheld;
1114 if (hostCount == 0) {
1117 if (enumstart && !(held = h_Held_r(enumstart)))
1118 h_Hold_r(enumstart);
1119 for (host = enumstart; host; host = next, held = nheld) {
1121 if (next && !(nheld = h_Held_r(next)) && !H_ENUMERATE_ISSET_BAIL(held))
1123 held = (*proc) (host, held, param);
1124 if (!H_ENUMERATE_ISSET_HELD(held))
1125 h_Release_r(host); /* this might free up the host */
1126 if (H_ENUMERATE_ISSET_BAIL(held)) {
1127 if (!H_ENUMERATE_ISSET_HELD(nheld))
1128 h_Release_r(next); /* this might free up the host */
1132 } /*h_Enumerate_r */
1134 /* inserts a new HashChain structure corresponding to this UUID */
1136 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1139 struct h_UuidHashChain *chain;
1141 /* hash into proper bucket */
1142 index = h_UuidHashIndex(uuid);
1144 /* don't add the same entry multiple times */
1145 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1146 if (host->interface && afs_uuid_equal(&host->interface->uuid, uuid))
1150 /* insert into beginning of list for this bucket */
1151 chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
1153 ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1157 chain->hostPtr = host;
1158 chain->next = hostUuidHashTable[index];
1159 hostUuidHashTable[index] = chain;
1163 /* inserts a new HashChain structure corresponding to this address */
1165 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1168 struct h_AddrHashChain *chain;
1172 /* hash into proper bucket */
1173 index = h_HashIndex(addr);
1175 /* don't add the same entry multiple times */
1176 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1177 if (chain->addr == addr && chain->port == port) {
1178 if (chain->hostPtr == host)
1180 else if (!(host->hostFlags & HOSTDELETED))
1181 ViceLog(125, ("Addr %s:%d assigned to %x and %x.\n",
1182 afs_inet_ntoa_r(addr, hoststr), ntohs(port),
1190 /* insert into beginning of list for this bucket */
1191 chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
1193 ViceLog(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1196 chain->hostPtr = host;
1197 chain->next = hostAddrHashTable[index];
1200 hostAddrHashTable[index] = chain;
1204 * This is called with host locked and held. At this point, the
1205 * hostAddrHashTable should not have entries for the alternate
1206 * interfaces. This function has to insert these entries in the
1207 * hostAddrHashTable.
1209 * All addresses are in network byte order.
1212 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1217 struct Interface *interface;
1218 char hoststr[16], hoststr2[16];
1221 assert(host->interface);
1224 * Make sure this address is on the list of known addresses
1227 number = host->interface->numberOfInterfaces;
1228 for (i = 0, found = 0; i < number && !found; i++) {
1229 if (host->interface->interface[i].addr == addr &&
1230 host->interface->interface[i].port == port) {
1232 host->interface->interface[i].valid = 1;
1236 ViceLog(125, ("addInterfaceAddr : host %s:%d addr %s:%d : found:%d\n",
1237 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1238 afs_inet_ntoa_r(addr, hoststr2), ntohs(port), found));
1241 interface = (struct Interface *)
1242 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1244 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
1247 interface->numberOfInterfaces = number + 1;
1248 interface->uuid = host->interface->uuid;
1249 for (i = 0; i < number; i++)
1250 interface->interface[i] = host->interface->interface[i];
1251 interface->interface[number].addr = addr;
1252 interface->interface[number].port = port;
1253 interface->interface[number].valid = 1;
1254 free(host->interface);
1255 host->interface = interface;
1263 * This is called with host locked and held. At this point, the
1264 * hostAddrHashTable should not be having entries for the alternate
1265 * interfaces. This function has to insert these entries in the
1266 * hostAddrHashTable.
1268 * All addresses are in network byte order.
1271 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1276 struct Interface *interface;
1277 char hoststr[16], hoststr2[16];
1280 assert(host->interface);
1282 ViceLog(125, ("removeInterfaceAddr : host %s:%d addr %s:%d\n",
1283 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1284 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
1287 * Make sure this address is on the list of known addresses
1290 interface = host->interface;
1291 number = host->interface->numberOfInterfaces;
1292 for (i = 0, found = 0; i < number && !found; i++) {
1293 if (interface->interface[i].addr == addr &&
1294 interface->interface[i].port == port) {
1296 interface->interface[i].valid = 0;
1301 for (; i < number; i++) {
1302 interface->interface[i] = interface->interface[i+1];
1304 interface->numberOfInterfaces = number;
1308 * Remove the hash table entry for this address
1310 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1316 * This is called with host locked and held. This function differs
1317 * from removeInterfaceAddr_r in that it is called when the address
1318 * is being removed from the host regardless of whether or not there
1319 * is an interface list for the host. This function will delete the
1320 * host if there are no addresses left on it.
1322 * All addresses are in network byte order.
1325 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1328 char hoststr[16], hoststr2[16];
1330 if (!host->interface) {
1331 if (host->host == addr && host->port == port) {
1333 ("Removing only address for host %x (%s:%d), deleting host.\n",
1334 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1335 host->hostFlags |= HOSTDELETED;
1338 removeInterfaceAddr_r(host, host->host, host->port);
1339 if (host->interface->numberOfInterfaces == 0) {
1341 ("Removed only address for host %x (%s:%d), no alternate interfaces, deleting host.\n",
1342 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1343 host->hostFlags |= HOSTDELETED;
1345 struct rx_connection *rxconn;
1347 rxconn = host->callback_rxcon;
1348 host->callback_rxcon = NULL;
1351 struct client *client;
1353 * If rx_DestroyConnection calls h_FreeConnection we will
1354 * deadlock on the host_glock_mutex. Work around the problem
1355 * by unhooking the client from the connection before
1356 * destroying the connection.
1358 client = rx_GetSpecific(rxconn, rxcon_client_key);
1359 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1360 rx_DestroyConnection(rxconn);
1363 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1364 if (host->interface->interface[i].valid) {
1366 ("Removed address for host %x (%s:%d), new primary interface %s:%d.\n",
1367 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1368 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2),
1369 ntohs(host->interface->interface[i].port)));
1370 host->host = host->interface->interface[i].addr;
1371 host->port = host->interface->interface[i].port;
1372 h_AddHostToAddrHashTable_r(host->host, host->port, host);
1377 if (i == host->interface->numberOfInterfaces) {
1379 ("Removed only address for host %x (%s:%d), no valid alternate interfaces, deleting host.\n",
1380 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1381 host->hostFlags |= HOSTDELETED;
1384 sc = rxnull_NewClientSecurityObject();
1385 host->callback_rxcon =
1386 rx_NewConnection(host->host, host->port, 1, sc, 0);
1387 rx_SetConnDeadTime(host->callback_rxcon, 50);
1388 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
1396 h_threadquota(int waiting)
1401 } else if (lwps > 32) {
1404 } else if (lwps > 16) {
1414 /* Host is returned held */
1416 h_GetHost_r(struct rx_connection *tcon)
1419 struct host *oldHost;
1422 struct interfaceAddr interf;
1423 int interfValid = 0;
1424 struct Identity *identP = NULL;
1427 char hoststr[16], hoststr2[16];
1429 struct rx_connection *cb_conn = NULL;
1431 caps.Capabilities_val = NULL;
1433 haddr = rxr_HostOf(tcon);
1434 hport = rxr_PortOf(tcon);
1436 if (caps.Capabilities_val)
1437 free(caps.Capabilities_val);
1438 caps.Capabilities_val = NULL;
1439 caps.Capabilities_len = 0;
1442 host = h_Lookup_r(haddr, hport, &held);
1443 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1444 if (host && !identP && !(host->Console & 1)) {
1445 /* This is a new connection, and we already have a host
1446 * structure for this address. Verify that the identity
1447 * of the caller matches the identity in the host structure.
1449 if ((host->hostFlags & HWHO_INPROGRESS) &&
1450 h_threadquota(host->lock.num_waiting))
1453 if (!(host->hostFlags & ALTADDR)) {
1454 host->hostFlags &= ~HWHO_INPROGRESS;
1455 /* Another thread is doing initialization */
1460 ("Host %x (%s:%d) starting h_Lookup again\n",
1461 host, afs_inet_ntoa_r(host->host, hoststr),
1462 ntohs(host->port)));
1465 host->hostFlags &= ~ALTADDR;
1466 cb_conn = host->callback_rxcon;
1467 rx_GetConnection(cb_conn);
1470 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1471 if (code == RXGEN_OPCODE)
1472 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1473 rx_PutConnection(cb_conn);
1476 if ((code == RXGEN_OPCODE) ||
1477 (afs_uuid_equal(&interf.uuid, &nulluuid))) {
1478 identP = (struct Identity *)malloc(sizeof(struct Identity));
1480 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1484 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1485 /* The host on this connection was unable to respond to
1486 * the WhoAreYou. We will treat this as a new connection
1487 * from the existing host. The worst that can happen is
1488 * that we maintain some extra callback state information */
1489 if (host->interface) {
1491 ("Host %x (%s:%d) used to support WhoAreYou, deleting.\n",
1493 afs_inet_ntoa_r(host->host, hoststr),
1494 ntohs(host->port)));
1495 host->hostFlags |= HOSTDELETED;
1496 host->hostFlags &= ~HWHO_INPROGRESS;
1503 } else if (code == 0) {
1505 identP = (struct Identity *)malloc(sizeof(struct Identity));
1507 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1511 identP->uuid = interf.uuid;
1512 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1513 /* Check whether the UUID on this connection matches
1514 * the UUID in the host structure. If they don't match
1515 * then this is not the same host as before. */
1516 if (!host->interface
1517 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1519 ("Uuid doesn't match host %x (%s:%d).\n",
1520 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1522 removeAddress_r(host, host->host, host->port);
1523 host->hostFlags &= ~HWHO_INPROGRESS;
1532 ("CB: WhoAreYou failed for host %x (%s:%d), error %d\n",
1533 host, afs_inet_ntoa_r(host->host, hoststr),
1534 ntohs(host->port), code));
1535 host->hostFlags |= VENUSDOWN;
1537 if (caps.Capabilities_val
1538 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1539 host->hostFlags |= HERRORTRANS;
1541 host->hostFlags &= ~(HERRORTRANS);
1542 host->hostFlags |= ALTADDR;
1543 host->hostFlags &= ~HWHO_INPROGRESS;
1546 if (!(host->hostFlags & ALTADDR)) {
1547 /* another thread is doing the initialisation */
1549 ("Host %x (%s:%d) waiting for host-init to complete\n",
1550 host, afs_inet_ntoa_r(host->host, hoststr),
1551 ntohs(host->port)));
1553 host->hostFlags &= ~HWHO_INPROGRESS;
1558 ("Host %x (%s:%d) starting h_Lookup again\n",
1559 host, afs_inet_ntoa_r(host->host, hoststr),
1560 ntohs(host->port)));
1563 /* We need to check whether the identity in the host structure
1564 * matches the identity on the connection. If they don't match
1565 * then treat this a new host. */
1566 if (!(host->Console & 1)
1567 && ((!identP->valid && host->interface)
1568 || (identP->valid && !host->interface)
1570 && !afs_uuid_equal(&identP->uuid,
1571 &host->interface->uuid)))) {
1572 char uuid1[128], uuid2[128];
1574 afsUUID_to_string(&identP->uuid, uuid1, 127);
1575 if (host->interface)
1576 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1578 ("CB: new identity for host %x (%s:%d), deleting(%x %x %s %s)\n",
1579 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1580 identP->valid, host->interface,
1581 identP->valid ? uuid1 : "no_uuid",
1582 host->interface ? uuid2 : "no_uuid"));
1584 /* The host in the cache is not the host for this connection */
1585 host->hostFlags |= HOSTDELETED;
1586 host->hostFlags &= ~HWHO_INPROGRESS;
1593 host = h_Alloc_r(tcon); /* returned held and locked */
1594 h_gethostcps_r(host, FT_ApproxTime());
1595 if (!(host->Console & 1)) {
1597 cb_conn = host->callback_rxcon;
1598 rx_GetConnection(cb_conn);
1601 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1602 if (code == RXGEN_OPCODE)
1603 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1604 rx_PutConnection(cb_conn);
1607 if ((code == RXGEN_OPCODE) ||
1608 afs_uuid_equal(&interf.uuid, &nulluuid)) {
1611 (struct Identity *)malloc(sizeof(struct Identity));
1616 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1621 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1623 ("Host %x (%s:%d) does not support WhoAreYou.\n",
1624 host, afs_inet_ntoa_r(host->host, hoststr),
1625 ntohs(host->port)));
1627 } else if (code == 0) {
1630 (struct Identity *)malloc(sizeof(struct Identity));
1635 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1640 identP->uuid = interf.uuid;
1642 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1644 ("WhoAreYou success on host %x (%s:%d)\n",
1645 host, afs_inet_ntoa_r(host->host, hoststr),
1646 ntohs(host->port)));
1648 if (code == 0 && !identP->valid) {
1649 cb_conn = host->callback_rxcon;
1650 rx_GetConnection(cb_conn);
1652 code = RXAFSCB_InitCallBackState(cb_conn);
1653 rx_PutConnection(cb_conn);
1656 } else if (code == 0) {
1657 oldHost = h_LookupUuid_r(&identP->uuid);
1661 if (!h_Held_r(oldHost))
1664 oldHost->hostFlags |= HWHO_INPROGRESS;
1666 if (oldHost->interface) {
1668 afsUUID uuid = oldHost->interface->uuid;
1669 cb_conn = oldHost->callback_rxcon;
1670 rx_GetConnection(cb_conn);
1671 rx_SetConnDeadTime(cb_conn, 2);
1672 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1674 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1676 rx_SetConnDeadTime(cb_conn, 50);
1677 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1678 rx_PutConnection(cb_conn);
1681 /* The primary address is either not responding or
1682 * is not the client we are looking for.
1683 * MultiProbeAlternateAddress_r() will remove the
1684 * alternate interfaces that do not have the same
1686 ViceLog(0,("CB: ProbeUuid for host %x (%s:%d) failed %d\n",
1688 afs_inet_ntoa_r(oldHost->host, hoststr),
1689 ntohs(oldHost->port),code2));
1690 MultiProbeAlternateAddress_r(oldHost);
1697 /* This is a new address for an existing host. Update
1698 * the list of interfaces for the existing host and
1699 * delete the host structure we just allocated. */
1701 /* prevent warnings while manipulating interface lists */
1702 host->hostFlags |= HOSTDELETED;
1704 if (oldHost->host != haddr || oldHost->port != hport) {
1705 struct rx_connection *rxconn;
1708 ("CB: Host %x (%s:%d) has new addr %s:%d\n",
1710 afs_inet_ntoa_r(oldHost->host, hoststr2),
1711 ntohs(oldHost->port),
1712 afs_inet_ntoa_r(haddr, hoststr),
1714 if (probefail || oldHost->host == haddr) {
1715 /* The probe failed which means that the old address is
1716 * either unreachable or is not the same host we were just
1717 * contacted by. We will also remove addresses if only
1718 * the port has changed because that indicates the client
1721 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
1724 struct Interface *interface = oldHost->interface;
1725 int number = oldHost->interface->numberOfInterfaces;
1726 for (i = 0, found = 0; i < number; i++) {
1727 if (interface->interface[i].addr == haddr &&
1728 interface->interface[i].port != hport) {
1734 /* We have just been contacted by a client that has been
1735 * seen from behind a NAT and at least one other address.
1737 removeInterfaceAddr_r(oldHost, haddr, interface->interface[i].port);
1740 addInterfaceAddr_r(oldHost, haddr, hport);
1741 oldHost->host = haddr;
1742 oldHost->port = hport;
1743 rxconn = oldHost->callback_rxcon;
1744 oldHost->callback_rxcon = host->callback_rxcon;
1745 host->callback_rxcon = NULL;
1748 struct client *client;
1750 * If rx_DestroyConnection calls h_FreeConnection we will
1751 * deadlock on the host_glock_mutex. Work around the problem
1752 * by unhooking the client from the connection before
1753 * destroying the connection.
1755 client = rx_GetSpecific(rxconn, rxcon_client_key);
1756 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1757 rx_DestroyConnection(rxconn);
1760 host->hostFlags &= ~HWHO_INPROGRESS;
1762 /* release host because it was allocated by h_Alloc_r */
1765 /* the new host is held and locked */
1767 /* This really is a new host */
1768 h_AddHostToUuidHashTable_r(&identP->uuid, host);
1769 cb_conn = host->callback_rxcon;
1770 rx_GetConnection(cb_conn);
1773 RXAFSCB_InitCallBackState3(cb_conn,
1775 rx_PutConnection(cb_conn);
1779 ("InitCallBackState3 success on host %x (%s:%d)\n",
1780 host, afs_inet_ntoa_r(host->host, hoststr),
1781 ntohs(host->port)));
1782 assert(interfValid == 1);
1783 initInterfaceAddr_r(host, &interf);
1789 ("CB: RCallBackConnectBack failed for %x (%s:%d)\n",
1790 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1791 host->hostFlags |= VENUSDOWN;
1794 ("CB: RCallBackConnectBack succeeded for %x (%s:%d)\n",
1795 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1796 host->hostFlags |= RESETDONE;
1799 if (caps.Capabilities_val
1800 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1801 host->hostFlags |= HERRORTRANS;
1803 host->hostFlags &= ~(HERRORTRANS);
1804 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1805 host->hostFlags &= ~HWHO_INPROGRESS;
1808 if (caps.Capabilities_val)
1809 free(caps.Capabilities_val);
1810 caps.Capabilities_val = NULL;
1811 caps.Capabilities_len = 0;
1817 static char localcellname[PR_MAXNAMELEN + 1];
1818 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1819 int num_lrealms = -1;
1825 memset(&nulluuid, 0, sizeof(afsUUID));
1826 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1827 if (num_lrealms == -1) {
1829 for (i=0; i<AFS_NUM_LREALMS; i++) {
1830 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1836 ("afs_krb_get_lrealm failed, using %s.\n",
1838 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1844 /* initialize the rest of the local realms to nullstring for debugging */
1845 for (; i<AFS_NUM_LREALMS; i++)
1846 local_realms[i][0] = '\0';
1848 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1849 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1850 #ifdef AFS_PTHREAD_ENV
1851 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1852 #endif /* AFS_PTHREAD_ENV */
1856 MapName_r(char *aname, char *acell, afs_int32 * aval)
1861 afs_int32 anamelen, cnamelen;
1865 anamelen = strlen(aname);
1866 if (anamelen >= PR_MAXNAMELEN)
1867 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1869 lnames.namelist_len = 1;
1870 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1871 lids.idlist_len = 0;
1872 lids.idlist_val = NULL;
1874 cnamelen = strlen(acell);
1876 if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
1878 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1879 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1880 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1882 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1884 *aval = AnonymousID;
1887 foreign = 1; /* attempt cross-cell authentication */
1888 tname = (char *)malloc(PR_MAXNAMELEN);
1890 ViceLog(0, ("Failed malloc in MapName_r\n"));
1893 strcpy(tname, aname);
1894 tname[anamelen] = '@';
1895 strcpy(tname + anamelen + 1, acell);
1896 lnames.namelist_val = (prname *) tname;
1901 code = hpr_NameToId(&lnames, &lids);
1904 if (lids.idlist_val) {
1905 *aval = lids.idlist_val[0];
1906 if (*aval == AnonymousID) {
1908 ("MapName: NameToId on %s returns anonymousID\n",
1909 lnames.namelist_val));
1911 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1914 ("MapName: NameToId on '%s' is unknown\n",
1915 lnames.namelist_val));
1921 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1929 /* NOTE: this returns the client with a Write lock and a refCount */
1931 h_ID2Client(afs_int32 vid)
1933 register struct client *client;
1934 register struct host *host;
1937 for (host = hostList; host; host = host->next) {
1938 if (host->hostFlags & HOSTDELETED)
1940 for (client = host->FirstClient; client; client = client->next) {
1941 if (!client->deleted && client->ViceId == vid) {
1944 ObtainWriteLock(&client->lock);
1955 * Called by the server main loop. Returns a h_Held client, which must be
1956 * released later the main loop. Allocates a client if the matching one
1957 * isn't around. The client is returned with its reference count incremented
1958 * by one. The caller must call h_ReleaseClient_r when finished with
1961 * the client->host is returned held. h_ReleaseClient_r does not release
1962 * the hold on client->host.
1965 h_FindClient_r(struct rx_connection *tcon)
1967 register struct client *client;
1968 register struct host *host;
1969 struct client *oldClient;
1974 #if (64-MAXKTCNAMELEN)
1975 ticket name length != 64
1979 char uname[PR_MAXNAMELEN];
1980 char tcell[MAXKTCREALMLEN];
1984 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1985 if (client && client->sid == rxr_CidOf(tcon)
1986 && client->VenusEpoch == rxr_GetEpoch(tcon)) {
1988 h_Hold_r(client->host);
1989 if (!client->deleted && client->prfail != 2) {
1990 /* Could add shared lock on client here */
1991 /* note that we don't have to lock entry in this path to
1992 * ensure CPS is initialized, since we don't call rx_SetSpecific
1993 * until initialization is done, and we only get here if
1994 * rx_GetSpecific located the client structure.
1999 ObtainWriteLock(&client->lock); /* released at end */
2005 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
2007 ("FindClient: authenticating connection: authClass=%d\n",
2009 if (authClass == 1) {
2010 /* A bcrypt tickets, no longer supported */
2011 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2012 viceid = AnonymousID;
2013 expTime = 0x7fffffff;
2014 } else if (authClass == 2) {
2017 /* kerberos ticket */
2018 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
2019 tname, tinst, tcell, &kvno);
2021 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2022 viceid = AnonymousID;
2023 expTime = 0x7fffffff;
2025 int ilen = strlen(tinst);
2027 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2028 tname, tinst, tcell, expTime, kvno));
2029 strncpy(uname, tname, sizeof(uname));
2031 if (strlen(uname) + 1 + ilen >= sizeof(uname))
2034 strcat(uname, tinst);
2036 /* translate the name to a vice id */
2037 code = MapName_r(uname, tcell, &viceid);
2041 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
2044 viceid = AnonymousID;
2045 expTime = 0x7fffffff;
2049 viceid = AnonymousID; /* unknown security class */
2050 expTime = 0x7fffffff;
2053 if (!client) { /* loop */
2054 host = h_GetHost_r(tcon); /* Returns it h_Held */
2060 /* First try to find the client structure */
2061 for (client = host->FirstClient; client; client = client->next) {
2062 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2063 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2066 ObtainWriteLock(&client->lock);
2072 /* Still no client structure - get one */
2075 /* Retry to find the client structure */
2076 for (client = host->FirstClient; client; client = client->next) {
2077 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2078 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2080 goto retryfirstclient;
2085 ObtainWriteLock(&client->lock);
2086 client->refCount = 1;
2087 client->host = host;
2088 #if FS_STATS_DETAILED
2089 client->InSameNetwork = host->InSameNetwork;
2090 #endif /* FS_STATS_DETAILED */
2091 client->ViceId = viceid;
2092 client->expTime = expTime; /* rx only */
2093 client->authClass = authClass; /* rx only */
2094 client->sid = rxr_CidOf(tcon);
2095 client->VenusEpoch = rxr_GetEpoch(tcon);
2096 client->CPS.prlist_val = NULL;
2097 client->CPS.prlist_len = 0;
2101 client->prfail = fail;
2103 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
2104 client->CPS.prlist_len = 0;
2105 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
2106 free(client->CPS.prlist_val);
2107 client->CPS.prlist_val = NULL;
2108 client->ViceId = viceid;
2109 client->expTime = expTime;
2111 if (viceid == ANONYMOUSID) {
2112 client->CPS.prlist_len = AnonCPS.prlist_len;
2113 client->CPS.prlist_val = AnonCPS.prlist_val;
2116 code = hpr_GetCPS(viceid, &client->CPS);
2121 ("pr_GetCPS failed(%d) for user %d, host %x (%s:%d)\n",
2122 code, viceid, client->host,
2123 afs_inet_ntoa_r(client->host->host,hoststr),
2124 ntohs(client->host->port)));
2126 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2127 * all protection servers and reevaluates things if no
2128 * sync server or quorum is found we could still end up
2129 * with one of these errors. In such case we would like to
2130 * reevaluate the rpc call to find if there's cps for this
2131 * guy. We treat other errors (except network failures
2132 * ones - i.e. code < 0) as an indication that there is no
2133 * CPS for this host. Ideally we could like to deal this
2134 * problem the other way around (i.e. if code == NOCPS
2135 * ignore else retry next time) but the problem is that
2136 * there're other errors (i.e. EPERM) for which we don't
2137 * want to retry and we don't know the whole code list!
2139 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2143 /* the disabling of system:administrators is so iffy and has so many
2144 * possible failure modes that we will disable it again */
2145 /* Turn off System:Administrator for safety
2146 * if (AL_IsAMember(SystemId, client->CPS) == 0)
2147 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
2150 /* Now, tcon may already be set to a rock, since we blocked with no host
2151 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2152 * required). So, before setting the RPC's rock, we should disconnect
2153 * the RPC from the other client structure's rock.
2155 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2156 if (oldClient && oldClient != client && oldClient->sid == rxr_CidOf(tcon)
2157 && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
2159 if (!oldClient->deleted) {
2160 /* if we didn't create it, it's not ours to put back */
2162 ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n",
2163 client, client->sid, tcon,
2164 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2165 ntohs(rxr_PortOf(tcon)),
2166 oldClient, oldClient->sid));
2167 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2168 free(client->CPS.prlist_val);
2169 client->CPS.prlist_val = NULL;
2170 client->CPS.prlist_len = 0;
2172 /* We should perhaps check for 0 here */
2174 ReleaseWriteLock(&client->lock);
2179 oldClient->refCount++;
2181 ObtainWriteLock(&oldClient->lock);
2185 ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
2186 oldClient, oldClient->sid, tcon,
2187 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2188 ntohs(rxr_PortOf(tcon)),
2189 client, client->sid));
2190 /* rx_SetSpecific will be done immediately below */
2193 /* Avoid chaining in more than once. */
2196 client->next = host->FirstClient;
2197 host->FirstClient = client;
2199 CurrentConnections++; /* increment number of connections */
2201 rx_SetSpecific(tcon, rxcon_client_key, client);
2202 ReleaseWriteLock(&client->lock);
2206 } /*h_FindClient_r */
2209 h_ReleaseClient_r(struct client *client)
2211 assert(client->refCount > 0);
2218 * Sigh: this one is used to get the client AGAIN within the individual
2219 * server routines. This does not bother h_Holding the host, since
2220 * this is assumed already have been done by the server main loop.
2221 * It does check tokens, since only the server routines can return the
2222 * VICETOKENDEAD error code
2225 GetClient(struct rx_connection *tcon, struct client **cp)
2227 register struct client *client;
2232 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2233 if (client == NULL) {
2235 ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
2236 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2237 ntohs(rxr_PortOf(tcon))));
2241 if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
2243 ("GetClient: tcon %x tcon sid %d client sid %d\n",
2244 tcon, rxr_CidOf(tcon), client->sid));
2248 if (client && client->LastCall > client->expTime && client->expTime) {
2250 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2251 afs_inet_ntoa_r(client->host->host, hoststr),
2252 ntohs(client->host->port), client->expTime));
2254 return VICETOKENDEAD;
2264 PutClient(struct client **cp)
2270 h_ReleaseClient_r(*cp);
2277 /* Client user name for short term use. Note that this is NOT inexpensive */
2279 h_UserName(struct client *client)
2281 static char User[PR_MAXNAMELEN + 1];
2285 lids.idlist_len = 1;
2286 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
2287 if (!lids.idlist_val) {
2288 ViceLog(0, ("Failed malloc in h_UserName\n"));
2291 lnames.namelist_len = 0;
2292 lnames.namelist_val = (prname *) 0;
2293 lids.idlist_val[0] = client->ViceId;
2294 if (hpr_IdToName(&lids, &lnames)) {
2295 /* We need to free id we alloced above! */
2296 free(lids.idlist_val);
2297 return "*UNKNOWN USER NAME*";
2299 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2300 free(lids.idlist_val);
2301 free(lnames.namelist_val);
2310 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2311 CEs, CEBlocks, HTs, HTBlocks));
2317 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
2319 register struct client *client;
2324 time_t LastCall, expTime;
2327 LastCall = host->LastCall;
2328 if (host->hostFlags & HOSTDELETED) {
2332 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2333 "Host %s:%d down = %d, LastCall %s",
2334 afs_inet_ntoa_r(host->host, hoststr),
2335 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2336 afs_ctime(&LastCall, tbuffer,
2338 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2339 for (client = host->FirstClient; client; client = client->next) {
2340 if (!client->deleted) {
2341 expTime = client->expTime;
2342 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2343 " user id=%d, name=%s, sl=%s till %s",
2344 client->ViceId, h_UserName(client),
2346 authClass ? "Authenticated" :
2347 "Not authenticated",
2349 authClass ? afs_ctime(&expTime, tbuffer,
2352 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2353 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2354 client->CPS.prlist_len);
2355 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2356 if (client->CPS.prlist_val) {
2357 for (i = 0; i > client->CPS.prlist_len; i++) {
2358 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2359 client->CPS.prlist_val[i]);
2360 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2363 sprintf(tmpStr, "]\n");
2364 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2370 } /*h_PrintClient */
2375 * Print a list of clients, with last security level and token value seen,
2385 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2389 ("Couldn't create client dump file %s\n",
2390 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2393 now = FT_ApproxTime();
2394 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
2395 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2396 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2397 h_Enumerate(h_PrintClient, (char *)file);
2398 STREAM_REALLYCLOSE(file);
2399 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2406 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
2413 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2414 "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 [",
2415 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
2416 host->cblist, CheckLock(&host->lock), host->LastCall,
2417 host->ActiveCall, (host->hostFlags & VENUSDOWN),
2418 host->hostFlags & HOSTDELETED, host->Console,
2419 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2421 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2422 if (host->hcps.prlist_val)
2423 for (i = 0; i < host->hcps.prlist_len; i++) {
2424 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2425 host->hcps.prlist_val[i]);
2426 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2428 sprintf(tmpStr, "] [");
2429 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2430 if (host->interface)
2431 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2433 sprintf(tmpStr, " %s:%d",
2434 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2435 ntohs(host->interface->interface[i].port));
2436 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2438 sprintf(tmpStr, "] holds: ");
2439 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2441 for (i = 0; i < h_maxSlots; i++) {
2442 sprintf(tmpStr, "%04x", host->holds[i]);
2443 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2445 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
2446 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2458 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2464 ("Couldn't create host dump file %s\n",
2465 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2468 now = FT_ApproxTime();
2469 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
2470 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2471 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2472 h_Enumerate(h_DumpHost, (char *)file);
2473 STREAM_REALLYCLOSE(file);
2474 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2478 #ifdef AFS_DEMAND_ATTACH_FS
2481 * host state serialization
2483 static int h_stateFillHeader(struct host_state_header * hdr);
2484 static int h_stateCheckHeader(struct host_state_header * hdr);
2485 static int h_stateAllocMap(struct fs_dump_state * state);
2486 static int h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state);
2487 static int h_stateRestoreHost(struct fs_dump_state * state);
2488 static int h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state);
2489 static int h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state);
2490 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port);
2491 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2492 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2493 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2496 /* this procedure saves all host state to disk for fast startup */
2498 h_stateSave(struct fs_dump_state * state)
2500 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2503 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2505 /* invalidate host state header */
2506 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2508 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2509 sizeof(struct host_state_header))) {
2514 fs_stateIncEOF(state, sizeof(struct host_state_header));
2516 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2521 h_stateFillHeader(state->h_hdr);
2523 /* write the real header to disk */
2524 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2525 sizeof(struct host_state_header));
2532 * host state serialization
2534 * this procedure restores all host state from a disk for fast startup
2537 h_stateRestore(struct fs_dump_state * state)
2541 /* seek to the right position and read in the host state header */
2542 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2543 sizeof(struct host_state_header))) {
2548 /* check the validity of the header */
2549 if (h_stateCheckHeader(state->h_hdr)) {
2554 records = state->h_hdr->records;
2556 if (h_stateAllocMap(state)) {
2561 /* iterate over records restoring host state */
2562 for (i=0; i < records; i++) {
2563 if (h_stateRestoreHost(state) != 0) {
2574 h_stateRestoreIndices(struct fs_dump_state * state)
2576 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
2581 h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state)
2583 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
2584 return H_ENUMERATE_BAIL(held);
2590 h_stateVerify(struct fs_dump_state * state)
2592 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
2597 h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state)
2602 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
2603 return H_ENUMERATE_BAIL(held);
2607 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
2608 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
2609 h->interface->interface[i].port)) {
2613 if (h_stateVerifyUuidHash(state, h)) {
2616 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port)) {
2620 if (cb_stateVerifyHCBList(state, h)) {
2629 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port)
2631 int ret = 0, found = 0;
2632 struct host *host = NULL;
2633 struct h_AddrHashChain *chain;
2634 int index = h_HashIndex(addr);
2638 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
2639 host = chain->hostPtr;
2641 afs_inet_ntoa_r(addr, tmp);
2642 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
2646 if ((chain->addr == addr) && (chain->port == port)) {
2648 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry points to different host struct (%d, %d)\n",
2649 h->index, host->index));
2650 state->flags.warnings_generated = 1;
2655 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
2656 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2657 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
2665 afs_inet_ntoa_r(addr, tmp);
2666 if (state->mode == FS_STATE_LOAD_MODE) {
2667 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s not found in hash\n", tmp));
2671 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s not found in hash\n", tmp));
2672 state->flags.warnings_generated = 1;
2681 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
2683 int ret = 0, found = 0;
2684 struct host *host = NULL;
2685 struct h_UuidHashChain *chain;
2686 afsUUID * uuidp = &h->interface->uuid;
2687 int index = h_UuidHashIndex(uuidp);
2691 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
2692 host = chain->hostPtr;
2694 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2695 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
2699 if (host->interface &&
2700 afs_uuid_equal(&host->interface->uuid, uuidp)) {
2702 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
2703 h->index, host->index));
2704 state->flags.warnings_generated = 1;
2709 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
2710 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2711 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
2719 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2720 if (state->mode == FS_STATE_LOAD_MODE) {
2721 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
2725 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
2726 state->flags.warnings_generated = 1;
2734 /* create the host state header structure */
2736 h_stateFillHeader(struct host_state_header * hdr)
2738 hdr->stamp.magic = HOST_STATE_MAGIC;
2739 hdr->stamp.version = HOST_STATE_VERSION;
2742 /* check the contents of the host state header structure */
2744 h_stateCheckHeader(struct host_state_header * hdr)
2748 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
2749 ViceLog(0, ("check_host_state_header: invalid state header\n"));
2752 else if (hdr->stamp.version != HOST_STATE_VERSION) {
2753 ViceLog(0, ("check_host_state_header: unknown version number\n"));
2759 /* allocate the host id mapping table */
2761 h_stateAllocMap(struct fs_dump_state * state)
2763 state->h_map.len = state->h_hdr->index_max + 1;
2764 state->h_map.entries = (struct idx_map_entry_t *)
2765 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
2766 return (state->h_map.entries != NULL) ? 0 : 1;
2769 /* function called by h_Enumerate to save a host to disk */
2771 h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state)
2773 int i, if_len=0, hcps_len=0;
2774 struct hostDiskEntry hdsk;
2775 struct host_state_entry_header hdr;
2776 struct Interface * ifp = NULL;
2777 afs_int32 * hcps = NULL;
2778 struct iovec iov[4];
2781 memset(&hdr, 0, sizeof(hdr));
2783 if (state->h_hdr->index_max < host->index) {
2784 state->h_hdr->index_max = host->index;
2787 h_hostToDiskEntry_r(host, &hdsk);
2788 if (host->interface) {
2789 if_len = sizeof(struct Interface) +
2790 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
2791 ifp = (struct Interface *) malloc(if_len);
2792 assert(ifp != NULL);
2793 memcpy(ifp, host->interface, if_len);
2794 hdr.interfaces = host->interface->numberOfInterfaces;
2795 iov[iovcnt].iov_base = (char *) ifp;
2796 iov[iovcnt].iov_len = if_len;
2799 if (host->hcps.prlist_val) {
2800 hdr.hcps = host->hcps.prlist_len;
2801 hcps_len = hdr.hcps * sizeof(afs_int32);
2802 hcps = (afs_int32 *) malloc(hcps_len);
2803 assert(hcps != NULL);
2804 memcpy(hcps, host->hcps.prlist_val, hcps_len);
2805 iov[iovcnt].iov_base = (char *) hcps;
2806 iov[iovcnt].iov_len = hcps_len;
2810 if (hdsk.index > state->h_hdr->index_max)
2811 state->h_hdr->index_max = hdsk.index;
2813 hdr.len = sizeof(struct host_state_entry_header) +
2814 sizeof(struct hostDiskEntry) + if_len + hcps_len;
2815 hdr.magic = HOST_STATE_ENTRY_MAGIC;
2817 iov[0].iov_base = (char *) &hdr;
2818 iov[0].iov_len = sizeof(hdr);
2819 iov[1].iov_base = (char *) &hdsk;
2820 iov[1].iov_len = sizeof(struct hostDiskEntry);
2822 if (fs_stateWriteV(state, iov, iovcnt)) {
2823 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
2827 fs_stateIncEOF(state, hdr.len);
2829 state->h_hdr->records++;
2837 return H_ENUMERATE_BAIL(held);
2842 /* restores a host from disk */
2844 h_stateRestoreHost(struct fs_dump_state * state)
2846 int ifp_len=0, hcps_len=0, bail=0;
2847 struct host_state_entry_header hdr;
2848 struct hostDiskEntry hdsk;
2849 struct host *host = NULL;
2850 struct Interface *ifp = NULL;
2851 afs_int32 * hcps = NULL;
2852 struct iovec iov[3];
2855 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
2856 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
2862 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
2863 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
2869 iov[0].iov_base = (char *) &hdsk;
2870 iov[0].iov_len = sizeof(struct hostDiskEntry);
2872 if (hdr.interfaces) {
2873 ifp_len = sizeof(struct Interface) +
2874 ((hdr.interfaces-1) * sizeof(struct AddrPort));
2875 ifp = (struct Interface *) malloc(ifp_len);
2876 assert(ifp != NULL);
2877 iov[iovcnt].iov_base = (char *) ifp;
2878 iov[iovcnt].iov_len = ifp_len;
2882 hcps_len = hdr.hcps * sizeof(afs_int32);
2883 hcps = (afs_int32 *) malloc(hcps_len);
2884 assert(hcps != NULL);
2885 iov[iovcnt].iov_base = (char *) hcps;
2886 iov[iovcnt].iov_len = hcps_len;
2890 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
2891 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
2896 if (fs_stateReadV(state, iov, iovcnt)) {
2897 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
2902 if (!hdr.hcps && hdsk.hcps_valid) {
2903 /* valid, zero-length host cps ; does this ever happen? */
2904 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
2905 assert(hcps != NULL);
2909 assert(host != NULL);
2912 host->interface = ifp;
2915 host->hcps.prlist_val = hcps;
2916 host->hcps.prlist_len = hdr.hcps;
2919 h_diskEntryToHost_r(&hdsk, host);
2920 h_SetupCallbackConn_r(host);
2924 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
2926 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
2928 h_AddHostToAddrHashTable_r(host->host, host->port, host);
2929 h_InsertList_r(host);
2931 /* setup host id map entry */
2932 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
2933 state->h_map.entries[hdsk.index].new_idx = host->index;
2945 /* serialize a host structure to disk */
2947 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
2949 out->host = in->host;
2950 out->port = in->port;
2951 out->hostFlags = in->hostFlags;
2952 out->Console = in->Console;
2953 out->hcpsfailed = in->hcpsfailed;
2954 out->LastCall = in->LastCall;
2955 out->ActiveCall = in->ActiveCall;
2956 out->cpsCall = in->cpsCall;
2957 out->cblist = in->cblist;
2958 #ifdef FS_STATS_DETAILED
2959 out->InSameNetwork = in->InSameNetwork;
2962 /* special fields we save, but are not memcpy'd back on restore */
2963 out->index = in->index;
2964 out->hcps_len = in->hcps.prlist_len;
2965 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
2968 /* restore a host structure from disk */
2970 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
2972 out->host = in->host;
2973 out->port = in->port;
2974 out->hostFlags = in->hostFlags;
2975 out->Console = in->Console;
2976 out->hcpsfailed = in->hcpsfailed;
2977 out->LastCall = in->LastCall;
2978 out->ActiveCall = in->ActiveCall;
2979 out->cpsCall = in->cpsCall;
2980 out->cblist = in->cblist;
2981 #ifdef FS_STATS_DETAILED
2982 out->InSameNetwork = in->InSameNetwork;
2986 /* index translation routines */
2988 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2992 /* hosts use a zero-based index, so old==0 is valid */
2994 if (old >= state->h_map.len) {
2995 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
2997 } else if (state->h_map.entries[old].old_idx != old) { /* sanity check */
2998 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
3001 *new = state->h_map.entries[old].new_idx;
3007 #endif /* AFS_DEMAND_ATTACH_FS */
3011 * This counts the number of workstations, the number of active workstations,
3012 * and the number of workstations declared "down" (i.e. not heard from
3013 * recently). An active workstation has received a call since the cutoff
3014 * time argument passed.
3017 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3019 register struct host *host;
3020 register int num = 0, active = 0, del = 0;
3023 for (host = hostList; host; host = host->next) {
3024 if (!(host->hostFlags & HOSTDELETED)) {
3026 if (host->ActiveCall > cutofftime)
3028 if (host->hostFlags & VENUSDOWN)
3040 } /*h_GetWorkStats */
3043 /*------------------------------------------------------------------------
3044 * PRIVATE h_ClassifyAddress
3047 * Given a target IP address and a candidate IP address (both
3048 * in host byte order), classify the candidate into one of three
3049 * buckets in relation to the target by bumping the counters passed
3053 * a_targetAddr : Target address.
3054 * a_candAddr : Candidate address.
3055 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3056 * addresses are either in the same network
3057 * or the same subnet.
3058 * a_diffSubnetP : ...when the candidate is in a different
3060 * a_diffNetworkP : ...when the candidate is in a different
3067 * The target and candidate addresses are both in host byte
3068 * order, NOT network byte order, when passed in.
3072 *------------------------------------------------------------------------*/
3075 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3076 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3077 afs_int32 * a_diffNetworkP)
3078 { /*h_ClassifyAddress */
3080 afs_uint32 targetNet;
3081 afs_uint32 targetSubnet;
3083 afs_uint32 candSubnet;
3086 * Put bad values into the subnet info to start with.
3088 targetSubnet = (afs_uint32) 0;
3089 candSubnet = (afs_uint32) 0;
3092 * Pull out the network and subnetwork numbers from the target
3093 * and candidate addresses. We can short-circuit this whole
3094 * affair if the target and candidate addresses are not of the
3097 if (IN_CLASSA(a_targetAddr)) {
3098 if (!(IN_CLASSA(a_candAddr))) {
3099 (*a_diffNetworkP)++;
3102 targetNet = a_targetAddr & IN_CLASSA_NET;
3103 candNet = a_candAddr & IN_CLASSA_NET;
3104 if (IN_SUBNETA(a_targetAddr))
3105 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
3106 if (IN_SUBNETA(a_candAddr))
3107 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
3108 } else if (IN_CLASSB(a_targetAddr)) {
3109 if (!(IN_CLASSB(a_candAddr))) {
3110 (*a_diffNetworkP)++;
3113 targetNet = a_targetAddr & IN_CLASSB_NET;
3114 candNet = a_candAddr & IN_CLASSB_NET;
3115 if (IN_SUBNETB(a_targetAddr))
3116 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
3117 if (IN_SUBNETB(a_candAddr))
3118 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
3119 } /*Class B target */
3120 else if (IN_CLASSC(a_targetAddr)) {
3121 if (!(IN_CLASSC(a_candAddr))) {
3122 (*a_diffNetworkP)++;
3125 targetNet = a_targetAddr & IN_CLASSC_NET;
3126 candNet = a_candAddr & IN_CLASSC_NET;
3129 * Note that class C addresses can't have subnets,
3130 * so we leave the defaults untouched.
3132 } /*Class C target */
3134 targetNet = a_targetAddr;
3135 candNet = a_candAddr;
3136 } /*Class D address */
3139 * Now, simply compare the extracted net and subnet values for
3140 * the two addresses (which at this point are known to be of the
3143 if (targetNet == candNet) {
3144 if (targetSubnet == candSubnet)
3145 (*a_sameNetOrSubnetP)++;
3149 (*a_diffNetworkP)++;
3151 } /*h_ClassifyAddress */
3154 /*------------------------------------------------------------------------
3155 * EXPORTED h_GetHostNetStats
3158 * Iterate through the host table, and classify each (non-deleted)
3159 * host entry into ``proximity'' categories (same net or subnet,
3160 * different subnet, different network).
3163 * a_numHostsP : Set to total number of (non-deleted) hosts.
3164 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
3165 * a_diffSubnetP : Set to # hosts on diff subnet as server.
3166 * a_diffNetworkP : Set to # hosts on diff network as server.
3172 * We only count non-deleted hosts. The storage pointed to by our
3173 * parameters is zeroed upon entry.
3177 *------------------------------------------------------------------------*/
3180 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
3181 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
3182 { /*h_GetHostNetStats */
3184 register struct host *hostP; /*Ptr to current host entry */
3185 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
3188 * Clear out the storage pointed to by our parameters.
3190 *a_numHostsP = (afs_int32) 0;
3191 *a_sameNetOrSubnetP = (afs_int32) 0;
3192 *a_diffSubnetP = (afs_int32) 0;
3193 *a_diffNetworkP = (afs_int32) 0;
3196 for (hostP = hostList; hostP; hostP = hostP->next) {
3197 if (!(hostP->hostFlags & HOSTDELETED)) {
3199 * Bump the number of undeleted host entries found.
3200 * In classifying the current entry's address, make
3201 * sure to first convert to host byte order.
3204 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
3205 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
3206 a_sameNetOrSubnetP, a_diffSubnetP,
3208 } /*Only look at non-deleted hosts */
3209 } /*For each host record hashed to this index */
3211 } /*h_GetHostNetStats */
3213 static afs_uint32 checktime;
3214 static afs_uint32 clientdeletetime;
3215 static struct AFSFid zerofid;
3219 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
3220 * Since it can serialize them, and pile up, it should be a separate LWP
3221 * from other events.
3224 CheckHost(register struct host *host, int held)
3226 register struct client *client;
3227 struct rx_connection *cb_conn = NULL;
3230 #ifdef AFS_DEMAND_ATTACH_FS
3231 /* kill the checkhost lwp ASAP during shutdown */
3233 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3235 return H_ENUMERATE_BAIL(held);
3240 /* Host is held by h_Enumerate */
3242 for (client = host->FirstClient; client; client = client->next) {
3243 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3244 client->deleted = 1;
3245 host->hostFlags |= CLIENTDELETED;
3248 if (host->LastCall < checktime) {
3250 host->hostFlags |= HWHO_INPROGRESS;
3251 if (!(host->hostFlags & HOSTDELETED)) {
3252 cb_conn = host->callback_rxcon;
3253 rx_GetConnection(cb_conn);
3254 if (host->LastCall < clientdeletetime) {
3255 host->hostFlags |= HOSTDELETED;
3256 if (!(host->hostFlags & VENUSDOWN)) {
3257 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3258 if (host->interface) {
3261 RXAFSCB_InitCallBackState3(cb_conn,
3267 RXAFSCB_InitCallBackState(cb_conn);
3270 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3273 (void)afs_inet_ntoa_r(host->host, hoststr);
3275 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3276 hoststr, ntohs(host->port)));
3277 host->hostFlags |= VENUSDOWN;
3279 /* Note: it's safe to delete hosts even if they have call
3280 * back state, because break delayed callbacks (called when a
3281 * message is received from the workstation) will always send a
3282 * break all call backs to the workstation if there is no
3287 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3289 (void)afs_inet_ntoa_r(host->host, hoststr);
3290 if (host->interface) {
3291 afsUUID uuid = host->interface->uuid;
3293 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3296 if (MultiProbeAlternateAddress_r(host)) {
3297 ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
3298 hoststr, ntohs(host->port), code));
3299 host->hostFlags |= VENUSDOWN;
3304 code = RXAFSCB_Probe(cb_conn);
3308 ("CheckHost: Probe failed for host %s:%d, code %d\n",
3309 hoststr, ntohs(host->port), code));
3310 host->hostFlags |= VENUSDOWN;
3316 rx_PutConnection(cb_conn);
3320 host->hostFlags &= ~HWHO_INPROGRESS;
3329 CheckHost_r(register struct host *host, int held, char *dummy)
3331 register struct client *client;
3332 struct rx_connection *cb_conn = NULL;
3335 /* Host is held by h_Enumerate_r */
3336 for (client = host->FirstClient; client; client = client->next) {
3337 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3338 client->deleted = 1;
3339 host->hostFlags |= CLIENTDELETED;
3342 if (host->LastCall < checktime) {
3344 if (!(host->hostFlags & HOSTDELETED)) {
3345 cb_conn = host->callback_rxcon;
3346 rx_GetConnection(cb_conn);
3347 if (host->LastCall < clientdeletetime) {
3348 host->hostFlags |= HOSTDELETED;
3349 if (!(host->hostFlags & VENUSDOWN)) {
3350 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3351 if (host->interface) {
3354 RXAFSCB_InitCallBackState3(cb_conn,
3360 RXAFSCB_InitCallBackState(cb_conn);
3363 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3366 (void)afs_inet_ntoa_r(host->host, hoststr);
3368 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3369 hoststr, ntohs(host->port)));
3370 host->hostFlags |= VENUSDOWN;
3372 /* Note: it's safe to delete hosts even if they have call
3373 * back state, because break delayed callbacks (called when a
3374 * message is received from the workstation) will always send a
3375 * break all call backs to the workstation if there is no
3380 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3382 (void)afs_inet_ntoa_r(host->host, hoststr);
3383 if (host->interface) {
3384 afsUUID uuid = host->interface->uuid;
3386 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3389 if (MultiProbeAlternateAddress_r(host)) {
3390 ViceLog(0,("CheckHost_r: Probing all interfaces of host %s:%d failed, code %d\n",
3391 hoststr, ntohs(host->port), code));
3392 host->hostFlags |= VENUSDOWN;
3397 code = RXAFSCB_Probe(cb_conn);
3401 ("CheckHost_r: Probe failed for host %s:%d, code %d\n",
3402 hoststr, ntohs(host->port), code));
3403 host->hostFlags |= VENUSDOWN;
3409 rx_PutConnection(cb_conn);
3421 * Set VenusDown for any hosts that have not had a call in 15 minutes and
3422 * don't respond to a probe. Note that VenusDown can only be cleared if
3423 * a message is received from the host (see ServerLWP in file.c).
3424 * Delete hosts that have not had any calls in 1 hour, clients that
3425 * have not had any calls in 15 minutes.
3427 * This routine is called roughly every 5 minutes.
3432 afs_uint32 now = FT_ApproxTime();
3434 memset((char *)&zerofid, 0, sizeof(zerofid));
3436 * Send a probe to the workstation if it hasn't been heard from in
3439 checktime = now - 15 * 60;
3440 clientdeletetime = now - 120 * 60; /* 2 hours ago */
3443 h_Enumerate_r(CheckHost_r, hostList, NULL);
3448 * This is called with host locked and held. At this point, the
3449 * hostAddrHashTable should not have any entries for the alternate
3450 * interfaces. This function has to insert these entries in the
3451 * hostAddrHashTable.
3453 * The addresses in the interfaceAddr list are in host byte order.
3456 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
3463 struct Interface *interface;
3466 afs_uint16 port7001 = htons(7001);
3471 number = interf->numberOfInterfaces;
3472 myAddr = host->host; /* current interface address */
3473 myPort = host->port; /* current port */
3476 ("initInterfaceAddr : host %s:%d numAddr %d\n",
3477 afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
3479 /* validation checks */
3480 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
3482 ("Invalid number of alternate addresses is %d\n", number));
3487 * The client's notion of its own IP addresses is not reliable.
3489 * 1. The client list might contain private address ranges which
3490 * are likely to be re-used by many clients allocated addresses
3493 * 2. The client list will not include any public addresses that
3494 * are hidden by a NAT.
3496 * 3. Private address ranges that are exposed to the server will
3497 * be obtained from the rx connections that use them.
3499 * 4. Lists provided by the client are not necessarily truthful.
3500 * Many existing clients (UNIX) do not refresh the IP address
3501 * list as the actual assigned addresses change. The end result
3502 * is that they report the initial address list for the lifetime
3503 * of the process. In other words, a client can report addresses
3504 * that they are in fact not using. Adding these addresses to
3505 * the host interface list without verification is not only
3506 * pointless, it is downright dangerous.
3508 * We therefore do not add alternate addresses to the addr hash table.
3509 * We only use them for multi-rx callback breaks.
3513 * Convert IP addresses to network byte order, and remove
3514 * duplicate IP addresses from the interface list, and
3515 * determine whether or not the incoming addr/port is
3516 * listed. Note that if the address matches it is not
3517 * truly a match because the port number for the entries
3518 * in the interface list are port 7001 and the port number
3519 * for this connection might not be 7001.
3521 for (i = 0, count = 0, found = 0; i < number; i++) {
3522 interf->addr_in[i] = htonl(interf->addr_in[i]);
3523 for (j = 0; j < count; j++) {
3524 if (interf->addr_in[j] == interf->addr_in[i])
3528 interf->addr_in[count] = interf->addr_in[i];
3529 if (interf->addr_in[count] == myAddr &&
3537 * Allocate and initialize an interface structure for this host.
3540 interface = (struct Interface *)
3541 malloc(sizeof(struct Interface) +
3542 (sizeof(struct AddrPort) * (count - 1)));
3544 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
3547 interface->numberOfInterfaces = count;
3549 interface = (struct Interface *)
3550 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
3552 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
3555 interface->numberOfInterfaces = count + 1;
3556 interface->interface[count].addr = myAddr;
3557 interface->interface[count].port = myPort;
3558 interface->interface[count].valid = 1;
3561 for (i = 0; i < count; i++) {
3563 interface->interface[i].addr = interf->addr_in[i];
3564 /* We store the port as 7001 because the addresses reported by
3565 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
3566 * are coming from fully connected hosts (no NAT/PATs)
3568 interface->interface[i].port = port7001;
3569 interface->interface[i].valid = 1; /* valid until a conflict is found */
3572 interface->uuid = interf->uuid;
3574 assert(!host->interface);
3575 host->interface = interface;
3577 afsUUID_to_string(&interface->uuid, uuidstr, 127);
3579 ViceLog(125, ("--- uuid %s\n", uuidstr));
3580 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
3581 ViceLog(125, ("--- alt address %s:%d\n",
3582 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3583 ntohs(host->interface->interface[i].port)));
3589 /* deleted a HashChain structure for this address and host */
3590 /* returns 1 on success */
3592 h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
3595 register struct h_AddrHashChain **hp, *th;
3597 for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);) {
3598 assert(th->hostPtr);
3599 if (th->hostPtr == host && th->addr == addr && th->port == port) {
3613 ** prints out all alternate interface address for the host. The 'level'
3614 ** parameter indicates what level of debugging sets this output
3617 printInterfaceAddr(struct host *host, int level)
3622 if (host->interface) {
3623 /* check alternate addresses */
3624 number = host->interface->numberOfInterfaces;
3626 ViceLog(level, ("no-addresses "));
3628 for (i = 0; i < number; i++)
3629 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3630 ntohs(host->interface->interface[i].port)));
3633 ViceLog(level, ("\n"));