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
17 /*! \name ubik_trans types */
18 #define UBIK_READTRANS 0
19 #define UBIK_WRITETRANS 1
22 /*! \name ubik_lock types */
28 /*! \name ubik client flags */
29 #define UPUBIKONLY 1 /*!< only check servers presumed functional */
30 #define UBIK_CALL_NEW 2 /*!< use the semantics of ubik_Call_New */
33 /*! \name RX services types */
34 #define VOTE_SERVICE_ID 50
35 #define DISK_SERVICE_ID 51
36 #define USER_SERVICE_ID 52 /*!< Since most applications use same port! */
39 #define UBIK_MAGIC 0x354545
41 /*! \name global ubik parameters */
42 #define MAXSERVERS 20 /*!< max number of servers */
45 /*! version comparison macro */
46 #define vcmp(a,b) ((a).epoch == (b).epoch? ((a).counter - (b).counter) : ((a).epoch - (b).epoch))
48 /*! \name ubik_client state bits */
49 #define CFLastFailed 1 /*!< last call failed to this guy (to detect down hosts) */
52 #include <afs/afs_assert.h>
53 #ifdef AFS_PTHREAD_ENV
60 * \brief per-client structure for ubik
63 short initializationState; /*!< ubik client init state */
64 short states[MAXSERVERS]; /*!< state bits */
65 struct rx_connection *conns[MAXSERVERS];
67 #ifdef AFS_PTHREAD_ENV
72 #ifdef AFS_PTHREAD_ENV
73 #define LOCK_UBIK_CLIENT(client) MUTEX_ENTER(&client->cm)
74 #define UNLOCK_UBIK_CLIENT(client) MUTEX_EXIT(&client->cm)
76 #define LOCK_UBIK_CLIENT(client)
77 #define UNLOCK_UBIK_CLIENT(client)
80 #define ubik_GetRPCConn(astr,aindex) ((aindex) >= MAXSERVERS? 0 : (astr)->conns[aindex])
81 #define ubik_GetRPCHost(astr,aindex) ((aindex) >= MAXSERVERS? 0 : (astr)->hosts[aindex])
84 * \brief ubik header file structure
87 afs_int32 magic; /*!< magic number */
88 short pad1; /*!< some 0-initd padding */
89 short size; /*!< header allocation size */
90 struct ubik_version version; /*!< the version for this file */
94 * \brief representation of a ubik transaction
97 struct ubik_dbase *dbase; /*!< corresponding database */
98 struct ubik_trans *next; /*!< in the list */
99 afs_int32 locktype; /*!< transaction lock */
100 struct ubik_trunc *activeTruncs; /*!< queued truncates */
101 struct ubik_tid tid; /*!< transaction id of this trans (if write trans.) */
102 afs_int32 minCommitTime; /*!< time before which this trans can't commit */
103 afs_int32 seekFile; /*!< seek ptr: file number */
104 afs_int32 seekPos; /*!< seek ptr: offset therein */
105 short flags; /*!< trans flag bits */
106 char type; /*!< type of trans */
107 iovec_wrt iovec_info;
108 iovec_buf iovec_data;
112 * \brief representation of a truncation operation
115 struct ubik_trunc *next;
116 afs_int32 file; /*!< file to truncate */
117 afs_int32 length; /*!< new size */
126 #include "afs/lock.h"
127 #else /* defined(UKERNEL) */
128 #include <lock.h> /* just to make sure we've got this */
129 #endif /* defined(UKERNEL) */
132 * \brief representation of a ubik database.
134 * Contains info on low-level disk access routines
135 * for use by disk transaction module.
138 char *pathName; /*!< root name for dbase */
139 struct ubik_trans *activeTrans; /*!< active transaction list */
140 struct ubik_version version; /*!< version number */
141 #ifdef AFS_PTHREAD_ENV
142 pthread_mutex_t versionLock; /*!< lock on version number */
143 #elif defined(UKERNEL)
144 struct afs_lock versionLock; /*!< lock on version number */
145 #else /* defined(UKERNEL) */
146 struct Lock versionLock; /*!< lock on version number */
147 #endif /* defined(UKERNEL) */
148 afs_int32 tidCounter; /*!< last RW or RO trans tid counter */
149 afs_int32 writeTidCounter; /*!< last write trans tid counter */
150 afs_int32 flags; /*!< flags */
151 /* physio procedures */
152 int (*read) (struct ubik_dbase * adbase, afs_int32 afile, void *abuffer,
153 afs_int32 apos, afs_int32 alength);
154 int (*write) (struct ubik_dbase * adbase, afs_int32 afile, void *abuffer,
155 afs_int32 apos, afs_int32 alength);
156 int (*truncate) (struct ubik_dbase * adbase, afs_int32 afile,
158 int (*sync) (struct ubik_dbase * adbase, afs_int32 afile);
159 int (*stat) (struct ubik_dbase * adbase, afs_int32 afid,
160 struct ubik_stat * astat);
161 void (*open) (struct ubik_dbase * adbase, afs_int32 afid);
162 int (*setlabel) (struct ubik_dbase * adbase, afs_int32 afile, struct ubik_version * aversion); /*!< set the version label */
163 int (*getlabel) (struct ubik_dbase * adbase, afs_int32 afile, struct ubik_version * aversion); /*!< retrieve the version label */
164 int (*getnfiles) (struct ubik_dbase * adbase); /*!< find out number of files */
165 short readers; /*!< number of current read transactions */
166 struct ubik_version cachedVersion; /*!< version of caller's cached data */
168 struct afs_lock cache_lock;
170 struct Lock cache_lock; /*!< protects cached application data */
172 #ifdef AFS_PTHREAD_ENV
173 pthread_cond_t version_cond; /*!< condition variable to manage changes to version */
174 pthread_cond_t flags_cond; /*!< condition variable to manage changes to flags */
179 * ubik_CheckCache callback function.
181 * @param[in] atrans ubik transaction
182 * @param[in] rock rock passed to ubik_CheckCache
184 * @return operation status
185 * @retval 0 cache was read properly
187 typedef int (*ubik_updatecache_func) (struct ubik_trans *atrans, void *rock);
189 /*! \name procedures for automatically authenticating ubik connections */
190 extern int (*ubik_CRXSecurityProc) (void *, struct rx_securityClass **,
192 extern void *ubik_CRXSecurityRock;
193 extern int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **,
195 extern void *ubik_SRXSecurityRock;
196 extern int (*ubik_CheckRXSecurityProc) (void *, struct rx_call *);
197 extern void *ubik_CheckRXSecurityRock;
199 extern void ubik_SetClientSecurityProcs(int (*scproc)(void *,
200 struct rx_securityClass **,
202 int (*checkproc) (void *),
204 extern void ubik_SetServerSecurityProcs
205 (void (*buildproc) (void *,
206 struct rx_securityClass ***,
208 int (*checkproc) (void *, struct rx_call *),
214 * For applications that make use of ubik_BeginTransReadAnyWrite, writing
215 * processes must not update the application-level cache as they write,
216 * or else readers can read the new cache before the data is committed to
217 * the db. So, when a commit occurs, the cache must be updated right then.
218 * If set, this function will be called during commits of write transactions,
219 * to update the application-level cache after a write. This will be called
220 * immediately after the local disk commit succeeds, and it will be called
221 * with a lock held that prevents other threads from reading from the cache
222 * or the db in general.
224 * Note that this function MUST be set in order to make use of
225 * ubik_BeginTransReadAnyWrite.
227 extern int (*ubik_SyncWriterCacheProc) (void);
229 /****************INTERNALS BELOW ****************/
231 #ifdef UBIK_INTERNALS
232 /*! \name some ubik parameters */
233 #define UBIK_PAGESIZE 1024 /*!< fits in current r packet */
234 #define UBIK_LOGPAGESIZE 10 /*!< base 2 log thereof */
235 #define NBUFFERS 20 /*!< number of 1K buffers */
236 #define HDRSIZE 64 /*!< bytes of header per dbfile */
239 /*! \name ubik_dbase flags */
240 #define DBWRITING 1 /*!< are any write trans. in progress */
243 /*!\name ubik trans flags */
244 #define TRDONE 1 /*!< commit or abort done */
245 #define TRABORT 2 /*!< if #TRDONE, tells if aborted */
246 #define TRREADANY 4 /*!< read any data available in trans */
247 #define TRCACHELOCKED 32 /*!< this trans has locked dbase->cache_lock
248 * (meaning, this trans has called
249 * ubik_CheckCache at some point */
250 #define TRREADWRITE 64 /*!< read even if there's a conflicting ubik-
251 * level write lock */
254 /*! \name ubik_lock flags */
258 /*! \name ubik system database numbers */
262 /*! \name define log opcodes */
263 #define LOGNEW 100 /*!< start transaction */
264 #define LOGEND 101 /*!< commit (good) end transaction */
265 #define LOGABORT 102 /*!< abort (fail) transaction */
266 #define LOGDATA 103 /*!< data */
267 #define LOGTRUNCATE 104 /*!< truncate operation */
271 * \name timer constants
272 * time constant for replication algorithms: the R time period is 20 seconds. Both
273 * #SMALLTIME and #BIGTIME must be larger than #RPCTIMEOUT+max(#RPCTIMEOUT, #POLLTIME),
274 * so that timeouts do not prevent us from getting through to our servers in time.
276 * We use multi-R to time out multiple down hosts concurrently.
277 * The only other restrictions: #BIGTIME > #SMALLTIME and
278 * #BIGTIME-#SMALLTIME > #MAXSKEW (the clock skew).
282 #define RPCTIMEOUT 20
288 * \brief the per-server state, used by the sync site to keep track of its charges
291 struct ubik_server *next; /*!< next ptr */
292 afs_uint32 addr[UBIK_MAX_INTERFACE_ADDR]; /*!< network order, addr[0] is primary */
293 afs_int32 lastVoteTime; /*!< last time yes vote received */
294 afs_int32 lastBeaconSent; /*!< last time beacon attempted */
295 struct ubik_version version; /*!< version, only used during recovery */
296 struct rx_connection *vote_rxcid; /*!< cid to use to contact dude for votes */
297 struct rx_connection *disk_rxcid; /*!< cid to use to contact dude for disk reqs */
298 char lastVote; /*!< true if last vote was yes */
299 char up; /*!< is it up? */
300 char beaconSinceDown; /*!< did beacon get through since last crash? */
301 char currentDB; /*!< is dbase up-to-date */
302 char magic; /*!< the one whose vote counts twice */
303 char isClone; /*!< is only a clone, doesn't vote */
306 /*! \name hold and release functions on a database */
307 #ifdef AFS_PTHREAD_ENV
308 # define DBHOLD(a) MUTEX_ENTER(&((a)->versionLock))
309 # define DBRELE(a) MUTEX_EXIT(&((a)->versionLock))
310 #else /* !AFS_PTHREAD_ENV */
311 # define DBHOLD(a) ObtainWriteLock(&((a)->versionLock))
312 # define DBRELE(a) ReleaseWriteLock(&((a)->versionLock))
313 #endif /* !AFS_PTHREAD_ENV */
318 /*!name list of all servers in the system */
319 extern struct ubik_server *ubik_servers;
320 extern char amIClone;
323 /*! \name network port info */
324 extern short ubik_callPortal;
327 /*! \name urecovery state bits for sync site */
328 #define UBIK_RECSYNCSITE 1 /* am sync site */
329 #define UBIK_RECFOUNDDB 2 /* found acceptable dbase from quorum */
330 #define UBIK_RECHAVEDB 4 /* fetched best dbase */
331 #define UBIK_RECLABELDB 8 /* relabelled dbase */
332 #define UBIK_RECSENTDB 0x10 /* sent best db to *everyone* */
333 #define UBIK_RECSBETTER UBIK_RECLABELDB /* last state */
336 extern afs_int32 ubik_quorum; /* min hosts in quorum */
337 extern struct ubik_dbase *ubik_dbase; /* the database handled by this server */
338 extern afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR]; /* this host addr, in net order */
339 extern int ubik_amSyncSite; /* sleep on this waiting to be sync site */
340 extern struct ubik_stats { /* random stats */
343 extern afs_int32 ubik_epochTime; /* time when this site started */
344 extern afs_int32 urecovery_state; /* sync site recovery process state */
345 extern struct ubik_trans *ubik_currentTrans; /* current trans */
346 extern struct ubik_version ubik_dbVersion; /* sync site's dbase version */
347 extern afs_int32 ubik_debugFlag; /* ubik debug flag */
348 extern int ubikPrimaryAddrOnly; /* use only primary address */
350 /* this extern gives the sync site's db version, with epoch of 0 if none yet */
353 extern int uphys_stat(struct ubik_dbase *adbase, afs_int32 afid,
354 struct ubik_stat *astat);
355 extern int uphys_read(struct ubik_dbase *adbase, afs_int32 afile,
356 void *abuffer, afs_int32 apos,
358 extern int uphys_write(struct ubik_dbase *adbase, afs_int32 afile,
359 void *abuffer, afs_int32 apos,
361 extern int uphys_truncate(struct ubik_dbase *adbase, afs_int32 afile,
363 extern int uphys_getnfiles(struct ubik_dbase *adbase);
364 extern int uphys_getlabel(struct ubik_dbase *adbase, afs_int32 afile,
365 struct ubik_version *aversion);
366 extern int uphys_setlabel(struct ubik_dbase *adbase, afs_int32 afile,
367 struct ubik_version *aversion);
368 extern int uphys_sync(struct ubik_dbase *adbase, afs_int32 afile);
369 extern void uphys_invalidate(struct ubik_dbase *adbase,
372 /*! \name recovery.c */
373 extern int urecovery_ResetState(void);
374 extern int urecovery_LostServer(struct ubik_server *server);
375 extern int urecovery_AllBetter(struct ubik_dbase *adbase,
377 extern int urecovery_AbortAll(struct ubik_dbase *adbase);
378 extern int urecovery_CheckTid(struct ubik_tid *atid, int abortalways);
379 extern int urecovery_Initialize(struct ubik_dbase *adbase);
380 extern void *urecovery_Interact(void *);
381 extern int DoProbe(struct ubik_server *server);
385 extern afs_int32 ContactQuorum_NoArguments(afs_int32 (*proc)
386 (struct rx_connection *,
388 struct ubik_trans *atrans,
391 extern afs_int32 ContactQuorum_DISK_Lock(struct ubik_trans *atrans,
393 afs_int32 file, afs_int32 position,
394 afs_int32 length, afs_int32 type);
396 extern afs_int32 ContactQuorum_DISK_Write(struct ubik_trans *atrans,
398 afs_int32 file, afs_int32 position,
401 extern afs_int32 ContactQuorum_DISK_Truncate(struct ubik_trans *atrans,
403 afs_int32 file, afs_int32 length);
405 extern afs_int32 ContactQuorum_DISK_WriteV(struct ubik_trans *atrans,
407 iovec_wrt * io_vector,
408 iovec_buf *io_buffer);
410 extern afs_int32 ContactQuorum_DISK_SetVersion(struct ubik_trans *atrans,
412 ubik_version *OldVersion,
413 ubik_version *NewVersion);
415 extern void panic(char *format, ...)
416 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
418 extern afs_uint32 ubikGetPrimaryInterfaceAddr(afs_uint32 addr);
420 extern int ubik_CheckAuth(struct rx_call *);
424 /*! \name beacon.c */
426 extern void ubeacon_InitSecurityClass(void);
427 extern void ubeacon_ReinitServer(struct ubik_server *ts);
428 extern void ubeacon_Debug(struct ubik_debug *aparm);
429 extern int ubeacon_AmSyncSite(void);
430 extern int ubeacon_InitServerListByInfo(afs_uint32 ame,
431 struct afsconf_cell *info,
433 extern int ubeacon_InitServerList(afs_uint32 ame, afs_uint32 aservers[]);
434 extern void *ubeacon_Interact(void *);
438 extern int udisk_Init(int nBUffers);
439 extern void udisk_Debug(struct ubik_debug *aparm);
440 extern int udisk_Invalidate(struct ubik_dbase *adbase, afs_int32 afid);
441 extern int udisk_read(struct ubik_trans *atrans, afs_int32 afile,
442 void *abuffer, afs_int32 apos, afs_int32 alen);
443 extern int udisk_truncate(struct ubik_trans *atrans, afs_int32 afile,
445 extern int udisk_write(struct ubik_trans *atrans, afs_int32 afile,
446 void *abuffer, afs_int32 apos, afs_int32 alen);
447 extern int udisk_begin(struct ubik_dbase *adbase, int atype,
448 struct ubik_trans **atrans);
449 extern int udisk_commit(struct ubik_trans *atrans);
450 extern int udisk_abort(struct ubik_trans *atrans);
451 extern int udisk_end(struct ubik_trans *atrans);
455 extern void ulock_Init(void);
456 extern int ulock_getLock(struct ubik_trans *atrans, int atype, int await);
457 extern void ulock_relLock(struct ubik_trans *atrans);
458 extern void ulock_Debug(struct ubik_debug *aparm);
462 extern int uvote_ShouldIRun(void);
463 extern afs_int32 uvote_GetSyncSite(void);
464 extern int uvote_Init(void);
465 extern void ubik_vprint(const char *format, va_list ap)
466 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 0);
468 extern void ubik_print(const char *format, ...)
469 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
471 extern void ubik_dprint(const char *format, ...)
472 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
474 extern void ubik_dprint_25(const char *format, ...)
475 AFS_ATTRIBUTE_FORMAT(__printf__, 1, 2);
478 #endif /* UBIK_INTERNALS */
480 extern afs_int32 ubik_nBuffers;
483 * \name Public function prototypes
488 extern int ubik_ServerInitByInfo(afs_uint32 myHost, short myPort,
489 struct afsconf_cell *info, char clones[],
490 const char *pathName,
491 struct ubik_dbase **dbase);
492 extern int ubik_ServerInit(afs_uint32 myHost, short myPort,
493 afs_uint32 serverList[],
494 const char *pathName, struct ubik_dbase **dbase);
495 extern int ubik_BeginTrans(struct ubik_dbase *dbase,
496 afs_int32 transMode, struct ubik_trans **transPtr);
497 extern int ubik_BeginTransReadAny(struct ubik_dbase *dbase,
499 struct ubik_trans **transPtr);
500 extern int ubik_BeginTransReadAnyWrite(struct ubik_dbase *dbase,
502 struct ubik_trans **transPtr);
503 extern int ubik_AbortTrans(struct ubik_trans *transPtr);
505 extern int ubik_EndTrans(struct ubik_trans *transPtr);
506 extern int ubik_Read(struct ubik_trans *transPtr, void *buffer,
508 extern int ubik_Flush(struct ubik_trans *transPtr);
509 extern int ubik_Write(struct ubik_trans *transPtr, void *buffer,
511 extern int ubik_Seek(struct ubik_trans *transPtr, afs_int32 fileid,
513 extern int ubik_Tell(struct ubik_trans *transPtr, afs_int32 * fileid,
514 afs_int32 * position);
515 extern int ubik_Truncate(struct ubik_trans *transPtr,
517 extern int ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos,
518 afs_int32 alen, int atype);
519 extern int ubik_WaitVersion(struct ubik_dbase *adatabase,
520 struct ubik_version *aversion);
521 extern int ubik_GetVersion(struct ubik_trans *atrans,
522 struct ubik_version *avers);
523 extern int ubik_CheckCache(struct ubik_trans *atrans,
524 ubik_updatecache_func check,
528 /*! \name ubikclient.c */
530 extern int ubik_ParseClientList(int argc, char **argv, afs_uint32 * aothers);
531 extern unsigned int afs_random(void);
532 extern int ubik_ClientInit(struct rx_connection **serverconns,
533 struct ubik_client **aclient);
534 extern afs_int32 ubik_ClientDestroy(struct ubik_client *aclient);
535 extern struct rx_connection *ubik_RefreshConn(struct rx_connection *tc);
536 #ifdef UBIK_LEGACY_CALLITER
537 extern afs_int32 ubik_CallIter(int (*aproc) (), struct ubik_client *aclient,
538 afs_int32 aflags, int *apos, long p1, long p2,
539 long p3, long p4, long p5, long p6, long p7,
540 long p8, long p9, long p10, long p11, long p12,
541 long p13, long p14, long p15, long p16);
542 extern afs_int32 ubik_Call_New(int (*aproc) (), struct ubik_client
543 *aclient, afs_int32 aflags, long p1, long p2,
544 long p3, long p4, long p5, long p6, long p7,
545 long p8, long p9, long p10, long p11, long p12,
546 long p13, long p14, long p15, long p16);
550 /* \name ubikcmd.c */
551 extern int ubik_ParseServerList(int argc, char **argv, afs_uint32 *ahost,
552 afs_uint32 *aothers);
557 struct rx_securityClass;
558 extern afs_int32 ugen_ClientInit(int noAuthFlag, const char *confDir,
559 char *cellName, afs_int32 sauth,
560 struct ubik_client **uclientp,
561 int (*secproc) (struct rx_securityClass *sc,
564 afs_int32 gen_rxkad_level,
565 afs_int32 maxservers, char *serviceid,
566 afs_int32 deadtime, afs_uint32 server,
567 afs_uint32 port, afs_int32 usrvid);