Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / viced / host.h
1
2 /* Copyright (C) 1990 Transarc Corporation - All rights reserved */
3 /*
4  * (C) COPYRIGHT IBM CORPORATION 1987
5  * LICENSED MATERIALS - PROPERTY OF IBM
6  */
7 /* host.h */
8
9 #include "fs_stats.h"           /*File Server stats package*/
10
11 #ifdef AFS_PTHREAD_ENV
12 /*
13  * There are three locks in the host package.
14  * the global hash lock protects hash chains.
15  * the global list lock protects the list of hosts.
16  * a mutex in each host structure protects the structure.
17  * precedence is host_listlock_mutex, host->mutex, host_glock_mutex.
18  */
19 #include <rx/rx_globals.h>
20 #include <assert.h>
21 #include <pthread.h>
22 extern pthread_mutex_t host_glock_mutex;
23 #define H_LOCK \
24     assert(pthread_mutex_lock(&host_glock_mutex) == 0);
25 #define H_UNLOCK \
26     assert(pthread_mutex_unlock(&host_glock_mutex) == 0);
27 #else /* AFS_PTHREAD_ENV */
28 #define H_LOCK
29 #define H_UNLOCK
30 #endif /* AFS_PTHREAD_ENV */
31
32 #define h_MAXHOSTTABLEENTRIES 1000
33 #define h_HASHENTRIES 256 /* Power of 2 */
34 #define h_MAXHOSTTABLES 200
35 #define h_HTSPERBLOCK 512 /* Power of 2 */
36 #define h_HTSHIFT 9 /* log base 2 of HTSPERBLOCK */
37
38 #define h_threadsPerSlot        32              /* bits per afs_int32 */
39 #define h_threadsShift          5               /* for multiply/divide */
40 #define h_threadsMask           31              /* for remainder */
41
42 /* size of the hold array for each host */
43 #define h_maxSlots      ((MAX_FILESERVER_THREAD+h_threadsPerSlot-1)>>h_threadsShift)
44
45 struct Identity
46 {
47         char valid;             /* zero if UUID is unknown */
48         afsUUID uuid;
49 };
50
51 struct Interface
52 {
53         int             numberOfInterfaces;
54         afsUUID         uuid;
55         afs_int32               addr[1]; /* there are actually more than one here */
56                                  /* in network byte order */
57 };
58 struct host {
59     struct host         *next, *prev;       /* linked list of all hosts */
60     struct rx_connection *callback_rxcon;   /* rx callback connection */
61     afs_int32                holds[h_maxSlots];        
62                                         /* holds on this host; 1 bit per lwp.
63                                        A hold prevents this structure and
64                                        inferior structures from disappearing */
65     unsigned int        host;           /* IP address of host interface that is
66                                            currently being used, in network
67                                            byte order*/
68     unsigned short      port;           /* port address of host */
69     char                Console;        /* XXXX This host is a console */
70     char                hostFlags;      /*  bit map */
71 #if FS_STATS_DETAILED
72     char                InSameNetwork;  /*Is host's addr in the same network as
73                                           the File Server's?*/
74     char                dummy[3];       /* for padding */
75 #endif /* FS_STATS_DETAILED */
76     char                hcpsfailed;     /* Retry the cps call next time */
77     prlist              hcps;           /* cps for hostip acls */
78     afs_uint32  LastCall;       /* time of last call from host */
79     afs_uint32  ActiveCall;     /* time of any call but gettime */
80     struct client       *FirstClient;   /* first connection from host */
81     afs_uint32  cpsCall;        /* time of last cps call from this host */
82     struct Interface*   interface;      /* all alternate addr for client */
83     unsigned short      cblist;         /* Call back list for this host */
84     /*
85      * These don't get zeroed, keep them at the end. If index doesn't
86      * follow an unsigned short then we need to pad to ensure that
87      * the index fields isn't zeroed.
88      */
89     unsigned short      index;          /* Host table index, for vicecb.c */
90     struct Lock         lock;           /* Write lock for synchronization of
91                                       VenusDown flag */
92 #ifdef AFS_PTHREAD_ENV
93     pthread_cond_t      cond;           /* used to wait on hpcsValid */
94 #endif /* AFS_PTHREAD_ENV */
95 };
96
97 /* * Don't zero the index, lock or condition varialbles */
98 #define HOST_TO_ZERO(H) (int)(((char *)(&((H)->index))-(char *)(H)))
99
100 struct h_hashChain
101 {
102         struct host*            hostPtr;
103         struct h_hashChain*       next;
104         afs_int32                         addr;
105 }; 
106
107 struct client {
108     struct client       *next;          /* next client entry for host */
109     struct host         *host;          /* ptr to parent host entry */
110     afs_int32                sid;            /* Connection number from this host */
111     struct rx_connection *tcon;         /* most recent server connection
112                                          * associated with this client */
113     prlist              CPS;            /* cps for authentication */
114     int                 ViceId;         /* Vice ID of user */
115     afs_int32           expTime;        /* RX-only: expiration time */
116     afs_uint32  LastCall;       /* time of last call */
117     afs_uint32       VenusEpoch;     /* Venus start time--used to identify
118                                          * venus.  Actually, now an extension of the
119                                          * sid, which is why it moved.
120                                          */
121     afs_int32           refCount;       /* reference count */
122     char                deleted;        /* True if this client should be deleted
123                                       when there are no more users of the
124                                       structure */
125     char                authClass;      /* auth type, RX-only */
126     char                prfail;         /* True if prserver couldn't be contacted */
127 #if FS_STATS_DETAILED
128     char                InSameNetwork;  /* Is client's IP address in the same
129                                            network as ours? */
130 #else /* FS_STATS_DETAILED */
131     char                dummy;          /* For padding */
132 #endif /* FS_STATS_DETAILED */
133     struct Lock         lock;           /* lock to ensure CPS valid if entry
134                                          * on host's clients list. */
135 };
136
137 /* Don't zero the lock */
138 #define CLIENT_TO_ZERO(C)       ((int)(((char *)(&((C)->lock))-(char *)(C))))
139
140 /*
141  * key for the client structure stored in connection specific data
142  */
143 extern int rxcon_client_key;
144
145 /* Some additional functions to get at client information.  Client must have
146    an active connection for this to work.  If a lwp is working on a request
147    for the client, then the client must have a connection */
148 /* N.B. h_UserName returns pointer to static data; also relatively expensive */
149 extern char *h_UserName();
150
151 /* all threads whose thread-id is greater than the size of the hold array,
152 ** then use the most significant bit in the 'hold' field in the host structure 
153 */
154 #ifdef AFS_PTHREAD_ENV
155 #define h_lwpIndex() ( ((long)(pthread_getspecific(rx_thread_id_key)) > \
156                         ((h_maxSlots << h_threadsShift)-1)) ? \
157                                 (h_maxSlots << h_threadsShift) -1 : \
158                                 (long)(pthread_getspecific(rx_thread_id_key)) )
159 #else /* AFS_PTHREAD_ENV */
160 #define h_lwpIndex() ( (LWP_Index() > ((h_maxSlots << h_threadsShift)-1)) ? \
161                                         (h_maxSlots << h_threadsShift) -1 : \
162                                         LWP_Index() )
163 #endif /* AFS_PTHREAD_ENV */
164 #define h_holdIndex()( h_lwpIndex() & h_threadsMask) 
165 #define h_holdSlot() ( h_lwpIndex() >> h_threadsShift)  /*index in 'holds'*/
166 #define h_holdbit()  ( 1<<h_holdIndex() )
167
168 #define h_Hold_r(host)   ((host)->holds[h_holdSlot()] |= h_holdbit())
169 extern int h_Hold();
170 extern int h_Release();
171 extern int h_Release_r();
172
173 #define h_Held_r(host)   ((h_holdbit() & (host)->holds[h_holdSlot()]) != 0)
174 extern int h_Held();
175 extern int h_OtherHolds();
176 extern int h_OtherHolds_r();
177 #define h_Lock(host)    ObtainWriteLock(&(host)->lock)
178 extern int h_Lock_r();
179 #define h_Unlock(host)  ReleaseWriteLock(&(host)->lock)
180 #define h_Unlock_r(host)  ReleaseWriteLock(&(host)->lock)
181
182 #define AddCallBack(host, fid)  AddCallBack1((host), (fid), (u_short *)0, 1/*CB_NORMAL*/, 0)
183 #define AddVolCallBack(host, fid) AddCallBack1((host), (fid), (u_short *)0, 3/*CB_VOLUME*/, 0)
184 #define AddBulkCallBack(host, fid) AddCallBack1((host), (fid), (u_short *)0, 4/*CB_BULK*/, 0)
185
186 /* operations on the global linked list of hosts */
187 #define h_InsertList_r(h)       (h)->next =  hostList;                  \
188                                 (h)->prev = 0;                          \
189                                 hostList ? (hostList->prev = (h)):0;    \
190                                 hostList = (h);                         \
191                                 hostCount++;
192 #define h_DeleteList_r(h)       assert(hostCount>0);                        \
193                                 hostCount--;                                \
194                                 (h)->next ? ((h)->next->prev = (h)->prev):0;\
195                                 (h)->prev ? ((h)->prev->next = (h)->next):0;\
196                                 ( h == hostList )? (hostList = h->next):0;
197
198 extern struct host *h_Alloc();
199 extern struct host *h_Alloc_r();
200 extern struct host *h_Lookup();
201 extern struct host *h_Lookup_r();
202 extern struct host *h_LookupUuid_r();
203 extern int h_FreeConnection();
204 extern int h_Enumerate();
205 extern struct host *h_GetHost();
206 extern struct host *h_GetHost_r();
207 extern struct client *h_FindClient_r();
208 extern int h_ReleaseClient_r();
209 extern struct client *h_ID2Client();
210 extern int GetClient();
211 extern h_PrintStats();
212 extern h_PrintStats_r();
213 extern h_PrintClients();
214 extern h_PrintClients_r();
215 extern h_GetWorkStats();
216 extern h_GetWorkStats_r();
217 extern void h_flushhostcps();
218 struct Interface *MultiVerifyInterface_r();
219
220 struct host *(hosttableptrs[h_MAXHOSTTABLES]);  /* Used by h_itoh */
221 #define h_htoi(host) ((host)->index) /* index isn't zeroed, no need to lock */
222 #define h_itoh(hostindex) (hosttableptrs[(hostindex)>>h_HTSHIFT]+((hostindex)&(h_HTSPERBLOCK-1)))
223
224 #define HPCS_INPROGRESS                 0x01 /*set when CPS is being updated */
225 #define HPCS_WAITING                    0x02 /*waiting for CPS to get updated*/
226 #define ALTADDR                         0x04 /*InitCallBack is being done */
227 #define VENUSDOWN                       0x08 /* venus CallBack failed */
228 #define HOSTDELETED                     0x10 /* host delated */
229 #define CLIENTDELETED                   0x20 /* client deleted */
230 #define RESETDONE                       0x40 /* callback reset done */