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>
35 #ifdef AFS_ATHENA_STDENV
39 #include <afs/ptclient.h>
40 #include <afs/ptuser.h>
41 #include <afs/prs_fs.h>
43 #include <afs/afsutil.h>
44 #include <afs/com_err.h>
46 #include <afs/cellconfig.h>
47 #include "viced_prototypes.h"
51 #ifdef AFS_DEMAND_ATTACH_FS
52 #include "../util/afsutil_prototypes.h"
53 #include "../tviced/serialize_state.h"
54 #endif /* AFS_DEMAND_ATTACH_FS */
56 #ifdef AFS_PTHREAD_ENV
57 pthread_mutex_t host_glock_mutex;
58 #endif /* AFS_PTHREAD_ENV */
61 extern int CurrentConnections;
63 extern int AnonymousID;
64 extern prlist AnonCPS;
66 extern struct afsconf_dir *confDir; /* config dir object */
67 extern int lwps; /* the max number of server threads */
68 extern afsUUID FS_HostUUID;
71 int CEs = 0; /* active clients */
72 int CEBlocks = 0; /* number of blocks of CEs */
73 struct client *CEFree = 0; /* first free client */
74 struct host *hostList = 0; /* linked list of all hosts */
75 int hostCount = 0; /* number of hosts in hostList */
79 static struct rx_securityClass *sc = NULL;
81 static void h_SetupCallbackConn_r(struct host * host);
82 static int h_threadquota(int);
84 #define CESPERBLOCK 73
85 struct CEBlock { /* block of CESPERBLOCK file entries */
86 struct client entry[CESPERBLOCK];
89 void h_TossStuff_r(struct host *host);
92 * Make sure the subnet macros have been defined.
95 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
98 #ifndef IN_CLASSA_SUBNET
99 #define IN_CLASSA_SUBNET 0xffff0000
103 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
106 #ifndef IN_CLASSB_SUBNET
107 #define IN_CLASSB_SUBNET 0xffffff00
111 /* get a new block of CEs and chain it on CEFree */
115 struct CEBlock *block;
118 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
120 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
121 ShutDownAndCore(PANIC);
124 for (i = 0; i < (CESPERBLOCK - 1); i++) {
125 Lock_Init(&block->entry[i].lock);
126 block->entry[i].next = &(block->entry[i + 1]);
128 block->entry[CESPERBLOCK - 1].next = 0;
129 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
130 CEFree = (struct client *)block;
136 /* get the next available CE */
137 static struct client *
140 struct client *entry;
145 ViceLog(0, ("CEFree NULL in GetCE\n"));
146 ShutDownAndCore(PANIC);
150 CEFree = entry->next;
152 memset(entry, 0, CLIENT_TO_ZERO(entry));
158 /* return an entry to the free list */
160 FreeCE(struct client *entry)
162 entry->VenusEpoch = 0;
164 entry->next = CEFree;
171 * The HTs and HTBlocks variables were formerly static, but they are
172 * now referenced elsewhere in the FileServer.
174 int HTs = 0; /* active file entries */
175 int HTBlocks = 0; /* number of blocks of HTs */
176 static struct host *HTFree = 0; /* first free file entry */
179 * Hash tables of host pointers. We need two tables, one
180 * to map IP addresses onto host pointers, and another
181 * to map host UUIDs onto host pointers.
183 static struct h_AddrHashChain *hostAddrHashTable[h_HASHENTRIES];
184 static struct h_UuidHashChain *hostUuidHashTable[h_HASHENTRIES];
185 #define h_HashIndex(hostip) (ntohl(hostip) & (h_HASHENTRIES-1))
186 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
188 struct HTBlock { /* block of HTSPERBLOCK file entries */
189 struct host entry[h_HTSPERBLOCK];
193 /* get a new block of HTs and chain it on HTFree */
197 struct HTBlock *block;
199 static int index = 0;
201 if (HTBlocks == h_MAXHOSTTABLES) {
202 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
206 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
208 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
209 ShutDownAndCore(PANIC);
211 #ifdef AFS_PTHREAD_ENV
212 for (i = 0; i < (h_HTSPERBLOCK); i++)
213 CV_INIT(&block->entry[i].cond, "block entry", CV_DEFAULT, 0);
214 #endif /* AFS_PTHREAD_ENV */
215 for (i = 0; i < (h_HTSPERBLOCK); i++)
216 Lock_Init(&block->entry[i].lock);
217 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
218 block->entry[i].next = &(block->entry[i + 1]);
219 for (i = 0; i < (h_HTSPERBLOCK); i++)
220 block->entry[i].index = index++;
221 block->entry[h_HTSPERBLOCK - 1].next = 0;
222 HTFree = (struct host *)block;
223 hosttableptrs[HTBlocks++] = block->entry;
228 /* get the next available HT */
239 HTFree = entry->next;
241 memset(entry, 0, HOST_TO_ZERO(entry));
247 /* return an entry to the free list */
249 FreeHT(struct host *entry)
251 entry->next = HTFree;
258 hpr_Initialize(struct ubik_client **uclient)
261 struct rx_connection *serverconns[MAXSERVERS];
262 struct rx_securityClass *sc;
263 struct afsconf_dir *tdir;
265 struct afsconf_cell info;
269 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
271 ViceLog(0, ("hpr_Initialize: Could not open configuration directory: %s", AFSDIR_SERVER_ETC_DIRPATH));
275 code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
277 ViceLog(0, ("hpr_Initialize: Could not get local cell. [%d]", code));
282 code = afsconf_GetCellInfo(tdir, cellstr, "afsprot", &info);
284 ViceLog(0, ("hpr_Initialize: Could not locate cell %s in %s/%s",
285 cellstr, confDir->name, AFSDIR_CELLSERVDB_FILE));
292 ViceLog(0, ("hpr_Initialize: Could not initialize rx."));
297 /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
298 * to force use of the KeyFile. secLevel == 0 implies -noauth was
300 code = afsconf_ClientAuthSecure(tdir, &sc, &scIndex);
302 ViceLog(0, ("hpr_Initialize: clientauthsecure returns %d %s "
303 "(so trying noauth)", code, afs_error_message(code)));
304 scIndex = RX_SECIDX_NULL;
305 sc = rxnull_NewClientSecurityObject();
308 if (scIndex == RX_SECIDX_NULL)
309 ViceLog(0, ("hpr_Initialize: Could not get afs tokens, "
310 "running unauthenticated. [%d]", code));
312 memset(serverconns, 0, sizeof(serverconns)); /* terminate list!!! */
313 for (i = 0; i < info.numServers; i++) {
315 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
316 info.hostAddr[i].sin_port, PRSRV,
320 code = ubik_ClientInit(serverconns, uclient);
322 ViceLog(0, ("hpr_Initialize: ubik client init failed. [%d]", code));
325 code = rxs_Release(sc);
330 hpr_End(struct ubik_client *uclient)
335 code = ubik_ClientDestroy(uclient);
341 hpr_GetHostCPS(afs_int32 host, prlist *CPS)
343 #ifdef AFS_PTHREAD_ENV
346 struct ubik_client *uclient =
347 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
350 code = hpr_Initialize(&uclient);
352 osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
358 code = ubik_PR_GetHostCPS(uclient, 0, host, CPS, &over);
359 if (code != PRSUCCESS)
362 /* do something about this, probably make a new call */
363 /* don't forget there's a hard limit in the interface */
365 "membership list for host id %d exceeds display limit\n",
370 return pr_GetHostCPS(host, CPS);
375 hpr_NameToId(namelist *names, idlist *ids)
377 #ifdef AFS_PTHREAD_ENV
380 struct ubik_client *uclient =
381 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
384 code = hpr_Initialize(&uclient);
386 osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
391 for (i = 0; i < names->namelist_len; i++)
392 stolower(names->namelist_val[i]);
393 code = ubik_PR_NameToID(uclient, 0, names, ids);
396 return pr_NameToId(names, ids);
401 hpr_IdToName(idlist *ids, namelist *names)
403 #ifdef AFS_PTHREAD_ENV
405 struct ubik_client *uclient =
406 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
409 code = hpr_Initialize(&uclient);
411 osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
416 code = ubik_PR_IDToName(uclient, 0, ids, names);
419 return pr_IdToName(ids, names);
424 hpr_GetCPS(afs_int32 id, prlist *CPS)
426 #ifdef AFS_PTHREAD_ENV
429 struct ubik_client *uclient =
430 (struct ubik_client *)pthread_getspecific(viced_uclient_key);
433 code = hpr_Initialize(&uclient);
435 osi_Assert(pthread_setspecific(viced_uclient_key, (void *)uclient) == 0);
441 code = ubik_PR_GetCPS(uclient, 0, id, CPS, &over);
442 if (code != PRSUCCESS)
445 /* do something about this, probably make a new call */
446 /* don't forget there's a hard limit in the interface */
447 fprintf(stderr, "membership list for id %d exceeds display limit\n",
452 return pr_GetCPS(id, CPS);
456 static short consolePort = 0;
459 h_Lock_r(struct host *host)
469 * returns 1 if already locked
470 * else returns locks and returns 0
474 h_NBLock_r(struct host *host)
476 struct Lock *hostLock = &host->lock;
481 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
482 hostLock->excl_locked = WRITE_LOCK;
486 LOCK_UNLOCK(hostLock);
495 #if FS_STATS_DETAILED
496 /*------------------------------------------------------------------------
497 * PRIVATE h_AddrInSameNetwork
500 * Given a target IP address and a candidate IP address (both
501 * in host byte order), return a non-zero value (1) if the
502 * candidate address is in a different network from the target
506 * a_targetAddr : Target address.
507 * a_candAddr : Candidate address.
510 * 1 if the candidate address is in the same net as the target,
514 * The target and candidate addresses are both in host byte
515 * order, NOT network byte order, when passed in. We return
516 * our value as a character, since that's the type of field in
517 * the host structure, where this info will be stored.
521 *------------------------------------------------------------------------*/
524 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
525 { /*h_AddrInSameNetwork */
527 afs_uint32 targetNet;
531 * Pull out the network and subnetwork numbers from the target
532 * and candidate addresses. We can short-circuit this whole
533 * affair if the target and candidate addresses are not of the
536 if (IN_CLASSA(a_targetAddr)) {
537 if (!(IN_CLASSA(a_candAddr))) {
540 targetNet = a_targetAddr & IN_CLASSA_NET;
541 candNet = a_candAddr & IN_CLASSA_NET;
542 } else if (IN_CLASSB(a_targetAddr)) {
543 if (!(IN_CLASSB(a_candAddr))) {
546 targetNet = a_targetAddr & IN_CLASSB_NET;
547 candNet = a_candAddr & IN_CLASSB_NET;
548 } /*Class B target */
549 else if (IN_CLASSC(a_targetAddr)) {
550 if (!(IN_CLASSC(a_candAddr))) {
553 targetNet = a_targetAddr & IN_CLASSC_NET;
554 candNet = a_candAddr & IN_CLASSC_NET;
555 } /*Class C target */
557 targetNet = a_targetAddr;
558 candNet = a_candAddr;
559 } /*Class D address */
562 * Now, simply compare the extracted net values for the two addresses
563 * (which at this point are known to be of the same class)
565 if (targetNet == candNet)
570 } /*h_AddrInSameNetwork */
571 #endif /* FS_STATS_DETAILED */
574 /* Assumptions: called with held host */
576 h_gethostcps_r(struct host *host, afs_int32 now)
581 /* wait if somebody else is already doing the getCPS call */
582 while (host->hostFlags & HCPS_INPROGRESS) {
583 slept = 1; /* I did sleep */
584 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
585 #ifdef AFS_PTHREAD_ENV
586 CV_WAIT(&host->cond, &host_glock_mutex);
587 #else /* AFS_PTHREAD_ENV */
588 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
589 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
590 #endif /* AFS_PTHREAD_ENV */
594 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
595 if (host->hcps.prlist_val)
596 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
597 host->hcps.prlist_val = NULL;
598 host->hcps.prlist_len = 0;
599 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
602 code = hpr_GetHostCPS(ntohl(host->host), &host->hcps);
607 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
608 * and reevaluates things if no sync server or quorum is found we could still end up
609 * with one of these errors. In such case we would like to reevaluate the rpc call to
610 * find if there's cps for this guy. We treat other errors (except network failures
611 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
612 * we could like to deal this problem the other way around (i.e. if code == NOCPS
613 * ignore else retry next time) but the problem is that there're other errors (i.e.
614 * EPERM) for which we don't want to retry and we don't know the whole code list!
616 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
618 * We would have preferred to use a while loop and try again since ops in protected
619 * acls for this host will fail now but they'll be reevaluated on any subsequent
620 * call. The attempt to wait for a quorum/sync site or network error won't work
621 * since this problems really should only occurs during a complete fileserver
622 * restart. Since the fileserver will start before the ptservers (and thus before
623 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
625 host->hcpsfailed = 1;
627 ("Warning: GetHostCPS failed (%d) for %p (%s:%d); will retry\n",
628 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
630 host->hcpsfailed = 0;
632 ("gethost: GetHostCPS failed (%d) for %p (%s:%d); ignored\n",
633 code, host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
635 if (host->hcps.prlist_val)
636 free(host->hcps.prlist_val);
637 host->hcps.prlist_val = NULL;
638 host->hcps.prlist_len = 0; /* Make sure it's zero */
640 host->hcpsfailed = 0;
642 host->hostFlags &= ~HCPS_INPROGRESS;
643 /* signal all who are waiting */
644 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
645 host->hostFlags &= ~HCPS_WAITING;
646 #ifdef AFS_PTHREAD_ENV
647 CV_BROADCAST(&host->cond);
648 #else /* AFS_PTHREAD_ENV */
649 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
650 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
651 #endif /* AFS_PTHREAD_ENV */
655 /* args in net byte order */
657 h_flushhostcps(afs_uint32 hostaddr, afs_uint16 hport)
662 h_Lookup_r(hostaddr, hport, &host);
664 host->hcpsfailed = 1;
673 * Allocate a host. It will be identified by the peer (ip,port) info in the
674 * rx connection provided. The host is returned held and locked
676 #define DEF_ROPCONS 2115
679 h_Alloc_r(struct rx_connection *r_con)
681 struct servent *serverentry;
683 #if FS_STATS_DETAILED
684 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
685 #endif /* FS_STATS_DETAILED */
691 host->host = rxr_HostOf(r_con);
692 host->port = rxr_PortOf(r_con);
694 h_AddHostToAddrHashTable_r(host->host, host->port, host);
696 if (consolePort == 0) { /* find the portal number for console */
697 #if defined(AFS_OSF_ENV)
698 serverentry = getservbyname("ropcons", "");
700 serverentry = getservbyname("ropcons", 0);
703 consolePort = serverentry->s_port;
705 consolePort = htons(DEF_ROPCONS); /* Use a default */
707 if (host->port == consolePort)
709 /* Make a callback channel even for the console, on the off chance that it
710 * makes a request that causes a break call back. It shouldn't. */
711 h_SetupCallbackConn_r(host);
712 host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
714 host->hcps.prlist_val = NULL;
715 host->hcps.prlist_len = 0;
716 host->interface = NULL;
718 host->hcpsfailed = 0; /* save cycles */
719 h_gethostcps(host); /* do this under host hold/lock */
721 host->FirstClient = NULL;
724 h_InsertList_r(host); /* update global host List */
725 #if FS_STATS_DETAILED
727 * Compare the new host's IP address (in host byte order) with ours
728 * (the File Server's), remembering if they are in the same network.
730 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
731 host->InSameNetwork =
732 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
733 #endif /* FS_STATS_DETAILED */
740 /* Make a callback channel even for the console, on the off chance that it
741 * makes a request that causes a break call back. It shouldn't. */
743 h_SetupCallbackConn_r(struct host * host)
746 sc = rxnull_NewClientSecurityObject();
747 host->callback_rxcon =
748 rx_NewConnection(host->host, host->port, 1, sc, 0);
749 rx_SetConnDeadTime(host->callback_rxcon, 50);
750 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
751 rx_SetConnSecondsUntilNatPing(host->callback_rxcon, 20);
755 * Lookup a host given an IP address and UDP port number.
756 * hostaddr and hport are in network order
757 * hostaddr and hport are in network order
758 * On return, refCount is incremented.
761 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, struct host **hostp)
764 struct host *host = NULL;
765 struct h_AddrHashChain *chain;
766 int index = h_HashIndex(haddr);
767 extern int hostaclRefresh;
770 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
771 host = chain->hostPtr;
773 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
774 && chain->port == hport) {
775 if ((host->hostFlags & HWHO_INPROGRESS) &&
776 h_threadquota(host->lock.num_waiting)) {
782 if (host->hostFlags & HOSTDELETED) {
789 now = FT_ApproxTime(); /* always evaluate "now" */
790 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
792 * Every hostaclRefresh period (def 2 hrs) get the new
793 * membership list for the host. Note this could be the
794 * first time that the host is added to a group. Also
795 * here we also retry on previous legitimate hcps failures.
797 * If we get here refCount is elevated.
799 h_gethostcps_r(host, now);
809 /* Lookup a host given its UUID. */
811 h_LookupUuid_r(afsUUID * uuidp)
813 struct host *host = 0;
814 struct h_UuidHashChain *chain;
815 int index = h_UuidHashIndex(uuidp);
817 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
818 host = chain->hostPtr;
820 if (!(host->hostFlags & HOSTDELETED) && host->interface
821 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
829 /* h_TossStuff_r: Toss anything in the host structure (the host or
830 * clients marked for deletion. Called from h_Release_r ONLY.
831 * To be called, there must be no holds, and either host->deleted
832 * or host->clientDeleted must be set.
835 h_TossStuff_r(struct host *host)
837 struct client **cp, *client;
840 /* make sure host doesn't go away over h_NBLock_r */
843 code = h_NBLock_r(host);
845 /* don't use h_Release_r, since that may call h_TossStuff_r again */
848 /* if somebody still has this host locked */
852 ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT " (%s:%d) was locked.\n",
853 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
859 /* if somebody still has this host held */
860 /* we must check this _after_ h_NBLock_r, since h_NBLock_r can drop and
861 * reacquire H_LOCK */
862 if (host->refCount > 0) {
865 ("Warning: h_TossStuff_r failed; Host %" AFS_PTR_FMT " (%s:%d) was held.\n",
866 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
870 /* ASSUMPTION: rxi_FreeConnection() does not yield */
871 for (cp = &host->FirstClient; (client = *cp);) {
872 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
874 ObtainWriteLockNoBlock(&client->lock, code);
878 ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
879 "client %p was locked.\n",
880 host, afs_inet_ntoa_r(host->host, hoststr),
881 ntohs(host->port), client));
885 if (client->refCount) {
888 ("Warning: h_TossStuff_r failed: Host %p (%s:%d) "
889 "client %p refcount %d.\n",
890 host, afs_inet_ntoa_r(host->host, hoststr),
891 ntohs(host->port), client, client->refCount));
892 /* This is the same thing we do if the host is locked */
893 ReleaseWriteLock(&client->lock);
896 client->CPS.prlist_len = 0;
897 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
898 free(client->CPS.prlist_val);
899 client->CPS.prlist_val = NULL;
900 CurrentConnections--;
902 ReleaseWriteLock(&client->lock);
908 /* We've just cleaned out all the deleted clients; clear the flag */
909 host->hostFlags &= ~CLIENTDELETED;
911 if (host->hostFlags & HOSTDELETED) {
912 struct rx_connection *rxconn;
913 struct AddrPort hostAddrPort;
916 if (host->Console & 1)
918 if ((rxconn = host->callback_rxcon)) {
919 host->callback_rxcon = (struct rx_connection *)0;
920 rx_DestroyConnection(rxconn);
922 if (host->hcps.prlist_val)
923 free(host->hcps.prlist_val);
924 host->hcps.prlist_val = NULL;
925 host->hcps.prlist_len = 0;
926 DeleteAllCallBacks_r(host, 1);
927 host->hostFlags &= ~RESETDONE; /* just to be safe */
929 /* if alternate addresses do not exist */
930 if (!(host->interface)) {
931 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
933 h_DeleteHostFromUuidHashTable_r(host);
934 h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
935 /* delete the hash entry for each valid alternate addresses */
936 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
937 hostAddrPort = host->interface->interface[i];
939 * if the interface addr/port is the primary, we already
940 * removed it. If the addr/port is not valid, its not
943 if (hostAddrPort.valid &&
944 (host->host != hostAddrPort.addr ||
945 host->port != hostAddrPort.port))
946 h_DeleteHostFromAddrHashTable_r(hostAddrPort.addr, hostAddrPort.port, host);
948 free(host->interface);
949 host->interface = NULL;
950 } /* if alternate address exists */
952 h_DeleteList_r(host); /* remove host from global host List */
959 /* h_Enumerate: Calls (*proc)(host, param) for at least each host in the
960 * system at the start of the enumeration (perhaps more). Hosts may be deleted
961 * (have delete flag set); ditto for clients. refCount is always incremented
962 * before (*proc) is called.
964 * The return value of the proc is a set of flags. The proc should set
965 * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
968 h_Enumerate(int (*proc) (struct host*, void *), void *param)
970 struct host *host, **list;
975 if (hostCount == 0) {
979 list = (struct host **)malloc(hostCount * sizeof(struct host *));
981 ViceLogThenPanic(0, ("Failed malloc in h_Enumerate (list)\n"));
983 for (totalCount = count = 0, host = hostList;
984 host && totalCount < hostCount;
985 host = host->next, totalCount++) {
987 if (!(host->hostFlags & HOSTDELETED)) {
993 if (totalCount != hostCount) {
994 ViceLog(0, ("h_Enumerate found %d of %d hosts\n", totalCount, hostCount));
995 } else if (host != NULL) {
996 ViceLog(0, ("h_Enumerate found more than %d hosts\n", hostCount));
997 ShutDownAndCore(PANIC);
1000 for (i = 0; i < count; i++) {
1002 flags = (*proc) (list[i], param);
1004 h_Release_r(list[i]);
1006 /* bail out of the enumeration early */
1007 if (H_ENUMERATE_ISSET_BAIL(flags)) {
1010 ViceLog(0, ("h_Enumerate got back invalid return value %d\n", flags));
1011 ShutDownAndCore(PANIC);
1015 /* we bailed out of enumerating hosts early; we still have holds on
1016 * some of the hosts in 'list', so release them */
1019 for ( ; i < count; i++) {
1020 h_Release_r(list[i]);
1028 /* h_Enumerate_r (revised):
1029 * Calls (*proc)(host, param) for each host in hostList, starting
1030 * at enumstart. Called only under H_LOCK. Hosts may be deleted (have
1031 * delete flag set); ditto for clients. refCount is always incremented
1032 * before (*proc) is called.
1034 * @note Assumes that hostList is only prepended to, that a host is never
1035 * inserted into the middle. Otherwise this would not be guaranteed to
1038 * The return value of the proc is a set of flags. The proc should set
1039 * H_ENUMERATE_BAIL(foo) if the enumeration of hosts should be stopped early.
1042 h_Enumerate_r(int (*proc) (struct host *, void *),
1043 struct host *enumstart, void *param)
1045 struct host *host, *next;
1049 if (hostCount == 0) {
1056 /* find the first non-deleted host, so we know where to actually start
1058 for (count = 0; host && count < hostCount; count++) {
1059 if (!(host->hostFlags & HOSTDELETED)) {
1066 /* we didn't find a non-deleted host... */
1068 if (host && count >= hostCount) {
1069 /* ...because we found a loop */
1070 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", hostCount));
1071 ShutDownAndCore(PANIC);
1074 /* ...because the hostList is full of deleted hosts */
1078 h_Hold_r(enumstart);
1080 /* remember hostCount, lest it change over the potential H_LOCK drop in
1082 origHostCount = hostCount;
1084 for (count = 0, host = enumstart; host && count < origHostCount; host = next, count++) {
1087 /* find the next non-deleted host */
1088 while (next && (next->hostFlags & HOSTDELETED)) {
1090 /* inc count for the skipped-over host */
1091 if (++count > origHostCount) {
1092 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", origHostCount));
1093 ShutDownAndCore(PANIC);
1099 if (!(host->hostFlags & HOSTDELETED)) {
1101 flags = (*proc) (host, param);
1102 if (H_ENUMERATE_ISSET_BAIL(flags)) {
1103 h_Release_r(host); /* this might free up the host */
1109 ViceLog(0, ("h_Enumerate_r got back invalid return value %d\n", flags));
1110 ShutDownAndCore(PANIC);
1113 h_Release_r(host); /* this might free up the host */
1115 if (host != NULL && count >= origHostCount) {
1116 ViceLog(0, ("h_Enumerate_r found more than %d hosts\n", origHostCount));
1117 ShutDownAndCore(PANIC);
1119 } /*h_Enumerate_r */
1122 /* inserts a new HashChain structure corresponding to this UUID */
1124 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
1127 struct h_UuidHashChain *chain;
1128 char uuid1[128], uuid2[128];
1131 /* hash into proper bucket */
1132 index = h_UuidHashIndex(uuid);
1134 /* don't add the same entry multiple times */
1135 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
1136 if (!chain->hostPtr)
1139 if (chain->hostPtr->interface &&
1140 afs_uuid_equal(&chain->hostPtr->interface->uuid, uuid)) {
1141 if (LogLevel >= 125) {
1142 afsUUID_to_string(&chain->hostPtr->interface->uuid, uuid1,
1144 afsUUID_to_string(uuid, uuid2, 127);
1145 ViceLog(125, ("h_AddHostToUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s) exists as %s:%d (uuid %s)\n",
1147 afs_inet_ntoa_r(chain->hostPtr->host, hoststr),
1148 ntohs(chain->hostPtr->port), uuid2));
1154 /* insert into beginning of list for this bucket */
1155 chain = (struct h_UuidHashChain *)malloc(sizeof(struct h_UuidHashChain));
1157 ViceLogThenPanic(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
1159 chain->hostPtr = host;
1160 chain->next = hostUuidHashTable[index];
1161 hostUuidHashTable[index] = chain;
1164 afsUUID_to_string(uuid, uuid2, 127);
1166 ("h_AddHostToUuidHashTable_r: host %p (%s:%d) added as uuid %s\n",
1167 host, afs_inet_ntoa_r(chain->hostPtr->host, hoststr),
1168 ntohs(chain->hostPtr->port), uuid2));
1171 /* deletes a HashChain structure corresponding to this host */
1173 h_DeleteHostFromUuidHashTable_r(struct host *host)
1176 struct h_UuidHashChain **uhp, *uth;
1180 if (!host->interface)
1183 /* hash into proper bucket */
1184 index = h_UuidHashIndex(&host->interface->uuid);
1186 if (LogLevel >= 125)
1187 afsUUID_to_string(&host->interface->uuid, uuid1, 127);
1188 for (uhp = &hostUuidHashTable[index]; (uth = *uhp); uhp = &uth->next) {
1189 osi_Assert(uth->hostPtr);
1190 if (uth->hostPtr == host) {
1192 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d)\n",
1193 host, uuid1, afs_inet_ntoa_r(host->host, hoststr),
1194 ntohs(host->port)));
1201 ("h_DeleteHostFromUuidHashTable_r: host %" AFS_PTR_FMT " (uuid %s %s:%d) not found\n",
1202 host, uuid1, afs_inet_ntoa_r(host->host, hoststr),
1203 ntohs(host->port)));
1208 * This is called with host locked and held.
1210 * All addresses are in network byte order.
1213 invalidateInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1217 struct Interface *interface;
1218 char hoststr[16], hoststr2[16];
1221 osi_Assert(host->interface);
1223 ViceLog(125, ("invalidateInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1224 host, afs_inet_ntoa_r(host->host, hoststr),
1225 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1229 * Make sure this address is on the list of known addresses
1232 interface = host->interface;
1233 number = host->interface->numberOfInterfaces;
1234 for (i = 0; i < number; i++) {
1235 if (interface->interface[i].addr == addr &&
1236 interface->interface[i].port == port) {
1237 if (interface->interface[i].valid) {
1238 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1239 interface->interface[i].valid = 0;
1250 * This is called with host locked and held. This function differs
1251 * from removeInterfaceAddr_r in that it is called when the address
1252 * is being removed from the host regardless of whether or not there
1253 * is an interface list for the host. This function will delete the
1254 * host if there are no addresses left on it.
1256 * All addresses are in network byte order.
1259 removeAddress_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1262 char hoststr[16], hoststr2[16];
1263 struct rx_connection *rxconn;
1265 if (!host->interface || host->interface->numberOfInterfaces == 1) {
1266 if (host->host == addr && host->port == port) {
1268 ("Removing only address for host %" AFS_PTR_FMT " (%s:%d), deleting host.\n",
1269 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1270 host->hostFlags |= HOSTDELETED;
1272 * Do not remove the primary addr/port from the hash table.
1273 * It will be ignored due to the HOSTDELETED flag and will
1274 * be removed when h_TossStuff_r() cleans up the HOSTDELETED
1275 * host. Removing it here will only result in a search for
1276 * the host/addr/port in the hash chain which will fail.
1280 ("Removing address that does not belong to host %" AFS_PTR_FMT " (%s:%d).\n",
1281 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1284 if (host->host == addr && host->port == port) {
1285 removeInterfaceAddr_r(host, addr, port);
1287 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1288 if (host->interface->interface[i].valid) {
1290 ("Removed address for host %" AFS_PTR_FMT " (%s:%d), new primary interface %s:%d.\n",
1291 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1292 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr2),
1293 ntohs(host->interface->interface[i].port)));
1294 host->host = host->interface->interface[i].addr;
1295 host->port = host->interface->interface[i].port;
1296 h_AddHostToAddrHashTable_r(host->host, host->port, host);
1301 if (i == host->interface->numberOfInterfaces) {
1303 ("Removed only address for host %" AFS_PTR_FMT " (%s:%d), no valid alternate interfaces, deleting host.\n",
1304 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1305 host->hostFlags |= HOSTDELETED;
1306 /* addr/port was removed from the hash table */
1310 rxconn = host->callback_rxcon;
1311 host->callback_rxcon = NULL;
1314 rx_DestroyConnection(rxconn);
1318 h_SetupCallbackConn_r(host);
1321 /* not the primary addr/port, just invalidate it */
1322 invalidateInterfaceAddr_r(host, addr, port);
1330 createHostAddrHashChain_r(int index, afs_uint32 addr, afs_uint16 port, struct host *host)
1332 struct h_AddrHashChain *chain;
1335 /* insert into beginning of list for this bucket */
1336 chain = (struct h_AddrHashChain *)malloc(sizeof(struct h_AddrHashChain));
1338 ViceLogThenPanic(0, ("Failed malloc in h_AddHostToAddrHashTable_r\n"));
1340 chain->hostPtr = host;
1341 chain->next = hostAddrHashTable[index];
1344 hostAddrHashTable[index] = chain;
1345 ViceLog(125, ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " added as %s:%d\n",
1346 host, afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1350 * Resolve host address conflicts when hashing by address.
1352 * @param[in] addr an ip address of the interface
1353 * @param[in] port the port of the interface
1354 * @param[in] newHost the host being added with this address
1355 * @param[in] oldHost the host previously added with this address
1358 reconcileHosts_r(afs_uint32 addr, afs_uint16 port, struct host *newHost,
1359 struct host *oldHost)
1361 struct rx_connection *cb = NULL;
1363 struct interfaceAddr interf;
1365 afsUUID *newHostUuid = &nulluuid;
1366 afsUUID *oldHostUuid = &nulluuid;
1370 ("reconcileHosts_r: addr %s:%d newHost %" AFS_PTR_FMT " oldHost %"
1371 AFS_PTR_FMT, afs_inet_ntoa_r(addr, hoststr), ntohs(port),
1374 osi_Assert(oldHost != newHost);
1375 caps.Capabilities_val = NULL;
1378 sc = rxnull_NewClientSecurityObject();
1381 cb = rx_NewConnection(addr, port, 1, sc, 0);
1382 rx_SetConnDeadTime(cb, 50);
1383 rx_SetConnHardDeadTime(cb, AFS_HARDDEADTIME);
1388 code = RXAFSCB_TellMeAboutYourself(cb, &interf, &caps);
1389 if (code == RXGEN_OPCODE) {
1390 code = RXAFSCB_WhoAreYou(cb, &interf);
1394 if (code == RXGEN_OPCODE ||
1395 (code == 0 && afs_uuid_equal(&interf.uuid, &nulluuid))) {
1397 ("reconcileHosts_r: WhoAreYou not supported for connection (%s:%d), error %d\n",
1398 afs_inet_ntoa_r(addr, hoststr), ntohs(port), code));
1403 ("reconcileHosts_r: WhoAreYou failed for connection (%s:%d), error %d\n",
1404 afs_inet_ntoa_r(addr, hoststr), ntohs(port), code));
1408 /* Since lock was dropped, the hosts may have been deleted during the rpcs. */
1409 if ((newHost->hostFlags & HOSTDELETED)
1410 && (oldHost->hostFlags & HOSTDELETED)) {
1412 ("reconcileHosts_r: new and old hosts were deleted during probe.\n"));
1416 /* A check can be done if at least one of the hosts has a uuid. It
1417 * is an error if the hosts have the same (not null) uuid. */
1418 if ((!(newHost->hostFlags & HOSTDELETED)) && newHost->interface) {
1419 newHostUuid = &(newHost->interface->uuid);
1421 if ((!(oldHost->hostFlags & HOSTDELETED)) && oldHost->interface) {
1422 oldHostUuid = &(oldHost->interface->uuid);
1424 if (afs_uuid_equal(newHostUuid, &nulluuid) &&
1425 afs_uuid_equal(oldHostUuid, &nulluuid)) {
1427 ("reconcileHosts_r: Cannot reconcile hosts for connection (%s:%d), no uuids\n",
1428 afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1431 if (afs_uuid_equal(newHostUuid, oldHostUuid)) {
1433 ("reconcileHosts_r: Cannot reconcile hosts for connection (%s:%d), same uuids\n",
1434 afs_inet_ntoa_r(addr, hoststr), ntohs(port)));
1438 /* Determine which host should be hashed */
1439 if ((!(newHost->hostFlags & HOSTDELETED))
1440 && afs_uuid_equal(newHostUuid, &(interf.uuid))) {
1441 /* Install the new host into the hash before removing the stale
1442 * addresses. Walk the hash chain again since the hash table may have
1443 * been changed when the host lock was dropped to get the uuid. */
1444 struct h_AddrHashChain *chain;
1445 int index = h_HashIndex(addr);
1446 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1447 if (chain->addr == addr && chain->port == port) {
1448 chain->hostPtr = newHost;
1449 removeAddress_r(oldHost, addr, port);
1453 createHostAddrHashChain_r(index, addr, port, newHost);
1454 removeAddress_r(oldHost, addr, port);
1457 if ((!(oldHost->hostFlags & HOSTDELETED))
1458 && afs_uuid_equal(oldHostUuid, &(interf.uuid))) {
1459 removeAddress_r(newHost, addr, port);
1464 if (!(newHost->hostFlags & HOSTDELETED)) {
1465 removeAddress_r(newHost, addr, port);
1467 if (!(oldHost->hostFlags & HOSTDELETED)) {
1468 removeAddress_r(oldHost, addr, port);
1472 h_Release_r(newHost);
1473 h_Release_r(oldHost);
1474 rx_DestroyConnection(cb);
1478 /* inserts a new HashChain structure corresponding to this address */
1480 h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
1483 struct h_AddrHashChain *chain;
1486 /* hash into proper bucket */
1487 index = h_HashIndex(addr);
1489 /* don't add the same address:port pair entry multiple times */
1490 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
1491 if (chain->addr == addr && chain->port == port) {
1492 if (chain->hostPtr == host) {
1494 ("h_AddHostToAddrHashTable_r: host %" AFS_PTR_FMT " (%s:%d) already hashed\n",
1495 host, afs_inet_ntoa_r(chain->addr, hoststr),
1496 ntohs(chain->port)));
1499 if (!(chain->hostPtr->hostFlags & HOSTDELETED)) {
1500 /* attempt to resolve host address collision */
1501 reconcileHosts_r(addr, port, host, chain->hostPtr);
1506 createHostAddrHashChain_r(index, addr, port, host);
1510 * This is called with host locked and held.
1511 * It is called to either validate or add an additional interface
1512 * address/port on the specified host.
1514 * All addresses are in network byte order.
1517 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1521 struct Interface *interface;
1522 char hoststr[16], hoststr2[16];
1525 osi_Assert(host->interface);
1528 * Make sure this address is on the list of known addresses
1531 number = host->interface->numberOfInterfaces;
1532 for (i = 0; i < number; i++) {
1533 if (host->interface->interface[i].addr == addr &&
1534 host->interface->interface[i].port == port) {
1536 ("addInterfaceAddr : found host %" AFS_PTR_FMT " (%s:%d) adding %s:%d%s\n",
1537 host, afs_inet_ntoa_r(host->host, hoststr),
1538 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1539 ntohs(port), host->interface->interface[i].valid ? "" :
1542 if (host->interface->interface[i].valid == 0) {
1543 host->interface->interface[i].valid = 1;
1544 h_AddHostToAddrHashTable_r(addr, port, host);
1550 ViceLog(125, ("addInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) adding %s:%d\n",
1551 host, afs_inet_ntoa_r(host->host, hoststr),
1552 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1555 interface = (struct Interface *)
1556 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1558 ViceLogThenPanic(0, ("Failed malloc in addInterfaceAddr_r\n"));
1560 interface->numberOfInterfaces = number + 1;
1561 interface->uuid = host->interface->uuid;
1562 for (i = 0; i < number; i++)
1563 interface->interface[i] = host->interface->interface[i];
1565 /* Add the new valid interface */
1566 interface->interface[number].addr = addr;
1567 interface->interface[number].port = port;
1568 interface->interface[number].valid = 1;
1569 h_AddHostToAddrHashTable_r(addr, port, host);
1570 free(host->interface);
1571 host->interface = interface;
1578 * This is called with host locked and held.
1580 * All addresses are in network byte order.
1583 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1587 struct Interface *interface;
1588 char hoststr[16], hoststr2[16];
1591 osi_Assert(host->interface);
1593 ViceLog(125, ("removeInterfaceAddr : host %" AFS_PTR_FMT " (%s:%d) addr %s:%d\n",
1594 host, afs_inet_ntoa_r(host->host, hoststr),
1595 ntohs(host->port), afs_inet_ntoa_r(addr, hoststr2),
1599 * Make sure this address is on the list of known addresses
1602 interface = host->interface;
1603 number = host->interface->numberOfInterfaces;
1604 for (i = 0; i < number; i++) {
1605 if (interface->interface[i].addr == addr &&
1606 interface->interface[i].port == port) {
1607 if (interface->interface[i].valid)
1608 h_DeleteHostFromAddrHashTable_r(addr, port, host);
1610 for (; i < number; i++) {
1611 interface->interface[i] = interface->interface[i+1];
1613 interface->numberOfInterfaces = number;
1624 h_threadquota(int waiting)
1629 } else if (lwps > 32) {
1632 } else if (lwps > 16) {
1642 /* If found, host is returned with refCount incremented */
1644 h_GetHost_r(struct rx_connection *tcon)
1647 struct host *oldHost;
1649 struct interfaceAddr interf;
1650 int interfValid = 0;
1651 struct Identity *identP = NULL;
1654 char hoststr[16], hoststr2[16];
1656 struct rx_connection *cb_conn = NULL;
1657 struct rx_connection *cb_in = NULL;
1659 caps.Capabilities_val = NULL;
1661 haddr = rxr_HostOf(tcon);
1662 hport = rxr_PortOf(tcon);
1665 rx_DestroyConnection(cb_in);
1668 if (caps.Capabilities_val)
1669 free(caps.Capabilities_val);
1670 caps.Capabilities_val = NULL;
1671 caps.Capabilities_len = 0;
1674 if (h_Lookup_r(haddr, hport, &host))
1676 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1677 if (host && !identP && !(host->Console & 1)) {
1678 /* This is a new connection, and we already have a host
1679 * structure for this address. Verify that the identity
1680 * of the caller matches the identity in the host structure.
1682 if ((host->hostFlags & HWHO_INPROGRESS) &&
1683 h_threadquota(host->lock.num_waiting)) {
1689 if (!(host->hostFlags & ALTADDR) ||
1690 (host->hostFlags & HOSTDELETED)) {
1691 /* Another thread is doing initialization
1692 * or this host was deleted while we
1693 * waited for the lock. */
1696 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1697 host, afs_inet_ntoa_r(host->host, hoststr),
1698 ntohs(host->port)));
1702 host->hostFlags |= HWHO_INPROGRESS;
1703 host->hostFlags &= ~ALTADDR;
1705 /* We received a new connection from an IP address/port
1706 * that is associated with 'host' but the address/port of
1707 * the callback connection does not have to match it.
1708 * If there is a match, we can use the existing callback
1709 * connection to verify the UUID. If they do not match
1710 * we need to use a new callback connection to verify the
1711 * UUID of the incoming caller and perhaps use the old
1712 * callback connection to verify that the old address/port
1716 cb_conn = host->callback_rxcon;
1717 rx_GetConnection(cb_conn);
1719 if (haddr == host->host && hport == host->port) {
1720 /* The existing callback connection matches the
1721 * incoming connection so just use it.
1724 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1725 if (code == RXGEN_OPCODE)
1726 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1728 /* We do not have a match. Create a new connection
1729 * for the new addr/port and use multi_Rx to probe
1730 * both of them simultaneously.
1733 sc = rxnull_NewClientSecurityObject();
1734 cb_in = rx_NewConnection(haddr, hport, 1, sc, 0);
1735 rx_SetConnDeadTime(cb_in, 50);
1736 rx_SetConnHardDeadTime(cb_in, AFS_HARDDEADTIME);
1737 rx_SetConnSecondsUntilNatPing(cb_in, 20);
1740 RXAFSCB_TellMeAboutYourself(cb_in, &interf, &caps);
1741 if (code == RXGEN_OPCODE)
1742 code = RXAFSCB_WhoAreYou(cb_in, &interf);
1744 rx_PutConnection(cb_conn);
1747 if ((code == RXGEN_OPCODE) ||
1748 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1749 identP = (struct Identity *)malloc(sizeof(struct Identity));
1751 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1754 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1755 if (cb_in == NULL) {
1756 /* The host on this connection was unable to respond to
1757 * the WhoAreYou. We will treat this as a new connection
1758 * from the existing host. The worst that can happen is
1759 * that we maintain some extra callback state information */
1760 if (host->interface) {
1762 ("Host %" AFS_PTR_FMT " (%s:%d) used to support WhoAreYou, deleting.\n",
1764 afs_inet_ntoa_r(host->host, hoststr),
1765 ntohs(host->port)));
1766 host->hostFlags |= HOSTDELETED;
1767 host->hostFlags &= ~HWHO_INPROGRESS;
1774 /* The incoming connection does not support WhoAreYou but
1775 * the original one might have. Use removeAddress_r() to
1776 * remove this addr/port from the host that was found.
1777 * If there are no more addresses left for the host it
1778 * will be deleted. Then we retry.
1780 removeAddress_r(host, haddr, hport);
1781 host->hostFlags &= ~HWHO_INPROGRESS;
1782 host->hostFlags |= ALTADDR;
1788 } else if (code == 0) {
1790 identP = (struct Identity *)malloc(sizeof(struct Identity));
1792 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1795 identP->uuid = interf.uuid;
1796 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1797 /* Check whether the UUID on this connection matches
1798 * the UUID in the host structure. If they don't match
1799 * then this is not the same host as before. */
1800 if (!host->interface
1801 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1804 ("Uuid doesn't match connection (%s:%d).\n",
1805 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport)));
1806 removeAddress_r(host, haddr, hport);
1809 ("Uuid doesn't match host %" AFS_PTR_FMT " (%s:%d).\n",
1810 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1812 removeAddress_r(host, host->host, host->port);
1814 host->hostFlags &= ~HWHO_INPROGRESS;
1815 host->hostFlags |= ALTADDR;
1821 /* the UUID matched the client at the incoming addr/port
1822 * but this is not the address of the active callback
1823 * connection. Try that connection and see if the client
1824 * is still there and if the reported UUID is the same.
1827 afsUUID uuid = host->interface->uuid;
1828 cb_conn = host->callback_rxcon;
1829 rx_GetConnection(cb_conn);
1830 rx_SetConnDeadTime(cb_conn, 2);
1831 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1833 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1835 rx_SetConnDeadTime(cb_conn, 50);
1836 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1837 rx_PutConnection(cb_conn);
1840 /* The primary address is either not responding or
1841 * is not the client we are looking for. Need to
1842 * remove the primary address and add swap in the new
1843 * callback connection, and destroy the old one.
1845 struct rx_connection *rxconn;
1846 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
1848 afs_inet_ntoa_r(host->host, hoststr),
1849 ntohs(host->port),code2));
1852 * make sure we add and then remove. otherwise, we
1853 * might end up with no valid interfaces after the
1854 * remove and the host will have been marked deleted.
1856 addInterfaceAddr_r(host, haddr, hport);
1857 removeInterfaceAddr_r(host, host->host, host->port);
1860 rxconn = host->callback_rxcon;
1861 host->callback_rxcon = cb_in;
1866 * If rx_DestroyConnection calls h_FreeConnection we
1867 * will deadlock on the host_glock_mutex. Work around
1868 * the problem by unhooking the client from the
1869 * connection before destroying the connection.
1871 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1872 rx_DestroyConnection(rxconn);
1878 /* A callback to the incoming connection address is failing.
1879 * Assume that the addr/port is no longer associated with the host
1880 * returned by h_Lookup_r.
1883 ("CB: WhoAreYou failed for connection (%s:%d) , error %d\n",
1884 afs_inet_ntoa_r(haddr, hoststr), ntohs(hport), code));
1885 removeAddress_r(host, haddr, hport);
1886 host->hostFlags &= ~HWHO_INPROGRESS;
1887 host->hostFlags |= ALTADDR;
1891 rx_DestroyConnection(cb_in);
1896 ("CB: WhoAreYou failed for host %" AFS_PTR_FMT " (%s:%d), error %d\n",
1897 host, afs_inet_ntoa_r(host->host, hoststr),
1898 ntohs(host->port), code));
1899 host->hostFlags |= VENUSDOWN;
1902 if (caps.Capabilities_val
1903 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1904 host->hostFlags |= HERRORTRANS;
1906 host->hostFlags &= ~(HERRORTRANS);
1907 host->hostFlags |= ALTADDR;
1908 host->hostFlags &= ~HWHO_INPROGRESS;
1911 if (!(host->hostFlags & ALTADDR)) {
1912 /* another thread is doing the initialisation */
1914 ("Host %" AFS_PTR_FMT " (%s:%d) waiting for host-init to complete\n",
1915 host, afs_inet_ntoa_r(host->host, hoststr),
1916 ntohs(host->port)));
1920 ("Host %" AFS_PTR_FMT " (%s:%d) starting h_Lookup again\n",
1921 host, afs_inet_ntoa_r(host->host, hoststr),
1922 ntohs(host->port)));
1926 /* We need to check whether the identity in the host structure
1927 * matches the identity on the connection. If they don't match
1928 * then treat this a new host. */
1929 if (!(host->Console & 1)
1930 && ((!identP->valid && host->interface)
1931 || (identP->valid && !host->interface)
1933 && !afs_uuid_equal(&identP->uuid,
1934 &host->interface->uuid)))) {
1935 char uuid1[128], uuid2[128];
1937 afsUUID_to_string(&identP->uuid, uuid1, 127);
1938 if (host->interface)
1939 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1941 ("CB: new identity for host %p (%s:%d), "
1942 "deleting(%x %p %s %s)\n",
1943 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1944 identP->valid, host->interface,
1945 identP->valid ? uuid1 : "no_uuid",
1946 host->interface ? uuid2 : "no_uuid"));
1948 /* The host in the cache is not the host for this connection */
1950 host->hostFlags |= HOSTDELETED;
1956 host = h_Alloc_r(tcon); /* returned held and locked */
1959 h_gethostcps_r(host, FT_ApproxTime());
1960 if (!(host->Console & 1)) {
1962 cb_conn = host->callback_rxcon;
1963 rx_GetConnection(cb_conn);
1964 host->hostFlags |= HWHO_INPROGRESS;
1967 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1968 if (code == RXGEN_OPCODE)
1969 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1970 rx_PutConnection(cb_conn);
1973 if ((code == RXGEN_OPCODE) ||
1974 ((code == 0) && (afs_uuid_equal(&interf.uuid, &nulluuid)))) {
1977 (struct Identity *)malloc(sizeof(struct Identity));
1982 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
1986 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1988 ("Host %" AFS_PTR_FMT " (%s:%d) does not support WhoAreYou.\n",
1989 host, afs_inet_ntoa_r(host->host, hoststr),
1990 ntohs(host->port)));
1992 } else if (code == 0) {
1995 (struct Identity *)malloc(sizeof(struct Identity));
2000 ViceLogThenPanic(0, ("Failed malloc in h_GetHost_r\n"));
2004 identP->uuid = interf.uuid;
2006 rx_SetSpecific(tcon, rxcon_ident_key, identP);
2008 ("WhoAreYou success on host %" AFS_PTR_FMT " (%s:%d)\n",
2009 host, afs_inet_ntoa_r(host->host, hoststr),
2010 ntohs(host->port)));
2012 if (code == 0 && !identP->valid) {
2013 cb_conn = host->callback_rxcon;
2014 rx_GetConnection(cb_conn);
2016 code = RXAFSCB_InitCallBackState(cb_conn);
2017 rx_PutConnection(cb_conn);
2020 } else if (code == 0) {
2021 oldHost = h_LookupUuid_r(&identP->uuid);
2026 if (oldHost->hostFlags & HOSTDELETED) {
2027 h_Unlock_r(oldHost);
2028 h_Release_r(oldHost);
2036 oldHost->hostFlags |= HWHO_INPROGRESS;
2038 if (oldHost->interface) {
2040 afsUUID uuid = oldHost->interface->uuid;
2041 cb_conn = oldHost->callback_rxcon;
2042 rx_GetConnection(cb_conn);
2043 rx_SetConnDeadTime(cb_conn, 2);
2044 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2046 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2048 rx_SetConnDeadTime(cb_conn, 50);
2049 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
2050 rx_PutConnection(cb_conn);
2053 /* The primary address is either not responding or
2054 * is not the client we are looking for.
2055 * MultiProbeAlternateAddress_r() will remove the
2056 * alternate interfaces that do not have the same
2058 ViceLog(0,("CB: ProbeUuid for host %" AFS_PTR_FMT " (%s:%d) failed %d\n",
2060 afs_inet_ntoa_r(oldHost->host, hoststr),
2061 ntohs(oldHost->port),code2));
2062 MultiProbeAlternateAddress_r(oldHost);
2069 /* This is a new address for an existing host. Update
2070 * the list of interfaces for the existing host and
2071 * delete the host structure we just allocated. */
2073 /* prevent warnings while manipulating interface lists */
2074 host->hostFlags |= HOSTDELETED;
2076 if (oldHost->host != haddr || oldHost->port != hport) {
2077 struct rx_connection *rxconn;
2080 ("CB: Host %" AFS_PTR_FMT " (%s:%d) has new addr %s:%d\n",
2082 afs_inet_ntoa_r(oldHost->host, hoststr2),
2083 ntohs(oldHost->port),
2084 afs_inet_ntoa_r(haddr, hoststr),
2087 * add then remove. otherwise the host may get marked
2088 * deleted if we removed the only valid address.
2090 addInterfaceAddr_r(oldHost, haddr, hport);
2091 if (probefail || oldHost->host == haddr) {
2093 * The probe failed which means that the old
2094 * address is either unreachable or is not the
2095 * same host we were just contacted by. We will
2096 * also remove addresses if only the port has
2097 * changed because that indicates the client
2100 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
2103 struct Interface *interface = oldHost->interface;
2104 int number = oldHost->interface->numberOfInterfaces;
2105 for (i = 0; i < number; i++) {
2106 if (interface->interface[i].addr == haddr &&
2107 interface->interface[i].port != hport) {
2109 * We have just been contacted by a client
2110 * that has been seen from behind a NAT
2111 * and at least one other address.
2113 removeInterfaceAddr_r(oldHost, haddr,
2114 interface->interface[i].port);
2119 h_AddHostToAddrHashTable_r(haddr, hport, oldHost);
2120 oldHost->host = haddr;
2121 oldHost->port = hport;
2122 rxconn = oldHost->callback_rxcon;
2123 oldHost->callback_rxcon = host->callback_rxcon;
2124 host->callback_rxcon = rxconn;
2126 /* don't destroy rxconn here; let h_TossStuff_r
2127 * take care of that via h_Release_r below */
2129 host->hostFlags &= ~HWHO_INPROGRESS;
2131 /* release host because it was allocated by h_Alloc_r */
2134 /* the new host is held and locked */
2136 /* This really is a new host */
2137 h_AddHostToUuidHashTable_r(&identP->uuid, host);
2138 cb_conn = host->callback_rxcon;
2139 rx_GetConnection(cb_conn);
2142 RXAFSCB_InitCallBackState3(cb_conn,
2144 rx_PutConnection(cb_conn);
2149 ("InitCallBackState3 success on host %" AFS_PTR_FMT " (%s:%d)\n",
2150 host, afs_inet_ntoa_r(host->host, hoststr),
2151 ntohs(host->port)));
2152 osi_Assert(interfValid == 1);
2153 initInterfaceAddr_r(host, &interf);
2159 ("CB: RCallBackConnectBack failed for %" AFS_PTR_FMT " (%s:%d)\n",
2160 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
2161 host->hostFlags |= VENUSDOWN;
2164 ("CB: RCallBackConnectBack succeeded for %" AFS_PTR_FMT " (%s:%d)\n",
2165 host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
2166 host->hostFlags |= RESETDONE;
2169 if (caps.Capabilities_val
2170 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
2171 host->hostFlags |= HERRORTRANS;
2173 host->hostFlags &= ~(HERRORTRANS);
2174 host->hostFlags |= ALTADDR; /* host structure initialization complete */
2175 host->hostFlags &= ~HWHO_INPROGRESS;
2180 if (caps.Capabilities_val)
2181 free(caps.Capabilities_val);
2182 caps.Capabilities_val = NULL;
2183 caps.Capabilities_len = 0;
2185 rx_DestroyConnection(cb_in);
2193 static char localcellname[PR_MAXNAMELEN + 1];
2194 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
2195 int num_lrealms = -1;
2199 h_InitHostPackage(void)
2201 memset(&nulluuid, 0, sizeof(afsUUID));
2202 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
2203 if (num_lrealms == -1) {
2205 for (i=0; i<AFS_NUM_LREALMS; i++) {
2206 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
2212 ("afs_krb_get_lrealm failed, using %s.\n",
2214 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
2220 /* initialize the rest of the local realms to nullstring for debugging */
2221 for (; i<AFS_NUM_LREALMS; i++)
2222 local_realms[i][0] = '\0';
2224 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
2225 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
2226 MUTEX_INIT(&host_glock_mutex, "host glock", MUTEX_DEFAULT, 0);
2230 MapName_r(char *aname, char *acell, afs_int32 * aval)
2235 afs_int32 anamelen, cnamelen;
2239 anamelen = strlen(aname);
2240 if (anamelen >= PR_MAXNAMELEN)
2241 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
2243 lnames.namelist_len = 1;
2244 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
2245 lids.idlist_len = 0;
2246 lids.idlist_val = NULL;
2248 cnamelen = strlen(acell);
2250 if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
2252 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
2253 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
2254 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
2256 ("MapName: Name too long, using AnonymousID for %s@%s\n",
2258 *aval = AnonymousID;
2261 foreign = 1; /* attempt cross-cell authentication */
2262 tname = (char *)malloc(PR_MAXNAMELEN);
2264 ViceLogThenPanic(0, ("Failed malloc in MapName_r\n"));
2266 strcpy(tname, aname);
2267 tname[anamelen] = '@';
2268 strcpy(tname + anamelen + 1, acell);
2269 lnames.namelist_val = (prname *) tname;
2274 code = hpr_NameToId(&lnames, &lids);
2277 if (lids.idlist_val) {
2278 *aval = lids.idlist_val[0];
2279 if (*aval == AnonymousID) {
2281 ("MapName: NameToId on %s returns anonymousID\n",
2282 lnames.namelist_val[0]));
2284 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
2287 ("MapName: NameToId on '%s' is unknown\n",
2288 lnames.namelist_val[0]));
2294 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
2302 /* NOTE: this returns the client with a Write lock and a refCount */
2304 h_ID2Client(afs_int32 vid)
2306 struct client *client;
2311 for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
2312 if (host->hostFlags & HOSTDELETED)
2314 for (client = host->FirstClient; client; client = client->next) {
2315 if (!client->deleted && client->ViceId == vid) {
2318 ObtainWriteLock(&client->lock);
2323 if (count != hostCount) {
2324 ViceLog(0, ("h_ID2Client found %d of %d hosts\n", count, hostCount));
2325 } else if (host != NULL) {
2326 ViceLog(0, ("h_ID2Client found more than %d hosts\n", hostCount));
2327 ShutDownAndCore(PANIC);
2335 * Called by the server main loop. Returns a h_Held client, which must be
2336 * released later the main loop. Allocates a client if the matching one
2337 * isn't around. The client is returned with its reference count incremented
2338 * by one. The caller must call h_ReleaseClient_r when finished with
2341 * The refCount on client->host is returned incremented. h_ReleaseClient_r
2342 * does not decrement the refCount on client->host.
2345 h_FindClient_r(struct rx_connection *tcon)
2347 struct client *client;
2348 struct host *host = NULL;
2349 struct client *oldClient;
2350 afs_int32 viceid = 0;
2354 #if (64-MAXKTCNAMELEN)
2355 ticket name length != 64
2359 char uname[PR_MAXNAMELEN];
2360 char tcell[MAXKTCREALMLEN];
2364 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2365 if (client && client->sid == rxr_CidOf(tcon)
2366 && client->VenusEpoch == rxr_GetEpoch(tcon)
2367 && !(client->host->hostFlags & HOSTDELETED)
2368 && !client->deleted) {
2371 h_Hold_r(client->host);
2372 if (client->prfail != 2) {
2373 /* Could add shared lock on client here */
2374 /* note that we don't have to lock entry in this path to
2375 * ensure CPS is initialized, since we don't call rx_SetSpecific
2376 * until initialization is done, and we only get here if
2377 * rx_GetSpecific located the client structure.
2382 ObtainWriteLock(&client->lock); /* released at end */
2388 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
2390 ("FindClient: authenticating connection: authClass=%d\n",
2392 if (authClass == 1) {
2393 /* A bcrypt tickets, no longer supported */
2394 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
2395 viceid = AnonymousID;
2396 expTime = 0x7fffffff;
2397 } else if (authClass == 2) {
2400 /* kerberos ticket */
2401 code = rxkad_GetServerInfo(tcon, /*level */ 0, (afs_uint32 *)&expTime,
2402 tname, tinst, tcell, &kvno);
2404 ViceLog(1, ("Failed to get rxkad ticket info\n"));
2405 viceid = AnonymousID;
2406 expTime = 0x7fffffff;
2408 int ilen = strlen(tinst);
2410 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
2411 tname, tinst, tcell, expTime, kvno));
2412 strncpy(uname, tname, sizeof(uname));
2414 if (strlen(uname) + 1 + ilen >= sizeof(uname))
2417 strcat(uname, tinst);
2419 /* translate the name to a vice id */
2420 code = MapName_r(uname, tcell, &viceid);
2424 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
2427 viceid = AnonymousID;
2428 expTime = 0x7fffffff;
2432 viceid = AnonymousID; /* unknown security class */
2433 expTime = 0x7fffffff;
2436 if (!client) { /* loop */
2437 host = h_GetHost_r(tcon); /* Returns with incremented refCount */
2443 /* First try to find the client structure */
2444 for (client = host->FirstClient; client; client = client->next) {
2445 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2446 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2449 ObtainWriteLock(&client->lock);
2455 /* Still no client structure - get one */
2458 if (host->hostFlags & HOSTDELETED) {
2463 /* Retry to find the client structure */
2464 for (client = host->FirstClient; client; client = client->next) {
2465 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
2466 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
2468 goto retryfirstclient;
2473 ObtainWriteLock(&client->lock);
2474 client->refCount = 1;
2475 client->host = host;
2476 #if FS_STATS_DETAILED
2477 client->InSameNetwork = host->InSameNetwork;
2478 #endif /* FS_STATS_DETAILED */
2479 client->ViceId = viceid;
2480 client->expTime = expTime; /* rx only */
2481 client->authClass = authClass; /* rx only */
2482 client->sid = rxr_CidOf(tcon);
2483 client->VenusEpoch = rxr_GetEpoch(tcon);
2484 client->CPS.prlist_val = NULL;
2485 client->CPS.prlist_len = 0;
2489 client->prfail = fail;
2491 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
2492 client->CPS.prlist_len = 0;
2493 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
2494 free(client->CPS.prlist_val);
2495 client->CPS.prlist_val = NULL;
2496 client->ViceId = viceid;
2497 client->expTime = expTime;
2499 if (viceid == ANONYMOUSID) {
2500 client->CPS.prlist_len = AnonCPS.prlist_len;
2501 client->CPS.prlist_val = AnonCPS.prlist_val;
2504 code = hpr_GetCPS(viceid, &client->CPS);
2509 ("pr_GetCPS failed(%d) for user %d, host %" AFS_PTR_FMT " (%s:%d)\n",
2510 code, viceid, client->host,
2511 afs_inet_ntoa_r(client->host->host,hoststr),
2512 ntohs(client->host->port)));
2514 /* Although ubik_Call (called by pr_GetCPS) traverses thru
2515 * all protection servers and reevaluates things if no
2516 * sync server or quorum is found we could still end up
2517 * with one of these errors. In such case we would like to
2518 * reevaluate the rpc call to find if there's cps for this
2519 * guy. We treat other errors (except network failures
2520 * ones - i.e. code < 0) as an indication that there is no
2521 * CPS for this host. Ideally we could like to deal this
2522 * problem the other way around (i.e. if code == NOCPS
2523 * ignore else retry next time) but the problem is that
2524 * there're other errors (i.e. EPERM) for which we don't
2525 * want to retry and we don't know the whole code list!
2527 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
2531 /* the disabling of system:administrators is so iffy and has so many
2532 * possible failure modes that we will disable it again */
2533 /* Turn off System:Administrator for safety
2534 * if (AL_IsAMember(SystemId, client->CPS) == 0)
2535 * osi_Assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
2538 /* Now, tcon may already be set to a rock, since we blocked with no host
2539 * or client locks set above in pr_GetCPS (XXXX some locking is probably
2540 * required). So, before setting the RPC's rock, we should disconnect
2541 * the RPC from the other client structure's rock.
2543 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2544 if (oldClient && oldClient != client && oldClient->sid == rxr_CidOf(tcon)
2545 && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
2547 if (!oldClient->deleted) {
2548 /* if we didn't create it, it's not ours to put back */
2550 ViceLog(0, ("FindClient: stillborn client %p(%x); "
2551 "conn %p (host %s:%d) had client %p(%x)\n",
2552 client, client->sid, tcon,
2553 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2554 ntohs(rxr_PortOf(tcon)),
2555 oldClient, oldClient->sid));
2556 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2557 free(client->CPS.prlist_val);
2558 client->CPS.prlist_val = NULL;
2559 client->CPS.prlist_len = 0;
2561 /* We should perhaps check for 0 here */
2563 ReleaseWriteLock(&client->lock);
2568 oldClient->refCount++;
2570 h_Hold_r(oldClient->host);
2571 h_Release_r(client->host);
2574 ObtainWriteLock(&oldClient->lock);
2577 host = oldClient->host;
2579 ViceLog(0, ("FindClient: deleted client %p(%x ref %d host %p href "
2580 "%d) already had conn %p (host %s:%d, cid %x), stolen "
2581 "by client %p(%x, ref %d host %p href %d)\n",
2582 oldClient, oldClient->sid, oldClient->refCount,
2583 oldClient->host, oldClient->host->refCount, tcon,
2584 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2585 ntohs(rxr_PortOf(tcon)), rxr_CidOf(tcon),
2586 client, client->sid, client->refCount,
2587 client->host, client->host->refCount));
2588 /* rx_SetSpecific will be done immediately below */
2591 /* Avoid chaining in more than once. */
2595 if (host->hostFlags & HOSTDELETED) {
2600 client->host = NULL;
2602 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
2603 free(client->CPS.prlist_val);
2604 client->CPS.prlist_val = NULL;
2605 client->CPS.prlist_len = 0;
2608 ReleaseWriteLock(&client->lock);
2613 client->next = host->FirstClient;
2614 host->FirstClient = client;
2616 CurrentConnections++; /* increment number of connections */
2618 rx_SetSpecific(tcon, rxcon_client_key, client);
2619 ReleaseWriteLock(&client->lock);
2623 } /*h_FindClient_r */
2626 h_ReleaseClient_r(struct client *client)
2628 osi_Assert(client->refCount > 0);
2635 * Sigh: this one is used to get the client AGAIN within the individual
2636 * server routines. This does not bother h_Holding the host, since
2637 * this is assumed already have been done by the server main loop.
2638 * It does check tokens, since only the server routines can return the
2639 * VICETOKENDEAD error code
2642 GetClient(struct rx_connection *tcon, struct client **cp)
2644 struct client *client;
2649 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
2650 if (client == NULL) {
2652 ("GetClient: no client in conn %p (host %s:%d), VBUSYING\n",
2653 tcon, afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
2654 ntohs(rxr_PortOf(tcon))));
2658 if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
2660 ("GetClient: tcon %p tcon sid %d client sid %d\n",
2661 tcon, rxr_CidOf(tcon), client->sid));
2665 if (client && client->LastCall > client->expTime && client->expTime) {
2667 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
2668 afs_inet_ntoa_r(client->host->host, hoststr),
2669 ntohs(client->host->port), client->expTime));
2671 return VICETOKENDEAD;
2673 if (client->deleted) {
2674 ViceLog(0, ("GetClient: got deleted client, connection will appear "
2675 "anonymous; tcon %p cid %x client %p ref %d host %p "
2676 "(%s:%d) href %d ViceId %d\n",
2677 tcon, rxr_CidOf(tcon), client, client->refCount,
2679 afs_inet_ntoa_r(client->host->host, hoststr),
2680 (int)ntohs(client->host->port), client->host->refCount,
2681 (int)client->ViceId));
2691 PutClient(struct client **cp)
2697 h_ReleaseClient_r(*cp);
2704 /* Client user name for short term use. Note that this is NOT inexpensive */
2706 h_UserName(struct client *client)
2708 static char User[PR_MAXNAMELEN + 1];
2712 lids.idlist_len = 1;
2713 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
2714 if (!lids.idlist_val) {
2715 ViceLogThenPanic(0, ("Failed malloc in h_UserName\n"));
2717 lnames.namelist_len = 0;
2718 lnames.namelist_val = (prname *) 0;
2719 lids.idlist_val[0] = client->ViceId;
2720 if (hpr_IdToName(&lids, &lnames)) {
2721 /* We need to free id we alloced above! */
2722 free(lids.idlist_val);
2723 return "*UNKNOWN USER NAME*";
2725 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
2726 free(lids.idlist_val);
2727 free(lnames.namelist_val);
2736 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
2737 CEs, CEBlocks, HTs, HTBlocks));
2743 h_PrintClient(struct host *host, void *rock)
2745 StreamHandle_t *file = (StreamHandle_t *)rock;
2746 struct client *client;
2751 time_t LastCall, expTime;
2755 LastCall = host->LastCall;
2756 if (host->hostFlags & HOSTDELETED) {
2760 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2761 localtime_r(&LastCall, &tm));
2762 snprintf(tmpStr, sizeof tmpStr, "Host %s:%d down = %d, LastCall %s\n",
2763 afs_inet_ntoa_r(host->host, hoststr),
2764 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2766 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2767 for (client = host->FirstClient; client; client = client->next) {
2768 if (!client->deleted) {
2769 expTime = client->expTime;
2770 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2771 localtime_r(&expTime, &tm));
2772 snprintf(tmpStr, sizeof tmpStr,
2773 " user id=%d, name=%s, sl=%s till %s\n",
2774 client->ViceId, h_UserName(client),
2775 client->authClass ? "Authenticated"
2776 : "Not authenticated",
2777 client->authClass ? tbuffer : "No Limit");
2778 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2779 snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2780 client->CPS.prlist_len);
2781 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2782 if (client->CPS.prlist_val) {
2783 for (i = 0; i < client->CPS.prlist_len; i++) {
2784 snprintf(tmpStr, sizeof tmpStr, " %d",
2785 client->CPS.prlist_val[i]);
2786 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2789 sprintf(tmpStr, "]\n");
2790 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2796 } /*h_PrintClient */
2801 * Print a list of clients, with last security level and token value seen,
2805 h_PrintClients(void)
2812 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2816 ("Couldn't create client dump file %s\n",
2817 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2820 now = FT_ApproxTime();
2821 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2822 localtime_r(&now, &tm));
2823 snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n\n",
2825 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2826 h_Enumerate(h_PrintClient, (char *)file);
2827 STREAM_REALLYCLOSE(file);
2828 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2835 h_DumpHost(struct host *host, void *rock)
2837 StreamHandle_t *file = (StreamHandle_t *)rock;
2844 snprintf(tmpStr, sizeof tmpStr,
2845 "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u "
2846 "down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u "
2848 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
2849 host->index, host->cblist, CheckLock(&host->lock),
2850 host->LastCall, host->ActiveCall, (host->hostFlags & VENUSDOWN),
2851 host->hostFlags & HOSTDELETED, host->Console,
2852 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2854 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2855 if (host->hcps.prlist_val)
2856 for (i = 0; i < host->hcps.prlist_len; i++) {
2857 snprintf(tmpStr, sizeof tmpStr, " %d", host->hcps.prlist_val[i]);
2858 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2860 sprintf(tmpStr, "] [");
2861 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2862 if (host->interface)
2863 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2865 sprintf(tmpStr, " %s:%d",
2866 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2867 ntohs(host->interface->interface[i].port));
2868 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2870 sprintf(tmpStr, "] refCount:%d hostFlags:%hu\n", host->refCount, host->hostFlags);
2871 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2883 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2890 ("Couldn't create host dump file %s\n",
2891 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2894 now = FT_ApproxTime();
2895 strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y",
2896 localtime_r(&now, &tm));
2897 snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n\n", tbuffer);
2898 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2899 h_Enumerate(h_DumpHost, (char *)file);
2900 STREAM_REALLYCLOSE(file);
2901 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2905 #ifdef AFS_DEMAND_ATTACH_FS
2908 * host state serialization
2910 static int h_stateFillHeader(struct host_state_header * hdr);
2911 static int h_stateCheckHeader(struct host_state_header * hdr);
2912 static int h_stateAllocMap(struct fs_dump_state * state);
2913 static int h_stateSaveHost(struct host * host, void *rock);
2914 static int h_stateRestoreHost(struct fs_dump_state * state);
2915 static int h_stateRestoreIndex(struct host * h, void *rock);
2916 static int h_stateVerifyHost(struct host * h, void *rock);
2917 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
2918 afs_uint32 addr, afs_uint16 port, int valid);
2919 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2920 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2921 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2924 /* this procedure saves all host state to disk for fast startup */
2926 h_stateSave(struct fs_dump_state * state)
2928 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2931 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2933 /* invalidate host state header */
2934 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2936 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2937 sizeof(struct host_state_header))) {
2942 fs_stateIncEOF(state, sizeof(struct host_state_header));
2944 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2949 h_stateFillHeader(state->h_hdr);
2951 /* write the real header to disk */
2952 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2953 sizeof(struct host_state_header));
2960 * host state serialization
2962 * this procedure restores all host state from a disk for fast startup
2965 h_stateRestore(struct fs_dump_state * state)
2969 /* seek to the right position and read in the host state header */
2970 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2971 sizeof(struct host_state_header))) {
2976 /* check the validity of the header */
2977 if (h_stateCheckHeader(state->h_hdr)) {
2982 records = state->h_hdr->records;
2984 if (h_stateAllocMap(state)) {
2989 /* iterate over records restoring host state */
2990 for (i=0; i < records; i++) {
2991 if (h_stateRestoreHost(state) != 0) {
3002 h_stateRestoreIndices(struct fs_dump_state * state)
3004 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
3009 h_stateRestoreIndex(struct host * h, void *rock)
3011 struct fs_dump_state *state = (struct fs_dump_state *)rock;
3012 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
3013 return H_ENUMERATE_BAIL(0);
3019 h_stateVerify(struct fs_dump_state * state)
3021 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
3026 h_stateVerifyHost(struct host * h, void* rock)
3028 struct fs_dump_state *state = (struct fs_dump_state *)rock;
3032 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
3033 return H_ENUMERATE_BAIL(0);
3037 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
3038 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
3039 h->interface->interface[i].port,
3040 h->interface->interface[i].valid)) {
3044 if (h_stateVerifyUuidHash(state, h)) {
3047 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port, 1)) {
3051 if (cb_stateVerifyHCBList(state, h)) {
3059 * verify a host is either in, or absent from, the addr hash table.
3061 * @param[in] state fs dump state
3062 * @param[in] h host we're dealing with
3063 * @param[in] addr addr to look for (NBO)
3064 * @param[in] port port to look for (NBO)
3065 * @param[in] valid 1 if we're verifying that the specified addr and port
3066 * in the hash table point to the specified host. 0 if we're
3067 * verifying that the specified addr and port do NOT point
3068 * to the specified host
3070 * @return operation status
3071 * @retval 1 failed to verify, bail out
3072 * @retval 0 verified successfully, all is well
3075 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h,
3076 afs_uint32 addr, afs_uint16 port, int valid)
3078 int ret = 0, found = 0;
3079 struct host *host = NULL;
3080 struct h_AddrHashChain *chain;
3081 int index = h_HashIndex(addr);
3085 for (chain = hostAddrHashTable[index]; chain; chain = chain->next) {
3086 host = chain->hostPtr;
3088 afs_inet_ntoa_r(addr, tmp);
3089 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
3093 if ((chain->addr == addr) && (chain->port == port)) {
3096 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry "
3097 "points to different host struct (%d, %d)\n",
3098 h->index, host->index));
3099 state->flags.warnings_generated = 1;
3103 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s:%u is "
3104 "marked invalid, but points to the containing "
3105 "host\n", afs_inet_ntoa_r(addr, tmp),
3106 (unsigned)htons(port)));
3114 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
3115 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
3116 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
3123 if (!found && valid) {
3124 afs_inet_ntoa_r(addr, tmp);
3125 if (state->mode == FS_STATE_LOAD_MODE) {
3126 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s:%u not found in hash\n",
3127 tmp, (unsigned)htons(port)));
3131 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s:%u not found in hash\n",
3132 tmp, (unsigned)htons(port)));
3133 state->flags.warnings_generated = 1;
3142 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
3144 int ret = 0, found = 0;
3145 struct host *host = NULL;
3146 struct h_UuidHashChain *chain;
3147 afsUUID * uuidp = &h->interface->uuid;
3148 int index = h_UuidHashIndex(uuidp);
3152 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
3153 host = chain->hostPtr;
3155 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
3156 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
3160 if (host->interface &&
3161 afs_uuid_equal(&host->interface->uuid, uuidp)) {
3163 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
3164 h->index, host->index));
3165 state->flags.warnings_generated = 1;
3170 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
3171 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
3172 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
3180 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
3181 if (state->mode == FS_STATE_LOAD_MODE) {
3182 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
3186 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
3187 state->flags.warnings_generated = 1;
3195 /* create the host state header structure */
3197 h_stateFillHeader(struct host_state_header * hdr)
3199 hdr->stamp.magic = HOST_STATE_MAGIC;
3200 hdr->stamp.version = HOST_STATE_VERSION;
3204 /* check the contents of the host state header structure */
3206 h_stateCheckHeader(struct host_state_header * hdr)
3210 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
3211 ViceLog(0, ("check_host_state_header: invalid state header\n"));
3214 else if (hdr->stamp.version != HOST_STATE_VERSION) {
3215 ViceLog(0, ("check_host_state_header: unknown version number\n"));
3221 /* allocate the host id mapping table */
3223 h_stateAllocMap(struct fs_dump_state * state)
3225 state->h_map.len = state->h_hdr->index_max + 1;
3226 state->h_map.entries = (struct idx_map_entry_t *)
3227 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
3228 return (state->h_map.entries != NULL) ? 0 : 1;
3231 /* function called by h_Enumerate to save a host to disk */
3233 h_stateSaveHost(struct host * host, void* rock)
3235 struct fs_dump_state *state = (struct fs_dump_state *) rock;
3236 int if_len=0, hcps_len=0;
3237 struct hostDiskEntry hdsk;
3238 struct host_state_entry_header hdr;
3239 struct Interface * ifp = NULL;
3240 afs_int32 * hcps = NULL;
3241 struct iovec iov[4];
3244 memset(&hdr, 0, sizeof(hdr));
3246 if (state->h_hdr->index_max < host->index) {
3247 state->h_hdr->index_max = host->index;
3250 h_hostToDiskEntry_r(host, &hdsk);
3251 if (host->interface) {
3252 if_len = sizeof(struct Interface) +
3253 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
3254 ifp = (struct Interface *) malloc(if_len);
3255 osi_Assert(ifp != NULL);
3256 memcpy(ifp, host->interface, if_len);
3257 hdr.interfaces = host->interface->numberOfInterfaces;
3258 iov[iovcnt].iov_base = (char *) ifp;
3259 iov[iovcnt].iov_len = if_len;
3262 if (host->hcps.prlist_val) {
3263 hdr.hcps = host->hcps.prlist_len;
3264 hcps_len = hdr.hcps * sizeof(afs_int32);
3265 hcps = (afs_int32 *) malloc(hcps_len);
3266 osi_Assert(hcps != NULL);
3267 memcpy(hcps, host->hcps.prlist_val, hcps_len);
3268 iov[iovcnt].iov_base = (char *) hcps;
3269 iov[iovcnt].iov_len = hcps_len;
3273 if (hdsk.index > state->h_hdr->index_max)
3274 state->h_hdr->index_max = hdsk.index;
3276 hdr.len = sizeof(struct host_state_entry_header) +
3277 sizeof(struct hostDiskEntry) + if_len + hcps_len;
3278 hdr.magic = HOST_STATE_ENTRY_MAGIC;
3280 iov[0].iov_base = (char *) &hdr;
3281 iov[0].iov_len = sizeof(hdr);
3282 iov[1].iov_base = (char *) &hdsk;
3283 iov[1].iov_len = sizeof(struct hostDiskEntry);
3285 if (fs_stateWriteV(state, iov, iovcnt)) {
3286 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
3290 fs_stateIncEOF(state, hdr.len);
3292 state->h_hdr->records++;
3299 return H_ENUMERATE_BAIL(0);
3304 /* restores a host from disk */
3306 h_stateRestoreHost(struct fs_dump_state * state)
3308 int ifp_len=0, hcps_len=0, bail=0;
3309 struct host_state_entry_header hdr;
3310 struct hostDiskEntry hdsk;
3311 struct host *host = NULL;
3312 struct Interface *ifp = NULL;
3313 afs_int32 * hcps = NULL;
3314 struct iovec iov[3];
3317 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
3318 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
3324 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
3325 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
3331 iov[0].iov_base = (char *) &hdsk;
3332 iov[0].iov_len = sizeof(struct hostDiskEntry);
3334 if (hdr.interfaces) {
3335 ifp_len = sizeof(struct Interface) +
3336 ((hdr.interfaces-1) * sizeof(struct AddrPort));
3337 ifp = (struct Interface *) malloc(ifp_len);
3338 osi_Assert(ifp != NULL);
3339 iov[iovcnt].iov_base = (char *) ifp;
3340 iov[iovcnt].iov_len = ifp_len;
3344 hcps_len = hdr.hcps * sizeof(afs_int32);
3345 hcps = (afs_int32 *) malloc(hcps_len);
3346 osi_Assert(hcps != NULL);
3347 iov[iovcnt].iov_base = (char *) hcps;
3348 iov[iovcnt].iov_len = hcps_len;
3352 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
3353 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
3358 if (fs_stateReadV(state, iov, iovcnt)) {
3359 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
3364 if (!hdr.hcps && hdsk.hcps_valid) {
3365 /* valid, zero-length host cps ; does this ever happen? */
3366 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
3367 osi_Assert(hcps != NULL);
3370 /* for restoring state, we better be able to get a host! */
3372 osi_Assert(host != NULL);
3375 host->interface = ifp;
3378 host->hcps.prlist_val = hcps;
3379 host->hcps.prlist_len = hdr.hcps;
3382 h_diskEntryToHost_r(&hdsk, host);
3383 h_SetupCallbackConn_r(host);
3385 h_AddHostToAddrHashTable_r(host->host, host->port, host);
3388 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
3389 if (ifp->interface[i].valid &&
3390 !(ifp->interface[i].addr == host->host &&
3391 ifp->interface[i].port == host->port)) {
3392 h_AddHostToAddrHashTable_r(ifp->interface[i].addr,
3393 ifp->interface[i].port,
3397 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
3399 h_InsertList_r(host);
3401 /* setup host id map entry */
3402 state->h_map.entries[hdsk.index].valid = FS_STATE_IDX_VALID;
3403 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
3404 state->h_map.entries[hdsk.index].new_idx = host->index;
3416 /* serialize a host structure to disk */
3418 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
3420 out->host = in->host;
3421 out->port = in->port;
3422 out->hostFlags = in->hostFlags;
3423 out->Console = in->Console;
3424 out->hcpsfailed = in->hcpsfailed;
3425 out->LastCall = in->LastCall;
3426 out->ActiveCall = in->ActiveCall;
3427 out->cpsCall = in->cpsCall;
3428 out->cblist = in->cblist;
3429 #ifdef FS_STATS_DETAILED
3430 out->InSameNetwork = in->InSameNetwork;
3433 /* special fields we save, but are not memcpy'd back on restore */
3434 out->index = in->index;
3435 out->hcps_len = in->hcps.prlist_len;
3436 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
3439 /* restore a host structure from disk */
3441 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
3443 out->host = in->host;
3444 out->port = in->port;
3445 out->hostFlags = in->hostFlags;
3446 out->Console = in->Console;
3447 out->hcpsfailed = in->hcpsfailed;
3448 out->LastCall = in->LastCall;
3449 out->ActiveCall = in->ActiveCall;
3450 out->cpsCall = in->cpsCall;
3451 out->cblist = in->cblist;
3452 #ifdef FS_STATS_DETAILED
3453 out->InSameNetwork = in->InSameNetwork;
3457 /* index translation routines */
3459 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
3463 /* hosts use a zero-based index, so old==0 is valid */
3465 if (old >= state->h_map.len) {
3466 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
3468 } else if (state->h_map.entries[old].valid != FS_STATE_IDX_VALID ||
3469 state->h_map.entries[old].old_idx != old) { /* sanity check */
3470 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
3473 *new = state->h_map.entries[old].new_idx;
3478 #endif /* AFS_DEMAND_ATTACH_FS */
3482 * This counts the number of workstations, the number of active workstations,
3483 * and the number of workstations declared "down" (i.e. not heard from
3484 * recently). An active workstation has received a call since the cutoff
3485 * time argument passed.
3488 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
3491 int num = 0, active = 0, del = 0;
3495 for (count = 0, host = hostList; host && count < hostCount; host = host->next, count++) {
3496 if (!(host->hostFlags & HOSTDELETED)) {
3498 if (host->ActiveCall > cutofftime)
3500 if (host->hostFlags & VENUSDOWN)
3504 if (count != hostCount) {
3505 ViceLog(0, ("h_GetWorkStats found %d of %d hosts\n", count, hostCount));
3506 } else if (host != NULL) {
3507 ViceLog(0, ("h_GetWorkStats found more than %d hosts\n", hostCount));
3508 ShutDownAndCore(PANIC);
3518 } /*h_GetWorkStats */
3521 h_GetWorkStats64(afs_uint64 *nump, afs_uint64 *activep, afs_uint64 *delp,
3522 afs_int32 cutofftime)
3524 int num, active, del;
3525 h_GetWorkStats(&num, &active, &del, cutofftime);
3534 /*------------------------------------------------------------------------
3535 * PRIVATE h_ClassifyAddress
3538 * Given a target IP address and a candidate IP address (both
3539 * in host byte order), classify the candidate into one of three
3540 * buckets in relation to the target by bumping the counters passed
3544 * a_targetAddr : Target address.
3545 * a_candAddr : Candidate address.
3546 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
3547 * addresses are either in the same network
3548 * or the same subnet.
3549 * a_diffSubnetP : ...when the candidate is in a different
3551 * a_diffNetworkP : ...when the candidate is in a different
3558 * The target and candidate addresses are both in host byte
3559 * order, NOT network byte order, when passed in.
3563 *------------------------------------------------------------------------*/
3566 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
3567 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
3568 afs_int32 * a_diffNetworkP)
3569 { /*h_ClassifyAddress */
3571 afs_uint32 targetNet;
3572 afs_uint32 targetSubnet;
3574 afs_uint32 candSubnet;
3577 * Put bad values into the subnet info to start with.
3579 targetSubnet = (afs_uint32) 0;
3580 candSubnet = (afs_uint32) 0;
3583 * Pull out the network and subnetwork numbers from the target
3584 * and candidate addresses. We can short-circuit this whole
3585 * affair if the target and candidate addresses are not of the
3588 if (IN_CLASSA(a_targetAddr)) {
3589 if (!(IN_CLASSA(a_candAddr))) {
3590 (*a_diffNetworkP)++;
3593 targetNet = a_targetAddr & IN_CLASSA_NET;
3594 candNet = a_candAddr & IN_CLASSA_NET;
3595 if (IN_SUBNETA(a_targetAddr))
3596 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
3597 if (IN_SUBNETA(a_can