2 * Copyright 2000, International Business Machines Corporation and others.
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
15 /* these are now appended by the error table compiler */
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 */
41 /*! \name ubik_trans types */
42 #define UBIK_READTRANS 0
43 #define UBIK_WRITETRANS 1
46 /*! \name ubik_lock types */
49 #if !defined(UBIK_PAUSE)
51 #endif /* UBIK_PAUSE */
54 /*! \name ubik client flags */
55 #define UPUBIKONLY 1 /*!< only check servers presumed functional */
56 #define UBIK_CALL_NEW 2 /*!< use the semantics of ubik_Call_New */
59 /*! \name RX services types */
60 #define VOTE_SERVICE_ID 50
61 #define DISK_SERVICE_ID 51
62 #define USER_SERVICE_ID 52 /*!< Since most applications use same port! */
65 #define UBIK_MAGIC 0x354545
67 /*! \name global ubik parameters */
68 #define MAXSERVERS 20 /*!< max number of servers */
71 /*! version comparison macro */
72 #define vcmp(a,b) ((a).epoch == (b).epoch? ((a).counter - (b).counter) : ((a).epoch - (b).epoch))
74 /*! \name ubik_client state bits */
75 #define CFLastFailed 1 /*!< last call failed to this guy (to detect down hosts) */
78 #ifdef AFS_PTHREAD_ENV
86 * \brief per-client structure for ubik
89 short initializationState; /*!< ubik client init state */
90 short states[MAXSERVERS]; /*!< state bits */
91 struct rx_connection *conns[MAXSERVERS];
93 #ifdef AFS_PTHREAD_ENV
98 #ifdef AFS_PTHREAD_ENV
99 #define LOCK_UBIK_CLIENT(client) assert(pthread_mutex_lock(&client->cm)==0)
100 #define UNLOCK_UBIK_CLIENT(client) assert(pthread_mutex_unlock(&client->cm)==0)
102 #define LOCK_UBIK_CLIENT(client)
103 #define UNLOCK_UBIK_CLIENT(client)
106 #define ubik_GetRPCConn(astr,aindex) ((aindex) >= MAXSERVERS? 0 : (astr)->conns[aindex])
107 #define ubik_GetRPCHost(astr,aindex) ((aindex) >= MAXSERVERS? 0 : (astr)->hosts[aindex])
110 * \brief ubik header file structure
113 afs_int32 magic; /*!< magic number */
114 short pad1; /*!< some 0-initd padding */
115 short size; /*!< header allocation size */
116 struct ubik_version version; /*!< the version for this file */
120 * \brief representation of a ubik transaction
123 struct ubik_dbase *dbase; /*!< corresponding database */
124 struct ubik_trans *next; /*!< in the list */
125 afs_int32 locktype; /*!< transaction lock */
126 struct ubik_trunc *activeTruncs; /*!< queued truncates */
127 struct ubik_tid tid; /*!< transaction id of this trans (if write trans.) */
128 afs_int32 minCommitTime; /*!< time before which this trans can't commit */
129 afs_int32 seekFile; /*!< seek ptr: file number */
130 afs_int32 seekPos; /*!< seek ptr: offset therein */
131 short flags; /*!< trans flag bits */
132 char type; /*!< type of trans */
133 iovec_wrt iovec_info;
134 iovec_buf iovec_data;
138 * \brief representation of a truncation operation
141 struct ubik_trunc *next;
142 afs_int32 file; /*!< file to truncate */
143 afs_int32 length; /*!< new size */
152 #include "afs/lock.h"
153 #else /* defined(UKERNEL) */
154 #include <lock.h> /* just to make sure we've got this */
155 #endif /* defined(UKERNEL) */
158 * \brief representation of a ubik database.
160 * Contains info on low-level disk access routines
161 * for use by disk transaction module.
164 char *pathName; /*!< root name for dbase */
165 struct ubik_trans *activeTrans; /*!< active transaction list */
166 struct ubik_version version; /*!< version number */
167 #ifdef AFS_PTHREAD_ENV
168 pthread_mutex_t versionLock; /*!< lock on version number */
169 #elif defined(UKERNEL)
170 struct afs_lock versionLock; /*!< lock on version number */
171 #else /* defined(UKERNEL) */
172 struct Lock versionLock; /*!< lock on version number */
173 #endif /* defined(UKERNEL) */
174 afs_int32 tidCounter; /*!< last RW or RO trans tid counter */
175 afs_int32 writeTidCounter; /*!< last write trans tid counter */
176 afs_int32 flags; /*!< flags */
177 /* physio procedures */
178 int (*read) (struct ubik_dbase * adbase, afs_int32 afile, void *abuffer,
179 afs_int32 apos, afs_int32 alength);
180 int (*write) (struct ubik_dbase * adbase, afs_int32 afile, void *abuffer,
181 afs_int32 apos, afs_int32 alength);
182 int (*truncate) (struct ubik_dbase * adbase, afs_int32 afile,
184 int (*sync) (struct ubik_dbase * adbase, afs_int32 afile);
185 int (*stat) (struct ubik_dbase * adbase, afs_int32 afid,
186 struct ubik_stat * astat);
187 void (*open) (struct ubik_dbase * adbase, afs_int32 afid);
188 int (*setlabel) (struct ubik_dbase * adbase, afs_int32 afile, struct ubik_version * aversion); /*!< set the version label */
189 int (*getlabel) (struct ubik_dbase * adbase, afs_int32 afile, struct ubik_version * aversion); /*!< retrieve the version label */
190 int (*getnfiles) (struct ubik_dbase * adbase); /*!< find out number of files */
191 short readers; /*!< number of current read transactions */
192 struct ubik_version cachedVersion; /*!< version of caller's cached data */
194 struct afs_lock cache_lock;
196 struct Lock cache_lock; /*!< protects cached application data */
198 #ifdef AFS_PTHREAD_ENV
199 pthread_cond_t version_cond; /*!< condition variable to manage changes to version */
200 pthread_cond_t flags_cond; /*!< condition variable to manage changes to flags */
205 * ubik_CheckCache callback function.
207 * @param[in] atrans ubik transaction
208 * @param[in] rock rock passed to ubik_CheckCache
210 * @return operation status
211 * @retval 0 cache was read properly
213 typedef int (*ubik_updatecache_func) (struct ubik_trans *atrans, void *rock);
215 /*! \name procedures for automatically authenticating ubik connections */
216 extern int (*ubik_CRXSecurityProc) (void *, struct rx_securityClass **,
218 extern void *ubik_CRXSecurityRock;
219 extern int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **,
221 extern void *ubik_SRXSecurityRock;
222 extern int (*ubik_CheckRXSecurityProc) (void *, struct rx_call *);
223 extern void *ubik_CheckRXSecurityRock;
227 * For applications that make use of ubik_BeginTransReadAnyWrite, writing
228 * processes must not update the application-level cache as they write,
229 * or else readers can read the new cache before the data is committed to
230 * the db. So, when a commit occurs, the cache must be updated right then.
231 * If set, this function will be called during commits of write transactions,
232 * to update the application-level cache after a write. This will be called
233 * immediately after the local disk commit succeeds, and it will be called
234 * with a lock held that prevents other threads from reading from the cache
235 * or the db in general.
237 * Note that this function MUST be set in order to make use of
238 * ubik_BeginTransReadAnyWrite.
240 extern int (*ubik_SyncWriterCacheProc) (void);
242 /****************INTERNALS BELOW ****************/
244 #ifdef UBIK_INTERNALS
245 /*! \name some ubik parameters */
246 #define UBIK_PAGESIZE 1024 /*!< fits in current r packet */
247 #define UBIK_LOGPAGESIZE 10 /*!< base 2 log thereof */
248 #define NBUFFERS 20 /*!< number of 1K buffers */
249 #define HDRSIZE 64 /*!< bytes of header per dbfile */
252 /*! \name ubik_dbase flags */
253 #define DBWRITING 1 /*!< are any write trans. in progress */
254 #if defined(UBIK_PAUSE)
255 #define DBVOTING 2 /*!< the beacon task is polling */
256 #endif /* UBIK_PAUSE */
259 /*!\name ubik trans flags */
260 #define TRDONE 1 /*!< commit or abort done */
261 #define TRABORT 2 /*!< if #TRDONE, tells if aborted */
262 #define TRREADANY 4 /*!< read any data available in trans */
263 #if defined(UBIK_PAUSE)
264 #define TRSETLOCK 8 /*!< SetLock is using trans */
265 #define TRSTALE 16 /*!< udisk_end during getLock */
266 #endif /* UBIK_PAUSE */
267 #define TRCACHELOCKED 32 /*!< this trans has locked dbase->cache_lock
268 * (meaning, this trans has called
269 * ubik_CheckCache at some point */
270 #define TRREADWRITE 64 /*!< read even if there's a conflicting ubik-
271 * level write lock */
274 /*! \name ubik_lock flags */
278 /*! \name ubik system database numbers */
282 /*! \name define log opcodes */
283 #define LOGNEW 100 /*!< start transaction */
284 #define LOGEND 101 /*!< commit (good) end transaction */
285 #define LOGABORT 102 /*!< abort (fail) transaction */
286 #define LOGDATA 103 /*!< data */
287 #define LOGTRUNCATE 104 /*!< truncate operation */
291 * \name timer constants
292 * time constant for replication algorithms: the R time period is 20 seconds. Both
293 * #SMALLTIME and #BIGTIME must be larger than #RPCTIMEOUT+max(#RPCTIMEOUT, #POLLTIME),
294 * so that timeouts do not prevent us from getting through to our servers in time.
296 * We use multi-R to time out multiple down hosts concurrently.
297 * The only other restrictions: #BIGTIME > #SMALLTIME and
298 * #BIGTIME-#SMALLTIME > #MAXSKEW (the clock skew).
302 #define RPCTIMEOUT 20
308 * \brief the per-server state, used by the sync site to keep track of its charges
311 struct ubik_server *next; /*!< next ptr */
312 afs_uint32 addr[UBIK_MAX_INTERFACE_ADDR]; /*!< network order, addr[0] is primary */
313 afs_int32 lastVoteTime; /*!< last time yes vote received */
314 afs_int32 lastBeaconSent; /*!< last time beacon attempted */
315 struct ubik_version version; /*!< version, only used during recovery */
316 struct rx_connection *vote_rxcid; /*!< cid to use to contact dude for votes */
317 struct rx_connection *disk_rxcid; /*!< cid to use to contact dude for disk reqs */
318 char lastVote; /*!< true if last vote was yes */
319 char up; /*!< is it up? */
320 char beaconSinceDown; /*!< did beacon get through since last crash? */
321 char currentDB; /*!< is dbase up-to-date */
322 char magic; /*!< the one whose vote counts twice */
323 char isClone; /*!< is only a clone, doesn't vote */
326 /*! \name hold and release functions on a database */
327 #ifdef AFS_PTHREAD_ENV
328 # define DBHOLD(a) assert(pthread_mutex_lock(&((a)->versionLock)) == 0)
329 # define DBRELE(a) assert(pthread_mutex_unlock(&((a)->versionLock)) == 0)
330 #else /* !AFS_PTHREAD_ENV */
331 # define DBHOLD(a) ObtainWriteLock(&((a)->versionLock))
332 # define DBRELE(a) ReleaseWriteLock(&((a)->versionLock))
333 #endif /* !AFS_PTHREAD_ENV */
338 /*!name list of all servers in the system */
339 extern struct ubik_server *ubik_servers;
340 extern char amIClone;
343 /*! \name network port info */
344 extern short ubik_callPortal;
347 /*! \name urecovery state bits for sync site */
348 #define UBIK_RECSYNCSITE 1 /* am sync site */
349 #define UBIK_RECFOUNDDB 2 /* found acceptable dbase from quorum */
350 #define UBIK_RECHAVEDB 4 /* fetched best dbase */
351 #define UBIK_RECLABELDB 8 /* relabelled dbase */
352 #define UBIK_RECSENTDB 0x10 /* sent best db to *everyone* */
353 #define UBIK_RECSBETTER UBIK_RECLABELDB /* last state */
356 extern afs_int32 ubik_quorum; /* min hosts in quorum */
357 extern struct ubik_dbase *ubik_dbase; /* the database handled by this server */
358 extern afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR]; /* this host addr, in net order */
359 extern int ubik_amSyncSite; /* sleep on this waiting to be sync site */
360 extern struct ubik_stats { /* random stats */
363 extern afs_int32 ubik_epochTime; /* time when this site started */
364 extern afs_int32 urecovery_state; /* sync site recovery process state */
365 extern struct ubik_trans *ubik_currentTrans; /* current trans */
366 extern struct ubik_version ubik_dbVersion; /* sync site's dbase version */
367 extern afs_int32 ubik_debugFlag; /* ubik debug flag */
368 extern int ubikPrimaryAddrOnly; /* use only primary address */
370 /* this extern gives the sync site's db version, with epoch of 0 if none yet */
373 extern int uphys_close(int afd);
374 extern int uphys_stat(struct ubik_dbase *adbase, afs_int32 afid,
375 struct ubik_stat *astat);
376 extern int uphys_read(struct ubik_dbase *adbase, afs_int32 afile,
377 void *abuffer, afs_int32 apos,
379 extern int uphys_write(struct ubik_dbase *adbase, afs_int32 afile,
380 void *abuffer, afs_int32 apos,
382 extern int uphys_truncate(struct ubik_dbase *adbase, afs_int32 afile,
384 extern int uphys_getnfiles(struct ubik_dbase *adbase);
385 extern int uphys_getlabel(struct ubik_dbase *adbase, afs_int32 afile,
386 struct ubik_version *aversion);
387 extern int uphys_setlabel(struct ubik_dbase *adbase, afs_int32 afile,
388 struct ubik_version *aversion);
389 extern int uphys_sync(struct ubik_dbase *adbase, afs_int32 afile);
390 extern void uphys_invalidate(struct ubik_dbase *adbase,
393 /*! \name recovery.c */
394 extern int urecovery_ResetState(void);
395 extern int urecovery_LostServer(void);
396 extern int urecovery_AllBetter(struct ubik_dbase *adbase,
398 extern int urecovery_AbortAll(struct ubik_dbase *adbase);
399 extern int urecovery_CheckTid(struct ubik_tid *atid);
400 extern int urecovery_Initialize(struct ubik_dbase *adbase);
401 extern void *urecovery_Interact(void *);
402 extern int DoProbe(struct ubik_server *server);
406 extern afs_int32 ContactQuorum_NoArguments(afs_int32 (*proc)
407 (struct rx_connection *,
409 struct ubik_trans *atrans,
412 extern afs_int32 ContactQuorum_DISK_Lock(struct ubik_trans *atrans,
414 afs_int32 file, afs_int32 position,
415 afs_int32 length, afs_int32 type);
417 extern afs_int32 ContactQuorum_DISK_Write(struct ubik_trans *atrans,
419 afs_int32 file, afs_int32 position,
422 extern afs_int32 ContactQuorum_DISK_Truncate(struct ubik_trans *atrans,
424 afs_int32 file, afs_int32 length);
426 extern afs_int32 ContactQuorum_DISK_WriteV(struct ubik_trans *atrans,
428 iovec_wrt * io_vector,
429 iovec_buf *io_buffer);
431 extern afs_int32 ContactQuorum_DISK_SetVersion(struct ubik_trans *atrans,
433 ubik_version *OldVersion,
434 ubik_version *NewVersion);
436 extern void panic(char *format, ...)
437 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
439 extern afs_uint32 ubikGetPrimaryInterfaceAddr(afs_uint32 addr);
442 /*! \name beacon.c */
444 extern void ubeacon_Debug(struct ubik_debug *aparm);
445 extern int ubeacon_AmSyncSite(void);
446 extern int ubeacon_InitServerListByInfo(afs_uint32 ame,
447 struct afsconf_cell *info,
449 extern int ubeacon_InitServerList(afs_uint32 ame, afs_uint32 aservers[]);
450 extern void *ubeacon_Interact(void *);
454 extern void udisk_Debug(struct ubik_debug *aparm);
455 extern int udisk_Invalidate(struct ubik_dbase *adbase, afs_int32 afid);
456 extern int udisk_read(struct ubik_trans *atrans, afs_int32 afile,
457 void *abuffer, afs_int32 apos, afs_int32 alen);
458 extern int udisk_truncate(struct ubik_trans *atrans, afs_int32 afile,
460 extern int udisk_write(struct ubik_trans *atrans, afs_int32 afile,
461 void *abuffer, afs_int32 apos, afs_int32 alen);
462 extern int udisk_begin(struct ubik_dbase *adbase, int atype,
463 struct ubik_trans **atrans);
464 extern int udisk_commit(struct ubik_trans *atrans);
465 extern int udisk_abort(struct ubik_trans *atrans);
466 extern int udisk_end(struct ubik_trans *atrans);
470 extern int ulock_getLock(struct ubik_trans *atrans, int atype, int await);
471 extern void ulock_relLock(struct ubik_trans *atrans);
472 extern void ulock_Debug(struct ubik_debug *aparm);
476 extern int uvote_ShouldIRun(void);
477 extern afs_int32 uvote_GetSyncSite(void);
478 extern int uvote_Init(void);
479 extern void ubik_vprint(const char *format, va_list ap)
480 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 0);
482 extern void ubik_print(const char *format, ...)
483 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
485 extern void ubik_dprint(const char *format, ...)
486 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
488 extern void ubik_dprint_25(const char *format, ...)
489 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
492 #endif /* UBIK_INTERNALS */
494 extern afs_int32 ubik_nBuffers;
497 * \name Public function prototypes
502 extern int ubik_ServerInitByInfo(afs_uint32 myHost, short myPort,
503 struct afsconf_cell *info, char clones[],
504 const char *pathName,
505 struct ubik_dbase **dbase);
506 extern int ubik_ServerInit(afs_uint32 myHost, short myPort,
507 afs_uint32 serverList[],
508 const char *pathName, struct ubik_dbase **dbase);
509 extern int ubik_BeginTrans(struct ubik_dbase *dbase,
510 afs_int32 transMode, struct ubik_trans **transPtr);
511 extern int ubik_BeginTransReadAny(struct ubik_dbase *dbase,
513 struct ubik_trans **transPtr);
514 extern int ubik_BeginTransReadAnyWrite(struct ubik_dbase *dbase,
516 struct ubik_trans **transPtr);
517 extern int ubik_AbortTrans(struct ubik_trans *transPtr);
519 extern int ubik_EndTrans(struct ubik_trans *transPtr);
520 extern int ubik_Read(struct ubik_trans *transPtr, void *buffer,
522 extern int ubik_Flush(struct ubik_trans *transPtr);
523 extern int ubik_Write(struct ubik_trans *transPtr, void *buffer,
525 extern int ubik_Seek(struct ubik_trans *transPtr, afs_int32 fileid,
527 extern int ubik_Tell(struct ubik_trans *transPtr, afs_int32 * fileid,
528 afs_int32 * position);
529 extern int ubik_Truncate(struct ubik_trans *transPtr,
531 extern int ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos,
532 afs_int32 alen, int atype);
533 extern int ubik_WaitVersion(struct ubik_dbase *adatabase,
534 struct ubik_version *aversion);
535 extern int ubik_GetVersion(struct ubik_trans *atrans,
536 struct ubik_version *avers);
537 extern int ubik_CheckCache(struct ubik_trans *atrans,
538 ubik_updatecache_func check,
542 /*! \name ubikclient.c */
544 extern int ubik_ParseClientList(int argc, char **argv, afs_uint32 * aothers);
545 extern unsigned int afs_random(void);
546 extern int ubik_ClientInit(struct rx_connection **serverconns,
547 struct ubik_client **aclient);
548 extern afs_int32 ubik_ClientDestroy(struct ubik_client *aclient);
549 extern struct rx_connection *ubik_RefreshConn(struct rx_connection *tc);
550 #ifdef UBIK_LEGACY_CALLITER
551 extern afs_int32 ubik_CallIter(int (*aproc) (), struct ubik_client *aclient,
552 afs_int32 aflags, int *apos, long p1, long p2,
553 long p3, long p4, long p5, long p6, long p7,
554 long p8, long p9, long p10, long p11, long p12,
555 long p13, long p14, long p15, long p16);
556 extern afs_int32 ubik_Call_New(int (*aproc) (), struct ubik_client
557 *aclient, afs_int32 aflags, long p1, long p2,
558 long p3, long p4, long p5, long p6, long p7,
559 long p8, long p9, long p10, long p11, long p12,
560 long p13, long p14, long p15, long p16);
564 /* \name ubikcmd.c */
565 extern int ubik_ParseServerList(int argc, char **argv, afs_uint32 *ahost,
566 afs_uint32 *aothers);
571 struct rx_securityClass;
572 extern afs_int32 ugen_ClientInit(int noAuthFlag, const char *confDir,
573 char *cellName, afs_int32 sauth,
574 struct ubik_client **uclientp,
575 int (*secproc) (struct rx_securityClass *sc,
578 afs_int32 gen_rxkad_level,
579 afs_int32 maxservers, char *serviceid,
580 afs_int32 deadtime, afs_uint32 server,
581 afs_uint32 port, afs_int32 usrvid);