2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
9 * Portions Copyright (c) 2006 Sine Nomine Associates
12 #include <afsconfig.h>
13 #include <afs/param.h>
27 #include <netinet/in.h>
32 #include <afs/assert.h>
35 #include <afs/afsint.h>
36 #include <afs/rxgen_consts.h>
38 #include <afs/errors.h>
39 #include <afs/ihandle.h>
40 #include <afs/vnode.h>
41 #include <afs/volume.h>
42 #ifdef AFS_ATHENA_STDENV
46 #include <afs/ptclient.h>
47 #include <afs/ptuser.h>
48 #include <afs/prs_fs.h>
50 #include <afs/afsutil.h>
51 #include <afs/com_err.h>
53 #include <afs/cellconfig.h>
55 #include "viced_prototypes.h"
59 #ifdef AFS_DEMAND_ATTACH_FS
60 #include "../util/afsutil_prototypes.h"
61 #include "../tviced/serialize_state.h"
62 #endif /* AFS_DEMAND_ATTACH_FS */
64 #ifdef AFS_PTHREAD_ENV
65 pthread_mutex_t host_glock_mutex;
66 #endif /* AFS_PTHREAD_ENV */
69 extern int CurrentConnections;
71 extern int AnonymousID;
72 extern prlist AnonCPS;
74 extern struct afsconf_dir *confDir; /* config dir object */
75 extern int lwps; /* the max number of server threads */
76 extern afsUUID FS_HostUUID;
79 int CEs = 0; /* active clients */
80 int CEBlocks = 0; /* number of blocks of CEs */
81 struct client *CEFree = 0; /* first free client */
82 struct host *hostList = 0; /* linked list of all hosts */
83 int hostCount = 0; /* number of hosts in hostList */
87 static struct rx_securityClass *sc = NULL;
89 static void h_SetupCallbackConn_r(struct host * host);
91 #define CESPERBLOCK 73
92 struct CEBlock { /* block of CESPERBLOCK file entries */
93 struct client entry[CESPERBLOCK];
96 static void h_TossStuff_r(register struct host *host);
99 * Make sure the subnet macros have been defined.
102 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
105 #ifndef IN_CLASSA_SUBNET
106 #define IN_CLASSA_SUBNET 0xffff0000
110 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
113 #ifndef IN_CLASSB_SUBNET
114 #define IN_CLASSB_SUBNET 0xffffff00
118 /* get a new block of CEs and chain it on CEFree */
122 register struct CEBlock *block;
125 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
127 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
128 ShutDownAndCore(PANIC);
131 for (i = 0; i < (CESPERBLOCK - 1); i++) {
132 Lock_Init(&block->entry[i].lock);
133 block->entry[i].next = &(block->entry[i + 1]);
135 block->entry[CESPERBLOCK - 1].next = 0;
136 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
137 CEFree = (struct client *)block;
143 /* get the next available CE */
144 static struct client *
147 register struct client *entry;
152 ViceLog(0, ("CEFree NULL in GetCE\n"));
153 ShutDownAndCore(PANIC);
157 CEFree = entry->next;
159 memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
165 /* return an entry to the free list */
167 FreeCE(register struct client *entry)
169 entry->VenusEpoch = 0;
171 entry->next = CEFree;
178 * The HTs and HTBlocks variables were formerly static, but they are
179 * now referenced elsewhere in the FileServer.
181 int HTs = 0; /* active file entries */
182 int HTBlocks = 0; /* number of blocks of HTs */
183 static struct host *HTFree = 0; /* first free file entry */
186 * Hash tables of host pointers. We need two tables, one
187 * to map IP addresses onto host pointers, and another
188 * to map host UUIDs onto host pointers.
190 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
191 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
192 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
193 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
195 struct HTBlock { /* block of HTSPERBLOCK file entries */
196 struct host entry[h_HTSPERBLOCK];
200 /* get a new block of HTs and chain it on HTFree */
204 register struct HTBlock *block;
206 static int index = 0;
208 if (HTBlocks == h_MAXHOSTTABLES) {
209 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
210 ShutDownAndCore(PANIC);
213 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
215 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
216 ShutDownAndCore(PANIC);
218 #ifdef AFS_PTHREAD_ENV
219 for (i = 0; i < (h_HTSPERBLOCK); i++)
220 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
221 #endif /* AFS_PTHREAD_ENV */
222 for (i = 0; i < (h_HTSPERBLOCK); i++)
223 Lock_Init(&block->entry[i].lock);
224 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
225 block->entry[i].next = &(block->entry[i + 1]);
226 for (i = 0; i < (h_HTSPERBLOCK); i++)
227 block->entry[i].index = index++;
228 block->entry[h_HTSPERBLOCK - 1].next = 0;
229 HTFree = (struct host *)block;
230 hosttableptrs[HTBlocks++] = block->entry;
235 /* get the next available HT */
239 register struct host *entry;
243 assert(HTFree != NULL);
245 HTFree = entry->next;
247 memset((char *)entry, 0, HOST_TO_ZERO(entry));
253 /* return an entry to the free list */
255 FreeHT(register struct host *entry)
257 entry->next = HTFree;
264 hpr_Initialize(struct ubik_client **uclient)
267 struct rx_connection *serverconns[MAXSERVERS];
268 struct rx_securityClass *sc[3];
269 struct afsconf_dir *tdir;
270 char tconfDir[100] = "";
272 struct ktc_token ttoken;
274 struct afsconf_cell info;
278 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
280 ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
284 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
286 ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]", code));
291 code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
293 ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s", cellstr, confDir, AFSDIR_CELLSERVDB_FILE));
300 ViceLog(0, ("hpr_Initialize: Could not initialize rx."));
309 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
310 * to force use of the KeyFile. secLevel == 0 implies -noauth was
312 if ((afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
313 code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
315 ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s (so trying noauth)", code, afs_error_message(code)));
317 scIndex = 0; /* use noauth */
319 /* if there was a problem, an unauthenticated conn is returned */
322 struct ktc_principal sname;
323 strcpy(sname.cell, info.name);
324 sname.instance[0] = 0;
325 strcpy(sname.name, "afs");
326 code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
330 if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
331 /* this is a kerberos ticket, set scIndex accordingly */
334 ViceLog(0, ("hpr_Initialize: funny kvno (%d) in ticket, proceeding", ttoken.kvno));
338 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
339 ttoken.kvno, ttoken.ticketLen,
343 if ((scIndex == 0) && (sc[0] == 0))
344 sc[0] = rxnull_NewClientSecurityObject();
346 ViceLog(0, ("hpr_Initialize: Could not get afs tokens, running unauthenticated. [%d]", code));
348 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
349 for (i = 0; i < info.numServers; i++) {
351 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
352 info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
356 code = ubik_ClientInit(serverconns, uclient);
358 ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
361 code = rxs_Release(sc[scIndex]);
366 hpr_End(struct ubik_client *uclient)
371 code = ubik_ClientDestroy(uclient);
377 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
379 #ifdef AFS_PTHREAD_ENV
380 register afs_int32 code;
382 struct ubik_client *uclient =
383 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
386 code = hpr_Initialize(&uclient);
388 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
394 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
395 if (code != PRSUCCESS)
398 /* do something about this, probably make a new call */
399 /* don't forget there's a hard limit in the interface */
401 "membership list for host id %d exceeds display limit\n",
406 return pr_GetHostCPS(host, CPS);
411 hpr_NameToId(namelist *names, idlist *ids)
413 #ifdef AFS_PTHREAD_ENV
414 register afs_int32 code;
415 register afs_int32 i;
416 struct ubik_client *uclient =
417 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
420 code = hpr_Initialize(&uclient);
422 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
427 for (i = 0; i < names->namelist_len; i++)
428 stolower(names->namelist_val[i]);
429 code = ubik_PR_NameToID(uclient, 0, names, ids);
432 return pr_NameToId(names, ids);
437 hpr_IdToName(idlist *ids, namelist *names)
439 #ifdef AFS_PTHREAD_ENV
440 register afs_int32 code;
441 struct ubik_client *uclient =
442 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
445 code = hpr_Initialize(&uclient);
447 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
452 code = ubik_PR_IDToName(uclient, 0, ids, names);
455 return pr_IdToName(ids, names);
460 hpr_GetCPS(afs_int32 id, prlist *CPS)
462 #ifdef AFS_PTHREAD_ENV
463 register afs_int32 code;
465 struct ubik_client *uclient =
466 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
469 code = hpr_Initialize(&uclient);
471 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
477 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
478 if (code != PRSUCCESS)
481 /* do something about this, probably make a new call */
482 /* don't forget there's a hard limit in the interface */
483 fprintf(stderr, "membership list for id %d exceeds display limit\n",
488 return pr_GetCPS(id, CPS);
492 static short consolePort = 0;
495 h_Release(register struct host *host)
504 * If this thread does not have a hold on this host AND
505 * if other threads also dont have any holds on this host AND
506 * If either the HOSTDELETED or CLIENTDELETED flags are set
510 h_Release_r(register struct host *host)
513 if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
514 if (!h_OtherHolds_r(host)) {
515 /* must avoid masking this until after h_OtherHolds_r runs
516 * but it should be run before h_TossStuff_r */
517 (host)->holds[h_holdSlot()] &= ~h_holdbit();
518 if ((host->hostFlags & HOSTDELETED)
519 || (host->hostFlags & CLIENTDELETED)) {
523 (host)->holds[h_holdSlot()] &= ~h_holdbit();
525 (host)->holds[h_holdSlot()] &= ~h_holdbit();
531 h_OtherHolds_r(register struct host *host)
533 register int i, bit, slot;
536 for (i = 0; i < h_maxSlots; i++) {
537 if (host->holds[i] != ((i == slot) ? bit : 0)) {
545 h_Lock_r(register struct host *host)
555 * returns 1 if already locked
556 * else returns locks and returns 0
560 h_NBLock_r(register struct host *host)
562 struct Lock *hostLock = &host->lock;
567 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
568 hostLock->excl_locked = WRITE_LOCK;
572 LOCK_UNLOCK(hostLock);
581 #if FS_STATS_DETAILED
582 /*------------------------------------------------------------------------
583 * PRIVATE h_AddrInSameNetwork
586 * Given a target IP address and a candidate IP address (both
587 * in host byte order), return a non-zero value (1) if the
588 * candidate address is in a different network from the target
592 * a_targetAddr : Target address.
593 * a_candAddr : Candidate address.
596 * 1 if the candidate address is in the same net as the target,
600 * The target and candidate addresses are both in host byte
601 * order, NOT network byte order, when passed in. We return
602 * our value as a character, since that's the type of field in
603 * the host structure, where this info will be stored.
607 *------------------------------------------------------------------------*/
610 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
611 { /*h_AddrInSameNetwork */
613 afs_uint32 targetNet;
617 * Pull out the network and subnetwork numbers from the target
618 * and candidate addresses. We can short-circuit this whole
619 * affair if the target and candidate addresses are not of the
622 if (IN_CLASSA(a_targetAddr)) {
623 if (!(IN_CLASSA(a_candAddr))) {
626 targetNet = a_targetAddr & IN_CLASSA_NET;
627 candNet = a_candAddr & IN_CLASSA_NET;
628 } else if (IN_CLASSB(a_targetAddr)) {
629 if (!(IN_CLASSB(a_candAddr))) {
632 targetNet = a_targetAddr & IN_CLASSB_NET;
633 candNet = a_candAddr & IN_CLASSB_NET;
634 } /*Class B target */
635 else if (IN_CLASSC(a_targetAddr)) {
636 if (!(IN_CLASSC(a_candAddr))) {
639 targetNet = a_targetAddr & IN_CLASSC_NET;
640 candNet = a_candAddr & IN_CLASSC_NET;
641 } /*Class C target */
643 targetNet = a_targetAddr;
644 candNet = a_candAddr;
645 } /*Class D address */
648 * Now, simply compare the extracted net values for the two addresses
649 * (which at this point are known to be of the same class)
651 if (targetNet == candNet)
656 } /*h_AddrInSameNetwork */
657 #endif /* FS_STATS_DETAILED */
660 /* Assumptions: called with held host */
662 h_gethostcps_r(register struct host *host, register afs_int32 now)
667 /* wait if somebody else is already doing the getCPS call */
668 while (host->hostFlags & HCPS_INPROGRESS) {
669 slept = 1; /* I did sleep */
670 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
671 #ifdef AFS_PTHREAD_ENV
672 pthread_cond_wait(&host->cond, &host_glock_mutex);
673 #else /* AFS_PTHREAD_ENV */
674 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
675 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
676 #endif /* AFS_PTHREAD_ENV */
680 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
681 if (host->hcps.prlist_val)
682 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
683 host->hcps.prlist_val = NULL;
684 host->hcps.prlist_len = 0;
685 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
688 code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
693 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
694 * and reevaluates things if no sync server or quorum is found we could still end up
695 * with one of these errors. In such case we would like to reevaluate the rpc call to
696 * find if there's cps for this guy. We treat other errors (except network failures
697 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
698 * we could like to deal this problem the other way around (i.e. if code == NOCPS
699 * ignore else retry next time) but the problem is that there're other errors (i.e.
700 * EPERM) for which we don't want to retry and we don't know the whole code list!
702 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
704 * We would have preferred to use a while loop and try again since ops in protected
705 * acls for this host will fail now but they'll be reevaluated on any subsequent
706 * call. The attempt to wait for a quorum/sync site or network error won't work
707 * since this problems really should only occurs during a complete fileserver
708 * restart. Since the fileserver will start before the ptservers (and thus before
709 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
711 host->hcpsfailed = 1;
713 ("Warning: GetHostCPS failed (%d) for %x (%s:%d); will retry\n",
714 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
716 host->hcpsfailed = 0;
718 ("gethost: GetHostCPS failed (%d) for %x (%s:%d); ignored\n",
719 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
721 if (host->hcps.prlist_val)
722 free(host->hcps.prlist_val);
723 host->hcps.prlist_val = NULL;
724 host->hcps.prlist_len = 0; /* Make sure it's zero */
726 host->hcpsfailed = 0;
728 host->hostFlags &= ~HCPS_INPROGRESS;
729 /* signal all who are waiting */
730 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
731 host->hostFlags &= ~HCPS_WAITING;
732 #ifdef AFS_PTHREAD_ENV
733 assert(pthread_cond_broadcast(&host->cond) == 0);
734 #else /* AFS_PTHREAD_ENV */
735 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
736 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
737 #endif /* AFS_PTHREAD_ENV */
741 /* args in net byte order */
743 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
749 h_Lookup_r(hostaddr, hport, &held, &host);
751 host->hcpsfailed = 1;
761 * Allocate a host. It will be identified by the peer (ip,port) info in the
762 * rx connection provided. The host is returned held and locked
764 #define DEF_ROPCONS 2115
767 h_Alloc_r(register struct rx_connection *r_con)
769 struct servent *serverentry;
772 #if FS_STATS_DETAILED
773 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
774 #endif /* FS_STATS_DETAILED */
778 host->host = rxr_HostOf(r_con);
779 host->port = rxr_PortOf(r_con);
781 h_AddHostToAddrHashTable_r(host->host, host->port, host);
783 if (consolePort == 0) { /* find the portal number for console */
784 #if defined(AFS_OSF_ENV)
785 serverentry = getservbyname("ropcons", "");
787 serverentry = getservbyname("ropcons", 0);
790 consolePort = serverentry->s_port;
792 consolePort = htons(DEF_ROPCONS); /* Use a default */
794 if (host->port == consolePort)
796 /* Make a callback channel even for the console, on the off chance that it
797 * makes a request that causes a break call back. It shouldn't. */
798 h_SetupCallbackConn_r(host);
799 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
801 host->hcps.prlist_val = NULL;
802 host->hcps.prlist_len = 0;
803 host->interface = NULL;
805 host->hcpsfailed = 0; /* save cycles */
806 h_gethostcps(host); /* do this under host hold/lock */
808 host->FirstClient = NULL;
811 h_InsertList_r(host); /* update global host List */
812 #if FS_STATS_DETAILED
814 * Compare the new host's IP address (in host byte order) with ours
815 * (the File Server's), remembering if they are in the same network.
817 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
818 host->InSameNetwork =
819 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
820 #endif /* FS_STATS_DETAILED */
827 /* Make a callback channel even for the console, on the off chance that it
828 * makes a request that causes a break call back. It shouldn't. */
830 h_SetupCallbackConn_r(struct host * host)
833 sc = rxnull_NewClientSecurityObject();
834 host->callback_rxcon =
835 rx_NewConnection(host->host, host->port, 1, sc, 0);
836 rx_SetConnDeadTime(host->callback_rxcon, 50);
837 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
840 /* Lookup a host given an IP address and UDP port number. */
841 /* hostaddr and hport are in network order */
842 /* Note: host should be released by caller if 0 == *heldp and non-null */
843 /* hostaddr and hport are in network order */
845 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp, struct host **hostp)
848 struct host *host = NULL;
849 struct h_AddrHashChain *chain;
850 int index = h_HashIndex(haddr);
851 extern int hostaclRefresh;
854 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
855 host = chain->hostPtr;
857 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
858 && chain->port == hport) {
859 if ((host->hostFlags & HWHO_INPROGRESS) &&
860 h_threadquota(host->lock.num_waiting)) {
864 *heldp = h_Held_r(host);
868 if (host->hostFlags & HOSTDELETED) {
875 now = FT_ApproxTime(); /* always evaluate "now" */
876 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
878 * Every hostaclRefresh period (def 2 hrs) get the new
879 * membership list for the host. Note this could be the
880 * first time that the host is added to a group. Also
881 * here we also retry on previous legitimate hcps failures.
883 * If we get here we still have a host hold.
885 h_gethostcps_r(host, now);
895 /* Lookup a host given its UUID. */
897 h_LookupUuid_r(afsUUID * uuidp)
899 struct host *host = 0;
900 struct h_UuidHashChain *chain;
901 int index = h_UuidHashIndex(uuidp);
903 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
904 host = chain->hostPtr;
906 if (!(host->hostFlags & HOSTDELETED) && host->interface
907 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
918 * h_Hold_r: Establish a hold by the current LWP on this host--the host
919 * or its clients will not be physically deleted until all holds have
921 * NOTE: h_Hold_r is a macro defined in host.h.
924 /* h_TossStuff_r: Toss anything in the host structure (the host or
925 * clients marked for deletion. Called from h_Release_r ONLY.
926 * To be called, there must be no holds, and either host->deleted
927 * or host->clientDeleted must be set.
930 h_TossStuff_r(register struct host *host)
932 register struct client **cp, *client;
935 /* if somebody still has this host held */
936 for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
940 /* if somebody still has this host locked */
941 if (h_NBLock_r(host) != 0) {
944 ("Warning: h_TossStuff_r failed; Host %x (%s:%d) was locked.\n",
945 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
951 /* ASSUMPTION: rxi_FreeConnection() does not yield */
952 for (cp = &host->FirstClient; (client = *cp);) {
953 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
955 ObtainWriteLockNoBlock(&client->lock, code);
959 ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x was locked.\n",
960 host, afs_inet_ntoa_r(host->host, hoststr),
961 ntohs(host->port), client));
965 if (client->refCount) {
968 ("Warning: h_TossStuff_r failed: Host %x (%s:%d) client %x refcount %d.\n",
969 host, afs_inet_ntoa_r(host->host, hoststr),
970 ntohs(host->port), client, client->refCount));
971 /* This is the same thing we do if the host is locked */
972 ReleaseWriteLock(&client->lock);
975 client->CPS.prlist_len = 0;
976 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
977 free(client->CPS.prlist_val);
978 client->CPS.prlist_val = NULL;
979 CurrentConnections--;
981 ReleaseWriteLock(&client->lock);
987 /* We've just cleaned out all the deleted clients; clear the flag */
988 host->hostFlags &= ~CLIENTDELETED;
990 if (host->hostFlags & HOSTDELETED) {
991 register struct h_AddrHashChain **ahp, *ath;
992 register struct rx_connection *rxconn;
994 struct AddrPort hostAddrPort;
997 if (host->Console & 1)
999 if ((rxconn = host->callback_rxcon)) {
1000 host->callback_rxcon = (struct rx_connection *)0;
1001 rx_DestroyConnection(rxconn);
1003 if (host->hcps.prlist_val)
1004 free(host->hcps.prlist_val);
1005 host->hcps.prlist_val = NULL;
1006 host->hcps.prlist_len = 0;
1007 DeleteAllCallBacks_r(host, 1);
1008 host->hostFlags &= ~RESETDONE; /* just to be safe */
1010 /* if alternate addresses do not exist */
1011 if (!(host->interface)) {
1012 for (ahp = &hostAddrHashTable[h_HashIndex(host->host)]; (ath = *ahp);
1014 assert(ath->hostPtr);
1015 if (ath->hostPtr == host) {
1022 register struct h_UuidHashChain **uhp, *uth;
1023 /* delete the hash entry for the UUID */
1024 uuidp = &host->interface->uuid;
1025 for (uhp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (uth = *uhp);
1027 assert(uth->hostPtr);
1028 if (uth->hostPtr == host) {
1034 /* delete the hash entry for each alternate addresses */
1035 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
1036 hostAddrPort = host->interface->interface[i];
1038 if (!hostAddrPort.valid)
1041 for (ahp = &hostAddrHashTable[h_HashIndex(hostAddrPort.addr)]; (ath = *ahp);
1043 assert(ath->hostPtr);
1044 if (ath->hostPtr == host) {
1051 free(host->interface);
1052 host->interface = NULL;
1053 } /* if alternate address exists */
1055 h_DeleteList_r(host); /* remove host from global host List */
1058 } /*h_TossStuff_r */
1061 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
1062 * system at the start of the enumeration (perhaps more). Hosts may be deleted
1063 * (have delete flag set); ditto for clients. (*proc) is always called with
1064 * host h_held(). The hold state of the host with respect to this lwp is passed
1065 * to (*proc) as the param held. The proc should return 0 if the host should be
1066 * released, 1 if it should be held after enumeration.
1069 h_Enumerate(int (*proc) (), char *param)
1071 register struct host *host, **list;
1073 register int i, count;
1076 if (hostCount == 0) {
1080 list = (struct host **)malloc(hostCount * sizeof(struct host *));
1082 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
1085 held = (int *)malloc(hostCount * sizeof(int));
1087 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
1090 for (count = 0, host = hostList; host; host = host->next, count++) {
1092 if (!(held[count] = h_Held_r(host)))
1095 if (count != hostCount) {
1096 ViceLog(0, ("h_Enumerate found %d of %d hosts\n", count, hostCount));
1098 assert(count <= hostCount);
1100 for (i = 0; i < count; i++) {
1101 held[i] = (*proc) (list[i], held[i], param);
1102 if (!H_ENUMERATE_ISSET_HELD(held[i]))
1103 h_Release(list[i]); /* this might free up the host */
1104 /* bail out of the enumeration early */
1105 if (H_ENUMERATE_ISSET_BAIL(held[i]))
1112 /* h_Enumerate_r (revised):
1113 * Calls (*proc)(host, held, param) for each host in hostList, starting
1115 * Hosts may be deleted (have delete flag set); ditto for clients.
1116 * (*proc) is always called with
1117 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
1118 * host with respect to this lwp is passed to (*proc) as the param held.
1119 * The proc should return 0 if the host should be released, 1 if it should
1120 * be held after enumeration.
1123 h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
1125 register struct host *host, *next;
1126 register int held, nheld;
1128 if (hostCount == 0) {
1131 if (enumstart && !(held = h_Held_r(enumstart)))
1132 h_Hold_r(enumstart);
1133 for (host = enumstart; host; host = next, held = nheld) {
1135 if (next && !(nheld = h_Held_r(next)) && !H_ENUMERATE_ISSET_BAIL(held))
1137 held = (*proc) (host, held, param);
1138 if (!H_ENUMERATE_ISSET_HELD(held))
1139 h_Release_r(host); /* this might free up the host */
1140 if (H_ENUMERATE_ISSET_BAIL(held)) {
1141 if (!H_ENUMERATE_ISSET_HELD(nheld))
1142 h_Release_r(next); /* this might free up the host */
1146 } /*h_Enumerate_r */
1148 /* inserts a new HashChain structure corresponding to this UUID */
1150 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1153 struct h_UuidHashChain *chain;
1155 /* hash into proper bucket */
1156 index = h_UuidHashIndex(uuid);
1158 /* don't add the same entry multiple times */
1159 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1160 if (host->interface && afs_uuid_equal(&host->interface->uuid, uuid))
1164 /* insert into beginning of list for this bucket */
1165 chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
1167 ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1171 chain->hostPtr = host;
1172 chain->next = hostUuidHashTable[index];
1173 hostUuidHashTable[index] = chain;
1177 /* inserts a new HashChain structure corresponding to this address */
1179 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1182 struct h_AddrHashChain *chain;
1186 /* hash into proper bucket */
1187 index = h_HashIndex(addr);
1189 /* don't add the same entry multiple times */
1190 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1191 if (chain->addr == addr && chain->port == port) {
1192 if (chain->hostPtr == host)
1194 else if (!(host->hostFlags & HOSTDELETED))
1195 ViceLog(125, ("Addr %s:%d assigned to %x and %x.\n",
1196 afs_inet_ntoa_r(addr, hoststr), ntohs(port),
1204 /* insert into beginning of list for this bucket */
1205 chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
1207 ViceLog(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1210 chain->hostPtr = host;
1211 chain->next = hostAddrHashTable[index];
1214 hostAddrHashTable[index] = chain;
1218 * This is called with host locked and held. At this point, the
1219 * hostAddrHashTable should not have entries for the alternate
1220 * interfaces. This function has to insert these entries in the
1221 * hostAddrHashTable.
1223 * All addresses are in network byte order.
1226 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1231 struct Interface *interface;
1232 char hoststr[16], hoststr2[16];
1235 assert(host->interface);
1238 * Make sure this address is on the list of known addresses
1241 number = host->interface->numberOfInterfaces;
1242 for (i = 0, found = 0; i < number && !found; i++) {
1243 if (host->interface->interface[i].addr == addr &&
1244 host->interface->interface[i].port == port) {
1246 host->interface->interface[i].valid = 1;
1250 ViceLog(125, ("addInterfaceAddr : host %s:%d addr %s:%d : found:%d\n",
1251 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1252 afs_inet_ntoa_r(addr, hoststr2), ntohs(port), found));
1255 interface = (struct Interface *)
1256 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1258 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
1261 interface->numberOfInterfaces = number + 1;
1262 interface->uuid = host->interface->uuid;
1263 for (i = 0; i < number; i++)
1264 interface->interface[i] = host->interface->interface[i];
1265 interface->interface[number].addr = addr;
1266 interface->interface[number].port = port;
1267 interface->interface[number].valid = 1;
1268 free(host->interface);
1269 host->interface = interface;
1277 * This is called with host locked and held. At this point, the
1278 * hostAddrHashTable should not be having entries for the alternate
1279 * interfaces. This function has to insert these entries in the
1280 * hostAddrHashTable.
1282 * All addresses are in network byte order.
1285 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1290 struct Interface *interface;
1291 char hoststr[16], hoststr2[16];
1294 assert(host->interface);
1296 ViceLog(125, ("removeInterfaceAddr : host %s:%d addr %s:%d\n",
1297 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1298 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
1301 * Make sure this address is on the list of known addresses
1304 interface = host->interface;
1305 number = host->interface->numberOfInterfaces;
1306 for (i = 0, found = 0; i < number && !found; i++) {
1307 if (interface->interface[i].addr == addr &&
1308 interface->interface[i].port == port) {
1310 interface->interface[i].valid = 0;
1315 for (; i < number; i++) {
1316 interface->interface[i] = interface->interface[i+1];
1318 interface->numberOfInterfaces = number;
1322 * Remove the hash table entry for this address
1324 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1330 * This is called with host locked and held. This function differs
1331 * from removeInterfaceAddr_r in that it is called when the address
1332 * is being removed from the host regardless of whether or not there
1333 * is an interface list for the host. This function will delete the
1334 * host if there are no addresses left on it.
1336 * All addresses are in network byte order.
1339 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1342 char hoststr[16], hoststr2[16];
1344 if (!host->interface) {
1345 if (host->host == addr && host->port == port) {
1347 ("Removing only address for host %x (%s:%d), deleting host.\n",
1348 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1349 host->hostFlags |= HOSTDELETED;
1352 removeInterfaceAddr_r(host, host->host, host->port);
1353 if (host->interface->numberOfInterfaces == 0) {
1355 ("Removed only address for host %x (%s:%d), no alternate interfaces, deleting host.\n",
1356 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1357 host->hostFlags |= HOSTDELETED;
1359 struct rx_connection *rxconn;
1361 rxconn = host->callback_rxcon;
1362 host->callback_rxcon = NULL;
1365 struct client *client;
1367 * If rx_DestroyConnection calls h_FreeConnection we will
1368 * deadlock on the host_glock_mutex. Work around the problem
1369 * by unhooking the client from the connection before
1370 * destroying the connection.
1372 client = rx_GetSpecific(rxconn, rxcon_client_key);
1373 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1374 rx_DestroyConnection(rxconn);
1377 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1378 if (host->interface->interface[i].valid) {
1380 ("Removed address for host %x (%s:%d), new primary interface %s:%d.\n",
1381 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1382 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2),
1383 ntohs(host->interface->interface[i].port)));
1384 host->host = host->interface->interface[i].addr;
1385 host->port = host->interface->interface[i].port;
1386 h_AddHostToAddrHashTable_r(host->host, host->port, host);
1391 if (i == host->interface->numberOfInterfaces) {
1393 ("Removed only address for host %x (%s:%d), no valid alternate interfaces, deleting host.\n",
1394 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1395 host->hostFlags |= HOSTDELETED;
1398 sc = rxnull_NewClientSecurityObject();
1399 host->callback_rxcon =
1400 rx_NewConnection(host->host, host->port, 1, sc, 0);
1401 rx_SetConnDeadTime(host->callback_rxcon, 50);
1402 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
1410 h_threadquota(int waiting)
1415 } else if (lwps > 32) {
1418 } else if (lwps > 16) {
1428 /* Host is returned held */
1430 h_GetHost_r(struct rx_connection *tcon)
1433 struct host *oldHost;
1436 struct interfaceAddr interf;
1437 int interfValid = 0;
1438 struct Identity *identP = NULL;
1441 char hoststr[16], hoststr2[16];
1443 struct rx_connection *cb_conn = NULL;
1445 caps.Capabilities_val = NULL;
1447 haddr = rxr_HostOf(tcon);
1448 hport = rxr_PortOf(tcon);
1450 if (caps.Capabilities_val)
1451 free(caps.Capabilities_val);
1452 caps.Capabilities_val = NULL;
1453 caps.Capabilities_len = 0;
1456 if (h_Lookup_r(haddr, hport, &held, &host))
1458 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1459 if (host && !identP && !(host->Console & 1)) {
1460 /* This is a new connection, and we already have a host
1461 * structure for this address. Verify that the identity
1462 * of the caller matches the identity in the host structure.
1464 if ((host->hostFlags & HWHO_INPROGRESS) &&
1465 h_threadquota(host->lock.num_waiting)) {
1471 if (!(host->hostFlags & ALTADDR)) {
1472 /* Another thread is doing initialization */
1477 ("Host %x (%s:%d) starting h_Lookup again\n",
1478 host, afs_inet_ntoa_r(host->host, hoststr),
1479 ntohs(host->port)));
1482 host->hostFlags |= HWHO_INPROGRESS;
1483 host->hostFlags &= ~ALTADDR;
1484 cb_conn = host->callback_rxcon;
1485 rx_GetConnection(cb_conn);
1488 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1489 if (code == RXGEN_OPCODE)
1490 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1491 rx_PutConnection(cb_conn);
1494 if ((code == RXGEN_OPCODE) ||
1495 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1496 identP = (struct Identity *)malloc(sizeof(struct Identity));
1498 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1502 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1503 /* The host on this connection was unable to respond to
1504 * the WhoAreYou. We will treat this as a new connection
1505 * from the existing host. The worst that can happen is
1506 * that we maintain some extra callback state information */
1507 if (host->interface) {
1509 ("Host %x (%s:%d) used to support WhoAreYou, deleting.\n",
1511 afs_inet_ntoa_r(host->host, hoststr),
1512 ntohs(host->port)));
1513 host->hostFlags |= HOSTDELETED;
1514 host->hostFlags &= ~HWHO_INPROGRESS;
1521 } else if (code == 0) {
1523 identP = (struct Identity *)malloc(sizeof(struct Identity));
1525 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1529 identP->uuid = interf.uuid;
1530 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1531 /* Check whether the UUID on this connection matches
1532 * the UUID in the host structure. If they don't match
1533 * then this is not the same host as before. */
1534 if (!host->interface
1535 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1537 ("Uuid doesn't match host %x (%s:%d).\n",
1538 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1540 removeAddress_r(host, host->host, host->port);
1541 host->hostFlags &= ~HWHO_INPROGRESS;
1550 ("CB: WhoAreYou failed for host %x (%s:%d), error %d\n",
1551 host, afs_inet_ntoa_r(host->host, hoststr),
1552 ntohs(host->port), code));
1553 host->hostFlags |= VENUSDOWN;
1555 if (caps.Capabilities_val
1556 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1557 host->hostFlags |= HERRORTRANS;
1559 host->hostFlags &= ~(HERRORTRANS);
1560 host->hostFlags |= ALTADDR;
1561 host->hostFlags &= ~HWHO_INPROGRESS;
1564 if (!(host->hostFlags & ALTADDR)) {
1565 /* another thread is doing the initialisation */
1567 ("Host %x (%s:%d) waiting for host-init to complete\n",
1568 host, afs_inet_ntoa_r(host->host, hoststr),
1569 ntohs(host->port)));
1575 ("Host %x (%s:%d) starting h_Lookup again\n",
1576 host, afs_inet_ntoa_r(host->host, hoststr),
1577 ntohs(host->port)));
1580 /* We need to check whether the identity in the host structure
1581 * matches the identity on the connection. If they don't match
1582 * then treat this a new host. */
1583 if (!(host->Console & 1)
1584 && ((!identP->valid && host->interface)
1585 || (identP->valid && !host->interface)
1587 && !afs_uuid_equal(&identP->uuid,
1588 &host->interface->uuid)))) {
1589 char uuid1[128], uuid2[128];
1591 afsUUID_to_string(&identP->uuid, uuid1, 127);
1592 if (host->interface)
1593 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1595 ("CB: new identity for host %x (%s:%d), deleting(%x %x %s %s)\n",
1596 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1597 identP->valid, host->interface,
1598 identP->valid ? uuid1 : "no_uuid",
1599 host->interface ? uuid2 : "no_uuid"));
1601 /* The host in the cache is not the host for this connection */
1603 host->hostFlags |= HOSTDELETED;
1610 host = h_Alloc_r(tcon); /* returned held and locked */
1611 h_gethostcps_r(host, FT_ApproxTime());
1612 if (!(host->Console & 1)) {
1614 cb_conn = host->callback_rxcon;
1615 rx_GetConnection(cb_conn);
1616 host->hostFlags |= HWHO_INPROGRESS;
1619 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1620 if (code == RXGEN_OPCODE)
1621 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1622 rx_PutConnection(cb_conn);
1625 if ((code == RXGEN_OPCODE) ||
1626 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1629 (struct Identity *)malloc(sizeof(struct Identity));
1634 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1639 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1641 ("Host %x (%s:%d) does not support WhoAreYou.\n",
1642 host, afs_inet_ntoa_r(host->host, hoststr),
1643 ntohs(host->port)));
1645 } else if (code == 0) {
1648 (struct Identity *)malloc(sizeof(struct Identity));
1653 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1658 identP->uuid = interf.uuid;
1660 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1662 ("WhoAreYou success on host %x (%s:%d)\n",
1663 host, afs_inet_ntoa_r(host->host, hoststr),
1664 ntohs(host->port)));
1666 if (code == 0 && !identP->valid) {
1667 cb_conn = host->callback_rxcon;
1668 rx_GetConnection(cb_conn);
1670 code = RXAFSCB_InitCallBackState(cb_conn);
1671 rx_PutConnection(cb_conn);
1674 } else if (code == 0) {
1675 oldHost = h_LookupUuid_r(&identP->uuid);
1679 if (!h_Held_r(oldHost))
1682 oldHost->hostFlags |= HWHO_INPROGRESS;
1684 if (oldHost->interface) {
1686 afsUUID uuid = oldHost->interface->uuid;
1687 cb_conn = oldHost->callback_rxcon;
1688 rx_GetConnection(cb_conn);
1689 rx_SetConnDeadTime(cb_conn, 2);
1690 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1692 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1694 rx_SetConnDeadTime(cb_conn, 50);
1695 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1696 rx_PutConnection(cb_conn);
1699 /* The primary address is either not responding or
1700 * is not the client we are looking for.
1701 * MultiProbeAlternateAddress_r() will remove the
1702 * alternate interfaces that do not have the same
1704 ViceLog(0,("CB: ProbeUuid for host %x (%s:%d) failed %d\n",
1706 afs_inet_ntoa_r(oldHost->host, hoststr),
1707 ntohs(oldHost->port),code2));
1708 MultiProbeAlternateAddress_r(oldHost);
1715 /* This is a new address for an existing host. Update
1716 * the list of interfaces for the existing host and
1717 * delete the host structure we just allocated. */
1719 /* prevent warnings while manipulating interface lists */
1720 host->hostFlags |= HOSTDELETED;
1722 if (oldHost->host != haddr || oldHost->port != hport) {
1723 struct rx_connection *rxconn;
1726 ("CB: Host %x (%s:%d) has new addr %s:%d\n",
1728 afs_inet_ntoa_r(oldHost->host, hoststr2),
1729 ntohs(oldHost->port),
1730 afs_inet_ntoa_r(haddr, hoststr),
1732 if (probefail || oldHost->host == haddr) {
1733 /* The probe failed which means that the old address is
1734 * either unreachable or is not the same host we were just
1735 * contacted by. We will also remove addresses if only
1736 * the port has changed because that indicates the client
1739 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
1742 struct Interface *interface = oldHost->interface;
1743 int number = oldHost->interface->numberOfInterfaces;
1744 for (i = 0, found = 0; i < number; i++) {
1745 if (interface->interface[i].addr == haddr &&
1746 interface->interface[i].port != hport) {
1752 /* We have just been contacted by a client that has been
1753 * seen from behind a NAT and at least one other address.
1755 removeInterfaceAddr_r(oldHost, haddr, interface->interface[i].port);
1758 addInterfaceAddr_r(oldHost, haddr, hport);
1759 oldHost->host = haddr;
1760 oldHost->port = hport;
1761 rxconn = oldHost->callback_rxcon;
1762 oldHost->callback_rxcon = host->callback_rxcon;
1763 host->callback_rxcon = NULL;
1766 struct client *client;
1768 * If rx_DestroyConnection calls h_FreeConnection we will
1769 * deadlock on the host_glock_mutex. Work around the problem
1770 * by unhooking the client from the connection before
1771 * destroying the connection.
1773 client = rx_GetSpecific(rxconn, rxcon_client_key);
1774 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1775 rx_DestroyConnection(rxconn);
1778 host->hostFlags &= ~HWHO_INPROGRESS;
1780 /* release host because it was allocated by h_Alloc_r */
1783 /* the new host is held and locked */
1785 /* This really is a new host */
1786 h_AddHostToUuidHashTable_r(&identP->uuid, host);
1787 cb_conn = host->callback_rxcon;
1788 rx_GetConnection(cb_conn);
1791 RXAFSCB_InitCallBackState3(cb_conn,
1793 rx_PutConnection(cb_conn);
1798 ("InitCallBackState3 success on host %x (%s:%d)\n",
1799 host, afs_inet_ntoa_r(host->host, hoststr),
1800 ntohs(host->port)));
1801 assert(interfValid == 1);
1802 initInterfaceAddr_r(host, &interf);
1808 ("CB: RCallBackConnectBack failed for %x (%s:%d)\n",
1809 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1810 host->hostFlags |= VENUSDOWN;
1813 ("CB: RCallBackConnectBack succeeded for %x (%s:%d)\n",
1814 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1815 host->hostFlags |= RESETDONE;
1818 if (caps.Capabilities_val
1819 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1820 host->hostFlags |= HERRORTRANS;
1822 host->hostFlags &= ~(HERRORTRANS);
1823 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1824 host->hostFlags &= ~HWHO_INPROGRESS;
1827 if (caps.Capabilities_val)
1828 free(caps.Capabilities_val);
1829 caps.Capabilities_val = NULL;
1830 caps.Capabilities_len = 0;
1836 static char localcellname[PR_MAXNAMELEN + 1];
1837 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1838 int num_lrealms = -1;
1844 memset(&nulluuid, 0, sizeof(afsUUID));
1845 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1846 if (num_lrealms == -1) {
1848 for (i=0; i<AFS_NUM_LREALMS; i++) {
1849 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1855 ("afs_krb_get_lrealm failed, using %s.\n",
1857 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1863 /* initialize the rest of the local realms to nullstring for debugging */
1864 for (; i<AFS_NUM_LREALMS; i++)
1865 local_realms[i][0] = '\0';
1867 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1868 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1869 #ifdef AFS_PTHREAD_ENV
1870 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1871 #endif /* AFS_PTHREAD_ENV */
1875 MapName_r(char *aname, char *acell, afs_int32 * aval)
1880 afs_int32 anamelen, cnamelen;
1884 anamelen = strlen(aname);
1885 if (anamelen >= PR_MAXNAMELEN)
1886 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1888 lnames.namelist_len = 1;
1889 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1890 lids.idlist_len = 0;
1891 lids.idlist_val = NULL;
1893 cnamelen = strlen(acell);
1895 if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
1897 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1898 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1899 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1901 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1903 *aval = AnonymousID;
1906 foreign = 1; /* attempt cross-cell authentication */
1907 tname = (char *)malloc(PR_MAXNAMELEN);
1909 ViceLog(0, ("Failed malloc in MapName_r\n"));
1912 strcpy(tname, aname);
1913 tname[anamelen] = '@';
1914 strcpy(tname + anamelen + 1, acell);
1915 lnames.namelist_val = (prname *) tname;
1920 code = hpr_NameToId(&lnames, &lids);
1923 if (lids.idlist_val) {
1924 *aval = lids.idlist_val[0];
1925 if (*aval == AnonymousID) {
1927 ("MapName: NameToId on %s returns anonymousID\n",
1928 lnames.namelist_val));
1930 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1933 ("MapName: NameToId on '%s' is unknown\n",
1934 lnames.namelist_val));
1940 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1948 /* NOTE: this returns the client with a Write lock and a refCount */
1950 h_ID2Client(afs_int32 vid)
1952 register struct client *client;
1953 register struct host *host;
1956 for (host = hostList; host; host = host->next) {
1957 if (host->hostFlags & HOSTDELETED)
1959 for (client = host->FirstClient; client; client = client->next) {
1960 if (!client->deleted && client->ViceId == vid) {
1963 ObtainWriteLock(&client->lock);
1974 * Called by the server main loop. Returns a h_Held client, which must be
1975 * released later the main loop. Allocates a client if the matching one
1976 * isn't around. The client is returned with its reference count incremented
1977 * by one. The caller must call h_ReleaseClient_r when finished with
1980 * the client->host is returned held. h_ReleaseClient_r does not release
1981 * the hold on client->host.
1984 h_FindClient_r(struct rx_connection *tcon)
1986 register struct client *client;
1987 register struct host *host;
1988 struct client *oldClient;
1993 #if (64-MAXKTCNAMELEN)
1994 ticket name length != 64
1998 char uname[PR_MAXNAMELEN];
1999 char tcell[MAXKTCREALMLEN];
2003 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2004 if (client && client->sid == rxr_CidOf(tcon)
2005 && client->VenusEpoch == rxr_GetEpoch(tcon)) {
2007 h_Hold_r(client->host);
2008 if (!client->deleted && client->prfail != 2) {
2009 /* Could add shared lock on client here */
2010 /* note that we don't have to lock entry in this path to
2011 * ensure CPS is initialized, since we don't call rx_SetSpecific
2012 * until initialization is done, and we only get here if
2013 * rx_GetSpecific located the client structure.
2018 ObtainWriteLock(&client->lock); /* released at end */
2024 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
2026 ("FindClient: authenticating connection: authClass=%d\n",
2028 if (authClass == 1) {
2029 /* A bcrypt tickets, no longer supported */
2030 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2031 viceid = AnonymousID;
2032 expTime = 0x7fffffff;
2033 } else if (authClass == 2) {
2036 /* kerberos ticket */
2037 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
2038 tname, tinst, tcell, &kvno);
2040 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2041 viceid = AnonymousID;
2042 expTime = 0x7fffffff;
2044 int ilen = strlen(tinst);
2046 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2047 tname, tinst, tcell, expTime, kvno));
2048 strncpy(uname, tname, sizeof(uname));
2050 if (strlen(uname) + 1 + ilen >= sizeof(uname))
2053 strcat(uname, tinst);
2055 /* translate the name to a vice id */
2056 code = MapName_r(uname, tcell, &viceid);
2060 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
2063 viceid = AnonymousID;
2064 expTime = 0x7fffffff;
2068 viceid = AnonymousID; /* unknown security class */
2069 expTime = 0x7fffffff;
2072 if (!client) { /* loop */
2073 host = h_GetHost_r(tcon); /* Returns it h_Held */
2079 /* First try to find the client structure */
2080 for (client = host->FirstClient; client; client = client->next) {
2081 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2082 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2085 ObtainWriteLock(&client->lock);
2091 /* Still no client structure - get one */
2094 /* Retry to find the client structure */
2095 for (client = host->FirstClient; client; client = client->next) {
2096 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2097 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2099 goto retryfirstclient;
2104 ObtainWriteLock(&client->lock);
2105 client->refCount = 1;
2106 client->host = host;
2107 #if FS_STATS_DETAILED
2108 client->InSameNetwork = host->InSameNetwork;
2109 #endif /* FS_STATS_DETAILED */
2110 client->ViceId = viceid;
2111 client->expTime = expTime; /* rx only */
2112 client->authClass = authClass; /* rx only */
2113 client->sid = rxr_CidOf(tcon);
2114 client->VenusEpoch = rxr_GetEpoch(tcon);
2115 client->CPS.prlist_val = NULL;
2116 client->CPS.prlist_len = 0;
2120 client->prfail = fail;
2122 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
2123 client->CPS.prlist_len = 0;
2124 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
2125 free(client->CPS.prlist_val);
2126 client->CPS.prlist_val = NULL;
2127 client->ViceId = viceid;
2128 client->expTime = expTime;
2130 if (viceid == ANONYMOUSID) {
2131 client->CPS.prlist_len = AnonCPS.prlist_len;
2132 client->CPS.prlist_val = AnonCPS.prlist_val;
2135 code = hpr_GetCPS(viceid, &client->CPS);
2140 ("pr_GetCPS failed(%d) for user %d, host %x (%s:%d)\n",
2141 code, viceid, client->host,
2142 afs_inet_ntoa_r(client->host->host,hoststr),
2143 ntohs(client->host->port)));
2145 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2146 * all protection servers and reevaluates things if no
2147 * sync server or quorum is found we could still end up
2148 * with one of these errors. In such case we would like to
2149 * reevaluate the rpc call to find if there's cps for this
2150 * guy. We treat other errors (except network failures
2151 * ones - i.e. code < 0) as an indication that there is no
2152 * CPS for this host. Ideally we could like to deal this
2153 * problem the other way around (i.e. if code == NOCPS
2154 * ignore else retry next time) but the problem is that
2155 * there're other errors (i.e. EPERM) for which we don't
2156 * want to retry and we don't know the whole code list!
2158 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2162 /* the disabling of system:administrators is so iffy and has so many
2163 * possible failure modes that we will disable it again */
2164 /* Turn off System:Administrator for safety
2165 * if (AL_IsAMember(SystemId, client->CPS) == 0)
2166 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
2169 /* Now, tcon may already be set to a rock, since we blocked with no host
2170 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2171 * required). So, before setting the RPC's rock, we should disconnect
2172 * the RPC from the other client structure's rock.
2174 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2175 if (oldClient && oldClient != client && oldClient->sid == rxr_CidOf(tcon)
2176 && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
2178 if (!oldClient->deleted) {
2179 /* if we didn't create it, it's not ours to put back */
2181 ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n",
2182 client, client->sid, tcon,
2183 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2184 ntohs(rxr_PortOf(tcon)),
2185 oldClient, oldClient->sid));
2186 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2187 free(client->CPS.prlist_val);
2188 client->CPS.prlist_val = NULL;
2189 client->CPS.prlist_len = 0;
2191 /* We should perhaps check for 0 here */
2193 ReleaseWriteLock(&client->lock);
2198 oldClient->refCount++;
2200 ObtainWriteLock(&oldClient->lock);
2204 ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
2205 oldClient, oldClient->sid, tcon,
2206 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2207 ntohs(rxr_PortOf(tcon)),
2208 client, client->sid));
2209 /* rx_SetSpecific will be done immediately below */
2212 /* Avoid chaining in more than once. */
2215 client->next = host->FirstClient;
2216 host->FirstClient = client;
2218 CurrentConnections++; /* increment number of connections */
2220 rx_SetSpecific(tcon, rxcon_client_key, client);
2221 ReleaseWriteLock(&client->lock);
2225 } /*h_FindClient_r */
2228 h_ReleaseClient_r(struct client *client)
2230 assert(client->refCount > 0);
2237 * Sigh: this one is used to get the client AGAIN within the individual
2238 * server routines. This does not bother h_Holding the host, since
2239 * this is assumed already have been done by the server main loop.
2240 * It does check tokens, since only the server routines can return the
2241 * VICETOKENDEAD error code
2244 GetClient(struct rx_connection *tcon, struct client **cp)
2246 register struct client *client;
2251 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2252 if (client == NULL) {
2254 ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
2255 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2256 ntohs(rxr_PortOf(tcon))));
2260 if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
2262 ("GetClient: tcon %x tcon sid %d client sid %d\n",
2263 tcon, rxr_CidOf(tcon), client->sid));
2267 if (client && client->LastCall > client->expTime && client->expTime) {
2269 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2270 afs_inet_ntoa_r(client->host->host, hoststr),
2271 ntohs(client->host->port), client->expTime));
2273 return VICETOKENDEAD;
2283 PutClient(struct client **cp)
2289 h_ReleaseClient_r(*cp);
2296 /* Client user name for short term use. Note that this is NOT inexpensive */
2298 h_UserName(struct client *client)
2300 static char User[PR_MAXNAMELEN + 1];
2304 lids.idlist_len = 1;
2305 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
2306 if (!lids.idlist_val) {
2307 ViceLog(0, ("Failed malloc in h_UserName\n"));
2310 lnames.namelist_len = 0;
2311 lnames.namelist_val = (prname *) 0;
2312 lids.idlist_val[0] = client->ViceId;
2313 if (hpr_IdToName(&lids, &lnames)) {
2314 /* We need to free id we alloced above! */
2315 free(lids.idlist_val);
2316 return "*UNKNOWN USER NAME*";
2318 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2319 free(lids.idlist_val);
2320 free(lnames.namelist_val);
2329 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2330 CEs, CEBlocks, HTs, HTBlocks));
2336 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
2338 register struct client *client;
2343 time_t LastCall, expTime;
2346 LastCall = host->LastCall;
2347 if (host->hostFlags & HOSTDELETED) {
2351 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2352 "Host %s:%d down = %d, LastCall %s",
2353 afs_inet_ntoa_r(host->host, hoststr),
2354 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2355 afs_ctime(&LastCall, tbuffer,
2357 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2358 for (client = host->FirstClient; client; client = client->next) {
2359 if (!client->deleted) {
2360 expTime = client->expTime;
2361 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2362 " user id=%d, name=%s, sl=%s till %s",
2363 client->ViceId, h_UserName(client),
2365 authClass ? "Authenticated" :
2366 "Not authenticated",
2368 authClass ? afs_ctime(&expTime, tbuffer,
2371 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2372 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2373 client->CPS.prlist_len);
2374 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2375 if (client->CPS.prlist_val) {
2376 for (i = 0; i > client->CPS.prlist_len; i++) {
2377 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2378 client->CPS.prlist_val[i]);
2379 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2382 sprintf(tmpStr, "]\n");
2383 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2389 } /*h_PrintClient */
2394 * Print a list of clients, with last security level and token value seen,
2404 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2408 ("Couldn't create client dump file %s\n",
2409 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2412 now = FT_ApproxTime();
2413 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
2414 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2415 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2416 h_Enumerate(h_PrintClient, (char *)file);
2417 STREAM_REALLYCLOSE(file);
2418 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2425 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
2432 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2433 "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
2434 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
2435 host->cblist, CheckLock(&host->lock), host->LastCall,
2436 host->ActiveCall, (host->hostFlags & VENUSDOWN),
2437 host->hostFlags & HOSTDELETED, host->Console,
2438 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2440 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2441 if (host->hcps.prlist_val)
2442 for (i = 0; i < host->hcps.prlist_len; i++) {
2443 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2444 host->hcps.prlist_val[i]);
2445 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2447 sprintf(tmpStr, "] [");
2448 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2449 if (host->interface)
2450 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2452 sprintf(tmpStr, " %s:%d",
2453 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2454 ntohs(host->interface->interface[i].port));
2455 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2457 sprintf(tmpStr, "] holds: ");
2458 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2460 for (i = 0; i < h_maxSlots; i++) {
2461 sprintf(tmpStr, "%04x", host->holds[i]);
2462 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2464 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
2465 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2477 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2483 ("Couldn't create host dump file %s\n",
2484 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2487 now = FT_ApproxTime();
2488 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
2489 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2490 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2491 h_Enumerate(h_DumpHost, (char *)file);
2492 STREAM_REALLYCLOSE(file);
2493 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2497 #ifdef AFS_DEMAND_ATTACH_FS
2500 * host state serialization
2502 static int h_stateFillHeader(struct host_state_header * hdr);
2503 static int h_stateCheckHeader(struct host_state_header * hdr);
2504 static int h_stateAllocMap(struct fs_dump_state * state);
2505 static int h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state);
2506 static int h_stateRestoreHost(struct fs_dump_state * state);
2507 static int h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state);
2508 static int h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state);
2509 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port);
2510 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2511 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2512 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2515 /* this procedure saves all host state to disk for fast startup */
2517 h_stateSave(struct fs_dump_state * state)
2519 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2522 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2524 /* invalidate host state header */
2525 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2527 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2528 sizeof(struct host_state_header))) {
2533 fs_stateIncEOF(state, sizeof(struct host_state_header));
2535 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2540 h_stateFillHeader(state->h_hdr);
2542 /* write the real header to disk */
2543 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2544 sizeof(struct host_state_header));
2551 * host state serialization
2553 * this procedure restores all host state from a disk for fast startup
2556 h_stateRestore(struct fs_dump_state * state)
2560 /* seek to the right position and read in the host state header */
2561 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2562 sizeof(struct host_state_header))) {
2567 /* check the validity of the header */
2568 if (h_stateCheckHeader(state->h_hdr)) {
2573 records = state->h_hdr->records;
2575 if (h_stateAllocMap(state)) {
2580 /* iterate over records restoring host state */
2581 for (i=0; i < records; i++) {
2582 if (h_stateRestoreHost(state) != 0) {
2593 h_stateRestoreIndices(struct fs_dump_state * state)
2595 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
2600 h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state)
2602 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
2603 return H_ENUMERATE_BAIL(held);
2609 h_stateVerify(struct fs_dump_state * state)
2611 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
2616 h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state)
2621 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
2622 return H_ENUMERATE_BAIL(held);
2626 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
2627 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
2628 h->interface->interface[i].port)) {
2632 if (h_stateVerifyUuidHash(state, h)) {
2635 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port)) {
2639 if (cb_stateVerifyHCBList(state, h)) {
2648 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port)
2650 int ret = 0, found = 0;
2651 struct host *host = NULL;
2652 struct h_AddrHashChain *chain;
2653 int index = h_HashIndex(addr);
2657 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
2658 host = chain->hostPtr;
2660 afs_inet_ntoa_r(addr, tmp);
2661 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
2665 if ((chain->addr == addr) && (chain->port == port)) {
2667 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry points to different host struct (%d, %d)\n",
2668 h->index, host->index));
2669 state->flags.warnings_generated = 1;
2674 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
2675 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2676 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
2684 afs_inet_ntoa_r(addr, tmp);
2685 if (state->mode == FS_STATE_LOAD_MODE) {
2686 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s not found in hash\n", tmp));
2690 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s not found in hash\n", tmp));
2691 state->flags.warnings_generated = 1;
2700 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
2702 int ret = 0, found = 0;
2703 struct host *host = NULL;
2704 struct h_UuidHashChain *chain;
2705 afsUUID * uuidp = &h->interface->uuid;
2706 int index = h_UuidHashIndex(uuidp);
2710 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
2711 host = chain->hostPtr;
2713 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2714 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
2718 if (host->interface &&
2719 afs_uuid_equal(&host->interface->uuid, uuidp)) {
2721 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
2722 h->index, host->index));
2723 state->flags.warnings_generated = 1;
2728 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
2729 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2730 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
2738 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2739 if (state->mode == FS_STATE_LOAD_MODE) {
2740 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
2744 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
2745 state->flags.warnings_generated = 1;
2753 /* create the host state header structure */
2755 h_stateFillHeader(struct host_state_header * hdr)
2757 hdr->stamp.magic = HOST_STATE_MAGIC;
2758 hdr->stamp.version = HOST_STATE_VERSION;
2761 /* check the contents of the host state header structure */
2763 h_stateCheckHeader(struct host_state_header * hdr)
2767 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
2768 ViceLog(0, ("check_host_state_header: invalid state header\n"));
2771 else if (hdr->stamp.version != HOST_STATE_VERSION) {
2772 ViceLog(0, ("check_host_state_header: unknown version number\n"));
2778 /* allocate the host id mapping table */
2780 h_stateAllocMap(struct fs_dump_state * state)
2782 state->h_map.len = state->h_hdr->index_max + 1;
2783 state->h_map.entries = (struct idx_map_entry_t *)
2784 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
2785 return (state->h_map.entries != NULL) ? 0 : 1;
2788 /* function called by h_Enumerate to save a host to disk */
2790 h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state)
2792 int i, if_len=0, hcps_len=0;
2793 struct hostDiskEntry hdsk;
2794 struct host_state_entry_header hdr;
2795 struct Interface * ifp = NULL;
2796 afs_int32 * hcps = NULL;
2797 struct iovec iov[4];
2800 memset(&hdr, 0, sizeof(hdr));
2802 if (state->h_hdr->index_max < host->index) {
2803 state->h_hdr->index_max = host->index;
2806 h_hostToDiskEntry_r(host, &hdsk);
2807 if (host->interface) {
2808 if_len = sizeof(struct Interface) +
2809 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
2810 ifp = (struct Interface *) malloc(if_len);
2811 assert(ifp != NULL);
2812 memcpy(ifp, host->interface, if_len);
2813 hdr.interfaces = host->interface->numberOfInterfaces;
2814 iov[iovcnt].iov_base = (char *) ifp;
2815 iov[iovcnt].iov_len = if_len;
2818 if (host->hcps.prlist_val) {
2819 hdr.hcps = host->hcps.prlist_len;
2820 hcps_len = hdr.hcps * sizeof(afs_int32);
2821 hcps = (afs_int32 *) malloc(hcps_len);
2822 assert(hcps != NULL);
2823 memcpy(hcps, host->hcps.prlist_val, hcps_len);
2824 iov[iovcnt].iov_base = (char *) hcps;
2825 iov[iovcnt].iov_len = hcps_len;
2829 if (hdsk.index > state->h_hdr->index_max)
2830 state->h_hdr->index_max = hdsk.index;
2832 hdr.len = sizeof(struct host_state_entry_header) +
2833 sizeof(struct hostDiskEntry) + if_len + hcps_len;
2834 hdr.magic = HOST_STATE_ENTRY_MAGIC;
2836 iov[0].iov_base = (char *) &hdr;
2837 iov[0].iov_len = sizeof(hdr);
2838 iov[1].iov_base = (char *) &hdsk;
2839 iov[1].iov_len = sizeof(struct hostDiskEntry);
2841 if (fs_stateWriteV(state, iov, iovcnt)) {
2842 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
2846 fs_stateIncEOF(state, hdr.len);
2848 state->h_hdr->records++;
2856 return H_ENUMERATE_BAIL(held);
2861 /* restores a host from disk */
2863 h_stateRestoreHost(struct fs_dump_state * state)
2865 int ifp_len=0, hcps_len=0, bail=0;
2866 struct host_state_entry_header hdr;
2867 struct hostDiskEntry hdsk;
2868 struct host *host = NULL;
2869 struct Interface *ifp = NULL;
2870 afs_int32 * hcps = NULL;
2871 struct iovec iov[3];
2874 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
2875 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
2881 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
2882 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
2888 iov[0].iov_base = (char *) &hdsk;
2889 iov[0].iov_len = sizeof(struct hostDiskEntry);
2891 if (hdr.interfaces) {
2892 ifp_len = sizeof(struct Interface) +
2893 ((hdr.interfaces-1) * sizeof(struct AddrPort));
2894 ifp = (struct Interface *) malloc(ifp_len);
2895 assert(ifp != NULL);
2896 iov[iovcnt].iov_base = (char *) ifp;
2897 iov[iovcnt].iov_len = ifp_len;
2901 hcps_len = hdr.hcps * sizeof(afs_int32);
2902 hcps = (afs_int32 *) malloc(hcps_len);
2903 assert(hcps != NULL);
2904 iov[iovcnt].iov_base = (char *) hcps;
2905 iov[iovcnt].iov_len = hcps_len;
2909 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
2910 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
2915 if (fs_stateReadV(state, iov, iovcnt)) {
2916 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
2921 if (!hdr.hcps && hdsk.hcps_valid) {
2922 /* valid, zero-length host cps ; does this ever happen? */
2923 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
2924 assert(hcps != NULL);
2928 assert(host != NULL);
2931 host->interface = ifp;
2934 host->hcps.prlist_val = hcps;
2935 host->hcps.prlist_len = hdr.hcps;
2938 h_diskEntryToHost_r(&hdsk, host);
2939 h_SetupCallbackConn_r(host);
2943 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
2945 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
2947 h_AddHostToAddrHashTable_r(host->host, host->port, host);
2948 h_InsertList_r(host);
2950 /* setup host id map entry */
2951 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
2952 state->h_map.entries[hdsk.index].new_idx = host->index;
2964 /* serialize a host structure to disk */
2966 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
2968 out->host = in->host;
2969 out->port = in->port;
2970 out->hostFlags = in->hostFlags;
2971 out->Console = in->Console;
2972 out->hcpsfailed = in->hcpsfailed;
2973 out->LastCall = in->LastCall;
2974 out->ActiveCall = in->ActiveCall;
2975 out->cpsCall = in->cpsCall;
2976 out->cblist = in->cblist;
2977 #ifdef FS_STATS_DETAILED
2978 out->InSameNetwork = in->InSameNetwork;
2981 /* special fields we save, but are not memcpy'd back on restore */
2982 out->index = in->index;
2983 out->hcps_len = in->hcps.prlist_len;
2984 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
2987 /* restore a host structure from disk */
2989 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
2991 out->host = in->host;
2992 out->port = in->port;
2993 out->hostFlags = in->hostFlags;
2994 out->Console = in->Console;
2995 out->hcpsfailed = in->hcpsfailed;
2996 out->LastCall = in->LastCall;
2997 out->ActiveCall = in->ActiveCall;
2998 out->cpsCall = in->cpsCall;
2999 out->cblist = in->cblist;
3000 #ifdef FS_STATS_DETAILED
3001 out->InSameNetwork = in->InSameNetwork;
3005 /* index translation routines */
3007 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
3011 /* hosts use a zero-based index, so old==0 is valid */
3013 if (old >= state->h_map.len) {
3014 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
3016 } else if (state->h_map.entries[old].old_idx != old) { /* sanity check */
3017 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
3020 *new = state->h_map.entries[old].new_idx;
3026 #endif /* AFS_DEMAND_ATTACH_FS */
3030 * This counts the number of workstations, the number of active workstations,
3031 * and the number of workstations declared "down" (i.e. not heard from
3032 * recently). An active workstation has received a call since the cutoff
3033 * time argument passed.
3036 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3038 register struct host *host;
3039 register int num = 0, active = 0, del = 0;
3042 for (host = hostList; host; host = host->next) {
3043 if (!(host->hostFlags & HOSTDELETED)) {
3045 if (host->ActiveCall > cutofftime)
3047 if (host->hostFlags & VENUSDOWN)
3059 } /*h_GetWorkStats */
3062 /*------------------------------------------------------------------------
3063 * PRIVATE h_ClassifyAddress
3066 * Given a target IP address and a candidate IP address (both
3067 * in host byte order), classify the candidate into one of three
3068 * buckets in relation to the target by bumping the counters passed
3072 * a_targetAddr : Target address.
3073 * a_candAddr : Candidate address.
3074 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3075 * addresses are either in the same network
3076 * or the same subnet.
3077 * a_diffSubnetP : ...when the candidate is in a different
3079 * a_diffNetworkP : ...when the candidate is in a different
3086 * The target and candidate addresses are both in host byte
3087 * order, NOT network byte order, when passed in.
3091 *------------------------------------------------------------------------*/
3094 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3095 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3096 afs_int32 * a_diffNetworkP)
3097 { /*h_ClassifyAddress */
3099 afs_uint32 targetNet;
3100 afs_uint32 targetSubnet;
3102 afs_uint32 candSubnet;
3105 * Put bad values into the subnet info to start with.
3107 targetSubnet = (afs_uint32) 0;
3108 candSubnet = (afs_uint32) 0;
3111 * Pull out the network and subnetwork numbers from the target
3112 * and candidate addresses. We can short-circuit this whole
3113 * affair if the target and candidate addresses are not of the
3116 if (IN_CLASSA(a_targetAddr)) {
3117 if (!(IN_CLASSA(a_candAddr))) {
3118 (*a_diffNetworkP)++;
3121 targetNet = a_targetAddr & IN_CLASSA_NET;
3122 candNet = a_candAddr & IN_CLASSA_NET;
3123 if (IN_SUBNETA(a_targetAddr))
3124 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
3125 if (IN_SUBNETA(a_candAddr))
3126 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
3127 } else if (IN_CLASSB(a_targetAddr)) {
3128 if (!(IN_CLASSB(a_candAddr))) {
3129 (*a_diffNetworkP)++;
3132 targetNet = a_targetAddr & IN_CLASSB_NET;
3133 candNet = a_candAddr & IN_CLASSB_NET;
3134 if (IN_SUBNETB(a_targetAddr))
3135 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
3136 if (IN_SUBNETB(a_candAddr))
3137 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
3138 } /*Class B target */
3139 else if (IN_CLASSC(a_targetAddr)) {
3140 if (!(IN_CLASSC(a_candAddr))) {
3141 (*a_diffNetworkP)++;
3144 targetNet = a_targetAddr & IN_CLASSC_NET;
3145 candNet = a_candAddr & IN_CLASSC_NET;
3148 * Note that class C addresses can't have subnets,
3149 * so we leave the defaults untouched.
3151 } /*Class C target */
3153 targetNet = a_targetAddr;
3154 candNet = a_candAddr;
3155 } /*Class D address */
3158 * Now, simply compare the extracted net and subnet values for
3159 * the two addresses (which at this point are known to be of the
3162 if (targetNet == candNet) {
3163 if (targetSubnet == candSubnet)
3164 (*a_sameNetOrSubnetP)++;
3168 (*a_diffNetworkP)++;
3170 } /*h_ClassifyAddress */
3173 /*------------------------------------------------------------------------
3174 * EXPORTED h_GetHostNetStats
3177 * Iterate through the host table, and classify each (non-deleted)
3178 * host entry into ``proximity'' categories (same net or subnet,
3179 * different subnet, different network).
3182 * a_numHostsP : Set to total number of (non-deleted) hosts.
3183 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
3184 * a_diffSubnetP : Set to # hosts on diff subnet as server.
3185 * a_diffNetworkP : Set to # hosts on diff network as server.
3191 * We only count non-deleted hosts. The storage pointed to by our
3192 * parameters is zeroed upon entry.
3196 *------------------------------------------------------------------------*/
3199 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
3200 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
3201 { /*h_GetHostNetStats */
3203 register struct host *hostP; /*Ptr to current host entry */
3204 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
3207 * Clear out the storage pointed to by our parameters.
3209 *a_numHostsP = (afs_int32) 0;
3210 *a_sameNetOrSubnetP = (afs_int32) 0;
3211 *a_diffSubnetP = (afs_int32) 0;
3212 *a_diffNetworkP = (afs_int32) 0;
3215 for (hostP = hostList; hostP; hostP = hostP->next) {
3216 if (!(hostP->hostFlags & HOSTDELETED)) {
3218 * Bump the number of undeleted host entries found.
3219 * In classifying the current entry's address, make
3220 * sure to first convert to host byte order.
3223 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
3224 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
3225 a_sameNetOrSubnetP, a_diffSubnetP,
3227 } /*Only look at non-deleted hosts */
3228 } /*For each host record hashed to this index */
3230 } /*h_GetHostNetStats */
3232 static afs_uint32 checktime;
3233 static afs_uint32 clientdeletetime;
3234 static struct AFSFid zerofid;
3238 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
3239 * Since it can serialize them, and pile up, it should be a separate LWP
3240 * from other events.
3243 CheckHost(register struct host *host, int held)
3245 register struct client *client;
3246 struct rx_connection *cb_conn = NULL;
3249 #ifdef AFS_DEMAND_ATTACH_FS
3250 /* kill the checkhost lwp ASAP during shutdown */
3252 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3254 return H_ENUMERATE_BAIL(held);
3259 /* Host is held by h_Enumerate */
3261 for (client = host->FirstClient; client; client = client->next) {
3262 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3263 client->deleted = 1;
3264 host->hostFlags |= CLIENTDELETED;
3267 if (host->LastCall < checktime) {
3269 host->hostFlags |= HWHO_INPROGRESS;
3270 if (!(host->hostFlags & HOSTDELETED)) {
3271 cb_conn = host->callback_rxcon;
3272 rx_GetConnection(cb_conn);
3273 if (host->LastCall < clientdeletetime) {
3274 host->hostFlags |= HOSTDELETED;
3275 if (!(host->hostFlags & VENUSDOWN)) {
3276 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3277 if (host->interface) {
3280 RXAFSCB_InitCallBackState3(cb_conn,
3286 RXAFSCB_InitCallBackState(cb_conn);
3289 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3292 (void)afs_inet_ntoa_r(host->host, hoststr);
3294 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3295 hoststr, ntohs(host->port)));
3296 host->hostFlags |= VENUSDOWN;
3298 /* Note: it's safe to delete hosts even if they have call
3299 * back state, because break delayed callbacks (called when a
3300 * message is received from the workstation) will always send a
3301 * break all call backs to the workstation if there is no
3306 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3308 (void)afs_inet_ntoa_r(host->host, hoststr);
3309 if (host->interface) {
3310 afsUUID uuid = host->interface->uuid;
3312 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3315 if (MultiProbeAlternateAddress_r(host)) {
3316 ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
3317 hoststr, ntohs(host->port), code));
3318 host->hostFlags |= VENUSDOWN;
3323 code = RXAFSCB_Probe(cb_conn);
3327 ("CheckHost: Probe failed for host %s:%d, code %d\n",
3328 hoststr, ntohs(host->port), code));
3329 host->hostFlags |= VENUSDOWN;
3335 rx_PutConnection(cb_conn);
3339 host->hostFlags &= ~HWHO_INPROGRESS;
3348 CheckHost_r(register struct host *host, int held, char *dummy)
3350 register struct client *client;
3351 struct rx_connection *cb_conn = NULL;
3354 #ifdef AFS_DEMAND_ATTACH_FS
3355 /* kill the checkhost lwp ASAP during shutdown */
3357 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3359 return H_ENUMERATE_BAIL(held);
3364 /* Host is held by h_Enumerate_r */
3365 for (client = host->FirstClient; client; client = client->next) {
3366 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3367 client->deleted = 1;
3368 host->hostFlags |= CLIENTDELETED;
3371 if (host->LastCall < checktime) {
3373 if (!(host->hostFlags & HOSTDELETED)) {
3374 cb_conn = host->callback_rxcon;
3375 rx_GetConnection(cb_conn);
3376 if (host->LastCall < clientdeletetime) {
3377 host->hostFlags |= HOSTDELETED;
3378 if (!(host->hostFlags & VENUSDOWN)) {
3379 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3380 if (host->interface) {
3383 RXAFSCB_InitCallBackState3(cb_conn,
3389 RXAFSCB_InitCallBackState(cb_conn);
3392 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3395 (void)afs_inet_ntoa_r(host->host, hoststr);
3397 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3398 hoststr, ntohs(host->port)));
3399 host->hostFlags |= VENUSDOWN;
3401 /* Note: it's safe to delete hosts even if they have call
3402 * back state, because break delayed callbacks (called when a
3403 * message is received from the workstation) will always send a
3404 * break all call backs to the workstation if there is no
3409 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3411 (void)afs_inet_ntoa_r(host->host, hoststr);
3412 if (host->interface) {
3413 afsUUID uuid = host->interface->uuid;
3415 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3418 if (MultiProbeAlternateAddress_r(host)) {
3419 ViceLog(0,("CheckHost_r: Probing all interfaces of host %s:%d failed, code %d\n",
3420 hoststr, ntohs(host->port), code));
3421 host->hostFlags |= VENUSDOWN;
3426 code = RXAFSCB_Probe(cb_conn);
3430 ("CheckHost_r: Probe failed for host %s:%d, code %d\n",
3431 hoststr, ntohs(host->port), code));
3432 host->hostFlags |= VENUSDOWN;
3438 rx_PutConnection(cb_conn);
3450 * Set VenusDown for any hosts that have not had a call in 15 minutes and
3451 * don't respond to a probe. Note that VenusDown can only be cleared if
3452 * a message is received from the host (see ServerLWP in file.c).
3453 * Delete hosts that have not had any calls in 1 hour, clients that
3454 * have not had any calls in 15 minutes.
3456 * This routine is called roughly every 5 minutes.
3461 afs_uint32 now = FT_ApproxTime();
3463 memset((char *)&zerofid, 0, sizeof(zerofid));
3465 * Send a probe to the workstation if it hasn't been heard from in
3468 checktime = now - 15 * 60;
3469 clientdeletetime = now - 120 * 60; /* 2 hours ago */
3472 h_Enumerate_r(CheckHost_r, hostList, NULL);
3477 * This is called with host locked and held. At this point, the
3478 * hostAddrHashTable should not have any entries for the alternate
3479 * interfaces. This function has to insert these entries in the
3480 * hostAddrHashTable.
3482 * The addresses in the interfaceAddr list are in host byte order.
3485 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
3492 struct Interface *interface;
3495 afs_uint16 port7001 = htons(7001);
3500 number = interf->numberOfInterfaces;
3501 myAddr = host->host; /* current interface address */
3502 myPort = host->port; /* current port */
3505 ("initInterfaceAddr : host %s:%d numAddr %d\n",
3506 afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
3508 /* validation checks */
3509 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
3511 ("Invalid number of alternate addresses is %d\n", number));
3516 * The client's notion of its own IP addresses is not reliable.
3518 * 1. The client list might contain private address ranges which
3519 * are likely to be re-used by many clients allocated addresses
3522 * 2. The client list will not include any public addresses that
3523 * are hidden by a NAT.
3525 * 3. Private address ranges that are exposed to the server will
3526 * be obtained from the rx connections that use them.
3528 * 4. Lists provided by the client are not necessarily truthful.
3529 * Many existing clients (UNIX) do not refresh the IP address
3530 * list as the actual assigned addresses change. The end result
3531 * is that they report the initial address list for the lifetime
3532 * of the process. In other words, a client can report addresses
3533 * that they are in fact not using. Adding these addresses to
3534 * the host interface list without verification is not only
3535 * pointless, it is downright dangerous.
3537 * We therefore do not add alternate addresses to the addr hash table.
3538 * We only use them for multi-rx callback breaks.
3542 * Convert IP addresses to network byte order, and remove
3543 * duplicate IP addresses from the interface list, and
3544 * determine whether or not the incoming addr/port is
3545 * listed. Note that if the address matches it is not
3546 * truly a match because the port number for the entries
3547 * in the interface list are port 7001 and the port number
3548 * for this connection might not be 7001.
3550 for (i = 0, count = 0, found = 0; i < number; i++) {
3551 interf->addr_in[i] = htonl(interf->addr_in[i]);
3552 for (j = 0; j < count; j++) {
3553 if (interf->addr_in[j] == interf->addr_in[i])
3557 interf->addr_in[count] = interf->addr_in[i];
3558 if (interf->addr_in[count] == myAddr &&
3566 * Allocate and initialize an interface structure for this host.
3569 interface = (struct Interface *)
3570 malloc(sizeof(struct Interface) +
3571 (sizeof(struct AddrPort) * (count - 1)));
3573 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
3576 interface->numberOfInterfaces = count;
3578 interface = (struct Interface *)
3579 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
3581 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
3584 interface->numberOfInterfaces = count + 1;
3585 interface->interface[count].addr = myAddr;
3586 interface->interface[count].port = myPort;
3587 interface->interface[count].valid = 1;
3590 for (i = 0; i < count; i++) {
3592 interface->interface[i].addr = interf->addr_in[i];
3593 /* We store the port as 7001 because the addresses reported by
3594 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
3595 * are coming from fully connected hosts (no NAT/PATs)
3597 interface->interface[i].port = port7001;
3598 interface->interface[i].valid = 1; /* valid until a conflict is found */
3601 interface->uuid = interf->uuid;
3603 assert(!host->interface);
3604 host->interface = interface;
3606 afsUUID_to_string(&interface->uuid, uuidstr, 127);
3608 ViceLog(125, ("--- uuid %s\n", uuidstr));
3609 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
3610 ViceLog(125, ("--- alt address %s:%d\n",
3611 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3612 ntohs(host->interface->interface[i].port)));
3618 /* deleted a HashChain structure for this address and host */
3619 /* returns 1 on success */
3621 h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
3624 register struct h_AddrHashChain **hp, *th;
3626 for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);) {
3627 assert(th->hostPtr);
3628 if (th->hostPtr == host && th->addr == addr && th->port == port) {
3642 ** prints out all alternate interface address for the host. The 'level'
3643 ** parameter indicates what level of debugging sets this output
3646 printInterfaceAddr(struct host *host, int level)
3651 if (host->interface) {
3652 /* check alternate addresses */
3653 number = host->interface->numberOfInterfaces;
3655 ViceLog(level, ("no-addresses "));
3657 for (i = 0; i < number; i++)
3658 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3659 ntohs(host->interface->interface[i].port)));
3662 ViceLog(level, ("\n"));