bf75fbc8d3aba4536eba1df58dac43ed82d84417
[openafs.git] / src / ubik / ubik.p.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
10 #if !defined(lint) && !defined(LOCORE) && defined(RCS_HDRS)
11 #endif
12 #ifndef UBIK_H
13 #define UBIK_H
14
15 /* these are now appended by the error table compiler */
16 #if 0
17 /* ubik error codes */
18 #define UMINCODE        100000  /* lowest ubik error code */
19 #define UNOQUORUM       100000  /* no quorum elected */
20 #define UNOTSYNC        100001  /* not synchronization site (should work on sync site) */
21 #define UNHOSTS         100002  /* too many hosts */
22 #define UIOERROR        100003  /* I/O error writing dbase or log */
23 #define UINTERNAL       100004  /* mysterious internal error */
24 #define USYNC           100005  /* major synchronization error */
25 #define UNOENT          100006  /* file not found when processing dbase */
26 #define UBADLOCK        100007  /* bad lock range size (must be 1) */
27 #define UBADLOG         100008  /* read error reprocessing log */
28 #define UBADHOST        100009  /* problems with host name */
29 #define UBADTYPE        100010  /* bad operation for this transaction type */
30 #define UTWOENDS        100011  /* two commits or aborts done to transaction */
31 #define UDONE           100012  /* operation done after abort (or commmit) */
32 #define UNOSERVERS      100013  /* no servers appear to be up */
33 #define UEOF            100014  /* premature EOF */
34 #define ULOGIO          100015  /* error writing log file */
35 #define UMAXCODE        100100  /* largest ubik error code */
36
37 #endif
38
39 #if defined(UKERNEL)
40 #include "../afsint/ubik_int.h"
41 #else /* defined(UKERNEL) */
42 #include <ubik_int.h>
43 #endif /* defined(UKERNEL) */
44
45 /* ubik_trans types */
46 #define UBIK_READTRANS      0
47 #define UBIK_WRITETRANS     1
48
49 /* ubik_lock types */
50 #define LOCKREAD            1
51 #define LOCKWRITE           2
52 #define LOCKWAIT            3
53
54 /* ubik client flags */
55 #define UPUBIKONLY          1    /* only check servers presumed functional */
56
57 /* RX services types */
58 #define VOTE_SERVICE_ID     50
59 #define DISK_SERVICE_ID     51
60 #define USER_SERVICE_ID     52          /* Since most applications use same port! */
61
62 #define UBIK_MAGIC      0x354545
63
64 /* global ubik parameters */
65 #define MAXSERVERS          20          /* max number of servers */
66
67 /* version comparison macro */
68 #define vcmp(a,b) ((a).epoch == (b).epoch? ((a).counter - (b).counter) : ((a).epoch - (b).epoch))
69
70 /* ubik_client state bits */
71 #define CFLastFailed        1           /* last call failed to this guy (to detect down hosts) */
72
73 #ifdef AFS_PTHREAD_ENV
74 #include <pthread.h>
75 #include <assert.h>
76 #endif
77
78 /* per-client structure for ubik */
79 struct ubik_client {
80     short initializationState;          /* ubik client init state */
81     short states[MAXSERVERS];           /* state bits */
82     struct rx_connection *conns[MAXSERVERS];
83     afs_int32  syncSite;
84 #ifdef AFS_PTHREAD_ENV
85     pthread_mutex_t cm;
86 #endif
87 };
88
89 #ifdef AFS_PTHREAD_ENV
90 #define LOCK_UBIK_CLIENT(client) assert(pthread_mutex_lock(&client->cm)==0);
91 #define UNLOCK_UBIK_CLIENT(client) assert(pthread_mutex_unlock(&client->cm)==0);
92 #else
93 #define LOCK_UBIK_CLIENT(client)
94 #define UNLOCK_UBIK_CLIENT(client)
95 #endif
96
97 #define ubik_GetRPCConn(astr,aindex)    ((aindex) >= MAXSERVERS? 0 : (astr)->conns[aindex])
98 #define ubik_GetRPCHost(astr,aindex)    ((aindex) >= MAXSERVERS? 0 : (astr)->hosts[aindex])
99
100 /* ubik transaction id representation */
101 struct ubik_tid {
102     afs_int32 epoch;        /* time this server started */
103     afs_int32 counter;   /* counter within epoch of transactions */
104 };
105
106 /* ubik version representation */
107 struct ubik_version {
108     afs_int32 epoch;        /* time this server started */
109     afs_int32 counter;   /* counter within epoch of transactions */
110 };
111
112 /* ubik header file structure */
113 struct ubik_hdr {
114     afs_int32 magic;                /* magic number */
115     short pad1;             /* some 0-initd padding */
116     short size;             /* header allocation size */
117     struct ubik_version version;    /* the version for this file */
118 };
119
120 /* representation of a ubik transaction */
121 struct ubik_trans {
122     struct ubik_dbase *dbase;       /* corresponding database */
123     struct ubik_trans *next;        /* in the list */
124     afs_int32 locktype;                 /* transaction lock */
125     struct ubik_trunc *activeTruncs;/* queued truncates */
126     struct ubik_tid tid;            /* transaction id of this trans (if write trans.) */
127     afs_int32 minCommitTime;                /* time before which this trans can't commit */
128     afs_int32 seekFile;             /* seek ptr: file number */
129     afs_int32 seekPos;              /* seek ptr: offset therein */
130     short flags;                    /* trans flag bits */
131     char type;                      /* type of trans */
132     iovec_wrt iovec_info;
133     iovec_buf iovec_data;
134 };
135
136 /* representation of a truncation operation */
137 struct ubik_trunc {
138     struct ubik_trunc *next;
139     afs_int32 file;                         /* file to truncate */
140     afs_int32 length;               /* new size */
141 };
142
143 struct ubik_stat {
144     afs_int32 size;
145     afs_int32 mtime;
146 };
147
148 #if defined(UKERNEL)
149 #include "../afs/lock.h"
150 #else /* defined(UKERNEL) */
151 #include <lock.h>                       /* just to make sure we've go this */
152 #endif /* defined(UKERNEL) */
153
154 /* representation of a ubik database.  Contains info on low-level disk access routines
155     for use by disk transaction module.
156 */
157 struct ubik_dbase {
158     char *pathName;                 /* root name for dbase */
159     struct ubik_trans *activeTrans; /* active transaction list */
160     struct ubik_version version;    /* version number */
161 #if defined(UKERNEL)
162     struct afs_lock versionLock;    /* lock on version number */
163 #else /* defined(UKERNEL) */
164     struct Lock versionLock;        /* lock on version number */
165 #endif /* defined(UKERNEL) */
166     afs_int32 tidCounter;                /* last RW or RO trans tid counter */
167     afs_int32 writeTidCounter;           /* last write trans tid counter */
168     afs_int32 flags;                        /* flags */
169     int (*read)();                  /* physio procedures */
170     int (*write)();
171     int (*truncate)();
172     int (*sync)();
173     int (*stat)();
174     int (*open)();
175     int (*setlabel)();              /* set the version label */
176     int (*getlabel)();              /* retrieve the version label */
177     int (*getnfiles)();             /* find out number of files */
178     short readers;                  /* number of current read transactions */
179     struct ubik_version cachedVersion; /* version of caller's cached data */
180 };
181
182 /* procedures for automatically authenticating ubik connections */
183 extern int (*ubik_CRXSecurityProc)();
184 extern char *ubik_CRXSecurityRock;
185 extern int (*ubik_SRXSecurityProc)();
186 extern char *ubik_SRXSecurityRock;
187 extern int (*ubik_CheckRXSecurityProc)();
188 extern char *ubik_CheckRXSecurityRock;
189
190 /****************INTERNALS BELOW ****************/
191
192 #ifdef UBIK_INTERNALS
193 /* some ubik parameters */
194 #ifdef  PAGESIZE
195 #undef  PAGESIZE
196 #endif
197 #define PAGESIZE            1024        /* fits in current r packet */
198 #define LOGPAGESIZE         10          /* base 2 log thereof */
199 #define NBUFFERS            20          /* number of 1K buffers */
200 #define HDRSIZE             64          /* bytes of header per dbfile */
201
202 /* ubik_dbase flags */
203 #define DBWRITING           1           /* are any write trans. in progress */
204
205 /* ubik trans flags */
206 #define TRDONE              1           /* commit or abort done */
207 #define TRABORT             2           /* if TRDONE, tells if aborted */
208 #define TRREADANY           4           /* read any data available in trans */
209
210 /* ubik_lock flags */
211 #define LWANT               1
212
213 /* ubik system database numbers */
214 #define LOGFILE             (-1)
215
216 /* define log opcodes */
217 #define LOGNEW              100     /* start transaction */
218 #define LOGEND              101     /* commit (good) end transaction */
219 #define LOGABORT            102     /* abort (fail) transaction */
220 #define LOGDATA             103     /* data */
221 #define LOGTRUNCATE         104     /* truncate operation */
222
223 /* time constant for replication algorithms: the R time period is 20 seconds.  Both SMALLTIME
224     and BIGTIME must be larger than RPCTIMEOUT+max(RPCTIMEOUT,POLLTIME),
225     so that timeouts do not prevent us from getting through to our servers in time.
226
227     We use multi-R to time out multiple down hosts concurrently.
228     The only other restrictions:  BIGTIME > SMALLTIME and
229     BIGTIME-SMALLTIME > MAXSKEW (the clock skew).
230 */
231 #define MAXSKEW 10
232 #define POLLTIME 15
233 #define RPCTIMEOUT 20
234 #define BIGTIME 75
235 #define SMALLTIME 60
236
237 /* the per-server state, used by the sync site to keep track of its charges */
238 struct ubik_server {
239     struct ubik_server *next;           /* next ptr */
240     afs_uint32 addr[UBIK_MAX_INTERFACE_ADDR];/* network order, addr[0] is primary*/
241     afs_int32 lastVoteTime;                     /* last time yes vote received */
242     afs_int32 lastBeaconSent;           /* last time beacon attempted */
243     struct ubik_version version;        /* version, only used during recovery */
244     struct rx_connection *vote_rxcid;   /* cid to use to contact dude for votes */
245     struct rx_connection *disk_rxcid;   /* cid to use to contact dude for disk reqs */
246     char lastVote;                      /* true if last vote was yes */
247     char up;                            /* is it up? */
248     char beaconSinceDown;               /* did beacon get through since last crash? */
249     char currentDB;                     /* is dbase up-to-date */
250     char magic;                         /* the one whose vote counts twice */
251     char isClone;                       /* is only a clone, doesn't vote */
252 };
253
254 /* hold and release functions on a database */
255 #define DBHOLD(a)       ObtainWriteLock(&((a)->versionLock))
256 #define DBRELE(a)       ReleaseWriteLock(&((a)->versionLock))
257
258 /* globals */
259
260 /* list of all servers in the system */
261 extern struct ubik_server *ubik_servers;
262 extern char amIClone;
263
264 /* network port info */
265 extern short ubik_callPortal;
266
267 /* urecovery state bits for sync site */
268 #define UBIK_RECSYNCSITE        1       /* am sync site */
269 #define UBIK_RECFOUNDDB         2       /* found acceptable dbase from quorum */
270 #define UBIK_RECHAVEDB          4       /* fetched best dbase */
271 #define UBIK_RECLABELDB         8       /* relabelled dbase */
272 #define UBIK_RECSENTDB          0x10    /* sent best db to *everyone* */
273 #define UBIK_RECSBETTER         UBIK_RECLABELDB /* last state */
274
275 extern afs_int32 ubik_quorum;                   /* min hosts in quorum */
276 extern struct ubik_dbase *ubik_dbase;           /* the database handled by this server */
277 extern afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];/* this host addr, in net order */
278 extern int ubik_amSyncSite;                     /* sleep on this waiting to be sync site */
279 extern struct ubik_stats {                      /* random stats */
280     afs_int32 escapes;
281 } ubik_stats;
282 extern afs_int32 ubik_epochTime;                        /* time when this site started */
283 extern afs_int32 urecovery_state;                       /* sync site recovery process state */
284 extern struct ubik_trans *ubik_currentTrans;    /* current trans */
285 extern struct ubik_version ubik_dbVersion;      /* sync site's dbase version */
286 extern afs_int32 ubik_debugFlag;                        /* ubik debug flag */
287 extern int ubikPrimaryAddrOnly;                 /* use only primary address */
288
289 /* this extern gives the sync site's db version, with epoch of 0 if none yet */
290
291 extern int uphys_read();
292 extern int uphys_write();
293 extern int uphys_truncate();
294 extern int uphys_sync();
295 /*
296  * This is static.
297 extern int uphys_open();
298  */
299 extern int uphys_stat();
300 extern int uphys_getlabel();
301 extern int uphys_setlabel();
302 extern int uphys_getnfiles();
303 extern int ubeacon_Interact();
304 extern int urecovery_Interact();
305 extern int sdisk_Interact();
306 extern int uvote_Interact();
307 extern int DISK_Abort();
308 extern int DISK_Begin();
309 extern int DISK_ReleaseLocks();
310 extern int DISK_Commit();
311 extern int DISK_Lock();
312 extern int DISK_Write();
313 extern int DISK_WriteV();
314 extern int DISK_Truncate();
315 extern int DISK_SetVersion();
316 #endif /* UBIK_INTERNALS */
317
318 extern afs_int32 ubik_nBuffers;
319
320 /*
321  * Public function prototypes
322  */
323
324 extern int ubik_ParseClientList(
325   int argc,
326   char **argv,
327   afs_int32 *aothers
328 );
329
330 extern unsigned int afs_random(
331   void
332 );
333
334 extern int ubik_ClientInit(
335   register struct rx_connection **serverconns,
336   struct ubik_client **aclient
337 );
338
339 extern afs_int32 ubik_ClientDestroy(
340     struct ubik_client *aclient
341 );
342
343
344 #endif /* UBIK_H */