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>
26 #include <netinet/in.h>
39 #include <afs/assert.h>
42 #include <afs/afsint.h>
43 #include <afs/rxgen_consts.h>
45 #include <afs/errors.h>
46 #include <afs/ihandle.h>
47 #include <afs/vnode.h>
48 #include <afs/volume.h>
49 #ifdef AFS_ATHENA_STDENV
53 #include <afs/ptclient.h>
54 #include <afs/ptuser.h>
55 #include <afs/prs_fs.h>
57 #include <afs/afsutil.h>
59 #include <afs/cellconfig.h>
61 #include "viced_prototypes.h"
65 #ifdef AFS_DEMAND_ATTACH_FS
66 #include "../util/afsutil_prototypes.h"
67 #include "../tviced/serialize_state.h"
68 #endif /* AFS_DEMAND_ATTACH_FS */
70 #ifdef AFS_PTHREAD_ENV
71 pthread_mutex_t host_glock_mutex;
72 #endif /* AFS_PTHREAD_ENV */
75 extern int CurrentConnections;
77 extern int AnonymousID;
78 extern prlist AnonCPS;
80 extern struct afsconf_dir *confDir; /* config dir object */
81 extern int lwps; /* the max number of server threads */
82 extern afsUUID FS_HostUUID;
84 int CEs = 0; /* active clients */
85 int CEBlocks = 0; /* number of blocks of CEs */
86 struct client *CEFree = 0; /* first free client */
87 struct host *hostList = 0; /* linked list of all hosts */
88 int hostCount = 0; /* number of hosts in hostList */
92 static struct rx_securityClass *sc = NULL;
94 static void h_SetupCallbackConn_r(struct host * host);
95 static void h_AddHostToHashTable_r(afs_uint32 addr, afs_uint16 port, struct host * host);
96 static void h_AddHostToUuidHashTable_r(afsUUID * uuid, struct host * host);
97 static int h_DeleteHostFromHashTableByAddr_r(afs_uint32 addr, afs_uint16 port, struct host *host);
99 #define CESPERBLOCK 73
100 struct CEBlock { /* block of CESPERBLOCK file entries */
101 struct client entry[CESPERBLOCK];
104 static void h_TossStuff_r(register struct host *host);
105 static int hashDelete_r(afs_uint32 addr, afs_uint16 port, struct host *host);
108 * Make sure the subnet macros have been defined.
111 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
114 #ifndef IN_CLASSA_SUBNET
115 #define IN_CLASSA_SUBNET 0xffff0000
119 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
122 #ifndef IN_CLASSB_SUBNET
123 #define IN_CLASSB_SUBNET 0xffffff00
127 /* get a new block of CEs and chain it on CEFree */
131 register struct CEBlock *block;
134 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
136 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
137 ShutDownAndCore(PANIC);
140 for (i = 0; i < (CESPERBLOCK - 1); i++) {
141 Lock_Init(&block->entry[i].lock);
142 block->entry[i].next = &(block->entry[i + 1]);
144 block->entry[CESPERBLOCK - 1].next = 0;
145 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
146 CEFree = (struct client *)block;
152 /* get the next available CE */
153 static struct client *
156 register struct client *entry;
161 ViceLog(0, ("CEFree NULL in GetCE\n"));
162 ShutDownAndCore(PANIC);
166 CEFree = entry->next;
168 memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
174 /* return an entry to the free list */
176 FreeCE(register struct client *entry)
178 entry->next = CEFree;
185 * The HTs and HTBlocks variables were formerly static, but they are
186 * now referenced elsewhere in the FileServer.
188 int HTs = 0; /* active file entries */
189 int HTBlocks = 0; /* number of blocks of HTs */
190 static struct host *HTFree = 0; /* first free file entry */
193 * Hash tables of host pointers. We need two tables, one
194 * to map IP addresses onto host pointers, and another
195 * to map host UUIDs onto host pointers.
197 static struct h_hashChain *hostHashTable[h_HASHENTRIES];
198 static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
199 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
200 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
202 struct HTBlock { /* block of HTSPERBLOCK file entries */
203 struct host entry[h_HTSPERBLOCK];
207 /* get a new block of HTs and chain it on HTFree */
211 register struct HTBlock *block;
213 static int index = 0;
215 if (HTBlocks == h_MAXHOSTTABLES) {
216 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
217 ShutDownAndCore(PANIC);
220 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
222 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
223 ShutDownAndCore(PANIC);
225 #ifdef AFS_PTHREAD_ENV
226 for (i = 0; i < (h_HTSPERBLOCK); i++)
227 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
228 #endif /* AFS_PTHREAD_ENV */
229 for (i = 0; i < (h_HTSPERBLOCK); i++)
230 Lock_Init(&block->entry[i].lock);
231 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
232 block->entry[i].next = &(block->entry[i + 1]);
233 for (i = 0; i < (h_HTSPERBLOCK); i++)
234 block->entry[i].index = index++;
235 block->entry[h_HTSPERBLOCK - 1].next = 0;
236 HTFree = (struct host *)block;
237 hosttableptrs[HTBlocks++] = block->entry;
242 /* get the next available HT */
246 register struct host *entry;
250 assert(HTFree != NULL);
252 HTFree = entry->next;
254 memset((char *)entry, 0, HOST_TO_ZERO(entry));
260 /* return an entry to the free list */
262 FreeHT(register struct host *entry)
264 entry->next = HTFree;
271 static short consolePort = 0;
274 h_Release(register struct host *host)
283 * If this thread does not have a hold on this host AND
284 * if other threads also dont have any holds on this host AND
285 * If either the HOSTDELETED or CLIENTDELETED flags are set
289 h_Release_r(register struct host *host)
292 if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
293 if (!h_OtherHolds_r(host)) {
294 /* must avoid masking this until after h_OtherHolds_r runs
295 * but it should be run before h_TossStuff_r */
296 (host)->holds[h_holdSlot()] &= ~h_holdbit();
297 if ((host->hostFlags & HOSTDELETED)
298 || (host->hostFlags & CLIENTDELETED)) {
302 (host)->holds[h_holdSlot()] &= ~h_holdbit();
304 (host)->holds[h_holdSlot()] &= ~h_holdbit();
310 h_OtherHolds_r(register struct host *host)
312 register int i, bit, slot;
315 for (i = 0; i < h_maxSlots; i++) {
316 if (host->holds[i] != ((i == slot) ? bit : 0)) {
324 h_Lock_r(register struct host *host)
334 * returns 1 if already locked
335 * else returns locks and returns 0
339 h_NBLock_r(register struct host *host)
341 struct Lock *hostLock = &host->lock;
346 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
347 hostLock->excl_locked = WRITE_LOCK;
351 LOCK_UNLOCK(hostLock);
360 #if FS_STATS_DETAILED
361 /*------------------------------------------------------------------------
362 * PRIVATE h_AddrInSameNetwork
365 * Given a target IP address and a candidate IP address (both
366 * in host byte order), return a non-zero value (1) if the
367 * candidate address is in a different network from the target
371 * a_targetAddr : Target address.
372 * a_candAddr : Candidate address.
375 * 1 if the candidate address is in the same net as the target,
379 * The target and candidate addresses are both in host byte
380 * order, NOT network byte order, when passed in. We return
381 * our value as a character, since that's the type of field in
382 * the host structure, where this info will be stored.
386 *------------------------------------------------------------------------*/
389 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
390 { /*h_AddrInSameNetwork */
392 afs_uint32 targetNet;
396 * Pull out the network and subnetwork numbers from the target
397 * and candidate addresses. We can short-circuit this whole
398 * affair if the target and candidate addresses are not of the
401 if (IN_CLASSA(a_targetAddr)) {
402 if (!(IN_CLASSA(a_candAddr))) {
405 targetNet = a_targetAddr & IN_CLASSA_NET;
406 candNet = a_candAddr & IN_CLASSA_NET;
407 } else if (IN_CLASSB(a_targetAddr)) {
408 if (!(IN_CLASSB(a_candAddr))) {
411 targetNet = a_targetAddr & IN_CLASSB_NET;
412 candNet = a_candAddr & IN_CLASSB_NET;
413 } /*Class B target */
414 else if (IN_CLASSC(a_targetAddr)) {
415 if (!(IN_CLASSC(a_candAddr))) {
418 targetNet = a_targetAddr & IN_CLASSC_NET;
419 candNet = a_candAddr & IN_CLASSC_NET;
420 } /*Class C target */
422 targetNet = a_targetAddr;
423 candNet = a_candAddr;
424 } /*Class D address */
427 * Now, simply compare the extracted net values for the two addresses
428 * (which at this point are known to be of the same class)
430 if (targetNet == candNet)
435 } /*h_AddrInSameNetwork */
436 #endif /* FS_STATS_DETAILED */
439 /* Assumptions: called with held host */
441 h_gethostcps_r(register struct host *host, register afs_int32 now)
446 /* wait if somebody else is already doing the getCPS call */
447 while (host->hostFlags & HCPS_INPROGRESS) {
448 slept = 1; /* I did sleep */
449 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
450 #ifdef AFS_PTHREAD_ENV
451 pthread_cond_wait(&host->cond, &host_glock_mutex);
452 #else /* AFS_PTHREAD_ENV */
453 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
454 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
455 #endif /* AFS_PTHREAD_ENV */
459 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
460 if (host->hcps.prlist_val)
461 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
462 host->hcps.prlist_val = NULL;
463 host->hcps.prlist_len = 0;
464 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
467 code = pr_GetHostCPS(ntohl(host->host), &host->hcps);
471 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
472 * and reevaluates things if no sync server or quorum is found we could still end up
473 * with one of these errors. In such case we would like to reevaluate the rpc call to
474 * find if there's cps for this guy. We treat other errors (except network failures
475 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
476 * we could like to deal this problem the other way around (i.e. if code == NOCPS
477 * ignore else retry next time) but the problem is that there're other errors (i.e.
478 * EPERM) for which we don't want to retry and we don't know the whole code list!
480 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
482 * We would have preferred to use a while loop and try again since ops in protected
483 * acls for this host will fail now but they'll be reevaluated on any subsequent
484 * call. The attempt to wait for a quorum/sync site or network error won't work
485 * since this problems really should only occurs during a complete fileserver
486 * restart. Since the fileserver will start before the ptservers (and thus before
487 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
489 host->hcpsfailed = 1;
491 ("Warning: GetHostCPS failed (%d) for %x; will retry\n",
494 host->hcpsfailed = 0;
496 ("gethost: GetHostCPS failed (%d) for %x; ignored\n",
499 if (host->hcps.prlist_val)
500 free(host->hcps.prlist_val);
501 host->hcps.prlist_val = NULL;
502 host->hcps.prlist_len = 0; /* Make sure it's zero */
504 host->hcpsfailed = 0;
506 host->hostFlags &= ~HCPS_INPROGRESS;
507 /* signal all who are waiting */
508 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
509 host->hostFlags &= ~HCPS_WAITING;
510 #ifdef AFS_PTHREAD_ENV
511 assert(pthread_cond_broadcast(&host->cond) == 0);
512 #else /* AFS_PTHREAD_ENV */
513 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
514 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
515 #endif /* AFS_PTHREAD_ENV */
519 /* args in net byte order */
521 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
523 register struct host *host;
527 host = h_Lookup_r(hostaddr, hport, &held);
529 host->hcpsfailed = 1;
539 * Allocate a host. It will be identified by the peer (ip,port) info in the
540 * rx connection provided. The host is returned held and locked
542 #define DEF_ROPCONS 2115
545 h_Alloc_r(register struct rx_connection *r_con)
547 struct servent *serverentry;
550 #if FS_STATS_DETAILED
551 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
552 #endif /* FS_STATS_DETAILED */
556 host->host = rxr_HostOf(r_con);
557 host->port = rxr_PortOf(r_con);
559 h_AddHostToHashTable_r(host->host, host->port, host);
561 if (consolePort == 0) { /* find the portal number for console */
562 #if defined(AFS_OSF_ENV)
563 serverentry = getservbyname("ropcons", "");
565 serverentry = getservbyname("ropcons", 0);
568 consolePort = serverentry->s_port;
570 consolePort = htons(DEF_ROPCONS); /* Use a default */
572 if (host->port == consolePort)
574 /* Make a callback channel even for the console, on the off chance that it
575 * makes a request that causes a break call back. It shouldn't. */
576 h_SetupCallbackConn_r(host);
577 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
579 host->hcps.prlist_val = NULL;
580 host->hcps.prlist_len = 0;
581 host->interface = NULL;
583 host->hcpsfailed = 0; /* save cycles */
584 h_gethostcps(host); /* do this under host hold/lock */
586 host->FirstClient = NULL;
589 h_InsertList_r(host); /* update global host List */
590 #if FS_STATS_DETAILED
592 * Compare the new host's IP address (in host byte order) with ours
593 * (the File Server's), remembering if they are in the same network.
595 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
596 host->InSameNetwork =
597 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
598 #endif /* FS_STATS_DETAILED */
605 /* Make a callback channel even for the console, on the off chance that it
606 * makes a request that causes a break call back. It shouldn't. */
608 h_SetupCallbackConn_r(struct host * host)
611 sc = rxnull_NewClientSecurityObject();
612 host->callback_rxcon =
613 rx_NewConnection(host->host, host->port, 1, sc, 0);
614 rx_SetConnDeadTime(host->callback_rxcon, 50);
615 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
618 /* Lookup a host given an IP address and UDP port number. */
619 /* hostaddr and hport are in network order */
620 /* Note: host should be released by caller if 0 == *heldp and non-null */
621 /* hostaddr and hport are in network order */
623 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
626 struct host *host = 0;
627 struct h_hashChain *chain;
628 int index = h_HashIndex(haddr);
629 extern int hostaclRefresh;
632 for (chain = hostHashTable[index]; chain; chain = chain->next) {
633 host = chain->hostPtr;
635 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
636 && chain->port == hport) {
637 *heldp = h_Held_r(host);
641 if (host->hostFlags & HOSTDELETED) {
648 now = FT_ApproxTime(); /* always evaluate "now" */
649 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
651 * Every hostaclRefresh period (def 2 hrs) get the new
652 * membership list for the host. Note this could be the
653 * first time that the host is added to a group. Also
654 * here we also retry on previous legitimate hcps failures.
656 * If we get here we still have a host hold.
658 h_gethostcps_r(host, now);
668 /* Lookup a host given its UUID. */
670 h_LookupUuid_r(afsUUID * uuidp)
672 struct host *host = 0;
673 struct h_hashChain *chain;
674 int index = h_UuidHashIndex(uuidp);
676 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
677 host = chain->hostPtr;
679 if (!(host->hostFlags & HOSTDELETED) && host->interface
680 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
691 * h_Hold_r: Establish a hold by the current LWP on this host--the host
692 * or its clients will not be physically deleted until all holds have
694 * NOTE: h_Hold_r is a macro defined in host.h.
697 /* h_TossStuff_r: Toss anything in the host structure (the host or
698 * clients marked for deletion. Called from h_Release_r ONLY.
699 * To be called, there must be no holds, and either host->deleted
700 * or host->clientDeleted must be set.
703 h_TossStuff_r(register struct host *host)
705 register struct client **cp, *client;
708 /* if somebody still has this host held */
709 for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
713 /* if somebody still has this host locked */
714 if (h_NBLock_r(host) != 0) {
717 ("Warning: h_TossStuff_r failed; Host %s:%d was locked.\n",
718 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
724 /* ASSUMPTION: rxi_FreeConnection() does not yield */
725 for (cp = &host->FirstClient; (client = *cp);) {
726 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
728 ObtainWriteLockNoBlock(&client->lock, code);
732 ("Warning: h_TossStuff_r failed: Host %s:%d client %x was locked.\n",
733 afs_inet_ntoa_r(host->host, hoststr),
734 ntohs(host->port), client));
738 if (client->refCount) {
741 ("Warning: h_TossStuff_r failed: Host %s:%d client %x refcount %d.\n",
742 afs_inet_ntoa_r(host->host, hoststr),
743 ntohs(host->port), client, client->refCount));
744 /* This is the same thing we do if the host is locked */
745 ReleaseWriteLock(&client->lock);
748 client->CPS.prlist_len = 0;
749 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
750 free(client->CPS.prlist_val);
751 client->CPS.prlist_val = NULL;
753 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
755 CurrentConnections--;
757 ReleaseWriteLock(&client->lock);
763 /* We've just cleaned out all the deleted clients; clear the flag */
764 host->hostFlags &= ~CLIENTDELETED;
766 if (host->hostFlags & HOSTDELETED) {
767 register struct h_hashChain **hp, *th;
768 register struct rx_connection *rxconn;
770 struct AddrPort hostAddrPort;
773 if (host->Console & 1)
775 if ((rxconn = host->callback_rxcon)) {
776 host->callback_rxcon = (struct rx_connection *)0;
778 * If rx_DestroyConnection calls h_FreeConnection we will
779 * deadlock on the host_glock_mutex. Work around the problem
780 * by unhooking the client from the connection before
781 * destroying the connection.
783 client = rx_GetSpecific(rxconn, rxcon_client_key);
784 if (client && client->tcon == rxconn)
786 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
787 rx_DestroyConnection(rxconn);
789 if (host->hcps.prlist_val)
790 free(host->hcps.prlist_val);
791 host->hcps.prlist_val = NULL;
792 host->hcps.prlist_len = 0;
793 DeleteAllCallBacks_r(host, 1);
794 host->hostFlags &= ~RESETDONE; /* just to be safe */
796 /* if alternate addresses do not exist */
797 if (!(host->interface)) {
798 for (hp = &hostHashTable[h_HashIndex(host->host)]; (th = *hp);
801 if (th->hostPtr == host) {
803 h_DeleteList_r(host);
810 /* delete all hash entries for the UUID */
811 uuidp = &host->interface->uuid;
812 for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (th = *hp);
815 if (th->hostPtr == host) {
821 /* delete all hash entries for alternate addresses */
822 assert(host->interface->numberOfInterfaces > 0);
823 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
824 hostAddrPort = host->interface->interface[i];
826 for (hp = &hostHashTable[h_HashIndex(hostAddrPort.addr)]; (th = *hp);
829 if (th->hostPtr == host) {
836 free(host->interface);
837 host->interface = NULL;
838 h_DeleteList_r(host); /* remove host from global host List */
840 } /* if alternate address exists */
845 /* Called by rx when a server connection disappears */
847 h_FreeConnection(struct rx_connection *tcon)
849 register struct client *client;
851 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
854 if (client->tcon == tcon)
859 } /*h_FreeConnection */
862 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
863 * system at the start of the enumeration (perhaps more). Hosts may be deleted
864 * (have delete flag set); ditto for clients. (*proc) is always called with
865 * host h_held(). The hold state of the host with respect to this lwp is passed
866 * to (*proc) as the param held. The proc should return 0 if the host should be
867 * released, 1 if it should be held after enumeration.
870 h_Enumerate(int (*proc) (), char *param)
872 register struct host *host, **list;
874 register int i, count;
877 if (hostCount == 0) {
881 list = (struct host **)malloc(hostCount * sizeof(struct host *));
883 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
886 held = (int *)malloc(hostCount * sizeof(int));
888 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
891 for (count = 0, host = hostList; host; host = host->next, count++) {
893 if (!(held[count] = h_Held_r(host)))
896 assert(count == hostCount);
898 for (i = 0; i < count; i++) {
899 held[i] = (*proc) (list[i], held[i], param);
900 if (!H_ENUMERATE_ISSET_HELD(held[i]))
901 h_Release(list[i]); /* this might free up the host */
902 /* bail out of the enumeration early */
903 if (H_ENUMERATE_ISSET_BAIL(held[i]))
910 /* h_Enumerate_r (revised):
911 * Calls (*proc)(host, held, param) for each host in hostList, starting
913 * Hosts may be deleted (have delete flag set); ditto for clients.
914 * (*proc) is always called with
915 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
916 * host with respect to this lwp is passed to (*proc) as the param held.
917 * The proc should return 0 if the host should be released, 1 if it should
918 * be held after enumeration.
921 h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
923 register struct host *host, *next;
924 register int held, nheld;
926 if (hostCount == 0) {
929 if (enumstart && !(held = h_Held_r(enumstart)))
931 for (host = enumstart; host; host = next, held = nheld) {
933 if (next && !(nheld = h_Held_r(next)) && !H_ENUMERATE_ISSET_BAIL(held))
935 held = (*proc) (host, held, param);
936 if (!H_ENUMERATE_ISSET_HELD(held))
937 h_Release_r(host); /* this might free up the host */
938 if (H_ENUMERATE_ISSET_BAIL(held))
943 /* inserts a new HashChain structure corresponding to this UUID */
945 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
948 struct h_hashChain *chain;
950 /* hash into proper bucket */
951 index = h_UuidHashIndex(uuid);
953 /* insert into beginning of list for this bucket */
954 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
956 ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
960 chain->hostPtr = host;
961 chain->next = hostUuidHashTable[index];
962 hostUuidHashTable[index] = chain;
966 /* inserts a new HashChain structure corresponding to this address */
968 h_AddHostToHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
971 struct h_hashChain *chain;
973 /* hash into proper bucket */
974 index = h_HashIndex(addr);
976 /* insert into beginning of list for this bucket */
977 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
979 ViceLog(0, ("Failed malloc in h_AddHostToHashTable_r\n"));
982 chain->hostPtr = host;
983 chain->next = hostHashTable[index];
986 hostHashTable[index] = chain;
991 * This is called with host locked and held. At this point, the
992 * hostHashTable should not be having entries for the alternate
993 * interfaces. This function has to insert these entries in the
996 * All addresses are in network byte order.
999 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1004 struct Interface *interface;
1005 char hoststr[16], hoststr2[16];
1008 assert(host->interface);
1010 ViceLog(125, ("addInterfaceAddr : host %s:%d addr %s:%d\n",
1011 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1012 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
1015 * Make sure this address is on the list of known addresses
1018 number = host->interface->numberOfInterfaces;
1019 for (i = 0, found = 0; i < number && !found; i++) {
1020 if (host->interface->interface[i].addr == addr &&
1021 host->interface->interface[i].port == port)
1025 interface = (struct Interface *)
1026 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1028 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
1031 interface->numberOfInterfaces = number + 1;
1032 interface->uuid = host->interface->uuid;
1033 for (i = 0; i < number; i++)
1034 interface->interface[i] = host->interface->interface[i];
1035 interface->interface[number].addr = addr;
1036 interface->interface[number].port = port;
1037 free(host->interface);
1038 host->interface = interface;
1042 * Create a hash table entry for this address
1044 h_AddHostToHashTable_r(addr, port, host);
1051 * This is called with host locked and held. At this point, the
1052 * hostHashTable should not be having entries for the alternate
1053 * interfaces. This function has to insert these entries in the
1056 * All addresses are in network byte order.
1059 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1064 struct Interface *interface;
1065 char hoststr[16], hoststr2[16];
1068 assert(host->interface);
1070 ViceLog(125, ("removeInterfaceAddr : host %s:%d addr %s:%d\n",
1071 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1072 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
1075 * Make sure this address is on the list of known addresses
1078 interface = host->interface;
1079 number = host->interface->numberOfInterfaces;
1080 for (i = 0, found = 0; i < number; i++) {
1081 if (interface->interface[i].addr == addr &&
1082 interface->interface[i].port == port) {
1089 for (; i < number; i++) {
1090 interface->interface[i].addr = interface->interface[i+1].addr;
1091 interface->interface[i].port = interface->interface[i+1].port;
1093 interface->numberOfInterfaces = number;
1097 * Remove the hash table entry for this address
1099 h_DeleteHostFromHashTableByAddr_r(addr, port, host);
1105 /* Host is returned held */
1107 h_GetHost_r(struct rx_connection *tcon)
1110 struct host *oldHost;
1113 struct interfaceAddr interf;
1114 int interfValid = 0;
1115 struct Identity *identP = NULL;
1118 char hoststr[16], hoststr2[16];
1120 struct rx_connection *cb_conn = NULL;
1122 caps.Capabilities_val = NULL;
1124 haddr = rxr_HostOf(tcon);
1125 hport = rxr_PortOf(tcon);
1127 if (caps.Capabilities_val)
1128 free(caps.Capabilities_val);
1129 caps.Capabilities_val = NULL;
1130 caps.Capabilities_len = 0;
1133 host = h_Lookup_r(haddr, hport, &held);
1134 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1135 if (host && !identP && !(host->Console & 1)) {
1136 /* This is a new connection, and we already have a host
1137 * structure for this address. Verify that the identity
1138 * of the caller matches the identity in the host structure.
1141 if (!(host->hostFlags & ALTADDR)) {
1142 /* Another thread is doing initialization */
1147 ("Host %s:%d starting h_Lookup again\n",
1148 afs_inet_ntoa_r(host->host, hoststr),
1149 ntohs(host->port)));
1152 host->hostFlags &= ~ALTADDR;
1153 cb_conn = host->callback_rxcon;
1154 rx_GetConnection(cb_conn);
1157 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1158 if (code == RXGEN_OPCODE)
1159 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1160 rx_PutConnection(cb_conn);
1163 if (code == RXGEN_OPCODE) {
1164 identP = (struct Identity *)malloc(sizeof(struct Identity));
1166 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1170 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1171 /* The host on this connection was unable to respond to
1172 * the WhoAreYou. We will treat this as a new connection
1173 * from the existing host. The worst that can happen is
1174 * that we maintain some extra callback state information */
1175 if (host->interface) {
1177 ("Host %s:%d used to support WhoAreYou, deleting.\n",
1178 afs_inet_ntoa_r(host->host, hoststr),
1179 ntohs(host->port)));
1180 host->hostFlags |= HOSTDELETED;
1187 } else if (code == 0) {
1189 identP = (struct Identity *)malloc(sizeof(struct Identity));
1191 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1195 identP->uuid = interf.uuid;
1196 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1197 /* Check whether the UUID on this connection matches
1198 * the UUID in the host structure. If they don't match
1199 * then this is not the same host as before. */
1200 if (!host->interface
1201 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1203 ("Host %s:%d has changed its identity, deleting.\n",
1204 afs_inet_ntoa_r(host->host, hoststr), host->port));
1205 host->hostFlags |= HOSTDELETED;
1213 afs_inet_ntoa_r(host->host, hoststr);
1215 ("CB: WhoAreYou failed for %s:%d, error %d\n", hoststr,
1216 ntohs(host->port), code));
1217 host->hostFlags |= VENUSDOWN;
1219 if (caps.Capabilities_val
1220 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1221 host->hostFlags |= HERRORTRANS;
1223 host->hostFlags &= ~(HERRORTRANS);
1224 host->hostFlags |= ALTADDR;
1227 if (!(host->hostFlags & ALTADDR)) {
1228 /* another thread is doing the initialisation */
1230 ("Host %s:%d waiting for host-init to complete\n",
1231 afs_inet_ntoa_r(host->host, hoststr),
1232 ntohs(host->port)));
1238 ("Host %s:%d starting h_Lookup again\n",
1239 afs_inet_ntoa_r(host->host, hoststr),
1240 ntohs(host->port)));
1243 /* We need to check whether the identity in the host structure
1244 * matches the identity on the connection. If they don't match
1245 * then treat this a new host. */
1246 if (!(host->Console & 1)
1247 && ((!identP->valid && host->interface)
1248 || (identP->valid && !host->interface)
1250 && !afs_uuid_equal(&identP->uuid,
1251 &host->interface->uuid)))) {
1252 char uuid1[128], uuid2[128];
1254 afsUUID_to_string(&identP->uuid, uuid1, 127);
1255 if (host->interface)
1256 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1258 ("CB: new identity for host %s:%d, deleting(%x %x %s %s)\n",
1259 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1260 identP->valid, host->interface,
1261 identP->valid ? uuid1 : "",
1262 host->interface ? uuid2 : ""));
1264 /* The host in the cache is not the host for this connection */
1265 host->hostFlags |= HOSTDELETED;
1272 host = h_Alloc_r(tcon); /* returned held and locked */
1273 h_gethostcps_r(host, FT_ApproxTime());
1274 if (!(host->Console & 1)) {
1276 cb_conn = host->callback_rxcon;
1277 rx_GetConnection(cb_conn);
1280 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1281 if (code == RXGEN_OPCODE)
1282 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1283 rx_PutConnection(cb_conn);
1286 if (code == RXGEN_OPCODE) {
1289 (struct Identity *)malloc(sizeof(struct Identity));
1294 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1299 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1301 ("Host %s:%d does not support WhoAreYou.\n",
1302 afs_inet_ntoa_r(host->host, hoststr),
1303 ntohs(host->port)));
1305 } else if (code == 0) {
1308 (struct Identity *)malloc(sizeof(struct Identity));
1313 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1318 identP->uuid = interf.uuid;
1320 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1322 ("WhoAreYou success on %s:%d\n",
1323 afs_inet_ntoa_r(host->host, hoststr),
1324 ntohs(host->port)));
1326 if (code == 0 && !identP->valid) {
1327 cb_conn = host->callback_rxcon;
1328 rx_GetConnection(cb_conn);
1330 code = RXAFSCB_InitCallBackState(cb_conn);
1331 rx_PutConnection(cb_conn);
1334 } else if (code == 0) {
1335 oldHost = h_LookupUuid_r(&identP->uuid);
1339 if (!(oheld = h_Held_r(oldHost)))
1343 if (oldHost->interface) {
1344 afsUUID uuid = oldHost->interface->uuid;
1345 cb_conn = oldHost->callback_rxcon;
1346 rx_GetConnection(cb_conn);
1347 rx_SetConnDeadTime(cb_conn, 2);
1348 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1350 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1352 rx_SetConnDeadTime(cb_conn, 50);
1353 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1354 rx_PutConnection(cb_conn);
1356 if (code && MultiProbeAlternateAddress_r(oldHost)) {
1364 /* The old host is either does not have a Uuid,
1365 * is not responding to Probes,
1366 * or does not have a matching Uuid.
1368 oldHost->hostFlags |= HOSTDELETED;
1369 h_Unlock_r(oldHost);
1370 /* Let the holder be last release */
1372 h_Release_r(oldHost);
1378 /* This is a new address for an existing host. Update
1379 * the list of interfaces for the existing host and
1380 * delete the host structure we just allocated. */
1381 if (oldHost->host != haddr || oldHost->port != hport) {
1383 ("CB: new addr %s:%d for old host %s:%d\n",
1384 afs_inet_ntoa_r(haddr, hoststr),
1386 afs_inet_ntoa_r(oldHost->host, hoststr2),
1387 ntohs(oldHost->port)));
1388 if (oldHost->host == haddr) {
1389 /* We have just been contacted by a client behind a NAT */
1390 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
1393 struct Interface *interface = oldHost->interface;
1394 int number = oldHost->interface->numberOfInterfaces;
1395 for (i = 0, found = 0; i < number; i++) {
1396 if (interface->interface[i].addr == haddr &&
1397 interface->interface[i].port != hport) {
1403 /* We have just been contacted by a client that has been
1404 * seen from behind a NAT and at least one other address.
1406 removeInterfaceAddr_r(oldHost, haddr, interface->interface[i].port);
1409 addInterfaceAddr_r(oldHost, haddr, hport);
1410 oldHost->host = haddr;
1411 oldHost->port = hport;
1413 host->hostFlags |= HOSTDELETED;
1415 /* release host because it was allocated by h_Alloc_r */
1418 /* the new host is held and locked */
1420 /* This really is a new host */
1421 h_AddHostToUuidHashTable_r(&identP->uuid, host);
1422 cb_conn = host->callback_rxcon;
1423 rx_GetConnection(cb_conn);
1426 RXAFSCB_InitCallBackState3(cb_conn,
1428 rx_PutConnection(cb_conn);
1433 ("InitCallBackState3 success on %s:%d\n",
1434 afs_inet_ntoa_r(host->host, hoststr),
1435 ntohs(host->port)));
1436 assert(interfValid == 1);
1437 initInterfaceAddr_r(host, &interf);
1442 afs_inet_ntoa_r(host->host, hoststr);
1444 ("CB: RCallBackConnectBack failed for %s:%d\n",
1445 hoststr, ntohs(host->port)));
1446 host->hostFlags |= VENUSDOWN;
1449 ("CB: RCallBackConnectBack succeeded for %s:%d\n",
1450 hoststr, ntohs(host->port)));
1451 host->hostFlags |= RESETDONE;
1454 if (caps.Capabilities_val
1455 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1456 host->hostFlags |= HERRORTRANS;
1458 host->hostFlags &= ~(HERRORTRANS);
1459 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1462 if (caps.Capabilities_val)
1463 free(caps.Capabilities_val);
1464 caps.Capabilities_val = NULL;
1465 caps.Capabilities_len = 0;
1471 static char localcellname[PR_MAXNAMELEN + 1];
1472 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1473 int num_lrealms = -1;
1479 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1480 if (num_lrealms == -1) {
1482 for (i=0; i<AFS_NUM_LREALMS; i++) {
1483 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1489 ("afs_krb_get_lrealm failed, using %s.\n",
1491 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1497 /* initialize the rest of the local realms to nullstring for debugging */
1498 for (; i<AFS_NUM_LREALMS; i++)
1499 local_realms[i][0] = '\0';
1501 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1502 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1503 #ifdef AFS_PTHREAD_ENV
1504 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1505 #endif /* AFS_PTHREAD_ENV */
1509 MapName_r(char *aname, char *acell, afs_int32 * aval)
1514 afs_int32 anamelen, cnamelen;
1518 anamelen = strlen(aname);
1519 if (anamelen >= PR_MAXNAMELEN)
1520 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1522 lnames.namelist_len = 1;
1523 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1524 lids.idlist_len = 0;
1525 lids.idlist_val = NULL;
1527 cnamelen = strlen(acell);
1529 if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
1531 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1532 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1533 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1535 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1537 *aval = AnonymousID;
1540 foreign = 1; /* attempt cross-cell authentication */
1541 tname = (char *)malloc(PR_MAXNAMELEN);
1543 ViceLog(0, ("Failed malloc in MapName_r\n"));
1546 strcpy(tname, aname);
1547 tname[anamelen] = '@';
1548 strcpy(tname + anamelen + 1, acell);
1549 lnames.namelist_val = (prname *) tname;
1554 code = pr_NameToId(&lnames, &lids);
1557 if (lids.idlist_val) {
1558 *aval = lids.idlist_val[0];
1559 if (*aval == AnonymousID) {
1561 ("MapName: NameToId on %s returns anonymousID\n",
1562 lnames.namelist_val));
1564 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1567 ("MapName: NameToId on '%s' is unknown\n",
1568 lnames.namelist_val));
1574 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1582 /* NOTE: this returns the client with a Write lock and a refCount */
1584 h_ID2Client(afs_int32 vid)
1586 register struct client *client;
1587 register struct host *host;
1590 for (host = hostList; host; host = host->next) {
1591 if (host->hostFlags & HOSTDELETED)
1593 for (client = host->FirstClient; client; client = client->next) {
1594 if (!client->deleted && client->ViceId == vid) {
1597 ObtainWriteLock(&client->lock);
1608 * Called by the server main loop. Returns a h_Held client, which must be
1609 * released later the main loop. Allocates a client if the matching one
1610 * isn't around. The client is returned with its reference count incremented
1611 * by one. The caller must call h_ReleaseClient_r when finished with
1614 * the client->host is returned held. h_ReleaseClient_r does not release
1615 * the hold on client->host.
1618 h_FindClient_r(struct rx_connection *tcon)
1620 register struct client *client;
1621 register struct host *host;
1622 struct client *oldClient;
1627 #if (64-MAXKTCNAMELEN)
1628 ticket name length != 64
1632 char uname[PR_MAXNAMELEN];
1633 char tcell[MAXKTCREALMLEN];
1637 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1640 h_Hold_r(client->host);
1641 if (!client->deleted && client->prfail != 2) {
1642 /* Could add shared lock on client here */
1643 /* note that we don't have to lock entry in this path to
1644 * ensure CPS is initialized, since we don't call rx_SetSpecific
1645 * until initialization is done, and we only get here if
1646 * rx_GetSpecific located the client structure.
1651 ObtainWriteLock(&client->lock); /* released at end */
1655 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1657 ("FindClient: authenticating connection: authClass=%d\n",
1659 if (authClass == 1) {
1660 /* A bcrypt tickets, no longer supported */
1661 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1662 viceid = AnonymousID;
1663 expTime = 0x7fffffff;
1664 } else if (authClass == 2) {
1667 /* kerberos ticket */
1668 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
1669 tname, tinst, tcell, &kvno);
1671 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1672 viceid = AnonymousID;
1673 expTime = 0x7fffffff;
1675 int ilen = strlen(tinst);
1677 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1678 tname, tinst, tcell, expTime, kvno));
1679 strncpy(uname, tname, sizeof(uname));
1681 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1684 strcat(uname, tinst);
1686 /* translate the name to a vice id */
1687 code = MapName_r(uname, tcell, &viceid);
1691 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
1694 viceid = AnonymousID;
1695 expTime = 0x7fffffff;
1699 viceid = AnonymousID; /* unknown security class */
1700 expTime = 0x7fffffff;
1703 if (!client) { /* loop */
1704 host = h_GetHost_r(tcon); /* Returns it h_Held */
1707 /* First try to find the client structure */
1708 for (client = host->FirstClient; client; client = client->next) {
1709 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1710 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1711 if (client->tcon && (client->tcon != tcon)) {
1713 ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
1714 client->ViceId, client->sid, client->tcon,
1717 (struct client *)rx_GetSpecific(client->tcon,
1720 if (oldClient == client)
1721 rx_SetSpecific(client->tcon, rxcon_client_key,
1725 ("Client-conn mismatch: CL1=%x, CN=%x, CL2=%x\n",
1726 client, client->tcon, oldClient));
1728 client->tcon = (struct rx_connection *)0;
1732 ObtainWriteLock(&client->lock);
1738 /* Still no client structure - get one */
1741 /* Retry to find the client structure */
1742 for (client = host->FirstClient; client; client = client->next) {
1743 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1744 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1746 goto retryfirstclient;
1751 ObtainWriteLock(&client->lock);
1752 client->refCount = 1;
1753 client->host = host;
1754 #if FS_STATS_DETAILED
1755 client->InSameNetwork = host->InSameNetwork;
1756 #endif /* FS_STATS_DETAILED */
1757 client->ViceId = viceid;
1758 client->expTime = expTime; /* rx only */
1759 client->authClass = authClass; /* rx only */
1760 client->sid = rxr_CidOf(tcon);
1761 client->VenusEpoch = rxr_GetEpoch(tcon);
1762 client->CPS.prlist_val = NULL;
1763 client->CPS.prlist_len = 0;
1767 client->prfail = fail;
1769 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1770 client->CPS.prlist_len = 0;
1771 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
1772 free(client->CPS.prlist_val);
1773 client->CPS.prlist_val = NULL;
1774 client->ViceId = viceid;
1775 client->expTime = expTime;
1777 if (viceid == ANONYMOUSID) {
1778 client->CPS.prlist_len = AnonCPS.prlist_len;
1779 client->CPS.prlist_val = AnonCPS.prlist_val;
1782 code = pr_GetCPS(viceid, &client->CPS);
1787 ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
1788 code, viceid, afs_inet_ntoa_r(client->host->host,
1790 ntohs(client->host->port)));
1792 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1793 * all protection servers and reevaluates things if no
1794 * sync server or quorum is found we could still end up
1795 * with one of these errors. In such case we would like to
1796 * reevaluate the rpc call to find if there's cps for this
1797 * guy. We treat other errors (except network failures
1798 * ones - i.e. code < 0) as an indication that there is no
1799 * CPS for this host. Ideally we could like to deal this
1800 * problem the other way around (i.e. if code == NOCPS
1801 * ignore else retry next time) but the problem is that
1802 * there're other errors (i.e. EPERM) for which we don't
1803 * want to retry and we don't know the whole code list!
1805 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1809 /* the disabling of system:administrators is so iffy and has so many
1810 * possible failure modes that we will disable it again */
1811 /* Turn off System:Administrator for safety
1812 * if (AL_IsAMember(SystemId, client->CPS) == 0)
1813 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1816 /* Now, tcon may already be set to a rock, since we blocked with no host
1817 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1818 * required). So, before setting the RPC's rock, we should disconnect
1819 * the RPC from the other client structure's rock.
1821 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1822 if (oldClient && oldClient->tcon == tcon) {
1824 if (!oldClient->deleted) {
1825 /* if we didn't create it, it's not ours to put back */
1827 ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n",
1828 client, client->sid, tcon,
1829 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
1830 ntohs(rxr_PortOf(tcon)),
1831 oldClient, oldClient->sid));
1832 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
1833 free(client->CPS.prlist_val);
1834 client->CPS.prlist_val = NULL;
1835 client->CPS.prlist_len = 0;
1837 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
1840 /* We should perhaps check for 0 here */
1842 ReleaseWriteLock(&client->lock);
1847 ObtainWriteLock(&oldClient->lock);
1848 oldClient->refCount++;
1851 oldClient->tcon = (struct rx_connection *)0;
1852 ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
1853 oldClient, oldClient->sid, tcon,
1854 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
1855 ntohs(rxr_PortOf(tcon)),
1856 client, client->sid));
1857 /* rx_SetSpecific will be done immediately below */
1860 /* Avoid chaining in more than once. */
1863 client->next = host->FirstClient;
1864 host->FirstClient = client;
1866 CurrentConnections++; /* increment number of connections */
1868 client->tcon = tcon;
1869 rx_SetSpecific(tcon, rxcon_client_key, client);
1870 ReleaseWriteLock(&client->lock);
1874 } /*h_FindClient_r */
1877 h_ReleaseClient_r(struct client *client)
1879 assert(client->refCount > 0);
1886 * Sigh: this one is used to get the client AGAIN within the individual
1887 * server routines. This does not bother h_Holding the host, since
1888 * this is assumed already have been done by the server main loop.
1889 * It does check tokens, since only the server routines can return the
1890 * VICETOKENDEAD error code
1893 GetClient(struct rx_connection *tcon, struct client **cp)
1895 register struct client *client;
1899 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1900 if (client == NULL || client->tcon == NULL) {
1902 ("GetClient: no client in conn %x (host %x:%d), VBUSYING\n",
1903 tcon, rxr_HostOf(tcon),ntohs(rxr_PortOf(tcon))));
1907 if (rxr_CidOf(client->tcon) != client->sid) {
1909 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1910 client->tcon, rxr_CidOf(client->tcon), client->sid));
1914 if (!(client && client->tcon && rxr_CidOf(client->tcon) == client->sid)) {
1916 ViceLog(0, ("GetClient: no client in conn %x\n", tcon));
1919 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1920 client->tcon, client->tcon ? rxr_CidOf(client->tcon)
1921 : -1, client->sid));
1924 if (client && client->LastCall > client->expTime && client->expTime) {
1927 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
1928 afs_inet_ntoa_r(client->host->host, hoststr),
1929 ntohs(client->host->port), client->expTime));
1931 return VICETOKENDEAD;
1941 PutClient(struct client **cp)
1947 h_ReleaseClient_r(*cp);
1954 /* Client user name for short term use. Note that this is NOT inexpensive */
1956 h_UserName(struct client *client)
1958 static char User[PR_MAXNAMELEN + 1];
1962 lids.idlist_len = 1;
1963 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
1964 if (!lids.idlist_val) {
1965 ViceLog(0, ("Failed malloc in h_UserName\n"));
1968 lnames.namelist_len = 0;
1969 lnames.namelist_val = (prname *) 0;
1970 lids.idlist_val[0] = client->ViceId;
1971 if (pr_IdToName(&lids, &lnames)) {
1972 /* We need to free id we alloced above! */
1973 free(lids.idlist_val);
1974 return "*UNKNOWN USER NAME*";
1976 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
1977 free(lids.idlist_val);
1978 free(lnames.namelist_val);
1988 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1989 CEs, CEBlocks, HTs, HTBlocks));
1995 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
1997 register struct client *client;
2004 if (host->hostFlags & HOSTDELETED) {
2008 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2009 "Host %s:%d down = %d, LastCall %s",
2010 afs_inet_ntoa_r(host->host, hoststr),
2011 ntohs(host->port), (host->hostFlags & VENUSDOWN),
2012 afs_ctime((time_t *) & host->LastCall, tbuffer,
2014 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2015 for (client = host->FirstClient; client; client = client->next) {
2016 if (!client->deleted) {
2018 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2019 " user id=%d, name=%s, sl=%s till %s",
2020 client->ViceId, h_UserName(client),
2022 authClass ? "Authenticated" :
2023 "Not authenticated",
2025 authClass ? afs_ctime((time_t *) & client->
2029 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2031 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2032 " user=%s, no current server connection\n",
2033 h_UserName(client));
2034 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2036 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2037 client->CPS.prlist_len);
2038 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2039 if (client->CPS.prlist_val) {
2040 for (i = 0; i > client->CPS.prlist_len; i++) {
2041 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2042 client->CPS.prlist_val[i]);
2043 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2046 sprintf(tmpStr, "]\n");
2047 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2053 } /*h_PrintClient */
2058 * Print a list of clients, with last security level and token value seen,
2068 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2072 ("Couldn't create client dump file %s\n",
2073 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2076 now = FT_ApproxTime();
2077 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
2078 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2079 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2080 h_Enumerate(h_PrintClient, (char *)file);
2081 STREAM_REALLYCLOSE(file);
2082 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2089 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
2096 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2097 "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
2098 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
2099 host->cblist, CheckLock(&host->lock), host->LastCall,
2100 host->ActiveCall, (host->hostFlags & VENUSDOWN),
2101 host->hostFlags & HOSTDELETED, host->Console,
2102 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2104 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2105 if (host->hcps.prlist_val)
2106 for (i = 0; i < host->hcps.prlist_len; i++) {
2107 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2108 host->hcps.prlist_val[i]);
2109 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2111 sprintf(tmpStr, "] [");
2112 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2113 if (host->interface)
2114 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2116 sprintf(tmpStr, " %s:%d",
2117 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2118 ntohs(host->interface->interface[i].port));
2119 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2121 sprintf(tmpStr, "] holds: ");
2122 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2124 for (i = 0; i < h_maxSlots; i++) {
2125 sprintf(tmpStr, "%04x", host->holds[i]);
2126 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2128 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
2129 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2141 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2147 ("Couldn't create host dump file %s\n",
2148 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2151 now = FT_ApproxTime();
2152 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
2153 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2154 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2155 h_Enumerate(h_DumpHost, (char *)file);
2156 STREAM_REALLYCLOSE(file);
2157 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2161 #ifdef AFS_DEMAND_ATTACH_FS
2164 * host state serialization
2166 static int h_stateFillHeader(struct host_state_header * hdr);
2167 static int h_stateCheckHeader(struct host_state_header * hdr);
2168 static int h_stateAllocMap(struct fs_dump_state * state);
2169 static int h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state);
2170 static int h_stateRestoreHost(struct fs_dump_state * state);
2171 static int h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state);
2172 static int h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state);
2173 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port);
2174 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2175 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2176 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2179 /* this procedure saves all host state to disk for fast startup */
2181 h_stateSave(struct fs_dump_state * state)
2183 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2186 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2188 /* invalidate host state header */
2189 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2191 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2192 sizeof(struct host_state_header))) {
2197 fs_stateIncEOF(state, sizeof(struct host_state_header));
2199 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2204 h_stateFillHeader(state->h_hdr);
2206 /* write the real header to disk */
2207 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2208 sizeof(struct host_state_header));
2215 * host state serialization
2217 * this procedure restores all host state from a disk for fast startup
2220 h_stateRestore(struct fs_dump_state * state)
2224 /* seek to the right position and read in the host state header */
2225 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2226 sizeof(struct host_state_header))) {
2231 /* check the validity of the header */
2232 if (h_stateCheckHeader(state->h_hdr)) {
2237 records = state->h_hdr->records;
2239 if (h_stateAllocMap(state)) {
2244 /* iterate over records restoring host state */
2245 for (i=0; i < records; i++) {
2246 if (h_stateRestoreHost(state) != 0) {
2257 h_stateRestoreIndices(struct fs_dump_state * state)
2259 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
2264 h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state)
2266 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
2267 return H_ENUMERATE_BAIL(held);
2273 h_stateVerify(struct fs_dump_state * state)
2275 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
2280 h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state)
2285 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
2286 return H_ENUMERATE_BAIL(held);
2290 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
2291 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
2292 h->interface->interface[i].port)) {
2296 if (h_stateVerifyUuidHash(state, h)) {
2299 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port)) {
2303 if (cb_stateVerifyHCBList(state, h)) {
2312 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port)
2314 int ret = 0, found = 0;
2315 struct host *host = NULL;
2316 struct h_hashChain *chain;
2317 int index = h_HashIndex(addr);
2321 for (chain = hostHashTable[index]; chain; chain = chain->next) {
2322 host = chain->hostPtr;
2324 afs_inet_ntoa_r(addr, tmp);
2325 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
2329 if ((chain->addr == addr) && (chain->port == port)) {
2331 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry points to different host struct (%d, %d)\n",
2332 h->index, host->index));
2333 state->flags.warnings_generated = 1;
2338 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
2339 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2340 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
2348 afs_inet_ntoa_r(addr, tmp);
2349 if (state->mode == FS_STATE_LOAD_MODE) {
2350 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s not found in hash\n", tmp));
2354 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s not found in hash\n", tmp));
2355 state->flags.warnings_generated = 1;
2364 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
2366 int ret = 0, found = 0;
2367 struct host *host = NULL;
2368 struct h_hashChain *chain;
2369 afsUUID * uuidp = &h->interface->uuid;
2370 int index = h_UuidHashIndex(uuidp);
2374 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
2375 host = chain->hostPtr;
2377 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2378 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
2382 if (host->interface &&
2383 afs_uuid_equal(&host->interface->uuid, uuidp)) {
2385 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
2386 h->index, host->index));
2387 state->flags.warnings_generated = 1;
2392 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
2393 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2394 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
2402 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2403 if (state->mode == FS_STATE_LOAD_MODE) {
2404 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
2408 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
2409 state->flags.warnings_generated = 1;
2417 /* create the host state header structure */
2419 h_stateFillHeader(struct host_state_header * hdr)
2421 hdr->stamp.magic = HOST_STATE_MAGIC;
2422 hdr->stamp.version = HOST_STATE_VERSION;
2425 /* check the contents of the host state header structure */
2427 h_stateCheckHeader(struct host_state_header * hdr)
2431 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
2432 ViceLog(0, ("check_host_state_header: invalid state header\n"));
2435 else if (hdr->stamp.version != HOST_STATE_VERSION) {
2436 ViceLog(0, ("check_host_state_header: unknown version number\n"));
2442 /* allocate the host id mapping table */
2444 h_stateAllocMap(struct fs_dump_state * state)
2446 state->h_map.len = state->h_hdr->index_max + 1;
2447 state->h_map.entries = (struct idx_map_entry_t *)
2448 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
2449 return (state->h_map.entries != NULL) ? 0 : 1;
2452 /* function called by h_Enumerate to save a host to disk */
2454 h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state)
2456 int i, if_len=0, hcps_len=0;
2457 struct hostDiskEntry hdsk;
2458 struct host_state_entry_header hdr;
2459 struct Interface * ifp = NULL;
2460 afs_int32 * hcps = NULL;
2461 struct iovec iov[4];
2464 memset(&hdr, 0, sizeof(hdr));
2466 if (state->h_hdr->index_max < host->index) {
2467 state->h_hdr->index_max = host->index;
2470 h_hostToDiskEntry_r(host, &hdsk);
2471 if (host->interface) {
2472 if_len = sizeof(struct Interface) +
2473 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
2474 ifp = (struct Interface *) malloc(if_len);
2475 assert(ifp != NULL);
2476 memcpy(ifp, host->interface, if_len);
2477 hdr.interfaces = host->interface->numberOfInterfaces;
2478 iov[iovcnt].iov_base = (char *) ifp;
2479 iov[iovcnt].iov_len = if_len;
2482 if (host->hcps.prlist_val) {
2483 hdr.hcps = host->hcps.prlist_len;
2484 hcps_len = hdr.hcps * sizeof(afs_int32);
2485 hcps = (afs_int32 *) malloc(hcps_len);
2486 assert(hcps != NULL);
2487 memcpy(hcps, host->hcps.prlist_val, hcps_len);
2488 iov[iovcnt].iov_base = (char *) hcps;
2489 iov[iovcnt].iov_len = hcps_len;
2493 if (hdsk.index > state->h_hdr->index_max)
2494 state->h_hdr->index_max = hdsk.index;
2496 hdr.len = sizeof(struct host_state_entry_header) +
2497 sizeof(struct hostDiskEntry) + if_len + hcps_len;
2498 hdr.magic = HOST_STATE_ENTRY_MAGIC;
2500 iov[0].iov_base = (char *) &hdr;
2501 iov[0].iov_len = sizeof(hdr);
2502 iov[1].iov_base = (char *) &hdsk;
2503 iov[1].iov_len = sizeof(struct hostDiskEntry);
2505 if (fs_stateWriteV(state, iov, iovcnt)) {
2506 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
2510 fs_stateIncEOF(state, hdr.len);
2512 state->h_hdr->records++;
2520 return H_ENUMERATE_BAIL(held);
2525 /* restores a host from disk */
2527 h_stateRestoreHost(struct fs_dump_state * state)
2529 int ifp_len=0, hcps_len=0, bail=0;
2530 struct host_state_entry_header hdr;
2531 struct hostDiskEntry hdsk;
2532 struct host *host = NULL;
2533 struct Interface *ifp = NULL;
2534 afs_int32 * hcps = NULL;
2535 struct iovec iov[3];
2538 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
2539 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
2545 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
2546 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
2552 iov[0].iov_base = (char *) &hdsk;
2553 iov[0].iov_len = sizeof(struct hostDiskEntry);
2555 if (hdr.interfaces) {
2556 ifp_len = sizeof(struct Interface) +
2557 ((hdr.interfaces-1) * sizeof(struct AddrPort));
2558 ifp = (struct Interface *) malloc(ifp_len);
2559 assert(ifp != NULL);
2560 iov[iovcnt].iov_base = (char *) ifp;
2561 iov[iovcnt].iov_len = ifp_len;
2565 hcps_len = hdr.hcps * sizeof(afs_int32);
2566 hcps = (afs_int32 *) malloc(hcps_len);
2567 assert(hcps != NULL);
2568 iov[iovcnt].iov_base = (char *) hcps;
2569 iov[iovcnt].iov_len = hcps_len;
2573 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
2574 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
2579 if (fs_stateReadV(state, iov, iovcnt)) {
2580 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
2585 if (!hdr.hcps && hdsk.hcps_valid) {
2586 /* valid, zero-length host cps ; does this ever happen? */
2587 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
2588 assert(hcps != NULL);
2592 assert(host != NULL);
2595 host->interface = ifp;
2598 host->hcps.prlist_val = hcps;
2599 host->hcps.prlist_len = hdr.hcps;
2602 h_diskEntryToHost_r(&hdsk, host);
2603 h_SetupCallbackConn_r(host);
2607 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
2608 h_AddHostToHashTable_r(ifp->interface[i].addr,
2609 ifp->interface[i].port, host);
2611 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
2613 h_AddHostToHashTable_r(host->host, host->port, host);
2615 h_InsertList_r(host);
2617 /* setup host id map entry */
2618 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
2619 state->h_map.entries[hdsk.index].new_idx = host->index;
2631 /* serialize a host structure to disk */
2633 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
2635 out->host = in->host;
2636 out->port = in->port;
2637 out->hostFlags = in->hostFlags;
2638 out->Console = in->Console;
2639 out->hcpsfailed = in->hcpsfailed;
2640 out->LastCall = in->LastCall;
2641 out->ActiveCall = in->ActiveCall;
2642 out->cpsCall = in->cpsCall;
2643 out->cblist = in->cblist;
2644 #ifdef FS_STATS_DETAILED
2645 out->InSameNetwork = in->InSameNetwork;
2648 /* special fields we save, but are not memcpy'd back on restore */
2649 out->index = in->index;
2650 out->hcps_len = in->hcps.prlist_len;
2651 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
2654 /* restore a host structure from disk */
2656 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
2658 out->host = in->host;
2659 out->port = in->port;
2660 out->hostFlags = in->hostFlags;
2661 out->Console = in->Console;
2662 out->hcpsfailed = in->hcpsfailed;
2663 out->LastCall = in->LastCall;
2664 out->ActiveCall = in->ActiveCall;
2665 out->cpsCall = in->cpsCall;
2666 out->cblist = in->cblist;
2667 #ifdef FS_STATS_DETAILED
2668 out->InSameNetwork = in->InSameNetwork;
2672 /* index translation routines */
2674 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2678 /* hosts use a zero-based index, so old==0 is valid */
2680 if (old >= state->h_map.len) {
2681 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
2683 } else if (state->h_map.entries[old].old_idx != old) { /* sanity check */
2684 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
2687 *new = state->h_map.entries[old].new_idx;
2693 #endif /* AFS_DEMAND_ATTACH_FS */
2697 * This counts the number of workstations, the number of active workstations,
2698 * and the number of workstations declared "down" (i.e. not heard from
2699 * recently). An active workstation has received a call since the cutoff
2700 * time argument passed.
2703 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
2705 register struct host *host;
2706 register int num = 0, active = 0, del = 0;
2709 for (host = hostList; host; host = host->next) {
2710 if (!(host->hostFlags & HOSTDELETED)) {
2712 if (host->ActiveCall > cutofftime)
2714 if (host->hostFlags & VENUSDOWN)
2726 } /*h_GetWorkStats */
2729 /*------------------------------------------------------------------------
2730 * PRIVATE h_ClassifyAddress
2733 * Given a target IP address and a candidate IP address (both
2734 * in host byte order), classify the candidate into one of three
2735 * buckets in relation to the target by bumping the counters passed
2739 * a_targetAddr : Target address.
2740 * a_candAddr : Candidate address.
2741 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
2742 * addresses are either in the same network
2743 * or the same subnet.
2744 * a_diffSubnetP : ...when the candidate is in a different
2746 * a_diffNetworkP : ...when the candidate is in a different
2753 * The target and candidate addresses are both in host byte
2754 * order, NOT network byte order, when passed in.
2758 *------------------------------------------------------------------------*/
2761 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
2762 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
2763 afs_int32 * a_diffNetworkP)
2764 { /*h_ClassifyAddress */
2766 afs_uint32 targetNet;
2767 afs_uint32 targetSubnet;
2769 afs_uint32 candSubnet;
2772 * Put bad values into the subnet info to start with.
2774 targetSubnet = (afs_uint32) 0;
2775 candSubnet = (afs_uint32) 0;
2778 * Pull out the network and subnetwork numbers from the target
2779 * and candidate addresses. We can short-circuit this whole
2780 * affair if the target and candidate addresses are not of the
2783 if (IN_CLASSA(a_targetAddr)) {
2784 if (!(IN_CLASSA(a_candAddr))) {
2785 (*a_diffNetworkP)++;
2788 targetNet = a_targetAddr & IN_CLASSA_NET;
2789 candNet = a_candAddr & IN_CLASSA_NET;
2790 if (IN_SUBNETA(a_targetAddr))
2791 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
2792 if (IN_SUBNETA(a_candAddr))
2793 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
2794 } else if (IN_CLASSB(a_targetAddr)) {
2795 if (!(IN_CLASSB(a_candAddr))) {
2796 (*a_diffNetworkP)++;
2799 targetNet = a_targetAddr & IN_CLASSB_NET;
2800 candNet = a_candAddr & IN_CLASSB_NET;
2801 if (IN_SUBNETB(a_targetAddr))
2802 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
2803 if (IN_SUBNETB(a_candAddr))
2804 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
2805 } /*Class B target */
2806 else if (IN_CLASSC(a_targetAddr)) {
2807 if (!(IN_CLASSC(a_candAddr))) {
2808 (*a_diffNetworkP)++;
2811 targetNet = a_targetAddr & IN_CLASSC_NET;
2812 candNet = a_candAddr & IN_CLASSC_NET;
2815 * Note that class C addresses can't have subnets,
2816 * so we leave the defaults untouched.
2818 } /*Class C target */
2820 targetNet = a_targetAddr;
2821 candNet = a_candAddr;
2822 } /*Class D address */
2825 * Now, simply compare the extracted net and subnet values for
2826 * the two addresses (which at this point are known to be of the
2829 if (targetNet == candNet) {
2830 if (targetSubnet == candSubnet)
2831 (*a_sameNetOrSubnetP)++;
2835 (*a_diffNetworkP)++;
2837 } /*h_ClassifyAddress */
2840 /*------------------------------------------------------------------------
2841 * EXPORTED h_GetHostNetStats
2844 * Iterate through the host table, and classify each (non-deleted)
2845 * host entry into ``proximity'' categories (same net or subnet,
2846 * different subnet, different network).
2849 * a_numHostsP : Set to total number of (non-deleted) hosts.
2850 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
2851 * a_diffSubnetP : Set to # hosts on diff subnet as server.
2852 * a_diffNetworkP : Set to # hosts on diff network as server.
2858 * We only count non-deleted hosts. The storage pointed to by our
2859 * parameters is zeroed upon entry.
2863 *------------------------------------------------------------------------*/
2866 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
2867 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
2868 { /*h_GetHostNetStats */
2870 register struct host *hostP; /*Ptr to current host entry */
2871 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
2874 * Clear out the storage pointed to by our parameters.
2876 *a_numHostsP = (afs_int32) 0;
2877 *a_sameNetOrSubnetP = (afs_int32) 0;
2878 *a_diffSubnetP = (afs_int32) 0;
2879 *a_diffNetworkP = (afs_int32) 0;
2882 for (hostP = hostList; hostP; hostP = hostP->next) {
2883 if (!(hostP->hostFlags & HOSTDELETED)) {
2885 * Bump the number of undeleted host entries found.
2886 * In classifying the current entry's address, make
2887 * sure to first convert to host byte order.
2890 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
2891 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
2892 a_sameNetOrSubnetP, a_diffSubnetP,
2894 } /*Only look at non-deleted hosts */
2895 } /*For each host record hashed to this index */
2897 } /*h_GetHostNetStats */
2899 static afs_uint32 checktime;
2900 static afs_uint32 clientdeletetime;
2901 static struct AFSFid zerofid;
2905 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
2906 * Since it can serialize them, and pile up, it should be a separate LWP
2907 * from other events.
2910 CheckHost(register struct host *host, int held)
2912 register struct client *client;
2913 struct rx_connection *cb_conn = NULL;
2916 #ifdef AFS_DEMAND_ATTACH_FS
2917 /* kill the checkhost lwp ASAP during shutdown */
2919 if (fs_state.mode == FS_MODE_SHUTDOWN) {
2921 return H_ENUMERATE_BAIL(held);
2926 /* Host is held by h_Enumerate */
2928 for (client = host->FirstClient; client; client = client->next) {
2929 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
2930 client->deleted = 1;
2931 host->hostFlags |= CLIENTDELETED;
2934 if (host->LastCall < checktime) {
2936 if (!(host->hostFlags & HOSTDELETED)) {
2937 cb_conn = host->callback_rxcon;
2938 rx_GetConnection(cb_conn);
2939 if (host->LastCall < clientdeletetime) {
2940 host->hostFlags |= HOSTDELETED;
2941 if (!(host->hostFlags & VENUSDOWN)) {
2942 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
2943 if (host->interface) {
2946 RXAFSCB_InitCallBackState3(cb_conn,
2952 RXAFSCB_InitCallBackState(cb_conn);
2955 host->hostFlags |= ALTADDR; /* alternate addresses valid */
2958 (void)afs_inet_ntoa_r(host->host, hoststr);
2960 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
2961 hoststr, ntohs(host->port)));
2962 host->hostFlags |= VENUSDOWN;
2964 /* Note: it's safe to delete hosts even if they have call
2965 * back state, because break delayed callbacks (called when a
2966 * message is received from the workstation) will always send a
2967 * break all call backs to the workstation if there is no
2972 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
2973 if (host->interface) {
2974 afsUUID uuid = host->interface->uuid;
2976 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2979 if (MultiProbeAlternateAddress_r(host)) {
2981 (void)afs_inet_ntoa_r(host->host, hoststr);
2983 ("ProbeUuid failed for host %s:%d\n",
2984 hoststr, ntohs(host->port)));
2985 host->hostFlags |= VENUSDOWN;
2990 code = RXAFSCB_Probe(cb_conn);
2994 (void)afs_inet_ntoa_r(host->host, hoststr);
2996 ("Probe failed for host %s:%d\n", hoststr,
2997 ntohs(host->port)));
2998 host->hostFlags |= VENUSDOWN;
3004 rx_PutConnection(cb_conn);
3017 * Set VenusDown for any hosts that have not had a call in 15 minutes and
3018 * don't respond to a probe. Note that VenusDown can only be cleared if
3019 * a message is received from the host (see ServerLWP in file.c).
3020 * Delete hosts that have not had any calls in 1 hour, clients that
3021 * have not had any calls in 15 minutes.
3023 * This routine is called roughly every 5 minutes.
3028 afs_uint32 now = FT_ApproxTime();
3030 memset((char *)&zerofid, 0, sizeof(zerofid));
3032 * Send a probe to the workstation if it hasn't been heard from in
3035 checktime = now - 15 * 60;
3036 clientdeletetime = now - 120 * 60; /* 2 hours ago */
3037 h_Enumerate(CheckHost, NULL);
3042 * This is called with host locked and held. At this point, the
3043 * hostHashTable should not have any entries for the alternate
3044 * interfaces. This function has to insert these entries in the
3047 * The addresses in the interfaceAddr list are in host byte order.
3050 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
3057 struct Interface *interface;
3063 ("initInterfaceAddr : host %x numAddr %d\n", host->host,
3064 interf->numberOfInterfaces));
3066 number = interf->numberOfInterfaces;
3067 myAddr = host->host; /* current interface address */
3068 myPort = host->port; /* current port */
3070 /* validation checks */
3071 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
3073 ("Number of alternate addresses returned is %d\n", number));
3078 * Convert IP addresses to network byte order, and remove for
3079 * duplicate IP addresses from the interface list.
3081 for (i = 0, count = 0, found = 0; i < number; i++) {
3082 interf->addr_in[i] = htonl(interf->addr_in[i]);
3083 for (j = 0; j < count; j++) {
3084 if (interf->addr_in[j] == interf->addr_in[i])
3088 interf->addr_in[count] = interf->addr_in[i];
3089 if (interf->addr_in[count] == myAddr)
3096 * Allocate and initialize an interface structure for this host.
3099 interface = (struct Interface *)
3100 malloc(sizeof(struct Interface) +
3101 (sizeof(struct AddrPort) * (count - 1)));
3103 ViceLog(0, ("Failed malloc in initInterfaceAddr_r\n"));
3106 interface->numberOfInterfaces = count;
3108 interface = (struct Interface *)
3109 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
3111 interface->numberOfInterfaces = count + 1;
3112 interface->interface[count].addr = myAddr;
3113 interface->interface[count].port = myPort;
3115 interface->uuid = interf->uuid;
3116 for (i = 0; i < count; i++) {
3117 interface->interface[i].addr = interf->addr_in[i];
3118 /* We store the port as 7001 because the addresses reported by
3119 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
3120 * are coming from fully connected hosts (no NAT/PATs)
3122 interface->interface[i].port = htons(7001);
3125 assert(!host->interface);
3126 host->interface = interface;
3128 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
3130 ViceLog(125, ("--- alt address %s:%d\n",
3131 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3132 ntohs(host->interface->interface[i].port)));
3138 /* deleted a HashChain structure for this address and host */
3139 /* returns 1 on success */
3141 h_DeleteHostFromHashTableByAddr_r(afs_uint32 addr, afs_uint16 port, struct host *host)
3144 register struct h_hashChain **hp, *th;
3146 for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp);) {
3147 assert(th->hostPtr);
3148 if (th->hostPtr == host && th->addr == addr && th->port == port) {
3162 ** prints out all alternate interface address for the host. The 'level'
3163 ** parameter indicates what level of debugging sets this output
3166 printInterfaceAddr(struct host *host, int level)
3171 if (host->interface) {
3172 /* check alternate addresses */
3173 number = host->interface->numberOfInterfaces;
3175 for (i = 0; i < number; i++)
3176 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3177 ntohs(host->interface->interface[i].port)));
3179 ViceLog(level, ("\n"));