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>
25 #include <netinet/in.h>
30 #include <afs/assert.h>
33 #include <afs/afsint.h>
34 #define FSINT_COMMON_XG
35 #include <afs/afscbint.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);
90 static int h_threadquota(int);
92 #define CESPERBLOCK 73
93 struct CEBlock { /* block of CESPERBLOCK file entries */
94 struct client entry[CESPERBLOCK];
97 void h_TossStuff_r(register struct host *host);
100 * Make sure the subnet macros have been defined.
103 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
106 #ifndef IN_CLASSA_SUBNET
107 #define IN_CLASSA_SUBNET 0xffff0000
111 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
114 #ifndef IN_CLASSB_SUBNET
115 #define IN_CLASSB_SUBNET 0xffffff00
119 /* get a new block of CEs and chain it on CEFree */
123 register struct CEBlock *block;
126 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
128 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
129 ShutDownAndCore(PANIC);
132 for (i = 0; i < (CESPERBLOCK - 1); i++) {
133 Lock_Init(&block->entry[i].lock);
134 block->entry[i].next = &(block->entry[i + 1]);
136 block->entry[CESPERBLOCK - 1].next = 0;
137 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
138 CEFree = (struct client *)block;
144 /* get the next available CE */
145 static struct client *
148 register struct client *entry;
153 ViceLog(0, ("CEFree NULL in GetCE\n"));
154 ShutDownAndCore(PANIC);
158 CEFree = entry->next;
160 memset(entry, 0, CLIENT_TO_ZERO(entry));
166 /* return an entry to the free list */
168 FreeCE(register struct client *entry)
170 entry->VenusEpoch = 0;
172 entry->next = CEFree;
179 * The HTs and HTBlocks variables were formerly static, but they are
180 * now referenced elsewhere in the FileServer.
182 int HTs = 0; /* active file entries */
183 int HTBlocks = 0; /* number of blocks of HTs */
184 static struct host *HTFree = 0; /* first free file entry */
187 * Hash tables of host pointers. We need two tables, one
188 * to map IP addresses onto host pointers, and another
189 * to map host UUIDs onto host pointers.
191 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
192 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
193 #define h_HashIndex(hostip) (ntohl(hostip) & (h_HASHENTRIES-1))
194 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
196 struct HTBlock { /* block of HTSPERBLOCK file entries */
197 struct host entry[h_HTSPERBLOCK];
201 /* get a new block of HTs and chain it on HTFree */
205 register struct HTBlock *block;
207 static int index = 0;
209 if (HTBlocks == h_MAXHOSTTABLES) {
210 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
211 ShutDownAndCore(PANIC);
214 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
216 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
217 ShutDownAndCore(PANIC);
219 #ifdef AFS_PTHREAD_ENV
220 for (i = 0; i < (h_HTSPERBLOCK); i++)
221 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
222 #endif /* AFS_PTHREAD_ENV */
223 for (i = 0; i < (h_HTSPERBLOCK); i++)
224 Lock_Init(&block->entry[i].lock);
225 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
226 block->entry[i].next = &(block->entry[i + 1]);
227 for (i = 0; i < (h_HTSPERBLOCK); i++)
228 block->entry[i].index = index++;
229 block->entry[h_HTSPERBLOCK - 1].next = 0;
230 HTFree = (struct host *)block;
231 hosttableptrs[HTBlocks++] = block->entry;
236 /* get the next available HT */
240 register struct host *entry;
244 assert(HTFree != NULL);
246 HTFree = entry->next;
248 memset(entry, 0, HOST_TO_ZERO(entry));
254 /* return an entry to the free list */
256 FreeHT(register struct host *entry)
258 entry->next = HTFree;
265 hpr_Initialize(struct ubik_client **uclient)
268 struct rx_connection *serverconns[MAXSERVERS];
269 struct rx_securityClass *sc[3];
270 struct afsconf_dir *tdir;
271 struct ktc_token ttoken;
273 struct afsconf_cell info;
277 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
279 ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
283 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
285 ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]", code));
290 code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
292 ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s", cellstr, confDir, AFSDIR_CELLSERVDB_FILE));
299 ViceLog(0, ("hpr_Initialize: Could not initialize rx."));
308 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
309 * to force use of the KeyFile. secLevel == 0 implies -noauth was
311 if ((afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
312 code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
314 ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s (so trying noauth)", code, afs_error_message(code)));
316 scIndex = 0; /* use noauth */
318 /* if there was a problem, an unauthenticated conn is returned */
321 struct ktc_principal sname;
322 strcpy(sname.cell, info.name);
323 sname.instance[0] = 0;
324 strcpy(sname.name, "afs");
325 code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
329 if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
330 /* this is a kerberos ticket, set scIndex accordingly */
333 ViceLog(0, ("hpr_Initialize: funny kvno (%d) in ticket, proceeding", ttoken.kvno));
337 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
338 ttoken.kvno, ttoken.ticketLen,
342 if ((scIndex == 0) && (sc[0] == 0))
343 sc[0] = rxnull_NewClientSecurityObject();
345 ViceLog(0, ("hpr_Initialize: Could not get afs tokens, running unauthenticated. [%d]", code));
347 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
348 for (i = 0; i < info.numServers; i++) {
350 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
351 info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
355 code = ubik_ClientInit(serverconns, uclient);
357 ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
360 code = rxs_Release(sc[scIndex]);
365 hpr_End(struct ubik_client *uclient)
370 code = ubik_ClientDestroy(uclient);
376 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
378 #ifdef AFS_PTHREAD_ENV
379 register afs_int32 code;
381 struct ubik_client *uclient =
382 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
385 code = hpr_Initialize(&uclient);
387 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
393 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
394 if (code != PRSUCCESS)
397 /* do something about this, probably make a new call */
398 /* don't forget there's a hard limit in the interface */
400 "membership list for host id %d exceeds display limit\n",
405 return pr_GetHostCPS(host, CPS);
410 hpr_NameToId(namelist *names, idlist *ids)
412 #ifdef AFS_PTHREAD_ENV
413 register afs_int32 code;
414 register afs_int32 i;
415 struct ubik_client *uclient =
416 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
419 code = hpr_Initialize(&uclient);
421 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
426 for (i = 0; i < names->namelist_len; i++)
427 stolower(names->namelist_val[i]);
428 code = ubik_PR_NameToID(uclient, 0, names, ids);
431 return pr_NameToId(names, ids);
436 hpr_IdToName(idlist *ids, namelist *names)
438 #ifdef AFS_PTHREAD_ENV
439 register afs_int32 code;
440 struct ubik_client *uclient =
441 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
444 code = hpr_Initialize(&uclient);
446 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
451 code = ubik_PR_IDToName(uclient, 0, ids, names);
454 return pr_IdToName(ids, names);
459 hpr_GetCPS(afs_int32 id, prlist *CPS)
461 #ifdef AFS_PTHREAD_ENV
462 register afs_int32 code;
464 struct ubik_client *uclient =
465 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
468 code = hpr_Initialize(&uclient);
470 assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
476 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
477 if (code != PRSUCCESS)
480 /* do something about this, probably make a new call */
481 /* don't forget there's a hard limit in the interface */
482 fprintf(stderr, "membership list for id %d exceeds display limit\n",
487 return pr_GetCPS(id, CPS);
491 static short consolePort = 0;
494 h_Lock_r(register struct host *host)
504 * returns 1 if already locked
505 * else returns locks and returns 0
509 h_NBLock_r(register struct host *host)
511 struct Lock *hostLock = &host->lock;
516 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
517 hostLock->excl_locked = WRITE_LOCK;
521 LOCK_UNLOCK(hostLock);
530 #if FS_STATS_DETAILED
531 /*------------------------------------------------------------------------
532 * PRIVATE h_AddrInSameNetwork
535 * Given a target IP address and a candidate IP address (both
536 * in host byte order), return a non-zero value (1) if the
537 * candidate address is in a different network from the target
541 * a_targetAddr : Target address.
542 * a_candAddr : Candidate address.
545 * 1 if the candidate address is in the same net as the target,
549 * The target and candidate addresses are both in host byte
550 * order, NOT network byte order, when passed in. We return
551 * our value as a character, since that's the type of field in
552 * the host structure, where this info will be stored.
556 *------------------------------------------------------------------------*/
559 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
560 { /*h_AddrInSameNetwork */
562 afs_uint32 targetNet;
566 * Pull out the network and subnetwork numbers from the target
567 * and candidate addresses. We can short-circuit this whole
568 * affair if the target and candidate addresses are not of the
571 if (IN_CLASSA(a_targetAddr)) {
572 if (!(IN_CLASSA(a_candAddr))) {
575 targetNet = a_targetAddr & IN_CLASSA_NET;
576 candNet = a_candAddr & IN_CLASSA_NET;
577 } else if (IN_CLASSB(a_targetAddr)) {
578 if (!(IN_CLASSB(a_candAddr))) {
581 targetNet = a_targetAddr & IN_CLASSB_NET;
582 candNet = a_candAddr & IN_CLASSB_NET;
583 } /*Class B target */
584 else if (IN_CLASSC(a_targetAddr)) {
585 if (!(IN_CLASSC(a_candAddr))) {
588 targetNet = a_targetAddr & IN_CLASSC_NET;
589 candNet = a_candAddr & IN_CLASSC_NET;
590 } /*Class C target */
592 targetNet = a_targetAddr;
593 candNet = a_candAddr;
594 } /*Class D address */
597 * Now, simply compare the extracted net values for the two addresses
598 * (which at this point are known to be of the same class)
600 if (targetNet == candNet)
605 } /*h_AddrInSameNetwork */
606 #endif /* FS_STATS_DETAILED */
609 /* Assumptions: called with held host */
611 h_gethostcps_r(register struct host *host, register afs_int32 now)
616 /* wait if somebody else is already doing the getCPS call */
617 while (host->hostFlags & HCPS_INPROGRESS) {
618 slept = 1; /* I did sleep */
619 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
620 #ifdef AFS_PTHREAD_ENV
621 pthread_cond_wait(&host->cond, &host_glock_mutex);
622 #else /* AFS_PTHREAD_ENV */
623 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
624 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
625 #endif /* AFS_PTHREAD_ENV */
629 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
630 if (host->hcps.prlist_val)
631 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
632 host->hcps.prlist_val = NULL;
633 host->hcps.prlist_len = 0;
634 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
637 code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
642 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
643 * and reevaluates things if no sync server or quorum is found we could still end up
644 * with one of these errors. In such case we would like to reevaluate the rpc call to
645 * find if there's cps for this guy. We treat other errors (except network failures
646 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
647 * we could like to deal this problem the other way around (i.e. if code == NOCPS
648 * ignore else retry next time) but the problem is that there're other errors (i.e.
649 * EPERM) for which we don't want to retry and we don't know the whole code list!
651 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
653 * We would have preferred to use a while loop and try again since ops in protected
654 * acls for this host will fail now but they'll be reevaluated on any subsequent
655 * call. The attempt to wait for a quorum/sync site or network error won't work
656 * since this problems really should only occurs during a complete fileserver
657 * restart. Since the fileserver will start before the ptservers (and thus before
658 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
660 host->hcpsfailed = 1;
662 ("Warning: GetHostCPS failed (%d) for %x (%s:%d); will retry\n",
663 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
665 host->hcpsfailed = 0;
667 ("gethost: GetHostCPS failed (%d) for %x (%s:%d); ignored\n",
668 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
670 if (host->hcps.prlist_val)
671 free(host->hcps.prlist_val);
672 host->hcps.prlist_val = NULL;
673 host->hcps.prlist_len = 0; /* Make sure it's zero */
675 host->hcpsfailed = 0;
677 host->hostFlags &= ~HCPS_INPROGRESS;
678 /* signal all who are waiting */
679 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
680 host->hostFlags &= ~HCPS_WAITING;
681 #ifdef AFS_PTHREAD_ENV
682 assert(pthread_cond_broadcast(&host->cond) == 0);
683 #else /* AFS_PTHREAD_ENV */
684 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
685 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
686 #endif /* AFS_PTHREAD_ENV */
690 /* args in net byte order */
692 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
697 h_Lookup_r(hostaddr, hport, &host);
699 host->hcpsfailed = 1;
708 * Allocate a host. It will be identified by the peer (ip,port) info in the
709 * rx connection provided. The host is returned held and locked
711 #define DEF_ROPCONS 2115
714 h_Alloc_r(register struct rx_connection *r_con)
716 struct servent *serverentry;
719 #if FS_STATS_DETAILED
720 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
721 #endif /* FS_STATS_DETAILED */
725 host->host = rxr_HostOf(r_con);
726 host->port = rxr_PortOf(r_con);
728 h_AddHostToAddrHashTable_r(host->host, host->port, host);
730 if (consolePort == 0) { /* find the portal number for console */
731 #if defined(AFS_OSF_ENV)
732 serverentry = getservbyname("ropcons", "");
734 serverentry = getservbyname("ropcons", 0);
737 consolePort = serverentry->s_port;
739 consolePort = htons(DEF_ROPCONS); /* Use a default */
741 if (host->port == consolePort)
743 /* Make a callback channel even for the console, on the off chance that it
744 * makes a request that causes a break call back. It shouldn't. */
745 h_SetupCallbackConn_r(host);
746 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
748 host->hcps.prlist_val = NULL;
749 host->hcps.prlist_len = 0;
750 host->interface = NULL;
752 host->hcpsfailed = 0; /* save cycles */
753 h_gethostcps(host); /* do this under host hold/lock */
755 host->FirstClient = NULL;
758 h_InsertList_r(host); /* update global host List */
759 #if FS_STATS_DETAILED
761 * Compare the new host's IP address (in host byte order) with ours
762 * (the File Server's), remembering if they are in the same network.
764 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
765 host->InSameNetwork =
766 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
767 #endif /* FS_STATS_DETAILED */
774 /* Make a callback channel even for the console, on the off chance that it
775 * makes a request that causes a break call back. It shouldn't. */
777 h_SetupCallbackConn_r(struct host * host)
780 sc = rxnull_NewClientSecurityObject();
781 host->callback_rxcon =
782 rx_NewConnection(host->host, host->port, 1, sc, 0);
783 rx_SetConnDeadTime(host->callback_rxcon, 50);
784 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
788 * Lookup a host given an IP address and UDP port number.
789 * hostaddr and hport are in network order
790 * hostaddr and hport are in network order
791 * On return, refCount is incremented.
794 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
797 struct host *host = NULL;
798 struct h_AddrHashChain *chain;
799 int index = h_HashIndex(haddr);
800 extern int hostaclRefresh;
803 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
804 host = chain->hostPtr;
806 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
807 && chain->port == hport) {
808 if ((host->hostFlags & HWHO_INPROGRESS) &&
809 h_threadquota(host->lock.num_waiting)) {
815 if (host->hostFlags & HOSTDELETED) {
822 now = FT_ApproxTime(); /* always evaluate "now" */
823 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
825 * Every hostaclRefresh period (def 2 hrs) get the new
826 * membership list for the host. Note this could be the
827 * first time that the host is added to a group. Also
828 * here we also retry on previous legitimate hcps failures.
830 * If we get here refCount is elevated.
832 h_gethostcps_r(host, now);
842 /* Lookup a host given its UUID. */
844 h_LookupUuid_r(afsUUID * uuidp)
846 struct host *host = 0;
847 struct h_UuidHashChain *chain;
848 int index = h_UuidHashIndex(uuidp);
850 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
851 host = chain->hostPtr;
853 if (!(host->hostFlags & HOSTDELETED) && host->interface
854 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
862 /* h_TossStuff_r: Toss anything in the host structure (the host or
863 * clients marked for deletion. Called from h_Release_r ONLY.
864 * To be called, there must be no holds, and either host->deleted
865 * or host->clientDeleted must be set.
868 h_TossStuff_r(register struct host *host)
870 register struct client **cp, *client;
872 /* if somebody still has this host held */
873 if (host->refCount > 0)
876 /* if somebody still has this host locked */
877 if (h_NBLock_r(host) != 0) {
880 ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT " (%s:%d) was locked.\n",
881 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
887 /* ASSUMPTION: rxi_FreeConnection() does not yield */
888 for (cp = &host->FirstClient; (client = *cp);) {
889 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
891 ObtainWriteLockNoBlock(&client->lock, code);
895 ("Warning: h_TossStuff_r failed: Host %" AFS_PTR_FMT " (%s:%d) client %x was locked.\n",
896 host, afs_inet_ntoa_r(host->host, hoststr),
897 ntohs(host->port), client));
901 if (client->refCount) {
904 ("Warning: h_TossStuff_r failed: Host %" AFS_PTR_FMT " (%s:%d) client %x refcount %d.\n",
905 host, afs_inet_ntoa_r(host->host, hoststr),
906 ntohs(host->port), client, client->refCount));
907 /* This is the same thing we do if the host is locked */
908 ReleaseWriteLock(&client->lock);
911 client->CPS.prlist_len = 0;
912 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
913 free(client->CPS.prlist_val);
914 client->CPS.prlist_val = NULL;
915 CurrentConnections--;
917 ReleaseWriteLock(&client->lock);
923 /* We've just cleaned out all the deleted clients; clear the flag */
924 host->hostFlags &= ~CLIENTDELETED;
926 if (host->hostFlags & HOSTDELETED) {
927 register struct rx_connection *rxconn;
928 struct AddrPort hostAddrPort;
931 if (host->Console & 1)
933 if ((rxconn = host->callback_rxcon)) {
934 host->callback_rxcon = (struct rx_connection *)0;
935 rx_DestroyConnection(rxconn);
937 if (host->hcps.prlist_val)
938 free(host->hcps.prlist_val);
939 host->hcps.prlist_val = NULL;
940 host->hcps.prlist_len = 0;
941 DeleteAllCallBacks_r(host, 1);
942 host->hostFlags &= ~RESETDONE; /* just to be safe */
944 /* if alternate addresses do not exist */
945 if (!(host->interface)) {
946 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
948 h_DeleteHostFromUuidHashTable_r(host);
949 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
950 /* delete the hash entry for each valid alternate addresses */
951 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
952 hostAddrPort = host->interface->interface[i];
954 * if the interface addr/port is the primary, we already
955 * removed it. If the addr/port is not valid, its not
958 if (hostAddrPort.valid &&
959 (host->host != hostAddrPort.addr ||
960 host->port != hostAddrPort.port))
961 h_DeleteHostFromAddrHashTable_r(hostAddrPort.addr, hostAddrPort.port, host);
963 free(host->interface);
964 host->interface = NULL;
965 } /* if alternate address exists */
967 h_DeleteList_r(host); /* remove host from global host List */
974 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
975 * system at the start of the enumeration (perhaps more). Hosts may be deleted
976 * (have delete flag set); ditto for clients. refCount is always incremented
977 * before (*proc) is called. The param flags is passed to (*proc) as the
978 * param flags, permitting (*proc) to stop the enumeration (BAIL).
980 * Needed? Why not always h_Hold_r and h_Release_r in (*proc), or even -never-
981 * h_Hold_r or h_Release_r in (*proc)?
983 * **The proc should return 0 if the host should be released, 1 if it should
984 * be held after enumeration.
987 h_Enumerate(int (*proc) (struct host*, int, void *), void *param)
989 register struct host *host, **list;
991 register int i, count;
994 if (hostCount == 0) {
998 list = (struct host **)malloc(hostCount * sizeof(struct host *));
1000 ViceLog(0, ("Failed malloc in h_Enumerate (list)\n"));
1003 flags = (int *)malloc(hostCount * sizeof(int));
1005 ViceLog(0, ("Failed malloc in h_Enumerate (flags)\n"));
1008 for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
1012 if (count != hostCount) {
1013 ViceLog(0, ("h_Enumerate found %d of %d hosts\n", count, hostCount));
1014 } else if (host != NULL) {
1015 ViceLog(0, ("h_Enumerate found more than %d hosts\n", hostCount));
1019 for (i = 0; i < count; i++) {
1020 flags[i] = (*proc) (list[i], flags[i], param);
1021 h_Release_r(list[i]);
1022 /* bail out of the enumeration early */
1023 if (H_ENUMERATE_ISSET_BAIL(flags[i]))
1027 free((void *)flags);
1031 /* h_Enumerate_r (revised):
1032 * Calls (*proc)(host, flags, param) for each host in hostList, starting
1033 * at enumstart. Called only under H_LOCK. Hosts may be deleted (have
1034 * delete flag set); ditto for clients. refCount is always incremented
1035 * before (*proc) is called. The param flags is passed to (*proc) as the
1036 * param flags, permitting (*proc) to stop the enumeration (BAIL).
1038 * Needed? Why not always h_Hold_r and h_Release_r in (*proc), or even -never-
1039 * h_Hold_r or h_Release_r in (*proc)?
1041 * **The proc should return 0 if the host should be released, 1 if it should
1042 * be held after enumeration.
1045 h_Enumerate_r(int (*proc) (struct host *, int, void *),
1046 struct host *enumstart, void *param)
1048 register struct host *host, *next;
1052 if (hostCount == 0) {
1055 h_Hold_r(enumstart);
1056 for (host = enumstart; host; host = next, flags = nflags) {
1058 if (next && !H_ENUMERATE_ISSET_BAIL(flags))
1060 flags = (*proc) (host, flags, param);
1061 if (H_ENUMERATE_ISSET_BAIL(flags)) {
1062 h_Release_r(host); /* this might free up the host */
1065 h_Release_r(host); /* this might free up the host */
1067 } /*h_Enumerate_r */
1070 /* inserts a new HashChain structure corresponding to this UUID */
1072 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1075 struct h_UuidHashChain *chain;
1076 char uuid1[128], uuid2[128];
1079 /* hash into proper bucket */
1080 index = h_UuidHashIndex(uuid);
1082 /* don't add the same entry multiple times */
1083 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1084 if (!chain->hostPtr)
1087 if (chain->hostPtr->interface &&
1088 afs_uuid_equal(&chain->hostPtr->interface->uuid, uuid)) {
1089 if (LogLevel >= 125) {
1090 afsUUID_to_string(&chain->hostPtr->interface->uuid, uuid1,
1092 afsUUID_to_string(uuid, uuid2, 127);
1093 ViceLog(125, ("h_AddHostToUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s) exists as %s:%d (uuid %s)\n",
1095 afs_inet_ntoa_r(chain->hostPtr->host, hoststr),
1096 ntohs(chain->hostPtr->port), uuid2));
1102 /* insert into beginning of list for this bucket */
1103 chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
1105 ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1108 chain->hostPtr = host;
1109 chain->next = hostUuidHashTable[index];
1110 hostUuidHashTable[index] = chain;
1113 afsUUID_to_string(uuid, uuid2, 127);
1115 ("h_AddHostToUuidHashTable_r: host %" AFS_PTR_FMT " (%s:%d) added as uuid %s\n",
1116 host, afs_inet_ntoa_r(chain->hostPtr->host, hoststr),
1117 ntohs(chain->hostPtr->port), uuid));
1120 /* deletes a HashChain structure corresponding to this host */
1122 h_DeleteHostFromUuidHashTable_r(struct host *host)
1125 register struct h_UuidHashChain **uhp, *uth;
1129 if (!host->interface)
1132 /* hash into proper bucket */
1133 index = h_UuidHashIndex(&host->interface->uuid);
1135 if (LogLevel >= 125)
1136 afsUUID_to_string(&host->interface->uuid, uuid1, 127);
1137 for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
1138 assert(uth->hostPtr);
1139 if (uth->hostPtr == host) {
1141 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d)\n",
1142 host, uuid1, afs_inet_ntoa_r(host->host, hoststr),
1143 ntohs(host->port)));
1150 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d) not found\n",
1151 host, uuid1, afs_inet_ntoa_r(host->host, hoststr),
1152 ntohs(host->port)));
1156 /* inserts a new HashChain structure corresponding to this address */
1158 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1161 struct h_AddrHashChain *chain;
1162 char hoststr[16], hoststr2[16];
1164 /* hash into proper bucket */
1165 index = h_HashIndex(addr);
1167 /* don't add the same entry multiple times */
1168 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1169 if (chain->hostPtr == host) {
1170 if (chain->addr != addr || chain->port != port) {
1172 ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " exists as %s:%d when adding %s:%d\n",
1173 host, afs_inet_ntoa_r(chain->addr, hoststr),
1174 ntohs(chain->port), afs_inet_ntoa_r(addr, hoststr2),
1178 ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) already hashed\n",
1179 host, afs_inet_ntoa_r(chain->addr, hoststr),
1180 ntohs(chain->port)));
1186 /* insert into beginning of list for this bucket */
1187 chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
1189 ViceLog(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1192 chain->hostPtr = host;
1193 chain->next = hostAddrHashTable[index];
1196 hostAddrHashTable[index] = chain;
1197 ViceLog(125, ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " added as %s:%d\n",
1198 host, afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1202 * This is called with host locked and held.
1203 * It is called to either validate or add an additional interface
1204 * address/port on the specified host.
1206 * All addresses are in network byte order.
1209 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1213 struct Interface *interface;
1214 char hoststr[16], hoststr2[16];
1217 assert(host->interface);
1220 * Make sure this address is on the list of known addresses
1223 number = host->interface->numberOfInterfaces;
1224 for (i = 0; i < number; i++) {
1225 if (host->interface->interface[i].addr == addr &&
1226 host->interface->interface[i].port == port) {
1228 ("addInterfaceAddr : found host %" AFS_PTR_FMT " (%s:%d) adding %s:%d%s\n",
1229 host, afs_inet_ntoa_r(host->host, hoststr),
1230 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1231 ntohs(port), host->interface->interface[i].valid ? "" :
1234 if (host->interface->interface[i].valid == 0) {
1235 host->interface->interface[i].valid = 1;
1236 h_AddHostToAddrHashTable_r(addr, port, host);
1242 ViceLog(125, ("addInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) adding %s:%d\n",
1243 host, afs_inet_ntoa_r(host->host, hoststr),
1244 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1247 interface = (struct Interface *)
1248 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1250 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
1253 interface->numberOfInterfaces = number + 1;
1254 interface->uuid = host->interface->uuid;
1255 for (i = 0; i < number; i++)
1256 interface->interface[i] = host->interface->interface[i];
1258 /* Add the new valid interface */
1259 interface->interface[number].addr = addr;
1260 interface->interface[number].port = port;
1261 interface->interface[number].valid = 1;
1262 h_AddHostToAddrHashTable_r(addr, port, host);
1263 free(host->interface);
1264 host->interface = interface;
1271 * This is called with host locked and held.
1273 * All addresses are in network byte order.
1276 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1280 struct Interface *interface;
1281 char hoststr[16], hoststr2[16];
1284 assert(host->interface);
1286 ViceLog(125, ("removeInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1287 host, afs_inet_ntoa_r(host->host, hoststr),
1288 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1292 * Make sure this address is on the list of known addresses
1295 interface = host->interface;
1296 number = host->interface->numberOfInterfaces;
1297 for (i = 0; i < number; i++) {
1298 if (interface->interface[i].addr == addr &&
1299 interface->interface[i].port == port) {
1300 if (interface->interface[i].valid)
1301 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1303 for (; i < number; i++) {
1304 interface->interface[i] = interface->interface[i+1];
1306 interface->numberOfInterfaces = number;
1315 * This is called with host locked and held.
1317 * All addresses are in network byte order.
1320 invalidateInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1324 struct Interface *interface;
1325 char hoststr[16], hoststr2[16];
1328 assert(host->interface);
1330 ViceLog(125, ("invalidateInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1331 host, afs_inet_ntoa_r(host->host, hoststr),
1332 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1336 * Make sure this address is on the list of known addresses
1339 interface = host->interface;
1340 number = host->interface->numberOfInterfaces;
1341 for (i = 0; i < number; i++) {
1342 if (interface->interface[i].addr == addr &&
1343 interface->interface[i].port == port) {
1344 if (interface->interface[i].valid) {
1345 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1346 interface->interface[i].valid = 0;
1357 * This is called with host locked and held. This function differs
1358 * from removeInterfaceAddr_r in that it is called when the address
1359 * is being removed from the host regardless of whether or not there
1360 * is an interface list for the host. This function will delete the
1361 * host if there are no addresses left on it.
1363 * All addresses are in network byte order.
1366 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1369 char hoststr[16], hoststr2[16];
1370 struct rx_connection *rxconn;
1372 if (!host->interface || host->interface->numberOfInterfaces == 1) {
1373 if (host->host == addr && host->port == port) {
1375 ("Removing only address for host %" AFS_PTR_FMT " (%s:%d), deleting host.\n",
1376 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1377 host->hostFlags |= HOSTDELETED;
1379 * Do not remove the primary addr/port from the hash table.
1380 * It will be ignored due to the HOSTDELETED flag and will
1381 * be removed when h_TossStuff_r() cleans up the HOSTDELETED
1382 * host. Removing it here will only result in a search for
1383 * the host/addr/port in the hash chain which will fail.
1387 ("Removing address that does not belong to host %" AFS_PTR_FMT " (%s:%d).\n",
1388 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1391 if (host->host == addr && host->port == port) {
1392 removeInterfaceAddr_r(host, addr, port);
1394 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1395 if (host->interface->interface[i].valid) {
1397 ("Removed address for host %" AFS_PTR_FMT " (%s:%d), new primary interface %s:%d.\n",
1398 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1399 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2),
1400 ntohs(host->interface->interface[i].port)));
1401 host->host = host->interface->interface[i].addr;
1402 host->port = host->interface->interface[i].port;
1403 h_AddHostToAddrHashTable_r(host->host, host->port, host);
1408 if (i == host->interface->numberOfInterfaces) {
1410 ("Removed only address for host %" AFS_PTR_FMT " (%s:%d), no valid alternate interfaces, deleting host.\n",
1411 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1412 host->hostFlags |= HOSTDELETED;
1413 /* addr/port was removed from the hash table */
1417 rxconn = host->callback_rxcon;
1418 host->callback_rxcon = NULL;
1421 rx_DestroyConnection(rxconn);
1426 sc = rxnull_NewClientSecurityObject();
1427 host->callback_rxcon =
1428 rx_NewConnection(host->host, host->port, 1, sc, 0);
1429 rx_SetConnDeadTime(host->callback_rxcon, 50);
1430 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
1433 /* not the primary addr/port, just invalidate it */
1434 invalidateInterfaceAddr_r(host, addr, port);
1441 h_threadquota(int waiting)
1446 } else if (lwps > 32) {
1449 } else if (lwps > 16) {
1459 /* If found, host is returned with refCount incremented */
1461 h_GetHost_r(struct rx_connection *tcon)
1464 struct host *oldHost;
1466 struct interfaceAddr interf;
1467 int interfValid = 0;
1468 struct Identity *identP = NULL;
1471 char hoststr[16], hoststr2[16];
1473 struct rx_connection *cb_conn = NULL;
1474 struct rx_connection *cb_in = NULL;
1476 caps.Capabilities_val = NULL;
1478 haddr = rxr_HostOf(tcon);
1479 hport = rxr_PortOf(tcon);
1482 rx_DestroyConnection(cb_in);
1485 if (caps.Capabilities_val)
1486 free(caps.Capabilities_val);
1487 caps.Capabilities_val = NULL;
1488 caps.Capabilities_len = 0;
1491 if (h_Lookup_r(haddr, hport, &host))
1493 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1494 if (host && !identP && !(host->Console & 1)) {
1495 /* This is a new connection, and we already have a host
1496 * structure for this address. Verify that the identity
1497 * of the caller matches the identity in the host structure.
1499 if ((host->hostFlags & HWHO_INPROGRESS) &&
1500 h_threadquota(host->lock.num_waiting)) {
1506 if (!(host->hostFlags & ALTADDR)) {
1507 /* Another thread is doing initialization */
1510 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1511 host, afs_inet_ntoa_r(host->host, hoststr),
1512 ntohs(host->port)));
1516 host->hostFlags |= HWHO_INPROGRESS;
1517 host->hostFlags &= ~ALTADDR;
1519 /* We received a new connection from an IP address/port
1520 * that is associated with 'host' but the address/port of
1521 * the callback connection does not have to match it.
1522 * If there is a match, we can use the existing callback
1523 * connection to verify the UUID. If they do not match
1524 * we need to use a new callback connection to verify the
1525 * UUID of the incoming caller and perhaps use the old
1526 * callback connection to verify that the old address/port
1530 cb_conn = host->callback_rxcon;
1531 rx_GetConnection(cb_conn);
1533 if (haddr == host->host && hport == host->port) {
1534 /* The existing callback connection matches the
1535 * incoming connection so just use it.
1538 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1539 if (code == RXGEN_OPCODE)
1540 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1542 /* We do not have a match. Create a new connection
1543 * for the new addr/port and use multi_Rx to probe
1544 * both of them simultaneously.
1547 sc = rxnull_NewClientSecurityObject();
1548 cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
1549 rx_SetConnDeadTime(cb_in, 50);
1550 rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
1553 RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
1554 if (code == RXGEN_OPCODE)
1555 code = RXAFSCB_WhoAreYou(cb_in, &interf);
1557 rx_PutConnection(cb_conn);
1560 if ((code == RXGEN_OPCODE) ||
1561 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1562 identP = (struct Identity *)malloc(sizeof(struct Identity));
1564 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1568 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1569 if (cb_in == NULL) {
1570 /* The host on this connection was unable to respond to
1571 * the WhoAreYou. We will treat this as a new connection
1572 * from the existing host. The worst that can happen is
1573 * that we maintain some extra callback state information */
1574 if (host->interface) {
1576 ("Host %" AFS_PTR_FMT " (%s:%d) used to support WhoAreYou, deleting.\n",
1578 afs_inet_ntoa_r(host->host, hoststr),
1579 ntohs(host->port)));
1580 host->hostFlags |= HOSTDELETED;
1581 host->hostFlags &= ~HWHO_INPROGRESS;
1588 /* The incoming connection does not support WhoAreYou but
1589 * the original one might have. Use removeAddress_r() to
1590 * remove this addr/port from the host that was found.
1591 * If there are no more addresses left for the host it
1592 * will be deleted. Then we retry.
1594 removeAddress_r(host, haddr, hport);
1595 host->hostFlags &= ~HWHO_INPROGRESS;
1596 host->hostFlags |= ALTADDR;
1602 } else if (code == 0) {
1604 identP = (struct Identity *)malloc(sizeof(struct Identity));
1606 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1610 identP->uuid = interf.uuid;
1611 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1612 /* Check whether the UUID on this connection matches
1613 * the UUID in the host structure. If they don't match
1614 * then this is not the same host as before. */
1615 if (!host->interface
1616 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1619 ("Uuid doesn't match connection (%s:%d).\n",
1620 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport)));
1621 removeAddress_r(host, haddr, hport);
1624 ("Uuid doesn't match host %" AFS_PTR_FMT " (%s:%d).\n",
1625 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1627 removeAddress_r(host, host->host, host->port);
1629 host->hostFlags &= ~HWHO_INPROGRESS;
1630 host->hostFlags |= ALTADDR;
1636 /* the UUID matched the client at the incoming addr/port
1637 * but this is not the address of the active callback
1638 * connection. Try that connection and see if the client
1639 * is still there and if the reported UUID is the same.
1642 afsUUID uuid = host->interface->uuid;
1643 cb_conn = host->callback_rxcon;
1644 rx_GetConnection(cb_conn);
1645 rx_SetConnDeadTime(cb_conn, 2);
1646 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1648 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1650 rx_SetConnDeadTime(cb_conn, 50);
1651 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1652 rx_PutConnection(cb_conn);
1655 /* The primary address is either not responding or
1656 * is not the client we are looking for. Need to
1657 * remove the primary address and add swap in the new
1658 * callback connection, and destroy the old one.
1660 struct rx_connection *rxconn;
1661 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
1663 afs_inet_ntoa_r(host->host, hoststr),
1664 ntohs(host->port),code2));
1667 * make sure we add and then remove. otherwise, we
1668 * might end up with no valid interfaces after the
1669 * remove and the host will have been marked deleted.
1671 addInterfaceAddr_r(host, haddr, hport);
1672 removeInterfaceAddr_r(host, host->host, host->port);
1675 rxconn = host->callback_rxcon;
1676 host->callback_rxcon = cb_in;
1681 * If rx_DestroyConnection calls h_FreeConnection we
1682 * will deadlock on the host_glock_mutex. Work around
1683 * the problem by unhooking the client from the
1684 * connection before destroying the connection.
1686 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1687 rx_DestroyConnection(rxconn);
1693 /* A callback to the incoming connection address is failing.
1694 * Assume that the addr/port is no longer associated with the host
1695 * returned by h_Lookup_r.
1698 ("CB: WhoAreYou failed for connection (%s:%d) , error %d\n",
1699 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport), code));
1700 removeAddress_r(host, haddr, hport);
1701 host->hostFlags &= ~HWHO_INPROGRESS;
1702 host->hostFlags |= ALTADDR;
1706 rx_DestroyConnection(cb_in);
1711 ("CB: WhoAreYou failed for host %" AFS_PTR_FMT " (%s:%d), error %d\n",
1712 host, afs_inet_ntoa_r(host->host, hoststr),
1713 ntohs(host->port), code));
1714 host->hostFlags |= VENUSDOWN;
1717 if (caps.Capabilities_val
1718 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1719 host->hostFlags |= HERRORTRANS;
1721 host->hostFlags &= ~(HERRORTRANS);
1722 host->hostFlags |= ALTADDR;
1723 host->hostFlags &= ~HWHO_INPROGRESS;
1726 if (!(host->hostFlags & ALTADDR)) {
1727 /* another thread is doing the initialisation */
1729 ("Host %" AFS_PTR_FMT " (%s:%d) waiting for host-init to complete\n",
1730 host, afs_inet_ntoa_r(host->host, hoststr),
1731 ntohs(host->port)));
1735 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1736 host, afs_inet_ntoa_r(host->host, hoststr),
1737 ntohs(host->port)));
1741 /* We need to check whether the identity in the host structure
1742 * matches the identity on the connection. If they don't match
1743 * then treat this a new host. */
1744 if (!(host->Console & 1)
1745 && ((!identP->valid && host->interface)
1746 || (identP->valid && !host->interface)
1748 && !afs_uuid_equal(&identP->uuid,
1749 &host->interface->uuid)))) {
1750 char uuid1[128], uuid2[128];
1752 afsUUID_to_string(&identP->uuid, uuid1, 127);
1753 if (host->interface)
1754 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1756 ("CB: new identity for host %" AFS_PTR_FMT " (%s:%d), deleting(%x %x %s %s)\n",
1757 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1758 identP->valid, host->interface,
1759 identP->valid ? uuid1 : "no_uuid",
1760 host->interface ? uuid2 : "no_uuid"));
1762 /* The host in the cache is not the host for this connection */
1764 host->hostFlags |= HOSTDELETED;
1770 host = h_Alloc_r(tcon); /* returned held and locked */
1771 h_gethostcps_r(host, FT_ApproxTime());
1772 if (!(host->Console & 1)) {
1774 cb_conn = host->callback_rxcon;
1775 rx_GetConnection(cb_conn);
1776 host->hostFlags |= HWHO_INPROGRESS;
1779 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1780 if (code == RXGEN_OPCODE)
1781 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1782 rx_PutConnection(cb_conn);
1785 if ((code == RXGEN_OPCODE) ||
1786 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1789 (struct Identity *)malloc(sizeof(struct Identity));
1794 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1799 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1801 ("Host %" AFS_PTR_FMT " (%s:%d) does not support WhoAreYou.\n",
1802 host, afs_inet_ntoa_r(host->host, hoststr),
1803 ntohs(host->port)));
1805 } else if (code == 0) {
1808 (struct Identity *)malloc(sizeof(struct Identity));
1813 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1818 identP->uuid = interf.uuid;
1820 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1822 ("WhoAreYou success on host %" AFS_PTR_FMT " (%s:%d)\n",
1823 host, afs_inet_ntoa_r(host->host, hoststr),
1824 ntohs(host->port)));
1826 if (code == 0 && !identP->valid) {
1827 cb_conn = host->callback_rxcon;
1828 rx_GetConnection(cb_conn);
1830 code = RXAFSCB_InitCallBackState(cb_conn);
1831 rx_PutConnection(cb_conn);
1834 } else if (code == 0) {
1835 oldHost = h_LookupUuid_r(&identP->uuid);
1841 oldHost->hostFlags |= HWHO_INPROGRESS;
1843 if (oldHost->interface) {
1845 afsUUID uuid = oldHost->interface->uuid;
1846 cb_conn = oldHost->callback_rxcon;
1847 rx_GetConnection(cb_conn);
1848 rx_SetConnDeadTime(cb_conn, 2);
1849 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1851 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1853 rx_SetConnDeadTime(cb_conn, 50);
1854 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1855 rx_PutConnection(cb_conn);
1858 /* The primary address is either not responding or
1859 * is not the client we are looking for.
1860 * MultiProbeAlternateAddress_r() will remove the
1861 * alternate interfaces that do not have the same
1863 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
1865 afs_inet_ntoa_r(oldHost->host, hoststr),
1866 ntohs(oldHost->port),code2));
1867 MultiProbeAlternateAddress_r(oldHost);
1874 /* This is a new address for an existing host. Update
1875 * the list of interfaces for the existing host and
1876 * delete the host structure we just allocated. */
1878 /* prevent warnings while manipulating interface lists */
1879 host->hostFlags |= HOSTDELETED;
1881 if (oldHost->host != haddr || oldHost->port != hport) {
1882 struct rx_connection *rxconn;
1885 ("CB: Host %" AFS_PTR_FMT " (%s:%d) has new addr %s:%d\n",
1887 afs_inet_ntoa_r(oldHost->host, hoststr2),
1888 ntohs(oldHost->port),
1889 afs_inet_ntoa_r(haddr, hoststr),
1892 * add then remove. otherwise the host may get marked
1893 * deleted if we removed the only valid address.
1895 addInterfaceAddr_r(oldHost, haddr, hport);
1896 if (probefail || oldHost->host == haddr) {
1898 * The probe failed which means that the old
1899 * address is either unreachable or is not the
1900 * same host we were just contacted by. We will
1901 * also remove addresses if only the port has
1902 * changed because that indicates the client
1905 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
1908 struct Interface *interface = oldHost->interface;
1909 int number = oldHost->interface->numberOfInterfaces;
1910 for (i = 0; i < number; i++) {
1911 if (interface->interface[i].addr == haddr &&
1912 interface->interface[i].port != hport) {
1914 * We have just been contacted by a client
1915 * that has been seen from behind a NAT
1916 * and at least one other address.
1918 removeInterfaceAddr_r(oldHost, haddr,
1919 interface->interface[i].port);
1924 h_AddHostToAddrHashTable_r(haddr, hport, oldHost);
1925 oldHost->host = haddr;
1926 oldHost->port = hport;
1927 rxconn = oldHost->callback_rxcon;
1928 oldHost->callback_rxcon = host->callback_rxcon;
1929 host->callback_rxcon = rxconn;
1931 /* don't destroy rxconn here; let h_TossStuff_r
1932 * take care of that via h_Release_r below */
1934 host->hostFlags &= ~HWHO_INPROGRESS;
1936 /* release host because it was allocated by h_Alloc_r */
1939 /* the new host is held and locked */
1941 /* This really is a new host */
1942 h_AddHostToUuidHashTable_r(&identP->uuid, host);
1943 cb_conn = host->callback_rxcon;
1944 rx_GetConnection(cb_conn);
1947 RXAFSCB_InitCallBackState3(cb_conn,
1949 rx_PutConnection(cb_conn);
1954 ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
1955 host, afs_inet_ntoa_r(host->host, hoststr),
1956 ntohs(host->port)));
1957 assert(interfValid == 1);
1958 initInterfaceAddr_r(host, &interf);
1964 ("CB: RCallBackConnectBack failed for %" AFS_PTR_FMT " (%s:%d)\n",
1965 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1966 host->hostFlags |= VENUSDOWN;
1969 ("CB: RCallBackConnectBack succeeded for %" AFS_PTR_FMT " (%s:%d)\n",
1970 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1971 host->hostFlags |= RESETDONE;
1974 if (caps.Capabilities_val
1975 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1976 host->hostFlags |= HERRORTRANS;
1978 host->hostFlags &= ~(HERRORTRANS);
1979 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1980 host->hostFlags &= ~HWHO_INPROGRESS;
1985 if (caps.Capabilities_val)
1986 free(caps.Capabilities_val);
1987 caps.Capabilities_val = NULL;
1988 caps.Capabilities_len = 0;
1990 rx_DestroyConnection(cb_in);
1998 static char localcellname[PR_MAXNAMELEN + 1];
1999 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
2000 int num_lrealms = -1;
2004 h_InitHostPackage(void)
2006 memset(&nulluuid, 0, sizeof(afsUUID));
2007 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
2008 if (num_lrealms == -1) {
2010 for (i=0; i<AFS_NUM_LREALMS; i++) {
2011 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
2017 ("afs_krb_get_lrealm failed, using %s.\n",
2019 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
2025 /* initialize the rest of the local realms to nullstring for debugging */
2026 for (; i<AFS_NUM_LREALMS; i++)
2027 local_realms[i][0] = '\0';
2029 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
2030 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
2031 #ifdef AFS_PTHREAD_ENV
2032 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
2033 #endif /* AFS_PTHREAD_ENV */
2037 MapName_r(char *aname, char *acell, afs_int32 * aval)
2042 afs_int32 anamelen, cnamelen;
2046 anamelen = strlen(aname);
2047 if (anamelen >= PR_MAXNAMELEN)
2048 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
2050 lnames.namelist_len = 1;
2051 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
2052 lids.idlist_len = 0;
2053 lids.idlist_val = NULL;
2055 cnamelen = strlen(acell);
2057 if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
2059 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
2060 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
2061 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
2063 ("MapName: Name too long, using AnonymousID for %s@%s\n",
2065 *aval = AnonymousID;
2068 foreign = 1; /* attempt cross-cell authentication */
2069 tname = (char *)malloc(PR_MAXNAMELEN);
2071 ViceLog(0, ("Failed malloc in MapName_r\n"));
2074 strcpy(tname, aname);
2075 tname[anamelen] = '@';
2076 strcpy(tname + anamelen + 1, acell);
2077 lnames.namelist_val = (prname *) tname;
2082 code = hpr_NameToId(&lnames, &lids);
2085 if (lids.idlist_val) {
2086 *aval = lids.idlist_val[0];
2087 if (*aval == AnonymousID) {
2089 ("MapName: NameToId on %s returns anonymousID\n",
2090 lnames.namelist_val));
2092 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
2095 ("MapName: NameToId on '%s' is unknown\n",
2096 lnames.namelist_val));
2102 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
2110 /* NOTE: this returns the client with a Write lock and a refCount */
2112 h_ID2Client(afs_int32 vid)
2114 register struct client *client;
2115 register struct host *host;
2118 for (host = hostList; host; host = host->next) {
2119 if (host->hostFlags & HOSTDELETED)
2121 for (client = host->FirstClient; client; client = client->next) {
2122 if (!client->deleted && client->ViceId == vid) {
2125 ObtainWriteLock(&client->lock);
2136 * Called by the server main loop. Returns a h_Held client, which must be
2137 * released later the main loop. Allocates a client if the matching one
2138 * isn't around. The client is returned with its reference count incremented
2139 * by one. The caller must call h_ReleaseClient_r when finished with
2142 * The refCount on client->host is returned incremented. h_ReleaseClient_r
2143 * does not decrement the refCount on client->host.
2146 h_FindClient_r(struct rx_connection *tcon)
2148 register struct client *client;
2149 struct host *host = NULL;
2150 struct client *oldClient;
2151 afs_int32 viceid = 0;
2155 #if (64-MAXKTCNAMELEN)
2156 ticket name length != 64
2160 char uname[PR_MAXNAMELEN];
2161 char tcell[MAXKTCREALMLEN];
2165 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2166 if (client && client->sid == rxr_CidOf(tcon)
2167 && client->VenusEpoch == rxr_GetEpoch(tcon)) {
2169 h_Hold_r(client->host);
2170 if (!client->deleted && client->prfail != 2) {
2171 /* Could add shared lock on client here */
2172 /* note that we don't have to lock entry in this path to
2173 * ensure CPS is initialized, since we don't call rx_SetSpecific
2174 * until initialization is done, and we only get here if
2175 * rx_GetSpecific located the client structure.
2180 ObtainWriteLock(&client->lock); /* released at end */
2186 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
2188 ("FindClient: authenticating connection: authClass=%d\n",
2190 if (authClass == 1) {
2191 /* A bcrypt tickets, no longer supported */
2192 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2193 viceid = AnonymousID;
2194 expTime = 0x7fffffff;
2195 } else if (authClass == 2) {
2198 /* kerberos ticket */
2199 code = rxkad_GetServerInfo(tcon, /*level */ 0, (afs_uint32 *)&expTime,
2200 tname, tinst, tcell, &kvno);
2202 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2203 viceid = AnonymousID;
2204 expTime = 0x7fffffff;
2206 int ilen = strlen(tinst);
2208 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2209 tname, tinst, tcell, expTime, kvno));
2210 strncpy(uname, tname, sizeof(uname));
2212 if (strlen(uname) + 1 + ilen >= sizeof(uname))
2215 strcat(uname, tinst);
2217 /* translate the name to a vice id */
2218 code = MapName_r(uname, tcell, &viceid);
2222 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
2225 viceid = AnonymousID;
2226 expTime = 0x7fffffff;
2230 viceid = AnonymousID; /* unknown security class */
2231 expTime = 0x7fffffff;
2234 if (!client) { /* loop */
2235 host = h_GetHost_r(tcon); /* Returns with incremented refCount */
2241 /* First try to find the client structure */
2242 for (client = host->FirstClient; client; client = client->next) {
2243 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2244 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2247 ObtainWriteLock(&client->lock);
2253 /* Still no client structure - get one */
2256 /* Retry to find the client structure */
2257 for (client = host->FirstClient; client; client = client->next) {
2258 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2259 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2261 goto retryfirstclient;
2266 ObtainWriteLock(&client->lock);
2267 client->refCount = 1;
2268 client->host = host;
2269 #if FS_STATS_DETAILED
2270 client->InSameNetwork = host->InSameNetwork;
2271 #endif /* FS_STATS_DETAILED */
2272 client->ViceId = viceid;
2273 client->expTime = expTime; /* rx only */
2274 client->authClass = authClass; /* rx only */
2275 client->sid = rxr_CidOf(tcon);
2276 client->VenusEpoch = rxr_GetEpoch(tcon);
2277 client->CPS.prlist_val = NULL;
2278 client->CPS.prlist_len = 0;
2282 client->prfail = fail;
2284 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
2285 client->CPS.prlist_len = 0;
2286 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
2287 free(client->CPS.prlist_val);
2288 client->CPS.prlist_val = NULL;
2289 client->ViceId = viceid;
2290 client->expTime = expTime;
2292 if (viceid == ANONYMOUSID) {
2293 client->CPS.prlist_len = AnonCPS.prlist_len;
2294 client->CPS.prlist_val = AnonCPS.prlist_val;
2297 code = hpr_GetCPS(viceid, &client->CPS);
2302 ("pr_GetCPS failed(%d) for user %d, host %" AFS_PTR_FMT " (%s:%d)\n",
2303 code, viceid, client->host,
2304 afs_inet_ntoa_r(client->host->host,hoststr),
2305 ntohs(client->host->port)));
2307 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2308 * all protection servers and reevaluates things if no
2309 * sync server or quorum is found we could still end up
2310 * with one of these errors. In such case we would like to
2311 * reevaluate the rpc call to find if there's cps for this
2312 * guy. We treat other errors (except network failures
2313 * ones - i.e. code < 0) as an indication that there is no
2314 * CPS for this host. Ideally we could like to deal this
2315 * problem the other way around (i.e. if code == NOCPS
2316 * ignore else retry next time) but the problem is that
2317 * there're other errors (i.e. EPERM) for which we don't
2318 * want to retry and we don't know the whole code list!
2320 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2324 /* the disabling of system:administrators is so iffy and has so many
2325 * possible failure modes that we will disable it again */
2326 /* Turn off System:Administrator for safety
2327 * if (AL_IsAMember(SystemId, client->CPS) == 0)
2328 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
2331 /* Now, tcon may already be set to a rock, since we blocked with no host
2332 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2333 * required). So, before setting the RPC's rock, we should disconnect
2334 * the RPC from the other client structure's rock.
2336 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2337 if (oldClient && oldClient != client && oldClient->sid == rxr_CidOf(tcon)
2338 && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
2340 if (!oldClient->deleted) {
2341 /* if we didn't create it, it's not ours to put back */
2343 ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n",
2344 client, client->sid, tcon,
2345 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2346 ntohs(rxr_PortOf(tcon)),
2347 oldClient, oldClient->sid));
2348 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2349 free(client->CPS.prlist_val);
2350 client->CPS.prlist_val = NULL;
2351 client->CPS.prlist_len = 0;
2353 /* We should perhaps check for 0 here */
2355 ReleaseWriteLock(&client->lock);
2360 oldClient->refCount++;
2362 ObtainWriteLock(&oldClient->lock);
2366 ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
2367 oldClient, oldClient->sid, tcon,
2368 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2369 ntohs(rxr_PortOf(tcon)),
2370 client, client->sid));
2371 /* rx_SetSpecific will be done immediately below */
2374 /* Avoid chaining in more than once. */
2377 client->next = host->FirstClient;
2378 host->FirstClient = client;
2380 CurrentConnections++; /* increment number of connections */
2382 rx_SetSpecific(tcon, rxcon_client_key, client);
2383 ReleaseWriteLock(&client->lock);
2387 } /*h_FindClient_r */
2390 h_ReleaseClient_r(struct client *client)
2392 assert(client->refCount > 0);
2399 * Sigh: this one is used to get the client AGAIN within the individual
2400 * server routines. This does not bother h_Holding the host, since
2401 * this is assumed already have been done by the server main loop.
2402 * It does check tokens, since only the server routines can return the
2403 * VICETOKENDEAD error code
2406 GetClient(struct rx_connection *tcon, struct client **cp)
2408 register struct client *client;
2413 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2414 if (client == NULL) {
2416 ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
2417 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2418 ntohs(rxr_PortOf(tcon))));
2422 if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
2424 ("GetClient: tcon %x tcon sid %d client sid %d\n",
2425 tcon, rxr_CidOf(tcon), client->sid));
2429 if (client && client->LastCall > client->expTime && client->expTime) {
2431 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2432 afs_inet_ntoa_r(client->host->host, hoststr),
2433 ntohs(client->host->port), client->expTime));
2435 return VICETOKENDEAD;
2445 PutClient(struct client **cp)
2451 h_ReleaseClient_r(*cp);
2458 /* Client user name for short term use. Note that this is NOT inexpensive */
2460 h_UserName(struct client *client)
2462 static char User[PR_MAXNAMELEN + 1];
2466 lids.idlist_len = 1;
2467 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
2468 if (!lids.idlist_val) {
2469 ViceLog(0, ("Failed malloc in h_UserName\n"));
2472 lnames.namelist_len = 0;
2473 lnames.namelist_val = (prname *) 0;
2474 lids.idlist_val[0] = client->ViceId;
2475 if (hpr_IdToName(&lids, &lnames)) {
2476 /* We need to free id we alloced above! */
2477 free(lids.idlist_val);
2478 return "*UNKNOWN USER NAME*";
2480 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2481 free(lids.idlist_val);
2482 free(lnames.namelist_val);
2491 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2492 CEs, CEBlocks, HTs, HTBlocks));
2498 h_PrintClient(register struct host *host, int flags, void *rock)
2500 StreamHandle_t *file = (StreamHandle_t *)rock;
2501 register struct client *client;
2506 time_t LastCall, expTime;
2509 LastCall = host->LastCall;
2510 if (host->hostFlags & HOSTDELETED) {
2514 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2515 "Host %s:%d down = %d, LastCall %s",
2516 afs_inet_ntoa_r(host->host, hoststr),
2517 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2518 afs_ctime(&LastCall, tbuffer,
2520 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2521 for (client = host->FirstClient; client; client = client->next) {
2522 if (!client->deleted) {
2523 expTime = client->expTime;
2524 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2525 " user id=%d, name=%s, sl=%s till %s",
2526 client->ViceId, h_UserName(client),
2528 authClass ? "Authenticated" :
2529 "Not authenticated",
2531 authClass ? afs_ctime(&expTime, tbuffer,
2534 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2535 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2536 client->CPS.prlist_len);
2537 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2538 if (client->CPS.prlist_val) {
2539 for (i = 0; i > client->CPS.prlist_len; i++) {
2540 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2541 client->CPS.prlist_val[i]);
2542 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2545 sprintf(tmpStr, "]\n");
2546 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2552 } /*h_PrintClient */
2557 * Print a list of clients, with last security level and token value seen,
2561 h_PrintClients(void)
2567 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2571 ("Couldn't create client dump file %s\n",
2572 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2575 now = FT_ApproxTime();
2576 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
2577 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2578 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2579 h_Enumerate(h_PrintClient, (char *)file);
2580 STREAM_REALLYCLOSE(file);
2581 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2588 h_DumpHost(register struct host *host, int flags, void *rock)
2590 StreamHandle_t *file = (StreamHandle_t *)rock;
2597 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2598 "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 [",
2599 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
2600 host->cblist, CheckLock(&host->lock), host->LastCall,
2601 host->ActiveCall, (host->hostFlags & VENUSDOWN),
2602 host->hostFlags & HOSTDELETED, host->Console,
2603 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2605 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2606 if (host->hcps.prlist_val)
2607 for (i = 0; i < host->hcps.prlist_len; i++) {
2608 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2609 host->hcps.prlist_val[i]);
2610 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2612 sprintf(tmpStr, "] [");
2613 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2614 if (host->interface)
2615 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2617 sprintf(tmpStr, " %s:%d",
2618 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2619 ntohs(host->interface->interface[i].port));
2620 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2622 sprintf(tmpStr, "] refCount: %d\n", host->refCount);
2623 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2635 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2641 ("Couldn't create host dump file %s\n",
2642 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2645 now = FT_ApproxTime();
2646 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
2647 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2648 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2649 h_Enumerate(h_DumpHost, (char *)file);
2650 STREAM_REALLYCLOSE(file);
2651 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2655 #ifdef AFS_DEMAND_ATTACH_FS
2658 * host state serialization
2660 static int h_stateFillHeader(struct host_state_header * hdr);
2661 static int h_stateCheckHeader(struct host_state_header * hdr);
2662 static int h_stateAllocMap(struct fs_dump_state * state);
2663 static int h_stateSaveHost(struct host * host, int flags, void *rock);
2664 static int h_stateRestoreHost(struct fs_dump_state * state);
2665 static int h_stateRestoreIndex(struct host * h, int flags, void *rock);
2666 static int h_stateVerifyHost(struct host * h, int flags, void *rock);
2667 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port);
2668 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2669 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2670 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2673 /* this procedure saves all host state to disk for fast startup */
2675 h_stateSave(struct fs_dump_state * state)
2677 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2680 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2682 /* invalidate host state header */
2683 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2685 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2686 sizeof(struct host_state_header))) {
2691 fs_stateIncEOF(state, sizeof(struct host_state_header));
2693 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2698 h_stateFillHeader(state->h_hdr);
2700 /* write the real header to disk */
2701 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2702 sizeof(struct host_state_header));
2709 * host state serialization
2711 * this procedure restores all host state from a disk for fast startup
2714 h_stateRestore(struct fs_dump_state * state)
2718 /* seek to the right position and read in the host state header */
2719 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2720 sizeof(struct host_state_header))) {
2725 /* check the validity of the header */
2726 if (h_stateCheckHeader(state->h_hdr)) {
2731 records = state->h_hdr->records;
2733 if (h_stateAllocMap(state)) {
2738 /* iterate over records restoring host state */
2739 for (i=0; i < records; i++) {
2740 if (h_stateRestoreHost(state) != 0) {
2751 h_stateRestoreIndices(struct fs_dump_state * state)
2753 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
2758 h_stateRestoreIndex(struct host * h, int flags, void *rock)
2760 struct fs_dump_state *state = (struct fs_dump_state *)rock;
2761 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
2762 return H_ENUMERATE_BAIL(flags);
2768 h_stateVerify(struct fs_dump_state * state)
2770 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
2775 h_stateVerifyHost(struct host * h, int flags, void* rock)
2777 struct fs_dump_state *state = (struct fs_dump_state *)rock;
2781 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
2782 return H_ENUMERATE_BAIL(flags);
2786 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
2787 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
2788 h->interface->interface[i].port)) {
2792 if (h_stateVerifyUuidHash(state, h)) {
2795 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port)) {
2799 if (cb_stateVerifyHCBList(state, h)) {
2807 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port)
2809 int ret = 0, found = 0;
2810 struct host *host = NULL;
2811 struct h_AddrHashChain *chain;
2812 int index = h_HashIndex(addr);
2816 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
2817 host = chain->hostPtr;
2819 afs_inet_ntoa_r(addr, tmp);
2820 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
2824 if ((chain->addr == addr) && (chain->port == port)) {
2826 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry points to different host struct (%d, %d)\n",
2827 h->index, host->index));
2828 state->flags.warnings_generated = 1;
2833 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
2834 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2835 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
2843 afs_inet_ntoa_r(addr, tmp);
2844 if (state->mode == FS_STATE_LOAD_MODE) {
2845 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s not found in hash\n", tmp));
2849 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s not found in hash\n", tmp));
2850 state->flags.warnings_generated = 1;
2859 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
2861 int ret = 0, found = 0;
2862 struct host *host = NULL;
2863 struct h_UuidHashChain *chain;
2864 afsUUID * uuidp = &h->interface->uuid;
2865 int index = h_UuidHashIndex(uuidp);
2869 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
2870 host = chain->hostPtr;
2872 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2873 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
2877 if (host->interface &&
2878 afs_uuid_equal(&host->interface->uuid, uuidp)) {
2880 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
2881 h->index, host->index));
2882 state->flags.warnings_generated = 1;
2887 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
2888 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2889 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
2897 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2898 if (state->mode == FS_STATE_LOAD_MODE) {
2899 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
2903 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
2904 state->flags.warnings_generated = 1;
2912 /* create the host state header structure */
2914 h_stateFillHeader(struct host_state_header * hdr)
2916 hdr->stamp.magic = HOST_STATE_MAGIC;
2917 hdr->stamp.version = HOST_STATE_VERSION;
2921 /* check the contents of the host state header structure */
2923 h_stateCheckHeader(struct host_state_header * hdr)
2927 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
2928 ViceLog(0, ("check_host_state_header: invalid state header\n"));
2931 else if (hdr->stamp.version != HOST_STATE_VERSION) {
2932 ViceLog(0, ("check_host_state_header: unknown version number\n"));
2938 /* allocate the host id mapping table */
2940 h_stateAllocMap(struct fs_dump_state * state)
2942 state->h_map.len = state->h_hdr->index_max + 1;
2943 state->h_map.entries = (struct idx_map_entry_t *)
2944 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
2945 return (state->h_map.entries != NULL) ? 0 : 1;
2948 /* function called by h_Enumerate to save a host to disk */
2950 h_stateSaveHost(struct host * host, int flags, void* rock)
2952 struct fs_dump_state *state = (struct fs_dump_state *) rock;
2953 int if_len=0, hcps_len=0;
2954 struct hostDiskEntry hdsk;
2955 struct host_state_entry_header hdr;
2956 struct Interface * ifp = NULL;
2957 afs_int32 * hcps = NULL;
2958 struct iovec iov[4];
2961 memset(&hdr, 0, sizeof(hdr));
2963 if (state->h_hdr->index_max < host->index) {
2964 state->h_hdr->index_max = host->index;
2967 h_hostToDiskEntry_r(host, &hdsk);
2968 if (host->interface) {
2969 if_len = sizeof(struct Interface) +
2970 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
2971 ifp = (struct Interface *) malloc(if_len);
2972 assert(ifp != NULL);
2973 memcpy(ifp, host->interface, if_len);
2974 hdr.interfaces = host->interface->numberOfInterfaces;
2975 iov[iovcnt].iov_base = (char *) ifp;
2976 iov[iovcnt].iov_len = if_len;
2979 if (host->hcps.prlist_val) {
2980 hdr.hcps = host->hcps.prlist_len;
2981 hcps_len = hdr.hcps * sizeof(afs_int32);
2982 hcps = (afs_int32 *) malloc(hcps_len);
2983 assert(hcps != NULL);
2984 memcpy(hcps, host->hcps.prlist_val, hcps_len);
2985 iov[iovcnt].iov_base = (char *) hcps;
2986 iov[iovcnt].iov_len = hcps_len;
2990 if (hdsk.index > state->h_hdr->index_max)
2991 state->h_hdr->index_max = hdsk.index;
2993 hdr.len = sizeof(struct host_state_entry_header) +
2994 sizeof(struct hostDiskEntry) + if_len + hcps_len;
2995 hdr.magic = HOST_STATE_ENTRY_MAGIC;
2997 iov[0].iov_base = (char *) &hdr;
2998 iov[0].iov_len = sizeof(hdr);
2999 iov[1].iov_base = (char *) &hdsk;
3000 iov[1].iov_len = sizeof(struct hostDiskEntry);
3002 if (fs_stateWriteV(state, iov, iovcnt)) {
3003 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
3007 fs_stateIncEOF(state, hdr.len);
3009 state->h_hdr->records++;
3016 return H_ENUMERATE_BAIL(flags);
3021 /* restores a host from disk */
3023 h_stateRestoreHost(struct fs_dump_state * state)
3025 int ifp_len=0, hcps_len=0, bail=0;
3026 struct host_state_entry_header hdr;
3027 struct hostDiskEntry hdsk;
3028 struct host *host = NULL;
3029 struct Interface *ifp = NULL;
3030 afs_int32 * hcps = NULL;
3031 struct iovec iov[3];
3034 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
3035 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
3041 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
3042 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
3048 iov[0].iov_base = (char *) &hdsk;
3049 iov[0].iov_len = sizeof(struct hostDiskEntry);
3051 if (hdr.interfaces) {
3052 ifp_len = sizeof(struct Interface) +
3053 ((hdr.interfaces-1) * sizeof(struct AddrPort));
3054 ifp = (struct Interface *) malloc(ifp_len);
3055 assert(ifp != NULL);
3056 iov[iovcnt].iov_base = (char *) ifp;
3057 iov[iovcnt].iov_len = ifp_len;
3061 hcps_len = hdr.hcps * sizeof(afs_int32);
3062 hcps = (afs_int32 *) malloc(hcps_len);
3063 assert(hcps != NULL);
3064 iov[iovcnt].iov_base = (char *) hcps;
3065 iov[iovcnt].iov_len = hcps_len;
3069 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
3070 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
3075 if (fs_stateReadV(state, iov, iovcnt)) {
3076 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
3081 if (!hdr.hcps && hdsk.hcps_valid) {
3082 /* valid, zero-length host cps ; does this ever happen? */
3083 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
3084 assert(hcps != NULL);
3088 assert(host != NULL);
3091 host->interface = ifp;
3094 host->hcps.prlist_val = hcps;
3095 host->hcps.prlist_len = hdr.hcps;
3098 h_diskEntryToHost_r(&hdsk, host);
3099 h_SetupCallbackConn_r(host);
3101 h_AddHostToAddrHashTable_r(host->host, host->port, host);
3104 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
3105 if (ifp->interface[i].valid &&
3106 !(ifp->interface[i].addr == host->host &&
3107 ifp->interface[i].port == host->port)) {
3108 h_AddHostToAddrHashTable_r(ifp->interface[i].addr,
3109 ifp->interface[i].port,
3113 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
3115 h_InsertList_r(host);
3117 /* setup host id map entry */
3118 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
3119 state->h_map.entries[hdsk.index].new_idx = host->index;
3131 /* serialize a host structure to disk */
3133 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
3135 out->host = in->host;
3136 out->port = in->port;
3137 out->hostFlags = in->hostFlags;
3138 out->Console = in->Console;
3139 out->hcpsfailed = in->hcpsfailed;
3140 out->LastCall = in->LastCall;
3141 out->ActiveCall = in->ActiveCall;
3142 out->cpsCall = in->cpsCall;
3143 out->cblist = in->cblist;
3144 #ifdef FS_STATS_DETAILED
3145 out->InSameNetwork = in->InSameNetwork;
3148 /* special fields we save, but are not memcpy'd back on restore */
3149 out->index = in->index;
3150 out->hcps_len = in->hcps.prlist_len;
3151 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
3154 /* restore a host structure from disk */
3156 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
3158 out->host = in->host;
3159 out->port = in->port;
3160 out->hostFlags = in->hostFlags;
3161 out->Console = in->Console;
3162 out->hcpsfailed = in->hcpsfailed;
3163 out->LastCall = in->LastCall;
3164 out->ActiveCall = in->ActiveCall;
3165 out->cpsCall = in->cpsCall;
3166 out->cblist = in->cblist;
3167 #ifdef FS_STATS_DETAILED
3168 out->InSameNetwork = in->InSameNetwork;
3172 /* index translation routines */
3174 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
3178 /* hosts use a zero-based index, so old==0 is valid */
3180 if (old >= state->h_map.len) {
3181 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
3183 } else if (state->h_map.entries[old].old_idx != old) { /* sanity check */
3184 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
3187 *new = state->h_map.entries[old].new_idx;
3192 #endif /* AFS_DEMAND_ATTACH_FS */
3196 * This counts the number of workstations, the number of active workstations,
3197 * and the number of workstations declared "down" (i.e. not heard from
3198 * recently). An active workstation has received a call since the cutoff
3199 * time argument passed.
3202 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3204 register struct host *host;
3205 register int num = 0, active = 0, del = 0;
3208 for (host = hostList; host; host = host->next) {
3209 if (!(host->hostFlags & HOSTDELETED)) {
3211 if (host->ActiveCall > cutofftime)
3213 if (host->hostFlags & VENUSDOWN)
3225 } /*h_GetWorkStats */
3228 /*------------------------------------------------------------------------
3229 * PRIVATE h_ClassifyAddress
3232 * Given a target IP address and a candidate IP address (both
3233 * in host byte order), classify the candidate into one of three
3234 * buckets in relation to the target by bumping the counters passed
3238 * a_targetAddr : Target address.
3239 * a_candAddr : Candidate address.
3240 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3241 * addresses are either in the same network
3242 * or the same subnet.
3243 * a_diffSubnetP : ...when the candidate is in a different
3245 * a_diffNetworkP : ...when the candidate is in a different
3252 * The target and candidate addresses are both in host byte
3253 * order, NOT network byte order, when passed in.
3257 *------------------------------------------------------------------------*/
3260 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3261 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3262 afs_int32 * a_diffNetworkP)
3263 { /*h_ClassifyAddress */
3265 afs_uint32 targetNet;
3266 afs_uint32 targetSubnet;
3268 afs_uint32 candSubnet;
3271 * Put bad values into the subnet info to start with.
3273 targetSubnet = (afs_uint32) 0;
3274 candSubnet = (afs_uint32) 0;
3277 * Pull out the network and subnetwork numbers from the target
3278 * and candidate addresses. We can short-circuit this whole
3279 * affair if the target and candidate addresses are not of the
3282 if (IN_CLASSA(a_targetAddr)) {
3283 if (!(IN_CLASSA(a_candAddr))) {
3284 (*a_diffNetworkP)++;
3287 targetNet = a_targetAddr & IN_CLASSA_NET;
3288 candNet = a_candAddr & IN_CLASSA_NET;
3289 if (IN_SUBNETA(a_targetAddr))
3290 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
3291 if (IN_SUBNETA(a_candAddr))
3292 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
3293 } else if (IN_CLASSB(a_targetAddr)) {
3294 if (!(IN_CLASSB(a_candAddr))) {
3295 (*a_diffNetworkP)++;
3298 targetNet = a_targetAddr & IN_CLASSB_NET;
3299 candNet = a_candAddr & IN_CLASSB_NET;
3300 if (IN_SUBNETB(a_targetAddr))
3301 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
3302 if (IN_SUBNETB(a_candAddr))
3303 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
3304 } /*Class B target */
3305 else if (IN_CLASSC(a_targetAddr)) {
3306 if (!(IN_CLASSC(a_candAddr))) {
3307 (*a_diffNetworkP)++;
3310 targetNet = a_targetAddr & IN_CLASSC_NET;
3311 candNet = a_candAddr & IN_CLASSC_NET;
3314 * Note that class C addresses can't have subnets,
3315 * so we leave the defaults untouched.
3317 } /*Class C target */
3319 targetNet = a_targetAddr;
3320 candNet = a_candAddr;
3321 } /*Class D address */
3324 * Now, simply compare the extracted net and subnet values for
3325 * the two addresses (which at this point are known to be of the
3328 if (targetNet == candNet) {
3329 if (targetSubnet == candSubnet)
3330 (*a_sameNetOrSubnetP)++;
3334 (*a_diffNetworkP)++;
3336 } /*h_ClassifyAddress */
3339 /*------------------------------------------------------------------------
3340 * EXPORTED h_GetHostNetStats
3343 * Iterate through the host table, and classify each (non-deleted)
3344 * host entry into ``proximity'' categories (same net or subnet,
3345 * different subnet, different network).
3348 * a_numHostsP : Set to total number of (non-deleted) hosts.
3349 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
3350 * a_diffSubnetP : Set to # hosts on diff subnet as server.
3351 * a_diffNetworkP : Set to # hosts on diff network as server.
3357 * We only count non-deleted hosts. The storage pointed to by our
3358 * parameters is zeroed upon entry.
3362 *------------------------------------------------------------------------*/
3365 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
3366 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
3367 { /*h_GetHostNetStats */
3369 register struct host *hostP; /*Ptr to current host entry */
3370 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
3373 * Clear out the storage pointed to by our parameters.
3375 *a_numHostsP = (afs_int32) 0;
3376 *a_sameNetOrSubnetP = (afs_int32) 0;
3377 *a_diffSubnetP = (afs_int32) 0;
3378 *a_diffNetworkP = (afs_int32) 0;
3381 for (hostP = hostList; hostP; hostP = hostP->next) {
3382 if (!(hostP->hostFlags & HOSTDELETED)) {
3384 * Bump the number of undeleted host entries found.
3385 * In classifying the current entry's address, make
3386 * sure to first convert to host byte order.
3389 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
3390 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
3391 a_sameNetOrSubnetP, a_diffSubnetP,
3393 } /*Only look at non-deleted hosts */
3394 } /*For each host record hashed to this index */
3396 } /*h_GetHostNetStats */
3398 static afs_uint32 checktime;
3399 static afs_uint32 clientdeletetime;
3400 static struct AFSFid zerofid;
3404 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
3405 * Since it can serialize them, and pile up, it should be a separate LWP
3406 * from other events.
3410 CheckHost(register struct host *host, int flags, void *rock)
3412 register struct client *client;
3413 struct rx_connection *cb_conn = NULL;
3416 #ifdef AFS_DEMAND_ATTACH_FS
3417 /* kill the checkhost lwp ASAP during shutdown */
3419 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3421 return H_ENUMERATE_BAIL(flags);
3426 /* Host is held by h_Enumerate */
3428 for (client = host->FirstClient; client; client = client->next) {
3429 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3430 client->deleted = 1;
3431 host->hostFlags |= CLIENTDELETED;
3434 if (host->LastCall < checktime) {
3436 host->hostFlags |= HWHO_INPROGRESS;
3437 if (!(host->hostFlags & HOSTDELETED)) {
3438 cb_conn = host->callback_rxcon;
3439 rx_GetConnection(cb_conn);
3440 if (host->LastCall < clientdeletetime) {
3441 host->hostFlags |= HOSTDELETED;
3442 if (!(host->hostFlags & VENUSDOWN)) {
3443 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3444 if (host->interface) {
3447 RXAFSCB_InitCallBackState3(cb_conn,
3453 RXAFSCB_InitCallBackState(cb_conn);
3456 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3459 (void)afs_inet_ntoa_r(host->host, hoststr);
3461 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3462 hoststr, ntohs(host->port)));
3463 host->hostFlags |= VENUSDOWN;
3465 /* Note: it's safe to delete hosts even if they have call
3466 * back state, because break delayed callbacks (called when a
3467 * message is received from the workstation) will always send a
3468 * break all call backs to the workstation if there is no
3473 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3475 (void)afs_inet_ntoa_r(host->host, hoststr);
3476 if (host->interface) {
3477 afsUUID uuid = host->interface->uuid;
3479 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3482 if (MultiProbeAlternateAddress_r(host)) {
3483 ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
3484 hoststr, ntohs(host->port), code));
3485 host->hostFlags |= VENUSDOWN;
3490 code = RXAFSCB_Probe(cb_conn);
3494 ("CheckHost: Probe failed for host %s:%d, code %d\n",
3495 hoststr, ntohs(host->port), code));
3496 host->hostFlags |= VENUSDOWN;
3502 rx_PutConnection(cb_conn);
3506 host->hostFlags &= ~HWHO_INPROGRESS;
3516 CheckHost_r(register struct host *host, int flags, void *dummy)
3518 register struct client *client;
3519 struct rx_connection *cb_conn = NULL;
3522 #ifdef AFS_DEMAND_ATTACH_FS
3523 /* kill the checkhost lwp ASAP during shutdown */
3525 if (fs_state.mode == FS_MODE_SHUTDOWN) {
3527 return H_ENUMERATE_BAIL(flags);
3532 /* Host is held by h_Enumerate_r */
3533 for (client = host->FirstClient; client; client = client->next) {
3534 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
3535 client->deleted = 1;
3536 host->hostFlags |= CLIENTDELETED;
3539 if (host->LastCall < checktime) {
3541 if (!(host->hostFlags & HOSTDELETED)) {
3542 cb_conn = host->callback_rxcon;
3543 rx_GetConnection(cb_conn);
3544 if (host->LastCall < clientdeletetime) {
3545 host->hostFlags |= HOSTDELETED;
3546 if (!(host->hostFlags & VENUSDOWN)) {
3547 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
3548 if (host->interface) {
3551 RXAFSCB_InitCallBackState3(cb_conn,
3557 RXAFSCB_InitCallBackState(cb_conn);
3560 host->hostFlags |= ALTADDR; /* alternate addresses valid */
3563 (void)afs_inet_ntoa_r(host->host, hoststr);
3565 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
3566 hoststr, ntohs(host->port)));
3567 host->hostFlags |= VENUSDOWN;
3569 /* Note: it's safe to delete hosts even if they have call
3570 * back state, because break delayed callbacks (called when a
3571 * message is received from the workstation) will always send a
3572 * break all call backs to the workstation if there is no
3577 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
3579 (void)afs_inet_ntoa_r(host->host, hoststr);
3580 if (host->interface) {
3581 afsUUID uuid = host->interface->uuid;
3583 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
3586 if (MultiProbeAlternateAddress_r(host)) {
3587 ViceLog(0,("CheckHost_r: Probing all interfaces of host %s:%d failed, code %d\n",
3588 hoststr, ntohs(host->port), code));
3589 host->hostFlags |= VENUSDOWN;
3594 code = RXAFSCB_Probe(cb_conn);
3598 ("CheckHost_r: Probe failed for host %s:%d, code %d\n",
3599 hoststr, ntohs(host->port), code));
3600 host->hostFlags |= VENUSDOWN;
3606 rx_PutConnection(cb_conn);
3618 * Set VenusDown for any hosts that have not had a call in 15 minutes and
3619 * don't respond to a probe. Note that VenusDown can only be cleared if
3620 * a message is received from the host (see ServerLWP in file.c).
3621 * Delete hosts that have not had any calls in 1 hour, clients that
3622 * have not had any calls in 15 minutes.
3624 * This routine is called roughly every 5 minutes.
3629 afs_uint32 now = FT_ApproxTime();
3631 memset(&zerofid, 0, sizeof(zerofid));
3633 * Send a probe to the workstation if it hasn't been heard from in
3636 checktime = now - 15 * 60;
3637 clientdeletetime = now - 120 * 60; /* 2 hours ago */
3640 h_Enumerate_r(CheckHost_r, hostList, NULL);
3645 * This is called with host locked and held. At this point, the
3646 * hostAddrHashTable has an entry for the primary addr/port inserted
3647 * by h_Alloc_r(). No other interfaces should be considered valid.
3649 * The addresses in the interfaceAddr list are in host byte order.
3652 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
3659 struct Interface *interface;
3662 afs_uint16 port7001 = htons(7001);
3667 number = interf->numberOfInterfaces;
3668 myAddr = host->host; /* current interface address */
3669 myPort = host->port; /* current port */
3672 ("initInterfaceAddr : host %s:%d numAddr %d\n",
3673 afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
3675 /* validation checks */
3676 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
3678 ("Invalid number of alternate addresses is %d\n", number));
3683 * The client's notion of its own IP addresses is not reliable.
3685 * 1. The client list might contain private address ranges which
3686 * are likely to be re-used by many clients allocated addresses
3689 * 2. The client list will not include any public addresses that
3690 * are hidden by a NAT.
3692 * 3. Private address ranges that are exposed to the server will
3693 * be obtained from the rx connections that use them.
3695 * 4. Lists provided by the client are not necessarily truthful.
3696 * Many existing clients (UNIX) do not refresh the IP address
3697 * list as the actual assigned addresses change. The end result
3698 * is that they report the initial address list for the lifetime
3699 * of the process. In other words, a client can report addresses
3700 * that they are in fact not using. Adding these addresses to
3701 * the host interface list without verification is not only
3702 * pointless, it is downright dangerous.
3704 * We therefore do not add alternate addresses to the addr hash table.
3705 * We only use them for multi-rx callback breaks.
3709 * Convert IP addresses to network byte order, and remove
3710 * duplicate IP addresses from the interface list, and
3711 * determine whether or not the incoming addr/port is
3712 * listed. Note that if the address matches it is not
3713 * truly a match because the port number for the entries
3714 * in the interface list are port 7001 and the port number
3715 * for this connection might not be 7001.
3717 for (i = 0, count = 0, found = 0; i < number; i++) {
3718 interf->addr_in[i] = htonl(interf->addr_in[i]);
3719 for (j = 0; j < count; j++) {
3720 if (interf->addr_in[j] == interf->addr_in[i])
3724 interf->addr_in[count] = interf->addr_in[i];
3725 if (interf->addr_in[count] == myAddr &&
3733 * Allocate and initialize an interface structure for this host.
3736 interface = (struct Interface *)
3737 malloc(sizeof(struct Interface) +
3738 (sizeof(struct AddrPort) * (count - 1)));
3740 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 1\n"));
3743 interface->numberOfInterfaces = count;
3745 interface = (struct Interface *)
3746 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
3748 ViceLog(0, ("Failed malloc in initInterfaceAddr_r 2\n"));
3751 interface->numberOfInterfaces = count + 1;
3752 interface->interface[count].addr = myAddr;
3753 interface->interface[count].port = myPort;
3754 interface->interface[count].valid = 1;
3757 for (i = 0; i < count; i++) {
3759 interface->interface[i].addr = interf->addr_in[i];
3760 /* We store the port as 7001 because the addresses reported by
3761 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
3762 * are coming from fully connected hosts (no NAT/PATs)
3764 interface->interface[i].port = port7001;
3765 interface->interface[i].valid =
3766 (interf->addr_in[i] == myAddr && port7001 == myPort) ? 1 : 0;
3769 interface->uuid = interf->uuid;
3771 assert(!host->interface);
3772 host->interface = interface;
3774 if (LogLevel >= 125) {
3775 afsUUID_to_string(&interface->uuid, uuidstr, 127);
3777 ViceLog(125, ("--- uuid %s\n", uuidstr));
3778 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
3779 ViceLog(125, ("--- alt address %s:%d\n",
3780 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3781 ntohs(host->interface->interface[i].port)));
3788 /* deleted a HashChain structure for this address and host */
3789 /* returns 1 on success */
3791 h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port,
3795 register struct h_AddrHashChain **hp, *th;
3797 if (addr == 0 && port == 0)
3800 for (hp = &hostAddrHashTable[h_HashIndex(addr)]; (th = *hp);
3802 assert(th->hostPtr);
3803 if (th->hostPtr == host && th->addr == addr && th->port == port) {
3804 ViceLog(125, ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d)\n",
3805 host, afs_inet_ntoa_r(host->host, hoststr),
3806 ntohs(host->port)));
3813 ("h_DeleteHostFromAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) not found\n",
3814 host, afs_inet_ntoa_r(host->host, hoststr),
3815 ntohs(host->port)));
3821 ** prints out all alternate interface address for the host. The 'level'
3822 ** parameter indicates what level of debugging sets this output
3825 printInterfaceAddr(struct host *host, int level)
3830 if (host->interface) {
3831 /* check alternate addresses */
3832 number = host->interface->numberOfInterfaces;
3834 ViceLog(level, ("no-addresses "));
3836 for (i = 0; i < number; i++)
3837 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3838 ntohs(host->interface->interface[i].port)));
3841 ViceLog(level, ("\n"));