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
10 #include <afsconfig.h>
11 #include <afs/param.h>
23 #include <netinet/in.h>
36 #include <afs/assert.h>
39 #include <afs/afsint.h>
40 #include <afs/rxgen_consts.h>
42 #include <afs/errors.h>
43 #include <afs/ihandle.h>
44 #include <afs/vnode.h>
45 #include <afs/volume.h>
46 #ifdef AFS_ATHENA_STDENV
50 #include <afs/ptclient.h>
51 #include <afs/prs_fs.h>
53 #include <afs/afsutil.h>
55 #include <afs/cellconfig.h>
57 #include "viced_prototypes.h"
62 #ifdef AFS_PTHREAD_ENV
63 pthread_mutex_t host_glock_mutex;
64 #endif /* AFS_PTHREAD_ENV */
67 extern int CurrentConnections;
69 extern int AnonymousID;
70 extern prlist AnonCPS;
72 extern struct afsconf_dir *confDir; /* config dir object */
73 extern int lwps; /* the max number of server threads */
74 extern afsUUID FS_HostUUID;
76 int CEs = 0; /* active clients */
77 int CEBlocks = 0; /* number of blocks of CEs */
78 struct client *CEFree = 0; /* first free client */
79 struct host *hostList = 0; /* linked list of all hosts */
80 int hostCount = 0; /* number of hosts in hostList */
84 #define CESPERBLOCK 73
85 struct CEBlock /* block of CESPERBLOCK file entries */
87 struct client entry[CESPERBLOCK];
91 * Make sure the subnet macros have been defined.
94 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
97 #ifndef IN_CLASSA_SUBNET
98 #define IN_CLASSA_SUBNET 0xffff0000
102 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
105 #ifndef IN_CLASSB_SUBNET
106 #define IN_CLASSB_SUBNET 0xffffff00
109 #define rxr_GetEpoch(aconn) (((struct rx_connection *)(aconn))->epoch)
111 #define rxr_CidOf(aconn) (((struct rx_connection *)(aconn))->cid)
113 #define rxr_PortOf(aconn) \
114 rx_PortOf(rx_PeerOf(((struct rx_connection *)(aconn))))
116 #define rxr_HostOf(aconn) \
117 rx_HostOf(rx_PeerOf((struct rx_connection *)(aconn)))
120 /* get a new block of CEs and chain it on CEFree */
121 static void GetCEBlock()
123 register struct CEBlock *block;
126 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
128 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
129 ShutDownAndCore(PANIC);
132 for(i = 0; i < (CESPERBLOCK -1); i++) {
133 Lock_Init(&block->entry[i].lock);
134 block->entry[i].next = &(block->entry[i+1]);
136 block->entry[CESPERBLOCK-1].next = 0;
137 Lock_Init(&block->entry[CESPERBLOCK-1].lock);
138 CEFree = (struct client *)block;
144 /* get the next available CE */
145 static struct client *GetCE()
147 register struct client *entry;
152 ViceLog(0, ("CEFree NULL in GetCE\n"));
153 ShutDownAndCore(PANIC);
157 CEFree = entry->next;
159 memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
165 /* return an entry to the free list */
166 static void FreeCE(register struct client *entry)
168 entry->next = CEFree;
175 * The HTs and HTBlocks variables were formerly static, but they are
176 * now referenced elsewhere in the FileServer.
178 int HTs = 0; /* active file entries */
179 int HTBlocks = 0; /* number of blocks of HTs */
180 static struct host *HTFree = 0; /* first free file entry */
183 * Hash tables of host pointers. We need two tables, one
184 * to map IP addresses onto host pointers, and another
185 * to map host UUIDs onto host pointers.
187 static struct h_hashChain *hostHashTable[h_HASHENTRIES];
188 static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
189 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
190 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
192 struct HTBlock /* block of HTSPERBLOCK file entries */
194 struct host entry[h_HTSPERBLOCK];
198 /* get a new block of HTs and chain it on HTFree */
199 static void GetHTBlock()
201 register struct HTBlock *block;
203 static int index = 0;
205 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
207 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
208 ShutDownAndCore(PANIC);
211 #ifdef AFS_PTHREAD_ENV
212 for(i=0; i < (h_HTSPERBLOCK); i++)
213 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 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 */
229 static struct host *GetHT()
231 register struct host *entry;
237 HTFree = entry->next;
239 memset((char *)entry, 0, HOST_TO_ZERO(entry));
245 /* return an entry to the free list */
246 static void FreeHT(register struct host *entry)
248 entry->next = HTFree;
255 static short consolePort = 0;
257 int h_Release(register struct host *host)
266 * If this thread does not have a hold on this host AND
267 * if other threads also dont have any holds on this host AND
268 * If either the HOSTDELETED or CLIENTDELETED flags are set
271 int h_Release_r(register struct host *host)
274 if (!((host)->holds[h_holdSlot()] & ~h_holdbit()) ) {
275 if (! h_OtherHolds_r(host) ) {
276 /* must avoid masking this until after h_OtherHolds_r runs
277 but it should be run before h_TossStuff_r */
278 (host)->holds[h_holdSlot()] &= ~h_holdbit();
279 if ( (host->hostFlags & HOSTDELETED) ||
280 (host->hostFlags & CLIENTDELETED) ) {
284 (host)->holds[h_holdSlot()] &= ~h_holdbit();
286 (host)->holds[h_holdSlot()] &= ~h_holdbit();
291 int h_OtherHolds_r(register struct host *host)
293 register int i, bit, slot;
296 for (i = 0 ; i < h_maxSlots ; i++) {
297 if (host->holds[i] != ((i == slot) ? bit : 0)) {
304 int h_Lock_r(register struct host *host)
314 * returns 1 if already locked
315 * else returns locks and returns 0
318 int h_NBLock_r(register struct host *host)
320 struct Lock *hostLock = &host->lock;
325 if ( !(hostLock->excl_locked) && !(hostLock->readers_reading) )
326 hostLock->excl_locked = WRITE_LOCK;
330 LOCK_UNLOCK(hostLock)
339 #if FS_STATS_DETAILED
340 /*------------------------------------------------------------------------
341 * PRIVATE h_AddrInSameNetwork
344 * Given a target IP address and a candidate IP address (both
345 * in host byte order), return a non-zero value (1) if the
346 * candidate address is in a different network from the target
350 * a_targetAddr : Target address.
351 * a_candAddr : Candidate address.
354 * 1 if the candidate address is in the same net as the target,
358 * The target and candidate addresses are both in host byte
359 * order, NOT network byte order, when passed in. We return
360 * our value as a character, since that's the type of field in
361 * the host structure, where this info will be stored.
365 *------------------------------------------------------------------------*/
367 static char h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
368 { /*h_AddrInSameNetwork*/
370 afs_uint32 targetNet;
374 * Pull out the network and subnetwork numbers from the target
375 * and candidate addresses. We can short-circuit this whole
376 * affair if the target and candidate addresses are not of the
379 if (IN_CLASSA(a_targetAddr)) {
380 if (!(IN_CLASSA(a_candAddr))) {
383 targetNet = a_targetAddr & IN_CLASSA_NET;
384 candNet = a_candAddr & IN_CLASSA_NET;
387 if (IN_CLASSB(a_targetAddr)) {
388 if (!(IN_CLASSB(a_candAddr))) {
391 targetNet = a_targetAddr & IN_CLASSB_NET;
392 candNet = a_candAddr & IN_CLASSB_NET;
395 if (IN_CLASSC(a_targetAddr)) {
396 if (!(IN_CLASSC(a_candAddr))) {
399 targetNet = a_targetAddr & IN_CLASSC_NET;
400 candNet = a_candAddr & IN_CLASSC_NET;
403 targetNet = a_targetAddr;
404 candNet = a_candAddr;
405 } /*Class D address*/
408 * Now, simply compare the extracted net values for the two addresses
409 * (which at this point are known to be of the same class)
411 if (targetNet == candNet)
416 } /*h_AddrInSameNetwork*/
417 #endif /* FS_STATS_DETAILED */
420 /* Assumptions: called with held host */
422 h_gethostcps_r(register struct host *host, register afs_int32 now)
427 /* wait if somebody else is already doing the getCPS call */
428 while ( host->hostFlags & HCPS_INPROGRESS )
430 slept = 1; /* I did sleep */
431 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
432 #ifdef AFS_PTHREAD_ENV
433 pthread_cond_wait(&host->cond, &host_glock_mutex);
434 #else /* AFS_PTHREAD_ENV */
435 if (( code = LWP_WaitProcess( &(host->hostFlags ))) != LWP_SUCCESS)
436 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
437 #endif /* AFS_PTHREAD_ENV */
441 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
442 if (host->hcps.prlist_val)
443 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
444 host->hcps.prlist_val = NULL;
445 host->hcps.prlist_len = 0;
446 slept? (host->cpsCall = FT_ApproxTime()): (host->cpsCall = now );
449 code = pr_GetHostCPS(htonl(host->host), &host->hcps);
453 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
454 * and reevaluates things if no sync server or quorum is found we could still end up
455 * with one of these errors. In such case we would like to reevaluate the rpc call to
456 * find if there's cps for this guy. We treat other errors (except network failures
457 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
458 * we could like to deal this problem the other way around (i.e. if code == NOCPS
459 * ignore else retry next time) but the problem is that there're other errors (i.e.
460 * EPERM) for which we don't want to retry and we don't know the whole code list!
462 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
464 * We would have preferred to use a while loop and try again since ops in protected
465 * acls for this host will fail now but they'll be reevaluated on any subsequent
466 * call. The attempt to wait for a quorum/sync site or network error won't work
467 * since this problems really should only occurs during a complete fileserver
468 * restart. Since the fileserver will start before the ptservers (and thus before
469 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
471 host->hcpsfailed = 1;
472 ViceLog(0, ("Warning: GetHostCPS failed (%d) for %x; will retry\n", code, host->host));
474 host->hcpsfailed = 0;
475 ViceLog(1, ("gethost: GetHostCPS failed (%d) for %x; ignored\n", code, host->host));
477 if (host->hcps.prlist_val)
478 free(host->hcps.prlist_val);
479 host->hcps.prlist_val = NULL;
480 host->hcps.prlist_len = 0; /* Make sure it's zero */
482 host->hcpsfailed = 0;
484 host->hostFlags &= ~HCPS_INPROGRESS;
485 /* signal all who are waiting */
486 if ( host->hostFlags & HCPS_WAITING) /* somebody is waiting */
488 host->hostFlags &= ~HCPS_WAITING;
489 #ifdef AFS_PTHREAD_ENV
490 assert(pthread_cond_broadcast(&host->cond) == 0);
491 #else /* AFS_PTHREAD_ENV */
492 if ( (code = LWP_NoYieldSignal( &(host->hostFlags) )) != LWP_SUCCESS )
493 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
494 #endif /* AFS_PTHREAD_ENV */
498 /* args in net byte order */
499 void h_flushhostcps(register afs_uint32 hostaddr, register afs_uint32 hport)
501 register struct host *host;
505 host = h_Lookup_r(hostaddr, hport, &held);
507 host->hcpsfailed = 1;
518 * Allocate a host. It will be identified by the peer (ip,port) info in the
519 * rx connection provided. The host is returned held and locked
521 #define DEF_ROPCONS 2115
523 struct host *h_Alloc_r(register struct rx_connection *r_con)
526 struct servent *serverentry;
527 register index = h_HashIndex(rxr_HostOf(r_con));
528 register struct host *host;
529 static struct rx_securityClass *sc = 0;
531 struct h_hashChain* h_hashChain;
532 #if FS_STATS_DETAILED
533 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order*/
534 #endif /* FS_STATS_DETAILED */
538 h_hashChain = (struct h_hashChain*) malloc(sizeof(struct h_hashChain));
540 ViceLog(0, ("Failed malloc in h_Alloc_r\n"));
543 h_hashChain->hostPtr = host;
544 h_hashChain->addr = rxr_HostOf(r_con);
545 h_hashChain->next = hostHashTable[index];
546 hostHashTable[index] = h_hashChain;
548 host->host = rxr_HostOf(r_con);
549 host->port = rxr_PortOf(r_con);
550 if(consolePort == 0 ) { /* find the portal number for console */
551 #if defined(AFS_OSF_ENV)
552 serverentry = getservbyname("ropcons", "");
554 serverentry = getservbyname("ropcons", 0);
557 consolePort = serverentry->s_port;
559 consolePort = htons(DEF_ROPCONS); /* Use a default */
561 if (host->port == consolePort) host->Console = 1;
562 /* Make a callback channel even for the console, on the off chance that it
563 makes a request that causes a break call back. It shouldn't. */
566 sc = rxnull_NewClientSecurityObject();
567 host->callback_rxcon = rx_NewConnection (host->host, host->port,
569 rx_SetConnDeadTime(host->callback_rxcon, 50);
570 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
572 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
574 host->hcps.prlist_val = NULL;
575 host->hcps.prlist_len = 0;
578 host->hcpsfailed = 0; /* save cycles */
579 h_gethostcps(host); /* do this under host hold/lock */
581 host->FirstClient = 0;
584 h_InsertList_r(host); /* update global host List */
585 #if FS_STATS_DETAILED
587 * Compare the new host's IP address (in host byte order) with ours
588 * (the File Server's), remembering if they are in the same network.
590 newHostAddr_HBO = (afs_uint32)ntohl(host->host);
591 host->InSameNetwork = h_AddrInSameNetwork(FS_HostAddr_HBO,
593 #endif /* FS_STATS_DETAILED */
599 /* Lookup a host given an IP address and UDP port number. */
600 /* hostaddr and hport are in network order */
601 /* Note: host should be released by caller if 0 == *heldp and non-null */
602 /* hostaddr and hport are in network order */
603 struct host *h_Lookup_r(afs_uint32 hostaddr, afs_uint32 hport, int *heldp)
605 register afs_int32 now;
606 register struct host *host=0;
607 register struct h_hashChain* chain;
608 register index = h_HashIndex(hostaddr);
609 extern int hostaclRefresh;
612 for (chain=hostHashTable[index]; chain; chain=chain->next) {
613 host = chain->hostPtr;
615 if (!(host->hostFlags & HOSTDELETED) && chain->addr == hostaddr
616 && host->port == hport) {
617 *heldp = h_Held_r(host);
621 if (host->hostFlags & HOSTDELETED) {
628 now = FT_ApproxTime(); /* always evaluate "now" */
629 if (host->hcpsfailed || (host->cpsCall+hostaclRefresh < now )) {
631 * Every hostaclRefresh period (def 2 hrs) get the new
632 * membership list for the host. Note this could be the
633 * first time that the host is added to a group. Also
634 * here we also retry on previous legitimate hcps failures.
636 * If we get here we still have a host hold.
638 h_gethostcps_r(host,now);
648 /* Lookup a host given its UUID. */
649 struct host *h_LookupUuid_r(afsUUID *uuidp)
651 register struct host *host=0;
652 register struct h_hashChain* chain;
653 register index = h_UuidHashIndex(uuidp);
655 for (chain=hostUuidHashTable[index]; chain; chain=chain->next) {
656 host = chain->hostPtr;
658 if (!(host->hostFlags & HOSTDELETED) && host->interface
659 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
670 * h_Hold_r: Establish a hold by the current LWP on this host--the host
671 * or its clients will not be physically deleted until all holds have
673 * NOTE: h_Hold_r is a macro defined in host.h.
676 /* h_TossStuff_r: Toss anything in the host structure (the host or
677 * clients marked for deletion. Called from h_Release_r ONLY.
678 * To be called, there must be no holds, and either host->deleted
679 * or host->clientDeleted must be set.
681 int h_TossStuff_r(register struct host *host)
683 register struct client **cp, *client;
686 /* if somebody still has this host held */
687 for (i=0; (i<h_maxSlots)&&(!(host)->holds[i]); i++);
691 /* if somebody still has this host locked */
692 if (h_NBLock_r(host) != 0) {
694 ViceLog(0, ("Warning: h_TossStuff_r failed; Host %s:%d was locked.\n",
695 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
701 /* ASSUMPTION: rxi_FreeConnection() does not yield */
702 for (cp = &host->FirstClient; (client = *cp); ) {
703 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
704 if (client->refCount) {
706 ViceLog(0, ("Warning: Host %s:%d client %x refcount %d while deleting.\n",
707 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
708 client, client->refCount));
710 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val) {
711 free(client->CPS.prlist_val);
712 client->CPS.prlist_val = NULL;
714 client->CPS.prlist_len = 0;
716 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
718 CurrentConnections--;
721 } else cp = &client->next;
724 /* We've just cleaned out all the deleted clients; clear the flag */
725 host->hostFlags &= ~CLIENTDELETED;
727 if (host->hostFlags & HOSTDELETED) {
728 register struct h_hashChain **hp, *th;
729 register struct rx_connection *rxconn;
734 if (host->Console & 1) Console--;
735 if ((rxconn = host->callback_rxcon)) {
736 host->callback_rxcon = (struct rx_connection *)0;
738 * If rx_DestroyConnection calls h_FreeConnection we will
739 * deadlock on the host_glock_mutex. Work around the problem
740 * by unhooking the client from the connection before
741 * destroying the connection.
743 client = rx_GetSpecific(rxconn, rxcon_client_key);
744 if (client && client->tcon == rxconn)
746 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
747 rx_DestroyConnection(rxconn);
749 if (host->hcps.prlist_val)
750 free(host->hcps.prlist_val);
751 host->hcps.prlist_val = NULL;
752 host->hcps.prlist_len = 0;
753 DeleteAllCallBacks_r(host, 1);
754 host->hostFlags &= ~RESETDONE; /* just to be safe */
756 /* if alternate addresses do not exist */
757 if ( !(host->interface) )
759 for (hp = &hostHashTable[h_HashIndex(host->host)];
760 (th = *hp); hp = &th->next)
763 if (th->hostPtr == host)
766 h_DeleteList_r(host);
774 /* delete all hash entries for the UUID */
775 uuidp = &host->interface->uuid;
776 for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)];
777 (th = *hp); hp = &th->next) {
779 if (th->hostPtr == host)
786 /* delete all hash entries for alternate addresses */
787 assert(host->interface->numberOfInterfaces > 0 );
788 for ( i=0; i < host->interface->numberOfInterfaces; i++)
790 hostAddr = host->interface->addr[i];
791 for (hp = &hostHashTable[h_HashIndex(hostAddr)];
792 (th = *hp); hp = &th->next)
795 if (th->hostPtr == host)
803 free(host->interface);
804 host->interface = NULL;
805 h_DeleteList_r(host); /* remove host from global host List */
807 } /* if alternate address exists */
812 /* Called by rx when a server connection disappears */
813 int h_FreeConnection(struct rx_connection *tcon)
815 register struct client *client;
817 client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
820 if (client->tcon == tcon)
821 client->tcon = (struct rx_connection *)0;
824 } /*h_FreeConnection*/
827 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
828 * system at the start of the enumeration (perhaps more). Hosts may be deleted
829 * (have delete flag set); ditto for clients. (*proc) is always called with
830 * host h_held(). The hold state of the host with respect to this lwp is passed
831 * to (*proc) as the param held. The proc should return 0 if the host should be
832 * released, 1 if it should be held after enumeration.
834 void h_Enumerate(int (*proc)(), char *param)
836 register struct host *host, **list;
838 register int i, count;
841 if (hostCount == 0) {
845 list = (struct host **)malloc(hostCount * sizeof(struct host *));
847 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
850 held = (int *)malloc(hostCount * sizeof(int));
852 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
855 for (count = 0, host = hostList ; host ; host = host->next, count++) {
857 if (!(held[count] = h_Held_r(host)))
860 assert(count == hostCount);
862 for ( i = 0 ; i < count ; i++) {
863 held[i] = (*proc)(list[i], held[i], param);
865 h_Release(list[i]);/* this might free up the host */
871 /* h_Enumerate_r (revised):
872 * Calls (*proc)(host, held, param) for each host in hostList, starting
874 * Hosts may be deleted (have delete flag set); ditto for clients.
875 * (*proc) is always called with
876 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
877 * host with respect to this lwp is passed to (*proc) as the param held.
878 * The proc should return 0 if the host should be released, 1 if it should
879 * be held after enumeration.
881 void h_Enumerate_r(int (*proc)(), struct host* enumstart, char *param)
883 register struct host *host;
886 if (hostCount == 0) {
889 for (host = enumstart ; host ; host = host->next) {
890 if (!(held = h_Held_r(host)))
892 held = (*proc)(host, held, param);
894 h_Release_r(host);/* this might free up the host */
898 /* inserts a new HashChain structure corresponding to this UUID */
899 void hashInsertUuid_r(struct afsUUID *uuid, struct host* host)
902 struct h_hashChain* chain;
904 /* hash into proper bucket */
905 index = h_UuidHashIndex(uuid);
907 /* insert into beginning of list for this bucket */
908 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
910 ViceLog(0, ("Failed malloc in hashInsertUuid_r\n"));
914 chain->hostPtr = host;
915 chain->next = hostUuidHashTable[index];
916 hostUuidHashTable[index] = chain;
919 /* Host is returned held */
920 struct host *h_GetHost_r(struct rx_connection *tcon)
923 struct host *oldHost;
926 struct interfaceAddr interf;
928 struct Identity *identP = NULL;
932 char hoststr[16], hoststr2[16];
934 haddr = rxr_HostOf(tcon);
935 hport = rxr_PortOf(tcon);
938 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
939 host = h_Lookup_r(haddr, hport, &held);
940 if (host && !identP && !(host->Console&1)) {
941 /* This is a new connection, and we already have a host
942 * structure for this address. Verify that the identity
943 * of the caller matches the identity in the host structure.
946 if ( !(host->hostFlags & ALTADDR) )
948 /* Another thread is doing initialization */
950 if ( !held) h_Release_r(host);
951 ViceLog(125, ("Host %s:%d starting h_Lookup again\n",
952 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
955 host->hostFlags &= ~ALTADDR;
957 code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
959 if ( code == RXGEN_OPCODE ) {
960 identP = (struct Identity *)malloc(sizeof(struct Identity));
962 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
966 rx_SetSpecific(tcon, rxcon_ident_key, identP);
967 /* The host on this connection was unable to respond to
968 * the WhoAreYou. We will treat this as a new connection
969 * from the existing host. The worst that can happen is
970 * that we maintain some extra callback state information */
971 if (host->interface) {
973 ("Host %s:%d used to support WhoAreYou, deleting.\n",
974 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
975 host->hostFlags |= HOSTDELETED;
977 if (!held) h_Release_r(host);
981 } else if (code == 0) {
983 identP = (struct Identity *)malloc(sizeof(struct Identity));
985 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
989 identP->uuid = interf.uuid;
990 rx_SetSpecific(tcon, rxcon_ident_key, identP);
991 /* Check whether the UUID on this connection matches
992 * the UUID in the host structure. If they don't match
993 * then this is not the same host as before. */
994 if ( !host->interface
995 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid) ) {
997 ("Host %s:%d has changed its identity, deleting.\n",
998 afs_inet_ntoa_r(host->host, hoststr), host->port));
999 host->hostFlags |= HOSTDELETED;
1001 if (!held) h_Release_r(host);
1006 afs_inet_ntoa_r(host->host, hoststr);
1007 ViceLog(0,("CB: WhoAreYou failed for %s:%d, error %d\n",
1008 hoststr, ntohs(host->port), code));
1009 host->hostFlags |= VENUSDOWN;
1011 host->hostFlags |= ALTADDR;
1014 if ( ! (host->hostFlags & ALTADDR) )
1016 /* another thread is doing the initialisation */
1017 ViceLog(125, ("Host %s:%d waiting for host-init to complete\n",
1018 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1021 if ( !held) h_Release_r(host);
1022 ViceLog(125, ("Host %s:%d starting h_Lookup again\n",
1023 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1026 /* We need to check whether the identity in the host structure
1027 * matches the identity on the connection. If they don't match
1028 * then treat this a new host. */
1029 if ( !(host->Console&1)
1030 && ( ( !identP->valid && host->interface )
1031 || ( identP->valid && !host->interface )
1033 && !afs_uuid_equal(&identP->uuid, &host->interface->uuid) ) ) )
1035 char uuid1[128], uuid2[128];
1037 afsUUID_to_string(identP->uuid, uuid1, 127);
1038 if (host->interface)
1039 afsUUID_to_string(host->interface->uuid, uuid2, 127);
1041 ("CB: new identity for host %s:%d, deleting(%x %x %s %s)\n",
1042 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1043 identP->valid, host->interface, identP->valid ? uuid1 :
1044 "", host->interface ? uuid2 : ""));
1046 /* The host in the cache is not the host for this connection */
1047 host->hostFlags |= HOSTDELETED;
1049 if (!held) h_Release_r(host);
1053 host = h_Alloc_r(tcon); /* returned held and locked */
1054 h_gethostcps_r(host,FT_ApproxTime());
1055 if (!(host->Console&1)) {
1056 if (!identP || !interfValid) {
1058 code = RXAFSCB_WhoAreYou(host->callback_rxcon, &interf);
1060 if ( code == RXGEN_OPCODE ) {
1061 identP = (struct Identity *)malloc(sizeof(struct Identity));
1063 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1067 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1069 ("Host %s:%d does not support WhoAreYou.\n",
1070 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1072 } else if (code == 0) {
1074 identP = (struct Identity *)malloc(sizeof(struct Identity));
1076 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1080 identP->uuid = interf.uuid;
1081 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1082 ViceLog(25, ("WhoAreYou success on %s:%d\n",
1083 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1086 if (code == 0 && !identP->valid) {
1088 code = RXAFSCB_InitCallBackState(host->callback_rxcon);
1090 } else if (code == 0) {
1091 oldHost = h_LookupUuid_r(&identP->uuid);
1093 /* This is a new address for an existing host. Update
1094 * the list of interfaces for the existing host and
1095 * delete the host structure we just allocated. */
1096 if (!(held = h_Held_r(oldHost)))
1099 ViceLog(25, ("CB: new addr %s:%d for old host %s:%d\n",
1100 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1101 afs_inet_ntoa_r(oldHost->host, hoststr2), ntohs(oldHost->port)));
1102 host->hostFlags |= HOSTDELETED;
1106 addInterfaceAddr_r(host, haddr);
1108 /* This really is a new host */
1109 hashInsertUuid_r(&identP->uuid, host);
1111 code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
1115 ViceLog(25, ("InitCallBackState3 success on %s:%d\n",
1116 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
1117 assert(interfValid == 1);
1118 initInterfaceAddr_r(host, &interf);
1123 afs_inet_ntoa_r(host->host, hoststr);
1124 ViceLog(0,("CB: RCallBackConnectBack failed for %s:%d\n",
1125 hoststr, ntohs(host->port)));
1126 host->hostFlags |= VENUSDOWN;
1129 host->hostFlags |= RESETDONE;
1132 host->hostFlags |= ALTADDR;/* host structure iniatilisation complete */
1140 static char localcellname[PR_MAXNAMELEN+1];
1141 char local_realm[AFS_REALM_SZ] = "";
1144 void h_InitHostPackage()
1146 afsconf_GetLocalCell (confDir, localcellname, PR_MAXNAMELEN);
1147 if (!local_realm[0]) {
1148 if (afs_krb_get_lrealm(local_realm, 0) != 0/*KSUCCESS*/) {
1149 ViceLog(0, ("afs_krb_get_lrealm failed, using %s.\n",localcellname));
1150 strcpy (local_realm, localcellname);
1153 rxcon_ident_key = rx_KeyCreate((rx_destructor_t)free);
1154 rxcon_client_key = rx_KeyCreate((rx_destructor_t)0);
1155 #ifdef AFS_PTHREAD_ENV
1156 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1157 #endif /* AFS_PTHREAD_ENV */
1160 static int MapName_r(char *aname, char *acell, afs_int32 *aval)
1165 afs_int32 anamelen, cnamelen;
1169 anamelen=strlen(aname);
1170 if (anamelen >= PR_MAXNAMELEN)
1171 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1173 lnames.namelist_len = 1;
1174 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1175 lids.idlist_len = 0;
1176 lids.idlist_val = NULL;
1178 cnamelen=strlen(acell);
1180 if (strcasecmp(local_realm, acell) && strcasecmp(localcellname, acell)) {
1181 ViceLog(2, ("MapName: cell is foreign. cell=%s, localcell=%s, localrealm=%s\n",
1182 acell, localcellname, local_realm));
1183 if ((anamelen+cnamelen+1) >= PR_MAXNAMELEN) {
1184 ViceLog(2, ("MapName: Name too long, using AnonymousID for %s@%s\n",
1186 *aval = AnonymousID;
1189 foreign = 1; /* attempt cross-cell authentication */
1190 tname = (char *) malloc(anamelen+cnamelen+2);
1192 ViceLog(0, ("Failed malloc in MapName_r\n"));
1195 strcpy(tname, aname);
1196 tname[anamelen] = '@';
1197 strcpy(tname+anamelen+1, acell);
1198 lnames.namelist_val = (prname *) tname;
1203 code = pr_NameToId(&lnames, &lids);
1206 if (lids.idlist_val) {
1207 *aval = lids.idlist_val[0];
1208 if (*aval == AnonymousID) {
1209 ViceLog(2, ("MapName: NameToId on %s returns anonymousID\n", lnames.namelist_val));
1211 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1213 ViceLog(0, ("MapName: NameToId on '%s' is unknown\n", lnames.namelist_val));
1219 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1226 /* NOTE: this returns the client with a Shared lock */
1227 struct client *h_ID2Client(afs_int32 vid)
1229 register struct client *client;
1230 register struct host *host;
1234 for (host=hostList; host; host=host->next) {
1235 if (host->hostFlags & HOSTDELETED)
1237 for (client = host->FirstClient; client; client = client->next) {
1238 if (!client->deleted && client->ViceId == vid) {
1241 ObtainSharedLock(&client->lock);
1255 * Called by the server main loop. Returns a h_Held client, which must be
1256 * released later the main loop. Allocates a client if the matching one
1257 * isn't around. The client is returned with its reference count incremented
1258 * by one. The caller must call h_ReleaseClient_r when finished with
1261 struct client *h_FindClient_r(struct rx_connection *tcon)
1263 register struct client *client;
1264 register struct host *host;
1265 struct client *oldClient;
1270 #if (64-MAXKTCNAMELEN)
1271 ticket name length != 64
1275 char uname[PR_MAXNAMELEN];
1276 char tcell[MAXKTCREALMLEN];
1279 client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1280 if (client && !client->deleted) {
1282 h_Hold_r(client->host);
1283 if (client->prfail != 2) { /* Could add shared lock on client here */
1284 /* note that we don't have to lock entry in this path to
1285 * ensure CPS is initialized, since we don't call rx_SetSpecific
1286 * until initialization is done, and we only get here if
1287 * rx_GetSpecific located the client structure.
1292 ObtainWriteLock(&client->lock); /* released at end */
1294 } else if (client) {
1298 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1299 ViceLog(5,("FindClient: authenticating connection: authClass=%d\n",
1301 if (authClass == 1) {
1302 /* A bcrypt tickets, no longer supported */
1303 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1304 viceid = AnonymousID;
1305 expTime = 0x7fffffff;
1306 } else if (authClass == 2) {
1309 /* kerberos ticket */
1310 code = rxkad_GetServerInfo (tcon, /*level*/0, &expTime,
1311 tname, tinst, tcell, &kvno);
1313 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1314 viceid = AnonymousID;
1315 expTime = 0x7fffffff;
1317 int ilen = strlen(tinst);
1319 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1320 tname, tinst, tcell, expTime, kvno));
1321 strncpy (uname, tname, sizeof(uname));
1323 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1325 strcat (uname, ".");
1326 strcat (uname, tinst);
1328 /* translate the name to a vice id */
1329 code = MapName_r(uname, tcell, &viceid);
1332 ViceLog(1, ("failed to map name=%s, cell=%s -> code=%d\n",
1333 uname, tcell, code));
1335 viceid = AnonymousID;
1336 expTime = 0x7fffffff;
1340 viceid = AnonymousID; /* unknown security class */
1341 expTime = 0x7fffffff;
1345 host = h_GetHost_r(tcon); /* Returns it h_Held */
1347 /* First try to find the client structure */
1348 for (client = host->FirstClient; client; client = client->next) {
1349 if (!client->deleted && (client->sid == rxr_CidOf(tcon)) &&
1350 (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1351 if (client->tcon && (client->tcon != tcon)) {
1352 ViceLog(0, ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
1353 client->ViceId, client->sid, client->tcon, tcon));
1354 oldClient = (struct client *) rx_GetSpecific(client->tcon,
1357 if (oldClient == client)
1358 rx_SetSpecific(client->tcon, rxcon_client_key, NULL);
1361 ("Client-conn mismatch: CL1=%x, CN=%x, CL2=%x\n",
1362 client, client->tcon, oldClient));
1364 client->tcon = (struct rx_connection *)0;
1368 ObtainWriteLock(&client->lock);
1374 /* Still no client structure - get one */
1377 ObtainWriteLock(&client->lock);
1378 client->refCount = 1;
1379 client->host = host;
1380 client->next = host->FirstClient;
1381 host->FirstClient = client;
1382 #if FS_STATS_DETAILED
1383 client->InSameNetwork = host->InSameNetwork;
1384 #endif /* FS_STATS_DETAILED */
1385 client->ViceId = viceid;
1386 client->expTime = expTime; /* rx only */
1387 client->authClass = authClass; /* rx only */
1388 client->sid = rxr_CidOf(tcon);
1389 client->VenusEpoch = rxr_GetEpoch(tcon);
1390 client->CPS.prlist_val = 0;
1391 client->CPS.prlist_len = 0;
1392 CurrentConnections++; /* increment number of connections */
1395 client->prfail = fail;
1397 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1398 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID)) {
1399 free(client->CPS.prlist_val);
1401 client->CPS.prlist_val = NULL;
1402 client->CPS.prlist_len = 0;
1403 client->ViceId = viceid;
1404 client->expTime = expTime;
1406 if (viceid == ANONYMOUSID) {
1407 client->CPS.prlist_len = AnonCPS.prlist_len;
1408 client->CPS.prlist_val = AnonCPS.prlist_val;
1411 code = pr_GetCPS(viceid, &client->CPS);
1415 ViceLog(0, ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
1417 afs_inet_ntoa_r(client->host->host, hoststr),
1418 ntohs(client->host->port)));
1420 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1421 * all protection servers and reevaluates things if no
1422 * sync server or quorum is found we could still end up
1423 * with one of these errors. In such case we would like to
1424 * reevaluate the rpc call to find if there's cps for this
1425 * guy. We treat other errors (except network failures
1426 * ones - i.e. code < 0) as an indication that there is no
1427 * CPS for this host. Ideally we could like to deal this
1428 * problem the other way around (i.e. if code == NOCPS
1429 * ignore else retry next time) but the problem is that
1430 * there're other errors (i.e. EPERM) for which we don't
1431 * want to retry and we don't know the whole code list!
1433 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1437 /* the disabling of system:administrators is so iffy and has so many
1438 * possible failure modes that we will disable it again */
1439 /* Turn off System:Administrator for safety
1440 if (AL_IsAMember(SystemId, client->CPS) == 0)
1441 assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1444 /* Now, tcon may already be set to a rock, since we blocked with no host
1445 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1446 * required). So, before setting the RPC's rock, we should disconnect
1447 * the RPC from the other client structure's rock.
1449 oldClient = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1450 if (oldClient && oldClient->tcon == tcon) {
1451 oldClient->tcon = (struct rx_connection *) 0;
1452 /* rx_SetSpecific will be done immediately below */
1454 client->tcon = tcon;
1455 rx_SetSpecific(tcon, rxcon_client_key, client);
1456 ReleaseWriteLock(&client->lock);
1460 } /*h_FindClient_r*/
1462 int h_ReleaseClient_r(struct client *client)
1464 assert(client->refCount > 0);
1471 * Sigh: this one is used to get the client AGAIN within the individual
1472 * server routines. This does not bother h_Holding the host, since
1473 * this is assumed already have been done by the server main loop.
1474 * It does check tokens, since only the server routines can return the
1475 * VICETOKENDEAD error code
1477 int GetClient(struct rx_connection * tcon, struct client **cp)
1479 register struct client *client;
1483 *cp = client = (struct client *) rx_GetSpecific(tcon, rxcon_client_key);
1484 if (!(client && client->tcon && rxr_CidOf(client->tcon) == client->sid)) {
1486 ViceLog(0, ("GetClient: no client in conn %x\n", tcon));
1488 ViceLog(0, ("GetClient: tcon %x tcon sid %d client sid %d\n",
1489 client->tcon, client->tcon ? rxr_CidOf(client->tcon)
1490 : -1, client->sid));
1494 client->LastCall > client->expTime && client->expTime) {
1496 ViceLog(1, ("Token for %s at %s:%d expired %d\n",
1498 afs_inet_ntoa_r(client->host->host, hoststr),
1499 ntohs(client->host->port), client->expTime));
1501 return VICETOKENDEAD;
1510 /* Client user name for short term use. Note that this is NOT inexpensive */
1511 char *h_UserName(struct client *client)
1513 static char User[PR_MAXNAMELEN+1];
1517 lids.idlist_len = 1;
1518 lids.idlist_val = (afs_int32 *)malloc(1*sizeof(afs_int32));
1519 if (!lids.idlist_val) {
1520 ViceLog(0, ("Failed malloc in h_UserName\n"));
1523 lnames.namelist_len = 0;
1524 lnames.namelist_val = (prname *)0;
1525 lids.idlist_val[0] = client->ViceId;
1526 if (pr_IdToName(&lids,&lnames)) {
1527 /* We need to free id we alloced above! */
1528 free(lids.idlist_val);
1529 return "*UNKNOWN USER NAME*";
1531 strncpy(User,lnames.namelist_val[0],PR_MAXNAMELEN);
1532 free(lids.idlist_val);
1533 free(lnames.namelist_val);
1542 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1543 CEs, CEBlocks, HTs, HTBlocks));
1549 h_PrintClient(register struct host *host, int held, StreamHandle_t *file)
1551 register struct client *client;
1558 if (host->hostFlags & HOSTDELETED) {
1562 sprintf(tmpStr,"Host %s:%d down = %d, LastCall %s",
1563 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1564 (host->hostFlags & VENUSDOWN),
1565 afs_ctime((time_t *)&host->LastCall, tbuffer, sizeof(tbuffer)));
1566 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1567 for (client = host->FirstClient; client; client=client->next) {
1568 if (!client->deleted) {
1570 sprintf(tmpStr, " user id=%d, name=%s, sl=%s till %s",
1571 client->ViceId, h_UserName(client),
1572 client->authClass ? "Authenticated" : "Not authenticated",
1574 afs_ctime((time_t *)&client->expTime, tbuffer, sizeof(tbuffer))
1576 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1579 sprintf(tmpStr, " user=%s, no current server connection\n",
1580 h_UserName(client));
1581 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1583 sprintf(tmpStr, " CPS-%d is [", client->CPS.prlist_len);
1584 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1585 if (client->CPS.prlist_val) {
1586 for (i=0; i > client->CPS.prlist_len; i++) {
1587 sprintf(tmpStr, " %d", client->CPS.prlist_val[i]);
1588 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1591 sprintf(tmpStr, "]\n");
1592 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1603 * Print a list of clients, with last security level and token value seen,
1606 void h_PrintClients()
1612 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
1615 ViceLog(0, ("Couldn't create client dump file %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1618 now = FT_ApproxTime();
1619 sprintf(tmpStr, "List of active users at %s\n",
1620 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1621 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1622 h_Enumerate(h_PrintClient, (char *)file);
1623 STREAM_REALLYCLOSE(file);
1624 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
1631 h_DumpHost(register struct host *host, int held, StreamHandle_t *file)
1637 sprintf(tmpStr, "ip:%x port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
1638 host->host, ntohs(host->port), host->index, host->cblist,
1639 CheckLock(&host->lock), host->LastCall, host->ActiveCall,
1640 (host->hostFlags & VENUSDOWN), host->hostFlags&HOSTDELETED,
1641 host->Console, host->hostFlags & CLIENTDELETED,
1642 host->hcpsfailed, host->cpsCall);
1643 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1644 if (host->hcps.prlist_val)
1645 for (i=0; i < host->hcps.prlist_len; i++) {
1646 sprintf(tmpStr, " %d", host->hcps.prlist_val[i]);
1647 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1649 sprintf(tmpStr, "] [");
1650 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1651 if ( host->interface)
1652 for (i=0; i < host->interface->numberOfInterfaces; i++) {
1653 sprintf(tmpStr, " %x", host->interface->addr[i]);
1654 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1656 sprintf(tmpStr, "] holds: ");
1657 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1659 for (i = 0 ; i < h_maxSlots ; i++) {
1660 sprintf(tmpStr, "%04x", host->holds[i]);
1661 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1663 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
1664 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1675 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
1680 ViceLog(0, ("Couldn't create host dump file %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1683 now = FT_ApproxTime();
1684 sprintf(tmpStr, "List of active hosts at %s\n",
1685 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
1686 STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1687 h_Enumerate(h_DumpHost, (char *) file);
1688 STREAM_REALLYCLOSE(file);
1689 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
1695 * This counts the number of workstations, the number of active workstations,
1696 * and the number of workstations declared "down" (i.e. not heard from
1697 * recently). An active workstation has received a call since the cutoff
1698 * time argument passed.
1701 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
1704 register struct host *host;
1705 register int num=0, active=0, del=0;
1708 for (host = hostList; host; host = host->next) {
1709 if (!(host->hostFlags & HOSTDELETED)) {
1711 if (host->ActiveCall > cutofftime)
1713 if (host->hostFlags & VENUSDOWN)
1725 } /*h_GetWorkStats*/
1728 /*------------------------------------------------------------------------
1729 * PRIVATE h_ClassifyAddress
1732 * Given a target IP address and a candidate IP address (both
1733 * in host byte order), classify the candidate into one of three
1734 * buckets in relation to the target by bumping the counters passed
1738 * a_targetAddr : Target address.
1739 * a_candAddr : Candidate address.
1740 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
1741 * addresses are either in the same network
1742 * or the same subnet.
1743 * a_diffSubnetP : ...when the candidate is in a different
1745 * a_diffNetworkP : ...when the candidate is in a different
1752 * The target and candidate addresses are both in host byte
1753 * order, NOT network byte order, when passed in.
1757 *------------------------------------------------------------------------*/
1759 static void h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
1760 afs_int32 *a_sameNetOrSubnetP,
1761 afs_int32 *a_diffSubnetP,
1762 afs_int32 *a_diffNetworkP)
1763 { /*h_ClassifyAddress*/
1765 register int i; /*Iterator thru host hash table*/
1766 register struct host *hostP; /*Ptr to current host entry*/
1767 register afs_uint32 currHostAddr; /*Current host address*/
1768 afs_uint32 targetNet;
1769 afs_uint32 targetSubnet;
1771 afs_uint32 candSubnet;
1774 * Put bad values into the subnet info to start with.
1776 targetSubnet = (afs_uint32) 0;
1777 candSubnet = (afs_uint32) 0;
1780 * Pull out the network and subnetwork numbers from the target
1781 * and candidate addresses. We can short-circuit this whole
1782 * affair if the target and candidate addresses are not of the
1785 if (IN_CLASSA(a_targetAddr)) {
1786 if (!(IN_CLASSA(a_candAddr))) {
1787 (*a_diffNetworkP)++;
1790 targetNet = a_targetAddr & IN_CLASSA_NET;
1791 candNet = a_candAddr & IN_CLASSA_NET;
1792 if (IN_SUBNETA(a_targetAddr))
1793 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
1794 if (IN_SUBNETA(a_candAddr))
1795 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
1798 if (IN_CLASSB(a_targetAddr)) {
1799 if (!(IN_CLASSB(a_candAddr))) {
1800 (*a_diffNetworkP)++;
1803 targetNet = a_targetAddr & IN_CLASSB_NET;
1804 candNet = a_candAddr & IN_CLASSB_NET;
1805 if (IN_SUBNETB(a_targetAddr))
1806 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
1807 if (IN_SUBNETB(a_candAddr))
1808 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
1809 } /*Class B target*/
1811 if (IN_CLASSC(a_targetAddr)) {
1812 if (!(IN_CLASSC(a_candAddr))) {
1813 (*a_diffNetworkP)++;
1816 targetNet = a_targetAddr & IN_CLASSC_NET;
1817 candNet = a_candAddr & IN_CLASSC_NET;
1820 * Note that class C addresses can't have subnets,
1821 * so we leave the defaults untouched.
1823 } /*Class C target*/
1825 targetNet = a_targetAddr;
1826 candNet = a_candAddr;
1827 } /*Class D address*/
1830 * Now, simply compare the extracted net and subnet values for
1831 * the two addresses (which at this point are known to be of the
1834 if (targetNet == candNet) {
1835 if (targetSubnet == candSubnet)
1836 (*a_sameNetOrSubnetP)++;
1841 (*a_diffNetworkP)++;
1843 } /*h_ClassifyAddress*/
1846 /*------------------------------------------------------------------------
1847 * EXPORTED h_GetHostNetStats
1850 * Iterate through the host table, and classify each (non-deleted)
1851 * host entry into ``proximity'' categories (same net or subnet,
1852 * different subnet, different network).
1855 * a_numHostsP : Set to total number of (non-deleted) hosts.
1856 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
1857 * a_diffSubnetP : Set to # hosts on diff subnet as server.
1858 * a_diffNetworkP : Set to # hosts on diff network as server.
1864 * We only count non-deleted hosts. The storage pointed to by our
1865 * parameters is zeroed upon entry.
1869 *------------------------------------------------------------------------*/
1871 void h_GetHostNetStats(afs_int32 *a_numHostsP, afs_int32 *a_sameNetOrSubnetP,
1872 afs_int32 *a_diffSubnetP, afs_int32 *a_diffNetworkP)
1873 { /*h_GetHostNetStats*/
1875 register struct host *hostP; /*Ptr to current host entry*/
1876 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order*/
1879 * Clear out the storage pointed to by our parameters.
1881 *a_numHostsP = (afs_int32) 0;
1882 *a_sameNetOrSubnetP = (afs_int32) 0;
1883 *a_diffSubnetP = (afs_int32) 0;
1884 *a_diffNetworkP = (afs_int32) 0;
1887 for (hostP = hostList; hostP; hostP = hostP->next) {
1888 if (!(hostP->hostFlags & HOSTDELETED)) {
1890 * Bump the number of undeleted host entries found.
1891 * In classifying the current entry's address, make
1892 * sure to first convert to host byte order.
1895 currAddr_HBO = (afs_uint32)ntohl(hostP->host);
1896 h_ClassifyAddress(FS_HostAddr_HBO,
1901 } /*Only look at non-deleted hosts*/
1902 } /*For each host record hashed to this index*/
1905 } /*h_GetHostNetStats*/
1907 static afs_uint32 checktime;
1908 static afs_uint32 clientdeletetime;
1909 static struct AFSFid zerofid;
1913 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
1914 * Since it can serialize them, and pile up, it should be a separate LWP
1915 * from other events.
1917 int CheckHost(register struct host *host, int held)
1919 register struct client *client;
1922 /* Host is held by h_Enumerate */
1924 for (client = host->FirstClient; client; client = client->next) {
1925 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
1926 client->deleted = 1;
1927 host->hostFlags |= CLIENTDELETED;
1930 if (host->LastCall < checktime) {
1932 if (!(host->hostFlags & HOSTDELETED)) {
1933 if (host->LastCall < clientdeletetime) {
1934 host->hostFlags |= HOSTDELETED;
1935 if (!(host->hostFlags & VENUSDOWN)) {
1936 host->hostFlags &= ~ALTADDR; /* alternate address invalid*/
1937 if (host->interface) {
1939 code = RXAFSCB_InitCallBackState3(host->callback_rxcon,
1944 code = RXAFSCB_InitCallBackState(host->callback_rxcon);
1947 host->hostFlags |= ALTADDR; /* alternate addresses valid */
1951 afs_inet_ntoa_r(host->host, hoststr);
1953 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
1954 hoststr, ntohs(host->port)));
1955 host->hostFlags |= VENUSDOWN;
1957 /* Note: it's safe to delete hosts even if they have call
1958 * back state, because break delayed callbacks (called when a
1959 * message is received from the workstation) will always send a
1960 * break all call backs to the workstation if there is no
1966 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
1967 if (host->interface) {
1968 afsUUID uuid = host->interface->uuid;
1970 code = RXAFSCB_ProbeUuid(host->callback_rxcon, &uuid);
1973 if ( MultiProbeAlternateAddress_r(host) ) {
1975 afs_inet_ntoa_r(host->host, hoststr);
1977 ("ProbeUuid failed for host %s:%d\n",
1978 hoststr, ntohs(host->port)));
1979 host->hostFlags |= VENUSDOWN;
1984 code = RXAFSCB_Probe(host->callback_rxcon);
1988 afs_inet_ntoa_r(host->host, hoststr);
1989 ViceLog(0, ("ProbeUuid failed for host %s:%d\n",
1990 hoststr, ntohs(host->port)));
1991 host->hostFlags |= VENUSDOWN;
2006 * Set VenusDown for any hosts that have not had a call in 15 minutes and
2007 * don't respond to a probe. Note that VenusDown can only be cleared if
2008 * a message is received from the host (see ServerLWP in file.c).
2009 * Delete hosts that have not had any calls in 1 hour, clients that
2010 * have not had any calls in 15 minutes.
2012 * This routine is called roughly every 5 minutes.
2014 void h_CheckHosts() {
2015 afs_uint32 now = FT_ApproxTime();
2017 memset((char *)&zerofid, 0, sizeof(zerofid));
2019 * Send a probe to the workstation if it hasn't been heard from in
2022 checktime = now - 15*60;
2023 clientdeletetime = now - 120*60; /* 2 hours ago */
2024 h_Enumerate(CheckHost, NULL);
2029 * This is called with host locked and held. At this point, the
2030 * hostHashTable should not be having entries for the alternate
2031 * interfaces. This function has to insert these entries in the
2034 * The addresses in the ineterfaceAddr list are in host byte order.
2037 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
2043 struct Interface *interface;
2048 ViceLog(125,("initInterfaceAddr : host %x numAddr %d\n",
2049 host->host, interf->numberOfInterfaces));
2051 number = interf->numberOfInterfaces;
2052 myHost = host->host; /* current interface address */
2054 /* validation checks */
2055 if ( number < 0 || number > AFS_MAX_INTERFACE_ADDR )
2057 ViceLog(0,("Number of alternate addresses returned is %d\n",
2063 * Convert IP addresses to network byte order, and remove for
2064 * duplicate IP addresses from the interface list.
2066 for (i = 0, count = 0, found = 0; i < number; i++)
2068 interf->addr_in[i] = htonl(interf->addr_in[i]);
2069 for (j = 0 ; j < count ; j++) {
2070 if (interf->addr_in[j] == interf->addr_in[i])
2074 interf->addr_in[count] = interf->addr_in[i];
2075 if (interf->addr_in[count] == myHost)
2082 * Allocate and initialize an interface structure for this host.
2085 interface = (struct Interface *)
2086 malloc(sizeof(struct Interface) +
2087 (sizeof(afs_int32) * (count-1)));
2089 ViceLog(0, ("Failed malloc in initInterfaceAddr_r\n"));
2092 interface->numberOfInterfaces = count;
2094 interface = (struct Interface *)
2095 malloc(sizeof(struct Interface) +
2096 (sizeof(afs_int32) * count));
2098 interface->numberOfInterfaces = count + 1;
2099 interface->addr[count] = myHost;
2101 interface->uuid = interf->uuid;
2102 for (i = 0 ; i < count ; i++)
2103 interface->addr[i] = interf->addr_in[i];
2105 assert(!host->interface);
2106 host->interface = interface;
2108 for ( i=0; i < host->interface->numberOfInterfaces; i++)
2110 ViceLog(125,("--- alt address %x\n", host->interface->addr[i]));
2116 /* inserts a new HashChain structure corresponding to this address */
2117 void hashInsert_r(afs_int32 addr, struct host* host)
2120 struct h_hashChain* chain;
2122 /* hash into proper bucket */
2123 index = h_HashIndex(addr);
2125 /* insert into beginning of list for this bucket */
2126 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
2128 ViceLog(0, ("Failed malloc in hashInsert_r\n"));
2131 chain->hostPtr = host;
2132 chain->next = hostHashTable[index];
2134 hostHashTable[index] = chain;
2139 * This is called with host locked and held. At this point, the
2140 * hostHashTable should not be having entries for the alternate
2141 * interfaces. This function has to insert these entries in the
2144 * All addresses are in network byte order.
2147 addInterfaceAddr_r(struct host *host, afs_int32 addr)
2152 struct Interface *interface;
2155 assert(host->interface);
2157 ViceLog(125,("addInterfaceAddr : host %x addr %d\n",
2161 * Make sure this address is on the list of known addresses
2164 number = host->interface->numberOfInterfaces;
2165 for ( i=0, found=0; i < number && !found; i++)
2167 if ( host->interface->addr[i] == addr)
2171 interface = (struct Interface *)
2172 malloc(sizeof(struct Interface) +
2173 (sizeof(afs_int32) * number));
2175 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
2178 interface->numberOfInterfaces = number + 1;
2179 interface->uuid = host->interface->uuid;
2180 for (i = 0 ; i < number ; i++)
2181 interface->addr[i] = host->interface->addr[i];
2182 interface->addr[number] = addr;
2183 free(host->interface);
2184 host->interface = interface;
2188 * Create a hash table entry for this address
2190 hashInsert_r(addr, host);
2195 /* deleted a HashChain structure for this address and host */
2196 /* returns 1 on success */
2198 hashDelete_r(afs_int32 addr, struct host* host)
2202 register struct h_hashChain **hp, *th;
2204 for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp); )
2206 assert(th->hostPtr);
2207 if (th->hostPtr == host && th->addr == addr)
2222 ** prints out all alternate interface address for the host. The 'level'
2223 ** parameter indicates what level of debugging sets this output
2226 printInterfaceAddr(struct host *host, int level)
2229 if ( host-> interface )
2231 /* check alternate addresses */
2232 number = host->interface->numberOfInterfaces;
2233 assert( number > 0 );
2234 for ( i=0; i < number; i++)
2235 ViceLog(level, ("%x ", host->interface->addr[i]));
2237 ViceLog(level, ("\n"));