h_TossStuff_r: make sure host does not go away
[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 <assert.h>
27 #include <pthread.h>
28 extern pthread_mutex_t host_glock_mutex;
29 #define H_LOCK \
30     assert(pthread_mutex_lock(&host_glock_mutex) == 0)
31 #define H_UNLOCK \
32     assert(pthread_mutex_unlock(&host_glock_mutex) == 0)
33 extern pthread_key_t viced_uclient_key;
34 #else /* AFS_PTHREAD_ENV */
35 #define H_LOCK
36 #define H_UNLOCK
37 #endif /* AFS_PTHREAD_ENV */
38
39 #define h_MAXHOSTTABLEENTRIES 1000
40 #define h_HASHENTRIES 256       /* Power of 2 */
41 #define h_MAXHOSTTABLES 200
42 #define h_HTSPERBLOCK 512       /* Power of 2 */
43 #define h_HTSHIFT 9             /* log base 2 of HTSPERBLOCK */
44
45 struct Identity {
46     char valid;                 /* zero if UUID is unknown */
47     afsUUID uuid;
48 };
49
50 struct AddrPort  {
51     afs_uint32 addr;            /* in network byte order */
52     afs_uint16 port;            /* in network byte order */
53     afs_int16  valid;
54 };
55
56 struct Interface {
57     afsUUID uuid;
58     int numberOfInterfaces;
59     struct AddrPort interface[1];/* there are actually more than one here */
60     /* in network byte order */
61 };
62
63 struct host {
64     struct host *next, *prev;   /* linked list of all hosts */
65     struct rx_connection *callback_rxcon;       /* rx callback connection */
66     afs_uint32 refCount; /* reference count */
67     afs_uint32 host;            /* IP address of host interface that is
68                                  * currently being used, in network
69                                  * byte order */
70     afs_uint16 port;            /* port address of host */
71     char Console;               /* XXXX This host is a console */
72     unsigned short hostFlags;           /*  bit map */
73 #if FS_STATS_DETAILED
74     char InSameNetwork;         /*Is host's addr in the same network as
75                                  * the File Server's? */
76     char dummy[3];              /* for padding */
77 #endif                          /* FS_STATS_DETAILED */
78     char hcpsfailed;            /* Retry the cps call next time */
79     prlist hcps;                /* cps for hostip acls */
80     afs_uint32 LastCall;        /* time of last call from host */
81     afs_uint32 ActiveCall;      /* time of any call but gettime, 
82                                    getstats and getcaps */
83     struct client *FirstClient; /* first connection from host */
84     afs_uint32 cpsCall;         /* time of last cps call from this host */
85     struct Interface *interface;        /* all alternate addr for client */
86     afs_uint32 cblist;          /* index of a cb in the per-host circular CB list */
87     /*
88      * These don't get zeroed, keep them at the end. If index doesn't
89      * follow an unsigned short then we need to pad to ensure that
90      * the index fields isn't zeroed. XXX
91      */
92     afs_uint32 index;           /* Host table index, for vicecb.c */
93     struct Lock lock;           /* Write lock for synchronization of
94                                  * VenusDown flag */
95 #ifdef AFS_PTHREAD_ENV
96     pthread_cond_t cond;        /* used to wait on hcpsValid */
97 #endif                          /* AFS_PTHREAD_ENV */
98 };
99
100 /* * Don't zero the index, lock or condition varialbles */
101 #define HOST_TO_ZERO(H) (int)(((char *)(&((H)->index))-(char *)(H)))
102
103 struct h_AddrHashChain {
104     struct host *hostPtr;
105     struct h_AddrHashChain *next;
106     afs_uint32 addr;
107     afs_uint16 port;
108 };
109
110 struct h_UuidHashChain {
111     struct host *hostPtr;
112     struct h_UuidHashChain *next;
113 };
114
115 struct client {
116     struct client *next;        /* next client entry for host */
117     struct host *host;          /* ptr to parent host entry */
118     afs_int32 sid;              /* Connection number from this host */
119     prlist CPS;                 /* cps for authentication */
120     int ViceId;                 /* Vice ID of user */
121     afs_int32 expTime;          /* RX-only: expiration time */
122     afs_uint32 LastCall;        /* time of last call */
123     afs_uint32 VenusEpoch;      /* Venus start time--used to identify
124                                  * venus.  Actually, now an extension of the
125                                  * sid, which is why it moved.
126                                  */
127     afs_int32 refCount;         /* reference count */
128     char deleted;               /* True if this client should be deleted
129                                  * when there are no more users of the
130                                  * structure */
131     char authClass;             /* auth type, RX-only */
132     char prfail;                /* True if prserver couldn't be contacted */
133 #if FS_STATS_DETAILED
134     char InSameNetwork;         /* Is client's IP address in the same
135                                  * network as ours? */
136 #else                           /* FS_STATS_DETAILED */
137     char dummy;                 /* For padding */
138 #endif                          /* FS_STATS_DETAILED */
139     struct Lock lock;           /* lock to ensure CPS valid if entry
140                                  * on host's clients list. */
141 };
142
143 /* Don't zero the lock */
144 #define CLIENT_TO_ZERO(C)       ((int)(((char *)(&((C)->lock))-(char *)(C))))
145
146
147 /*
148  * key for the client structure stored in connection specific data
149  */
150 extern int rxcon_client_key;
151
152 /* Some additional functions to get at client information.  Client must have
153    an active connection for this to work.  If a lwp is working on a request
154    for the client, then the client must have a connection */
155 /* N.B. h_UserName returns pointer to static data; also relatively expensive */
156 extern char *h_UserName(struct client *client);
157 #define h_Lock(host)    ObtainWriteLock(&(host)->lock)
158 extern int h_Lock_r(register struct host *host);
159 #define h_Unlock(host)  ReleaseWriteLock(&(host)->lock)
160 #define h_Unlock_r(host)  ReleaseWriteLock(&(host)->lock)
161
162 #define AddCallBack(host, fid)  AddCallBack1((host), (fid), (afs_uint32 *)0, 1/*CB_NORMAL*/, 0)
163 #define AddVolCallBack(host, fid) AddCallBack1((host), (fid), (afs_uint32 *)0, 3/*CB_VOLUME*/, 0)
164 #define AddBulkCallBack(host, fid) AddCallBack1((host), (fid), (afs_uint32 *)0, 4/*CB_BULK*/, 0)
165
166 /* A simple refCount replaces per-thread hold mechanism.  The former
167  * hold semantics are not different from refcounting, except with respect
168  * to cross-thread assertions.  In this change, refcount is protected by
169  * H_LOCK, just like former hold bitmap.  A future change will replace locks
170  * th lock-free operations.  */
171
172 #define h_Hold_r(x) \
173 do { \
174         ++((x)->refCount); \
175 } while(0)
176
177 #define h_Decrement_r(x) \
178 do { \
179         --((x)->refCount); \
180 } while (0)
181
182 #define h_Release_r(x) \
183 do { \
184         h_Decrement_r(x); \
185         if (((x)->refCount < 1) && \
186                 (((x)->hostFlags & HOSTDELETED) || \
187                  ((x)->hostFlags & CLIENTDELETED))) h_TossStuff_r((x));  \
188 } while(0)
189
190 /* operations on the global linked list of hosts */
191 #define h_InsertList_r(h)       (h)->next =  hostList;                  \
192                                 (h)->prev = 0;                          \
193                                 hostList ? (hostList->prev = (h)):0;    \
194                                 hostList = (h);                         \
195                                 hostCount++;
196 #define h_DeleteList_r(h)       assert(hostCount>0);                        \
197                                 hostCount--;                                \
198                                 (h)->next ? ((h)->next->prev = (h)->prev):0;\
199                                 (h)->prev ? ((h)->prev->next = (h)->next):0;\
200                                 ( h == hostList )? (hostList = h->next):0;
201
202 extern int DeleteAllCallBacks_r(struct host *host, int deletefe);
203 extern int DeleteCallBack(struct host *host, AFSFid * fid);
204 extern int MultiProbeAlternateAddress_r(struct host *host);
205 extern int BreakDelayedCallBacks_r(struct host *host);
206 extern int AddCallBack1(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
207              int locked);
208 extern int BreakCallBack(struct host *xhost, AFSFid * fid, int flag);
209 extern int DeleteFileCallBacks(AFSFid * fid);
210 extern int CleanupTimedOutCallBacks(void);
211 extern int CleanupTimedOutCallBacks_r(void);
212 extern int MultiBreakCallBackAlternateAddress(struct host *host, struct AFSCBFids *afidp);
213 extern int MultiBreakCallBackAlternateAddress_r(struct host *host,
214                                      struct AFSCBFids *afidp);
215 extern int DumpCallBackState(void);
216 extern int PrintCallBackStats(void);
217 extern void *ShutDown(void *);
218 extern void ShutDownAndCore(int dopanic);
219
220 extern struct host *h_Alloc(register struct rx_connection *r_con);
221 extern struct host *h_Alloc_r(register struct rx_connection *r_con);
222 extern int h_Lookup_r(afs_uint32 hostaddr, afs_uint16 hport,
223                       struct host **hostp);
224 extern struct host *h_LookupUuid_r(afsUUID * uuidp);
225 extern void h_Enumerate(int (*proc) (struct host *, int, void *), void *param);
226 extern void h_Enumerate_r(int (*proc) (struct host *, int, void *), struct host *enumstart, void *param);
227 extern struct host *h_GetHost_r(struct rx_connection *tcon);
228 extern struct client *h_FindClient_r(struct rx_connection *tcon);
229 extern int h_ReleaseClient_r(struct client *client);
230 extern void h_TossStuff_r(register struct host *host);
231 extern struct client *h_ID2Client(afs_int32 vid);
232 extern int GetClient(struct rx_connection *tcon, struct client **cp);
233 extern int PutClient(struct client **cp);
234 extern void h_PrintStats(void);
235 extern void h_PrintClients(void);
236 extern void h_GetWorkStats(int *, int *, int *, afs_int32);
237 extern void h_flushhostcps(register afs_uint32 hostaddr,
238                            register afs_uint16 hport);
239 extern void h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
240                   afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP);
241 extern int h_NBLock_r(register struct host *host);
242 extern void h_DumpHosts(void);
243 extern void h_InitHostPackage(void);
244 extern void h_CheckHosts(void );
245 extern int initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf);
246 extern void h_AddHostToAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host * host);
247 extern void h_AddHostToUuidHashTable_r(afsUUID * uuid, struct host * host);
248 extern int h_DeleteHostFromAddrHashTable_r(afs_uint32 addr, afs_uint16 port, struct host *host);
249 extern int h_DeleteHostFromUuidHashTable_r(struct host *host);
250 extern int initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf);
251 extern int addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port);
252 extern int removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port);
253 extern afs_int32 hpr_Initialize(struct ubik_client **);
254 extern int hpr_End(struct ubik_client *);
255
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 #endif /* _AFS_VICED_HOST_H */