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