e02e4426bbf29ac76284c12404d17284873cc2b8
[openafs.git] / src / viced / host.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  *
9  * Portions Copyright (c) 2006 Sine Nomine Associates
10  */
11
12 #ifndef _AFS_VICED_HOST_H
13 #define _AFS_VICED_HOST_H
14
15 #include "fs_stats.h"           /*File Server stats package */
16
17 #ifdef AFS_PTHREAD_ENV
18 /*
19  * There are three locks in the host package.
20  * the global hash lock protects hash chains.
21  * the global list lock protects the list of hosts.
22  * a mutex in each host structure protects the structure.
23  * precedence is host_listlock_mutex, host->mutex, host_glock_mutex.
24  */
25 #include <rx/rx_globals.h>
26 #include <afs/afs_assert.h>
27 #include <pthread.h>
28 extern pthread_mutex_t host_glock_mutex;
29 #define H_LOCK MUTEX_ENTER(&host_glock_mutex);
30 #define H_UNLOCK MUTEX_EXIT(&host_glock_mutex);
31 extern pthread_key_t viced_uclient_key;
32 #else /* AFS_PTHREAD_ENV */
33 #define H_LOCK
34 #define H_UNLOCK
35 #endif /* AFS_PTHREAD_ENV */
36
37 #define h_MAXHOSTTABLEENTRIES 1000
38 #define h_HASHENTRIES 256       /* Power of 2 */
39 #define h_MAXHOSTTABLES 200
40 #define h_HTSPERBLOCK 512       /* Power of 2 */
41 #define h_HTSHIFT 9             /* log base 2 of HTSPERBLOCK */
42
43 struct Identity {
44     char valid;                 /* zero if UUID is unknown */
45     afsUUID uuid;
46 };
47
48 struct AddrPort  {
49     afs_uint32 addr;            /* in network byte order */
50     afs_uint16 port;            /* in network byte order */
51     afs_int16  valid;
52 };
53
54 struct Interface {
55     afsUUID uuid;
56     int numberOfInterfaces;
57     struct AddrPort interface[1];/* there are actually more than one here */
58     /* in network byte order */
59 };
60
61 struct host {
62     struct host *next, *prev;   /* linked list of all hosts */
63     struct rx_connection *callback_rxcon;       /* rx callback connection */
64     afs_uint32 refCount; /* reference count */
65     afs_uint32 host;            /* IP address of host interface that is
66                                  * currently being used, in network
67                                  * byte order */
68     afs_uint16 port;            /* port address of host */
69     char Console;               /* XXXX This host is a console */
70     unsigned short 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                                    getstats and getcaps */
81     struct client *FirstClient; /* first connection from host */
82     afs_uint32 cpsCall;         /* time of last cps call from this host */
83     struct Interface *interface;        /* all alternate addr for client */
84     afs_uint32 cblist;          /* index of a cb in the per-host circular CB list */
85     /*
86      * These don't get zeroed, keep them at the end. If index doesn't
87      * follow an unsigned short then we need to pad to ensure that
88      * the index fields isn't zeroed. XXX
89      */
90     afs_uint32 index;           /* Host table index, for vicecb.c */
91     struct Lock lock;           /* Write lock for synchronization of
92                                  * VenusDown flag */
93 #ifdef AFS_PTHREAD_ENV
94     pthread_cond_t cond;        /* used to wait on hcpsValid */
95 #endif                          /* AFS_PTHREAD_ENV */
96 };
97
98 /* * Don't zero the index, lock or condition varialbles */
99 #define HOST_TO_ZERO(H) (int)(((char *)(&((H)->index))-(char *)(H)))
100
101 struct h_AddrHashChain {
102     struct host *hostPtr;
103     struct h_AddrHashChain *next;
104     afs_uint32 addr;
105     afs_uint16 port;
106 };
107
108 struct h_UuidHashChain {
109     struct host *hostPtr;
110     struct h_UuidHashChain *next;
111 };
112
113 struct client {
114     struct client *next;        /* next client entry for host */
115     struct host *host;          /* ptr to parent host entry */
116     afs_int32 sid;              /* Connection number from this host */
117     prlist CPS;                 /* cps for authentication */
118     int ViceId;                 /* Vice ID of user */
119     afs_int32 expTime;          /* RX-only: expiration time */
120     afs_uint32 LastCall;        /* time of last call */
121     afs_uint32 VenusEpoch;      /* Venus start time--used to identify
122                                  * venus.  Actually, now an extension of the
123                                  * sid, which is why it moved.
124                                  */
125     afs_int32 refCount;         /* reference count */
126     char deleted;               /* True if this client should be deleted
127                                  * when there are no more users of the
128                                  * structure */
129     char authClass;             /* auth type, RX-only */
130     char prfail;                /* True if prserver couldn't be contacted */
131 #if FS_STATS_DETAILED
132     char InSameNetwork;         /* Is client's IP address in the same
133                                  * network as ours? */
134 #else                           /* FS_STATS_DETAILED */
135     char dummy;                 /* For padding */
136 #endif                          /* FS_STATS_DETAILED */
137     struct Lock lock;           /* lock to ensure CPS valid if entry
138                                  * on host's clients list. */
139 };
140
141 /* Don't zero the lock */
142 #define CLIENT_TO_ZERO(C)       ((int)(((char *)(&((C)->lock))-(char *)(C))))
143
144
145 /*
146  * key for the client structure stored in connection specific data
147  */
148 extern int rxcon_client_key;
149
150 /* Some additional functions to get at client information.  Client must have
151    an active connection for this to work.  If a lwp is working on a request
152    for the client, then the client must have a connection */
153 /* N.B. h_UserName returns pointer to static data; also relatively expensive */
154 extern char *h_UserName(struct client *client);
155 #define h_Lock(host)    ObtainWriteLock(&(host)->lock)
156 extern int h_Lock_r(struct host *host);
157 #define h_Unlock(host)  ReleaseWriteLock(&(host)->lock)
158 #define h_Unlock_r(host)  ReleaseWriteLock(&(host)->lock)
159
160 #define AddCallBack(host, fid)  AddCallBack1((host), (fid), (afs_uint32 *)0, 1/*CB_NORMAL*/, 0)
161 #define AddVolCallBack(host, fid) AddCallBack1((host), (fid), (afs_uint32 *)0, 3/*CB_VOLUME*/, 0)
162 #define AddBulkCallBack(host, fid) AddCallBack1((host), (fid), (afs_uint32 *)0, 4/*CB_BULK*/, 0)
163
164 /* A simple refCount replaces per-thread hold mechanism.  The former
165  * hold semantics are not different from refcounting, except with respect
166  * to cross-thread assertions.  In this change, refcount is protected by
167  * H_LOCK, just like former hold bitmap.  A future change will replace locks
168  * th lock-free operations.  */
169
170 #define h_Hold_r(x) \
171 do { \
172         ++((x)->refCount); \
173 } while(0)
174
175 #define h_Decrement_r(x) \
176 do { \
177         --((x)->refCount); \
178 } while (0)
179
180 #define h_Release_r(x) \
181 do { \
182         h_Decrement_r(x); \
183         if (((x)->refCount < 1) && \
184                 (((x)->hostFlags & HOSTDELETED) || \
185                  ((x)->hostFlags & CLIENTDELETED))) h_TossStuff_r((x));  \
186 } while(0)
187
188 /* operations on the global linked list of hosts */
189 #define h_InsertList_r(h)       (h)->next =  hostList;                  \
190                                 (h)->prev = 0;                          \
191                                 hostList ? (hostList->prev = (h)):0;    \
192                                 hostList = (h);                         \
193                                 hostCount++;
194 #define h_DeleteList_r(h)       osi_Assert(hostCount>0);                    \
195                                 hostCount--;                                \
196                                 (h)->next ? ((h)->next->prev = (h)->prev):0;\
197                                 (h)->prev ? ((h)->prev->next = (h)->next):0;\
198                                 ( h == hostList )? (hostList = h->next):0;
199
200 extern int DeleteAllCallBacks_r(struct host *host, int deletefe);
201 extern int DeleteCallBack(struct host *host, AFSFid * fid);
202 extern int MultiProbeAlternateAddress_r(struct host *host);
203 extern int BreakDelayedCallBacks_r(struct host *host);
204 extern int AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
205              int locked);
206 extern int BreakCallBack(struct host *xhost, AFSFid * fid, int flag);
207 extern int DeleteFileCallBacks(AFSFid * fid);
208 extern int CleanupTimedOutCallBacks(void);
209 extern int CleanupTimedOutCallBacks_r(void);
210 extern int MultiBreakCallBackAlternateAddress(struct host *host, struct AFSCBFids *afidp);
211 extern int MultiBreakCallBackAlternateAddress_r(struct host *host,
212                                      struct AFSCBFids *afidp);
213 extern int DumpCallBackState(void);
214 extern int PrintCallBackStats(void);
215 extern void *ShutDown(void *);
216 extern void ShutDownAndCore(int dopanic);
217
218 extern struct host *h_Alloc(struct rx_connection *r_con);
219 extern struct host *h_Alloc_r(struct rx_connection *r_con);
220 extern int h_Lookup_r(afs_uint32 hostaddr, afs_uint16 hport,
221                       struct host **hostp);
222 extern struct host *h_LookupUuid_r(afsUUID * uuidp);
223 extern void h_Enumerate(int (*proc) (struct host *, int, void *), void *param);
224 extern void h_Enumerate_r(int (*proc) (struct host *, int, void *), struct host *enumstart, void *param);
225 extern struct host *h_GetHost_r(struct rx_connection *tcon);
226 extern struct client *h_FindClient_r(struct rx_connection *tcon);
227 extern int h_ReleaseClient_r(struct client *client);
228 extern void h_TossStuff_r(struct host *host);
229 extern struct client *h_ID2Client(afs_int32 vid);
230 extern int GetClient(struct rx_connection *tcon, struct client **cp);
231 extern int PutClient(struct client **cp);
232 extern void h_PrintStats(void);
233 extern void h_PrintClients(void);
234 extern void h_GetWorkStats(int *, int *, int *, afs_int32);
235 extern void h_GetWorkStats64(afs_uint64 *, afs_uint64 *, afs_uint64 *, afs_int32);
236 extern void h_flushhostcps(afs_uint32 hostaddr,
237                            afs_uint16 hport);
238 extern void h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
239                   afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP);
240 extern int h_NBLock_r(struct host *host);
241 extern void h_DumpHosts(void);
242 extern void h_InitHostPackage(void);
243 extern void h_CheckHosts(void );
244 extern int initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf);
245 extern void h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host * host);
246 extern void h_AddHostToUuidHashTable_r(afsUUID * uuid, struct host * host);
247 extern int h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host);
248 extern int h_DeleteHostFromUuidHashTable_r(struct host *host);
249 extern int initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf);
250 extern int addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port);
251 extern int removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port);
252 extern afs_int32 hpr_Initialize(struct ubik_client **);
253 extern int hpr_End(struct ubik_client *);
254 extern int hpr_IdToName(idlist *ids, namelist *names);
255 extern int hpr_NameToId(namelist *names, idlist *ids);
256
257 #ifdef AFS_DEMAND_ATTACH_FS
258 /*
259  * demand attach fs
260  * state serialization
261  */
262 extern int h_SaveState(void);
263 extern int h_RestoreState(void);
264 #endif
265
266 #define H_ENUMERATE_BAIL(flags)        ((flags)|0x80000000)
267 #define H_ENUMERATE_ISSET_BAIL(flags)  ((flags)&0x80000000)
268 #define H_ENUMERATE_ISSET_HELD(flags)  ((flags)&0x7FFFFFFF)
269
270 struct host *(hosttableptrs[h_MAXHOSTTABLES]);  /* Used by h_itoh */
271 #define h_htoi(host) ((host)->index)    /* index isn't zeroed, no need to lock */
272 #define h_itoh(hostindex) (hosttableptrs[(hostindex)>>h_HTSHIFT]+((hostindex)&(h_HTSPERBLOCK-1)))
273
274 #define rxr_GetEpoch(aconn) (((struct rx_connection *)(aconn))->epoch)
275
276 #define rxr_CidOf(aconn) (((struct rx_connection *)(aconn))->cid)
277
278 #define rxr_PortOf(aconn) \
279     rx_PortOf(rx_PeerOf(((struct rx_connection *)(aconn))))
280
281 #define rxr_HostOf(aconn) \
282     rx_HostOf(rx_PeerOf((struct rx_connection *)(aconn)))
283
284 #define HCPS_INPROGRESS                 0x01    /*set when CPS is being updated */
285 #define HCPS_WAITING                    0x02    /*waiting for CPS to get updated */
286 #define ALTADDR                         0x04    /*InitCallBack is being done */
287 #define VENUSDOWN                       0x08    /* venus CallBack failed */
288 #define HOSTDELETED                     0x10    /* host delated */
289 #define CLIENTDELETED                   0x20    /* client deleted */
290 #define RESETDONE                       0x40    /* callback reset done */
291 #define HFE_LATER                       0x80    /* host has FE_LATER callbacks */
292 #define HERRORTRANS                    0x100    /* do error translation */
293 #define HWHO_INPROGRESS                0x200    /* set when WhoAreYou running */
294 #define HCBREAK                        0x400    /* flag for a multi CB break */
295 #endif /* _AFS_VICED_HOST_H */