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