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>
19 #ifdef HAVE_SYS_FILE_H
26 #include <afs/afsint.h>
27 #define FSINT_COMMON_XG
28 #include <afs/afscbint.h>
29 #include <afs/rxgen_consts.h>
31 #include <afs/errors.h>
32 #include <afs/ihandle.h>
33 #include <afs/vnode.h>
34 #include <afs/volume.h>
36 #include <afs/ptclient.h>
37 #include <afs/ptuser.h>
38 #include <afs/prs_fs.h>
40 #include <afs/afsutil.h>
41 #include <afs/com_err.h>
43 #include <afs/cellconfig.h>
44 #include "viced_prototypes.h"
48 #ifdef AFS_DEMAND_ATTACH_FS
49 #include "../util/afsutil_prototypes.h"
50 #include "serialize_state.h"
51 #endif /* AFS_DEMAND_ATTACH_FS */
53 pthread_mutex_t host_glock_mutex;
56 extern int CurrentConnections;
58 extern int AnonymousID;
59 extern prlist AnonCPS;
61 extern struct afsconf_dir *confDir; /* config dir object */
62 extern int lwps; /* the max number of server threads */
63 extern afsUUID FS_HostUUID;
66 int CEs = 0; /* active clients */
67 int CEBlocks = 0; /* number of blocks of CEs */
68 struct client *CEFree = 0; /* first free client */
69 struct host *hostList = 0; /* linked list of all hosts */
70 int hostCount = 0; /* number of hosts in hostList */
74 static struct rx_securityClass *sc = NULL;
76 static void h_SetupCallbackConn_r(struct host * host);
77 static int h_threadquota(int);
79 #define CESPERBLOCK 73
80 struct CEBlock { /* block of CESPERBLOCK file entries */
81 struct client entry[CESPERBLOCK];
84 void h_TossStuff_r(struct host *host);
87 * Make sure the subnet macros have been defined.
90 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
93 #ifndef IN_CLASSA_SUBNET
94 #define IN_CLASSA_SUBNET 0xffff0000
98 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
101 #ifndef IN_CLASSB_SUBNET
102 #define IN_CLASSB_SUBNET 0xffffff00
106 /* get a new block of CEs and chain it on CEFree */
110 struct CEBlock *block;
113 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
115 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
116 ShutDownAndCore(PANIC);
119 for (i = 0; i < (CESPERBLOCK - 1); i++) {
120 Lock_Init(&block->entry[i].lock);
121 block->entry[i].next = &(block->entry[i + 1]);
123 block->entry[CESPERBLOCK - 1].next = 0;
124 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
125 CEFree = (struct client *)block;
131 /* get the next available CE */
132 static struct client *
135 struct client *entry;
140 ViceLog(0, ("CEFree NULL in GetCE\n"));
141 ShutDownAndCore(PANIC);
145 CEFree = entry->next;
147 memset(entry, 0, CLIENT_TO_ZERO(entry));
153 /* return an entry to the free list */
155 FreeCE(struct client *entry)
157 entry->VenusEpoch = 0;
159 entry->next = CEFree;
166 * The HTs and HTBlocks variables were formerly static, but they are
167 * now referenced elsewhere in the FileServer.
169 int HTs = 0; /* active file entries */
170 int HTBlocks = 0; /* number of blocks of HTs */
171 static struct host *HTFree = 0; /* first free file entry */
174 * Hash tables of host pointers. We need two tables, one
175 * to map IP addresses onto host pointers, and another
176 * to map host UUIDs onto host pointers.
178 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
179 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
180 #define h_HashIndex(hostip) (ntohl(hostip) & (h_HASHENTRIES-1))
181 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
183 struct HTBlock { /* block of HTSPERBLOCK file entries */
184 struct host entry[h_HTSPERBLOCK];
188 /* get a new block of HTs and chain it on HTFree */
192 struct HTBlock *block;
194 static int index = 0;
196 if (HTBlocks == h_MAXHOSTTABLES) {
197 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
201 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
203 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
204 ShutDownAndCore(PANIC);
206 for (i = 0; i < (h_HTSPERBLOCK); i++)
207 CV_INIT(&block->entry[i].cond, "block entry", CV_DEFAULT, 0);
208 for (i = 0; i < (h_HTSPERBLOCK); i++)
209 Lock_Init(&block->entry[i].lock);
210 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
211 block->entry[i].next = &(block->entry[i + 1]);
212 for (i = 0; i < (h_HTSPERBLOCK); i++)
213 block->entry[i].index = index++;
214 block->entry[h_HTSPERBLOCK - 1].next = 0;
215 HTFree = (struct host *)block;
216 hosttableptrs[HTBlocks++] = block->entry;
221 /* get the next available HT */
232 HTFree = entry->next;
234 memset(entry, 0, HOST_TO_ZERO(entry));
240 /* return an entry to the free list */
242 FreeHT(struct host *entry)
244 entry->next = HTFree;
251 hpr_Initialize(struct ubik_client **uclient)
254 struct rx_connection *serverconns[MAXSERVERS];
255 struct rx_securityClass *sc;
256 struct afsconf_dir *tdir;
258 struct afsconf_cell info;
262 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
264 ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
268 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
270 ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]", code));
275 code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
277 ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s",
278 cellstr, tdir->name, AFSDIR_CELLSERVDB_FILE));
285 ViceLog(0, ("hpr_Initialize: Could not initialize rx."));
290 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
291 * to force use of the KeyFile. secLevel == 0 implies -noauth was
293 code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
295 ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s "
296 "(so trying noauth)", code, afs_error_message(code)));
297 scIndex = RX_SECIDX_NULL;
298 sc = rxnull_NewClientSecurityObject();
301 if (scIndex == RX_SECIDX_NULL)
302 ViceLog(0, ("hpr_Initialize: Could not get afs tokens, "
303 "running unauthenticated. [%d]", code));
305 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
306 for (i = 0; i < info.numServers; i++) {
308 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
309 info.hostAddr[i].sin_port, PRSRV,
313 code = ubik_ClientInit(serverconns, uclient);
315 ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
318 code = rxs_Release(sc);
323 hpr_End(struct ubik_client *uclient)
328 code = ubik_ClientDestroy(uclient);
334 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
338 struct ubik_client *uclient =
339 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
342 code = hpr_Initialize(&uclient);
344 osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
350 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
351 if (code != PRSUCCESS)
354 /* do something about this, probably make a new call */
355 /* don't forget there's a hard limit in the interface */
357 "membership list for host id %d exceeds display limit\n",
364 hpr_NameToId(namelist *names, idlist *ids)
368 struct ubik_client *uclient =
369 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
372 code = hpr_Initialize(&uclient);
374 osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
379 for (i = 0; i < names->namelist_len; i++)
380 stolower(names->namelist_val[i]);
381 code = ubik_PR_NameToID(uclient, 0, names, ids);
386 hpr_IdToName(idlist *ids, namelist *names)
389 struct ubik_client *uclient =
390 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
393 code = hpr_Initialize(&uclient);
395 osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
400 code = ubik_PR_IDToName(uclient, 0, ids, names);
405 hpr_GetCPS(afs_int32 id, prlist *CPS)
409 struct ubik_client *uclient =
410 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
413 code = hpr_Initialize(&uclient);
415 osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
421 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
422 if (code != PRSUCCESS)
425 /* do something about this, probably make a new call */
426 /* don't forget there's a hard limit in the interface */
427 fprintf(stderr, "membership list for id %d exceeds display limit\n",
433 static short consolePort = 0;
436 h_Lock_r(struct host *host)
446 * returns 1 if already locked
447 * else returns locks and returns 0
451 h_NBLock_r(struct host *host)
453 struct Lock *hostLock = &host->lock;
458 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
459 hostLock->excl_locked = WRITE_LOCK;
463 LOCK_UNLOCK(hostLock);
472 /*------------------------------------------------------------------------
473 * PRIVATE h_AddrInSameNetwork
476 * Given a target IP address and a candidate IP address (both
477 * in host byte order), return a non-zero value (1) if the
478 * candidate address is in a different network from the target
482 * a_targetAddr : Target address.
483 * a_candAddr : Candidate address.
486 * 1 if the candidate address is in the same net as the target,
490 * The target and candidate addresses are both in host byte
491 * order, NOT network byte order, when passed in. We return
492 * our value as a character, since that's the type of field in
493 * the host structure, where this info will be stored.
497 *------------------------------------------------------------------------*/
500 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
501 { /*h_AddrInSameNetwork */
503 afs_uint32 targetNet;
507 * Pull out the network and subnetwork numbers from the target
508 * and candidate addresses. We can short-circuit this whole
509 * affair if the target and candidate addresses are not of the
512 if (IN_CLASSA(a_targetAddr)) {
513 if (!(IN_CLASSA(a_candAddr))) {
516 targetNet = a_targetAddr & IN_CLASSA_NET;
517 candNet = a_candAddr & IN_CLASSA_NET;
518 } else if (IN_CLASSB(a_targetAddr)) {
519 if (!(IN_CLASSB(a_candAddr))) {
522 targetNet = a_targetAddr & IN_CLASSB_NET;
523 candNet = a_candAddr & IN_CLASSB_NET;
524 } /*Class B target */
525 else if (IN_CLASSC(a_targetAddr)) {
526 if (!(IN_CLASSC(a_candAddr))) {
529 targetNet = a_targetAddr & IN_CLASSC_NET;
530 candNet = a_candAddr & IN_CLASSC_NET;
531 } /*Class C target */
533 targetNet = a_targetAddr;
534 candNet = a_candAddr;
535 } /*Class D address */
538 * Now, simply compare the extracted net values for the two addresses
539 * (which at this point are known to be of the same class)
541 if (targetNet == candNet)
546 } /*h_AddrInSameNetwork */
549 /* Assumptions: called with held host */
551 h_gethostcps_r(struct host *host, afs_int32 now)
556 /* wait if somebody else is already doing the getCPS call */
557 while (host->hostFlags & HCPS_INPROGRESS) {
558 slept = 1; /* I did sleep */
559 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
560 CV_WAIT(&host->cond, &host_glock_mutex);
564 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
565 if (host->hcps.prlist_val)
566 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
567 host->hcps.prlist_val = NULL;
568 host->hcps.prlist_len = 0;
569 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
572 code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
577 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
578 * and reevaluates things if no sync server or quorum is found we could still end up
579 * with one of these errors. In such case we would like to reevaluate the rpc call to
580 * find if there's cps for this guy. We treat other errors (except network failures
581 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
582 * we could like to deal this problem the other way around (i.e. if code == NOCPS
583 * ignore else retry next time) but the problem is that there're other errors (i.e.
584 * EPERM) for which we don't want to retry and we don't know the whole code list!
586 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
588 * We would have preferred to use a while loop and try again since ops in protected
589 * acls for this host will fail now but they'll be reevaluated on any subsequent
590 * call. The attempt to wait for a quorum/sync site or network error won't work
591 * since this problems really should only occurs during a complete fileserver
592 * restart. Since the fileserver will start before the ptservers (and thus before
593 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
595 host->hcpsfailed = 1;
597 ("Warning: GetHostCPS failed (%d) for %p (%s:%d); will retry\n",
598 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
600 host->hcpsfailed = 0;
602 ("gethost: GetHostCPS failed (%d) for %p (%s:%d); ignored\n",
603 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
605 if (host->hcps.prlist_val)
606 free(host->hcps.prlist_val);
607 host->hcps.prlist_val = NULL;
608 host->hcps.prlist_len = 0; /* Make sure it's zero */
610 host->hcpsfailed = 0;
612 host->hostFlags &= ~HCPS_INPROGRESS;
613 /* signal all who are waiting */
614 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
615 host->hostFlags &= ~HCPS_WAITING;
616 CV_BROADCAST(&host->cond);
620 /* args in net byte order */
622 h_flushhostcps(afs_uint32 hostaddr, afs_uint16 hport)
627 h_Lookup_r(hostaddr, hport, &host);
629 host->hcpsfailed = 1;
638 * Allocate a host. It will be identified by the peer (ip,port) info in the
639 * rx connection provided. The host is returned held and locked
641 #define DEF_ROPCONS 2115
644 h_Alloc_r(struct rx_connection *r_con)
646 struct servent *serverentry;
648 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
655 /* acquire the host lock withot dropping H_LOCK. we can do this here
656 * because we know we will not block; we just created this host and
657 * nobody else knows about it. */
658 ObtainWriteLock(&host->lock);
660 host->host = rxr_HostOf(r_con);
661 host->port = rxr_PortOf(r_con);
663 h_AddHostToAddrHashTable_r(host->host, host->port, host);
665 if (consolePort == 0) { /* find the portal number for console */
666 #if defined(AFS_OSF_ENV)
667 serverentry = getservbyname("ropcons", "");
669 serverentry = getservbyname("ropcons", 0);
672 consolePort = serverentry->s_port;
674 consolePort = htons(DEF_ROPCONS); /* Use a default */
676 if (host->port == consolePort)
678 /* Make a callback channel even for the console, on the off chance that it
679 * makes a request that causes a break call back. It shouldn't. */
680 h_SetupCallbackConn_r(host);
681 host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
683 host->hcps.prlist_val = NULL;
684 host->hcps.prlist_len = 0;
685 host->interface = NULL;
687 host->hcpsfailed = 0; /* save cycles */
688 h_gethostcps(host); /* do this under host hold/lock */
690 host->FirstClient = NULL;
691 h_InsertList_r(host); /* update global host List */
693 * Compare the new host's IP address (in host byte order) with ours
694 * (the File Server's), remembering if they are in the same network.
696 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
697 host->InSameNetwork =
698 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
705 /* Make a callback channel even for the console, on the off chance that it
706 * makes a request that causes a break call back. It shouldn't. */
708 h_SetupCallbackConn_r(struct host * host)
711 sc = rxnull_NewClientSecurityObject();
712 host->callback_rxcon =
713 rx_NewConnection(host->host, host->port, 1, sc, 0);
714 rx_SetConnDeadTime(host->callback_rxcon, 50);
715 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
716 rx_SetConnSecondsUntilNatPing(host->callback_rxcon, 20);
720 * Lookup a host given an IP address and UDP port number.
721 * hostaddr and hport are in network order
722 * hostaddr and hport are in network order
723 * On return, refCount is incremented.
726 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
729 struct host *host = NULL;
730 struct h_AddrHashChain *chain;
731 int index = h_HashIndex(haddr);
732 extern int hostaclRefresh;
735 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
736 host = chain->hostPtr;
738 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
739 && chain->port == hport) {
740 if ((host->hostFlags & HWHO_INPROGRESS) &&
741 h_threadquota(host->lock.num_waiting)) {
747 if (host->hostFlags & HOSTDELETED) {
754 now = FT_ApproxTime(); /* always evaluate "now" */
755 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
757 * Every hostaclRefresh period (def 2 hrs) get the new
758 * membership list for the host. Note this could be the
759 * first time that the host is added to a group. Also
760 * here we also retry on previous legitimate hcps failures.
762 * If we get here refCount is elevated.
764 h_gethostcps_r(host, now);
774 /* Lookup a host given its UUID. */
776 h_LookupUuid_r(afsUUID * uuidp)
778 struct host *host = 0;
779 struct h_UuidHashChain *chain;
780 int index = h_UuidHashIndex(uuidp);
782 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
783 host = chain->hostPtr;
785 if (!(host->hostFlags & HOSTDELETED) && host->interface
786 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
794 /* h_TossStuff_r: Toss anything in the host structure (the host or
795 * clients marked for deletion. Called from h_Release_r ONLY.
796 * To be called, there must be no holds, and either host->deleted
797 * or host->clientDeleted must be set.
800 h_TossStuff_r(struct host *host)
802 struct client **cp, *client;
806 if ((host->hostFlags & HOSTDELETED)) {
810 /* make sure host doesn't go away over h_NBLock_r */
813 code = h_NBLock_r(host);
815 /* don't use h_Release_r, since that may call h_TossStuff_r again */
818 /* if somebody still has this host locked */
822 /* someone locked the host while HOSTDELETED was set; that is bad */
823 ViceLog(0, ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT
824 " (%s:%d flags 0x%x) was locked.\n",
825 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
826 (unsigned)host->hostFlags));
833 /* if somebody still has this host held */
834 /* we must check this _after_ h_NBLock_r, since h_NBLock_r can drop and
835 * reacquire H_LOCK */
836 if (host->refCount > 0) {
839 /* someone grabbed a ref while HOSTDELETED was set; that is bad */
840 ViceLog(0, ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT
841 " (%s:%d flags 0x%x) was held.\n",
842 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
843 (unsigned)host->hostFlags));
848 /* ASSUMPTION: rxi_FreeConnection() does not yield */
849 for (cp = &host->FirstClient; (client = *cp);) {
850 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
852 ObtainWriteLockNoBlock(&client->lock, code);
856 ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
857 "client %p was locked.\n",
858 host, afs_inet_ntoa_r(host->host, hoststr),
859 ntohs(host->port), client));
863 if (client->refCount) {
866 ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
867 "client %p refcount %d.\n",
868 host, afs_inet_ntoa_r(host->host, hoststr),
869 ntohs(host->port), client, client->refCount));
870 /* This is the same thing we do if the host is locked */
871 ReleaseWriteLock(&client->lock);
874 client->CPS.prlist_len = 0;
875 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
876 free(client->CPS.prlist_val);
877 client->CPS.prlist_val = NULL;
878 CurrentConnections--;
880 ReleaseWriteLock(&client->lock);
886 /* We've just cleaned out all the deleted clients; clear the flag */
887 host->hostFlags &= ~CLIENTDELETED;
889 if (host->hostFlags & HOSTDELETED) {
890 struct rx_connection *rxconn;
891 struct AddrPort hostAddrPort;
894 if (host->Console & 1)
896 if ((rxconn = host->callback_rxcon)) {
897 host->callback_rxcon = (struct rx_connection *)0;
898 rx_DestroyConnection(rxconn);
900 if (host->hcps.prlist_val)
901 free(host->hcps.prlist_val);
902 host->hcps.prlist_val = NULL;
903 host->hcps.prlist_len = 0;
904 DeleteAllCallBacks_r(host, 1);
905 host->hostFlags &= ~RESETDONE; /* just to be safe */
907 /* if alternate addresses do not exist */
908 if (!(host->interface)) {
909 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
911 h_DeleteHostFromUuidHashTable_r(host);
912 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
913 /* delete the hash entry for each valid alternate addresses */
914 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
915 hostAddrPort = host->interface->interface[i];
917 * if the interface addr/port is the primary, we already
918 * removed it. If the addr/port is not valid, its not
921 if (hostAddrPort.valid &&
922 (host->host != hostAddrPort.addr ||
923 host->port != hostAddrPort.port))
924 h_DeleteHostFromAddrHashTable_r(hostAddrPort.addr, hostAddrPort.port, host);
926 free(host->interface);
927 host->interface = NULL;
928 } /* if alternate address exists */
930 h_DeleteList_r(host); /* remove host from global host List */
937 /* h_Enumerate: Calls (*proc)(host, param) for at least each host in the
938 * system at the start of the enumeration (perhaps more). Hosts may be deleted
939 * (have delete flag set); ditto for clients. refCount is always incremented
940 * before (*proc) is called.
942 * The return value of the proc is a set of flags. The proc should set
943 * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
946 h_Enumerate(int (*proc) (struct host*, void *), void *param)
948 struct host *host, **list;
953 if (hostCount == 0) {
957 list = (struct host **)malloc(hostCount * sizeof(struct host *));
959 ViceLogThenPanic(0, ("Failed malloc in h_Enumerate (list)\n"));
961 for (totalCount = count = 0, host = hostList;
962 host && totalCount < hostCount;
963 host = host->next, totalCount++) {
965 if (!(host->hostFlags & HOSTDELETED)) {
971 if (totalCount != hostCount) {
972 ViceLog(0, ("h_Enumerate found %d of %d hosts\n", totalCount, hostCount));
973 } else if (host != NULL) {
974 ViceLog(0, ("h_Enumerate found more than %d hosts\n", hostCount));
975 ShutDownAndCore(PANIC);
978 for (i = 0; i < count; i++) {
980 flags = (*proc) (list[i], param);
982 h_Release_r(list[i]);
984 /* bail out of the enumeration early */
985 if (H_ENUMERATE_ISSET_BAIL(flags)) {
988 ViceLog(0, ("h_Enumerate got back invalid return value %d\n", flags));
989 ShutDownAndCore(PANIC);
993 /* we bailed out of enumerating hosts early; we still have holds on
994 * some of the hosts in 'list', so release them */
997 for ( ; i < count; i++) {
998 h_Release_r(list[i]);
1006 /* h_Enumerate_r (revised):
1007 * Calls (*proc)(host, param) for each host in hostList, starting
1008 * at enumstart. Called only under H_LOCK. Hosts may be deleted (have
1009 * delete flag set); ditto for clients. refCount is always incremented
1010 * before (*proc) is called.
1012 * @note Assumes that hostList is only prepended to, that a host is never
1013 * inserted into the middle. Otherwise this would not be guaranteed to
1016 * The return value of the proc is a set of flags. The proc should set
1017 * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
1020 h_Enumerate_r(int (*proc) (struct host *, void *),
1021 struct host *enumstart, void *param)
1023 struct host *host, *next;
1027 if (hostCount == 0) {
1034 /* find the first non-deleted host, so we know where to actually start
1036 for (count = 0; host && count < hostCount; count++) {
1037 if (!(host->hostFlags & HOSTDELETED)) {
1044 /* we didn't find a non-deleted host... */
1046 if (host && count >= hostCount) {
1047 /* ...because we found a loop */
1048 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", hostCount));
1049 ShutDownAndCore(PANIC);
1052 /* ...because the hostList is full of deleted hosts */
1056 h_Hold_r(enumstart);
1058 /* remember hostCount, lest it change over the potential H_LOCK drop in
1060 origHostCount = hostCount;
1062 for (count = 0, host = enumstart; host && count < origHostCount; host = next, count++) {
1065 /* find the next non-deleted host */
1066 while (next && (next->hostFlags & HOSTDELETED)) {
1068 /* inc count for the skipped-over host */
1069 if (++count > origHostCount) {
1070 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", origHostCount));
1071 ShutDownAndCore(PANIC);
1077 if (!(host->hostFlags & HOSTDELETED)) {
1079 flags = (*proc) (host, param);
1080 if (H_ENUMERATE_ISSET_BAIL(flags)) {
1081 h_Release_r(host); /* this might free up the host */
1087 ViceLog(0, ("h_Enumerate_r got back invalid return value %d\n", flags));
1088 ShutDownAndCore(PANIC);
1091 h_Release_r(host); /* this might free up the host */
1093 if (host != NULL && count >= origHostCount) {
1094 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", origHostCount));
1095 ShutDownAndCore(PANIC);
1097 } /*h_Enumerate_r */
1100 /* inserts a new HashChain structure corresponding to this UUID */
1102 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1105 struct h_UuidHashChain *chain;
1106 char uuid1[128], uuid2[128];
1109 /* hash into proper bucket */
1110 index = h_UuidHashIndex(uuid);
1112 /* don't add the same entry multiple times */
1113 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1114 if (!chain->hostPtr)
1117 if (chain->hostPtr->interface &&
1118 afs_uuid_equal(&chain->hostPtr->interface->uuid, uuid)) {
1119 if (LogLevel >= 125) {
1120 afsUUID_to_string(&chain->hostPtr->interface->uuid, uuid1,
1122 afsUUID_to_string(uuid, uuid2, 127);
1123 ViceLog(125, ("h_AddHostToUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s) exists as %s:%d (uuid %s)\n",
1125 afs_inet_ntoa_r(chain->hostPtr->host, hoststr),
1126 ntohs(chain->hostPtr->port), uuid2));
1132 /* insert into beginning of list for this bucket */
1133 chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
1135 ViceLogThenPanic(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1137 chain->hostPtr = host;
1138 chain->next = hostUuidHashTable[index];
1139 hostUuidHashTable[index] = chain;
1142 afsUUID_to_string(uuid, uuid2, 127);
1144 ("h_AddHostToUuidHashTable_r: host %p (%s:%d) added as uuid %s\n",
1145 host, afs_inet_ntoa_r(chain->hostPtr->host, hoststr),
1146 ntohs(chain->hostPtr->port), uuid2));
1149 /* deletes a HashChain structure corresponding to this host */
1151 h_DeleteHostFromUuidHashTable_r(struct host *host)
1154 struct h_UuidHashChain **uhp, *uth;
1158 if (!host->interface)
1161 /* hash into proper bucket */
1162 index = h_UuidHashIndex(&host->interface->uuid);
1164 if (LogLevel >= 125)
1165 afsUUID_to_string(&host->interface->uuid, uuid1, 127);
1166 for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
1167 osi_Assert(uth->hostPtr);
1168 if (uth->hostPtr == host) {
1170 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d)\n",
1171 host, uuid1, afs_inet_ntoa_r(host->host, hoststr),
1172 ntohs(host->port)));
1179 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d) not found\n",
1180 host, uuid1, afs_inet_ntoa_r(host->host, hoststr),
1181 ntohs(host->port)));
1186 * This is called with host locked and held.
1188 * All addresses are in network byte order.
1191 invalidateInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1195 struct Interface *interface;
1196 char hoststr[16], hoststr2[16];
1199 osi_Assert(host->interface);
1201 ViceLog(125, ("invalidateInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1202 host, afs_inet_ntoa_r(host->host, hoststr),
1203 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1207 * Make sure this address is on the list of known addresses
1210 interface = host->interface;
1211 number = host->interface->numberOfInterfaces;
1212 for (i = 0; i < number; i++) {
1213 if (interface->interface[i].addr == addr &&
1214 interface->interface[i].port == port) {
1215 if (interface->interface[i].valid) {
1216 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1217 interface->interface[i].valid = 0;
1228 * This is called with host locked and held. This function differs
1229 * from removeInterfaceAddr_r in that it is called when the address
1230 * is being removed from the host regardless of whether or not there
1231 * is an interface list for the host. This function will delete the
1232 * host if there are no addresses left on it.
1234 * All addresses are in network byte order.
1237 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1240 char hoststr[16], hoststr2[16];
1241 struct rx_connection *rxconn;
1243 if (!host->interface || host->interface->numberOfInterfaces == 1) {
1244 if (host->host == addr && host->port == port) {
1246 ("Removing only address for host %" AFS_PTR_FMT " (%s:%d), deleting host.\n",
1247 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1248 host->hostFlags |= HOSTDELETED;
1250 * Do not remove the primary addr/port from the hash table.
1251 * It will be ignored due to the HOSTDELETED flag and will
1252 * be removed when h_TossStuff_r() cleans up the HOSTDELETED
1253 * host. Removing it here will only result in a search for
1254 * the host/addr/port in the hash chain which will fail.
1258 ("Removing address that does not belong to host %" AFS_PTR_FMT " (%s:%d).\n",
1259 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1262 if (host->host == addr && host->port == port) {
1263 removeInterfaceAddr_r(host, addr, port);
1265 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1266 if (host->interface->interface[i].valid) {
1268 ("Removed address for host %" AFS_PTR_FMT " (%s:%d), new primary interface %s:%d.\n",
1269 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1270 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2),
1271 ntohs(host->interface->interface[i].port)));
1272 host->host = host->interface->interface[i].addr;
1273 host->port = host->interface->interface[i].port;
1274 h_AddHostToAddrHashTable_r(host->host, host->port, host);
1279 if (i == host->interface->numberOfInterfaces) {
1281 ("Removed only address for host %" AFS_PTR_FMT " (%s:%d), no valid alternate interfaces, deleting host.\n",
1282 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1283 host->hostFlags |= HOSTDELETED;
1284 /* addr/port was removed from the hash table */
1288 rxconn = host->callback_rxcon;
1289 host->callback_rxcon = NULL;
1292 rx_DestroyConnection(rxconn);
1296 h_SetupCallbackConn_r(host);
1299 /* not the primary addr/port, just invalidate it */
1300 invalidateInterfaceAddr_r(host, addr, port);
1308 createHostAddrHashChain_r(int index, afs_uint32 addr, afs_uint16 port, struct host *host)
1310 struct h_AddrHashChain *chain;
1313 /* insert into beginning of list for this bucket */
1314 chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
1316 ViceLogThenPanic(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1318 chain->hostPtr = host;
1319 chain->next = hostAddrHashTable[index];
1322 hostAddrHashTable[index] = chain;
1323 ViceLog(125, ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " added as %s:%d\n",
1324 host, afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1328 * Resolve host address conflicts when hashing by address.
1330 * @param[in] addr an ip address of the interface
1331 * @param[in] port the port of the interface
1332 * @param[in] newHost the host being added with this address
1333 * @param[in] oldHost the host previously added with this address
1336 reconcileHosts_r(afs_uint32 addr, afs_uint16 port, struct host *newHost,
1337 struct host *oldHost)
1339 struct rx_connection *cb = NULL;
1341 struct interfaceAddr interf;
1343 afsUUID *newHostUuid = &nulluuid;
1344 afsUUID *oldHostUuid = &nulluuid;
1348 ("reconcileHosts_r: addr %s:%d newHost %" AFS_PTR_FMT " oldHost %"
1349 AFS_PTR_FMT, afs_inet_ntoa_r(addr, hoststr), ntohs(port),
1352 osi_Assert(oldHost != newHost);
1353 caps.Capabilities_val = NULL;
1356 sc = rxnull_NewClientSecurityObject();
1359 cb = rx_NewConnection(addr, port, 1, sc, 0);
1360 rx_SetConnDeadTime(cb, 50);
1361 rx_SetConnHardDeadTime(cb, AFS_HARDDEADTIME);
1366 code = RXAFSCB_TellMeAboutYourself(cb, &interf, &caps);
1367 if (code == RXGEN_OPCODE) {
1368 code = RXAFSCB_WhoAreYou(cb, &interf);
1372 if (code == RXGEN_OPCODE ||
1373 (code == 0 && afs_uuid_equal(&interf.uuid, &nulluuid))) {
1375 ("reconcileHosts_r: WhoAreYou not supported for connection (%s:%d), error %d\n",
1376 afs_inet_ntoa_r(addr, hoststr), ntohs(port), code));
1381 ("reconcileHosts_r: WhoAreYou failed for connection (%s:%d), error %d\n",
1382 afs_inet_ntoa_r(addr, hoststr), ntohs(port), code));
1386 /* Since lock was dropped, the hosts may have been deleted during the rpcs. */
1387 if ((newHost->hostFlags & HOSTDELETED)
1388 && (oldHost->hostFlags & HOSTDELETED)) {
1390 ("reconcileHosts_r: new and old hosts were deleted during probe.\n"));
1394 /* A check can be done if at least one of the hosts has a uuid. It
1395 * is an error if the hosts have the same (not null) uuid. */
1396 if ((!(newHost->hostFlags & HOSTDELETED)) && newHost->interface) {
1397 newHostUuid = &(newHost->interface->uuid);
1399 if ((!(oldHost->hostFlags & HOSTDELETED)) && oldHost->interface) {
1400 oldHostUuid = &(oldHost->interface->uuid);
1402 if (afs_uuid_equal(newHostUuid, &nulluuid) &&
1403 afs_uuid_equal(oldHostUuid, &nulluuid)) {
1405 ("reconcileHosts_r: Cannot reconcile hosts for connection (%s:%d), no uuids\n",
1406 afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1409 if (afs_uuid_equal(newHostUuid, oldHostUuid)) {
1411 ("reconcileHosts_r: Cannot reconcile hosts for connection (%s:%d), same uuids\n",
1412 afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1416 /* Determine which host should be hashed */
1417 if ((!(newHost->hostFlags & HOSTDELETED))
1418 && afs_uuid_equal(newHostUuid, &(interf.uuid))) {
1419 /* Install the new host into the hash before removing the stale
1420 * addresses. Walk the hash chain again since the hash table may have
1421 * been changed when the host lock was dropped to get the uuid. */
1422 struct h_AddrHashChain *chain;
1423 int index = h_HashIndex(addr);
1424 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1425 if (chain->addr == addr && chain->port == port) {
1426 chain->hostPtr = newHost;
1427 removeAddress_r(oldHost, addr, port);
1431 createHostAddrHashChain_r(index, addr, port, newHost);
1432 removeAddress_r(oldHost, addr, port);
1435 if ((!(oldHost->hostFlags & HOSTDELETED))
1436 && afs_uuid_equal(oldHostUuid, &(interf.uuid))) {
1437 removeAddress_r(newHost, addr, port);
1442 if (!(newHost->hostFlags & HOSTDELETED)) {
1443 removeAddress_r(newHost, addr, port);
1445 if (!(oldHost->hostFlags & HOSTDELETED)) {
1446 removeAddress_r(oldHost, addr, port);
1450 h_Release_r(newHost);
1451 h_Release_r(oldHost);
1452 rx_DestroyConnection(cb);
1456 /* inserts a new HashChain structure corresponding to this address */
1458 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1461 struct h_AddrHashChain *chain;
1464 /* hash into proper bucket */
1465 index = h_HashIndex(addr);
1467 /* don't add the same address:port pair entry multiple times */
1468 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1469 if (chain->addr == addr && chain->port == port) {
1470 if (chain->hostPtr == host) {
1472 ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) already hashed\n",
1473 host, afs_inet_ntoa_r(chain->addr, hoststr),
1474 ntohs(chain->port)));
1477 if (!(chain->hostPtr->hostFlags & HOSTDELETED)) {
1478 /* attempt to resolve host address collision */
1479 reconcileHosts_r(addr, port, host, chain->hostPtr);
1484 createHostAddrHashChain_r(index, addr, port, host);
1488 * This is called with host locked and held.
1489 * It is called to either validate or add an additional interface
1490 * address/port on the specified host.
1492 * All addresses are in network byte order.
1495 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1499 struct Interface *interface;
1500 char hoststr[16], hoststr2[16];
1503 osi_Assert(host->interface);
1506 * Make sure this address is on the list of known addresses
1509 number = host->interface->numberOfInterfaces;
1510 for (i = 0; i < number; i++) {
1511 if (host->interface->interface[i].addr == addr &&
1512 host->interface->interface[i].port == port) {
1514 ("addInterfaceAddr : found host %" AFS_PTR_FMT " (%s:%d) adding %s:%d%s\n",
1515 host, afs_inet_ntoa_r(host->host, hoststr),
1516 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1517 ntohs(port), host->interface->interface[i].valid ? "" :
1520 if (host->interface->interface[i].valid == 0) {
1521 host->interface->interface[i].valid = 1;
1522 h_AddHostToAddrHashTable_r(addr, port, host);
1528 ViceLog(125, ("addInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) adding %s:%d\n",
1529 host, afs_inet_ntoa_r(host->host, hoststr),
1530 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1533 interface = (struct Interface *)
1534 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1536 ViceLogThenPanic(0, ("Failed malloc in addInterfaceAddr_r\n"));
1538 interface->numberOfInterfaces = number + 1;
1539 interface->uuid = host->interface->uuid;
1540 for (i = 0; i < number; i++)
1541 interface->interface[i] = host->interface->interface[i];
1543 /* Add the new valid interface */
1544 interface->interface[number].addr = addr;
1545 interface->interface[number].port = port;
1546 interface->interface[number].valid = 1;
1547 h_AddHostToAddrHashTable_r(addr, port, host);
1548 free(host->interface);
1549 host->interface = interface;
1556 * This is called with host locked and held.
1558 * All addresses are in network byte order.
1561 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1565 struct Interface *interface;
1566 char hoststr[16], hoststr2[16];
1569 osi_Assert(host->interface);
1571 ViceLog(125, ("removeInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1572 host, afs_inet_ntoa_r(host->host, hoststr),
1573 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1577 * Make sure this address is on the list of known addresses
1580 interface = host->interface;
1581 number = host->interface->numberOfInterfaces;
1582 for (i = 0; i < number; i++) {
1583 if (interface->interface[i].addr == addr &&
1584 interface->interface[i].port == port) {
1585 if (interface->interface[i].valid)
1586 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1588 for (; i < number; i++) {
1589 interface->interface[i] = interface->interface[i+1];
1591 interface->numberOfInterfaces = number;
1602 h_threadquota(int waiting)
1607 } else if (lwps > 32) {
1610 } else if (lwps > 16) {
1620 /* If found, host is returned with refCount incremented */
1622 h_GetHost_r(struct rx_connection *tcon)
1625 struct host *oldHost;
1627 struct interfaceAddr interf;
1628 int interfValid = 0;
1629 struct Identity *identP = NULL;
1632 char hoststr[16], hoststr2[16];
1634 struct rx_connection *cb_conn = NULL;
1635 struct rx_connection *cb_in = NULL;
1637 caps.Capabilities_val = NULL;
1639 haddr = rxr_HostOf(tcon);
1640 hport = rxr_PortOf(tcon);
1643 rx_DestroyConnection(cb_in);
1646 if (caps.Capabilities_val)
1647 free(caps.Capabilities_val);
1648 caps.Capabilities_val = NULL;
1649 caps.Capabilities_len = 0;
1652 if (h_Lookup_r(haddr, hport, &host))
1654 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1655 if (host && !identP && !(host->Console & 1)) {
1656 /* This is a new connection, and we already have a host
1657 * structure for this address. Verify that the identity
1658 * of the caller matches the identity in the host structure.
1660 if ((host->hostFlags & HWHO_INPROGRESS) &&
1661 h_threadquota(host->lock.num_waiting)) {
1667 if (!(host->hostFlags & ALTADDR) ||
1668 (host->hostFlags & HOSTDELETED)) {
1669 /* Another thread is doing initialization
1670 * or this host was deleted while we
1671 * waited for the lock. */
1674 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1675 host, afs_inet_ntoa_r(host->host, hoststr),
1676 ntohs(host->port)));
1680 host->hostFlags |= HWHO_INPROGRESS;
1681 host->hostFlags &= ~ALTADDR;
1683 /* We received a new connection from an IP address/port
1684 * that is associated with 'host' but the address/port of
1685 * the callback connection does not have to match it.
1686 * If there is a match, we can use the existing callback
1687 * connection to verify the UUID. If they do not match
1688 * we need to use a new callback connection to verify the
1689 * UUID of the incoming caller and perhaps use the old
1690 * callback connection to verify that the old address/port
1694 cb_conn = host->callback_rxcon;
1695 rx_GetConnection(cb_conn);
1697 if (haddr == host->host && hport == host->port) {
1698 /* The existing callback connection matches the
1699 * incoming connection so just use it.
1702 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1703 if (code == RXGEN_OPCODE)
1704 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1706 /* We do not have a match. Create a new connection
1707 * for the new addr/port and use multi_Rx to probe
1708 * both of them simultaneously.
1711 sc = rxnull_NewClientSecurityObject();
1712 cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
1713 rx_SetConnDeadTime(cb_in, 50);
1714 rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
1715 rx_SetConnSecondsUntilNatPing(cb_in, 20);
1718 RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
1719 if (code == RXGEN_OPCODE)
1720 code = RXAFSCB_WhoAreYou(cb_in, &interf);
1722 rx_PutConnection(cb_conn);
1725 if ((code == RXGEN_OPCODE) ||
1726 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1727 identP = (struct Identity *)malloc(sizeof(struct Identity));
1729 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1732 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1733 if (cb_in == NULL) {
1734 /* The host on this connection was unable to respond to
1735 * the WhoAreYou. We will treat this as a new connection
1736 * from the existing host. The worst that can happen is
1737 * that we maintain some extra callback state information */
1738 if (host->interface) {
1740 ("Host %" AFS_PTR_FMT " (%s:%d) used to support WhoAreYou, deleting.\n",
1742 afs_inet_ntoa_r(host->host, hoststr),
1743 ntohs(host->port)));
1744 host->hostFlags |= HOSTDELETED;
1745 host->hostFlags &= ~HWHO_INPROGRESS;
1752 /* The incoming connection does not support WhoAreYou but
1753 * the original one might have. Use removeAddress_r() to
1754 * remove this addr/port from the host that was found.
1755 * If there are no more addresses left for the host it
1756 * will be deleted. Then we retry.
1758 removeAddress_r(host, haddr, hport);
1759 host->hostFlags &= ~HWHO_INPROGRESS;
1760 host->hostFlags |= ALTADDR;
1766 } else if (code == 0) {
1768 identP = (struct Identity *)malloc(sizeof(struct Identity));
1770 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1773 identP->uuid = interf.uuid;
1774 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1775 /* Check whether the UUID on this connection matches
1776 * the UUID in the host structure. If they don't match
1777 * then this is not the same host as before. */
1778 if (!host->interface
1779 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1782 ("Uuid doesn't match connection (%s:%d).\n",
1783 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport)));
1784 removeAddress_r(host, haddr, hport);
1787 ("Uuid doesn't match host %" AFS_PTR_FMT " (%s:%d).\n",
1788 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1790 removeAddress_r(host, host->host, host->port);
1792 host->hostFlags &= ~HWHO_INPROGRESS;
1793 host->hostFlags |= ALTADDR;
1799 /* the UUID matched the client at the incoming addr/port
1800 * but this is not the address of the active callback
1801 * connection. Try that connection and see if the client
1802 * is still there and if the reported UUID is the same.
1805 afsUUID uuid = host->interface->uuid;
1806 cb_conn = host->callback_rxcon;
1807 rx_GetConnection(cb_conn);
1808 rx_SetConnDeadTime(cb_conn, 2);
1809 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1811 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1813 rx_SetConnDeadTime(cb_conn, 50);
1814 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1815 rx_PutConnection(cb_conn);
1818 /* The primary address is either not responding or
1819 * is not the client we are looking for. Need to
1820 * remove the primary address and add swap in the new
1821 * callback connection, and destroy the old one.
1823 struct rx_connection *rxconn;
1824 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
1826 afs_inet_ntoa_r(host->host, hoststr),
1827 ntohs(host->port),code2));
1830 * make sure we add and then remove. otherwise, we
1831 * might end up with no valid interfaces after the
1832 * remove and the host will have been marked deleted.
1834 addInterfaceAddr_r(host, haddr, hport);
1835 removeInterfaceAddr_r(host, host->host, host->port);
1838 rxconn = host->callback_rxcon;
1839 host->callback_rxcon = cb_in;
1844 * If rx_DestroyConnection calls h_FreeConnection we
1845 * will deadlock on the host_glock_mutex. Work around
1846 * the problem by unhooking the client from the
1847 * connection before destroying the connection.
1849 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1850 rx_DestroyConnection(rxconn);
1856 /* A callback to the incoming connection address is failing.
1857 * Assume that the addr/port is no longer associated with the host
1858 * returned by h_Lookup_r.
1861 ("CB: WhoAreYou failed for connection (%s:%d) , error %d\n",
1862 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport), code));
1863 removeAddress_r(host, haddr, hport);
1864 host->hostFlags &= ~HWHO_INPROGRESS;
1865 host->hostFlags |= ALTADDR;
1869 rx_DestroyConnection(cb_in);
1874 ("CB: WhoAreYou failed for host %" AFS_PTR_FMT " (%s:%d), error %d\n",
1875 host, afs_inet_ntoa_r(host->host, hoststr),
1876 ntohs(host->port), code));
1877 host->hostFlags |= VENUSDOWN;
1880 if (caps.Capabilities_val
1881 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1882 host->hostFlags |= HERRORTRANS;
1884 host->hostFlags &= ~(HERRORTRANS);
1885 host->hostFlags |= ALTADDR;
1886 host->hostFlags &= ~HWHO_INPROGRESS;
1889 if (!(host->hostFlags & ALTADDR)) {
1890 /* another thread is doing the initialisation */
1892 ("Host %" AFS_PTR_FMT " (%s:%d) waiting for host-init to complete\n",
1893 host, afs_inet_ntoa_r(host->host, hoststr),
1894 ntohs(host->port)));
1898 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1899 host, afs_inet_ntoa_r(host->host, hoststr),
1900 ntohs(host->port)));
1904 /* We need to check whether the identity in the host structure
1905 * matches the identity on the connection. If they don't match
1906 * then treat this a new host. */
1907 if (!(host->Console & 1)
1908 && ((!identP->valid && host->interface)
1909 || (identP->valid && !host->interface)
1911 && !afs_uuid_equal(&identP->uuid,
1912 &host->interface->uuid)))) {
1913 char uuid1[128], uuid2[128];
1915 afsUUID_to_string(&identP->uuid, uuid1, 127);
1916 if (host->interface)
1917 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1919 ("CB: new identity for host %p (%s:%d), "
1920 "deleting(%x %p %s %s)\n",
1921 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1922 identP->valid, host->interface,
1923 identP->valid ? uuid1 : "no_uuid",
1924 host->interface ? uuid2 : "no_uuid"));
1926 /* The host in the cache is not the host for this connection */
1928 host->hostFlags |= HOSTDELETED;
1934 host = h_Alloc_r(tcon); /* returned held and locked */
1937 h_gethostcps_r(host, FT_ApproxTime());
1938 if (!(host->Console & 1)) {
1940 cb_conn = host->callback_rxcon;
1941 rx_GetConnection(cb_conn);
1942 host->hostFlags |= HWHO_INPROGRESS;
1945 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1946 if (code == RXGEN_OPCODE)
1947 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1948 rx_PutConnection(cb_conn);
1951 if ((code == RXGEN_OPCODE) ||
1952 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1955 (struct Identity *)malloc(sizeof(struct Identity));
1960 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1964 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1966 ("Host %" AFS_PTR_FMT " (%s:%d) does not support WhoAreYou.\n",
1967 host, afs_inet_ntoa_r(host->host, hoststr),
1968 ntohs(host->port)));
1970 } else if (code == 0) {
1973 (struct Identity *)malloc(sizeof(struct Identity));
1978 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1982 identP->uuid = interf.uuid;
1984 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1986 ("WhoAreYou success on host %" AFS_PTR_FMT " (%s:%d)\n",
1987 host, afs_inet_ntoa_r(host->host, hoststr),
1988 ntohs(host->port)));
1990 if (code == 0 && !identP->valid) {
1991 cb_conn = host->callback_rxcon;
1992 rx_GetConnection(cb_conn);
1994 code = RXAFSCB_InitCallBackState(cb_conn);
1995 rx_PutConnection(cb_conn);
1998 } else if (code == 0) {
1999 oldHost = h_LookupUuid_r(&identP->uuid);
2004 if (oldHost->hostFlags & HOSTDELETED) {
2005 h_Unlock_r(oldHost);
2006 h_Release_r(oldHost);
2014 /* This is a new address for an existing host. Update
2015 * the list of interfaces for the existing host and
2016 * delete the host structure we just allocated. */
2018 /* mark the duplicate host as deleted before we do
2019 * anything. The probing code below may try to change
2020 * "oldHost" to the same IP address as "host" currently
2021 * has, and we do not want a pseudo-"collision" to be
2023 host->hostFlags |= HOSTDELETED;
2025 oldHost->hostFlags |= HWHO_INPROGRESS;
2027 if (oldHost->interface) {
2029 afsUUID uuid = oldHost->interface->uuid;
2030 cb_conn = oldHost->callback_rxcon;
2031 rx_GetConnection(cb_conn);
2032 rx_SetConnDeadTime(cb_conn, 2);
2033 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2035 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2037 rx_SetConnDeadTime(cb_conn, 50);
2038 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2039 rx_PutConnection(cb_conn);
2042 /* The primary address is either not responding or
2043 * is not the client we are looking for.
2044 * MultiProbeAlternateAddress_r() will remove the
2045 * alternate interfaces that do not have the same
2047 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
2049 afs_inet_ntoa_r(oldHost->host, hoststr),
2050 ntohs(oldHost->port),code2));
2052 if (MultiProbeAlternateAddress_r(oldHost)) {
2053 /* If MultiProbeAlternateAddress_r succeeded,
2054 * it updated oldHost->host and oldHost->port
2055 * to an address that responded successfully to
2056 * a ProbeUuid, so it is as if the ProbeUuid
2057 * call above returned success. So, only set
2058 * 'probefail' if MultiProbeAlternateAddress_r
2067 if (oldHost->host != haddr || oldHost->port != hport) {
2068 struct rx_connection *rxconn;
2071 ("CB: Host %" AFS_PTR_FMT " (%s:%d) has new addr %s:%d\n",
2073 afs_inet_ntoa_r(oldHost->host, hoststr2),
2074 ntohs(oldHost->port),
2075 afs_inet_ntoa_r(haddr, hoststr),
2078 * add then remove. otherwise the host may get marked
2079 * deleted if we removed the only valid address.
2081 addInterfaceAddr_r(oldHost, haddr, hport);
2082 if (probefail || oldHost->host == haddr) {
2084 * The probe failed which means that the old
2085 * address is either unreachable or is not the
2086 * same host we were just contacted by. We will
2087 * also remove addresses if only the port has
2088 * changed because that indicates the client
2091 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
2094 struct Interface *interface = oldHost->interface;
2095 int number = oldHost->interface->numberOfInterfaces;
2096 for (i = 0; i < number; i++) {
2097 if (interface->interface[i].addr == haddr &&
2098 interface->interface[i].port != hport) {
2100 * We have just been contacted by a client
2101 * that has been seen from behind a NAT
2102 * and at least one other address.
2104 removeInterfaceAddr_r(oldHost, haddr,
2105 interface->interface[i].port);
2110 oldHost->host = haddr;
2111 oldHost->port = hport;
2112 rxconn = oldHost->callback_rxcon;
2113 oldHost->callback_rxcon = host->callback_rxcon;
2114 host->callback_rxcon = rxconn;
2116 /* don't destroy rxconn here; let h_TossStuff_r
2117 * take care of that via h_Release_r below */
2119 host->hostFlags &= ~HWHO_INPROGRESS;
2121 /* release host because it was allocated by h_Alloc_r */
2124 /* the new host is held and locked */
2126 /* This really is a new host */
2127 h_AddHostToUuidHashTable_r(&identP->uuid, host);
2128 cb_conn = host->callback_rxcon;
2129 rx_GetConnection(cb_conn);
2132 RXAFSCB_InitCallBackState3(cb_conn,
2134 rx_PutConnection(cb_conn);
2139 ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
2140 host, afs_inet_ntoa_r(host->host, hoststr),
2141 ntohs(host->port)));
2142 osi_Assert(interfValid == 1);
2143 initInterfaceAddr_r(host, &interf);
2149 ("CB: RCallBackConnectBack failed for %" AFS_PTR_FMT " (%s:%d)\n",
2150 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
2151 host->hostFlags |= VENUSDOWN;
2154 ("CB: RCallBackConnectBack succeeded for %" AFS_PTR_FMT " (%s:%d)\n",
2155 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
2156 host->hostFlags |= RESETDONE;
2159 if (caps.Capabilities_val
2160 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
2161 host->hostFlags |= HERRORTRANS;
2163 host->hostFlags &= ~(HERRORTRANS);
2164 host->hostFlags |= ALTADDR; /* host structure initialization complete */
2165 host->hostFlags &= ~HWHO_INPROGRESS;
2170 if (caps.Capabilities_val)
2171 free(caps.Capabilities_val);
2172 caps.Capabilities_val = NULL;
2173 caps.Capabilities_len = 0;
2175 rx_DestroyConnection(cb_in);
2183 static char localcellname[PR_MAXNAMELEN + 1];
2187 h_InitHostPackage(void)
2189 memset(&nulluuid, 0, sizeof(afsUUID));
2190 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
2191 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
2192 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
2193 MUTEX_INIT(&host_glock_mutex, "host glock", MUTEX_DEFAULT, 0);
2197 MapName_r(char *uname, afs_int32 * aval)
2203 lnames.namelist_len = 1;
2204 lnames.namelist_val = (prname *) uname;
2205 lids.idlist_len = 0;
2206 lids.idlist_val = NULL;
2209 code = hpr_NameToId(&lnames, &lids);
2212 if (lids.idlist_val) {
2213 *aval = lids.idlist_val[0];
2214 if (*aval == AnonymousID) {
2216 ("MapName: NameToId on %s returns anonymousID\n",
2217 lnames.namelist_val[0]));
2219 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
2222 ("MapName: NameToId on '%s' is unknown\n",
2223 lnames.namelist_val[0]));
2233 /* NOTE: this returns the client with a Write lock and a refCount */
2235 h_ID2Client(afs_int32 vid)
2237 struct client *client;
2242 for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
2243 if (host->hostFlags & HOSTDELETED)
2245 for (client = host->FirstClient; client; client = client->next) {
2246 if (!client->deleted && client->ViceId == vid) {
2249 ObtainWriteLock(&client->lock);
2254 if (count != hostCount) {
2255 ViceLog(0, ("h_ID2Client found %d of %d hosts\n", count, hostCount));
2256 } else if (host != NULL) {
2257 ViceLog(0, ("h_ID2Client found more than %d hosts\n", hostCount));
2258 ShutDownAndCore(PANIC);
2266 format_vname(char *vname, int usize, const char *tname, const char *tinst,
2267 const char *tcell, afs_int32 islocal)
2271 len = strlcpy(vname, tname, usize);
2275 len = strlcat(vname, ".", usize);
2278 len = strlcat(vname, tinst, usize);
2282 if (tcell[0] && !islocal) {
2283 len = strlcat(vname, "@", usize);
2286 len = strlcat(vname, tcell, usize);
2294 * Called by the server main loop. Returns a h_Held client, which must be
2295 * released later the main loop. Allocates a client if the matching one
2296 * isn't around. The client is returned with its reference count incremented
2297 * by one. The caller must call h_ReleaseClient_r when finished with
2300 * The refCount on client->host is returned incremented. h_ReleaseClient_r
2301 * does not decrement the refCount on client->host.
2304 h_FindClient_r(struct rx_connection *tcon)
2306 struct client *client;
2307 struct host *host = NULL;
2308 struct client *oldClient;
2309 afs_int32 viceid = 0;
2313 #if (64-MAXKTCNAMELEN)
2314 ticket name length != 64
2318 char uname[PR_MAXNAMELEN];
2319 char tcell[MAXKTCREALMLEN];
2323 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2324 if (client && client->sid == rx_GetConnectionId(tcon)
2325 && client->VenusEpoch == rx_GetConnectionEpoch(tcon)
2326 && !(client->host->hostFlags & HOSTDELETED)
2327 && !client->deleted) {
2330 h_Hold_r(client->host);
2331 if (client->prfail != 2) {
2332 /* Could add shared lock on client here */
2333 /* note that we don't have to lock entry in this path to
2334 * ensure CPS is initialized, since we don't call rx_SetSpecific
2335 * until initialization is done, and we only get here if
2336 * rx_GetSpecific located the client structure.
2341 ObtainWriteLock(&client->lock); /* released at end */
2347 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
2349 ("FindClient: authenticating connection: authClass=%d\n",
2351 if (authClass == 1) {
2352 /* A bcrypt tickets, no longer supported */
2353 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2354 viceid = AnonymousID;
2355 expTime = 0x7fffffff;
2356 } else if (authClass == 2) {
2360 /* kerberos ticket */
2361 code = rxkad_GetServerInfo(tcon, /*level */ 0, (afs_uint32 *)&expTime,
2362 tname, tinst, tcell, &kvno);
2364 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2365 viceid = AnonymousID;
2366 expTime = 0x7fffffff;
2369 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2370 tname, tinst, tcell, expTime, kvno));
2371 code = afsconf_IsLocalRealmMatch(confDir, &islocal, tname, tinst, tcell);
2373 ViceLog(0, ("FindClient: local realm check failed; code=%d", code));
2374 viceid = AnonymousID;
2375 expTime = 0x7fffffff;
2378 code = format_vname(uname, sizeof(uname), tname, tinst, tcell, islocal);
2380 ViceLog(0, ("FindClient: uname truncated."));
2381 viceid = AnonymousID;
2382 expTime = 0x7fffffff;
2386 /* translate the name to a vice id */
2387 code = MapName_r(uname, &viceid);
2390 ("failed to map name=%s -> code=%d\n", uname,
2393 viceid = AnonymousID;
2394 expTime = 0x7fffffff;
2399 viceid = AnonymousID; /* unknown security class */
2400 expTime = 0x7fffffff;
2403 if (!client) { /* loop */
2404 host = h_GetHost_r(tcon); /* Returns with incremented refCount */
2410 /* First try to find the client structure */
2411 for (client = host->FirstClient; client; client = client->next) {
2412 if (!client->deleted && (client->sid == rx_GetConnectionId(tcon))
2413 && (client->VenusEpoch == rx_GetConnectionEpoch(tcon))) {
2416 ObtainWriteLock(&client->lock);
2422 /* Still no client structure - get one */
2425 if (host->hostFlags & HOSTDELETED) {
2430 /* Retry to find the client structure */
2431 for (client = host->FirstClient; client; client = client->next) {
2432 if (!client->deleted && (client->sid == rx_GetConnectionId(tcon))
2433 && (client->VenusEpoch == rx_GetConnectionEpoch(tcon))) {
2435 goto retryfirstclient;
2440 ObtainWriteLock(&client->lock);
2441 client->refCount = 1;
2442 client->host = host;
2443 client->InSameNetwork = host->InSameNetwork;
2444 client->ViceId = viceid;
2445 client->expTime = expTime; /* rx only */
2446 client->authClass = authClass; /* rx only */
2447 client->sid = rx_GetConnectionId(tcon);
2448 client->VenusEpoch = rx_GetConnectionEpoch(tcon);
2449 client->CPS.prlist_val = NULL;
2450 client->CPS.prlist_len = 0;
2454 client->prfail = fail;
2456 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
2457 client->CPS.prlist_len = 0;
2458 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
2459 free(client->CPS.prlist_val);
2460 client->CPS.prlist_val = NULL;
2461 client->ViceId = viceid;
2462 client->expTime = expTime;
2464 if (viceid == ANONYMOUSID) {
2465 client->CPS.prlist_len = AnonCPS.prlist_len;
2466 client->CPS.prlist_val = AnonCPS.prlist_val;
2469 code = hpr_GetCPS(viceid, &client->CPS);
2474 ("pr_GetCPS failed(%d) for user %d, host %" AFS_PTR_FMT " (%s:%d)\n",
2475 code, viceid, client->host,
2476 afs_inet_ntoa_r(client->host->host,hoststr),
2477 ntohs(client->host->port)));
2479 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2480 * all protection servers and reevaluates things if no
2481 * sync server or quorum is found we could still end up
2482 * with one of these errors. In such case we would like to
2483 * reevaluate the rpc call to find if there's cps for this
2484 * guy. We treat other errors (except network failures
2485 * ones - i.e. code < 0) as an indication that there is no
2486 * CPS for this host. Ideally we could like to deal this
2487 * problem the other way around (i.e. if code == NOCPS
2488 * ignore else retry next time) but the problem is that
2489 * there're other errors (i.e. EPERM) for which we don't
2490 * want to retry and we don't know the whole code list!
2492 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2496 /* the disabling of system:administrators is so iffy and has so many
2497 * possible failure modes that we will disable it again */
2498 /* Turn off System:Administrator for safety
2499 * if (AL_IsAMember(SystemId, client->CPS) == 0)
2500 * osi_Assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
2503 /* Now, tcon may already be set to a rock, since we blocked with no host
2504 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2505 * required). So, before setting the RPC's rock, we should disconnect
2506 * the RPC from the other client structure's rock.
2508 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2509 if (oldClient && oldClient != client
2510 && oldClient->sid == rx_GetConnectionId(tcon)
2511 && oldClient->VenusEpoch == rx_GetConnectionEpoch(tcon)
2512 && !(oldClient->host->hostFlags & HOSTDELETED)) {
2514 if (!oldClient->deleted) {
2515 /* if we didn't create it, it's not ours to put back */
2517 ViceLog(0, ("FindClient: stillborn client %p(%x); "
2518 "conn %p (host %s:%d) had client %p(%x)\n",
2519 client, client->sid, tcon,
2520 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2521 ntohs(rxr_PortOf(tcon)),
2522 oldClient, oldClient->sid));
2523 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2524 free(client->CPS.prlist_val);
2525 client->CPS.prlist_val = NULL;
2526 client->CPS.prlist_len = 0;
2528 /* We should perhaps check for 0 here */
2530 ReleaseWriteLock(&client->lock);
2535 oldClient->refCount++;
2537 h_Hold_r(oldClient->host);
2538 h_Release_r(client->host);
2541 ObtainWriteLock(&oldClient->lock);
2544 host = oldClient->host;
2546 ViceLog(0, ("FindClient: deleted client %p(%x ref %d host %p href "
2547 "%d) already had conn %p (host %s:%d, cid %x), stolen "
2548 "by client %p(%x, ref %d host %p href %d)\n",
2549 oldClient, oldClient->sid, oldClient->refCount,
2550 oldClient->host, oldClient->host->refCount, tcon,
2551 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2552 ntohs(rxr_PortOf(tcon)), rx_GetConnectionId(tcon),
2553 client, client->sid, client->refCount,
2554 client->host, client->host->refCount));
2555 /* rx_SetSpecific will be done immediately below */
2558 /* Avoid chaining in more than once. */
2562 if (host->hostFlags & HOSTDELETED) {
2567 client->host = NULL;
2569 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2570 free(client->CPS.prlist_val);
2571 client->CPS.prlist_val = NULL;
2572 client->CPS.prlist_len = 0;
2575 ReleaseWriteLock(&client->lock);
2580 client->next = host->FirstClient;
2581 host->FirstClient = client;
2583 CurrentConnections++; /* increment number of connections */
2585 rx_SetSpecific(tcon, rxcon_client_key, client);
2586 ReleaseWriteLock(&client->lock);
2590 } /*h_FindClient_r */
2593 h_ReleaseClient_r(struct client *client)
2595 osi_Assert(client->refCount > 0);
2602 * Sigh: this one is used to get the client AGAIN within the individual
2603 * server routines. This does not bother h_Holding the host, since
2604 * this is assumed already have been done by the server main loop.
2605 * It does check tokens, since only the server routines can return the
2606 * VICETOKENDEAD error code
2609 GetClient(struct rx_connection *tcon, struct client **cp)
2611 struct client *client;
2616 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2617 if (client == NULL) {
2619 ("GetClient: no client in conn %p (host %s:%d), VBUSYING\n",
2620 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2621 ntohs(rxr_PortOf(tcon))));
2625 if (rx_GetConnectionId(tcon) != client->sid
2626 || rx_GetConnectionEpoch(tcon) != client->VenusEpoch) {
2628 ("GetClient: tcon %p tcon sid %d client sid %d\n",
2629 tcon, rx_GetConnectionId(tcon), client->sid));
2633 if (client && client->LastCall > client->expTime && client->expTime) {
2635 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2636 afs_inet_ntoa_r(client->host->host, hoststr),
2637 ntohs(client->host->port), client->expTime));
2639 return VICETOKENDEAD;
2641 if (client->deleted) {
2642 ViceLog(0, ("GetClient: got deleted client, connection will appear "
2643 "anonymous; tcon %p cid %x client %p ref %d host %p "
2644 "(%s:%d) href %d ViceId %d\n",
2645 tcon, rx_GetConnectionId(tcon), client, client->refCount,
2647 afs_inet_ntoa_r(client->host->host, hoststr),
2648 (int)ntohs(client->host->port), client->host->refCount,
2649 (int)client->ViceId));
2659 PutClient(struct client **cp)
2665 h_ReleaseClient_r(*cp);
2672 /* Client user name for short term use. Note that this is NOT inexpensive */
2674 h_UserName(struct client *client)
2676 static char User[PR_MAXNAMELEN + 1];
2680 lids.idlist_len = 1;
2681 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
2682 if (!lids.idlist_val) {
2683 ViceLogThenPanic(0, ("Failed malloc in h_UserName\n"));
2685 lnames.namelist_len = 0;
2686 lnames.namelist_val = (prname *) 0;
2687 lids.idlist_val[0] = client->ViceId;
2688 if (hpr_IdToName(&lids, &lnames)) {
2689 /* We need to free id we alloced above! */
2690 free(lids.idlist_val);
2691 return "*UNKNOWN USER NAME*";
2693 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2694 free(lids.idlist_val);
2695 free(lnames.namelist_val);
2704 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2705 CEs, CEBlocks, HTs, HTBlocks));
2711 h_PrintClient(struct host *host, void *rock)
2713 StreamHandle_t *file = (StreamHandle_t *)rock;
2714 struct client *client;
2719 time_t LastCall, expTime;
2723 LastCall = host->LastCall;
2724 if (host->hostFlags & HOSTDELETED) {
2728 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2729 localtime_r(&LastCall, &tm));
2730 snprintf(tmpStr, sizeof tmpStr, "Host %s:%d down = %d, LastCall %s\n",
2731 afs_inet_ntoa_r(host->host, hoststr),
2732 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2734 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2735 for (client = host->FirstClient; client; client = client->next) {
2736 if (!client->deleted) {
2737 expTime = client->expTime;
2738 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2739 localtime_r(&expTime, &tm));
2740 snprintf(tmpStr, sizeof tmpStr,
2741 " user id=%d, name=%s, sl=%s till %s\n",
2742 client->ViceId, h_UserName(client),
2743 client->authClass ? "Authenticated"
2744 : "Not authenticated",
2745 client->authClass ? tbuffer : "No Limit");
2746 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2747 snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2748 client->CPS.prlist_len);
2749 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2750 if (client->CPS.prlist_val) {
2751 for (i = 0; i < client->CPS.prlist_len; i++) {
2752 snprintf(tmpStr, sizeof tmpStr, " %d",
2753 client->CPS.prlist_val[i]);
2754 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2757 sprintf(tmpStr, "]\n");
2758 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2764 } /*h_PrintClient */
2769 * Print a list of clients, with last security level and token value seen,
2773 h_PrintClients(void)
2780 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2784 ("Couldn't create client dump file %s\n",
2785 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2788 now = FT_ApproxTime();
2789 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2790 localtime_r(&now, &tm));
2791 snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n\n",
2793 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2794 h_Enumerate(h_PrintClient, (char *)file);
2795 STREAM_REALLYCLOSE(file);
2796 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2803 h_DumpHost(struct host *host, void *rock)
2805 StreamHandle_t *file = (StreamHandle_t *)rock;
2812 snprintf(tmpStr, sizeof tmpStr,
2813 "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u "
2814 "down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u "
2816 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
2817 host->index, host->cblist, CheckLock(&host->lock),
2818 host->LastCall, host->ActiveCall, (host->hostFlags & VENUSDOWN),
2819 host->hostFlags & HOSTDELETED, host->Console,
2820 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2822 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2823 if (host->hcps.prlist_val)
2824 for (i = 0; i < host->hcps.prlist_len; i++) {
2825 snprintf(tmpStr, sizeof tmpStr, " %d", host->hcps.prlist_val[i]);
2826 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2828 sprintf(tmpStr, "] [");
2829 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2830 if (host->interface)
2831 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2833 sprintf(tmpStr, " %s:%d",
2834 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2835 ntohs(host->interface->interface[i].port));
2836 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2838 sprintf(tmpStr, "] refCount:%d hostFlags:%hu\n", host->refCount, host->hostFlags);
2839 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2851 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2858 ("Couldn't create host dump file %s\n",
2859 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2862 now = FT_ApproxTime();
2863 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2864 localtime_r(&now, &tm));
2865 snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n\n", tbuffer);
2866 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2867 h_Enumerate(h_DumpHost, (char *)file);
2868 STREAM_REALLYCLOSE(file);
2869 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2873 #ifdef AFS_DEMAND_ATTACH_FS
2876 * host state serialization
2878 static int h_stateFillHeader(struct host_state_header * hdr);
2879 static int h_stateCheckHeader(struct host_state_header * hdr);
2880 static int h_stateAllocMap(struct fs_dump_state * state);
2881 static int h_stateSaveHost(struct host * host, void *rock);
2882 static int h_stateRestoreHost(struct fs_dump_state * state);
2883 static int h_stateRestoreIndex(struct host * h, void *rock);
2884 static int h_stateVerifyHost(struct host * h, void *rock);
2885 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
2886 afs_uint32 addr, afs_uint16 port, int valid);
2887 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2888 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2889 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2892 * Is this host busy?
2894 * This is just a hint and should not be trusted; this should probably only be
2895 * used by the host state serialization code when trying to detect if a host
2896 * can be sanely serialized to disk or not. If this function returns 1, the
2897 * host may be in an invalid state and thus should not be saved to disk.
2900 h_isBusy_r(struct host *host)
2902 struct Lock *hostLock = &host->lock;
2905 LOCK_LOCK(hostLock);
2906 if (hostLock->excl_locked || hostLock->readers_reading) {
2909 LOCK_UNLOCK(hostLock);
2915 if ((host->hostFlags & HWHO_INPROGRESS) || !(host->hostFlags & ALTADDR)) {
2916 /* We shouldn't hit this if the host wasn't locked, but just in case... */
2923 /* this procedure saves all host state to disk for fast startup */
2925 h_stateSave(struct fs_dump_state * state)
2927 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2930 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2932 /* invalidate host state header */
2933 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2935 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2936 sizeof(struct host_state_header))) {
2941 fs_stateIncEOF(state, sizeof(struct host_state_header));
2943 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2948 h_stateFillHeader(state->h_hdr);
2950 /* write the real header to disk */
2951 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2952 sizeof(struct host_state_header));
2959 * host state serialization
2961 * this procedure restores all host state from a disk for fast startup
2964 h_stateRestore(struct fs_dump_state * state)
2968 /* seek to the right position and read in the host state header */
2969 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2970 sizeof(struct host_state_header))) {
2975 /* check the validity of the header */
2976 if (h_stateCheckHeader(state->h_hdr)) {
2981 records = state->h_hdr->records;
2983 if (h_stateAllocMap(state)) {
2988 /* iterate over records restoring host state */
2989 for (i=0; i < records; i++) {
2990 if (h_stateRestoreHost(state) != 0) {
3001 h_stateRestoreIndices(struct fs_dump_state * state)
3003 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
3008 h_stateRestoreIndex(struct host * h, void *rock)
3010 struct fs_dump_state *state = (struct fs_dump_state *)rock;
3011 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
3012 return H_ENUMERATE_BAIL(0);
3018 h_stateVerify(struct fs_dump_state * state)
3020 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
3025 h_stateVerifyHost(struct host * h, void* rock)
3027 struct fs_dump_state *state = (struct fs_dump_state *)rock;
3031 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
3032 return H_ENUMERATE_BAIL(0);
3036 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
3037 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
3038 h->interface->interface[i].port,
3039 h->interface->interface[i].valid)) {
3043 if (h_stateVerifyUuidHash(state, h)) {
3046 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port, 1)) {
3050 if (cb_stateVerifyHCBList(state, h)) {
3058 * verify a host is either in, or absent from, the addr hash table.
3060 * @param[in] state fs dump state
3061 * @param[in] h host we're dealing with
3062 * @param[in] addr addr to look for (NBO)
3063 * @param[in] port port to look for (NBO)
3064 * @param[in] valid 1 if we're verifying that the specified addr and port
3065 * in the hash table point to the specified host. 0 if we're
3066 * verifying that the specified addr and port do NOT point
3067 * to the specified host
3069 * @return operation status
3070 * @retval 1 failed to verify, bail out
3071 * @retval 0 verified successfully, all is well
3074 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
3075 afs_uint32 addr, afs_uint16 port, int valid)
3077 int ret = 0, found = 0;
3078 struct host *host = NULL;
3079 struct h_AddrHashChain *chain;
3080 int index = h_HashIndex(addr);
3084 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
3085 host = chain->hostPtr;
3087 afs_inet_ntoa_r(addr, tmp);
3088 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
3092 if ((chain->addr == addr) && (chain->port == port)) {
3095 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry "
3096 "points to different host struct (%d, %d)\n",
3097 h->index, host->index));
3098 state->flags.warnings_generated = 1;
3102 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s:%u is "
3103 "marked invalid, but points to the containing "
3104 "host\n", afs_inet_ntoa_r(addr, tmp),
3105 (unsigned)htons(port)));
3113 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
3114 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
3115 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
3122 if (!found && valid) {
3123 afs_inet_ntoa_r(addr, tmp);
3124 if (state->mode == FS_STATE_LOAD_MODE) {
3125 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s:%u not found in hash\n",
3126 tmp, (unsigned)htons(port)));
3130 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s:%u not found in hash\n",
3131 tmp, (unsigned)htons(port)));
3132 state->flags.warnings_generated = 1;
3141 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
3143 int ret = 0, found = 0;
3144 struct host *host = NULL;
3145 struct h_UuidHashChain *chain;
3146 afsUUID * uuidp = &h->interface->uuid;
3147 int index = h_UuidHashIndex(uuidp);
3151 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
3152 host = chain->hostPtr;
3154 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
3155 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
3159 if (host->interface &&
3160 afs_uuid_equal(&host->interface->uuid, uuidp)) {
3162 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
3163 h->index, host->index));
3164 state->flags.warnings_generated = 1;
3169 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
3170 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
3171 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
3179 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
3180 if (state->mode == FS_STATE_LOAD_MODE) {
3181 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
3185 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
3186 state->flags.warnings_generated = 1;
3194 /* create the host state header structure */
3196 h_stateFillHeader(struct host_state_header * hdr)
3198 hdr->stamp.magic = HOST_STATE_MAGIC;
3199 hdr->stamp.version = HOST_STATE_VERSION;
3203 /* check the contents of the host state header structure */
3205 h_stateCheckHeader(struct host_state_header * hdr)
3209 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
3210 ViceLog(0, ("check_host_state_header: invalid state header\n"));
3213 else if (hdr->stamp.version != HOST_STATE_VERSION) {
3214 ViceLog(0, ("check_host_state_header: unknown version number\n"));
3220 /* allocate the host id mapping table */
3222 h_stateAllocMap(struct fs_dump_state * state)
3224 state->h_map.len = state->h_hdr->index_max + 1;
3225 state->h_map.entries = (struct idx_map_entry_t *)
3226 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
3227 return (state->h_map.entries != NULL) ? 0 : 1;
3230 /* function called by h_Enumerate to save a host to disk */
3232 h_stateSaveHost(struct host * host, void* rock)
3234 struct fs_dump_state *state = (struct fs_dump_state *) rock;
3235 int if_len=0, hcps_len=0;
3236 struct hostDiskEntry hdsk;
3237 struct host_state_entry_header hdr;
3238 struct Interface * ifp = NULL;
3239 afs_int32 * hcps = NULL;
3240 struct iovec iov[4];
3243 if (h_isBusy_r(host)) {
3245 ViceLog(1, ("Not saving host %s:%d to disk; host appears busy\n",
3246 afs_inet_ntoa_r(host->host, hoststr), (int)ntohs(host->port)));
3247 /* Make sure we don't try to save callbacks to disk for this host, or
3248 * we'll get confused on restore */
3249 DeleteAllCallBacks_r(host, 1);
3253 memset(&hdr, 0, sizeof(hdr));
3255 if (state->h_hdr->index_max < host->index) {
3256 state->h_hdr->index_max = host->index;
3259 h_hostToDiskEntry_r(host, &hdsk);
3260 if (host->interface) {
3261 if_len = sizeof(struct Interface) +
3262 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
3263 ifp = (struct Interface *) malloc(if_len);
3264 osi_Assert(ifp != NULL);
3265 memcpy(ifp, host->interface, if_len);
3266 hdr.interfaces = host->interface->numberOfInterfaces;
3267 iov[iovcnt].iov_base = (char *) ifp;
3268 iov[iovcnt].iov_len = if_len;
3271 if (host->hcps.prlist_val) {
3272 hdr.hcps = host->hcps.prlist_len;
3273 hcps_len = hdr.hcps * sizeof(afs_int32);
3274 hcps = (afs_int32 *) malloc(hcps_len);
3275 osi_Assert(hcps != NULL);
3276 memcpy(hcps, host->hcps.prlist_val, hcps_len);
3277 iov[iovcnt].iov_base = (char *) hcps;
3278 iov[iovcnt].iov_len = hcps_len;
3282 if (hdsk.index > state->h_hdr->index_max)
3283 state->h_hdr->index_max = hdsk.index;
3285 hdr.len = sizeof(struct host_state_entry_header) +
3286 sizeof(struct hostDiskEntry) + if_len + hcps_len;
3287 hdr.magic = HOST_STATE_ENTRY_MAGIC;
3289 iov[0].iov_base = (char *) &hdr;
3290 iov[0].iov_len = sizeof(hdr);
3291 iov[1].iov_base = (char *) &hdsk;
3292 iov[1].iov_len = sizeof(struct hostDiskEntry);
3294 if (fs_stateWriteV(state, iov, iovcnt)) {
3295 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
3299 fs_stateIncEOF(state, hdr.len);
3301 state->h_hdr->records++;
3308 return H_ENUMERATE_BAIL(0);
3313 /* restores a host from disk */
3315 h_stateRestoreHost(struct fs_dump_state * state)
3317 int ifp_len=0, hcps_len=0, bail=0;
3318 struct host_state_entry_header hdr;
3319 struct hostDiskEntry hdsk;
3320 struct host *host = NULL;
3321 struct Interface *ifp = NULL;
3322 afs_int32 * hcps = NULL;
3323 struct iovec iov[3];
3326 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
3327 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
3333 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
3334 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
3340 iov[0].iov_base = (char *) &hdsk;
3341 iov[0].iov_len = sizeof(struct hostDiskEntry);
3343 if (hdr.interfaces) {
3344 ifp_len = sizeof(struct Interface) +
3345 ((hdr.interfaces-1) * sizeof(struct AddrPort));
3346 ifp = (struct Interface *) malloc(ifp_len);
3347 osi_Assert(ifp != NULL);
3348 iov[iovcnt].iov_base = (char *) ifp;
3349 iov[iovcnt].iov_len = ifp_len;
3353 hcps_len = hdr.hcps * sizeof(afs_int32);
3354 hcps = (afs_int32 *) malloc(hcps_len);
3355 osi_Assert(hcps != NULL);
3356 iov[iovcnt].iov_base = (char *) hcps;
3357 iov[iovcnt].iov_len = hcps_len;
3361 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
3362 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
3367 if (fs_stateReadV(state, iov, iovcnt)) {
3368 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
3373 if (!hdr.hcps && hdsk.hcps_valid) {
3374 /* valid, zero-length host cps ; does this ever happen? */
3375 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
3376 osi_Assert(hcps != NULL);
3379 if ((hdsk.hostFlags & HWHO_INPROGRESS) || !(hdsk.hostFlags & ALTADDR)) {
3381 ViceLog(0, ("h_stateRestoreHost: skipping host %s:%d due to invalid flags 0x%x\n",
3382 afs_inet_ntoa_r(hdsk.host, hoststr), (int)ntohs(hdsk.port),
3383 (unsigned)hdsk.hostFlags));
3385 state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_SKIPPED;
3389 /* for restoring state, we better be able to get a host! */
3391 osi_Assert(host != NULL);
3394 host->interface = ifp;
3397 host->hcps.prlist_val = hcps;
3398 host->hcps.prlist_len = hdr.hcps;
3401 h_diskEntryToHost_r(&hdsk, host);
3402 h_SetupCallbackConn_r(host);
3404 h_AddHostToAddrHashTable_r(host->host, host->port, host);
3407 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
3408 if (ifp->interface[i].valid &&
3409 !(ifp->interface[i].addr == host->host &&
3410 ifp->interface[i].port == host->port)) {
3411 h_AddHostToAddrHashTable_r(ifp->interface[i].addr,
3412 ifp->interface[i].port,
3416 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
3418 h_InsertList_r(host);
3420 /* setup host id map entry */
3421 state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_VALID;
3422 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
3423 state->h_map.entries[hdsk.index].new_idx = host->index;
3435 /* serialize a host structure to disk */
3437 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
3439 out->host = in->host;
3440 out->port = in->port;
3441 out->hostFlags = in->hostFlags;
3442 out->Console = in->Console;
3443 out->hcpsfailed = in->hcpsfailed;
3444 out->LastCall = in->LastCall;
3445 out->ActiveCall = in->ActiveCall;
3446 out->cpsCall = in->cpsCall;
3447 out->cblist = in->cblist;
3448 out->InSameNetwork = in->InSameNetwork;
3450 /* special fields we save, but are not memcpy'd back on restore */
3451 out->index = in->index;
3452 out->hcps_len = in->hcps.prlist_len;
3453 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
3456 /* restore a host structure from disk */
3458 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
3460 out->host = in->host;
3461 out->port = in->port;
3462 out->hostFlags = in->hostFlags;
3463 out->Console = in->Console;
3464 out->hcpsfailed = in->hcpsfailed;
3465 out->LastCall = in->LastCall;
3466 out->ActiveCall = in->ActiveCall;
3467 out->cpsCall = in->cpsCall;
3468 out->cblist = in->cblist;
3469 out->InSameNetwork = in->InSameNetwork;
3472 /* index translation routines */
3474 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
3478 /* hosts use a zero-based index, so old==0 is valid */
3480 if (old >= state->h_map.len) {
3481 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
3483 } else if (state->h_map.entries[old].valid != FS_STATE_IDX_VALID ||
3484 state->h_map.entries[old].old_idx != old) { /* sanity check */
3485 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
3488 *new = state->h_map.entries[old].new_idx;
3493 #endif /* AFS_DEMAND_ATTACH_FS */
3497 * This counts the number of workstations, the number of active workstations,
3498 * and the number of workstations declared "down" (i.e. not heard from
3499 * recently). An active workstation has received a call since the cutoff
3500 * time argument passed.
3503 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3506 int num = 0, active = 0, del = 0;
3510 for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
3511 if (!(host->hostFlags & HOSTDELETED)) {
3513 if (host->ActiveCall > cutofftime)
3515 if (host->hostFlags & VENUSDOWN)
3519 if (count != hostCount) {
3520 ViceLog(0, ("h_GetWorkStats found %d of %d hosts\n", count, hostCount));
3521 } else if (host != NULL) {
3522 ViceLog(0, ("h_GetWorkStats found more than %d hosts\n", hostCount));
3523 ShutDownAndCore(PANIC);
3533 } /*h_GetWorkStats */
3536 h_GetWorkStats64(afs_uint64 *nump, afs_uint64 *activep, afs_uint64 *delp,
3537 afs_int32 cutofftime)
3539 int num, active, del;
3540 h_GetWorkStats(&num, &active, &del, cutofftime);
3549 /*------------------------------------------------------------------------
3550 * PRIVATE h_ClassifyAddress
3553 * Given a target IP address and a candidate IP address (both
3554 * in host byte order), classify the candidate into one of three
3555 * buckets in relation to the target by bumping the counters passed
3559 * a_targetAddr : Target address.
3560 * a_candAddr : Candidate address.
3561 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3562 * addresses are either in the same network
3563 * or the same subnet.
3564 * a_diffSubnetP : ...when the candidate is in a different
3566 * a_diffNetworkP : ...when the candidate is in a different
3573 * The target and candidate addresses are both in host byte
3574 * order, NOT network byte order, when passed in.
3578 *------------------------------------------------------------------------*/
3581 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3582 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3583 afs_int32 * a_diffNetworkP)
3584 { /*h_ClassifyAddress */
3586 afs_uint32 targetNet;
3587 afs_uint32 targetSubnet;
3589 afs_uint32 candSubnet;
3592 * Put bad values into the subnet info to start with.
3594 targetSubnet = (afs_uint32) 0;
3595 candSubnet = (afs_uint32) 0;
3598 * Pull out the network and subnetwork numbers from the target
3599 * and candidate addresses. We can short-circuit this whole
3600 * affair if the target and candidate addresses are not of the
3603 if (IN_CLASSA(a_targetAddr)) {
3604 if (!(IN_CLASSA(a_candAddr))) {
3605 (*a_diffNetworkP)++;
3608 targetNet = a_targetAddr & IN_CLASSA_NET;
3609 candNet = a_candAddr & IN_CLASSA_NET;
3610 if (IN_SUBNETA(a_targetAddr))
3611 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
3612 if (IN_SUBNETA(a_candAddr))
3613 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
3614 } else if (IN_CLASSB(a_targetAddr)) {
3615 if (!(IN_CLASSB(a_candAddr))) {
3616 (*a_diffNetworkP)++;
3619 targetNet = a_targetAddr & IN_CLASSB_NET;
3620 candNet = a_candAddr & IN_CLASSB_NET;
3621 if (IN_SUBNETB(a_targetAddr))
3622 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
3623 if (IN_SUBNETB(a_candAddr))
3624 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
3625 } /*Class B target */
3626 else if (IN_CLASSC(a_targetAddr)) {
3627 if (!(IN_CLASSC(a_candAddr))) {
3628 (*a_diffNetworkP)++;
3631 targetNet = a_targetAddr & IN_CLASSC_NET;
3632 candNet = a_candAddr & IN_CLASSC_NET;
3635 * Note that class C addresses can't have subnets,
3636 * so we leave the defaults untouched.
3638 } /*Class C target */
3640 targetNet = a_targetAddr;
3641 candNet = a_candAddr;
3642 } /*Class D address */
3645 * Now, simply compare the extracted net and subnet values for
3646 * the two addresses (which at this point are known to be of the
3649 if (targetNet == candNet) {
3650 if (targetSubnet == candSubnet)
3651 (*a_sameNetOrSubnetP)++;
3655 (*a_diffNetworkP)++;
3657 } /*h_ClassifyAddress */
3660 /*------------------------------------------------------------------------
3661 * EXPORTED h_GetHostNetStats
3664 * Iterate through the host table, and classify each (non-deleted)
3665 * host entry into ``proximity'' categories (same net or subnet,
3666 * different subnet, different network).
3669 * a_numHostsP : Set to total number of (non-deleted) hosts.
3670 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
3671 * a_diffSubnetP : Set to # hosts on diff subnet as server.
3672 * a_diffNetworkP : Set to # hosts on diff network as server.
3678 * We only count non-deleted hosts. The storage pointed to by our
3679 * parameters is zeroed upon entry.
3683 *------------------------------------------------------------------------*/
3686 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
3687 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
3688 { /*h_GetHostNetStats */
3690 struct host *hostP; /*Ptr to current host entry */
3691 afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
3695 * Clear out the storage pointed to by our parameters.
3697 *a_numHostsP = (afs_int32) 0;
3698 *a_sameNetOrSubnetP = (afs_int32) 0;
3699 *a_diffSubnetP = (afs_int32) 0;
3700 *a_diffNetworkP = (afs_int32) 0;
3703 for (count = 0, hostP = hostList; hostP && count < hostCount; hostP = hostP->next, count++) {
3704 if (!(hostP->hostFlags & HOSTDELETED)) {
3706 * Bump the number of undeleted host entries found.
3707 * In classifying the current entry's address, make
3708 * sure to first convert to host byte order.
3711 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
3712 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
3713 a_sameNetOrSubnetP, a_diffSubnetP,
3715 } /*Only look at non-deleted hosts */
3716 } /*For each host record hashed to this index */
3717 if (count != hostCount) {
3718 ViceLog(0, ("h_GetHostNetStats found %d of %d hosts\n", count, hostCount));
3719 } else if (hostP != NULL) {
3720 ViceLog(0, ("h_GetHostNetStats found more than %d hosts\n", hostCount));
3721 ShutDownAndCore(PANIC);
3724 } /*h_GetHostNetStats */
3726 static afs_uint32 checktime;
3727 static afs_uint32 clientdeletetime;
3728 static struct AFSFid zerofid;
3732 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
3733 * Since it can serialize them, and pile up, it should be a separate LWP
3734 * from other events.
3738 CheckHost(struct host *host, int flags, void *rock)
3740 struct client *client;
3741 struct rx_connection *cb_conn = NULL;
3744 #ifdef AFS_DEMAND_ATTACH_FS
3745 /* kill the checkhost lwp ASAP during shutdown */
3747 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3749 return H_ENUMERATE_BAIL(flags);
3754 /* Host is held by h_Enumerate */
3756 for (client = host->FirstClient; client; client = client->next) {
3757 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3758 client->deleted = 1;
3759 host->hostFlags |= CLIENTDELETED;
3762 if (host->LastCall < checktime) {
3764 if (!(host->hostFlags & HOSTDELETED)) {
3765 host->hostFlags |= HWHO_INPROGRESS;
3766 cb_conn = host->callback_rxcon;
3767 rx_GetConnection(cb_conn);
3768 if (host->LastCall < clientdeletetime) {
3769 host->hostFlags |= HOSTDELETED;
3770 if (!(host->hostFlags & VENUSDOWN)) {
3771 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3772 if (host->interface) {
3775 RXAFSCB_InitCallBackState3(cb_conn,
3781 RXAFSCB_InitCallBackState(cb_conn);
3784 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3787 (void)afs_inet_ntoa_r(host->host, hoststr);
3789 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3790 hoststr, ntohs(host->port)));
3791 host->hostFlags |= VENUSDOWN;
3793 /* Note: it's safe to delete hosts even if they have call
3794 * back state, because break delayed callbacks (called when a
3795 * message is received from the workstation) will always send a
3796 * break all call backs to the workstation if there is no
3801 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3803 (void)afs_inet_ntoa_r(host->host, hoststr);
3804 if (host->interface) {
3805 afsUUID uuid = host->interface->uuid;
3807 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3810 if (MultiProbeAlternateAddress_r(host)) {
3811 ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
3812 hoststr, ntohs(host->port), code));
3813 host->hostFlags |= VENUSDOWN;
3818 code = RXAFSCB_Probe(cb_conn);
3822 ("CheckHost: Probe failed for host %s:%d, code %d\n",
3823 hoststr, ntohs(host->port), code));
3824 host->hostFlags |= VENUSDOWN;
3830 rx_PutConnection(cb_conn);
3833 host->hostFlags &= ~HWHO_INPROGRESS;
3844 CheckHost_r(struct host *host, void *dummy)
3846 struct client *client;
3847 struct rx_connection *cb_conn = NULL;
3850 #ifdef AFS_DEMAND_ATTACH_FS
3851 /* kill the checkhost lwp ASAP during shutdown */
3853 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3855 return H_ENUMERATE_BAIL(0);
3860 /* Host is held by h_Enumerate_r */
3861 for (client = host->FirstClient; client; client = client->next) {
3862 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3863 client->deleted = 1;
3864 host->hostFlags |= CLIENTDELETED;
3867 if (host->LastCall < checktime) {
3869 if (!(host->hostFlags & HOSTDELETED)) {
3870 host->hostFlags |= HWHO_INPROGRESS;
3871 cb_conn = host->callback_rxcon;
3872 rx_GetConnection(cb_conn);
3873 if (host->LastCall < clientdeletetime) {
3874 host->hostFlags |= HOSTDELETED;
3875 if (!(host->hostFlags & VENUSDOWN)) {
3876 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3877 if (host->interface) {
3880 RXAFSCB_InitCallBackState3(cb_conn,
3886 RXAFSCB_InitCallBackState(cb_conn);
3889 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3892 (void)afs_inet_ntoa_r(host->host, hoststr);
3894 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3895 hoststr, ntohs(host->port)));
3896 host->hostFlags |= VENUSDOWN;
3898 /* Note: it's safe to delete hosts even if they have call
3899 * back state, because break delayed callbacks (called when a
3900 * message is received from the workstation) will always send a
3901 * break all call backs to the workstation if there is no
3906 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3908 (void)afs_inet_ntoa_r(host->host, hoststr);
3909 if (host->interface) {
3910 afsUUID uuid = host->interface->uuid;
3912 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3915 if (MultiProbeAlternateAddress_r(host)) {
3916 ViceLog(0,("CheckHost_r: Probing all interfaces of host %s:%d failed, code %d\n",
3917 hoststr, ntohs(host->port), code));
3918 host->hostFlags |= VENUSDOWN;
3923 code = RXAFSCB_Probe(cb_conn);
3927 ("CheckHost_r: Probe failed for host %s:%d, code %d\n",
3928 hoststr, ntohs(host->port), code));
3929 host->hostFlags |= VENUSDOWN;
3935 rx_PutConnection(cb_conn);
3938 host->hostFlags &= ~HWHO_INPROGRESS;
3948 * Set VenusDown for any hosts that have not had a call in 15 minutes and
3949 * don't respond to a probe. Note that VenusDown can only be cleared if
3950 * a message is received from the host (see ServerLWP in file.c).
3951 * Delete hosts that have not had any calls in 1 hour, clients that
3952 * have not had any calls in 15 minutes.
3954 * This routine is called roughly every 5 minutes.
3959 afs_uint32 now = FT_ApproxTime();
3961 memset(&zerofid, 0, sizeof(zerofid));
3963 * Send a probe to the workstation if it hasn't been heard from in
3966 checktime = now - 15 * 60;
3967 clientdeletetime = now - 120 * 60; /* 2 hours ago */
3970 h_Enumerate_r(CheckHost_r, hostList, NULL);
3975 * This is called with host locked and held. At this point, the
3976 * hostAddrHashTable has an entry for the primary addr/port inserted
3977 * by h_Alloc_r(). No other interfaces should be considered valid.
3979 * The addresses in the interfaceAddr list are in host byte order.
3982 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
3989 struct Interface *interface;
3992 afs_uint16 port7001 = htons(7001);
3997 number = interf->numberOfInterfaces;
3998 myAddr = host->host; /* current interface address */
3999 myPort = host->port; /* current port */
4002 ("initInterfaceAddr : host %s:%d numAddr %d\n",
4003 afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
4005 /* validation checks */
4006 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
4008 ("Invalid number of alternate addresses is %d\n", number));
4013 * The client's notion of its own IP addresses is not reliable.
4015 * 1. The client list might contain private address ranges which
4016 * are likely to be re-used by many clients allocated addresses
4019 * 2. The client list will not include any public addresses that
4020 * are hidden by a NAT.
4022 * 3. Private address ranges that are exposed to the server will
4023 * be obtained from the rx connections that use them.
4025 * 4. Lists provided by the client are not necessarily truthful.
4026 * Many existing clients (UNIX) do not refresh the IP address
4027 * list as the actual assigned addresses change. The end result
4028 * is that they report the initial address list for the lifetime
4029 * of the process. In other words, a client can report addresses
4030 * that they are in fact not using. Adding these addresses to
4031 * the host interface list without verification is not only
4032 * pointless, it is downright dangerous.
4034 * We therefore do not add alternate addresses to the addr hash table.
4035 * We only use them for multi-rx callback breaks.
4039 * Convert IP addresses to network byte order, and remove
4040 * duplicate and loopback IP addresses from the interface list, and
4041 * determine whether or not the incoming addr/port is
4042 * listed. Note that if the address matches it is not
4043 * truly a match because the port number for the entries
4044 * in the interface list are port 7001 and the port number
4045 * for this connection might not be 7001.
4047 for (i = 0, count = 0, found = 0; i < number; i++) {
4048 if (rx_IsLoopbackAddr(interf->addr_in[i])) {
4051 interf->addr_in[i] = htonl(interf->addr_in[i]);
4052 for (j = 0; j < count; j++) {
4053 if (interf->addr_in[j] == interf->addr_in[i])
4057 interf->addr_in[count] = interf->addr_in[i];
4058 if (interf->addr_in[count] == myAddr &&
4066 * Allocate and initialize an interface structure for this host.
4069 interface = (struct Interface *)
4070 malloc(sizeof(struct Interface) +
4071 (sizeof(struct AddrPort) * (count - 1)));
4073 ViceLogThenPanic(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
4075 interface->numberOfInterfaces = count;
4077 interface = (struct Interface *)
4078 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
4080 ViceLogThenPanic(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
4082 interface->numberOfInterfaces = count + 1;
4083 interface->interface[count].addr = myAddr;
4084 interface->interface[count].port = myPort;
4085 interface->interface[count].valid = 1;
4088 for (i = 0; i < count; i++) {
4090 interface->interface[i].addr = interf->addr_in[i];
4091 /* We store the port as 7001 because the addresses reported by
4092 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
4093 * are coming from fully connected hosts (no NAT/PATs)
4095 interface->interface[i].port = port7001;
4096 interface->interface[i].valid =
4097 (interf->addr_in[i] == myAddr && port7001 == myPort) ? 1 : 0;
4100 interface->uuid = interf->uuid;
4102 osi_Assert(!host->interface);
4103 host->interface = interface;
4105 if (LogLevel >= 125) {
4106 afsUUID_to_string(&interface->uuid, uuidstr, 127);
4108 ViceLog(125, ("--- uuid %s\n", uuidstr));
4109 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
4110 ViceLog(125, ("--- alt address %s:%d\n",
4111 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
4112 ntohs(host->interface->interface[i].port)));
4119 /* deleted a HashChain structure for this address and host */
4120 /* returns 1 on success */
4122 h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port,
4126 struct h_AddrHashChain **hp, *th;
4128 if (addr == 0 && port == 0)
4131 for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);
4133 osi_Assert(th->hostPtr);
4134 if (th->hostPtr == host && th->addr == addr && th->port == port) {
4135 ViceLog(125, ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d)\n",
4136 host, afs_inet_ntoa_r(host->host, hoststr),
4137 ntohs(host->port)));
4144 ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) not found\n",
4145 host, afs_inet_ntoa_r(host->host, hoststr),
4146 ntohs(host->port)));
4152 ** prints out all alternate interface address for the host. The 'level'
4153 ** parameter indicates what level of debugging sets this output
4156 printInterfaceAddr(struct host *host, int level)
4161 if (host->interface) {
4162 /* check alternate addresses */
4163 number = host->interface->numberOfInterfaces;
4165 ViceLog(level, ("no-addresses "));
4167 for (i = 0; i < number; i++)
4168 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
4169 ntohs(host->interface->interface[i].port)));
4172 ViceLog(level, ("\n"));