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>
20 #ifdef HAVE_SYS_FILE_H
24 #include <afs/afsint.h>
25 #define FSINT_COMMON_XG
26 #include <afs/afscbint.h>
27 #include <afs/rxgen_consts.h>
29 #include <afs/errors.h>
30 #include <afs/ihandle.h>
32 #include <afs/ptclient.h>
33 #include <afs/ptuser.h>
34 #include <afs/prs_fs.h>
36 #include <afs/afsutil.h>
37 #include <afs/com_err.h>
39 #include <afs/cellconfig.h>
40 #include "viced_prototypes.h"
44 #ifdef AFS_DEMAND_ATTACH_FS
45 #include "../util/afsutil_prototypes.h"
46 #include "serialize_state.h"
47 #endif /* AFS_DEMAND_ATTACH_FS */
49 pthread_mutex_t host_glock_mutex;
52 extern int CurrentConnections;
54 extern int AnonymousID;
55 extern prlist AnonCPS;
56 extern struct afsconf_dir *confDir; /* config dir object */
57 extern int lwps; /* the max number of server threads */
58 extern afsUUID FS_HostUUID;
59 extern char *FS_configPath;
62 int CEs = 0; /* active clients */
63 int CEBlocks = 0; /* number of blocks of CEs */
64 struct client *CEFree = 0; /* first free client */
65 struct host *hostList = 0; /* linked list of all hosts */
66 int hostCount = 0; /* number of hosts in hostList */
70 static struct rx_securityClass *sc = NULL;
71 static int h_quota_limit;
73 /* arguments for PerHost_EnumerateClient enumeration */
74 struct enumclient_args {
76 int (*proc)(struct client *client, void *rock);
80 static void h_SetupCallbackConn_r(struct host * host);
81 static int h_threadquota(int);
82 static int initInterfaceAddr_r(struct host *, struct interfaceAddr *);
84 #define CESPERBLOCK 73
85 struct CEBlock { /* block of CESPERBLOCK file entries */
86 struct client entry[CESPERBLOCK];
89 void h_TossStuff_r(struct host *host);
92 * Make sure the subnet macros have been defined.
95 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
98 #ifndef IN_CLASSA_SUBNET
99 #define IN_CLASSA_SUBNET 0xffff0000
103 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
106 #ifndef IN_CLASSB_SUBNET
107 #define IN_CLASSB_SUBNET 0xffffff00
110 #define hostBusyFlags(hf) \
111 ((hf) & (HWHO_INPROGRESS | HCPS_INPROGRESS | HCPS_WAITING) \
112 || !((hf) & ALTADDR))
114 /* get a new block of CEs and chain it on CEFree */
118 struct CEBlock *block;
121 block = malloc(sizeof(struct CEBlock));
123 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
124 ShutDownAndCore(PANIC);
127 for (i = 0; i < (CESPERBLOCK - 1); i++) {
128 Lock_Init(&block->entry[i].lock);
129 block->entry[i].z.next = &(block->entry[i + 1]);
131 block->entry[CESPERBLOCK - 1].z.next = 0;
132 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
133 CEFree = (struct client *)block;
139 /* get the next available CE */
140 static struct client *
143 struct client *entry;
148 ViceLog(0, ("CEFree NULL in GetCE\n"));
149 ShutDownAndCore(PANIC);
153 CEFree = entry->z.next;
155 memset(&entry->z, 0, sizeof(struct client_to_zero));
161 /* return an entry to the free list */
163 FreeCE(struct client *entry)
165 entry->z.VenusEpoch = 0;
167 entry->z.next = CEFree;
174 * The HTs and HTBlocks variables were formerly static, but they are
175 * now referenced elsewhere in the FileServer.
177 int HTs = 0; /* active file entries */
178 int HTBlocks = 0; /* number of blocks of HTs */
179 static struct host *HTFree = 0; /* first free file entry */
182 * Hash tables of host pointers. We need two tables, one
183 * to map IP addresses onto host pointers, and another
184 * to map host UUIDs onto host pointers.
186 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
187 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
188 #define h_HashIndex(hostip) (ntohl(hostip) & (h_HASHENTRIES-1))
189 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
191 struct HTBlock { /* block of HTSPERBLOCK file entries */
192 struct host entry[h_HTSPERBLOCK];
196 /* get a new block of HTs and chain it on HTFree */
200 struct HTBlock *block;
202 static int index = 0;
204 if (HTBlocks == h_MAXHOSTTABLES) {
205 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
209 block = malloc(sizeof(struct HTBlock));
211 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
212 ShutDownAndCore(PANIC);
214 for (i = 0; i < (h_HTSPERBLOCK); i++)
215 opr_cv_init(&block->entry[i].cond);
216 for (i = 0; i < (h_HTSPERBLOCK); i++)
217 Lock_Init(&block->entry[i].lock);
218 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
219 block->entry[i].z.next = &(block->entry[i + 1]);
220 for (i = 0; i < (h_HTSPERBLOCK); i++)
221 block->entry[i].index = index++;
222 block->entry[h_HTSPERBLOCK - 1].z.next = 0;
223 HTFree = (struct host *)block;
224 hosttableptrs[HTBlocks++] = block->entry;
229 /* get the next available HT */
240 HTFree = entry->z.next;
242 memset(&entry->z, 0, sizeof(struct host_to_zero));
248 /* return an entry to the free list */
250 FreeHT(struct host *entry)
252 entry->z.next = HTFree;
259 hpr_Initialize(struct ubik_client **uclient)
261 afs_int32 code, code2;
262 struct rx_connection *serverconns[MAXSERVERS];
263 struct rx_securityClass *sc;
264 struct afsconf_dir *tdir;
266 struct afsconf_cell info;
270 tdir = afsconf_Open(FS_configPath);
273 ("hpr_Initialize: Could not open configuration directory: %s\n",
278 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
280 ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]\n", code));
285 code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
287 ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s\n",
288 cellstr, tdir->name, AFSDIR_CELLSERVDB_FILE));
295 ViceLog(0, ("hpr_Initialize: Could not initialize rx.\n"));
300 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
301 * to force use of the KeyFile. secLevel == 0 implies -noauth was
303 code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
305 ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s "
306 "(so trying noauth)\n", code, afs_error_message(code)));
307 scIndex = RX_SECIDX_NULL;
308 sc = rxnull_NewClientSecurityObject();
311 if (scIndex == RX_SECIDX_NULL)
312 ViceLog(0, ("hpr_Initialize: Could not get afs tokens, "
313 "running unauthenticated. [%d]\n", code));
315 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
316 for (i = 0; i < info.numServers; i++) {
318 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
319 info.hostAddr[i].sin_port, PRSRV,
323 code = ubik_ClientInit(serverconns, uclient);
325 ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]\n", code));
328 code2 = rxs_Release(sc);
337 hpr_End(struct ubik_client *uclient)
342 code = ubik_ClientDestroy(uclient);
348 getThreadClient(struct ubik_client **client)
352 *client = pthread_getspecific(viced_uclient_key);
356 code = hpr_Initialize(client);
360 opr_Verify(pthread_setspecific(viced_uclient_key, *client) == 0);
366 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
370 struct ubik_client *uclient;
372 code = getThreadClient(&uclient);
377 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
378 if (code != PRSUCCESS)
381 /* do something about this, probably make a new call */
382 /* don't forget there's a hard limit in the interface */
384 "membership list for host id %d exceeds display limit\n",
391 hpr_NameToId(namelist *names, idlist *ids)
395 struct ubik_client *uclient;
397 code = getThreadClient(&uclient);
401 for (i = 0; i < names->namelist_len; i++)
402 stolower(names->namelist_val[i]);
403 code = ubik_PR_NameToID(uclient, 0, names, ids);
408 hpr_IdToName(idlist *ids, namelist *names)
411 struct ubik_client *uclient;
413 code = getThreadClient(&uclient);
417 code = string_PR_IDToName(uclient, 0, ids, names);
422 hpr_GetCPS(afs_int32 id, prlist *CPS)
426 struct ubik_client *uclient;
428 code = getThreadClient(&uclient);
433 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
434 if (code != PRSUCCESS)
437 /* do something about this, probably make a new call */
438 /* don't forget there's a hard limit in the interface */
439 fprintf(stderr, "membership list for id %d exceeds display limit\n",
445 static short consolePort = 0;
448 h_Lock_r(struct host *host)
458 * returns 1 if already locked
459 * else returns locks and returns 0
463 h_NBLock_r(struct host *host)
465 struct Lock *hostLock = &host->lock;
470 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
471 hostLock->excl_locked = WRITE_LOCK;
475 LOCK_UNLOCK(hostLock);
484 /*------------------------------------------------------------------------
485 * PRIVATE h_AddrInSameNetwork
488 * Given a target IP address and a candidate IP address (both
489 * in host byte order), return a non-zero value (1) if the
490 * candidate address is in a different network from the target
494 * a_targetAddr : Target address.
495 * a_candAddr : Candidate address.
498 * 1 if the candidate address is in the same net as the target,
502 * The target and candidate addresses are both in host byte
503 * order, NOT network byte order, when passed in. We return
504 * our value as a character, since that's the type of field in
505 * the host structure, where this info will be stored.
509 *------------------------------------------------------------------------*/
512 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
513 { /*h_AddrInSameNetwork */
515 afs_uint32 targetNet;
519 * Pull out the network and subnetwork numbers from the target
520 * and candidate addresses. We can short-circuit this whole
521 * affair if the target and candidate addresses are not of the
524 if (IN_CLASSA(a_targetAddr)) {
525 if (!(IN_CLASSA(a_candAddr))) {
528 targetNet = a_targetAddr & IN_CLASSA_NET;
529 candNet = a_candAddr & IN_CLASSA_NET;
530 } else if (IN_CLASSB(a_targetAddr)) {
531 if (!(IN_CLASSB(a_candAddr))) {
534 targetNet = a_targetAddr & IN_CLASSB_NET;
535 candNet = a_candAddr & IN_CLASSB_NET;
536 } /*Class B target */
537 else if (IN_CLASSC(a_targetAddr)) {
538 if (!(IN_CLASSC(a_candAddr))) {
541 targetNet = a_targetAddr & IN_CLASSC_NET;
542 candNet = a_candAddr & IN_CLASSC_NET;
543 } /*Class C target */
545 targetNet = a_targetAddr;
546 candNet = a_candAddr;
547 } /*Class D address */
550 * Now, simply compare the extracted net values for the two addresses
551 * (which at this point are known to be of the same class)
553 if (targetNet == candNet)
558 } /*h_AddrInSameNetwork */
561 /* Assumptions: called with held host */
563 h_gethostcps_r(struct host *host, afs_int32 now)
568 /* wait if somebody else is already doing the getCPS call */
569 while (host->z.hostFlags & HCPS_INPROGRESS) {
570 slept = 1; /* I did sleep */
571 host->z.hostFlags |= HCPS_WAITING; /* I am sleeping now */
572 opr_cv_wait(&host->cond, &host_glock_mutex);
576 host->z.hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
577 if (host->z.hcps.prlist_val)
578 free(host->z.hcps.prlist_val); /* this is for hostaclRefresh */
579 host->z.hcps.prlist_val = NULL;
580 host->z.hcps.prlist_len = 0;
581 host->z.cpsCall = slept ? time(NULL) : (now);
584 code = hpr_GetHostCPS(ntohl(host->z.host), &host->z.hcps);
589 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
590 * and reevaluates things if no sync server or quorum is found we could still end up
591 * with one of these errors. In such case we would like to reevaluate the rpc call to
592 * find if there's cps for this guy. We treat other errors (except network failures
593 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
594 * we could like to deal this problem the other way around (i.e. if code == NOCPS
595 * ignore else retry next time) but the problem is that there're other errors (i.e.
596 * EPERM) for which we don't want to retry and we don't know the whole code list!
598 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
600 * We would have preferred to use a while loop and try again since ops in protected
601 * acls for this host will fail now but they'll be reevaluated on any subsequent
602 * call. The attempt to wait for a quorum/sync site or network error won't work
603 * since this problems really should only occurs during a complete fileserver
604 * restart. Since the fileserver will start before the ptservers (and thus before
605 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
607 host->z.hcpsfailed = 1;
609 ("Warning: GetHostCPS failed (%d) for %p (%s:%d); will retry\n",
610 code, host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
612 host->z.hcpsfailed = 0;
614 ("gethost: GetHostCPS failed (%d) for %p (%s:%d); ignored\n",
615 code, host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
617 if (host->z.hcps.prlist_val)
618 free(host->z.hcps.prlist_val);
619 host->z.hcps.prlist_val = NULL;
620 host->z.hcps.prlist_len = 0; /* Make sure it's zero */
622 host->z.hcpsfailed = 0;
624 host->z.hostFlags &= ~HCPS_INPROGRESS;
625 /* signal all who are waiting */
626 if (host->z.hostFlags & HCPS_WAITING) { /* somebody is waiting */
627 host->z.hostFlags &= ~HCPS_WAITING;
628 opr_cv_broadcast(&host->cond);
632 /* args in net byte order */
634 h_flushhostcps(afs_uint32 hostaddr, afs_uint16 hport)
639 h_Lookup_r(hostaddr, hport, &host);
641 host->z.hcpsfailed = 1;
650 * Allocate a host. It will be identified by the peer (ip,port) info in the
651 * rx connection provided. The host is returned held and locked
653 #define DEF_ROPCONS 2115
656 h_Alloc_r(struct rx_connection *r_con)
658 struct servent *serverentry;
660 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
667 /* acquire the host lock withot dropping H_LOCK. we can do this here
668 * because we know we will not block; we just created this host and
669 * nobody else knows about it. */
670 ObtainWriteLock(&host->lock);
672 host->z.host = rxr_HostOf(r_con);
673 host->z.port = rxr_PortOf(r_con);
675 h_AddHostToAddrHashTable_r(host->z.host, host->z.port, host);
677 if (consolePort == 0) { /* find the portal number for console */
678 serverentry = getservbyname("ropcons", 0);
680 consolePort = serverentry->s_port;
682 consolePort = htons(DEF_ROPCONS); /* Use a default */
684 if (host->z.port == consolePort)
686 /* Make a callback channel even for the console, on the off chance that it
687 * makes a request that causes a break call back. It shouldn't. */
688 h_SetupCallbackConn_r(host);
689 host->z.LastCall = host->z.cpsCall = host->z.ActiveCall = time(NULL);
690 host->z.hostFlags = 0;
691 host->z.hcps.prlist_val = NULL;
692 host->z.hcps.prlist_len = 0;
693 host->z.interface = NULL;
695 host->z.hcpsfailed = 0; /* save cycles */
696 h_gethostcps(host); /* do this under host hold/lock */
698 host->z.FirstClient = NULL;
699 h_InsertList_r(host); /* update global host List */
701 * Compare the new host's IP address (in host byte order) with ours
702 * (the File Server's), remembering if they are in the same network.
704 newHostAddr_HBO = (afs_uint32) ntohl(host->z.host);
705 host->z.InSameNetwork =
706 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
713 /* Make a callback channel even for the console, on the off chance that it
714 * makes a request that causes a break call back. It shouldn't. */
716 h_SetupCallbackConn_r(struct host * host)
719 sc = rxnull_NewClientSecurityObject();
720 host->z.callback_rxcon =
721 rx_NewConnection(host->z.host, host->z.port, 1, sc, 0);
722 rx_SetConnDeadTime(host->z.callback_rxcon, 50);
723 rx_SetConnHardDeadTime(host->z.callback_rxcon, AFS_HARDDEADTIME);
727 * Lookup a host given an IP address and UDP port number.
728 * hostaddr and hport are in network order
729 * hostaddr and hport are in network order
730 * On return, refCount is incremented.
733 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
736 struct host *host = NULL;
737 struct h_AddrHashChain *chain;
738 int index = h_HashIndex(haddr);
739 extern int hostaclRefresh;
742 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
743 host = chain->hostPtr;
745 if (!(host->z.hostFlags & HOSTDELETED) && chain->addr == haddr
746 && chain->port == hport) {
747 if ((host->z.hostFlags & HWHO_INPROGRESS) &&
748 h_threadquota(host->lock.num_waiting)) {
754 if (host->z.hostFlags & HOSTDELETED) {
761 now = time(NULL); /* always evaluate "now" */
762 if (host->z.hcpsfailed || (host->z.cpsCall + hostaclRefresh < now)) {
764 * Every hostaclRefresh period (def 2 hrs) get the new
765 * membership list for the host. Note this could be the
766 * first time that the host is added to a group. Also
767 * here we also retry on previous legitimate hcps failures.
769 * If we get here refCount is elevated.
771 h_gethostcps_r(host, now);
781 /* Lookup a host given its UUID. */
783 h_LookupUuid_r(afsUUID * uuidp)
785 struct host *host = 0;
786 struct h_UuidHashChain *chain;
787 int index = h_UuidHashIndex(uuidp);
789 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
790 host = chain->hostPtr;
792 if (!(host->z.hostFlags & HOSTDELETED) && host->z.interface
793 && afs_uuid_equal(&host->z.interface->uuid, uuidp)) {
801 /* h_TossStuff_r: Toss anything in the host structure (the host or
802 * clients marked for deletion. Called from h_Release_r ONLY.
803 * To be called, there must be no holds, and either host->z.deleted
804 * or host->clientDeleted must be set.
807 h_TossStuff_r(struct host *host)
809 struct client **cp, *client;
813 if ((host->z.hostFlags & HOSTDELETED)) {
817 /* make sure host doesn't go away over h_NBLock_r */
820 code = h_NBLock_r(host);
822 /* don't use h_Release_r, since that may call h_TossStuff_r again */
825 /* if somebody still has this host locked */
829 /* someone locked the host while HOSTDELETED was set; that is bad */
830 ViceLog(0, ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT
831 " (%s:%d flags 0x%x) was locked.\n",
832 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port),
833 (unsigned)host->z.hostFlags));
840 /* if somebody still has this host held */
841 /* we must check this _after_ h_NBLock_r, since h_NBLock_r can drop and
842 * reacquire H_LOCK */
843 if (host->z.refCount > 0) {
846 /* someone grabbed a ref while HOSTDELETED was set; that is bad */
847 ViceLog(0, ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT
848 " (%s:%d flags 0x%x) was held.\n",
849 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port),
850 (unsigned)host->z.hostFlags));
855 /* ASSUMPTION: rxi_FreeConnection() does not yield */
856 for (cp = &host->z.FirstClient; (client = *cp);) {
857 if ((host->z.hostFlags & HOSTDELETED) || client->z.deleted) {
859 ObtainWriteLockNoBlock(&client->lock, code);
863 ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
864 "client %p was locked.\n",
865 host, afs_inet_ntoa_r(host->z.host, hoststr),
866 ntohs(host->z.port), client));
870 if (client->z.refCount) {
873 ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
874 "client %p refcount %d.\n",
875 host, afs_inet_ntoa_r(host->z.host, hoststr),
876 ntohs(host->z.port), client, client->z.refCount));
877 /* This is the same thing we do if the host is locked */
878 ReleaseWriteLock(&client->lock);
881 client->z.CPS.prlist_len = 0;
882 if ((client->z.ViceId != ANONYMOUSID) && client->z.CPS.prlist_val)
883 free(client->z.CPS.prlist_val);
884 client->z.CPS.prlist_val = NULL;
885 CurrentConnections--;
886 *cp = client->z.next;
887 ReleaseWriteLock(&client->lock);
890 cp = &client->z.next;
893 /* We've just cleaned out all the deleted clients; clear the flag */
894 host->z.hostFlags &= ~CLIENTDELETED;
896 if (host->z.hostFlags & HOSTDELETED) {
897 struct rx_connection *rxconn;
898 struct AddrPort hostAddrPort;
901 if (host->z.Console & 1)
903 if ((rxconn = host->z.callback_rxcon)) {
904 host->z.callback_rxcon = (struct rx_connection *)0;
905 rx_DestroyConnection(rxconn);
907 if (host->z.hcps.prlist_val)
908 free(host->z.hcps.prlist_val);
909 host->z.hcps.prlist_val = NULL;
910 host->z.hcps.prlist_len = 0;
911 free(host->z.tmay_caps.Capabilities_val);
912 host->z.tmay_caps.Capabilities_val = NULL;
913 host->z.tmay_caps.Capabilities_len = 0;
914 DeleteAllCallBacks_r(host, 1);
915 host->z.hostFlags &= ~RESETDONE; /* just to be safe */
917 /* if alternate addresses do not exist */
918 if (!(host->z.interface)) {
919 h_DeleteHostFromAddrHashTable_r(host->z.host, host->z.port, host);
921 h_DeleteHostFromUuidHashTable_r(host);
922 h_DeleteHostFromAddrHashTable_r(host->z.host, host->z.port, host);
923 /* delete the hash entry for each valid alternate addresses */
924 for (i = 0; i < host->z.interface->numberOfInterfaces; i++) {
925 hostAddrPort = host->z.interface->interface[i];
927 * if the interface addr/port is the primary, we already
928 * removed it. If the addr/port is not valid, its not
931 if (hostAddrPort.valid &&
932 (host->z.host != hostAddrPort.addr ||
933 host->z.port != hostAddrPort.port))
934 h_DeleteHostFromAddrHashTable_r(hostAddrPort.addr, hostAddrPort.port, host);
936 free(host->z.interface);
937 host->z.interface = NULL;
938 } /* if alternate address exists */
940 h_DeleteList_r(host); /* remove host from global host List */
947 /* h_Enumerate: Calls (*proc)(host, param) for at least each host in the
948 * system at the start of the enumeration (perhaps more). Hosts may be deleted
949 * (have delete flag set); ditto for clients. refCount is always incremented
950 * before (*proc) is called.
952 * The return value of the proc is a set of flags. The proc should set
953 * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
956 h_Enumerate(int (*proc) (struct host*, void *), void *param)
958 struct host *host, **list;
963 if (hostCount == 0) {
967 list = malloc(hostCount * sizeof(struct host *));
969 ViceLogThenPanic(0, ("Failed malloc in h_Enumerate (list)\n"));
971 for (totalCount = count = 0, host = hostList;
972 host && totalCount < hostCount;
973 host = host->z.next, totalCount++) {
975 if (!(host->z.hostFlags & HOSTDELETED)) {
981 if (totalCount != hostCount) {
982 ViceLog(0, ("h_Enumerate found %d of %d hosts\n", totalCount, hostCount));
983 } else if (host != NULL) {
984 ViceLog(0, ("h_Enumerate found more than %d hosts\n", hostCount));
985 ShutDownAndCore(PANIC);
988 for (i = 0; i < count; i++) {
990 flags = (*proc) (list[i], param);
992 h_Release_r(list[i]);
994 /* bail out of the enumeration early */
995 if (H_ENUMERATE_ISSET_BAIL(flags)) {
998 ViceLog(0, ("h_Enumerate got back invalid return value %d\n", flags));
999 ShutDownAndCore(PANIC);
1003 /* we bailed out of enumerating hosts early; we still have holds on
1004 * some of the hosts in 'list', so release them */
1007 for ( ; i < count; i++) {
1008 h_Release_r(list[i]);
1016 /* h_Enumerate_r (revised):
1017 * Calls (*proc)(host, param) for each host in hostList, starting
1018 * at enumstart. Called only under H_LOCK. Hosts may be deleted (have
1019 * delete flag set); ditto for clients. refCount is always incremented
1020 * before (*proc) is called.
1022 * @note Assumes that hostList is only prepended to, that a host is never
1023 * inserted into the middle. Otherwise this would not be guaranteed to
1026 * The return value of the proc is a set of flags. The proc should set
1027 * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
1030 h_Enumerate_r(int (*proc) (struct host *, void *),
1031 struct host *enumstart, void *param)
1033 struct host *host, *next;
1037 if (hostCount == 0) {
1044 /* find the first non-deleted host, so we know where to actually start
1046 for (count = 0; host && count < hostCount; count++) {
1047 if (!(host->z.hostFlags & HOSTDELETED)) {
1051 host = host->z.next;
1054 /* we didn't find a non-deleted host... */
1056 if (host && count >= hostCount) {
1057 /* ...because we found a loop */
1058 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", hostCount));
1059 ShutDownAndCore(PANIC);
1062 /* ...because the hostList is full of deleted hosts */
1066 h_Hold_r(enumstart);
1068 /* remember hostCount, lest it change over the potential H_LOCK drop in
1070 origHostCount = hostCount;
1072 for (count = 0, host = enumstart; host && count < origHostCount; host = next, count++) {
1073 next = host->z.next;
1075 /* find the next non-deleted host */
1076 while (next && (next->z.hostFlags & HOSTDELETED)) {
1077 next = next->z.next;
1078 /* inc count for the skipped-over host */
1079 if (++count > origHostCount) {
1080 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", origHostCount));
1081 ShutDownAndCore(PANIC);
1087 if (!(host->z.hostFlags & HOSTDELETED)) {
1089 flags = (*proc) (host, param);
1090 if (H_ENUMERATE_ISSET_BAIL(flags)) {
1091 h_Release_r(host); /* this might free up the host */
1097 ViceLog(0, ("h_Enumerate_r got back invalid return value %d\n", flags));
1098 ShutDownAndCore(PANIC);
1101 h_Release_r(host); /* this might free up the host */
1103 if (host != NULL && count >= origHostCount) {
1104 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", origHostCount));
1105 ShutDownAndCore(PANIC);
1107 } /*h_Enumerate_r */
1110 /* inserts a new HashChain structure corresponding to this UUID */
1112 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1115 struct h_UuidHashChain *chain;
1116 char uuid1[128], uuid2[128];
1119 /* hash into proper bucket */
1120 index = h_UuidHashIndex(uuid);
1122 /* don't add the same entry multiple times */
1123 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1124 if (!chain->hostPtr)
1127 if (chain->hostPtr->z.interface &&
1128 afs_uuid_equal(&chain->hostPtr->z.interface->uuid, uuid)) {
1129 if (GetLogLevel() >= 125) {
1130 afsUUID_to_string(&chain->hostPtr->z.interface->uuid, uuid1,
1132 afsUUID_to_string(uuid, uuid2, 127);
1133 ViceLog(125, ("h_AddHostToUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s) exists as %s:%d (uuid %s)\n",
1135 afs_inet_ntoa_r(chain->hostPtr->z.host, hoststr),
1136 ntohs(chain->hostPtr->z.port), uuid2));
1142 /* insert into beginning of list for this bucket */
1143 chain = malloc(sizeof(struct h_UuidHashChain));
1145 ViceLogThenPanic(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1147 chain->hostPtr = host;
1148 chain->next = hostUuidHashTable[index];
1149 hostUuidHashTable[index] = chain;
1150 if (GetLogLevel() < 125)
1152 afsUUID_to_string(uuid, uuid2, 127);
1154 ("h_AddHostToUuidHashTable_r: host %p (%s:%d) added as uuid %s\n",
1155 host, afs_inet_ntoa_r(chain->hostPtr->z.host, hoststr),
1156 ntohs(chain->hostPtr->z.port), uuid2));
1159 /* deletes a HashChain structure corresponding to this host */
1161 h_DeleteHostFromUuidHashTable_r(struct host *host)
1164 struct h_UuidHashChain **uhp, *uth;
1168 if (!host->z.interface)
1171 /* hash into proper bucket */
1172 index = h_UuidHashIndex(&host->z.interface->uuid);
1174 if (GetLogLevel() >= 125)
1175 afsUUID_to_string(&host->z.interface->uuid, uuid1, 127);
1176 for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
1177 opr_Assert(uth->hostPtr);
1178 if (uth->hostPtr == host) {
1180 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d)\n",
1181 host, uuid1, afs_inet_ntoa_r(host->z.host, hoststr),
1182 ntohs(host->z.port)));
1189 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d) not found\n",
1190 host, uuid1, afs_inet_ntoa_r(host->z.host, hoststr),
1191 ntohs(host->z.port)));
1196 * This is called with host locked and held.
1198 * All addresses are in network byte order.
1201 invalidateInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1205 struct Interface *interface;
1206 char hoststr[16], hoststr2[16];
1209 opr_Assert(host->z.interface);
1211 ViceLog(125, ("invalidateInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1212 host, afs_inet_ntoa_r(host->z.host, hoststr),
1213 ntohs(host->z.port), afs_inet_ntoa_r(addr, hoststr2),
1217 * Make sure this address is on the list of known addresses
1220 interface = host->z.interface;
1221 number = host->z.interface->numberOfInterfaces;
1222 for (i = 0; i < number; i++) {
1223 if (interface->interface[i].addr == addr &&
1224 interface->interface[i].port == port) {
1225 if (interface->interface[i].valid) {
1226 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1227 interface->interface[i].valid = 0;
1238 * This is called with host locked and held. This function differs
1239 * from removeInterfaceAddr_r in that it is called when the address
1240 * is being removed from the host regardless of whether or not there
1241 * is an interface list for the host. This function will delete the
1242 * host if there are no addresses left on it.
1244 * All addresses are in network byte order.
1247 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1250 char hoststr[16], hoststr2[16];
1251 struct rx_connection *rxconn;
1253 if (!host->z.interface || host->z.interface->numberOfInterfaces == 1) {
1254 if (host->z.host == addr && host->z.port == port) {
1256 ("Removing only address for host %" AFS_PTR_FMT " (%s:%d), deleting host.\n",
1257 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
1258 host->z.hostFlags |= HOSTDELETED;
1260 * Do not remove the primary addr/port from the hash table.
1261 * It will be ignored due to the HOSTDELETED flag and will
1262 * be removed when h_TossStuff_r() cleans up the HOSTDELETED
1263 * host. Removing it here will only result in a search for
1264 * the host/addr/port in the hash chain which will fail.
1268 ("Removing address that does not belong to host %" AFS_PTR_FMT " (%s:%d).\n",
1269 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
1272 if (host->z.host == addr && host->z.port == port) {
1273 removeInterfaceAddr_r(host, addr, port);
1275 for (i=0; i < host->z.interface->numberOfInterfaces; i++) {
1276 if (host->z.interface->interface[i].valid) {
1278 ("Removed address for host %" AFS_PTR_FMT " (%s:%d), new primary interface %s:%d.\n",
1279 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port),
1280 afs_inet_ntoa_r(host->z.interface->interface[i].addr, hoststr2),
1281 ntohs(host->z.interface->interface[i].port)));
1282 host->z.host = host->z.interface->interface[i].addr;
1283 host->z.port = host->z.interface->interface[i].port;
1284 h_AddHostToAddrHashTable_r(host->z.host, host->z.port, host);
1289 if (i == host->z.interface->numberOfInterfaces) {
1291 ("Removed only address for host %" AFS_PTR_FMT " (%s:%d), no valid alternate interfaces, deleting host.\n",
1292 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
1293 host->z.hostFlags |= HOSTDELETED;
1294 /* addr/port was removed from the hash table */
1298 rxconn = host->z.callback_rxcon;
1299 host->z.callback_rxcon = NULL;
1302 rx_DestroyConnection(rxconn);
1306 h_SetupCallbackConn_r(host);
1309 /* not the primary addr/port, just invalidate it */
1310 invalidateInterfaceAddr_r(host, addr, port);
1318 createHostAddrHashChain_r(int index, afs_uint32 addr, afs_uint16 port, struct host *host)
1320 struct h_AddrHashChain *chain;
1323 /* insert into beginning of list for this bucket */
1324 chain = malloc(sizeof(struct h_AddrHashChain));
1326 ViceLogThenPanic(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1328 chain->hostPtr = host;
1329 chain->next = hostAddrHashTable[index];
1332 hostAddrHashTable[index] = chain;
1333 ViceLog(125, ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " added as %s:%d\n",
1334 host, afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1338 * Resolve host address conflicts when hashing by address.
1340 * @param[in] addr an ip address of the interface
1341 * @param[in] port the port of the interface
1342 * @param[in] newHost the host being added with this address
1343 * @param[in] oldHost the host previously added with this address
1346 reconcileHosts_r(afs_uint32 addr, afs_uint16 port, struct host *newHost,
1347 struct host *oldHost)
1349 struct rx_connection *cb = NULL;
1351 struct interfaceAddr interf;
1353 afsUUID *newHostUuid = &nulluuid;
1354 afsUUID *oldHostUuid = &nulluuid;
1358 ("reconcileHosts_r: addr %s:%d newHost %" AFS_PTR_FMT " oldHost %"
1359 AFS_PTR_FMT "\n", afs_inet_ntoa_r(addr, hoststr), ntohs(port),
1362 opr_Assert(oldHost != newHost);
1363 caps.Capabilities_val = NULL;
1366 sc = rxnull_NewClientSecurityObject();
1369 cb = rx_NewConnection(addr, port, 1, sc, 0);
1370 rx_SetConnDeadTime(cb, 50);
1371 rx_SetConnHardDeadTime(cb, AFS_HARDDEADTIME);
1376 code = RXAFSCB_TellMeAboutYourself(cb, &interf, &caps);
1377 if (code == RXGEN_OPCODE) {
1378 code = RXAFSCB_WhoAreYou(cb, &interf);
1382 if (code == RXGEN_OPCODE ||
1383 (code == 0 && afs_uuid_equal(&interf.uuid, &nulluuid))) {
1385 ("reconcileHosts_r: WhoAreYou not supported for connection (%s:%d), error %d\n",
1386 afs_inet_ntoa_r(addr, hoststr), ntohs(port), code));
1391 ("reconcileHosts_r: WhoAreYou failed for connection (%s:%d), error %d\n",
1392 afs_inet_ntoa_r(addr, hoststr), ntohs(port), code));
1396 /* Since lock was dropped, the hosts may have been deleted during the rpcs. */
1397 if ((newHost->z.hostFlags & HOSTDELETED)
1398 && (oldHost->z.hostFlags & HOSTDELETED)) {
1400 ("reconcileHosts_r: new and old hosts were deleted during probe.\n"));
1404 /* A check can be done if at least one of the hosts has a uuid. It
1405 * is an error if the hosts have the same (not null) uuid. */
1406 if ((!(newHost->z.hostFlags & HOSTDELETED)) && newHost->z.interface) {
1407 newHostUuid = &(newHost->z.interface->uuid);
1409 if ((!(oldHost->z.hostFlags & HOSTDELETED)) && oldHost->z.interface) {
1410 oldHostUuid = &(oldHost->z.interface->uuid);
1412 if (afs_uuid_equal(newHostUuid, &nulluuid) &&
1413 afs_uuid_equal(oldHostUuid, &nulluuid)) {
1415 ("reconcileHosts_r: Cannot reconcile hosts for connection (%s:%d), no uuids\n",
1416 afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1419 if (afs_uuid_equal(newHostUuid, oldHostUuid)) {
1421 ("reconcileHosts_r: Cannot reconcile hosts for connection (%s:%d), same uuids\n",
1422 afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1426 /* Determine which host should be hashed */
1427 if ((!(newHost->z.hostFlags & HOSTDELETED))
1428 && afs_uuid_equal(newHostUuid, &(interf.uuid))) {
1429 /* Install the new host into the hash before removing the stale
1430 * addresses. Walk the hash chain again since the hash table may have
1431 * been changed when the host lock was dropped to get the uuid. */
1432 struct h_AddrHashChain *chain;
1433 int index = h_HashIndex(addr);
1434 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1435 if (chain->addr == addr && chain->port == port) {
1436 chain->hostPtr = newHost;
1437 removeAddress_r(oldHost, addr, port);
1441 createHostAddrHashChain_r(index, addr, port, newHost);
1442 removeAddress_r(oldHost, addr, port);
1445 if ((!(oldHost->z.hostFlags & HOSTDELETED))
1446 && afs_uuid_equal(oldHostUuid, &(interf.uuid))) {
1447 removeAddress_r(newHost, addr, port);
1452 if (!(newHost->z.hostFlags & HOSTDELETED)) {
1453 removeAddress_r(newHost, addr, port);
1455 if (!(oldHost->z.hostFlags & HOSTDELETED)) {
1456 removeAddress_r(oldHost, addr, port);
1460 h_Release_r(newHost);
1461 h_Release_r(oldHost);
1462 rx_DestroyConnection(cb);
1466 /* inserts a new HashChain structure corresponding to this address */
1468 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1471 struct h_AddrHashChain *chain;
1474 /* hash into proper bucket */
1475 index = h_HashIndex(addr);
1477 /* don't add the same address:port pair entry multiple times */
1478 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1479 if (chain->addr == addr && chain->port == port) {
1480 if (chain->hostPtr == host) {
1482 ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) already hashed\n",
1483 host, afs_inet_ntoa_r(chain->addr, hoststr),
1484 ntohs(chain->port)));
1487 if (!(chain->hostPtr->z.hostFlags & HOSTDELETED)) {
1488 /* attempt to resolve host address collision */
1489 reconcileHosts_r(addr, port, host, chain->hostPtr);
1494 createHostAddrHashChain_r(index, addr, port, host);
1498 * This is called with host locked and held.
1499 * It is called to either validate or add an additional interface
1500 * address/port on the specified host.
1502 * All addresses are in network byte order.
1505 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1509 struct Interface *interface;
1510 char hoststr[16], hoststr2[16];
1513 opr_Assert(host->z.interface);
1516 * Make sure this address is on the list of known addresses
1519 number = host->z.interface->numberOfInterfaces;
1520 for (i = 0; i < number; i++) {
1521 if (host->z.interface->interface[i].addr == addr &&
1522 host->z.interface->interface[i].port == port) {
1524 ("addInterfaceAddr : found host %" AFS_PTR_FMT " (%s:%d) adding %s:%d%s\n",
1525 host, afs_inet_ntoa_r(host->z.host, hoststr),
1526 ntohs(host->z.port), afs_inet_ntoa_r(addr, hoststr2),
1527 ntohs(port), host->z.interface->interface[i].valid ? "" :
1530 if (host->z.interface->interface[i].valid == 0) {
1531 host->z.interface->interface[i].valid = 1;
1532 h_AddHostToAddrHashTable_r(addr, port, host);
1538 ViceLog(125, ("addInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) adding %s:%d\n",
1539 host, afs_inet_ntoa_r(host->z.host, hoststr),
1540 ntohs(host->z.port), afs_inet_ntoa_r(addr, hoststr2),
1543 interface = malloc(sizeof(struct Interface)
1544 + (sizeof(struct AddrPort) * number));
1546 ViceLogThenPanic(0, ("Failed malloc in addInterfaceAddr_r\n"));
1548 interface->numberOfInterfaces = number + 1;
1549 interface->uuid = host->z.interface->uuid;
1550 for (i = 0; i < number; i++)
1551 interface->interface[i] = host->z.interface->interface[i];
1553 /* Add the new valid interface */
1554 interface->interface[number].addr = addr;
1555 interface->interface[number].port = port;
1556 interface->interface[number].valid = 1;
1557 h_AddHostToAddrHashTable_r(addr, port, host);
1558 free(host->z.interface);
1559 host->z.interface = interface;
1566 * This is called with host locked and held.
1568 * All addresses are in network byte order.
1571 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1575 struct Interface *interface;
1576 char hoststr[16], hoststr2[16];
1579 opr_Assert(host->z.interface);
1581 ViceLog(125, ("removeInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1582 host, afs_inet_ntoa_r(host->z.host, hoststr),
1583 ntohs(host->z.port), afs_inet_ntoa_r(addr, hoststr2),
1587 * Make sure this address is on the list of known addresses
1590 interface = host->z.interface;
1591 number = host->z.interface->numberOfInterfaces;
1592 for (i = 0; i < number; i++) {
1593 if (interface->interface[i].addr == addr &&
1594 interface->interface[i].port == port) {
1595 if (interface->interface[i].valid)
1596 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1598 for (; i < number; i++) {
1599 interface->interface[i] = interface->interface[i+1];
1601 interface->numberOfInterfaces = number;
1610 * The following few functions deal with caching TellMeAboutYourself calls
1611 * that we issued to clients. Why do we do this? Well:
1613 * Q: First, why do we need to issue a TMAY against a client on an incoming new
1616 * A: We must verify that the incoming Rx connection is the same host that
1617 * we have a 'host' structure for. On new calls for existing connections, we
1618 * can remember which host corresponds to that connection, but for new
1619 * connections, we have no way to find what host it is for, except by looking
1620 * up the host by IP address. Since hosts can change IP addresses, we need to
1621 * contact the IP address to see if it's the host we think it is.
1623 * Q: Okay, then why cache the results?
1625 * A: The TMAY calls to a single host are serialized, because they are issued
1626 * with the host's host->lock held. If we get 4 Rx calls each on new
1627 * connections to the same host at the same time, the 1st call will lock the
1628 * host, and issue a TMAY. Once that's done, the second call will issue a
1629 * TMAY, then the 3rd and then the 4th. However, the 3rd and 4th calls have
1630 * been waiting to issue a TMAY since before the 2nd call even _started_ to
1631 * issue a TMAY. So, we can just effectively give the results of the 2nd
1632 * call's TMAY to the 3rd and 4th, too. Since it is nondeterministic which of
1633 * those calls gets to issue a TMAY "first", we can just assume they all got
1636 * Note that in the above example, we cannot reuse the results of the 1st TMAY
1637 * for the 2nd, 3rd, and 4th calls (we only reuse the results of the 2nd).
1638 * This is because by the time the 2nd call starts waiting for the host lock,
1639 * it doesn't know how long the 1st TMAY has been running, so the 2nd call
1640 * might indeed get a different TMAY result (though this probably would be
1643 * Anyway, so, if we don't cache the results, we are issuing TMAYs that are
1644 * pure overhead. In an environment with clients that create a lot of
1645 * connections to a fileserver (keep in mind each new PAG on OpenAFS clients
1646 * creates a new connection), this can mean a significant amount of overhead.
1647 * So, we use this "TMAY cache" to avoid this overhead for the common case.
1651 * Should we skip calling TellMeAboutYourself on this host, and instead rely
1652 * on cached TMAY results?
1654 * @param[in] host The host we are dealing with
1655 * @param[in] prewait_tmays What the value of host->z.n_tmays was _before_ we
1657 * @param[in] prewait_host What the primary IP address for 'host' was before
1659 * @param[in] prewait_port What the primary port was for 'host' before we
1662 * @return Whether we should skip calling TMAY, and instead rely on cached
1666 ShouldSkipTMAY(struct host *host, int prewait_tmays, afs_uint32 prewait_host,
1667 afs_uint16 prewait_port)
1670 if (host->z.n_tmays > prewait_tmays + 1) {
1671 /* while we were waiting for the host lock, the in-progress TMAY
1672 * call finished, someone else started a new TMAY call, and that
1673 * finished. so, calling TMAY again won't give us any information
1674 * or additional guarantees. */
1677 if (host->z.host != prewait_host || host->z.port != prewait_port) {
1678 /* ...but don't skip it if the host has changed */
1685 * If appropriate, simulate a TellMeAboutYourself call on 'host' by extracting
1686 * cached interfaceAddr and Capabilities information from 'host' itself.
1688 * @param[in] host The host we're dealing with
1689 * @param[inout] askiptmay On entering this function, this should contain the
1690 * result of ShouldSkipTMAY. On return, it is 1 if we
1691 * actually did use cached TMAY results, or 0 if we
1693 * @param[out] interf The interfaceAddr result of the simulated TMAY call
1694 * @param[out] caps The Capabilities result of the simulated TMAY call
1697 * @retval 0 We used the cached TMAY results; do NOT make a real TMAY request
1698 * @retval otherwise We did not use cached TMAY results; issue a real TMAY request
1701 SimulateTMAY(struct host *host, int *askiptmay, struct interfaceAddr *interf,
1707 /* we're not supposed to skip the actual TMAY call */
1711 *interf = host->z.tmay_interf;
1713 free(caps->Capabilities_val);
1714 caps->Capabilities_val = NULL;
1715 caps->Capabilities_len = 0;
1717 if (!host->z.tmay_caps.Capabilities_val) {
1721 capsize = sizeof(caps->Capabilities_val[0]) * host->z.tmay_caps.Capabilities_len;
1723 caps->Capabilities_val = malloc(capsize);
1724 if (!caps->Capabilities_val) {
1725 /* we should/did _not_ skip the real TMAY call, since we couldn't
1726 * alloc memory to use the cached results */
1730 caps->Capabilities_len = host->z.tmay_caps.Capabilities_len;
1731 memcpy(caps->Capabilities_val, host->z.tmay_caps.Capabilities_val, capsize);
1737 * If appropriate, store the given results from a real TellmeAboutYourself
1738 * call, and cache them in the given host structure.
1740 * @param[in] host The host we're dealing with
1741 * @param[in] skiptmay 1 if we skipped making a real TMAY call, 0 otherwise
1742 * @param[in] didtmay 1 if we issued a successful real TMAY call, 0 otherwise
1743 * @param[in] interf The interfaceAddr result from the real TMAY call
1744 * @param[in] caps The Capabilities result from the real TMAY call
1747 CacheTMAY(struct host *host, int skiptmay, int didtmay,
1748 struct interfaceAddr *interf, Capabilities *caps)
1753 /* we simulated the TMAY call, so the state of the world hasn't
1754 * changed; don't touch anything */
1758 /* we did not perform a successful TMAY, so we don't have valid
1759 * results to cache. blow away the existing cache so we don't use
1763 if (host->z.n_tmays == INT_MAX) {
1764 /* make sure int rollover doesn't screw up our ordering */
1767 if (host->lock.num_waiting == 0) {
1768 /* nobody is waiting for this host, so no reason to cache anything */
1772 /* okay, if we got here, everything looks good; let's cache the given
1773 * 'interf' and 'caps' */
1775 host->z.tmay_interf = *interf;
1777 if (!caps->Capabilities_val) {
1778 free(host->z.tmay_caps.Capabilities_val);
1779 host->z.tmay_caps.Capabilities_val = NULL;
1780 host->z.tmay_caps.Capabilities_len = 0;
1783 if (caps->Capabilities_len != host->z.tmay_caps.Capabilities_len) {
1784 free(host->z.tmay_caps.Capabilities_val);
1785 host->z.tmay_caps.Capabilities_val = NULL;
1786 host->z.tmay_caps.Capabilities_len = 0;
1789 capsize = sizeof(caps->Capabilities_val[0]) * caps->Capabilities_len;
1791 if (!host->z.tmay_caps.Capabilities_val) {
1792 host->z.tmay_caps.Capabilities_val = malloc(capsize);
1793 if (!host->z.tmay_caps.Capabilities_val) {
1798 host->z.tmay_caps.Capabilities_len = caps->Capabilities_len;
1799 memcpy(host->z.tmay_caps.Capabilities_val, caps->Capabilities_val, capsize);
1805 /* blow away the cached TMAY data; pretend we never saw anything */
1806 free(host->z.tmay_caps.Capabilities_val);
1807 host->z.tmay_caps.Capabilities_val = NULL;
1808 host->z.tmay_caps.Capabilities_len = 0;
1809 memset(&host->z.tmay_interf, 0, sizeof(host->z.tmay_interf));
1811 host->z.n_tmays = 0;
1815 h_threadquota(int waiting)
1817 if (waiting > h_quota_limit) {
1823 /* If found, host is returned with refCount incremented */
1825 h_GetHost_r(struct rx_connection *tcon)
1828 struct host *oldHost;
1830 struct interfaceAddr interf;
1831 int interfValid = 0;
1832 struct Identity *identP = NULL;
1835 char hoststr[16], hoststr2[16];
1837 struct rx_connection *cb_conn = NULL;
1838 struct rx_connection *cb_in = NULL;
1840 caps.Capabilities_val = NULL;
1842 haddr = rxr_HostOf(tcon);
1843 hport = rxr_PortOf(tcon);
1846 rx_DestroyConnection(cb_in);
1849 if (caps.Capabilities_val)
1850 free(caps.Capabilities_val);
1851 caps.Capabilities_val = NULL;
1852 caps.Capabilities_len = 0;
1855 if (h_Lookup_r(haddr, hport, &host))
1857 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1858 if (host && !identP && !(host->z.Console & 1)) {
1859 /* This is a new connection, and we already have a host
1860 * structure for this address. Verify that the identity
1861 * of the caller matches the identity in the host structure.
1864 int didtmay = 0; /* did we make a successful TMAY call against host->z.host? */
1865 unsigned int prewait_tmays;
1866 afs_uint32 prewait_host;
1867 afs_uint16 prewait_port;
1870 if ((host->z.hostFlags & HWHO_INPROGRESS) &&
1871 h_threadquota(host->lock.num_waiting)) {
1877 prewait_tmays = host->z.n_tmays;
1878 prewait_host = host->z.host;
1879 prewait_port = host->z.port;
1882 if (!(host->z.hostFlags & ALTADDR) ||
1883 (host->z.hostFlags & HOSTDELETED)) {
1884 /* Another thread is doing initialization
1885 * or this host was deleted while we
1886 * waited for the lock. */
1889 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1890 host, afs_inet_ntoa_r(host->z.host, hoststr),
1891 ntohs(host->z.port)));
1895 host->z.hostFlags |= HWHO_INPROGRESS;
1896 host->z.hostFlags &= ~ALTADDR;
1898 /* We received a new connection from an IP address/port
1899 * that is associated with 'host' but the address/port of
1900 * the callback connection does not have to match it.
1901 * If there is a match, we can use the existing callback
1902 * connection to verify the UUID. If they do not match
1903 * we need to use a new callback connection to verify the
1904 * UUID of the incoming caller and perhaps use the old
1905 * callback connection to verify that the old address/port
1909 cb_conn = host->z.callback_rxcon;
1910 rx_GetConnection(cb_conn);
1912 skiptmay = ShouldSkipTMAY(host, prewait_tmays, prewait_host, prewait_port);
1915 if (haddr == host->z.host && hport == host->z.port) {
1916 /* The existing callback connection matches the
1917 * incoming connection so just use it.
1920 if (SimulateTMAY(host, &skiptmay, &interf, &caps) == 0) {
1921 /* noop; we don't need to call TellMeAboutYourself; we can
1922 * trust the results from the last TMAY call */
1927 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1928 if (code == RXGEN_OPCODE)
1929 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1935 /* We do not have a match. Create a new connection
1936 * for the new addr/port and use multi_Rx to probe
1937 * both of them simultaneously.
1940 sc = rxnull_NewClientSecurityObject();
1941 cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
1942 rx_SetConnDeadTime(cb_in, 50);
1943 rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
1946 RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
1947 if (code == RXGEN_OPCODE)
1948 code = RXAFSCB_WhoAreYou(cb_in, &interf);
1950 rx_PutConnection(cb_conn);
1954 CacheTMAY(host, skiptmay, didtmay, &interf, &caps);
1956 if ((code == RXGEN_OPCODE) ||
1957 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1958 identP = malloc(sizeof(struct Identity));
1960 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1963 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1964 if (cb_in == NULL) {
1965 /* The host on this connection was unable to respond to
1966 * the WhoAreYou. We will treat this as a new connection
1967 * from the existing host. The worst that can happen is
1968 * that we maintain some extra callback state information */
1969 if (host->z.interface) {
1971 ("Host %" AFS_PTR_FMT " (%s:%d) used to support WhoAreYou, deleting.\n",
1973 afs_inet_ntoa_r(host->z.host, hoststr),
1974 ntohs(host->z.port)));
1975 host->z.hostFlags |= HOSTDELETED;
1976 host->z.hostFlags &= ~HWHO_INPROGRESS;
1983 /* The incoming connection does not support WhoAreYou but
1984 * the original one might have. Use removeAddress_r() to
1985 * remove this addr/port from the host that was found.
1986 * If there are no more addresses left for the host it
1987 * will be deleted. Then we retry.
1989 removeAddress_r(host, haddr, hport);
1990 host->z.hostFlags &= ~HWHO_INPROGRESS;
1991 host->z.hostFlags |= ALTADDR;
1997 } else if (code == 0) {
1999 identP = malloc(sizeof(struct Identity));
2001 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
2004 identP->uuid = interf.uuid;
2005 rx_SetSpecific(tcon, rxcon_ident_key, identP);
2006 /* Check whether the UUID on this connection matches
2007 * the UUID in the host structure. If they don't match
2008 * then this is not the same host as before. */
2009 if (!host->z.interface
2010 || !afs_uuid_equal(&interf.uuid, &host->z.interface->uuid)) {
2013 ("Uuid doesn't match connection (%s:%d).\n",
2014 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport)));
2015 removeAddress_r(host, haddr, hport);
2018 ("Uuid doesn't match host %" AFS_PTR_FMT " (%s:%d).\n",
2019 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
2021 removeAddress_r(host, host->z.host, host->z.port);
2023 host->z.hostFlags &= ~HWHO_INPROGRESS;
2024 host->z.hostFlags |= ALTADDR;
2030 /* the UUID matched the client at the incoming addr/port
2031 * but this is not the address of the active callback
2032 * connection. Try that connection and see if the client
2033 * is still there and if the reported UUID is the same.
2036 afsUUID uuid = host->z.interface->uuid;
2037 cb_conn = host->z.callback_rxcon;
2038 rx_GetConnection(cb_conn);
2039 rx_SetConnDeadTime(cb_conn, 2);
2040 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2042 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2044 rx_SetConnDeadTime(cb_conn, 50);
2045 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2046 rx_PutConnection(cb_conn);
2049 /* The primary address is either not responding or
2050 * is not the client we are looking for. Need to
2051 * remove the primary address and add swap in the new
2052 * callback connection, and destroy the old one.
2054 struct rx_connection *rxconn;
2055 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
2057 afs_inet_ntoa_r(host->z.host, hoststr),
2058 ntohs(host->z.port),code2));
2061 * make sure we add and then remove. otherwise, we
2062 * might end up with no valid interfaces after the
2063 * remove and the host will have been marked deleted.
2065 addInterfaceAddr_r(host, haddr, hport);
2066 removeInterfaceAddr_r(host, host->z.host, host->z.port);
2067 host->z.host = haddr;
2068 host->z.port = hport;
2069 rxconn = host->z.callback_rxcon;
2070 host->z.callback_rxcon = cb_in;
2075 * If rx_DestroyConnection calls h_FreeConnection we
2076 * will deadlock on the host_glock_mutex. Work around
2077 * the problem by unhooking the client from the
2078 * connection before destroying the connection.
2080 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
2081 rx_DestroyConnection(rxconn);
2087 /* A callback to the incoming connection address is failing.
2088 * Assume that the addr/port is no longer associated with the host
2089 * returned by h_Lookup_r.
2092 ("CB: WhoAreYou failed for connection (%s:%d) , error %d\n",
2093 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport), code));
2094 removeAddress_r(host, haddr, hport);
2095 host->z.hostFlags &= ~HWHO_INPROGRESS;
2096 host->z.hostFlags |= ALTADDR;
2100 rx_DestroyConnection(cb_in);
2105 ("CB: WhoAreYou failed for host %" AFS_PTR_FMT " (%s:%d), error %d\n",
2106 host, afs_inet_ntoa_r(host->z.host, hoststr),
2107 ntohs(host->z.port), code));
2108 host->z.hostFlags |= VENUSDOWN;
2111 if (caps.Capabilities_val
2112 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
2113 host->z.hostFlags |= HERRORTRANS;
2115 host->z.hostFlags &= ~(HERRORTRANS);
2116 host->z.hostFlags |= ALTADDR;
2117 host->z.hostFlags &= ~HWHO_INPROGRESS;
2120 if (!(host->z.hostFlags & ALTADDR)) {
2121 /* another thread is doing the initialisation */
2123 ("Host %" AFS_PTR_FMT " (%s:%d) waiting for host-init to complete\n",
2124 host, afs_inet_ntoa_r(host->z.host, hoststr),
2125 ntohs(host->z.port)));
2129 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
2130 host, afs_inet_ntoa_r(host->z.host, hoststr),
2131 ntohs(host->z.port)));
2135 /* We need to check whether the identity in the host structure
2136 * matches the identity on the connection. If they don't match
2137 * then treat this a new host. */
2138 if (!(host->z.Console & 1)
2139 && ((!identP->valid && host->z.interface)
2140 || (identP->valid && !host->z.interface)
2142 && !afs_uuid_equal(&identP->uuid,
2143 &host->z.interface->uuid)))) {
2144 char uuid1[128], uuid2[128];
2146 afsUUID_to_string(&identP->uuid, uuid1, 127);
2147 if (host->z.interface)
2148 afsUUID_to_string(&host->z.interface->uuid, uuid2, 127);
2150 ("CB: new identity for host %p (%s:%d), "
2151 "deleting(%x %p %s %s)\n",
2152 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port),
2153 identP->valid, host->z.interface,
2154 identP->valid ? uuid1 : "no_uuid",
2155 host->z.interface ? uuid2 : "no_uuid"));
2157 /* The host in the cache is not the host for this connection */
2159 host->z.hostFlags |= HOSTDELETED;
2165 host = h_Alloc_r(tcon); /* returned held and locked */
2168 h_gethostcps_r(host, time(NULL));
2169 if (!(host->z.Console & 1)) {
2171 cb_conn = host->z.callback_rxcon;
2172 rx_GetConnection(cb_conn);
2173 host->z.hostFlags |= HWHO_INPROGRESS;
2176 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
2177 if (code == RXGEN_OPCODE)
2178 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
2179 rx_PutConnection(cb_conn);
2182 if ((code == RXGEN_OPCODE) ||
2183 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
2185 identP = malloc(sizeof(struct Identity));
2190 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
2194 rx_SetSpecific(tcon, rxcon_ident_key, identP);
2196 ("Host %" AFS_PTR_FMT " (%s:%d) does not support WhoAreYou.\n",
2197 host, afs_inet_ntoa_r(host->z.host, hoststr),
2198 ntohs(host->z.port)));
2200 } else if (code == 0) {
2202 identP = malloc(sizeof(struct Identity));
2207 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
2211 identP->uuid = interf.uuid;
2213 rx_SetSpecific(tcon, rxcon_ident_key, identP);
2215 ("WhoAreYou success on host %" AFS_PTR_FMT " (%s:%d)\n",
2216 host, afs_inet_ntoa_r(host->z.host, hoststr),
2217 ntohs(host->z.port)));
2219 if (code == 0 && !identP->valid) {
2220 cb_conn = host->z.callback_rxcon;
2221 rx_GetConnection(cb_conn);
2223 code = RXAFSCB_InitCallBackState(cb_conn);
2224 rx_PutConnection(cb_conn);
2227 } else if (code == 0) {
2228 oldHost = h_LookupUuid_r(&identP->uuid);
2233 if (oldHost->z.hostFlags & HOSTDELETED) {
2234 h_Unlock_r(oldHost);
2235 h_Release_r(oldHost);
2243 /* This is a new address for an existing host. Update
2244 * the list of interfaces for the existing host and
2245 * delete the host structure we just allocated. */
2247 /* mark the duplicate host as deleted before we do
2248 * anything. The probing code below may try to change
2249 * "oldHost" to the same IP address as "host" currently
2250 * has, and we do not want a pseudo-"collision" to be
2252 host->z.hostFlags |= HOSTDELETED;
2254 oldHost->z.hostFlags |= HWHO_INPROGRESS;
2256 if (oldHost->z.interface) {
2258 afsUUID uuid = oldHost->z.interface->uuid;
2259 cb_conn = oldHost->z.callback_rxcon;
2260 rx_GetConnection(cb_conn);
2261 rx_SetConnDeadTime(cb_conn, 2);
2262 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2264 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2266 rx_SetConnDeadTime(cb_conn, 50);
2267 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2268 rx_PutConnection(cb_conn);
2271 /* The primary address is either not responding or
2272 * is not the client we are looking for.
2273 * MultiProbeAlternateAddress_r() will remove the
2274 * alternate interfaces that do not have the same
2276 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
2278 afs_inet_ntoa_r(oldHost->z.host, hoststr),
2279 ntohs(oldHost->z.port),code2));
2281 if (MultiProbeAlternateAddress_r(oldHost)) {
2282 /* If MultiProbeAlternateAddress_r succeeded,
2283 * it updated oldHost->host and oldHost->port
2284 * to an address that responded successfully to
2285 * a ProbeUuid, so it is as if the ProbeUuid
2286 * call above returned success. So, only set
2287 * 'probefail' if MultiProbeAlternateAddress_r
2296 if (oldHost->z.host != haddr || oldHost->z.port != hport) {
2297 struct rx_connection *rxconn;
2300 ("CB: Host %" AFS_PTR_FMT " (%s:%d) has new addr %s:%d\n",
2302 afs_inet_ntoa_r(oldHost->z.host, hoststr2),
2303 ntohs(oldHost->z.port),
2304 afs_inet_ntoa_r(haddr, hoststr),
2307 * add then remove. otherwise the host may get marked
2308 * deleted if we removed the only valid address.
2310 addInterfaceAddr_r(oldHost, haddr, hport);
2311 if (probefail || oldHost->z.host == haddr) {
2313 * The probe failed which means that the old
2314 * address is either unreachable or is not the
2315 * same host we were just contacted by. We will
2316 * also remove addresses if only the port has
2317 * changed because that indicates the client
2320 removeInterfaceAddr_r(oldHost, oldHost->z.host, oldHost->z.port);
2323 struct Interface *interface = oldHost->z.interface;
2324 int number = oldHost->z.interface->numberOfInterfaces;
2325 for (i = 0; i < number; i++) {
2326 if (interface->interface[i].addr == haddr &&
2327 interface->interface[i].port != hport) {
2329 * We have just been contacted by a client
2330 * that has been seen from behind a NAT
2331 * and at least one other address.
2333 removeInterfaceAddr_r(oldHost, haddr,
2334 interface->interface[i].port);
2339 oldHost->z.host = haddr;
2340 oldHost->z.port = hport;
2341 rxconn = oldHost->z.callback_rxcon;
2342 oldHost->z.callback_rxcon = host->z.callback_rxcon;
2343 host->z.callback_rxcon = rxconn;
2345 /* don't destroy rxconn here; let h_TossStuff_r
2346 * take care of that via h_Release_r below */
2348 host->z.hostFlags &= ~HWHO_INPROGRESS;
2350 /* release host because it was allocated by h_Alloc_r */
2353 /* the new host is held and locked */
2355 /* This really is a new host */
2356 opr_Assert(interfValid == 1);
2357 initInterfaceAddr_r(host, &interf);
2359 cb_conn = host->z.callback_rxcon;
2360 rx_GetConnection(cb_conn);
2363 RXAFSCB_InitCallBackState3(cb_conn,
2365 rx_PutConnection(cb_conn);
2370 ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
2371 host, afs_inet_ntoa_r(host->z.host, hoststr),
2372 ntohs(host->z.port)));
2378 ("CB: RCallBackConnectBack failed for %" AFS_PTR_FMT " (%s:%d)\n",
2379 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
2380 host->z.hostFlags |= VENUSDOWN;
2383 ("CB: RCallBackConnectBack succeeded for %" AFS_PTR_FMT " (%s:%d)\n",
2384 host, afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port)));
2385 host->z.hostFlags |= RESETDONE;
2388 if (caps.Capabilities_val
2389 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
2390 host->z.hostFlags |= HERRORTRANS;
2392 host->z.hostFlags &= ~(HERRORTRANS);
2393 host->z.hostFlags |= ALTADDR; /* host structure initialization complete */
2394 host->z.hostFlags &= ~HWHO_INPROGRESS;
2399 if (caps.Capabilities_val)
2400 free(caps.Capabilities_val);
2401 caps.Capabilities_val = NULL;
2402 caps.Capabilities_len = 0;
2404 rx_DestroyConnection(cb_in);
2414 h_InitHostPackage(int hquota)
2416 opr_Assert(hquota > 0);
2417 h_quota_limit = hquota;
2419 memset(&nulluuid, 0, sizeof(afsUUID));
2420 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
2421 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
2422 opr_mutex_init(&host_glock_mutex);
2426 MapName_r(char *uname, afs_int32 * aval)
2432 lnames.namelist_len = 1;
2433 lnames.namelist_val = (prname *) uname;
2434 lids.idlist_len = 0;
2435 lids.idlist_val = NULL;
2438 code = hpr_NameToId(&lnames, &lids);
2441 if (lids.idlist_val) {
2442 *aval = lids.idlist_val[0];
2443 if (*aval == AnonymousID) {
2445 ("MapName: NameToId on %s returns anonymousID\n",
2446 lnames.namelist_val[0]));
2448 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
2451 ("MapName: NameToId on '%s' is unknown\n",
2452 lnames.namelist_val[0]));
2463 PerHost_EnumerateClient(struct host *host, void *arock)
2465 struct enumclient_args *args = arock;
2466 struct client *client;
2469 for (client = host->z.FirstClient; client; client = client->z.next) {
2470 if (!client->z.deleted && client->z.ViceId == args->vid) {
2472 client->z.refCount++;
2475 code = (*args->proc)(client, args->rock);
2478 h_ReleaseClient_r(client);
2481 return H_ENUMERATE_BAIL(0);
2490 h_EnumerateClients(VolumeId vid,
2491 int (*proc)(struct client *client, void *rock),
2494 struct enumclient_args args;
2500 h_Enumerate_r(PerHost_EnumerateClient, hostList, &args);
2505 format_vname(char *vname, int usize, const char *tname, const char *tinst,
2506 const char *tcell, afs_int32 islocal)
2510 len = strlcpy(vname, tname, usize);
2514 len = strlcat(vname, ".", usize);
2517 len = strlcat(vname, tinst, usize);
2521 if (tcell[0] && !islocal) {
2522 len = strlcat(vname, "@", usize);
2525 len = strlcat(vname, tcell, usize);
2533 getPeerDetails(struct rx_connection *conn,
2534 afs_int32 *viceid, afs_int32 *expTime, int authClass)
2537 #if (64-MAXKTCNAMELEN)
2538 ticket name length != 64
2542 char tcell[MAXKTCREALMLEN];
2543 char uname[PR_MAXNAMELEN];
2545 *viceid = AnonymousID;
2546 *expTime = 0x7fffffff;
2549 ("FindClient: authenticating connection: authClass=%d\n",
2551 if (authClass == RX_SECIDX_VAB) {
2552 /* A bcrypt tickets, no longer supported */
2553 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2557 if (authClass == RX_SECIDX_KAD) {
2558 /* an rxkad ticket */
2562 /* kerberos ticket */
2563 code = rxkad_GetServerInfo(conn, /*level */ 0, (afs_uint32 *)expTime,
2564 tname, tinst, tcell, &kvno);
2566 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2571 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2572 tname, tinst, tcell, *expTime, kvno));
2573 code = afsconf_IsLocalRealmMatch(confDir, &islocal, tname, tinst, tcell);
2576 ViceLog(0, ("FindClient: local realm check failed; code=%d", code));
2580 code = format_vname(uname, sizeof(uname), tname, tinst, tcell, islocal);
2582 ViceLog(0, ("FindClient: uname truncated."));
2586 /* translate the name to a vice id */
2587 code = MapName_r(uname, viceid);
2589 ViceLog(1, ("failed to map name=%s -> code=%d\n", uname,
2591 return code; /* Actually flag this is a failure */
2601 * Called by the server main loop. Returns a h_Held client, which must be
2602 * released later the main loop. Allocates a client if the matching one
2603 * isn't around. The client is returned with its reference count incremented
2604 * by one. The caller must call h_ReleaseClient_r when finished with
2607 * The refCount on client->z.host is returned incremented. h_ReleaseClient_r
2608 * does not decrement the refCount on client->z.host.
2610 * *a_viceid is set to the user's ViceId, even if we don't return a client
2614 h_FindClient_r(struct rx_connection *tcon, afs_int32 *a_viceid)
2616 struct client *client;
2617 struct host *host = NULL;
2618 struct client *oldClient;
2619 afs_int32 viceid = 0;
2627 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2628 if (client && client->z.sid == rx_GetConnectionId(tcon)
2629 && client->z.VenusEpoch == rx_GetConnectionEpoch(tcon)
2630 && !(client->z.host->z.hostFlags & HOSTDELETED)
2631 && !client->z.deleted) {
2634 *a_viceid = client->z.ViceId;
2636 client->z.refCount++;
2637 h_Hold_r(client->z.host);
2638 if (client->z.prfail != 2) {
2639 /* Could add shared lock on client here */
2640 /* note that we don't have to lock entry in this path to
2641 * ensure CPS is initialized, since we don't call rx_SetSpecific
2642 * until initialization is done, and we only get here if
2643 * rx_GetSpecific located the client structure.
2648 ObtainWriteLock(&client->lock); /* released at end */
2654 authClass = rx_SecurityClassOf(tcon);
2656 code = getPeerDetails(tcon, &viceid, &expTime, authClass);
2664 if (!client) { /* loop */
2665 host = h_GetHost_r(tcon); /* Returns with incremented refCount */
2671 /* First try to find the client structure */
2672 for (client = host->z.FirstClient; client; client = client->z.next) {
2673 if (!client->z.deleted && (client->z.sid == rx_GetConnectionId(tcon))
2674 && (client->z.VenusEpoch == rx_GetConnectionEpoch(tcon))) {
2675 client->z.refCount++;
2677 ObtainWriteLock(&client->lock);
2683 /* Still no client structure - get one */
2686 if (host->z.hostFlags & HOSTDELETED) {
2691 /* Retry to find the client structure */
2692 for (client = host->z.FirstClient; client; client = client->z.next) {
2693 if (!client->z.deleted && (client->z.sid == rx_GetConnectionId(tcon))
2694 && (client->z.VenusEpoch == rx_GetConnectionEpoch(tcon))) {
2696 goto retryfirstclient;
2701 ObtainWriteLock(&client->lock);
2702 client->z.refCount = 1;
2703 client->z.host = host;
2704 client->z.InSameNetwork = host->z.InSameNetwork;
2705 client->z.ViceId = viceid;
2706 client->z.expTime = expTime; /* rx only */
2707 client->z.authClass = authClass; /* rx only */
2708 client->z.sid = rx_GetConnectionId(tcon);
2709 client->z.VenusEpoch = rx_GetConnectionEpoch(tcon);
2710 client->z.CPS.prlist_val = NULL;
2711 client->z.CPS.prlist_len = 0;
2715 client->z.prfail = fail;
2717 if (!(client->z.CPS.prlist_val) || (viceid != client->z.ViceId)) {
2718 client->z.CPS.prlist_len = 0;
2719 if (client->z.CPS.prlist_val && (client->z.ViceId != ANONYMOUSID))
2720 free(client->z.CPS.prlist_val);
2721 client->z.CPS.prlist_val = NULL;
2722 client->z.ViceId = viceid;
2723 client->z.expTime = expTime;
2725 if (viceid == ANONYMOUSID) {
2726 client->z.CPS.prlist_len = AnonCPS.prlist_len;
2727 client->z.CPS.prlist_val = AnonCPS.prlist_val;
2730 code = hpr_GetCPS(viceid, &client->z.CPS);
2735 ("pr_GetCPS failed(%d) for user %d, host %" AFS_PTR_FMT " (%s:%d)\n",
2736 code, viceid, client->z.host,
2737 afs_inet_ntoa_r(client->z.host->z.host,hoststr),
2738 ntohs(client->z.host->z.port)));
2740 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2741 * all protection servers and reevaluates things if no
2742 * sync server or quorum is found we could still end up
2743 * with one of these errors. In such case we would like to
2744 * reevaluate the rpc call to find if there's cps for this
2745 * guy. We treat other errors (except network failures
2746 * ones - i.e. code < 0) as an indication that there is no
2747 * CPS for this host. Ideally we could like to deal this
2748 * problem the other way around (i.e. if code == NOCPS
2749 * ignore else retry next time) but the problem is that
2750 * there're other errors (i.e. EPERM) for which we don't
2751 * want to retry and we don't know the whole code list!
2753 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2754 client->z.prfail = 1;
2757 /* the disabling of system:administrators is so iffy and has so many
2758 * possible failure modes that we will disable it again */
2759 /* Turn off System:Administrator for safety
2760 * if (AL_IsAMember(SystemId, client->z.CPS) == 0)
2761 * osi_Assert(AL_DisableGroup(SystemId, client->z.CPS) == 0); */
2764 /* Now, tcon may already be set to a rock, since we blocked with no host
2765 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2766 * required). So, before setting the RPC's rock, we should disconnect
2767 * the RPC from the other client structure's rock.
2769 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2770 if (oldClient && oldClient != client
2771 && oldClient->z.sid == rx_GetConnectionId(tcon)
2772 && oldClient->z.VenusEpoch == rx_GetConnectionEpoch(tcon)
2773 && !(oldClient->z.host->z.hostFlags & HOSTDELETED)) {
2775 if (!oldClient->z.deleted) {
2776 /* if we didn't create it, it's not ours to put back */
2778 ViceLog(0, ("FindClient: stillborn client %p(%x); "
2779 "conn %p (host %s:%d) had client %p(%x)\n",
2780 client, client->z.sid, tcon,
2781 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2782 ntohs(rxr_PortOf(tcon)),
2783 oldClient, oldClient->z.sid));
2784 if ((client->z.ViceId != ANONYMOUSID) && client->z.CPS.prlist_val)
2785 free(client->z.CPS.prlist_val);
2786 client->z.CPS.prlist_val = NULL;
2787 client->z.CPS.prlist_len = 0;
2789 /* We should perhaps check for 0 here */
2790 client->z.refCount--;
2791 ReleaseWriteLock(&client->lock);
2796 oldClient->z.refCount++;
2798 h_Hold_r(oldClient->z.host);
2799 h_Release_r(client->z.host);
2802 ObtainWriteLock(&oldClient->lock);
2805 host = oldClient->z.host;
2807 ViceLog(0, ("FindClient: deleted client %p(%x ref %d host %p href "
2808 "%d) already had conn %p (host %s:%d, cid %x), stolen "
2809 "by client %p(%x, ref %d host %p href %d)\n",
2810 oldClient, oldClient->z.sid, oldClient->z.refCount,
2811 oldClient->z.host, oldClient->z.host->z.refCount, tcon,
2812 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2813 ntohs(rxr_PortOf(tcon)), rx_GetConnectionId(tcon),
2814 client, client->z.sid, client->z.refCount,
2815 client->z.host, client->z.host->z.refCount));
2816 /* rx_SetSpecific will be done immediately below */
2819 /* Avoid chaining in more than once. */
2823 if (host->z.hostFlags & HOSTDELETED) {
2828 client->z.host = NULL;
2830 if ((client->z.ViceId != ANONYMOUSID) && client->z.CPS.prlist_val)
2831 free(client->z.CPS.prlist_val);
2832 client->z.CPS.prlist_val = NULL;
2833 client->z.CPS.prlist_len = 0;
2835 client->z.refCount--;
2836 ReleaseWriteLock(&client->lock);
2841 client->z.next = host->z.FirstClient;
2842 host->z.FirstClient = client;
2844 CurrentConnections++; /* increment number of connections */
2846 rx_SetSpecific(tcon, rxcon_client_key, client);
2847 ReleaseWriteLock(&client->lock);
2851 } /*h_FindClient_r */
2854 h_ReleaseClient_r(struct client *client)
2856 opr_Assert(client->z.refCount > 0);
2857 client->z.refCount--;
2863 * Sigh: this one is used to get the client AGAIN within the individual
2864 * server routines. This does not bother h_Holding the host, since
2865 * this is assumed already have been done by the server main loop.
2866 * It does check tokens, since only the server routines can return the
2867 * VICETOKENDEAD error code
2870 GetClient(struct rx_connection *tcon, struct client **cp)
2872 struct client *client;
2877 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2878 if (client == NULL) {
2880 ("GetClient: no client in conn %p (host %s:%d), VBUSYING\n",
2881 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2882 ntohs(rxr_PortOf(tcon))));
2886 if (rx_GetConnectionId(tcon) != client->z.sid
2887 || rx_GetConnectionEpoch(tcon) != client->z.VenusEpoch) {
2889 ("GetClient: tcon %p tcon sid %d client sid %d\n",
2890 tcon, rx_GetConnectionId(tcon), client->z.sid));
2894 if (client && client->z.LastCall > client->z.expTime && client->z.expTime) {
2896 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2897 afs_inet_ntoa_r(client->z.host->z.host, hoststr),
2898 ntohs(client->z.host->z.port), client->z.expTime));
2900 return VICETOKENDEAD;
2902 if (client->z.deleted) {
2903 ViceLog(0, ("GetClient: got deleted client, connection will appear "
2904 "anonymous; tcon %p cid %x client %p ref %d host %p "
2905 "(%s:%d) href %d ViceId %d\n",
2906 tcon, rx_GetConnectionId(tcon), client, client->z.refCount,
2908 afs_inet_ntoa_r(client->z.host->z.host, hoststr),
2909 (int)ntohs(client->z.host->z.port), client->z.host->z.refCount,
2910 (int)client->z.ViceId));
2913 client->z.refCount++;
2920 PutClient(struct client **cp)
2926 h_ReleaseClient_r(*cp);
2933 /* Client user name for short term use. Note that this is NOT inexpensive */
2935 h_UserName(struct client *client)
2937 static char User[PR_MAXNAMELEN + 1];
2941 lids.idlist_len = 1;
2942 lids.idlist_val = malloc(1 * sizeof(afs_int32));
2943 if (!lids.idlist_val) {
2944 ViceLogThenPanic(0, ("Failed malloc in h_UserName\n"));
2946 lnames.namelist_len = 0;
2947 lnames.namelist_val = (prname *) 0;
2948 lids.idlist_val[0] = client->z.ViceId;
2949 if (hpr_IdToName(&lids, &lnames)) {
2950 /* We need to free id we alloced above! */
2951 free(lids.idlist_val);
2952 return "*UNKNOWN USER NAME*";
2954 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2955 free(lids.idlist_val);
2956 free(lnames.namelist_val);
2965 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2966 CEs, CEBlocks, HTs, HTBlocks));
2972 h_PrintClient(struct host *host, void *rock)
2974 StreamHandle_t *file = (StreamHandle_t *)rock;
2975 struct client *client;
2980 time_t LastCall, expTime;
2984 LastCall = host->z.LastCall;
2985 if (host->z.hostFlags & HOSTDELETED) {
2989 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2990 localtime_r(&LastCall, &tm));
2991 snprintf(tmpStr, sizeof tmpStr, "Host %s:%d down = %d, LastCall %s\n",
2992 afs_inet_ntoa_r(host->z.host, hoststr),
2993 ntohs(host->z.port), (host->z.hostFlags & VENUSDOWN),
2995 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2996 for (client = host->z.FirstClient; client; client = client->z.next) {
2997 if (!client->z.deleted) {
2998 expTime = client->z.expTime;
2999 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
3000 localtime_r(&expTime, &tm));
3001 snprintf(tmpStr, sizeof tmpStr,
3002 " user id=%d, name=%s, sl=%s till %s\n",
3003 client->z.ViceId, h_UserName(client),
3004 client->z.authClass ? "Authenticated"
3005 : "Not authenticated",
3006 client->z.authClass ? tbuffer : "No Limit");
3007 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3008 snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
3009 client->z.CPS.prlist_len);
3010 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3011 if (client->z.CPS.prlist_val) {
3012 for (i = 0; i < client->z.CPS.prlist_len; i++) {
3013 snprintf(tmpStr, sizeof tmpStr, " %d",
3014 client->z.CPS.prlist_val[i]);
3015 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3018 sprintf(tmpStr, "]\n");
3019 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3025 } /*h_PrintClient */
3030 * Print a list of clients, with last security level and token value seen,
3034 h_PrintClients(void)
3041 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
3045 ("Couldn't create client dump file %s\n",
3046 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
3050 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
3051 localtime_r(&now, &tm));
3052 snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n\n",
3054 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3055 h_Enumerate(h_PrintClient, (char *)file);
3056 STREAM_REALLYCLOSE(file);
3057 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
3064 h_DumpHost(struct host *host, void *rock)
3066 StreamHandle_t *file = (StreamHandle_t *)rock;
3073 snprintf(tmpStr, sizeof tmpStr,
3074 "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u "
3075 "down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u "
3077 afs_inet_ntoa_r(host->z.host, hoststr), ntohs(host->z.port),
3078 host->index, host->z.cblist, CheckLock(&host->lock),
3079 host->z.LastCall, host->z.ActiveCall, (host->z.hostFlags & VENUSDOWN),
3080 host->z.hostFlags & HOSTDELETED, host->z.Console,
3081 host->z.hostFlags & CLIENTDELETED, host->z.hcpsfailed,
3083 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3084 if (host->z.hcps.prlist_val)
3085 for (i = 0; i < host->z.hcps.prlist_len; i++) {
3086 snprintf(tmpStr, sizeof tmpStr, " %d", host->z.hcps.prlist_val[i]);
3087 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3089 sprintf(tmpStr, "] [");
3090 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3091 if (host->z.interface)
3092 for (i = 0; i < host->z.interface->numberOfInterfaces; i++) {
3094 sprintf(tmpStr, " %s:%d",
3095 afs_inet_ntoa_r(host->z.interface->interface[i].addr, hoststr),
3096 ntohs(host->z.interface->interface[i].port));
3097 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3099 sprintf(tmpStr, "] refCount:%d hostFlags:%hu\n", host->z.refCount, host->z.hostFlags);
3100 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3112 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
3119 ("Couldn't create host dump file %s\n",
3120 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
3124 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
3125 localtime_r(&now, &tm));
3126 snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n\n", tbuffer);
3127 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
3128 h_Enumerate(h_DumpHost, (char *)file);
3129 STREAM_REALLYCLOSE(file);
3130 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
3134 #ifdef AFS_DEMAND_ATTACH_FS
3137 * host state serialization
3139 static int h_stateFillHeader(struct host_state_header * hdr);
3140 static int h_stateCheckHeader(struct host_state_header * hdr);
3141 static int h_stateAllocMap(struct fs_dump_state * state);
3142 static int h_stateSaveHost(struct host * host, void *rock);
3143 static int h_stateRestoreHost(struct fs_dump_state * state);
3144 static int h_stateRestoreIndex(struct host * h, void *rock);
3145 static int h_stateVerifyHost(struct host * h, void *rock);
3146 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
3147 afs_uint32 addr, afs_uint16 port, int valid);
3148 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
3149 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
3150 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
3153 * Is this host busy?
3155 * This is just a hint and should not be trusted; this should probably only be
3156 * used by the host state serialization code when trying to detect if a host
3157 * can be sanely serialized to disk or not. If this function returns 1, the
3158 * host may be in an invalid state and thus should not be saved to disk.
3161 h_isBusy_r(struct host *host)
3163 struct Lock *hostLock = &host->lock;
3166 LOCK_LOCK(hostLock);
3167 if (hostLock->excl_locked || hostLock->readers_reading) {
3170 LOCK_UNLOCK(hostLock);
3176 if (hostBusyFlags(host->z.hostFlags)) {
3183 /* this procedure saves all host state to disk for fast startup */
3185 h_stateSave(struct fs_dump_state * state)
3187 AssignInt64(state->eof_offset, &state->hdr->h_offset);
3190 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
3192 /* invalidate host state header */
3193 memset(state->h_hdr, 0, sizeof(struct host_state_header));
3195 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
3196 sizeof(struct host_state_header))) {
3201 fs_stateIncEOF(state, sizeof(struct host_state_header));
3203 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
3208 h_stateFillHeader(state->h_hdr);
3210 /* write the real header to disk */
3211 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
3212 sizeof(struct host_state_header));
3219 * host state serialization
3221 * this procedure restores all host state from a disk for fast startup
3224 h_stateRestore(struct fs_dump_state * state)
3228 /* seek to the right position and read in the host state header */
3229 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
3230 sizeof(struct host_state_header))) {
3235 /* check the validity of the header */
3236 if (h_stateCheckHeader(state->h_hdr)) {
3241 records = state->h_hdr->records;
3243 if (h_stateAllocMap(state)) {
3248 /* iterate over records restoring host state */
3249 for (i=0; i < records; i++) {
3250 if (h_stateRestoreHost(state) != 0) {
3261 h_stateRestoreIndices(struct fs_dump_state * state)
3263 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
3268 h_stateRestoreIndex(struct host * h, void *rock)
3270 struct fs_dump_state *state = (struct fs_dump_state *)rock;
3271 if (cb_OldToNew(state, h->z.cblist, &h->z.cblist)) {
3272 return H_ENUMERATE_BAIL(0);
3278 h_stateVerify(struct fs_dump_state * state)
3280 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
3285 h_stateVerifyHost(struct host * h, void* rock)
3287 struct fs_dump_state *state = (struct fs_dump_state *)rock;
3291 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
3292 return H_ENUMERATE_BAIL(0);
3295 if (h->z.interface) {
3296 for (i = h->z.interface->numberOfInterfaces-1; i >= 0; i--) {
3297 if (h_stateVerifyAddrHash(state, h, h->z.interface->interface[i].addr,
3298 h->z.interface->interface[i].port,
3299 h->z.interface->interface[i].valid)) {
3303 if (h_stateVerifyUuidHash(state, h)) {
3306 } else if (h_stateVerifyAddrHash(state, h, h->z.host, h->z.port, 1)) {
3310 if (cb_stateVerifyHCBList(state, h)) {
3318 * verify a host is either in, or absent from, the addr hash table.
3320 * @param[in] state fs dump state
3321 * @param[in] h host we're dealing with
3322 * @param[in] addr addr to look for (NBO)
3323 * @param[in] port port to look for (NBO)
3324 * @param[in] valid 1 if we're verifying that the specified addr and port
3325 * in the hash table point to the specified host. 0 if we're
3326 * verifying that the specified addr and port do NOT point
3327 * to the specified host
3329 * @return operation status
3330 * @retval 1 failed to verify, bail out
3331 * @retval 0 verified successfully, all is well
3334 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
3335 afs_uint32 addr, afs_uint16 port, int valid)
3337 int ret = 0, found = 0;
3338 struct host *host = NULL;
3339 struct h_AddrHashChain *chain;
3340 int index = h_HashIndex(addr);
3344 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
3345 host = chain->hostPtr;
3347 afs_inet_ntoa_r(addr, tmp);
3348 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
3352 if ((chain->addr == addr) && (chain->port == port)) {
3355 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry "
3356 "points to different host struct (%d, %d)\n",
3357 h->index, host->index));
3358 state->flags.warnings_generated = 1;
3362 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s:%u is "
3363 "marked invalid, but points to the containing "
3364 "host\n", afs_inet_ntoa_r(addr, tmp),
3365 (unsigned)htons(port)));
3373 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
3374 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
3375 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
3382 if (!found && valid) {
3383 afs_inet_ntoa_r(addr, tmp);
3384 if (state->mode == FS_STATE_LOAD_MODE) {
3385 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s:%u not found in hash\n",
3386 tmp, (unsigned)htons(port)));
3390 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s:%u not found in hash\n",
3391 tmp, (unsigned)htons(port)));
3392 state->flags.warnings_generated = 1;
3401 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
3404 struct host *host = NULL;
3405 struct h_UuidHashChain *chain;
3406 afsUUID * uuidp = &h->z.interface->uuid;
3407 int index = h_UuidHashIndex(uuidp);
3411 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
3412 host = chain->hostPtr;
3414 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
3415 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
3419 if (host->z.interface &&
3420 afs_uuid_equal(&host->z.interface->uuid, uuidp)) {
3422 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
3423 h->index, host->index));
3424 state->flags.warnings_generated = 1;
3428 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
3429 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
3430 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
3437 /* Fall through, so host not found */
3439 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
3440 if (state->mode == FS_STATE_LOAD_MODE) {
3441 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
3445 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
3446 state->flags.warnings_generated = 1;
3453 /* create the host state header structure */
3455 h_stateFillHeader(struct host_state_header * hdr)
3457 hdr->stamp.magic = HOST_STATE_MAGIC;
3458 hdr->stamp.version = HOST_STATE_VERSION;
3462 /* check the contents of the host state header structure */
3464 h_stateCheckHeader(struct host_state_header * hdr)
3468 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
3469 ViceLog(0, ("check_host_state_header: invalid state header\n"));
3472 else if (hdr->stamp.version != HOST_STATE_VERSION) {
3473 ViceLog(0, ("check_host_state_header: unknown version number\n"));
3479 /* allocate the host id mapping table */
3481 h_stateAllocMap(struct fs_dump_state * state)
3483 state->h_map.len = state->h_hdr->index_max + 1;
3484 state->h_map.entries = (struct idx_map_entry_t *)
3485 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
3486 return (state->h_map.entries != NULL) ? 0 : 1;
3489 /* function called by h_Enumerate to save a host to disk */
3491 h_stateSaveHost(struct host * host, void* rock)
3493 struct fs_dump_state *state = (struct fs_dump_state *) rock;
3494 int if_len=0, hcps_len=0;
3495 struct hostDiskEntry hdsk;
3496 struct host_state_entry_header hdr;
3497 struct Interface * ifp = NULL;
3498 afs_int32 * hcps = NULL;
3499 struct iovec iov[4];
3502 if (h_isBusy_r(host)) {
3504 ViceLog(1, ("Not saving host %s:%d to disk; host appears busy\n",
3505 afs_inet_ntoa_r(host->z.host, hoststr), (int)ntohs(host->z.port)));
3506 /* Make sure we don't try to save callbacks to disk for this host, or
3507 * we'll get confused on restore */
3508 DeleteAllCallBacks_r(host, 1);
3512 memset(&hdr, 0, sizeof(hdr));
3514 if (state->h_hdr->index_max < host->index) {
3515 state->h_hdr->index_max = host->index;
3518 h_hostToDiskEntry_r(host, &hdsk);
3519 if (host->z.interface) {
3520 if_len = sizeof(struct Interface) +
3521 ((host->z.interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
3522 ifp = malloc(if_len);
3523 opr_Assert(ifp != NULL);
3524 memcpy(ifp, host->z.interface, if_len);
3525 hdr.interfaces = host->z.interface->numberOfInterfaces;
3526 iov[iovcnt].iov_base = (char *) ifp;
3527 iov[iovcnt].iov_len = if_len;
3530 if (host->z.hcps.prlist_val) {
3531 hdr.hcps = host->z.hcps.prlist_len;
3532 hcps_len = hdr.hcps * sizeof(afs_int32);
3533 hcps = malloc(hcps_len);
3534 opr_Assert(hcps != NULL);
3535 memcpy(hcps, host->z.hcps.prlist_val, hcps_len);
3536 iov[iovcnt].iov_base = (char *) hcps;
3537 iov[iovcnt].iov_len = hcps_len;
3541 if (hdsk.index > state->h_hdr->index_max)
3542 state->h_hdr->index_max = hdsk.index;
3544 hdr.len = sizeof(struct host_state_entry_header) +
3545 sizeof(struct hostDiskEntry) + if_len + hcps_len;
3546 hdr.magic = HOST_STATE_ENTRY_MAGIC;
3548 iov[0].iov_base = (char *) &hdr;
3549 iov[0].iov_len = sizeof(hdr);
3550 iov[1].iov_base = (char *) &hdsk;
3551 iov[1].iov_len = sizeof(struct hostDiskEntry);
3553 if (fs_stateWriteV(state, iov, iovcnt)) {
3554 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
3558 fs_stateIncEOF(state, hdr.len);
3560 state->h_hdr->records++;
3567 return H_ENUMERATE_BAIL(0);
3572 /* restores a host from disk */
3574 h_stateRestoreHost(struct fs_dump_state * state)
3576 int ifp_len=0, hcps_len=0, bail=0;
3577 struct host_state_entry_header hdr;
3578 struct hostDiskEntry hdsk;
3579 struct host *host = NULL;
3580 struct Interface *ifp = NULL;
3581 afs_int32 * hcps = NULL;
3582 struct iovec iov[3];
3585 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
3586 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
3592 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
3593 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
3599 iov[0].iov_base = (char *) &hdsk;
3600 iov[0].iov_len = sizeof(struct hostDiskEntry);
3602 if (hdr.interfaces) {
3603 ifp_len = sizeof(struct Interface) +
3604 ((hdr.interfaces-1) * sizeof(struct AddrPort));
3605 ifp = malloc(ifp_len);
3606 opr_Assert(ifp != NULL);
3607 iov[iovcnt].iov_base = (char *) ifp;
3608 iov[iovcnt].iov_len = ifp_len;
3612 hcps_len = hdr.hcps * sizeof(afs_int32);
3613 hcps = malloc(hcps_len);
3614 opr_Assert(hcps != NULL);
3615 iov[iovcnt].iov_base = (char *) hcps;
3616 iov[iovcnt].iov_len = hcps_len;
3620 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
3621 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
3626 if (fs_stateReadV(state, iov, iovcnt)) {
3627 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
3632 if (!hdr.hcps && hdsk.hcps_valid) {
3633 /* valid, zero-length host cps ; does this ever happen? */
3634 hcps = malloc(sizeof(afs_int32));
3635 opr_Assert(hcps != NULL);
3638 if (hostBusyFlags(hdsk.hostFlags)) {
3640 ViceLog(0, ("h_stateRestoreHost: skipping host %s:%d due to invalid flags 0x%x\n",
3641 afs_inet_ntoa_r(hdsk.host, hoststr), (int)ntohs(hdsk.port),
3642 (unsigned)hdsk.hostFlags));
3644 state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_SKIPPED;
3648 /* for restoring state, we better be able to get a host! */
3650 opr_Assert(host != NULL);
3653 host->z.interface = ifp;
3656 host->z.hcps.prlist_val = hcps;
3657 host->z.hcps.prlist_len = hdr.hcps;
3660 h_diskEntryToHost_r(&hdsk, host);
3661 h_SetupCallbackConn_r(host);
3663 h_AddHostToAddrHashTable_r(host->z.host, host->z.port, host);
3666 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
3667 if (ifp->interface[i].valid &&
3668 !(ifp->interface[i].addr == host->z.host &&
3669 ifp->interface[i].port == host->z.port)) {
3670 h_AddHostToAddrHashTable_r(ifp->interface[i].addr,
3671 ifp->interface[i].port,
3675 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
3677 h_InsertList_r(host);
3679 /* setup host id map entry */
3680 state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_VALID;
3681 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
3682 state->h_map.entries[hdsk.index].new_idx = host->index;
3694 /* serialize a host structure to disk */
3696 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
3698 out->host = in->z.host;
3699 out->port = in->z.port;
3700 out->hostFlags = in->z.hostFlags;
3701 out->Console = in->z.Console;
3702 out->hcpsfailed = in->z.hcpsfailed;
3703 out->LastCall = in->z.LastCall;
3704 out->ActiveCall = in->z.ActiveCall;
3705 out->cpsCall = in->z.cpsCall;
3706 out->cblist = in->z.cblist;
3707 out->InSameNetwork = in->z.InSameNetwork;
3709 /* special fields we save, but are not memcpy'd back on restore */
3710 out->index = in->index;
3711 out->hcps_len = in->z.hcps.prlist_len;
3712 out->hcps_valid = (in->z.hcps.prlist_val == NULL) ? 0 : 1;
3715 /* restore a host structure from disk */
3717 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
3719 out->z.host = in->host;
3720 out->z.port = in->port;
3721 out->z.hostFlags = in->hostFlags;
3722 out->z.Console = in->Console;
3723 out->z.hcpsfailed = in->hcpsfailed;
3724 out->z.LastCall = in->LastCall;
3725 out->z.ActiveCall = in->ActiveCall;
3726 out->z.cpsCall = in->cpsCall;
3727 out->z.cblist = in->cblist;
3728 out->z.InSameNetwork = in->InSameNetwork;
3731 /* index translation routines */
3733 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
3737 /* hosts use a zero-based index, so old==0 is valid */
3739 if (old >= state->h_map.len) {
3740 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
3742 } else if (state->h_map.entries[old].valid != FS_STATE_IDX_VALID ||
3743 state->h_map.entries[old].old_idx != old) { /* sanity check */
3744 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
3747 *new = state->h_map.entries[old].new_idx;
3752 #endif /* AFS_DEMAND_ATTACH_FS */
3756 * This counts the number of workstations, the number of active workstations,
3757 * and the number of workstations declared "down" (i.e. not heard from
3758 * recently). An active workstation has received a call since the cutoff
3759 * time argument passed.
3762 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3765 int num = 0, active = 0, del = 0;
3769 for (count = 0, host = hostList; host && count < hostCount; host = host->z.next, count++) {
3770 if (!(host->z.hostFlags & HOSTDELETED)) {
3772 if (host->z.ActiveCall > cutofftime)
3774 if (host->z.hostFlags & VENUSDOWN)
3778 if (count != hostCount) {
3779 ViceLog(0, ("h_GetWorkStats found %d of %d hosts\n", count, hostCount));
3780 } else if (host != NULL) {
3781 ViceLog(0, ("h_GetWorkStats found more than %d hosts\n", hostCount));
3782 ShutDownAndCore(PANIC);
3792 } /*h_GetWorkStats */
3795 h_GetWorkStats64(afs_uint64 *nump, afs_uint64 *activep, afs_uint64 *delp,
3796 afs_int32 cutofftime)
3798 int num, active, del;
3799 h_GetWorkStats(&num, &active, &del, cutofftime);
3808 /*------------------------------------------------------------------------
3809 * PRIVATE h_ClassifyAddress
3812 * Given a target IP address and a candidate IP address (both
3813 * in host byte order), classify the candidate into one of three
3814 * buckets in relation to the target by bumping the counters passed
3818 * a_targetAddr : Target address.
3819 * a_candAddr : Candidate address.
3820 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3821 * addresses are either in the same network
3822 * or the same subnet.
3823 * a_diffSubnetP : ...when the candidate is in a different
3825 * a_diffNetworkP : ...when the candidate is in a different
3832 * The target and candidate addresses are both in host byte
3833 * order, NOT network byte order, when passed in.
3837 *------------------------------------------------------------------------*/
3840 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3841 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3842 afs_int32 * a_diffNetworkP)
3843 { /*h_ClassifyAddress */
3845 afs_uint32 targetNet;
3846 afs_uint32 targetSubnet;
3848 afs_uint32 candSubnet;
3851 * Put bad values into the subnet info to start with.
3853 targetSubnet = (afs_uint32) 0;
3854 candSubnet = (afs_uint32) 0;
3857 * Pull out the network and subnetwork numbers from the target
3858 * and candidate addresses. We can short-circuit this whole
3859 * affair if the target and candidate addresses are not of the
3862 if (IN_CLASSA(a_targetAddr)) {
3863 if (!(IN_CLASSA(a_candAddr))) {
3864 (*a_diffNetworkP)++;
3867 targetNet = a_targetAddr & IN_CLASSA_NET;
3868 candNet = a_candAddr & IN_CLASSA_NET;
3869 if (IN_SUBNETA(a_targetAddr))
3870 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
3871 if (IN_SUBNETA(a_candAddr))
3872 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
3873 } else if (IN_CLASSB(a_targetAddr)) {
3874 if (!(IN_CLASSB(a_candAddr))) {
3875 (*a_diffNetworkP)++;
3878 targetNet = a_targetAddr & IN_CLASSB_NET;
3879 candNet = a_candAddr & IN_CLASSB_NET;
3880 if (IN_SUBNETB(a_targetAddr))
3881 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
3882 if (IN_SUBNETB(a_candAddr))
3883 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
3884 } /*Class B target */
3885 else if (IN_CLASSC(a_targetAddr)) {
3886 if (!(IN_CLASSC(a_candAddr))) {
3887 (*a_diffNetworkP)++;
3890 targetNet = a_targetAddr & IN_CLASSC_NET;
3891 candNet = a_candAddr & IN_CLASSC_NET;
3894 * Note that class C addresses can't have subnets,
3895 * so we leave the defaults untouched.
3897 } /*Class C target */
3899 targetNet = a_targetAddr;
3900 candNet = a_candAddr;
3901 } /*Class D address */
3904 * Now, simply compare the extracted net and subnet values for
3905 * the two addresses (which at this point are known to be of the
3908 if (targetNet == candNet) {
3909 if (targetSubnet == candSubnet)
3910 (*a_sameNetOrSubnetP)++;
3914 (*a_diffNetworkP)++;
3916 } /*h_ClassifyAddress */
3919 /*------------------------------------------------------------------------
3920 * EXPORTED h_GetHostNetStats
3923 * Iterate through the host table, and classify each (non-deleted)
3924 * host entry into ``proximity'' categories (same net or subnet,
3925 * different subnet, different network).
3928 * a_numHostsP : Set to total number of (non-deleted) hosts.
3929 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
3930 * a_diffSubnetP : Set to # hosts on diff subnet as server.
3931 * a_diffNetworkP : Set to # hosts on diff network as server.
3937 * We only count non-deleted hosts. The storage pointed to by our
3938 * parameters is zeroed upon entry.
3942 *------------------------------------------------------------------------*/
3945 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
3946 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
3947 { /*h_GetHostNetStats */
3949 struct host *hostP; /*Ptr to current host entry */
3950 afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
3954 * Clear out the storage pointed to by our parameters.
3956 *a_numHostsP = (afs_int32) 0;
3957 *a_sameNetOrSubnetP = (afs_int32) 0;
3958 *a_diffSubnetP = (afs_int32) 0;
3959 *a_diffNetworkP = (afs_int32) 0;
3962 for (count = 0, hostP = hostList; hostP && count < hostCount; hostP = hostP->z.next, count++) {
3963 if (!(hostP->z.hostFlags & HOSTDELETED)) {
3965 * Bump the number of undeleted host entries found.
3966 * In classifying the current entry's address, make
3967 * sure to first convert to host byte order.
3970 currAddr_HBO = (afs_uint32) ntohl(hostP->z.host);
3971 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
3972 a_sameNetOrSubnetP, a_diffSubnetP,
3974 } /*Only look at non-deleted hosts */
3975 } /*For each host record hashed to this index */
3976 if (count != hostCount) {
3977 ViceLog(0, ("h_GetHostNetStats found %d of %d hosts\n", count, hostCount));
3978 } else if (hostP != NULL) {
3979 ViceLog(0, ("h_GetHostNetStats found more than %d hosts\n", hostCount));
3980 ShutDownAndCore(PANIC);
3983 } /*h_GetHostNetStats */
3985 static afs_uint32 checktime;
3986 static afs_uint32 clientdeletetime;
3987 static struct AFSFid zerofid;
3991 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
3992 * Since it can serialize them, and pile up, it should be a separate LWP
3993 * from other events.
3997 CheckHost_r(struct host *host, void *dummy)
3999 struct client *client;
4000 struct rx_connection *cb_conn = NULL;
4003 #ifdef AFS_DEMAND_ATTACH_FS
4004 /* kill the checkhost lwp ASAP during shutdown */
4006 if (fs_state.mode == FS_MODE_SHUTDOWN) {
4008 return H_ENUMERATE_BAIL(0);
4013 /* Host is held by h_Enumerate_r */
4014 for (client = host->z.FirstClient; client; client = client->z.next) {
4015 if (client->z.refCount == 0 && client->z.LastCall < clientdeletetime) {
4016 client->z.deleted = 1;
4017 host->z.hostFlags |= CLIENTDELETED;
4020 if (host->z.LastCall < checktime) {
4022 if (!(host->z.hostFlags & HOSTDELETED)) {
4023 host->z.hostFlags |= HWHO_INPROGRESS;
4024 cb_conn = host->z.callback_rxcon;
4025 rx_GetConnection(cb_conn);
4026 if (host->z.LastCall < clientdeletetime) {
4027 host->z.hostFlags |= HOSTDELETED;
4028 if (!(host->z.hostFlags & VENUSDOWN)) {
4029 host->z.hostFlags &= ~ALTADDR; /* alternate address invalid */
4030 if (host->z.interface) {
4033 RXAFSCB_InitCallBackState3(cb_conn,
4039 RXAFSCB_InitCallBackState(cb_conn);
4042 host->z.hostFlags |= ALTADDR; /* alternate addresses valid */
4045 (void)afs_inet_ntoa_r(host->z.host, hoststr);
4047 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
4048 hoststr, ntohs(host->z.port)));
4049 host->z.hostFlags |= VENUSDOWN;
4051 /* Note: it's safe to delete hosts even if they have call
4052 * back state, because break delayed callbacks (called when a
4053 * message is received from the workstation) will always send a
4054 * break all call backs to the workstation if there is no
4059 if (!(host->z.hostFlags & VENUSDOWN) && host->z.cblist) {
4061 (void)afs_inet_ntoa_r(host->z.host, hoststr);
4062 if (host->z.interface) {
4063 afsUUID uuid = host->z.interface->uuid;
4065 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
4068 if (MultiProbeAlternateAddress_r(host)) {
4069 ViceLog(0,("CheckHost_r: Probing all interfaces of host %s:%d failed, code %d\n",
4070 hoststr, ntohs(host->z.port), code));
4071 host->z.hostFlags |= VENUSDOWN;
4076 code = RXAFSCB_Probe(cb_conn);
4080 ("CheckHost_r: Probe failed for host %s:%d, code %d\n",
4081 hoststr, ntohs(host->z.port), code));
4082 host->z.hostFlags |= VENUSDOWN;
4088 rx_PutConnection(cb_conn);
4091 host->z.hostFlags &= ~HWHO_INPROGRESS;
4101 * Set VenusDown for any hosts that have not had a call in 15 minutes and
4102 * don't respond to a probe. Note that VenusDown can only be cleared if
4103 * a message is received from the host (see ServerLWP in file.c).
4104 * Delete hosts that have not had any calls in 1 hour, clients that
4105 * have not had any calls in 15 minutes.
4107 * This routine is called roughly every 5 minutes.
4112 afs_uint32 now = time(NULL);
4114 memset(&zerofid, 0, sizeof(zerofid));
4116 * Send a probe to the workstation if it hasn't been heard from in
4119 checktime = now - 15 * 60;
4120 clientdeletetime = now - 120 * 60; /* 2 hours ago */
4123 h_Enumerate_r(CheckHost_r, hostList, NULL);
4128 * This is called with host locked and held. At this point, the
4129 * hostAddrHashTable has an entry for the primary addr/port inserted
4130 * by h_Alloc_r(). No other interfaces should be considered valid.
4132 * The addresses in the interfaceAddr list are in host byte order.
4135 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
4142 struct Interface *interface;
4145 afs_uint16 port7001 = htons(7001);
4150 number = interf->numberOfInterfaces;
4151 myAddr = host->z.host; /* current interface address */
4152 myPort = host->z.port; /* current port */
4155 ("initInterfaceAddr : host %s:%d numAddr %d\n",
4156 afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
4158 /* validation checks */
4159 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
4161 ("Invalid number of alternate addresses is %d\n", number));
4166 * The client's notion of its own IP addresses is not reliable.
4168 * 1. The client list might contain private address ranges which
4169 * are likely to be re-used by many clients allocated addresses
4172 * 2. The client list will not include any public addresses that
4173 * are hidden by a NAT.
4175 * 3. Private address ranges that are exposed to the server will
4176 * be obtained from the rx connections that use them.
4178 * 4. Lists provided by the client are not necessarily truthful.
4179 * Many existing clients (UNIX) do not refresh the IP address
4180 * list as the actual assigned addresses change. The end result
4181 * is that they report the initial address list for the lifetime
4182 * of the process. In other words, a client can report addresses
4183 * that they are in fact not using. Adding these addresses to
4184 * the host interface list without verification is not only
4185 * pointless, it is downright dangerous.
4187 * We therefore do not add alternate addresses to the addr hash table.
4188 * We only use them for multi-rx callback breaks.
4192 * Convert IP addresses to network byte order, and remove
4193 * duplicate and loopback IP addresses from the interface list, and
4194 * determine whether or not the incoming addr/port is
4195 * listed. Note that if the address matches it is not
4196 * truly a match because the port number for the entries
4197 * in the interface list are port 7001 and the port number
4198 * for this connection might not be 7001.
4200 for (i = 0, count = 0, found = 0; i < number; i++) {
4201 if (rx_IsLoopbackAddr(interf->addr_in[i])) {
4204 interf->addr_in[i] = htonl(interf->addr_in[i]);
4205 for (j = 0; j < count; j++) {
4206 if (interf->addr_in[j] == interf->addr_in[i])
4210 interf->addr_in[count] = interf->addr_in[i];
4211 if (interf->addr_in[count] == myAddr &&
4219 * Allocate and initialize an interface structure for this host.
4222 interface = malloc(sizeof(struct Interface) +
4223 (sizeof(struct AddrPort) * (count - 1)));
4225 ViceLogThenPanic(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
4227 interface->numberOfInterfaces = count;
4229 interface = malloc(sizeof(struct Interface) +
4230 (sizeof(struct AddrPort) * count));
4232 ViceLogThenPanic(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
4234 interface->numberOfInterfaces = count + 1;
4235 interface->interface[count].addr = myAddr;
4236 interface->interface[count].port = myPort;
4237 interface->interface[count].valid = 1;
4240 for (i = 0; i < count; i++) {
4242 interface->interface[i].addr = interf->addr_in[i];
4243 /* We store the port as 7001 because the addresses reported by
4244 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
4245 * are coming from fully connected hosts (no NAT/PATs)
4247 interface->interface[i].port = port7001;
4248 interface->interface[i].valid =
4249 (interf->addr_in[i] == myAddr && port7001 == myPort) ? 1 : 0;
4252 interface->uuid = interf->uuid;
4254 opr_Assert(!host->z.interface);
4255 host->z.interface = interface;
4257 h_AddHostToUuidHashTable_r(&interface->uuid, host);
4259 if (GetLogLevel() >= 125) {
4260 afsUUID_to_string(&interface->uuid, uuidstr, 127);
4262 ViceLog(125, ("--- uuid %s\n", uuidstr));
4263 for (i = 0; i < host->z.interface->numberOfInterfaces; i++) {
4264 ViceLog(125, ("--- alt address %s:%d\n",
4265 afs_inet_ntoa_r(host->z.interface->interface[i].addr, hoststr),
4266 ntohs(host->z.interface->interface[i].port)));
4273 /* deleted a HashChain structure for this address and host */
4274 /* returns 1 on success */
4276 h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port,
4280 struct h_AddrHashChain **hp, *th;
4282 if (addr == 0 && port == 0)
4285 for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);
4287 opr_Assert(th->hostPtr);
4288 if (th->hostPtr == host && th->addr == addr && th->port == port) {
4289 ViceLog(125, ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d)\n",
4290 host, afs_inet_ntoa_r(host->z.host, hoststr),
4291 ntohs(host->z.port)));
4298 ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) not found\n",
4299 host, afs_inet_ntoa_r(host->z.host, hoststr),
4300 ntohs(host->z.port)));
4306 ** prints out all alternate interface address for the host. The 'level'
4307 ** parameter indicates what level of debugging sets this output
4310 printInterfaceAddr(struct host *host, int level)
4315 if (host->z.interface) {
4316 /* check alternate addresses */
4317 number = host->z.interface->numberOfInterfaces;
4319 ViceLog(level, ("no-addresses "));
4321 for (i = 0; i < number; i++)
4322 ViceLog(level, ("%s:%d ",
4323 afs_inet_ntoa_r(host->z.interface->interface[i].addr,
4325 ntohs(host->z.interface->interface[i].port)));
4328 ViceLog(level, ("\n"));