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->VenusEpoch = 0;
180 entry->next = CEFree;
187 * The HTs and HTBlocks variables were formerly static, but they are
188 * now referenced elsewhere in the FileServer.
190 int HTs = 0; /* active file entries */
191 int HTBlocks = 0; /* number of blocks of HTs */
192 static struct host *HTFree = 0; /* first free file entry */
195 * Hash tables of host pointers. We need two tables, one
196 * to map IP addresses onto host pointers, and another
197 * to map host UUIDs onto host pointers.
199 static struct h_hashChain *hostHashTable[h_HASHENTRIES];
200 static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
201 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
202 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
204 struct HTBlock { /* block of HTSPERBLOCK file entries */
205 struct host entry[h_HTSPERBLOCK];
209 /* get a new block of HTs and chain it on HTFree */
213 register struct HTBlock *block;
215 static int index = 0;
217 if (HTBlocks == h_MAXHOSTTABLES) {
218 ViceLog(0, ("h_MAXHOSTTABLES reached\n"));
219 ShutDownAndCore(PANIC);
222 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
224 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
225 ShutDownAndCore(PANIC);
227 #ifdef AFS_PTHREAD_ENV
228 for (i = 0; i < (h_HTSPERBLOCK); i++)
229 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
230 #endif /* AFS_PTHREAD_ENV */
231 for (i = 0; i < (h_HTSPERBLOCK); i++)
232 Lock_Init(&block->entry[i].lock);
233 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
234 block->entry[i].next = &(block->entry[i + 1]);
235 for (i = 0; i < (h_HTSPERBLOCK); i++)
236 block->entry[i].index = index++;
237 block->entry[h_HTSPERBLOCK - 1].next = 0;
238 HTFree = (struct host *)block;
239 hosttableptrs[HTBlocks++] = block->entry;
244 /* get the next available HT */
248 register struct host *entry;
252 assert(HTFree != NULL);
254 HTFree = entry->next;
256 memset((char *)entry, 0, HOST_TO_ZERO(entry));
262 /* return an entry to the free list */
264 FreeHT(register struct host *entry)
266 entry->next = HTFree;
273 static short consolePort = 0;
276 h_Release(register struct host *host)
285 * If this thread does not have a hold on this host AND
286 * if other threads also dont have any holds on this host AND
287 * If either the HOSTDELETED or CLIENTDELETED flags are set
291 h_Release_r(register struct host *host)
294 if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
295 if (!h_OtherHolds_r(host)) {
296 /* must avoid masking this until after h_OtherHolds_r runs
297 * but it should be run before h_TossStuff_r */
298 (host)->holds[h_holdSlot()] &= ~h_holdbit();
299 if ((host->hostFlags & HOSTDELETED)
300 || (host->hostFlags & CLIENTDELETED)) {
304 (host)->holds[h_holdSlot()] &= ~h_holdbit();
306 (host)->holds[h_holdSlot()] &= ~h_holdbit();
312 h_OtherHolds_r(register struct host *host)
314 register int i, bit, slot;
317 for (i = 0; i < h_maxSlots; i++) {
318 if (host->holds[i] != ((i == slot) ? bit : 0)) {
326 h_Lock_r(register struct host *host)
336 * returns 1 if already locked
337 * else returns locks and returns 0
341 h_NBLock_r(register struct host *host)
343 struct Lock *hostLock = &host->lock;
348 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
349 hostLock->excl_locked = WRITE_LOCK;
353 LOCK_UNLOCK(hostLock);
362 #if FS_STATS_DETAILED
363 /*------------------------------------------------------------------------
364 * PRIVATE h_AddrInSameNetwork
367 * Given a target IP address and a candidate IP address (both
368 * in host byte order), return a non-zero value (1) if the
369 * candidate address is in a different network from the target
373 * a_targetAddr : Target address.
374 * a_candAddr : Candidate address.
377 * 1 if the candidate address is in the same net as the target,
381 * The target and candidate addresses are both in host byte
382 * order, NOT network byte order, when passed in. We return
383 * our value as a character, since that's the type of field in
384 * the host structure, where this info will be stored.
388 *------------------------------------------------------------------------*/
391 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
392 { /*h_AddrInSameNetwork */
394 afs_uint32 targetNet;
398 * Pull out the network and subnetwork numbers from the target
399 * and candidate addresses. We can short-circuit this whole
400 * affair if the target and candidate addresses are not of the
403 if (IN_CLASSA(a_targetAddr)) {
404 if (!(IN_CLASSA(a_candAddr))) {
407 targetNet = a_targetAddr & IN_CLASSA_NET;
408 candNet = a_candAddr & IN_CLASSA_NET;
409 } else if (IN_CLASSB(a_targetAddr)) {
410 if (!(IN_CLASSB(a_candAddr))) {
413 targetNet = a_targetAddr & IN_CLASSB_NET;
414 candNet = a_candAddr & IN_CLASSB_NET;
415 } /*Class B target */
416 else if (IN_CLASSC(a_targetAddr)) {
417 if (!(IN_CLASSC(a_candAddr))) {
420 targetNet = a_targetAddr & IN_CLASSC_NET;
421 candNet = a_candAddr & IN_CLASSC_NET;
422 } /*Class C target */
424 targetNet = a_targetAddr;
425 candNet = a_candAddr;
426 } /*Class D address */
429 * Now, simply compare the extracted net values for the two addresses
430 * (which at this point are known to be of the same class)
432 if (targetNet == candNet)
437 } /*h_AddrInSameNetwork */
438 #endif /* FS_STATS_DETAILED */
441 /* Assumptions: called with held host */
443 h_gethostcps_r(register struct host *host, register afs_int32 now)
448 /* wait if somebody else is already doing the getCPS call */
449 while (host->hostFlags & HCPS_INPROGRESS) {
450 slept = 1; /* I did sleep */
451 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
452 #ifdef AFS_PTHREAD_ENV
453 pthread_cond_wait(&host->cond, &host_glock_mutex);
454 #else /* AFS_PTHREAD_ENV */
455 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
456 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
457 #endif /* AFS_PTHREAD_ENV */
461 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
462 if (host->hcps.prlist_val)
463 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
464 host->hcps.prlist_val = NULL;
465 host->hcps.prlist_len = 0;
466 host->cpsCall = slept ? (FT_ApproxTime()) : (now);
469 code = pr_GetHostCPS(ntohl(host->host), &host->hcps);
473 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
474 * and reevaluates things if no sync server or quorum is found we could still end up
475 * with one of these errors. In such case we would like to reevaluate the rpc call to
476 * find if there's cps for this guy. We treat other errors (except network failures
477 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
478 * we could like to deal this problem the other way around (i.e. if code == NOCPS
479 * ignore else retry next time) but the problem is that there're other errors (i.e.
480 * EPERM) for which we don't want to retry and we don't know the whole code list!
482 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
484 * We would have preferred to use a while loop and try again since ops in protected
485 * acls for this host will fail now but they'll be reevaluated on any subsequent
486 * call. The attempt to wait for a quorum/sync site or network error won't work
487 * since this problems really should only occurs during a complete fileserver
488 * restart. Since the fileserver will start before the ptservers (and thus before
489 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
491 host->hcpsfailed = 1;
493 ("Warning: GetHostCPS failed (%d) for %x; will retry\n",
496 host->hcpsfailed = 0;
498 ("gethost: GetHostCPS failed (%d) for %x; ignored\n",
501 if (host->hcps.prlist_val)
502 free(host->hcps.prlist_val);
503 host->hcps.prlist_val = NULL;
504 host->hcps.prlist_len = 0; /* Make sure it's zero */
506 host->hcpsfailed = 0;
508 host->hostFlags &= ~HCPS_INPROGRESS;
509 /* signal all who are waiting */
510 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
511 host->hostFlags &= ~HCPS_WAITING;
512 #ifdef AFS_PTHREAD_ENV
513 assert(pthread_cond_broadcast(&host->cond) == 0);
514 #else /* AFS_PTHREAD_ENV */
515 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
516 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
517 #endif /* AFS_PTHREAD_ENV */
521 /* args in net byte order */
523 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint16 hport)
525 register struct host *host;
529 host = h_Lookup_r(hostaddr, hport, &held);
531 host->hcpsfailed = 1;
541 * Allocate a host. It will be identified by the peer (ip,port) info in the
542 * rx connection provided. The host is returned held and locked
544 #define DEF_ROPCONS 2115
547 h_Alloc_r(register struct rx_connection *r_con)
549 struct servent *serverentry;
552 #if FS_STATS_DETAILED
553 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
554 #endif /* FS_STATS_DETAILED */
558 host->host = rxr_HostOf(r_con);
559 host->port = rxr_PortOf(r_con);
561 h_AddHostToHashTable_r(host->host, host->port, host);
563 if (consolePort == 0) { /* find the portal number for console */
564 #if defined(AFS_OSF_ENV)
565 serverentry = getservbyname("ropcons", "");
567 serverentry = getservbyname("ropcons", 0);
570 consolePort = serverentry->s_port;
572 consolePort = htons(DEF_ROPCONS); /* Use a default */
574 if (host->port == consolePort)
576 /* Make a callback channel even for the console, on the off chance that it
577 * makes a request that causes a break call back. It shouldn't. */
578 h_SetupCallbackConn_r(host);
579 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
581 host->hcps.prlist_val = NULL;
582 host->hcps.prlist_len = 0;
583 host->interface = NULL;
585 host->hcpsfailed = 0; /* save cycles */
586 h_gethostcps(host); /* do this under host hold/lock */
588 host->FirstClient = NULL;
591 h_InsertList_r(host); /* update global host List */
592 #if FS_STATS_DETAILED
594 * Compare the new host's IP address (in host byte order) with ours
595 * (the File Server's), remembering if they are in the same network.
597 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
598 host->InSameNetwork =
599 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
600 #endif /* FS_STATS_DETAILED */
607 /* Make a callback channel even for the console, on the off chance that it
608 * makes a request that causes a break call back. It shouldn't. */
610 h_SetupCallbackConn_r(struct host * host)
613 sc = rxnull_NewClientSecurityObject();
614 host->callback_rxcon =
615 rx_NewConnection(host->host, host->port, 1, sc, 0);
616 rx_SetConnDeadTime(host->callback_rxcon, 50);
617 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
620 /* Lookup a host given an IP address and UDP port number. */
621 /* hostaddr and hport are in network order */
622 /* Note: host should be released by caller if 0 == *heldp and non-null */
623 /* hostaddr and hport are in network order */
625 h_Lookup_r(afs_uint32 haddr, afs_uint16 hport, int *heldp)
628 struct host *host = 0;
629 struct h_hashChain *chain;
630 int index = h_HashIndex(haddr);
631 extern int hostaclRefresh;
634 for (chain = hostHashTable[index]; chain; chain = chain->next) {
635 host = chain->hostPtr;
637 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
638 && chain->port == hport) {
639 *heldp = h_Held_r(host);
643 if (host->hostFlags & HOSTDELETED) {
650 now = FT_ApproxTime(); /* always evaluate "now" */
651 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
653 * Every hostaclRefresh period (def 2 hrs) get the new
654 * membership list for the host. Note this could be the
655 * first time that the host is added to a group. Also
656 * here we also retry on previous legitimate hcps failures.
658 * If we get here we still have a host hold.
660 h_gethostcps_r(host, now);
670 /* Lookup a host given its UUID. */
672 h_LookupUuid_r(afsUUID * uuidp)
674 struct host *host = 0;
675 struct h_hashChain *chain;
676 int index = h_UuidHashIndex(uuidp);
678 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
679 host = chain->hostPtr;
681 if (!(host->hostFlags & HOSTDELETED) && host->interface
682 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
693 * h_Hold_r: Establish a hold by the current LWP on this host--the host
694 * or its clients will not be physically deleted until all holds have
696 * NOTE: h_Hold_r is a macro defined in host.h.
699 /* h_TossStuff_r: Toss anything in the host structure (the host or
700 * clients marked for deletion. Called from h_Release_r ONLY.
701 * To be called, there must be no holds, and either host->deleted
702 * or host->clientDeleted must be set.
705 h_TossStuff_r(register struct host *host)
707 register struct client **cp, *client;
710 /* if somebody still has this host held */
711 for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
715 /* if somebody still has this host locked */
716 if (h_NBLock_r(host) != 0) {
719 ("Warning: h_TossStuff_r failed; Host %s:%d was locked.\n",
720 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
726 /* ASSUMPTION: rxi_FreeConnection() does not yield */
727 for (cp = &host->FirstClient; (client = *cp);) {
728 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
730 ObtainWriteLockNoBlock(&client->lock, code);
734 ("Warning: h_TossStuff_r failed: Host %s:%d client %x was locked.\n",
735 afs_inet_ntoa_r(host->host, hoststr),
736 ntohs(host->port), client));
740 if (client->refCount) {
743 ("Warning: h_TossStuff_r failed: Host %s:%d client %x refcount %d.\n",
744 afs_inet_ntoa_r(host->host, hoststr),
745 ntohs(host->port), client, client->refCount));
746 /* This is the same thing we do if the host is locked */
747 ReleaseWriteLock(&client->lock);
750 client->CPS.prlist_len = 0;
751 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
752 free(client->CPS.prlist_val);
753 client->CPS.prlist_val = NULL;
754 CurrentConnections--;
756 ReleaseWriteLock(&client->lock);
762 /* We've just cleaned out all the deleted clients; clear the flag */
763 host->hostFlags &= ~CLIENTDELETED;
765 if (host->hostFlags & HOSTDELETED) {
766 register struct h_hashChain **hp, *th;
767 register struct rx_connection *rxconn;
769 struct AddrPort hostAddrPort;
772 if (host->Console & 1)
774 if ((rxconn = host->callback_rxcon)) {
775 host->callback_rxcon = (struct rx_connection *)0;
776 rx_DestroyConnection(rxconn);
778 if (host->hcps.prlist_val)
779 free(host->hcps.prlist_val);
780 host->hcps.prlist_val = NULL;
781 host->hcps.prlist_len = 0;
782 DeleteAllCallBacks_r(host, 1);
783 host->hostFlags &= ~RESETDONE; /* just to be safe */
785 /* if alternate addresses do not exist */
786 if (!(host->interface)) {
787 for (hp = &hostHashTable[h_HashIndex(host->host)]; (th = *hp);
790 if (th->hostPtr == host) {
792 h_DeleteList_r(host);
799 /* delete all hash entries for the UUID */
800 uuidp = &host->interface->uuid;
801 for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (th = *hp);
804 if (th->hostPtr == host) {
810 /* delete all hash entries for alternate addresses */
811 assert(host->interface->numberOfInterfaces > 0);
812 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
813 hostAddrPort = host->interface->interface[i];
815 for (hp = &hostHashTable[h_HashIndex(hostAddrPort.addr)]; (th = *hp);
818 if (th->hostPtr == host) {
825 free(host->interface);
826 host->interface = NULL;
827 h_DeleteList_r(host); /* remove host from global host List */
829 } /* if alternate address exists */
834 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
835 * system at the start of the enumeration (perhaps more). Hosts may be deleted
836 * (have delete flag set); ditto for clients. (*proc) is always called with
837 * host h_held(). The hold state of the host with respect to this lwp is passed
838 * to (*proc) as the param held. The proc should return 0 if the host should be
839 * released, 1 if it should be held after enumeration.
842 h_Enumerate(int (*proc) (), char *param)
844 register struct host *host, **list;
846 register int i, count;
849 if (hostCount == 0) {
853 list = (struct host **)malloc(hostCount * sizeof(struct host *));
855 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
858 held = (int *)malloc(hostCount * sizeof(int));
860 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
863 for (count = 0, host = hostList; host; host = host->next, count++) {
865 if (!(held[count] = h_Held_r(host)))
868 assert(count == hostCount);
870 for (i = 0; i < count; i++) {
871 held[i] = (*proc) (list[i], held[i], param);
872 if (!H_ENUMERATE_ISSET_HELD(held[i]))
873 h_Release(list[i]); /* this might free up the host */
874 /* bail out of the enumeration early */
875 if (H_ENUMERATE_ISSET_BAIL(held[i]))
882 /* h_Enumerate_r (revised):
883 * Calls (*proc)(host, held, param) for each host in hostList, starting
885 * Hosts may be deleted (have delete flag set); ditto for clients.
886 * (*proc) is always called with
887 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
888 * host with respect to this lwp is passed to (*proc) as the param held.
889 * The proc should return 0 if the host should be released, 1 if it should
890 * be held after enumeration.
893 h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
895 register struct host *host, *next;
896 register int held, nheld;
898 if (hostCount == 0) {
901 if (enumstart && !(held = h_Held_r(enumstart)))
903 for (host = enumstart; host; host = next, held = nheld) {
905 if (next && !(nheld = h_Held_r(next)) && !H_ENUMERATE_ISSET_BAIL(held))
907 held = (*proc) (host, held, param);
908 if (!H_ENUMERATE_ISSET_HELD(held))
909 h_Release_r(host); /* this might free up the host */
910 if (H_ENUMERATE_ISSET_BAIL(held))
915 /* inserts a new HashChain structure corresponding to this UUID */
917 h_AddHostToUuidHashTable_r(struct afsUUID *uuid, struct host *host)
920 struct h_hashChain *chain;
922 /* hash into proper bucket */
923 index = h_UuidHashIndex(uuid);
925 /* insert into beginning of list for this bucket */
926 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
928 ViceLog(0, ("Failed malloc in h_AddHostToUuidHashTable_r\n"));
932 chain->hostPtr = host;
933 chain->next = hostUuidHashTable[index];
934 hostUuidHashTable[index] = chain;
938 /* inserts a new HashChain structure corresponding to this address */
940 h_AddHostToHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host)
943 struct h_hashChain *chain;
945 /* hash into proper bucket */
946 index = h_HashIndex(addr);
948 /* don't add the same entry multiple times */
949 for (chain = hostHashTable[index]; chain; chain = chain->next) {
950 if (chain->hostPtr == host && chain->addr == addr && chain->port == port)
954 /* insert into beginning of list for this bucket */
955 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
957 ViceLog(0, ("Failed malloc in h_AddHostToHashTable_r\n"));
960 chain->hostPtr = host;
961 chain->next = hostHashTable[index];
964 hostHashTable[index] = chain;
968 * This is called with host locked and held. At this point, the
969 * hostHashTable should not be having entries for the alternate
970 * interfaces. This function has to insert these entries in the
973 * All addresses are in network byte order.
976 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
981 struct Interface *interface;
982 char hoststr[16], hoststr2[16];
985 assert(host->interface);
987 ViceLog(125, ("addInterfaceAddr : host %s:%d addr %s:%d\n",
988 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
989 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
992 * Make sure this address is on the list of known addresses
995 number = host->interface->numberOfInterfaces;
996 for (i = 0, found = 0; i < number && !found; i++) {
997 if (host->interface->interface[i].addr == addr &&
998 host->interface->interface[i].port == port)
1002 interface = (struct Interface *)
1003 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
1005 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
1008 interface->numberOfInterfaces = number + 1;
1009 interface->uuid = host->interface->uuid;
1010 for (i = 0; i < number; i++)
1011 interface->interface[i] = host->interface->interface[i];
1012 interface->interface[number].addr = addr;
1013 interface->interface[number].port = port;
1014 free(host->interface);
1015 host->interface = interface;
1019 * Create a hash table entry for this address
1021 h_AddHostToHashTable_r(addr, port, host);
1028 * This is called with host locked and held. At this point, the
1029 * hostHashTable should not be having entries for the alternate
1030 * interfaces. This function has to insert these entries in the
1033 * All addresses are in network byte order.
1036 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1041 struct Interface *interface;
1042 char hoststr[16], hoststr2[16];
1045 assert(host->interface);
1047 ViceLog(125, ("removeInterfaceAddr : host %s:%d addr %s:%d\n",
1048 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1049 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
1052 * Make sure this address is on the list of known addresses
1055 interface = host->interface;
1056 number = host->interface->numberOfInterfaces;
1057 for (i = 0, found = 0; i < number; i++) {
1058 if (interface->interface[i].addr == addr &&
1059 interface->interface[i].port == port) {
1066 for (; i < number; i++) {
1067 interface->interface[i].addr = interface->interface[i+1].addr;
1068 interface->interface[i].port = interface->interface[i+1].port;
1070 interface->numberOfInterfaces = number;
1074 * Remove the hash table entry for this address
1076 h_DeleteHostFromHashTableByAddr_r(addr, port, host);
1082 /* Host is returned held */
1084 h_GetHost_r(struct rx_connection *tcon)
1087 struct host *oldHost;
1090 struct interfaceAddr interf;
1091 int interfValid = 0;
1092 struct Identity *identP = NULL;
1095 char hoststr[16], hoststr2[16];
1097 struct rx_connection *cb_conn = NULL;
1099 caps.Capabilities_val = NULL;
1101 haddr = rxr_HostOf(tcon);
1102 hport = rxr_PortOf(tcon);
1104 if (caps.Capabilities_val)
1105 free(caps.Capabilities_val);
1106 caps.Capabilities_val = NULL;
1107 caps.Capabilities_len = 0;
1110 host = h_Lookup_r(haddr, hport, &held);
1111 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1112 if (host && !identP && !(host->Console & 1)) {
1113 /* This is a new connection, and we already have a host
1114 * structure for this address. Verify that the identity
1115 * of the caller matches the identity in the host structure.
1118 if (!(host->hostFlags & ALTADDR)) {
1119 /* Another thread is doing initialization */
1124 ("Host %s:%d starting h_Lookup again\n",
1125 afs_inet_ntoa_r(host->host, hoststr),
1126 ntohs(host->port)));
1129 host->hostFlags &= ~ALTADDR;
1130 cb_conn = host->callback_rxcon;
1131 rx_GetConnection(cb_conn);
1134 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1135 if (code == RXGEN_OPCODE)
1136 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1137 rx_PutConnection(cb_conn);
1140 if (code == RXGEN_OPCODE) {
1141 identP = (struct Identity *)malloc(sizeof(struct Identity));
1143 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1147 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1148 /* The host on this connection was unable to respond to
1149 * the WhoAreYou. We will treat this as a new connection
1150 * from the existing host. The worst that can happen is
1151 * that we maintain some extra callback state information */
1152 if (host->interface) {
1154 ("Host %s:%d used to support WhoAreYou, deleting.\n",
1155 afs_inet_ntoa_r(host->host, hoststr),
1156 ntohs(host->port)));
1157 host->hostFlags |= HOSTDELETED;
1164 } else if (code == 0) {
1166 identP = (struct Identity *)malloc(sizeof(struct Identity));
1168 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1172 identP->uuid = interf.uuid;
1173 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1174 /* Check whether the UUID on this connection matches
1175 * the UUID in the host structure. If they don't match
1176 * then this is not the same host as before. */
1177 if (!host->interface
1178 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1180 ("Host %s:%d has changed its identity, deleting.\n",
1181 afs_inet_ntoa_r(host->host, hoststr), host->port));
1182 host->hostFlags |= HOSTDELETED;
1190 afs_inet_ntoa_r(host->host, hoststr);
1192 ("CB: WhoAreYou failed for %s:%d, error %d\n", hoststr,
1193 ntohs(host->port), code));
1194 host->hostFlags |= VENUSDOWN;
1196 if (caps.Capabilities_val
1197 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1198 host->hostFlags |= HERRORTRANS;
1200 host->hostFlags &= ~(HERRORTRANS);
1201 host->hostFlags |= ALTADDR;
1204 if (!(host->hostFlags & ALTADDR)) {
1205 /* another thread is doing the initialisation */
1207 ("Host %s:%d waiting for host-init to complete\n",
1208 afs_inet_ntoa_r(host->host, hoststr),
1209 ntohs(host->port)));
1215 ("Host %s:%d starting h_Lookup again\n",
1216 afs_inet_ntoa_r(host->host, hoststr),
1217 ntohs(host->port)));
1220 /* We need to check whether the identity in the host structure
1221 * matches the identity on the connection. If they don't match
1222 * then treat this a new host. */
1223 if (!(host->Console & 1)
1224 && ((!identP->valid && host->interface)
1225 || (identP->valid && !host->interface)
1227 && !afs_uuid_equal(&identP->uuid,
1228 &host->interface->uuid)))) {
1229 char uuid1[128], uuid2[128];
1231 afsUUID_to_string(&identP->uuid, uuid1, 127);
1232 if (host->interface)
1233 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1235 ("CB: new identity for host %s:%d, deleting(%x %x %s %s)\n",
1236 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1237 identP->valid, host->interface,
1238 identP->valid ? uuid1 : "",
1239 host->interface ? uuid2 : ""));
1241 /* The host in the cache is not the host for this connection */
1242 host->hostFlags |= HOSTDELETED;
1249 host = h_Alloc_r(tcon); /* returned held and locked */
1250 h_gethostcps_r(host, FT_ApproxTime());
1251 if (!(host->Console & 1)) {
1253 cb_conn = host->callback_rxcon;
1254 rx_GetConnection(cb_conn);
1257 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1258 if (code == RXGEN_OPCODE)
1259 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1260 rx_PutConnection(cb_conn);
1263 if (code == RXGEN_OPCODE) {
1266 (struct Identity *)malloc(sizeof(struct Identity));
1271 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1276 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1278 ("Host %s:%d does not support WhoAreYou.\n",
1279 afs_inet_ntoa_r(host->host, hoststr),
1280 ntohs(host->port)));
1282 } else if (code == 0) {
1285 (struct Identity *)malloc(sizeof(struct Identity));
1290 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1295 identP->uuid = interf.uuid;
1297 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1299 ("WhoAreYou success on %s:%d\n",
1300 afs_inet_ntoa_r(host->host, hoststr),
1301 ntohs(host->port)));
1303 if (code == 0 && !identP->valid) {
1304 cb_conn = host->callback_rxcon;
1305 rx_GetConnection(cb_conn);
1307 code = RXAFSCB_InitCallBackState(cb_conn);
1308 rx_PutConnection(cb_conn);
1311 } else if (code == 0) {
1312 oldHost = h_LookupUuid_r(&identP->uuid);
1316 if (!(oheld = h_Held_r(oldHost)))
1320 if (oldHost->interface) {
1322 afsUUID uuid = oldHost->interface->uuid;
1323 cb_conn = oldHost->callback_rxcon;
1324 rx_GetConnection(cb_conn);
1325 rx_SetConnDeadTime(cb_conn, 2);
1326 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1328 code2 = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1330 rx_SetConnDeadTime(cb_conn, 50);
1331 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1332 rx_PutConnection(cb_conn);
1335 /* The primary address is either not responding or
1336 * is not the client we are looking for.
1337 * MultiProbeAlternateAddress_r() will remove the
1338 * alternate interfaces that do not have the same
1340 ViceLog(0,("CB: ProbeUuid for %s:%d failed %d\n",
1341 afs_inet_ntoa_r(oldHost->host, hoststr),
1342 ntohs(oldHost->port),code2));
1343 MultiProbeAlternateAddress_r(oldHost);
1350 /* This is a new address for an existing host. Update
1351 * the list of interfaces for the existing host and
1352 * delete the host structure we just allocated. */
1353 if (oldHost->host != haddr || oldHost->port != hport) {
1354 struct rx_connection *rxconn;
1357 ("CB: new addr %s:%d for old host %s:%d\n",
1358 afs_inet_ntoa_r(haddr, hoststr),
1360 afs_inet_ntoa_r(oldHost->host, hoststr2),
1361 ntohs(oldHost->port)));
1362 if (probefail || oldHost->host == haddr) {
1363 /* The probe failed which means that the old address is
1364 * either unreachable or is not the same host we were just
1365 * contacted by. We will also remove addresses if only
1366 * the port has changed because that indicates the client
1369 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
1372 struct Interface *interface = oldHost->interface;
1373 int number = oldHost->interface->numberOfInterfaces;
1374 for (i = 0, found = 0; i < number; i++) {
1375 if (interface->interface[i].addr == haddr &&
1376 interface->interface[i].port != hport) {
1382 /* We have just been contacted by a client that has been
1383 * seen from behind a NAT and at least one other address.
1385 removeInterfaceAddr_r(oldHost, haddr, interface->interface[i].port);
1388 addInterfaceAddr_r(oldHost, haddr, hport);
1389 oldHost->host = haddr;
1390 oldHost->port = hport;
1391 rxconn = oldHost->callback_rxcon;
1392 oldHost->callback_rxcon = host->callback_rxcon;
1393 host->callback_rxcon = NULL;
1396 struct client *client;
1398 * If rx_DestroyConnection calls h_FreeConnection we will
1399 * deadlock on the host_glock_mutex. Work around the problem
1400 * by unhooking the client from the connection before
1401 * destroying the connection.
1403 client = rx_GetSpecific(rxconn, rxcon_client_key);
1404 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
1405 rx_DestroyConnection(rxconn);
1408 host->hostFlags |= HOSTDELETED;
1410 /* release host because it was allocated by h_Alloc_r */
1413 /* the new host is held and locked */
1415 /* This really is a new host */
1416 h_AddHostToUuidHashTable_r(&identP->uuid, host);
1417 cb_conn = host->callback_rxcon;
1418 rx_GetConnection(cb_conn);
1421 RXAFSCB_InitCallBackState3(cb_conn,
1423 rx_PutConnection(cb_conn);
1428 ("InitCallBackState3 success on %s:%d\n",
1429 afs_inet_ntoa_r(host->host, hoststr),
1430 ntohs(host->port)));
1431 assert(interfValid == 1);
1432 initInterfaceAddr_r(host, &interf);
1437 afs_inet_ntoa_r(host->host, hoststr);
1439 ("CB: RCallBackConnectBack failed for %s:%d\n",
1440 hoststr, ntohs(host->port)));
1441 host->hostFlags |= VENUSDOWN;
1444 ("CB: RCallBackConnectBack succeeded for %s:%d\n",
1445 hoststr, ntohs(host->port)));
1446 host->hostFlags |= RESETDONE;
1449 if (caps.Capabilities_val
1450 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1451 host->hostFlags |= HERRORTRANS;
1453 host->hostFlags &= ~(HERRORTRANS);
1454 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1457 if (caps.Capabilities_val)
1458 free(caps.Capabilities_val);
1459 caps.Capabilities_val = NULL;
1460 caps.Capabilities_len = 0;
1466 static char localcellname[PR_MAXNAMELEN + 1];
1467 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1468 int num_lrealms = -1;
1474 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1475 if (num_lrealms == -1) {
1477 for (i=0; i<AFS_NUM_LREALMS; i++) {
1478 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1484 ("afs_krb_get_lrealm failed, using %s.\n",
1486 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1492 /* initialize the rest of the local realms to nullstring for debugging */
1493 for (; i<AFS_NUM_LREALMS; i++)
1494 local_realms[i][0] = '\0';
1496 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1497 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1498 #ifdef AFS_PTHREAD_ENV
1499 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1500 #endif /* AFS_PTHREAD_ENV */
1504 MapName_r(char *aname, char *acell, afs_int32 * aval)
1509 afs_int32 anamelen, cnamelen;
1513 anamelen = strlen(aname);
1514 if (anamelen >= PR_MAXNAMELEN)
1515 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1517 lnames.namelist_len = 1;
1518 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1519 lids.idlist_len = 0;
1520 lids.idlist_val = NULL;
1522 cnamelen = strlen(acell);
1524 if (afs_is_foreign_ticket_name(aname, NULL, acell, localcellname)) {
1526 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1527 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1528 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1530 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1532 *aval = AnonymousID;
1535 foreign = 1; /* attempt cross-cell authentication */
1536 tname = (char *)malloc(PR_MAXNAMELEN);
1538 ViceLog(0, ("Failed malloc in MapName_r\n"));
1541 strcpy(tname, aname);
1542 tname[anamelen] = '@';
1543 strcpy(tname + anamelen + 1, acell);
1544 lnames.namelist_val = (prname *) tname;
1549 code = pr_NameToId(&lnames, &lids);
1552 if (lids.idlist_val) {
1553 *aval = lids.idlist_val[0];
1554 if (*aval == AnonymousID) {
1556 ("MapName: NameToId on %s returns anonymousID\n",
1557 lnames.namelist_val));
1559 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1562 ("MapName: NameToId on '%s' is unknown\n",
1563 lnames.namelist_val));
1569 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1577 /* NOTE: this returns the client with a Write lock and a refCount */
1579 h_ID2Client(afs_int32 vid)
1581 register struct client *client;
1582 register struct host *host;
1585 for (host = hostList; host; host = host->next) {
1586 if (host->hostFlags & HOSTDELETED)
1588 for (client = host->FirstClient; client; client = client->next) {
1589 if (!client->deleted && client->ViceId == vid) {
1592 ObtainWriteLock(&client->lock);
1603 * Called by the server main loop. Returns a h_Held client, which must be
1604 * released later the main loop. Allocates a client if the matching one
1605 * isn't around. The client is returned with its reference count incremented
1606 * by one. The caller must call h_ReleaseClient_r when finished with
1609 * the client->host is returned held. h_ReleaseClient_r does not release
1610 * the hold on client->host.
1613 h_FindClient_r(struct rx_connection *tcon)
1615 register struct client *client;
1616 register struct host *host;
1617 struct client *oldClient;
1622 #if (64-MAXKTCNAMELEN)
1623 ticket name length != 64
1627 char uname[PR_MAXNAMELEN];
1628 char tcell[MAXKTCREALMLEN];
1632 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1633 if (client && client->sid == rxr_CidOf(tcon)
1634 && client->VenusEpoch == rxr_GetEpoch(tcon)) {
1636 h_Hold_r(client->host);
1637 if (!client->deleted && client->prfail != 2) {
1638 /* Could add shared lock on client here */
1639 /* note that we don't have to lock entry in this path to
1640 * ensure CPS is initialized, since we don't call rx_SetSpecific
1641 * until initialization is done, and we only get here if
1642 * rx_GetSpecific located the client structure.
1647 ObtainWriteLock(&client->lock); /* released at end */
1651 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1653 ("FindClient: authenticating connection: authClass=%d\n",
1655 if (authClass == 1) {
1656 /* A bcrypt tickets, no longer supported */
1657 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1658 viceid = AnonymousID;
1659 expTime = 0x7fffffff;
1660 } else if (authClass == 2) {
1663 /* kerberos ticket */
1664 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
1665 tname, tinst, tcell, &kvno);
1667 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1668 viceid = AnonymousID;
1669 expTime = 0x7fffffff;
1671 int ilen = strlen(tinst);
1673 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1674 tname, tinst, tcell, expTime, kvno));
1675 strncpy(uname, tname, sizeof(uname));
1677 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1680 strcat(uname, tinst);
1682 /* translate the name to a vice id */
1683 code = MapName_r(uname, tcell, &viceid);
1687 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
1690 viceid = AnonymousID;
1691 expTime = 0x7fffffff;
1695 viceid = AnonymousID; /* unknown security class */
1696 expTime = 0x7fffffff;
1699 if (!client) { /* loop */
1700 host = h_GetHost_r(tcon); /* Returns it h_Held */
1703 /* First try to find the client structure */
1704 for (client = host->FirstClient; client; client = client->next) {
1705 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1706 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1709 ObtainWriteLock(&client->lock);
1715 /* Still no client structure - get one */
1718 /* Retry to find the client structure */
1719 for (client = host->FirstClient; client; client = client->next) {
1720 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1721 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1723 goto retryfirstclient;
1728 ObtainWriteLock(&client->lock);
1729 client->refCount = 1;
1730 client->host = host;
1731 #if FS_STATS_DETAILED
1732 client->InSameNetwork = host->InSameNetwork;
1733 #endif /* FS_STATS_DETAILED */
1734 client->ViceId = viceid;
1735 client->expTime = expTime; /* rx only */
1736 client->authClass = authClass; /* rx only */
1737 client->sid = rxr_CidOf(tcon);
1738 client->VenusEpoch = rxr_GetEpoch(tcon);
1739 client->CPS.prlist_val = NULL;
1740 client->CPS.prlist_len = 0;
1744 client->prfail = fail;
1746 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1747 client->CPS.prlist_len = 0;
1748 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID))
1749 free(client->CPS.prlist_val);
1750 client->CPS.prlist_val = NULL;
1751 client->ViceId = viceid;
1752 client->expTime = expTime;
1754 if (viceid == ANONYMOUSID) {
1755 client->CPS.prlist_len = AnonCPS.prlist_len;
1756 client->CPS.prlist_val = AnonCPS.prlist_val;
1759 code = pr_GetCPS(viceid, &client->CPS);
1764 ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
1765 code, viceid, afs_inet_ntoa_r(client->host->host,
1767 ntohs(client->host->port)));
1769 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1770 * all protection servers and reevaluates things if no
1771 * sync server or quorum is found we could still end up
1772 * with one of these errors. In such case we would like to
1773 * reevaluate the rpc call to find if there's cps for this
1774 * guy. We treat other errors (except network failures
1775 * ones - i.e. code < 0) as an indication that there is no
1776 * CPS for this host. Ideally we could like to deal this
1777 * problem the other way around (i.e. if code == NOCPS
1778 * ignore else retry next time) but the problem is that
1779 * there're other errors (i.e. EPERM) for which we don't
1780 * want to retry and we don't know the whole code list!
1782 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1786 /* the disabling of system:administrators is so iffy and has so many
1787 * possible failure modes that we will disable it again */
1788 /* Turn off System:Administrator for safety
1789 * if (AL_IsAMember(SystemId, client->CPS) == 0)
1790 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1793 /* Now, tcon may already be set to a rock, since we blocked with no host
1794 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1795 * required). So, before setting the RPC's rock, we should disconnect
1796 * the RPC from the other client structure's rock.
1798 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1799 if (oldClient && oldClient->sid == rxr_CidOf(tcon)
1800 && oldClient->VenusEpoch == rxr_GetEpoch(tcon)) {
1802 if (!oldClient->deleted) {
1803 /* if we didn't create it, it's not ours to put back */
1805 ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n",
1806 client, client->sid, tcon,
1807 rxr_AddrStringOf(tcon),
1808 ntohs(rxr_PortOf(tcon)),
1809 oldClient, oldClient->sid));
1810 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val)
1811 free(client->CPS.prlist_val);
1812 client->CPS.prlist_val = NULL;
1813 client->CPS.prlist_len = 0;
1815 /* We should perhaps check for 0 here */
1817 ReleaseWriteLock(&client->lock);
1822 ObtainWriteLock(&oldClient->lock);
1823 oldClient->refCount++;
1826 ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
1827 oldClient, oldClient->sid, tcon,
1828 rxr_AddrStringOf(tcon),
1829 ntohs(rxr_PortOf(tcon)),
1830 client, client->sid));
1831 /* rx_SetSpecific will be done immediately below */
1834 /* Avoid chaining in more than once. */
1837 client->next = host->FirstClient;
1838 host->FirstClient = client;
1840 CurrentConnections++; /* increment number of connections */
1842 rx_SetSpecific(tcon, rxcon_client_key, client);
1843 ReleaseWriteLock(&client->lock);
1847 } /*h_FindClient_r */
1850 h_ReleaseClient_r(struct client *client)
1852 assert(client->refCount > 0);
1859 * Sigh: this one is used to get the client AGAIN within the individual
1860 * server routines. This does not bother h_Holding the host, since
1861 * this is assumed already have been done by the server main loop.
1862 * It does check tokens, since only the server routines can return the
1863 * VICETOKENDEAD error code
1866 GetClient(struct rx_connection *tcon, struct client **cp)
1868 register struct client *client;
1872 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1873 if (client == NULL) {
1875 ("GetClient: no client in conn %x (host %s:%d), VBUSYING\n",
1876 tcon, rxr_AddrStringOf(tcon),ntohs(rxr_PortOf(tcon))));
1880 if (rxr_CidOf(tcon) != client->sid || rxr_GetEpoch(tcon) != client->VenusEpoch) {
1882 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1883 tcon, rxr_CidOf(tcon), client->sid));
1887 if (client && client->LastCall > client->expTime && client->expTime) {
1890 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
1891 afs_inet_ntoa_r(client->host->host, hoststr),
1892 ntohs(client->host->port), client->expTime));
1894 return VICETOKENDEAD;
1904 PutClient(struct client **cp)
1910 h_ReleaseClient_r(*cp);
1917 /* Client user name for short term use. Note that this is NOT inexpensive */
1919 h_UserName(struct client *client)
1921 static char User[PR_MAXNAMELEN + 1];
1925 lids.idlist_len = 1;
1926 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
1927 if (!lids.idlist_val) {
1928 ViceLog(0, ("Failed malloc in h_UserName\n"));
1931 lnames.namelist_len = 0;
1932 lnames.namelist_val = (prname *) 0;
1933 lids.idlist_val[0] = client->ViceId;
1934 if (pr_IdToName(&lids, &lnames)) {
1935 /* We need to free id we alloced above! */
1936 free(lids.idlist_val);
1937 return "*UNKNOWN USER NAME*";
1939 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
1940 free(lids.idlist_val);
1941 free(lnames.namelist_val);
1951 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1952 CEs, CEBlocks, HTs, HTBlocks));
1958 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
1960 register struct client *client;
1965 time_t LastCall, expTime;
1968 LastCall = host->LastCall;
1969 if (host->hostFlags & HOSTDELETED) {
1973 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1974 "Host %s:%d down = %d, LastCall %s",
1975 afs_inet_ntoa_r(host->host, hoststr),
1976 ntohs(host->port), (host->hostFlags & VENUSDOWN),
1977 afs_ctime(&LastCall, tbuffer,
1979 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1980 for (client = host->FirstClient; client; client = client->next) {
1981 if (!client->deleted) {
1982 expTime = client->expTime;
1983 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1984 " user id=%d, name=%s, sl=%s till %s",
1985 client->ViceId, h_UserName(client),
1987 authClass ? "Authenticated" :
1988 "Not authenticated",
1990 authClass ? afs_ctime(&expTime, tbuffer,
1993 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1994 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
1995 client->CPS.prlist_len);
1996 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1997 if (client->CPS.prlist_val) {
1998 for (i = 0; i > client->CPS.prlist_len; i++) {
1999 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2000 client->CPS.prlist_val[i]);
2001 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2004 sprintf(tmpStr, "]\n");
2005 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2011 } /*h_PrintClient */
2016 * Print a list of clients, with last security level and token value seen,
2026 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2030 ("Couldn't create client dump file %s\n",
2031 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2034 now = FT_ApproxTime();
2035 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
2036 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2037 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2038 h_Enumerate(h_PrintClient, (char *)file);
2039 STREAM_REALLYCLOSE(file);
2040 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2047 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
2054 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2055 "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 [",
2056 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
2057 host->cblist, CheckLock(&host->lock), host->LastCall,
2058 host->ActiveCall, (host->hostFlags & VENUSDOWN),
2059 host->hostFlags & HOSTDELETED, host->Console,
2060 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2062 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2063 if (host->hcps.prlist_val)
2064 for (i = 0; i < host->hcps.prlist_len; i++) {
2065 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2066 host->hcps.prlist_val[i]);
2067 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2069 sprintf(tmpStr, "] [");
2070 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2071 if (host->interface)
2072 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2074 sprintf(tmpStr, " %s:%d",
2075 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2076 ntohs(host->interface->interface[i].port));
2077 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2079 sprintf(tmpStr, "] holds: ");
2080 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2082 for (i = 0; i < h_maxSlots; i++) {
2083 sprintf(tmpStr, "%04x", host->holds[i]);
2084 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2086 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
2087 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2099 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2105 ("Couldn't create host dump file %s\n",
2106 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2109 now = FT_ApproxTime();
2110 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
2111 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2112 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2113 h_Enumerate(h_DumpHost, (char *)file);
2114 STREAM_REALLYCLOSE(file);
2115 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2119 #ifdef AFS_DEMAND_ATTACH_FS
2122 * host state serialization
2124 static int h_stateFillHeader(struct host_state_header * hdr);
2125 static int h_stateCheckHeader(struct host_state_header * hdr);
2126 static int h_stateAllocMap(struct fs_dump_state * state);
2127 static int h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state);
2128 static int h_stateRestoreHost(struct fs_dump_state * state);
2129 static int h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state);
2130 static int h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state);
2131 static int h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port);
2132 static int h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h);
2133 static void h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out);
2134 static void h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out);
2137 /* this procedure saves all host state to disk for fast startup */
2139 h_stateSave(struct fs_dump_state * state)
2141 AssignInt64(state->eof_offset, &state->hdr->h_offset);
2144 ViceLog(0, ("h_stateSave: hostCount=%d\n", hostCount));
2146 /* invalidate host state header */
2147 memset(state->h_hdr, 0, sizeof(struct host_state_header));
2149 if (fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2150 sizeof(struct host_state_header))) {
2155 fs_stateIncEOF(state, sizeof(struct host_state_header));
2157 h_Enumerate_r(h_stateSaveHost, hostList, (char *)state);
2162 h_stateFillHeader(state->h_hdr);
2164 /* write the real header to disk */
2165 state->bail = fs_stateWriteHeader(state, &state->hdr->h_offset, state->h_hdr,
2166 sizeof(struct host_state_header));
2173 * host state serialization
2175 * this procedure restores all host state from a disk for fast startup
2178 h_stateRestore(struct fs_dump_state * state)
2182 /* seek to the right position and read in the host state header */
2183 if (fs_stateReadHeader(state, &state->hdr->h_offset, state->h_hdr,
2184 sizeof(struct host_state_header))) {
2189 /* check the validity of the header */
2190 if (h_stateCheckHeader(state->h_hdr)) {
2195 records = state->h_hdr->records;
2197 if (h_stateAllocMap(state)) {
2202 /* iterate over records restoring host state */
2203 for (i=0; i < records; i++) {
2204 if (h_stateRestoreHost(state) != 0) {
2215 h_stateRestoreIndices(struct fs_dump_state * state)
2217 h_Enumerate_r(h_stateRestoreIndex, hostList, (char *)state);
2222 h_stateRestoreIndex(struct host * h, int held, struct fs_dump_state * state)
2224 if (cb_OldToNew(state, h->cblist, &h->cblist)) {
2225 return H_ENUMERATE_BAIL(held);
2231 h_stateVerify(struct fs_dump_state * state)
2233 h_Enumerate_r(h_stateVerifyHost, hostList, (char *)state);
2238 h_stateVerifyHost(struct host * h, int held, struct fs_dump_state * state)
2243 ViceLog(0, ("h_stateVerifyHost: error: NULL host pointer in linked list\n"));
2244 return H_ENUMERATE_BAIL(held);
2248 for (i = h->interface->numberOfInterfaces-1; i >= 0; i--) {
2249 if (h_stateVerifyAddrHash(state, h, h->interface->interface[i].addr,
2250 h->interface->interface[i].port)) {
2254 if (h_stateVerifyUuidHash(state, h)) {
2257 } else if (h_stateVerifyAddrHash(state, h, h->host, h->port)) {
2261 if (cb_stateVerifyHCBList(state, h)) {
2270 h_stateVerifyAddrHash(struct fs_dump_state * state, struct host * h, afs_uint32 addr, afs_uint16 port)
2272 int ret = 0, found = 0;
2273 struct host *host = NULL;
2274 struct h_hashChain *chain;
2275 int index = h_HashIndex(addr);
2279 for (chain = hostHashTable[index]; chain; chain = chain->next) {
2280 host = chain->hostPtr;
2282 afs_inet_ntoa_r(addr, tmp);
2283 ViceLog(0, ("h_stateVerifyAddrHash: error: addr hash chain has NULL host ptr (lookup addr %s)\n", tmp));
2287 if ((chain->addr == addr) && (chain->port == port)) {
2289 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr hash entry points to different host struct (%d, %d)\n",
2290 h->index, host->index));
2291 state->flags.warnings_generated = 1;
2296 if (chain_len > FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN) {
2297 ViceLog(0, ("h_stateVerifyAddrHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2298 FS_STATE_H_MAX_ADDR_HASH_CHAIN_LEN));
2306 afs_inet_ntoa_r(addr, tmp);
2307 if (state->mode == FS_STATE_LOAD_MODE) {
2308 ViceLog(0, ("h_stateVerifyAddrHash: error: addr %s not found in hash\n", tmp));
2312 ViceLog(0, ("h_stateVerifyAddrHash: warning: addr %s not found in hash\n", tmp));
2313 state->flags.warnings_generated = 1;
2322 h_stateVerifyUuidHash(struct fs_dump_state * state, struct host * h)
2324 int ret = 0, found = 0;
2325 struct host *host = NULL;
2326 struct h_hashChain *chain;
2327 afsUUID * uuidp = &h->interface->uuid;
2328 int index = h_UuidHashIndex(uuidp);
2332 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
2333 host = chain->hostPtr;
2335 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2336 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid hash chain has NULL host ptr (lookup uuid %s)\n", tmp));
2340 if (host->interface &&
2341 afs_uuid_equal(&host->interface->uuid, uuidp)) {
2343 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid hash entry points to different host struct (%d, %d)\n",
2344 h->index, host->index));
2345 state->flags.warnings_generated = 1;
2350 if (chain_len > FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN) {
2351 ViceLog(0, ("h_stateVerifyUuidHash: error: hash chain length exceeds %d; assuming there's a loop\n",
2352 FS_STATE_H_MAX_UUID_HASH_CHAIN_LEN));
2360 afsUUID_to_string(uuidp, tmp, sizeof(tmp));
2361 if (state->mode == FS_STATE_LOAD_MODE) {
2362 ViceLog(0, ("h_stateVerifyUuidHash: error: uuid %s not found in hash\n", tmp));
2366 ViceLog(0, ("h_stateVerifyUuidHash: warning: uuid %s not found in hash\n", tmp));
2367 state->flags.warnings_generated = 1;
2375 /* create the host state header structure */
2377 h_stateFillHeader(struct host_state_header * hdr)
2379 hdr->stamp.magic = HOST_STATE_MAGIC;
2380 hdr->stamp.version = HOST_STATE_VERSION;
2383 /* check the contents of the host state header structure */
2385 h_stateCheckHeader(struct host_state_header * hdr)
2389 if (hdr->stamp.magic != HOST_STATE_MAGIC) {
2390 ViceLog(0, ("check_host_state_header: invalid state header\n"));
2393 else if (hdr->stamp.version != HOST_STATE_VERSION) {
2394 ViceLog(0, ("check_host_state_header: unknown version number\n"));
2400 /* allocate the host id mapping table */
2402 h_stateAllocMap(struct fs_dump_state * state)
2404 state->h_map.len = state->h_hdr->index_max + 1;
2405 state->h_map.entries = (struct idx_map_entry_t *)
2406 calloc(state->h_map.len, sizeof(struct idx_map_entry_t));
2407 return (state->h_map.entries != NULL) ? 0 : 1;
2410 /* function called by h_Enumerate to save a host to disk */
2412 h_stateSaveHost(register struct host * host, int held, struct fs_dump_state * state)
2414 int i, if_len=0, hcps_len=0;
2415 struct hostDiskEntry hdsk;
2416 struct host_state_entry_header hdr;
2417 struct Interface * ifp = NULL;
2418 afs_int32 * hcps = NULL;
2419 struct iovec iov[4];
2422 memset(&hdr, 0, sizeof(hdr));
2424 if (state->h_hdr->index_max < host->index) {
2425 state->h_hdr->index_max = host->index;
2428 h_hostToDiskEntry_r(host, &hdsk);
2429 if (host->interface) {
2430 if_len = sizeof(struct Interface) +
2431 ((host->interface->numberOfInterfaces-1) * sizeof(struct AddrPort));
2432 ifp = (struct Interface *) malloc(if_len);
2433 assert(ifp != NULL);
2434 memcpy(ifp, host->interface, if_len);
2435 hdr.interfaces = host->interface->numberOfInterfaces;
2436 iov[iovcnt].iov_base = (char *) ifp;
2437 iov[iovcnt].iov_len = if_len;
2440 if (host->hcps.prlist_val) {
2441 hdr.hcps = host->hcps.prlist_len;
2442 hcps_len = hdr.hcps * sizeof(afs_int32);
2443 hcps = (afs_int32 *) malloc(hcps_len);
2444 assert(hcps != NULL);
2445 memcpy(hcps, host->hcps.prlist_val, hcps_len);
2446 iov[iovcnt].iov_base = (char *) hcps;
2447 iov[iovcnt].iov_len = hcps_len;
2451 if (hdsk.index > state->h_hdr->index_max)
2452 state->h_hdr->index_max = hdsk.index;
2454 hdr.len = sizeof(struct host_state_entry_header) +
2455 sizeof(struct hostDiskEntry) + if_len + hcps_len;
2456 hdr.magic = HOST_STATE_ENTRY_MAGIC;
2458 iov[0].iov_base = (char *) &hdr;
2459 iov[0].iov_len = sizeof(hdr);
2460 iov[1].iov_base = (char *) &hdsk;
2461 iov[1].iov_len = sizeof(struct hostDiskEntry);
2463 if (fs_stateWriteV(state, iov, iovcnt)) {
2464 ViceLog(0, ("h_stateSaveHost: failed to save host %d", host->index));
2468 fs_stateIncEOF(state, hdr.len);
2470 state->h_hdr->records++;
2478 return H_ENUMERATE_BAIL(held);
2483 /* restores a host from disk */
2485 h_stateRestoreHost(struct fs_dump_state * state)
2487 int ifp_len=0, hcps_len=0, bail=0;
2488 struct host_state_entry_header hdr;
2489 struct hostDiskEntry hdsk;
2490 struct host *host = NULL;
2491 struct Interface *ifp = NULL;
2492 afs_int32 * hcps = NULL;
2493 struct iovec iov[3];
2496 if (fs_stateRead(state, &hdr, sizeof(hdr))) {
2497 ViceLog(0, ("h_stateRestoreHost: failed to read host entry header from dump file '%s'\n",
2503 if (hdr.magic != HOST_STATE_ENTRY_MAGIC) {
2504 ViceLog(0, ("h_stateRestoreHost: fileserver state dump file '%s' is corrupt.\n",
2510 iov[0].iov_base = (char *) &hdsk;
2511 iov[0].iov_len = sizeof(struct hostDiskEntry);
2513 if (hdr.interfaces) {
2514 ifp_len = sizeof(struct Interface) +
2515 ((hdr.interfaces-1) * sizeof(struct AddrPort));
2516 ifp = (struct Interface *) malloc(ifp_len);
2517 assert(ifp != NULL);
2518 iov[iovcnt].iov_base = (char *) ifp;
2519 iov[iovcnt].iov_len = ifp_len;
2523 hcps_len = hdr.hcps * sizeof(afs_int32);
2524 hcps = (afs_int32 *) malloc(hcps_len);
2525 assert(hcps != NULL);
2526 iov[iovcnt].iov_base = (char *) hcps;
2527 iov[iovcnt].iov_len = hcps_len;
2531 if ((ifp_len + hcps_len + sizeof(hdsk) + sizeof(hdr)) != hdr.len) {
2532 ViceLog(0, ("h_stateRestoreHost: host entry header length fields are inconsistent\n"));
2537 if (fs_stateReadV(state, iov, iovcnt)) {
2538 ViceLog(0, ("h_stateRestoreHost: failed to read host entry\n"));
2543 if (!hdr.hcps && hdsk.hcps_valid) {
2544 /* valid, zero-length host cps ; does this ever happen? */
2545 hcps = (afs_int32 *) malloc(sizeof(afs_int32));
2546 assert(hcps != NULL);
2550 assert(host != NULL);
2553 host->interface = ifp;
2556 host->hcps.prlist_val = hcps;
2557 host->hcps.prlist_len = hdr.hcps;
2560 h_diskEntryToHost_r(&hdsk, host);
2561 h_SetupCallbackConn_r(host);
2565 for (i = ifp->numberOfInterfaces-1; i >= 0; i--) {
2566 h_AddHostToHashTable_r(ifp->interface[i].addr,
2567 ifp->interface[i].port, host);
2569 h_AddHostToUuidHashTable_r(&ifp->uuid, host);
2571 h_AddHostToHashTable_r(host->host, host->port, host);
2573 h_InsertList_r(host);
2575 /* setup host id map entry */
2576 state->h_map.entries[hdsk.index].old_idx = hdsk.index;
2577 state->h_map.entries[hdsk.index].new_idx = host->index;
2589 /* serialize a host structure to disk */
2591 h_hostToDiskEntry_r(struct host * in, struct hostDiskEntry * out)
2593 out->host = in->host;
2594 out->port = in->port;
2595 out->hostFlags = in->hostFlags;
2596 out->Console = in->Console;
2597 out->hcpsfailed = in->hcpsfailed;
2598 out->LastCall = in->LastCall;
2599 out->ActiveCall = in->ActiveCall;
2600 out->cpsCall = in->cpsCall;
2601 out->cblist = in->cblist;
2602 #ifdef FS_STATS_DETAILED
2603 out->InSameNetwork = in->InSameNetwork;
2606 /* special fields we save, but are not memcpy'd back on restore */
2607 out->index = in->index;
2608 out->hcps_len = in->hcps.prlist_len;
2609 out->hcps_valid = (in->hcps.prlist_val == NULL) ? 0 : 1;
2612 /* restore a host structure from disk */
2614 h_diskEntryToHost_r(struct hostDiskEntry * in, struct host * out)
2616 out->host = in->host;
2617 out->port = in->port;
2618 out->hostFlags = in->hostFlags;
2619 out->Console = in->Console;
2620 out->hcpsfailed = in->hcpsfailed;
2621 out->LastCall = in->LastCall;
2622 out->ActiveCall = in->ActiveCall;
2623 out->cpsCall = in->cpsCall;
2624 out->cblist = in->cblist;
2625 #ifdef FS_STATS_DETAILED
2626 out->InSameNetwork = in->InSameNetwork;
2630 /* index translation routines */
2632 h_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
2636 /* hosts use a zero-based index, so old==0 is valid */
2638 if (old >= state->h_map.len) {
2639 ViceLog(0, ("h_OldToNew: index %d is out of range\n", old));
2641 } else if (state->h_map.entries[old].old_idx != old) { /* sanity check */
2642 ViceLog(0, ("h_OldToNew: index %d points to an invalid host record\n", old));
2645 *new = state->h_map.entries[old].new_idx;
2651 #endif /* AFS_DEMAND_ATTACH_FS */
2655 * This counts the number of workstations, the number of active workstations,
2656 * and the number of workstations declared "down" (i.e. not heard from
2657 * recently). An active workstation has received a call since the cutoff
2658 * time argument passed.
2661 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
2663 register struct host *host;
2664 register int num = 0, active = 0, del = 0;
2667 for (host = hostList; host; host = host->next) {
2668 if (!(host->hostFlags & HOSTDELETED)) {
2670 if (host->ActiveCall > cutofftime)
2672 if (host->hostFlags & VENUSDOWN)
2684 } /*h_GetWorkStats */
2687 /*------------------------------------------------------------------------
2688 * PRIVATE h_ClassifyAddress
2691 * Given a target IP address and a candidate IP address (both
2692 * in host byte order), classify the candidate into one of three
2693 * buckets in relation to the target by bumping the counters passed
2697 * a_targetAddr : Target address.
2698 * a_candAddr : Candidate address.
2699 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
2700 * addresses are either in the same network
2701 * or the same subnet.
2702 * a_diffSubnetP : ...when the candidate is in a different
2704 * a_diffNetworkP : ...when the candidate is in a different
2711 * The target and candidate addresses are both in host byte
2712 * order, NOT network byte order, when passed in.
2716 *------------------------------------------------------------------------*/
2719 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
2720 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
2721 afs_int32 * a_diffNetworkP)
2722 { /*h_ClassifyAddress */
2724 afs_uint32 targetNet;
2725 afs_uint32 targetSubnet;
2727 afs_uint32 candSubnet;
2730 * Put bad values into the subnet info to start with.
2732 targetSubnet = (afs_uint32) 0;
2733 candSubnet = (afs_uint32) 0;
2736 * Pull out the network and subnetwork numbers from the target
2737 * and candidate addresses. We can short-circuit this whole
2738 * affair if the target and candidate addresses are not of the
2741 if (IN_CLASSA(a_targetAddr)) {
2742 if (!(IN_CLASSA(a_candAddr))) {
2743 (*a_diffNetworkP)++;
2746 targetNet = a_targetAddr & IN_CLASSA_NET;
2747 candNet = a_candAddr & IN_CLASSA_NET;
2748 if (IN_SUBNETA(a_targetAddr))
2749 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
2750 if (IN_SUBNETA(a_candAddr))
2751 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
2752 } else if (IN_CLASSB(a_targetAddr)) {
2753 if (!(IN_CLASSB(a_candAddr))) {
2754 (*a_diffNetworkP)++;
2757 targetNet = a_targetAddr & IN_CLASSB_NET;
2758 candNet = a_candAddr & IN_CLASSB_NET;
2759 if (IN_SUBNETB(a_targetAddr))
2760 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
2761 if (IN_SUBNETB(a_candAddr))
2762 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
2763 } /*Class B target */
2764 else if (IN_CLASSC(a_targetAddr)) {
2765 if (!(IN_CLASSC(a_candAddr))) {
2766 (*a_diffNetworkP)++;
2769 targetNet = a_targetAddr & IN_CLASSC_NET;
2770 candNet = a_candAddr & IN_CLASSC_NET;
2773 * Note that class C addresses can't have subnets,
2774 * so we leave the defaults untouched.
2776 } /*Class C target */
2778 targetNet = a_targetAddr;
2779 candNet = a_candAddr;
2780 } /*Class D address */
2783 * Now, simply compare the extracted net and subnet values for
2784 * the two addresses (which at this point are known to be of the
2787 if (targetNet == candNet) {
2788 if (targetSubnet == candSubnet)
2789 (*a_sameNetOrSubnetP)++;
2793 (*a_diffNetworkP)++;
2795 } /*h_ClassifyAddress */
2798 /*------------------------------------------------------------------------
2799 * EXPORTED h_GetHostNetStats
2802 * Iterate through the host table, and classify each (non-deleted)
2803 * host entry into ``proximity'' categories (same net or subnet,
2804 * different subnet, different network).
2807 * a_numHostsP : Set to total number of (non-deleted) hosts.
2808 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
2809 * a_diffSubnetP : Set to # hosts on diff subnet as server.
2810 * a_diffNetworkP : Set to # hosts on diff network as server.
2816 * We only count non-deleted hosts. The storage pointed to by our
2817 * parameters is zeroed upon entry.
2821 *------------------------------------------------------------------------*/
2824 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
2825 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
2826 { /*h_GetHostNetStats */
2828 register struct host *hostP; /*Ptr to current host entry */
2829 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
2832 * Clear out the storage pointed to by our parameters.
2834 *a_numHostsP = (afs_int32) 0;
2835 *a_sameNetOrSubnetP = (afs_int32) 0;
2836 *a_diffSubnetP = (afs_int32) 0;
2837 *a_diffNetworkP = (afs_int32) 0;
2840 for (hostP = hostList; hostP; hostP = hostP->next) {
2841 if (!(hostP->hostFlags & HOSTDELETED)) {
2843 * Bump the number of undeleted host entries found.
2844 * In classifying the current entry's address, make
2845 * sure to first convert to host byte order.
2848 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
2849 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
2850 a_sameNetOrSubnetP, a_diffSubnetP,
2852 } /*Only look at non-deleted hosts */
2853 } /*For each host record hashed to this index */
2855 } /*h_GetHostNetStats */
2857 static afs_uint32 checktime;
2858 static afs_uint32 clientdeletetime;
2859 static struct AFSFid zerofid;
2863 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
2864 * Since it can serialize them, and pile up, it should be a separate LWP
2865 * from other events.
2868 CheckHost(register struct host *host, int held)
2870 register struct client *client;
2871 struct rx_connection *cb_conn = NULL;
2874 #ifdef AFS_DEMAND_ATTACH_FS
2875 /* kill the checkhost lwp ASAP during shutdown */
2877 if (fs_state.mode == FS_MODE_SHUTDOWN) {
2879 return H_ENUMERATE_BAIL(held);
2884 /* Host is held by h_Enumerate */
2886 for (client = host->FirstClient; client; client = client->next) {
2887 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
2888 client->deleted = 1;
2889 host->hostFlags |= CLIENTDELETED;
2892 if (host->LastCall < checktime) {
2894 if (!(host->hostFlags & HOSTDELETED)) {
2895 cb_conn = host->callback_rxcon;
2896 rx_GetConnection(cb_conn);
2897 if (host->LastCall < clientdeletetime) {
2898 host->hostFlags |= HOSTDELETED;
2899 if (!(host->hostFlags & VENUSDOWN)) {
2900 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
2901 if (host->interface) {
2904 RXAFSCB_InitCallBackState3(cb_conn,
2910 RXAFSCB_InitCallBackState(cb_conn);
2913 host->hostFlags |= ALTADDR; /* alternate addresses valid */
2916 (void)afs_inet_ntoa_r(host->host, hoststr);
2918 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
2919 hoststr, ntohs(host->port)));
2920 host->hostFlags |= VENUSDOWN;
2922 /* Note: it's safe to delete hosts even if they have call
2923 * back state, because break delayed callbacks (called when a
2924 * message is received from the workstation) will always send a
2925 * break all call backs to the workstation if there is no
2930 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
2932 (void)afs_inet_ntoa_r(host->host, hoststr);
2933 if (host->interface) {
2934 afsUUID uuid = host->interface->uuid;
2936 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2939 if (MultiProbeAlternateAddress_r(host)) {
2940 ViceLog(0,("CheckHost: Probing all interfaces of host %s:%d failed, code %d\n",
2941 hoststr, ntohs(host->port), code));
2942 host->hostFlags |= VENUSDOWN;
2947 code = RXAFSCB_Probe(cb_conn);
2951 ("CheckHost: Probe failed for host %s:%d, code %d\n",
2952 hoststr, ntohs(host->port), code));
2953 host->hostFlags |= VENUSDOWN;
2959 rx_PutConnection(cb_conn);
2972 * Set VenusDown for any hosts that have not had a call in 15 minutes and
2973 * don't respond to a probe. Note that VenusDown can only be cleared if
2974 * a message is received from the host (see ServerLWP in file.c).
2975 * Delete hosts that have not had any calls in 1 hour, clients that
2976 * have not had any calls in 15 minutes.
2978 * This routine is called roughly every 5 minutes.
2983 afs_uint32 now = FT_ApproxTime();
2985 memset((char *)&zerofid, 0, sizeof(zerofid));
2987 * Send a probe to the workstation if it hasn't been heard from in
2990 checktime = now - 15 * 60;
2991 clientdeletetime = now - 120 * 60; /* 2 hours ago */
2992 h_Enumerate(CheckHost, NULL);
2997 * This is called with host locked and held. At this point, the
2998 * hostHashTable should not have any entries for the alternate
2999 * interfaces. This function has to insert these entries in the
3002 * The addresses in the interfaceAddr list are in host byte order.
3005 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
3012 struct Interface *interface;
3018 number = interf->numberOfInterfaces;
3019 myAddr = host->host; /* current interface address */
3020 myPort = host->port; /* current port */
3023 ("initInterfaceAddr : host %s:%d numAddr %d\n",
3024 afs_inet_ntoa_r(myAddr, hoststr), ntohs(myPort), number));
3026 /* validation checks */
3027 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
3029 ("Invalid number of alternate addresses is %d\n", number));
3034 * Convert IP addresses to network byte order, and remove for
3035 * duplicate IP addresses from the interface list.
3037 for (i = 0, count = 0, found = 0; i < number; i++) {
3038 interf->addr_in[i] = htonl(interf->addr_in[i]);
3039 for (j = 0; j < count; j++) {
3040 if (interf->addr_in[j] == interf->addr_in[i])
3044 interf->addr_in[count] = interf->addr_in[i];
3045 if (interf->addr_in[count] == myAddr)
3052 * Allocate and initialize an interface structure for this host.
3055 interface = (struct Interface *)
3056 malloc(sizeof(struct Interface) +
3057 (sizeof(struct AddrPort) * (count - 1)));
3059 ViceLog(0, ("Failed malloc in initInterfaceAddr_r\n"));
3062 interface->numberOfInterfaces = count;
3064 interface = (struct Interface *)
3065 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
3067 interface->numberOfInterfaces = count + 1;
3068 interface->interface[count].addr = myAddr;
3069 interface->interface[count].port = myPort;
3071 interface->uuid = interf->uuid;
3072 for (i = 0; i < count; i++) {
3073 interface->interface[i].addr = interf->addr_in[i];
3074 /* We store the port as 7001 because the addresses reported by
3075 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
3076 * are coming from fully connected hosts (no NAT/PATs)
3078 interface->interface[i].port = htons(7001);
3081 assert(!host->interface);
3082 host->interface = interface;
3084 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
3085 ViceLog(125, ("--- alt address %s:%d\n",
3086 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3087 ntohs(host->interface->interface[i].port)));
3093 /* deleted a HashChain structure for this address and host */
3094 /* returns 1 on success */
3096 h_DeleteHostFromHashTableByAddr_r(afs_uint32 addr, afs_uint16 port, struct host *host)
3099 register struct h_hashChain **hp, *th;
3101 for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp);) {
3102 assert(th->hostPtr);
3103 if (th->hostPtr == host && th->addr == addr && th->port == port) {
3117 ** prints out all alternate interface address for the host. The 'level'
3118 ** parameter indicates what level of debugging sets this output
3121 printInterfaceAddr(struct host *host, int level)
3126 if (host->interface) {
3127 /* check alternate addresses */
3128 number = host->interface->numberOfInterfaces;
3130 for (i = 0; i < number; i++)
3131 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
3132 ntohs(host->interface->interface[i].port)));
3134 ViceLog(level, ("\n"));