2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
9 * Portions Copyright (c) 2006 Sine Nomine Associates
12 #include <afsconfig.h>
13 #include <afs/param.h>
19 #ifdef HAVE_SYS_FILE_H
26 #include <afs/afsint.h>
27 #define FSINT_COMMON_XG
28 #include <afs/afscbint.h>
29 #include <afs/rxgen_consts.h>
31 #include <afs/errors.h>
32 #include <afs/ihandle.h>
33 #include <afs/vnode.h>
34 #include <afs/volume.h>
36 #include <afs/ptclient.h>
37 #include <afs/ptuser.h>
38 #include <afs/prs_fs.h>
40 #include <afs/afsutil.h>
41 #include <afs/com_err.h>
43 #include <afs/cellconfig.h>
44 #include "viced_prototypes.h"
48 #ifdef AFS_DEMAND_ATTACH_FS
49 #include "../util/afsutil_prototypes.h"
50 #include "serialize_state.h"
51 #endif /* AFS_DEMAND_ATTACH_FS */
53 pthread_mutex_t host_glock_mutex;
56 extern int CurrentConnections;
58 extern int AnonymousID;
59 extern prlist AnonCPS;
61 extern struct afsconf_dir *confDir; /* config dir object */
62 extern int lwps; /* the max number of server threads */
63 extern afsUUID FS_HostUUID;
66 int CEs = 0; /* active clients */
67 int CEBlocks = 0; /* number of blocks of CEs */
68 struct client *CEFree = 0; /* first free client */
69 struct host *hostList = 0; /* linked list of all hosts */
70 int hostCount = 0; /* number of hosts in hostList */
74 static struct rx_securityClass *sc = NULL;
76 static void h_SetupCallbackConn_r(struct host * host);
77 static int h_threadquota(int);
79 #define CESPERBLOCK 73
80 struct CEBlock { /* block of CESPERBLOCK file entries */
81 struct client entry[CESPERBLOCK];
84 void h_TossStuff_r(struct host *host);
87 * Make sure the subnet macros have been defined.
90 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
93 #ifndef IN_CLASSA_SUBNET
94 #define IN_CLASSA_SUBNET 0xffff0000
98 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
101 #ifndef IN_CLASSB_SUBNET
102 #define IN_CLASSB_SUBNET 0xffffff00
106 /* get a new block of CEs and chain it on CEFree */
110 struct CEBlock *block;
113 block = malloc(sizeof(struct CEBlock));
115 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
116 ShutDownAndCore(PANIC);
119 for (i = 0; i < (CESPERBLOCK - 1); i++) {
120 Lock_Init(&block->entry[i].lock);
121 block->entry[i].next = &(block->entry[i + 1]);
123 block->entry[CESPERBLOCK - 1].next = 0;
124 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
125 CEFree = (struct client *)block;
131 /* get the next available CE */
132 static struct client *
135 struct client *entry;
140 ViceLog(0, ("CEFree NULL in GetCE\n"));
141 ShutDownAndCore(PANIC);
145 CEFree = entry->next;
147 memset(entry, 0, CLIENT_TO_ZERO(entry));
153 /* return an entry to the free list */
155 FreeCE(struct client *entry)
157 entry->VenusEpoch = 0;
159 entry->next = CEFree;
166 * The HTs and HTBlocks variables were formerly static, but they are
167 * now referenced elsewhere in the FileServer.
169 int HTs = 0; /* active file entries */
170 int HTBlocks = 0; /* number of blocks of HTs */
171 static struct host *HTFree = 0; /* first free file entry */
174 * Hash tables of host pointers. We need two tables, one
175 * to map IP addresses onto host pointers, and another
176 * to map host UUIDs onto host pointers.
178 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
179 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
180 #define h_HashIndex(hostip) (ntohl(hostip) & (h_HASHENTRIES-1))
181 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
183 struct HTBlock { /* block of HTSPERBLOCK file entries */
184 struct host entry[h_HTSPERBLOCK];
188 /* get a new block of HTs and chain it on HTFree */
192 struct HTBlock *block;
194 static int index = 0;
196 if (HTBlocks == h_MAXHOSTTABLES) {
197 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
201 block = malloc(sizeof(struct HTBlock));
203 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
204 ShutDownAndCore(PANIC);
206 for (i = 0; i < (h_HTSPERBLOCK); i++)
207 CV_INIT(&block->entry[i].cond, "block entry", CV_DEFAULT, 0);
208 for (i = 0; i < (h_HTSPERBLOCK); i++)
209 Lock_Init(&block->entry[i].lock);
210 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
211 block->entry[i].next = &(block->entry[i + 1]);
212 for (i = 0; i < (h_HTSPERBLOCK); i++)
213 block->entry[i].index = index++;
214 block->entry[h_HTSPERBLOCK - 1].next = 0;
215 HTFree = (struct host *)block;
216 hosttableptrs[HTBlocks++] = block->entry;
221 /* get the next available HT */
232 HTFree = entry->next;
234 memset(entry, 0, HOST_TO_ZERO(entry));
240 /* return an entry to the free list */
242 FreeHT(struct host *entry)
244 entry->next = HTFree;
251 hpr_Initialize(struct ubik_client **uclient)
254 struct rx_connection *serverconns[MAXSERVERS];
255 struct rx_securityClass *sc;
256 struct afsconf_dir *tdir;
258 struct afsconf_cell info;
262 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
264 ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
268 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
270 ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]", code));
275 code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
277 ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s",
278 cellstr, tdir->name, AFSDIR_CELLSERVDB_FILE));
285 ViceLog(0, ("hpr_Initialize: Could not initialize rx."));
290 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
291 * to force use of the KeyFile. secLevel == 0 implies -noauth was
293 code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
295 ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s "
296 "(so trying noauth)", code, afs_error_message(code)));
297 scIndex = RX_SECIDX_NULL;
298 sc = rxnull_NewClientSecurityObject();
301 if (scIndex == RX_SECIDX_NULL)
302 ViceLog(0, ("hpr_Initialize: Could not get afs tokens, "
303 "running unauthenticated. [%d]", code));
305 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
306 for (i = 0; i < info.numServers; i++) {
308 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
309 info.hostAddr[i].sin_port, PRSRV,
313 code = ubik_ClientInit(serverconns, uclient);
315 ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
318 code = rxs_Release(sc);
323 hpr_End(struct ubik_client *uclient)
328 code = ubik_ClientDestroy(uclient);
334 getThreadClient(struct ubik_client **client)
338 *client = pthread_getspecific(viced_uclient_key);
342 code = hpr_Initialize(client);
346 osi_Assert(pthread_setspecific(viced_uclient_key, *client) == 0);
352 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
356 struct ubik_client *uclient;
358 code = getThreadClient(&uclient);
363 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
364 if (code != PRSUCCESS)
367 /* do something about this, probably make a new call */
368 /* don't forget there's a hard limit in the interface */
370 "membership list for host id %d exceeds display limit\n",
377 hpr_NameToId(namelist *names, idlist *ids)
381 struct ubik_client *uclient;
383 code = getThreadClient(&uclient);
387 for (i = 0; i < names->namelist_len; i++)
388 stolower(names->namelist_val[i]);
389 code = ubik_PR_NameToID(uclient, 0, names, ids);
394 hpr_IdToName(idlist *ids, namelist *names)
397 struct ubik_client *uclient;
399 code = getThreadClient(&uclient);
403 code = ubik_PR_IDToName(uclient, 0, ids, names);
408 hpr_GetCPS(afs_int32 id, prlist *CPS)
412 struct ubik_client *uclient;
414 code = getThreadClient(&uclient);
419 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
420 if (code != PRSUCCESS)
423 /* do something about this, probably make a new call */
424 /* don't forget there's a hard limit in the interface */
425 fprintf(stderr, "membership list for id %d exceeds display limit\n",
431 static short consolePort = 0;
434 h_Lock_r(struct host *host)
444 * returns 1 if already locked
445 * else returns locks and returns 0
449 h_NBLock_r(struct host *host)
451 struct Lock *hostLock = &host->lock;
456 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
457 hostLock->excl_locked = WRITE_LOCK;
461 LOCK_UNLOCK(hostLock);
470 /*------------------------------------------------------------------------
471 * PRIVATE h_AddrInSameNetwork
474 * Given a target IP address and a candidate IP address (both
475 * in host byte order), return a non-zero value (1) if the
476 * candidate address is in a different network from the target
480 * a_targetAddr : Target address.
481 * a_candAddr : Candidate address.
484 * 1 if the candidate address is in the same net as the target,
488 * The target and candidate addresses are both in host byte
489 * order, NOT network byte order, when passed in. We return
490 * our value as a character, since that's the type of field in
491 * the host structure, where this info will be stored.
495 *------------------------------------------------------------------------*/
498 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
499 { /*h_AddrInSameNetwork */
501 afs_uint32 targetNet;
505 * Pull out the network and subnetwork numbers from the target
506 * and candidate addresses. We can short-circuit this whole
507 * affair if the target and candidate addresses are not of the
510 if (IN_CLASSA(a_targetAddr)) {
511 if (!(IN_CLASSA(a_candAddr))) {
514 targetNet = a_targetAddr & IN_CLASSA_NET;
515 candNet = a_candAddr & IN_CLASSA_NET;
516 } else if (IN_CLASSB(a_targetAddr)) {
517 if (!(IN_CLASSB(a_candAddr))) {
520 targetNet = a_targetAddr & IN_CLASSB_NET;
521 candNet = a_candAddr & IN_CLASSB_NET;
522 } /*Class B target */
523 else if (IN_CLASSC(a_targetAddr)) {
524 if (!(IN_CLASSC(a_candAddr))) {
527 targetNet = a_targetAddr & IN_CLASSC_NET;
528 candNet = a_candAddr & IN_CLASSC_NET;
529 } /*Class C target */
531 targetNet = a_targetAddr;
532 candNet = a_candAddr;
533 } /*Class D address */
536 * Now, simply compare the extracted net values for the two addresses
537 * (which at this point are known to be of the same class)
539 if (targetNet == candNet)
544 } /*h_AddrInSameNetwork */
547 /* Assumptions: called with held host */
549 h_gethostcps_r(struct host *host, afs_int32 now)
554 /* wait if somebody else is already doing the getCPS call */
555 while (host->hostFlags & HCPS_INPROGRESS) {
556 slept = 1; /* I did sleep */
557 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
558 CV_WAIT(&host->cond, &host_glock_mutex);
562 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
563 if (host->hcps.prlist_val)
564 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
565 host->hcps.prlist_val = NULL;
566 host->hcps.prlist_len = 0;
567 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
570 code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
575 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
576 * and reevaluates things if no sync server or quorum is found we could still end up
577 * with one of these errors. In such case we would like to reevaluate the rpc call to
578 * find if there's cps for this guy. We treat other errors (except network failures
579 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
580 * we could like to deal this problem the other way around (i.e. if code == NOCPS
581 * ignore else retry next time) but the problem is that there're other errors (i.e.
582 * EPERM) for which we don't want to retry and we don't know the whole code list!
584 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
586 * We would have preferred to use a while loop and try again since ops in protected
587 * acls for this host will fail now but they'll be reevaluated on any subsequent
588 * call. The attempt to wait for a quorum/sync site or network error won't work
589 * since this problems really should only occurs during a complete fileserver
590 * restart. Since the fileserver will start before the ptservers (and thus before
591 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
593 host->hcpsfailed = 1;
595 ("Warning: GetHostCPS failed (%d) for %p (%s:%d); will retry\n",
596 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
598 host->hcpsfailed = 0;
600 ("gethost: GetHostCPS failed (%d) for %p (%s:%d); ignored\n",
601 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
603 if (host->hcps.prlist_val)
604 free(host->hcps.prlist_val);
605 host->hcps.prlist_val = NULL;
606 host->hcps.prlist_len = 0; /* Make sure it's zero */
608 host->hcpsfailed = 0;
610 host->hostFlags &= ~HCPS_INPROGRESS;
611 /* signal all who are waiting */
612 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
613 host->hostFlags &= ~HCPS_WAITING;
614 CV_BROADCAST(&host->cond);
618 /* args in net byte order */
620 h_flushhostcps(afs_uint32 hostaddr, afs_uint16 hport)
625 h_Lookup_r(hostaddr, hport, &host);
627 host->hcpsfailed = 1;
636 * Allocate a host. It will be identified by the peer (ip,port) info in the
637 * rx connection provided. The host is returned held and locked
639 #define DEF_ROPCONS 2115
642 h_Alloc_r(struct rx_connection *r_con)
644 struct servent *serverentry;
646 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
653 /* acquire the host lock withot dropping H_LOCK. we can do this here
654 * because we know we will not block; we just created this host and
655 * nobody else knows about it. */
656 ObtainWriteLock(&host->lock);
658 host->host = rxr_HostOf(r_con);
659 host->port = rxr_PortOf(r_con);
661 h_AddHostToAddrHashTable_r(host->host, host->port, host);
663 if (consolePort == 0) { /* find the portal number for console */
664 #if defined(AFS_OSF_ENV)
665 serverentry = getservbyname("ropcons", "");
667 serverentry = getservbyname("ropcons", 0);
670 consolePort = serverentry->s_port;
672 consolePort = htons(DEF_ROPCONS); /* Use a default */
674 if (host->port == consolePort)
676 /* Make a callback channel even for the console, on the off chance that it
677 * makes a request that causes a break call back. It shouldn't. */
678 h_SetupCallbackConn_r(host);
679 host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
681 host->hcps.prlist_val = NULL;
682 host->hcps.prlist_len = 0;
683 host->interface = NULL;
685 host->hcpsfailed = 0; /* save cycles */
686 h_gethostcps(host); /* do this under host hold/lock */
688 host->FirstClient = NULL;
689 h_InsertList_r(host); /* update global host List */
691 * Compare the new host's IP address (in host byte order) with ours
692 * (the File Server's), remembering if they are in the same network.
694 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
695 host->InSameNetwork =
696 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
703 /* Make a callback channel even for the console, on the off chance that it
704 * makes a request that causes a break call back. It shouldn't. */
706 h_SetupCallbackConn_r(struct host * host)
709 sc = rxnull_NewClientSecurityObject();
710 host->callback_rxcon =
711 rx_NewConnection(host->host, host->port, 1, sc, 0);
712 rx_SetConnDeadTime(host->callback_rxcon, 50);
713 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
714 rx_SetConnSecondsUntilNatPing(host->callback_rxcon, 20);
718 * Lookup a host given an IP address and UDP port number.
719 * hostaddr and hport are in network order
720 * hostaddr and hport are in network order
721 * On return, refCount is incremented.
724 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
727 struct host *host = NULL;
728 struct h_AddrHashChain *chain;
729 int index = h_HashIndex(haddr);
730 extern int hostaclRefresh;
733 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
734 host = chain->hostPtr;
736 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
737 && chain->port == hport) {
738 if ((host->hostFlags & HWHO_INPROGRESS) &&
739 h_threadquota(host->lock.num_waiting)) {
745 if (host->hostFlags & HOSTDELETED) {
752 now = FT_ApproxTime(); /* always evaluate "now" */
753 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
755 * Every hostaclRefresh period (def 2 hrs) get the new
756 * membership list for the host. Note this could be the
757 * first time that the host is added to a group. Also
758 * here we also retry on previous legitimate hcps failures.
760 * If we get here refCount is elevated.
762 h_gethostcps_r(host, now);
772 /* Lookup a host given its UUID. */
774 h_LookupUuid_r(afsUUID * uuidp)
776 struct host *host = 0;
777 struct h_UuidHashChain *chain;
778 int index = h_UuidHashIndex(uuidp);
780 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
781 host = chain->hostPtr;
783 if (!(host->hostFlags & HOSTDELETED) && host->interface
784 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
792 /* h_TossStuff_r: Toss anything in the host structure (the host or
793 * clients marked for deletion. Called from h_Release_r ONLY.
794 * To be called, there must be no holds, and either host->deleted
795 * or host->clientDeleted must be set.
798 h_TossStuff_r(struct host *host)
800 struct client **cp, *client;
804 if ((host->hostFlags & HOSTDELETED)) {
808 /* make sure host doesn't go away over h_NBLock_r */
811 code = h_NBLock_r(host);
813 /* don't use h_Release_r, since that may call h_TossStuff_r again */
816 /* if somebody still has this host locked */
820 /* someone locked the host while HOSTDELETED was set; that is bad */
821 ViceLog(0, ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT
822 " (%s:%d flags 0x%x) was locked.\n",
823 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
824 (unsigned)host->hostFlags));
831 /* if somebody still has this host held */
832 /* we must check this _after_ h_NBLock_r, since h_NBLock_r can drop and
833 * reacquire H_LOCK */
834 if (host->refCount > 0) {
837 /* someone grabbed a ref while HOSTDELETED was set; that is bad */
838 ViceLog(0, ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT
839 " (%s:%d flags 0x%x) was held.\n",
840 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
841 (unsigned)host->hostFlags));
846 /* ASSUMPTION: rxi_FreeConnection() does not yield */
847 for (cp = &host->FirstClient; (client = *cp);) {
848 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
850 ObtainWriteLockNoBlock(&client->lock, code);
854 ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
855 "client %p was locked.\n",
856 host, afs_inet_ntoa_r(host->host, hoststr),
857 ntohs(host->port), client));
861 if (client->refCount) {
864 ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
865 "client %p refcount %d.\n",
866 host, afs_inet_ntoa_r(host->host, hoststr),
867 ntohs(host->port), client, client->refCount));
868 /* This is the same thing we do if the host is locked */
869 ReleaseWriteLock(&client->lock);
872 client->CPS.prlist_len = 0;
873 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
874 free(client->CPS.prlist_val);
875 client->CPS.prlist_val = NULL;
876 CurrentConnections--;
878 ReleaseWriteLock(&client->lock);
884 /* We've just cleaned out all the deleted clients; clear the flag */
885 host->hostFlags &= ~CLIENTDELETED;
887 if (host->hostFlags & HOSTDELETED) {
888 struct rx_connection *rxconn;
889 struct AddrPort hostAddrPort;
892 if (host->Console & 1)
894 if ((rxconn = host->callback_rxcon)) {
895 host->callback_rxcon = (struct rx_connection *)0;
896 rx_DestroyConnection(rxconn);
898 if (host->hcps.prlist_val)
899 free(host->hcps.prlist_val);
900 host->hcps.prlist_val = NULL;
901 host->hcps.prlist_len = 0;
902 DeleteAllCallBacks_r(host, 1);
903 host->hostFlags &= ~RESETDONE; /* just to be safe */
905 /* if alternate addresses do not exist */
906 if (!(host->interface)) {
907 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
909 h_DeleteHostFromUuidHashTable_r(host);
910 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
911 /* delete the hash entry for each valid alternate addresses */
912 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
913 hostAddrPort = host->interface->interface[i];
915 * if the interface addr/port is the primary, we already
916 * removed it. If the addr/port is not valid, its not
919 if (hostAddrPort.valid &&
920 (host->host != hostAddrPort.addr ||
921 host->port != hostAddrPort.port))
922 h_DeleteHostFromAddrHashTable_r(hostAddrPort.addr, hostAddrPort.port, host);
924 free(host->interface);
925 host->interface = NULL;
926 } /* if alternate address exists */
928 h_DeleteList_r(host); /* remove host from global host List */
935 /* h_Enumerate: Calls (*proc)(host, param) for at least each host in the
936 * system at the start of the enumeration (perhaps more). Hosts may be deleted
937 * (have delete flag set); ditto for clients. refCount is always incremented
938 * before (*proc) is called.
940 * The return value of the proc is a set of flags. The proc should set
941 * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
944 h_Enumerate(int (*proc) (struct host*, void *), void *param)
946 struct host *host, **list;
951 if (hostCount == 0) {
955 list = malloc(hostCount * sizeof(struct host *));
957 ViceLogThenPanic(0, ("Failed malloc in h_Enumerate (list)\n"));
959 for (totalCount = count = 0, host = hostList;
960 host && totalCount < hostCount;
961 host = host->next, totalCount++) {
963 if (!(host->hostFlags & HOSTDELETED)) {
969 if (totalCount != hostCount) {
970 ViceLog(0, ("h_Enumerate found %d of %d hosts\n", totalCount, hostCount));
971 } else if (host != NULL) {
972 ViceLog(0, ("h_Enumerate found more than %d hosts\n", hostCount));
973 ShutDownAndCore(PANIC);
976 for (i = 0; i < count; i++) {
978 flags = (*proc) (list[i], param);
980 h_Release_r(list[i]);
982 /* bail out of the enumeration early */
983 if (H_ENUMERATE_ISSET_BAIL(flags)) {
986 ViceLog(0, ("h_Enumerate got back invalid return value %d\n", flags));
987 ShutDownAndCore(PANIC);
991 /* we bailed out of enumerating hosts early; we still have holds on
992 * some of the hosts in 'list', so release them */
995 for ( ; i < count; i++) {
996 h_Release_r(list[i]);
1004 /* h_Enumerate_r (revised):
1005 * Calls (*proc)(host, param) for each host in hostList, starting
1006 * at enumstart. Called only under H_LOCK. Hosts may be deleted (have
1007 * delete flag set); ditto for clients. refCount is always incremented
1008 * before (*proc) is called.
1010 * @note Assumes that hostList is only prepended to, that a host is never
1011 * inserted into the middle. Otherwise this would not be guaranteed to
1014 * The return value of the proc is a set of flags. The proc should set
1015 * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
1018 h_Enumerate_r(int (*proc) (struct host *, void *),
1019 struct host *enumstart, void *param)
1021 struct host *host, *next;
1025 if (hostCount == 0) {
1032 /* find the first non-deleted host, so we know where to actually start
1034 for (count = 0; host && count < hostCount; count++) {
1035 if (!(host->hostFlags & HOSTDELETED)) {
1042 /* we didn't find a non-deleted host... */
1044 if (host && count >= hostCount) {
1045 /* ...because we found a loop */
1046 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", hostCount));
1047 ShutDownAndCore(PANIC);
1050 /* ...because the hostList is full of deleted hosts */
1054 h_Hold_r(enumstart);
1056 /* remember hostCount, lest it change over the potential H_LOCK drop in
1058 origHostCount = hostCount;
1060 for (count = 0, host = enumstart; host && count < origHostCount; host = next, count++) {
1063 /* find the next non-deleted host */
1064 while (next && (next->hostFlags & HOSTDELETED)) {
1066 /* inc count for the skipped-over host */
1067 if (++count > origHostCount) {
1068 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", origHostCount));
1069 ShutDownAndCore(PANIC);
1075 if (!(host->hostFlags & HOSTDELETED)) {
1077 flags = (*proc) (host, param);
1078 if (H_ENUMERATE_ISSET_BAIL(flags)) {
1079 h_Release_r(host); /* this might free up the host */
1085 ViceLog(0, ("h_Enumerate_r got back invalid return value %d\n", flags));
1086 ShutDownAndCore(PANIC);
1089 h_Release_r(host); /* this might free up the host */
1091 if (host != NULL && count >= origHostCount) {
1092 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", origHostCount));
1093 ShutDownAndCore(PANIC);
1095 } /*h_Enumerate_r */
1098 /* inserts a new HashChain structure corresponding to this UUID */
1100 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1103 struct h_UuidHashChain *chain;
1104 char uuid1[128], uuid2[128];
1107 /* hash into proper bucket */
1108 index = h_UuidHashIndex(uuid);
1110 /* don't add the same entry multiple times */
1111 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1112 if (!chain->hostPtr)
1115 if (chain->hostPtr->interface &&
1116 afs_uuid_equal(&chain->hostPtr->interface->uuid, uuid)) {
1117 if (LogLevel >= 125) {
1118 afsUUID_to_string(&chain->hostPtr->interface->uuid, uuid1,
1120 afsUUID_to_string(uuid, uuid2, 127);
1121 ViceLog(125, ("h_AddHostToUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s) exists as %s:%d (uuid %s)\n",
1123 afs_inet_ntoa_r(chain->hostPtr->host, hoststr),
1124 ntohs(chain->hostPtr->port), uuid2));
1130 /* insert into beginning of list for this bucket */
1131 chain = malloc(sizeof(struct h_UuidHashChain));
1133 ViceLogThenPanic(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1135 chain->hostPtr = host;
1136 chain->next = hostUuidHashTable[index];
1137 hostUuidHashTable[index] = chain;
1140 afsUUID_to_string(uuid, uuid2, 127);
1142 ("h_AddHostToUuidHashTable_r: host %p (%s:%d) added as uuid %s\n",
1143 host, afs_inet_ntoa_r(chain->hostPtr->host, hoststr),
1144 ntohs(chain->hostPtr->port), uuid2));
1147 /* deletes a HashChain structure corresponding to this host */
1149 h_DeleteHostFromUuidHashTable_r(struct host *host)
1152 struct h_UuidHashChain **uhp, *uth;
1156 if (!host->interface)
1159 /* hash into proper bucket */
1160 index = h_UuidHashIndex(&host->interface->uuid);
1162 if (LogLevel >= 125)
1163 afsUUID_to_string(&host->interface->uuid, uuid1, 127);
1164 for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
1165 osi_Assert(uth->hostPtr);
1166 if (uth->hostPtr == host) {
1168 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d)\n",
1169 host, uuid1, afs_inet_ntoa_r(host->host, hoststr),
1170 ntohs(host->port)));
1177 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d) not found\n",
1178 host, uuid1, afs_inet_ntoa_r(host->host, hoststr),
1179 ntohs(host->port)));
1184 * This is called with host locked and held.
1186 * All addresses are in network byte order.
1189 invalidateInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1193 struct Interface *interface;
1194 char hoststr[16], hoststr2[16];
1197 osi_Assert(host->interface);
1199 ViceLog(125, ("invalidateInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1200 host, afs_inet_ntoa_r(host->host, hoststr),
1201 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1205 * Make sure this address is on the list of known addresses
1208 interface = host->interface;
1209 number = host->interface->numberOfInterfaces;
1210 for (i = 0; i < number; i++) {
1211 if (interface->interface[i].addr == addr &&
1212 interface->interface[i].port == port) {
1213 if (interface->interface[i].valid) {
1214 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1215 interface->interface[i].valid = 0;
1226 * This is called with host locked and held. This function differs
1227 * from removeInterfaceAddr_r in that it is called when the address
1228 * is being removed from the host regardless of whether or not there
1229 * is an interface list for the host. This function will delete the
1230 * host if there are no addresses left on it.
1232 * All addresses are in network byte order.
1235 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1238 char hoststr[16], hoststr2[16];
1239 struct rx_connection *rxconn;
1241 if (!host->interface || host->interface->numberOfInterfaces == 1) {
1242 if (host->host == addr && host->port == port) {
1244 ("Removing only address for host %" AFS_PTR_FMT " (%s:%d), deleting host.\n",
1245 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1246 host->hostFlags |= HOSTDELETED;
1248 * Do not remove the primary addr/port from the hash table.
1249 * It will be ignored due to the HOSTDELETED flag and will
1250 * be removed when h_TossStuff_r() cleans up the HOSTDELETED
1251 * host. Removing it here will only result in a search for
1252 * the host/addr/port in the hash chain which will fail.
1256 ("Removing address that does not belong to host %" AFS_PTR_FMT " (%s:%d).\n",
1257 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1260 if (host->host == addr && host->port == port) {
1261 removeInterfaceAddr_r(host, addr, port);
1263 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1264 if (host->interface->interface[i].valid) {
1266 ("Removed address for host %" AFS_PTR_FMT " (%s:%d), new primary interface %s:%d.\n",
1267 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1268 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2),
1269 ntohs(host->interface->interface[i].port)));
1270 host->host = host->interface->interface[i].addr;
1271 host->port = host->interface->interface[i].port;
1272 h_AddHostToAddrHashTable_r(host->host, host->port, host);
1277 if (i == host->interface->numberOfInterfaces) {
1279 ("Removed only address for host %" AFS_PTR_FMT " (%s:%d), no valid alternate interfaces, deleting host.\n",
1280 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1281 host->hostFlags |= HOSTDELETED;
1282 /* addr/port was removed from the hash table */
1286 rxconn = host->callback_rxcon;
1287 host->callback_rxcon = NULL;
1290 rx_DestroyConnection(rxconn);
1294 h_SetupCallbackConn_r(host);
1297 /* not the primary addr/port, just invalidate it */
1298 invalidateInterfaceAddr_r(host, addr, port);
1306 createHostAddrHashChain_r(int index, afs_uint32 addr, afs_uint16 port, struct host *host)
1308 struct h_AddrHashChain *chain;
1311 /* insert into beginning of list for this bucket */
1312 chain = malloc(sizeof(struct h_AddrHashChain));
1314 ViceLogThenPanic(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1316 chain->hostPtr = host;
1317 chain->next = hostAddrHashTable[index];
1320 hostAddrHashTable[index] = chain;
1321 ViceLog(125, ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " added as %s:%d\n",
1322 host, afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1326 * Resolve host address conflicts when hashing by address.
1328 * @param[in] addr an ip address of the interface
1329 * @param[in] port the port of the interface
1330 * @param[in] newHost the host being added with this address
1331 * @param[in] oldHost the host previously added with this address
1334 reconcileHosts_r(afs_uint32 addr, afs_uint16 port, struct host *newHost,
1335 struct host *oldHost)
1337 struct rx_connection *cb = NULL;
1339 struct interfaceAddr interf;
1341 afsUUID *newHostUuid = &nulluuid;
1342 afsUUID *oldHostUuid = &nulluuid;
1346 ("reconcileHosts_r: addr %s:%d newHost %" AFS_PTR_FMT " oldHost %"
1347 AFS_PTR_FMT, afs_inet_ntoa_r(addr, hoststr), ntohs(port),
1350 osi_Assert(oldHost != newHost);
1351 caps.Capabilities_val = NULL;
1354 sc = rxnull_NewClientSecurityObject();
1357 cb = rx_NewConnection(addr, port, 1, sc, 0);
1358 rx_SetConnDeadTime(cb, 50);
1359 rx_SetConnHardDeadTime(cb, AFS_HARDDEADTIME);
1364 code = RXAFSCB_TellMeAboutYourself(cb, &interf, &caps);
1365 if (code == RXGEN_OPCODE) {
1366 code = RXAFSCB_WhoAreYou(cb, &interf);
1370 if (code == RXGEN_OPCODE ||
1371 (code == 0 && afs_uuid_equal(&interf.uuid, &nulluuid))) {
1373 ("reconcileHosts_r: WhoAreYou not supported for connection (%s:%d), error %d\n",
1374 afs_inet_ntoa_r(addr, hoststr), ntohs(port), code));
1379 ("reconcileHosts_r: WhoAreYou failed for connection (%s:%d), error %d\n",
1380 afs_inet_ntoa_r(addr, hoststr), ntohs(port), code));
1384 /* Since lock was dropped, the hosts may have been deleted during the rpcs. */
1385 if ((newHost->hostFlags & HOSTDELETED)
1386 && (oldHost->hostFlags & HOSTDELETED)) {
1388 ("reconcileHosts_r: new and old hosts were deleted during probe.\n"));
1392 /* A check can be done if at least one of the hosts has a uuid. It
1393 * is an error if the hosts have the same (not null) uuid. */
1394 if ((!(newHost->hostFlags & HOSTDELETED)) && newHost->interface) {
1395 newHostUuid = &(newHost->interface->uuid);
1397 if ((!(oldHost->hostFlags & HOSTDELETED)) && oldHost->interface) {
1398 oldHostUuid = &(oldHost->interface->uuid);
1400 if (afs_uuid_equal(newHostUuid, &nulluuid) &&
1401 afs_uuid_equal(oldHostUuid, &nulluuid)) {
1403 ("reconcileHosts_r: Cannot reconcile hosts for connection (%s:%d), no uuids\n",
1404 afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1407 if (afs_uuid_equal(newHostUuid, oldHostUuid)) {
1409 ("reconcileHosts_r: Cannot reconcile hosts for connection (%s:%d), same uuids\n",
1410 afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1414 /* Determine which host should be hashed */
1415 if ((!(newHost->hostFlags & HOSTDELETED))
1416 && afs_uuid_equal(newHostUuid, &(interf.uuid))) {
1417 /* Install the new host into the hash before removing the stale
1418 * addresses. Walk the hash chain again since the hash table may have
1419 * been changed when the host lock was dropped to get the uuid. */
1420 struct h_AddrHashChain *chain;
1421 int index = h_HashIndex(addr);
1422 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1423 if (chain->addr == addr && chain->port == port) {
1424 chain->hostPtr = newHost;
1425 removeAddress_r(oldHost, addr, port);
1429 createHostAddrHashChain_r(index, addr, port, newHost);
1430 removeAddress_r(oldHost, addr, port);
1433 if ((!(oldHost->hostFlags & HOSTDELETED))
1434 && afs_uuid_equal(oldHostUuid, &(interf.uuid))) {
1435 removeAddress_r(newHost, addr, port);
1440 if (!(newHost->hostFlags & HOSTDELETED)) {
1441 removeAddress_r(newHost, addr, port);
1443 if (!(oldHost->hostFlags & HOSTDELETED)) {
1444 removeAddress_r(oldHost, addr, port);
1448 h_Release_r(newHost);
1449 h_Release_r(oldHost);
1450 rx_DestroyConnection(cb);
1454 /* inserts a new HashChain structure corresponding to this address */
1456 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1459 struct h_AddrHashChain *chain;
1462 /* hash into proper bucket */
1463 index = h_HashIndex(addr);
1465 /* don't add the same address:port pair entry multiple times */
1466 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1467 if (chain->addr == addr && chain->port == port) {
1468 if (chain->hostPtr == host) {
1470 ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) already hashed\n",
1471 host, afs_inet_ntoa_r(chain->addr, hoststr),
1472 ntohs(chain->port)));
1475 if (!(chain->hostPtr->hostFlags & HOSTDELETED)) {
1476 /* attempt to resolve host address collision */
1477 reconcileHosts_r(addr, port, host, chain->hostPtr);
1482 createHostAddrHashChain_r(index, addr, port, host);
1486 * This is called with host locked and held.
1487 * It is called to either validate or add an additional interface
1488 * address/port on the specified host.
1490 * All addresses are in network byte order.
1493 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1497 struct Interface *interface;
1498 char hoststr[16], hoststr2[16];
1501 osi_Assert(host->interface);
1504 * Make sure this address is on the list of known addresses
1507 number = host->interface->numberOfInterfaces;
1508 for (i = 0; i < number; i++) {
1509 if (host->interface->interface[i].addr == addr &&
1510 host->interface->interface[i].port == port) {
1512 ("addInterfaceAddr : found host %" AFS_PTR_FMT " (%s:%d) adding %s:%d%s\n",
1513 host, afs_inet_ntoa_r(host->host, hoststr),
1514 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1515 ntohs(port), host->interface->interface[i].valid ? "" :
1518 if (host->interface->interface[i].valid == 0) {
1519 host->interface->interface[i].valid = 1;
1520 h_AddHostToAddrHashTable_r(addr, port, host);
1526 ViceLog(125, ("addInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) adding %s:%d\n",
1527 host, afs_inet_ntoa_r(host->host, hoststr),
1528 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1531 interface = malloc(sizeof(struct Interface)
1532 + (sizeof(struct AddrPort) * number));
1534 ViceLogThenPanic(0, ("Failed malloc in addInterfaceAddr_r\n"));
1536 interface->numberOfInterfaces = number + 1;
1537 interface->uuid = host->interface->uuid;
1538 for (i = 0; i < number; i++)
1539 interface->interface[i] = host->interface->interface[i];
1541 /* Add the new valid interface */
1542 interface->interface[number].addr = addr;
1543 interface->interface[number].port = port;
1544 interface->interface[number].valid = 1;
1545 h_AddHostToAddrHashTable_r(addr, port, host);
1546 free(host->interface);
1547 host->interface = interface;
1554 * This is called with host locked and held.
1556 * All addresses are in network byte order.
1559 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1563 struct Interface *interface;
1564 char hoststr[16], hoststr2[16];
1567 osi_Assert(host->interface);
1569 ViceLog(125, ("removeInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1570 host, afs_inet_ntoa_r(host->host, hoststr),
1571 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1575 * Make sure this address is on the list of known addresses
1578 interface = host->interface;
1579 number = host->interface->numberOfInterfaces;
1580 for (i = 0; i < number; i++) {
1581 if (interface->interface[i].addr == addr &&
1582 interface->interface[i].port == port) {
1583 if (interface->interface[i].valid)
1584 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1586 for (; i < number; i++) {
1587 interface->interface[i] = interface->interface[i+1];
1589 interface->numberOfInterfaces = number;
1600 h_threadquota(int waiting)
1605 } else if (lwps > 32) {
1608 } else if (lwps > 16) {
1618 /* If found, host is returned with refCount incremented */
1620 h_GetHost_r(struct rx_connection *tcon)
1623 struct host *oldHost;
1625 struct interfaceAddr interf;
1626 int interfValid = 0;
1627 struct Identity *identP = NULL;
1630 char hoststr[16], hoststr2[16];
1632 struct rx_connection *cb_conn = NULL;
1633 struct rx_connection *cb_in = NULL;
1635 caps.Capabilities_val = NULL;
1637 haddr = rxr_HostOf(tcon);
1638 hport = rxr_PortOf(tcon);
1641 rx_DestroyConnection(cb_in);
1644 if (caps.Capabilities_val)
1645 free(caps.Capabilities_val);
1646 caps.Capabilities_val = NULL;
1647 caps.Capabilities_len = 0;
1650 if (h_Lookup_r(haddr, hport, &host))
1652 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1653 if (host && !identP && !(host->Console & 1)) {
1654 /* This is a new connection, and we already have a host
1655 * structure for this address. Verify that the identity
1656 * of the caller matches the identity in the host structure.
1658 if ((host->hostFlags & HWHO_INPROGRESS) &&
1659 h_threadquota(host->lock.num_waiting)) {
1665 if (!(host->hostFlags & ALTADDR) ||
1666 (host->hostFlags & HOSTDELETED)) {
1667 /* Another thread is doing initialization
1668 * or this host was deleted while we
1669 * waited for the lock. */
1672 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1673 host, afs_inet_ntoa_r(host->host, hoststr),
1674 ntohs(host->port)));
1678 host->hostFlags |= HWHO_INPROGRESS;
1679 host->hostFlags &= ~ALTADDR;
1681 /* We received a new connection from an IP address/port
1682 * that is associated with 'host' but the address/port of
1683 * the callback connection does not have to match it.
1684 * If there is a match, we can use the existing callback
1685 * connection to verify the UUID. If they do not match
1686 * we need to use a new callback connection to verify the
1687 * UUID of the incoming caller and perhaps use the old
1688 * callback connection to verify that the old address/port
1692 cb_conn = host->callback_rxcon;
1693 rx_GetConnection(cb_conn);
1695 if (haddr == host->host && hport == host->port) {
1696 /* The existing callback connection matches the
1697 * incoming connection so just use it.
1700 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1701 if (code == RXGEN_OPCODE)
1702 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1704 /* We do not have a match. Create a new connection
1705 * for the new addr/port and use multi_Rx to probe
1706 * both of them simultaneously.
1709 sc = rxnull_NewClientSecurityObject();
1710 cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
1711 rx_SetConnDeadTime(cb_in, 50);
1712 rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
1713 rx_SetConnSecondsUntilNatPing(cb_in, 20);
1716 RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
1717 if (code == RXGEN_OPCODE)
1718 code = RXAFSCB_WhoAreYou(cb_in, &interf);
1720 rx_PutConnection(cb_conn);
1723 if ((code == RXGEN_OPCODE) ||
1724 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1725 identP = malloc(sizeof(struct Identity));
1727 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1730 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1731 if (cb_in == NULL) {
1732 /* The host on this connection was unable to respond to
1733 * the WhoAreYou. We will treat this as a new connection
1734 * from the existing host. The worst that can happen is
1735 * that we maintain some extra callback state information */
1736 if (host->interface) {
1738 ("Host %" AFS_PTR_FMT " (%s:%d) used to support WhoAreYou, deleting.\n",
1740 afs_inet_ntoa_r(host->host, hoststr),
1741 ntohs(host->port)));
1742 host->hostFlags |= HOSTDELETED;
1743 host->hostFlags &= ~HWHO_INPROGRESS;
1750 /* The incoming connection does not support WhoAreYou but
1751 * the original one might have. Use removeAddress_r() to
1752 * remove this addr/port from the host that was found.
1753 * If there are no more addresses left for the host it
1754 * will be deleted. Then we retry.
1756 removeAddress_r(host, haddr, hport);
1757 host->hostFlags &= ~HWHO_INPROGRESS;
1758 host->hostFlags |= ALTADDR;
1764 } else if (code == 0) {
1766 identP = malloc(sizeof(struct Identity));
1768 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1771 identP->uuid = interf.uuid;
1772 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1773 /* Check whether the UUID on this connection matches
1774 * the UUID in the host structure. If they don't match
1775 * then this is not the same host as before. */
1776 if (!host->interface
1777 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1780 ("Uuid doesn't match connection (%s:%d).\n",
1781 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport)));
1782 removeAddress_r(host, haddr, hport);
1785 ("Uuid doesn't match host %" AFS_PTR_FMT " (%s:%d).\n",
1786 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1788 removeAddress_r(host, host->host, host->port);
1790 host->hostFlags &= ~HWHO_INPROGRESS;
1791 host->hostFlags |= ALTADDR;
1797 /* the UUID matched the client at the incoming addr/port
1798 * but this is not the address of the active callback
1799 * connection. Try that connection and see if the client
1800 * is still there and if the reported UUID is the same.
1803 afsUUID uuid = host->interface->uuid;
1804 cb_conn = host->callback_rxcon;
1805 rx_GetConnection(cb_conn);
1806 rx_SetConnDeadTime(cb_conn, 2);
1807 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1809 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1811 rx_SetConnDeadTime(cb_conn, 50);
1812 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1813 rx_PutConnection(cb_conn);
1816 /* The primary address is either not responding or
1817 * is not the client we are looking for. Need to
1818 * remove the primary address and add swap in the new
1819 * callback connection, and destroy the old one.
1821 struct rx_connection *rxconn;
1822 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
1824 afs_inet_ntoa_r(host->host, hoststr),
1825 ntohs(host->port),code2));
1828 * make sure we add and then remove. otherwise, we
1829 * might end up with no valid interfaces after the
1830 * remove and the host will have been marked deleted.
1832 addInterfaceAddr_r(host, haddr, hport);
1833 removeInterfaceAddr_r(host, host->host, host->port);
1836 rxconn = host->callback_rxcon;
1837 host->callback_rxcon = cb_in;
1842 * If rx_DestroyConnection calls h_FreeConnection we
1843 * will deadlock on the host_glock_mutex. Work around
1844 * the problem by unhooking the client from the
1845 * connection before destroying the connection.
1847 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1848 rx_DestroyConnection(rxconn);
1854 /* A callback to the incoming connection address is failing.
1855 * Assume that the addr/port is no longer associated with the host
1856 * returned by h_Lookup_r.
1859 ("CB: WhoAreYou failed for connection (%s:%d) , error %d\n",
1860 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport), code));
1861 removeAddress_r(host, haddr, hport);
1862 host->hostFlags &= ~HWHO_INPROGRESS;
1863 host->hostFlags |= ALTADDR;
1867 rx_DestroyConnection(cb_in);
1872 ("CB: WhoAreYou failed for host %" AFS_PTR_FMT " (%s:%d), error %d\n",
1873 host, afs_inet_ntoa_r(host->host, hoststr),
1874 ntohs(host->port), code));
1875 host->hostFlags |= VENUSDOWN;
1878 if (caps.Capabilities_val
1879 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1880 host->hostFlags |= HERRORTRANS;
1882 host->hostFlags &= ~(HERRORTRANS);
1883 host->hostFlags |= ALTADDR;
1884 host->hostFlags &= ~HWHO_INPROGRESS;
1887 if (!(host->hostFlags & ALTADDR)) {
1888 /* another thread is doing the initialisation */
1890 ("Host %" AFS_PTR_FMT " (%s:%d) waiting for host-init to complete\n",
1891 host, afs_inet_ntoa_r(host->host, hoststr),
1892 ntohs(host->port)));
1896 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1897 host, afs_inet_ntoa_r(host->host, hoststr),
1898 ntohs(host->port)));
1902 /* We need to check whether the identity in the host structure
1903 * matches the identity on the connection. If they don't match
1904 * then treat this a new host. */
1905 if (!(host->Console & 1)
1906 && ((!identP->valid && host->interface)
1907 || (identP->valid && !host->interface)
1909 && !afs_uuid_equal(&identP->uuid,
1910 &host->interface->uuid)))) {
1911 char uuid1[128], uuid2[128];
1913 afsUUID_to_string(&identP->uuid, uuid1, 127);
1914 if (host->interface)
1915 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1917 ("CB: new identity for host %p (%s:%d), "
1918 "deleting(%x %p %s %s)\n",
1919 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1920 identP->valid, host->interface,
1921 identP->valid ? uuid1 : "no_uuid",
1922 host->interface ? uuid2 : "no_uuid"));
1924 /* The host in the cache is not the host for this connection */
1926 host->hostFlags |= HOSTDELETED;
1932 host = h_Alloc_r(tcon); /* returned held and locked */
1935 h_gethostcps_r(host, FT_ApproxTime());
1936 if (!(host->Console & 1)) {
1938 cb_conn = host->callback_rxcon;
1939 rx_GetConnection(cb_conn);
1940 host->hostFlags |= HWHO_INPROGRESS;
1943 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1944 if (code == RXGEN_OPCODE)
1945 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1946 rx_PutConnection(cb_conn);
1949 if ((code == RXGEN_OPCODE) ||
1950 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1952 identP = malloc(sizeof(struct Identity));
1957 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1961 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1963 ("Host %" AFS_PTR_FMT " (%s:%d) does not support WhoAreYou.\n",
1964 host, afs_inet_ntoa_r(host->host, hoststr),
1965 ntohs(host->port)));
1967 } else if (code == 0) {
1969 identP = malloc(sizeof(struct Identity));
1974 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1978 identP->uuid = interf.uuid;
1980 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1982 ("WhoAreYou success on host %" AFS_PTR_FMT " (%s:%d)\n",
1983 host, afs_inet_ntoa_r(host->host, hoststr),
1984 ntohs(host->port)));
1986 if (code == 0 && !identP->valid) {
1987 cb_conn = host->callback_rxcon;
1988 rx_GetConnection(cb_conn);
1990 code = RXAFSCB_InitCallBackState(cb_conn);
1991 rx_PutConnection(cb_conn);
1994 } else if (code == 0) {
1995 oldHost = h_LookupUuid_r(&identP->uuid);
2000 if (oldHost->hostFlags & HOSTDELETED) {
2001 h_Unlock_r(oldHost);
2002 h_Release_r(oldHost);
2010 /* This is a new address for an existing host. Update
2011 * the list of interfaces for the existing host and
2012 * delete the host structure we just allocated. */
2014 /* mark the duplicate host as deleted before we do
2015 * anything. The probing code below may try to change
2016 * "oldHost" to the same IP address as "host" currently
2017 * has, and we do not want a pseudo-"collision" to be
2019 host->hostFlags |= HOSTDELETED;
2021 oldHost->hostFlags |= HWHO_INPROGRESS;
2023 if (oldHost->interface) {
2025 afsUUID uuid = oldHost->interface->uuid;
2026 cb_conn = oldHost->callback_rxcon;
2027 rx_GetConnection(cb_conn);
2028 rx_SetConnDeadTime(cb_conn, 2);
2029 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2031 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2033 rx_SetConnDeadTime(cb_conn, 50);
2034 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2035 rx_PutConnection(cb_conn);
2038 /* The primary address is either not responding or
2039 * is not the client we are looking for.
2040 * MultiProbeAlternateAddress_r() will remove the
2041 * alternate interfaces that do not have the same
2043 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
2045 afs_inet_ntoa_r(oldHost->host, hoststr),
2046 ntohs(oldHost->port),code2));
2048 if (MultiProbeAlternateAddress_r(oldHost)) {
2049 /* If MultiProbeAlternateAddress_r succeeded,
2050 * it updated oldHost->host and oldHost->port
2051 * to an address that responded successfully to
2052 * a ProbeUuid, so it is as if the ProbeUuid
2053 * call above returned success. So, only set
2054 * 'probefail' if MultiProbeAlternateAddress_r
2063 if (oldHost->host != haddr || oldHost->port != hport) {
2064 struct rx_connection *rxconn;
2067 ("CB: Host %" AFS_PTR_FMT " (%s:%d) has new addr %s:%d\n",
2069 afs_inet_ntoa_r(oldHost->host, hoststr2),
2070 ntohs(oldHost->port),
2071 afs_inet_ntoa_r(haddr, hoststr),
2074 * add then remove. otherwise the host may get marked
2075 * deleted if we removed the only valid address.
2077 addInterfaceAddr_r(oldHost, haddr, hport);
2078 if (probefail || oldHost->host == haddr) {
2080 * The probe failed which means that the old
2081 * address is either unreachable or is not the
2082 * same host we were just contacted by. We will
2083 * also remove addresses if only the port has
2084 * changed because that indicates the client
2087 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
2090 struct Interface *interface = oldHost->interface;
2091 int number = oldHost->interface->numberOfInterfaces;
2092 for (i = 0; i < number; i++) {
2093 if (interface->interface[i].addr == haddr &&
2094 interface->interface[i].port != hport) {
2096 * We have just been contacted by a client
2097 * that has been seen from behind a NAT
2098 * and at least one other address.
2100 removeInterfaceAddr_r(oldHost, haddr,
2101 interface->interface[i].port);
2106 oldHost->host = haddr;
2107 oldHost->port = hport;
2108 rxconn = oldHost->callback_rxcon;
2109 oldHost->callback_rxcon = host->callback_rxcon;
2110 host->callback_rxcon = rxconn;
2112 /* don't destroy rxconn here; let h_TossStuff_r
2113 * take care of that via h_Release_r below */
2115 host->hostFlags &= ~HWHO_INPROGRESS;
2117 /* release host because it was allocated by h_Alloc_r */
2120 /* the new host is held and locked */
2122 /* This really is a new host */
2123 h_AddHostToUuidHashTable_r(&identP->uuid, host);
2124 cb_conn = host->callback_rxcon;
2125 rx_GetConnection(cb_conn);
2128 RXAFSCB_InitCallBackState3(cb_conn,
2130 rx_PutConnection(cb_conn);
2135 ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
2136 host, afs_inet_ntoa_r(host->host, hoststr),
2137 ntohs(host->port)));
2138 osi_Assert(interfValid == 1);
2139 initInterfaceAddr_r(host, &interf);
2145 ("CB: RCallBackConnectBack failed for %" AFS_PTR_FMT " (%s:%d)\n",
2146 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
2147 host->hostFlags |= VENUSDOWN;
2150 ("CB: RCallBackConnectBack succeeded for %" AFS_PTR_FMT " (%s:%d)\n",
2151 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
2152 host->hostFlags |= RESETDONE;
2155 if (caps.Capabilities_val
2156 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
2157 host->hostFlags |= HERRORTRANS;
2159 host->hostFlags &= ~(HERRORTRANS);
2160 host->hostFlags |= ALTADDR; /* host structure initialization complete */
2161 host->hostFlags &= ~HWHO_INPROGRESS;
2166 if (caps.Capabilities_val)
2167 free(caps.Capabilities_val);
2168 caps.Capabilities_val = NULL;
2169 caps.Capabilities_len = 0;
2171 rx_DestroyConnection(cb_in);
2179 static char localcellname[PR_MAXNAMELEN + 1];
2183 h_InitHostPackage(void)
2185 memset(&nulluuid, 0, sizeof(afsUUID));
2186 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
2187 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
2188 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
2189 MUTEX_INIT(&host_glock_mutex, "host glock", MUTEX_DEFAULT, 0);
2193 MapName_r(char *uname, afs_int32 * aval)
2199 lnames.namelist_len = 1;
2200 lnames.namelist_val = (prname *) uname;
2201 lids.idlist_len = 0;
2202 lids.idlist_val = NULL;
2205 code = hpr_NameToId(&lnames, &lids);
2208 if (lids.idlist_val) {
2209 *aval = lids.idlist_val[0];
2210 if (*aval == AnonymousID) {
2212 ("MapName: NameToId on %s returns anonymousID\n",
2213 lnames.namelist_val[0]));
2215 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
2218 ("MapName: NameToId on '%s' is unknown\n",
2219 lnames.namelist_val[0]));
2229 /* NOTE: this returns the client with a Write lock and a refCount */
2231 h_ID2Client(afs_int32 vid)
2233 struct client *client;
2238 for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
2239 if (host->hostFlags & HOSTDELETED)
2241 for (client = host->FirstClient; client; client = client->next) {
2242 if (!client->deleted && client->ViceId == vid) {
2245 ObtainWriteLock(&client->lock);
2250 if (count != hostCount) {
2251 ViceLog(0, ("h_ID2Client found %d of %d hosts\n", count, hostCount));
2252 } else if (host != NULL) {
2253 ViceLog(0, ("h_ID2Client found more than %d hosts\n", hostCount));
2254 ShutDownAndCore(PANIC);
2262 format_vname(char *vname, int usize, const char *tname, const char *tinst,
2263 const char *tcell, afs_int32 islocal)
2267 len = strlcpy(vname, tname, usize);
2271 len = strlcat(vname, ".", usize);
2274 len = strlcat(vname, tinst, usize);
2278 if (tcell[0] && !islocal) {
2279 len = strlcat(vname, "@", usize);
2282 len = strlcat(vname, tcell, usize);
2290 getPeerDetails(struct rx_connection *conn,
2291 afs_int32 *viceid, afs_int32 *expTime, int authClass)
2294 #if (64-MAXKTCNAMELEN)
2295 ticket name length != 64
2299 char tcell[MAXKTCREALMLEN];
2300 char uname[PR_MAXNAMELEN];
2302 *viceid = AnonymousID;
2303 *expTime = 0x7fffffff;
2306 ("FindClient: authenticating connection: authClass=%d\n",
2308 if (authClass == 1) {
2309 /* A bcrypt tickets, no longer supported */
2310 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2314 if (authClass == 2) {
2315 /* an rxkad ticket */
2319 /* kerberos ticket */
2320 code = rxkad_GetServerInfo(conn, /*level */ 0, (afs_uint32 *)expTime,
2321 tname, tinst, tcell, &kvno);
2323 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2328 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2329 tname, tinst, tcell, *expTime, kvno));
2330 code = afsconf_IsLocalRealmMatch(confDir, &islocal, tname, tinst, tcell);
2333 ViceLog(0, ("FindClient: local realm check failed; code=%d", code));
2337 code = format_vname(uname, sizeof(uname), tname, tinst, tcell, islocal);
2339 ViceLog(0, ("FindClient: uname truncated."));
2343 /* translate the name to a vice id */
2344 code = MapName_r(uname, viceid);
2346 ViceLog(1, ("failed to map name=%s -> code=%d\n", uname,
2348 return code; /* Actually flag this is a failure */
2358 * Called by the server main loop. Returns a h_Held client, which must be
2359 * released later the main loop. Allocates a client if the matching one
2360 * isn't around. The client is returned with its reference count incremented
2361 * by one. The caller must call h_ReleaseClient_r when finished with
2364 * The refCount on client->host is returned incremented. h_ReleaseClient_r
2365 * does not decrement the refCount on client->host.
2368 h_FindClient_r(struct rx_connection *tcon)
2370 struct client *client;
2371 struct host *host = NULL;
2372 struct client *oldClient;
2373 afs_int32 viceid = 0;
2381 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2382 if (client && client->sid == rx_GetConnectionId(tcon)
2383 && client->VenusEpoch == rx_GetConnectionEpoch(tcon)
2384 && !(client->host->hostFlags & HOSTDELETED)
2385 && !client->deleted) {
2388 h_Hold_r(client->host);
2389 if (client->prfail != 2) {
2390 /* Could add shared lock on client here */
2391 /* note that we don't have to lock entry in this path to
2392 * ensure CPS is initialized, since we don't call rx_SetSpecific
2393 * until initialization is done, and we only get here if
2394 * rx_GetSpecific located the client structure.
2399 ObtainWriteLock(&client->lock); /* released at end */
2405 authClass = rx_SecurityClassOf(tcon);
2407 code = getPeerDetails(tcon, &viceid, &expTime, authClass);
2411 if (!client) { /* loop */
2412 host = h_GetHost_r(tcon); /* Returns with incremented refCount */
2418 /* First try to find the client structure */
2419 for (client = host->FirstClient; client; client = client->next) {
2420 if (!client->deleted && (client->sid == rx_GetConnectionId(tcon))
2421 && (client->VenusEpoch == rx_GetConnectionEpoch(tcon))) {
2424 ObtainWriteLock(&client->lock);
2430 /* Still no client structure - get one */
2433 if (host->hostFlags & HOSTDELETED) {
2438 /* Retry to find the client structure */
2439 for (client = host->FirstClient; client; client = client->next) {
2440 if (!client->deleted && (client->sid == rx_GetConnectionId(tcon))
2441 && (client->VenusEpoch == rx_GetConnectionEpoch(tcon))) {
2443 goto retryfirstclient;
2448 ObtainWriteLock(&client->lock);
2449 client->refCount = 1;
2450 client->host = host;
2451 client->InSameNetwork = host->InSameNetwork;
2452 client->ViceId = viceid;
2453 client->expTime = expTime; /* rx only */
2454 client->authClass = authClass; /* rx only */
2455 client->sid = rx_GetConnectionId(tcon);
2456 client->VenusEpoch = rx_GetConnectionEpoch(tcon);
2457 client->CPS.prlist_val = NULL;
2458 client->CPS.prlist_len = 0;
2462 client->prfail = fail;
2464 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
2465 client->CPS.prlist_len = 0;
2466 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
2467 free(client->CPS.prlist_val);
2468 client->CPS.prlist_val = NULL;
2469 client->ViceId = viceid;
2470 client->expTime = expTime;
2472 if (viceid == ANONYMOUSID) {
2473 client->CPS.prlist_len = AnonCPS.prlist_len;
2474 client->CPS.prlist_val = AnonCPS.prlist_val;
2477 code = hpr_GetCPS(viceid, &client->CPS);
2482 ("pr_GetCPS failed(%d) for user %d, host %" AFS_PTR_FMT " (%s:%d)\n",
2483 code, viceid, client->host,
2484 afs_inet_ntoa_r(client->host->host,hoststr),
2485 ntohs(client->host->port)));
2487 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2488 * all protection servers and reevaluates things if no
2489 * sync server or quorum is found we could still end up
2490 * with one of these errors. In such case we would like to
2491 * reevaluate the rpc call to find if there's cps for this
2492 * guy. We treat other errors (except network failures
2493 * ones - i.e. code < 0) as an indication that there is no
2494 * CPS for this host. Ideally we could like to deal this
2495 * problem the other way around (i.e. if code == NOCPS
2496 * ignore else retry next time) but the problem is that
2497 * there're other errors (i.e. EPERM) for which we don't
2498 * want to retry and we don't know the whole code list!
2500 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2504 /* the disabling of system:administrators is so iffy and has so many
2505 * possible failure modes that we will disable it again */
2506 /* Turn off System:Administrator for safety
2507 * if (AL_IsAMember(SystemId, client->CPS) == 0)
2508 * osi_Assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
2511 /* Now, tcon may already be set to a rock, since we blocked with no host
2512 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2513 * required). So, before setting the RPC's rock, we should disconnect
2514 * the RPC from the other client structure's rock.
2516 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2517 if (oldClient && oldClient != client
2518 && oldClient->sid == rx_GetConnectionId(tcon)
2519 && oldClient->VenusEpoch == rx_GetConnectionEpoch(tcon)
2520 && !(oldClient->host->hostFlags & HOSTDELETED)) {
2522 if (!oldClient->deleted) {
2523 /* if we didn't create it, it's not ours to put back */
2525 ViceLog(0, ("FindClient: stillborn client %p(%x); "
2526 "conn %p (host %s:%d) had client %p(%x)\n",
2527 client, client->sid, tcon,
2528 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2529 ntohs(rxr_PortOf(tcon)),
2530 oldClient, oldClient->sid));
2531 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2532 free(client->CPS.prlist_val);
2533 client->CPS.prlist_val = NULL;
2534 client->CPS.prlist_len = 0;
2536 /* We should perhaps check for 0 here */
2538 ReleaseWriteLock(&client->lock);
2543 oldClient->refCount++;
2545 h_Hold_r(oldClient->host);
2546 h_Release_r(client->host);
2549 ObtainWriteLock(&oldClient->lock);
2552 host = oldClient->host;
2554 ViceLog(0, ("FindClient: deleted client %p(%x ref %d host %p href "
2555 "%d) already had conn %p (host %s:%d, cid %x), stolen "
2556 "by client %p(%x, ref %d host %p href %d)\n",
2557 oldClient, oldClient->sid, oldClient->refCount,
2558 oldClient->host, oldClient->host->refCount, tcon,
2559 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2560 ntohs(rxr_PortOf(tcon)), rx_GetConnectionId(tcon),
2561 client, client->sid, client->refCount,
2562 client->host, client->host->refCount));
2563 /* rx_SetSpecific will be done immediately below */
2566 /* Avoid chaining in more than once. */
2570 if (host->hostFlags & HOSTDELETED) {
2575 client->host = NULL;
2577 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2578 free(client->CPS.prlist_val);
2579 client->CPS.prlist_val = NULL;
2580 client->CPS.prlist_len = 0;
2583 ReleaseWriteLock(&client->lock);
2588 client->next = host->FirstClient;
2589 host->FirstClient = client;
2591 CurrentConnections++; /* increment number of connections */
2593 rx_SetSpecific(tcon, rxcon_client_key, client);
2594 ReleaseWriteLock(&client->lock);
2598 } /*h_FindClient_r */
2601 h_ReleaseClient_r(struct client *client)
2603 osi_Assert(client->refCount > 0);
2610 * Sigh: this one is used to get the client AGAIN within the individual
2611 * server routines. This does not bother h_Holding the host, since
2612 * this is assumed already have been done by the server main loop.
2613 * It does check tokens, since only the server routines can return the
2614 * VICETOKENDEAD error code
2617 GetClient(struct rx_connection *tcon, struct client **cp)
2619 struct client *client;
2624 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2625 if (client == NULL) {
2627 ("GetClient: no client in conn %p (host %s:%d), VBUSYING\n",
2628 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2629 ntohs(rxr_PortOf(tcon))));
2633 if (rx_GetConnectionId(tcon) != client->sid
2634 || rx_GetConnectionEpoch(tcon) != client->VenusEpoch) {
2636 ("GetClient: tcon %p tcon sid %d client sid %d\n",
2637 tcon, rx_GetConnectionId(tcon), client->sid));
2641 if (client && client->LastCall > client->expTime && client->expTime) {
2643 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2644 afs_inet_ntoa_r(client->host->host, hoststr),
2645 ntohs(client->host->port), client->expTime));
2647 return VICETOKENDEAD;
2649 if (client->deleted) {
2650 ViceLog(0, ("GetClient: got deleted client, connection will appear "
2651 "anonymous; tcon %p cid %x client %p ref %d host %p "
2652 "(%s:%d) href %d ViceId %d\n",
2653 tcon, rx_GetConnectionId(tcon), client, client->refCount,
2655 afs_inet_ntoa_r(client->host->host, hoststr),
2656 (int)ntohs(client->host->port), client->host->refCount,
2657 (int)client->ViceId));
2667 PutClient(struct client **cp)
2673 h_ReleaseClient_r(*cp);
2680 /* Client user name for short term use. Note that this is NOT inexpensive */
2682 h_UserName(struct client *client)
2684 static char User[PR_MAXNAMELEN + 1];
2688 lids.idlist_len = 1;
2689 lids.idlist_val = malloc(1 * sizeof(afs_int32));
2690 if (!lids.idlist_val) {
2691 ViceLogThenPanic(0, ("Failed malloc in h_UserName\n"));
2693 lnames.namelist_len = 0;
2694 lnames.namelist_val = (prname *) 0;
2695 lids.idlist_val[0] = client->ViceId;
2696 if (hpr_IdToName(&lids, &lnames)) {
2697 /* We need to free id we alloced above! */
2698 free(lids.idlist_val);
2699 return "*UNKNOWN USER NAME*";
2701 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2702 free(lids.idlist_val);
2703 free(lnames.namelist_val);
2712 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2713 CEs, CEBlocks, HTs, HTBlocks));
2719 h_PrintClient(struct host *host, void *rock)
2721 StreamHandle_t *file = (StreamHandle_t *)rock;
2722 struct client *client;
2727 time_t LastCall, expTime;
2731 LastCall = host->LastCall;
2732 if (host->hostFlags & HOSTDELETED) {
2736 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2737 localtime_r(&LastCall, &tm));
2738 snprintf(tmpStr, sizeof tmpStr, "Host %s:%d down = %d, LastCall %s\n",
2739 afs_inet_ntoa_r(host->host, hoststr),
2740 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2742 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2743 for (client = host->FirstClient; client; client = client->next) {
2744 if (!client->deleted) {
2745 expTime = client->expTime;
2746 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2747 localtime_r(&expTime, &tm));
2748 snprintf(tmpStr, sizeof tmpStr,
2749 " user id=%d, name=%s, sl=%s till %s\n",
2750 client->ViceId, h_UserName(client),
2751 client->authClass ? "Authenticated"
2752 : "Not authenticated",
2753 client->authClass ? tbuffer : "No Limit");
2754 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2755 snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2756 client->CPS.prlist_len);
2757 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2758 if (client->CPS.prlist_val) {
2759 for (i = 0; i < client->CPS.prlist_len; i++) {
2760 snprintf(tmpStr, sizeof tmpStr, " %d",
2761 client->CPS.prlist_val[i]);
2762 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2765 sprintf(tmpStr, "]\n");
2766 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2772 } /*h_PrintClient */
2777 * Print a list of clients, with last security level and token value seen,
2781 h_PrintClients(void)
2788 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2792 ("Couldn't create client dump file %s\n",
2793 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2796 now = FT_ApproxTime();
2797 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2798 localtime_r(&now, &tm));
2799 snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n\n",
2801 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2802 h_Enumerate(h_PrintClient, (char *)file);
2803 STREAM_REALLYCLOSE(file);
2804 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2811 h_DumpHost(struct host *host, void *rock)
2813 StreamHandle_t *file = (StreamHandle_t *)rock;
2820 snprintf(tmpStr, sizeof tmpStr,
2821 "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u "
2822 "down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u "
2824 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
2825 host->index, host->cblist, CheckLock(&host->lock),
2826 host->LastCall, host->ActiveCall, (host->hostFlags & VENUSDOWN),
2827 host->hostFlags & HOSTDELETED, host->Console,
2828 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2830 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2831 if (host->hcps.prlist_val)
2832 for (i = 0; i < host->hcps.prlist_len; i++) {
2833 snprintf(tmpStr, sizeof tmpStr, " %d", host->hcps.prlist_val[i]);
2834 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2836 sprintf(tmpStr, "] [");
2837 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2838 if (host->interface)
2839 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2841 sprintf(tmpStr, " %s:%d",
2842 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2843 ntohs(host->interface->interface[i].port));
2844 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2846 sprintf(tmpStr, "] refCount:%d hostFlags:%hu\n", host->refCount, host->hostFlags);
2847 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2859 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2866 ("Couldn't create host dump file %s\n",
2867 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2870 now = FT_ApproxTime();
2871 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2872 localtime_r(&now, &tm));
2873 snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n\n", tbuffer);
2874 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2875 h_Enumerate(h_DumpHost, (char *)file);
2876 STREAM_REALLYCLOSE(file);
2877 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2881 #ifdef AFS_DEMAND_ATTACH_FS
2884 * host state serialization
2886 static int h_stateFillHeader(struct host_state_header * hdr);
2887 static int h_stateCheckHeader(struct host_state_header * hdr);
2888 static int h_stateAllocMap(struct fs_dump_state * state);
2889 static int h_stateSaveHost(struct host * host, void *rock);
2890 static int h_stateRestoreHost(struct fs_dump_state * state);
2891 static int h_stateRestoreIndex(struct host * h, void *rock);
2892 static int h_stateVerifyHost(struct host * h, void *rock);
2893 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
2894 afs_uint32 addr, afs_uint16 port, int valid);
2895 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2896 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2897 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2900 * Is this host busy?
2902 * This is just a hint and should not be trusted; this should probably only be
2903 * used by the host state serialization code when trying to detect if a host
2904 * can be sanely serialized to disk or not. If this function returns 1, the
2905 * host may be in an invalid state and thus should not be saved to disk.
2908 h_isBusy_r(struct host *host)
2910 struct Lock *hostLock = &host->lock;
2913 LOCK_LOCK(hostLock);
2914 if (hostLock->excl_locked || hostLock->readers_reading) {
2917 LOCK_UNLOCK(hostLock);
2923 if ((host->hostFlags & HWHO_INPROGRESS) || !(host->hostFlags & ALTADDR)) {
2924 /* We shouldn't hit this if the host wasn't locked, but just in case... */
2931 /* this procedure saves all host state to disk for fast startup */
2933 h_stateSave(struct fs_dump_state * state)
2935 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2938 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2940 /* invalidate host state header */
2941 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2943 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2944 sizeof(struct host_state_header))) {
2949 fs_stateIncEOF(state, sizeof(struct host_state_header));
2951 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2956 h_stateFillHeader(state->h_hdr);
2958 /* write the real header to disk */
2959 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2960 sizeof(struct host_state_header));
2967 * host state serialization
2969 * this procedure restores all host state from a disk for fast startup
2972 h_stateRestore(struct fs_dump_state * state)
2976 /* seek to the right position and read in the host state header */
2977 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2978 sizeof(struct host_state_header))) {
2983 /* check the validity of the header */
2984 if (h_stateCheckHeader(state->h_hdr)) {
2989 records = state->h_hdr->records;
2991 if (h_stateAllocMap(state)) {
2996 /* iterate over records restoring host state */
2997 for (i=0; i < records; i++) {
2998 if (h_stateRestoreHost(state) != 0) {
3009 h_stateRestoreIndices(struct fs_dump_state * state)
3011 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
3016 h_stateRestoreIndex(struct host * h, void *rock)
3018 struct fs_dump_state *state = (struct fs_dump_state *)rock;
3019 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
3020 return H_ENUMERATE_BAIL(0);
3026 h_stateVerify(struct fs_dump_state * state)
3028 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
3033 h_stateVerifyHost(struct host * h, void* rock)
3035 struct fs_dump_state *state = (struct fs_dump_state *)rock;
3039 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
3040 return H_ENUMERATE_BAIL(0);
3044 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
3045 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
3046 h->interface->interface[i].port,
3047 h->interface->interface[i].valid)) {
3051 if (h_stateVerifyUuidHash(state, h)) {
3054 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port, 1)) {
3058 if (cb_stateVerifyHCBList(state, h)) {
3066 * verify a host is either in, or absent from, the addr hash table.
3068 * @param[in] state fs dump state
3069 * @param[in] h host we're dealing with
3070 * @param[in] addr addr to look for (NBO)
3071 * @param[in] port port to look for (NBO)
3072 * @param[in] valid 1 if we're verifying that the specified addr and port
3073 * in the hash table point to the specified host. 0 if we're
3074 * verifying that the specified addr and port do NOT point
3075 * to the specified host
3077 * @return operation status
3078 * @retval 1 failed to verify, bail out
3079 * @retval 0 verified successfully, all is well
3082 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
3083 afs_uint32 addr, afs_uint16 port, int valid)
3085 int ret = 0, found = 0;
3086 struct host *host = NULL;
3087 struct h_AddrHashChain *chain;
3088 int index = h_HashIndex(addr);
3092 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
3093 host = chain->hostPtr;
3095 afs_inet_ntoa_r(addr, tmp);
3096 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
3100 if ((chain->addr == addr) && (chain->port == port)) {
3103 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry "
3104 "points to different host struct (%d, %d)\n",
3105 h->index, host->index));
3106 state->flags.warnings_generated = 1;
3110 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s:%u is "
3111 "marked invalid, but points to the containing "
3112 "host\n", afs_inet_ntoa_r(addr, tmp),
3113 (unsigned)htons(port)));
3121 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
3122 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
3123 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
3130 if (!found && valid) {
3131 afs_inet_ntoa_r(addr, tmp);
3132 if (state->mode == FS_STATE_LOAD_MODE) {
3133 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s:%u not found in hash\n",
3134 tmp, (unsigned)htons(port)));
3138 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s:%u not found in hash\n",
3139 tmp, (unsigned)htons(port)));
3140 state->flags.warnings_generated = 1;
3149 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
3151 int ret = 0, found = 0;
3152 struct host *host = NULL;
3153 struct h_UuidHashChain *chain;
3154 afsUUID * uuidp = &h->interface->uuid;
3155 int index = h_UuidHashIndex(uuidp);
3159 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
3160 host = chain->hostPtr;
3162 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
3163 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
3167 if (host->interface &&
3168 afs_uuid_equal(&host->interface->uuid, uuidp)) {
3170 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
3171 h->index, host->index));
3172 state->flags.warnings_generated = 1;
3177 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
3178 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
3179 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
3187 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
3188 if (state->mode == FS_STATE_LOAD_MODE) {
3189 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
3193 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
3194 state->flags.warnings_generated = 1;
3202 /* create the host state header structure */
3204 h_stateFillHeader(struct host_state_header * hdr)
3206 hdr->stamp.magic = HOST_STATE_MAGIC;
3207 hdr->stamp.version = HOST_STATE_VERSION;
3211 /* check the contents of the host state header structure */
3213 h_stateCheckHeader(struct host_state_header * hdr)
3217 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
3218 ViceLog(0, ("check_host_state_header: invalid state header\n"));
3221 else if (hdr->stamp.version != HOST_STATE_VERSION) {
3222 ViceLog(0, ("check_host_state_header: unknown version number\n"));
3228 /* allocate the host id mapping table */
3230 h_stateAllocMap(struct fs_dump_state * state)
3232 state->h_map.len = state->h_hdr->index_max + 1;
3233 state->h_map.entries = (struct idx_map_entry_t *)
3234 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
3235 return (state->h_map.entries != NULL) ? 0 : 1;
3238 /* function called by h_Enumerate to save a host to disk */
3240 h_stateSaveHost(struct host * host, void* rock)
3242 struct fs_dump_state *state = (struct fs_dump_state *) rock;
3243 int if_len=0, hcps_len=0;
3244 struct hostDiskEntry hdsk;
3245 struct host_state_entry_header hdr;
3246 struct Interface * ifp = NULL;
3247 afs_int32 * hcps = NULL;
3248 struct iovec iov[4];
3251 if (h_isBusy_r(host)) {
3253 ViceLog(1, ("Not saving host %s:%d to disk; host appears busy\n",
3254 afs_inet_ntoa_r(host->host, hoststr), (int)ntohs(host->port)));
3255 /* Make sure we don't try to save callbacks to disk for this host, or
3256 * we'll get confused on restore */
3257 DeleteAllCallBacks_r(host, 1);
3261 memset(&hdr, 0, sizeof(hdr));
3263 if (state->h_hdr->index_max < host->index) {
3264 state->h_hdr->index_max = host->index;
3267 h_hostToDiskEntry_r(host, &hdsk);
3268 if (host->interface) {
3269 if_len = sizeof(struct Interface) +
3270 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
3271 ifp = malloc(if_len);
3272 osi_Assert(ifp != NULL);
3273 memcpy(ifp, host->interface, if_len);
3274 hdr.interfaces = host->interface->numberOfInterfaces;
3275 iov[iovcnt].iov_base = (char *) ifp;
3276 iov[iovcnt].iov_len = if_len;
3279 if (host->hcps.prlist_val) {
3280 hdr.hcps = host->hcps.prlist_len;
3281 hcps_len = hdr.hcps * sizeof(afs_int32);
3282 hcps = malloc(hcps_len);
3283 osi_Assert(hcps != NULL);
3284 memcpy(hcps, host->hcps.prlist_val, hcps_len);
3285 iov[iovcnt].iov_base = (char *) hcps;
3286 iov[iovcnt].iov_len = hcps_len;
3290 if (hdsk.index > state->h_hdr->index_max)
3291 state->h_hdr->index_max = hdsk.index;
3293 hdr.len = sizeof(struct host_state_entry_header) +
3294 sizeof(struct hostDiskEntry) + if_len + hcps_len;
3295 hdr.magic = HOST_STATE_ENTRY_MAGIC;
3297 iov[0].iov_base = (char *) &hdr;
3298 iov[0].iov_len = sizeof(hdr);
3299 iov[1].iov_base = (char *) &hdsk;
3300 iov[1].iov_len = sizeof(struct hostDiskEntry);
3302 if (fs_stateWriteV(state, iov, iovcnt)) {
3303 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
3307 fs_stateIncEOF(state, hdr.len);
3309 state->h_hdr->records++;
3316 return H_ENUMERATE_BAIL(0);
3321 /* restores a host from disk */
3323 h_stateRestoreHost(struct fs_dump_state * state)
3325 int ifp_len=0, hcps_len=0, bail=0;
3326 struct host_state_entry_header hdr;
3327 struct hostDiskEntry hdsk;
3328 struct host *host = NULL;
3329 struct Interface *ifp = NULL;
3330 afs_int32 * hcps = NULL;
3331 struct iovec iov[3];
3334 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
3335 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
3341 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
3342 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
3348 iov[0].iov_base = (char *) &hdsk;
3349 iov[0].iov_len = sizeof(struct hostDiskEntry);
3351 if (hdr.interfaces) {
3352 ifp_len = sizeof(struct Interface) +
3353 ((hdr.interfaces-1) * sizeof(struct AddrPort));
3354 ifp = malloc(ifp_len);
3355 osi_Assert(ifp != NULL);
3356 iov[iovcnt].iov_base = (char *) ifp;
3357 iov[iovcnt].iov_len = ifp_len;
3361 hcps_len = hdr.hcps * sizeof(afs_int32);
3362 hcps = malloc(hcps_len);
3363 osi_Assert(hcps != NULL);
3364 iov[iovcnt].iov_base = (char *) hcps;
3365 iov[iovcnt].iov_len = hcps_len;
3369 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
3370 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
3375 if (fs_stateReadV(state, iov, iovcnt)) {
3376 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
3381 if (!hdr.hcps && hdsk.hcps_valid) {
3382 /* valid, zero-length host cps ; does this ever happen? */
3383 hcps = malloc(sizeof(afs_int32));
3384 osi_Assert(hcps != NULL);
3387 if ((hdsk.hostFlags & HWHO_INPROGRESS) || !(hdsk.hostFlags & ALTADDR)) {
3389 ViceLog(0, ("h_stateRestoreHost: skipping host %s:%d due to invalid flags 0x%x\n",
3390 afs_inet_ntoa_r(hdsk.host, hoststr), (int)ntohs(hdsk.port),
3391 (unsigned)hdsk.hostFlags));
3393 state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_SKIPPED;
3397 /* for restoring state, we better be able to get a host! */
3399 osi_Assert(host != NULL);
3402 host->interface = ifp;
3405 host->hcps.prlist_val = hcps;
3406 host->hcps.prlist_len = hdr.hcps;
3409 h_diskEntryToHost_r(&hdsk, host);
3410 h_SetupCallbackConn_r(host);
3412 h_AddHostToAddrHashTable_r(host->host, host->port, host);
3415 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
3416 if (ifp->interface[i].valid &&
3417 !(ifp->interface[i].addr == host->host &&
3418 ifp->interface[i].port == host->port)) {
3419 h_AddHostToAddrHashTable_r(ifp->interface[i].addr,
3420 ifp->interface[i].port,
3424 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
3426 h_InsertList_r(host);
3428 /* setup host id map entry */
3429 state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_VALID;
3430 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
3431 state->h_map.entries[hdsk.index].new_idx = host->index;
3443 /* serialize a host structure to disk */
3445 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
3447 out->host = in->host;
3448 out->port = in->port;
3449 out->hostFlags = in->hostFlags;
3450 out->Console = in->Console;
3451 out->hcpsfailed = in->hcpsfailed;
3452 out->LastCall = in->LastCall;
3453 out->ActiveCall = in->ActiveCall;
3454 out->cpsCall = in->cpsCall;
3455 out->cblist = in->cblist;
3456 out->InSameNetwork = in->InSameNetwork;
3458 /* special fields we save, but are not memcpy'd back on restore */
3459 out->index = in->index;
3460 out->hcps_len = in->hcps.prlist_len;
3461 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
3464 /* restore a host structure from disk */
3466 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
3468 out->host = in->host;
3469 out->port = in->port;
3470 out->hostFlags = in->hostFlags;
3471 out->Console = in->Console;
3472 out->hcpsfailed = in->hcpsfailed;
3473 out->LastCall = in->LastCall;
3474 out->ActiveCall = in->ActiveCall;
3475 out->cpsCall = in->cpsCall;
3476 out->cblist = in->cblist;
3477 out->InSameNetwork = in->InSameNetwork;
3480 /* index translation routines */
3482 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
3486 /* hosts use a zero-based index, so old==0 is valid */
3488 if (old >= state->h_map.len) {
3489 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
3491 } else if (state->h_map.entries[old].valid != FS_STATE_IDX_VALID ||
3492 state->h_map.entries[old].old_idx != old) { /* sanity check */
3493 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
3496 *new = state->h_map.entries[old].new_idx;
3501 #endif /* AFS_DEMAND_ATTACH_FS */
3505 * This counts the number of workstations, the number of active workstations,
3506 * and the number of workstations declared "down" (i.e. not heard from
3507 * recently). An active workstation has received a call since the cutoff
3508 * time argument passed.
3511 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3514 int num = 0, active = 0, del = 0;
3518 for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
3519 if (!(host->hostFlags & HOSTDELETED)) {
3521 if (host->ActiveCall > cutofftime)
3523 if (host->hostFlags & VENUSDOWN)
3527 if (count != hostCount) {
3528 ViceLog(0, ("h_GetWorkStats found %d of %d hosts\n", count, hostCount));
3529 } else if (host != NULL) {
3530 ViceLog(0, ("h_GetWorkStats found more than %d hosts\n", hostCount));
3531 ShutDownAndCore(PANIC);
3541 } /*h_GetWorkStats */
3544 h_GetWorkStats64(afs_uint64 *nump, afs_uint64 *activep, afs_uint64 *delp,
3545 afs_int32 cutofftime)
3547 int num, active, del;
3548 h_GetWorkStats(&num, &active, &del, cutofftime);
3557 /*------------------------------------------------------------------------
3558 * PRIVATE h_ClassifyAddress
3561 * Given a target IP address and a candidate IP address (both
3562 * in host byte order), classify the candidate into one of three
3563 * buckets in relation to the target by bumping the counters passed
3567 * a_targetAddr : Target address.
3568 * a_candAddr : Candidate address.
3569 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3570 * addresses are either in the same network
3571 * or the same subnet.
3572 * a_diffSubnetP : ...when the candidate is in a different
3574 * a_diffNetworkP : ...when the candidate is in a different
3581 * The target and candidate addresses are both in host byte
3582 * order, NOT network byte order, when passed in.
3586 *------------------------------------------------------------------------*/
3589 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3590 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3591 afs_int32 * a_diffNetworkP)
3592 { /*h_ClassifyAddress */
3594 afs_uint32 targetNet;
3595 afs_uint32 targetSubnet;
3597 afs_uint32 candSubnet;