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
10 #include <afsconfig.h>
11 #include <afs/param.h>
18 #include <afs/cellconfig.h>
20 #define UBIK_INTERNALS
24 #include <lwp.h> /* temporary hack by klm */
26 #define ERROR_EXIT(code) do { \
33 * This system is organized in a hierarchical set of related modules. Modules
34 * at one level can only call modules at the same level or below.
36 * At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
37 * operating system primitives.
39 * At the next level (1) we have
41 * \li VOTER--The module responsible for casting votes when asked. It is also
42 * responsible for determining whether this server should try to become
43 * a synchronization site.
44 * \li BEACONER--The module responsible for sending keep-alives out when a
45 * server is actually the sync site, or trying to become a sync site.
46 * \li DISK--The module responsible for representing atomic transactions
47 * on the local disk. It maintains a new-value only log.
48 * \li LOCK--The module responsible for locking byte ranges in the database file.
50 * At the next level (2) we have
52 * \li RECOVERY--The module responsible for ensuring that all members of a quorum
53 * have the same up-to-date database after a new synchronization site is
54 * elected. This module runs only on the synchronization site.
56 * At the next level (3) we have
58 * \li REMOTE--The module responsible for interpreting requests from the sync
59 * site and applying them to the database, after obtaining the appropriate
62 * At the next level (4) we have
64 * \li UBIK--The module users call to perform operations on the database.
69 afs_int32 ubik_quorum = 0;
70 struct ubik_dbase *ubik_dbase = 0;
71 struct ubik_stats ubik_stats;
72 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
73 afs_int32 ubik_epochTime = 0;
74 afs_int32 urecovery_state = 0;
75 int (*ubik_SyncWriterCacheProc) (void);
76 struct ubik_server *ubik_servers;
77 short ubik_callPortal;
79 /* These global variables were used to control the server security layers.
80 * They are retained for backwards compatibility with legacy callers.
82 * The ubik_SetServerSecurityProcs() interface should be used instead.
85 int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **, afs_int32 *);
86 void *ubik_SRXSecurityRock;
87 int (*ubik_CheckRXSecurityProc) (void *, struct rx_call *);
88 void *ubik_CheckRXSecurityRock;
92 static int BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
93 struct ubik_trans **transPtr, int readAny);
95 static struct rx_securityClass **ubik_sc = NULL;
96 static void (*buildSecClassesProc)(void *, struct rx_securityClass ***,
98 static int (*checkSecurityProc)(void *, struct rx_call *) = NULL;
99 static void *securityRock = NULL;
101 #define CStampVersion 1 /* meaning set ts->version */
103 static_inline struct rx_connection *
104 Quorum_StartIO(struct ubik_trans *atrans, struct ubik_server *as)
106 struct rx_connection *conn;
108 conn = as->disk_rxcid;
110 #ifdef AFS_PTHREAD_ENV
111 rx_GetConnection(conn);
112 DBRELE(atrans->dbase);
113 #endif /* AFS_PTHREAD_ENV */
119 Quorum_EndIO(struct ubik_trans *atrans, struct rx_connection *aconn)
121 #ifdef AFS_PTHREAD_ENV
122 DBHOLD(atrans->dbase);
123 rx_PutConnection(aconn);
124 #endif /* AFS_PTHREAD_ENV */
129 * Iterate over all servers. Callers pass in *ts which is used to track
130 * the current server.
131 * - Returns 1 if there are no more servers
132 * - Returns 0 with conn set to the connection for the current server if
133 * it's up and current
136 ContactQuorum_iterate(struct ubik_trans *atrans, int aflags, struct ubik_server **ts,
137 struct rx_connection **conn, afs_int32 *rcode,
138 afs_int32 *okcalls, afs_int32 code)
141 /* Initial call - start iterating over servers */
148 Quorum_EndIO(atrans, *conn);
150 if (code) { /* failure */
153 (*ts)->up = 0; /* mark as down now; beacons will no longer be sent */
154 (*ts)->beaconSinceDown = 0;
156 (*ts)->currentDB = 0;
157 urecovery_LostServer(*ts); /* tell recovery to try to resend dbase later */
158 } else { /* success */
160 (*okcalls)++; /* count up how many worked */
161 if (aflags & CStampVersion) {
162 (*ts)->version = atrans->dbase->version;
171 if (!(*ts)->up || !(*ts)->currentDB) {
173 (*ts)->currentDB = 0; /* db is no longer current; we just missed an update */
174 return 0; /* not up-to-date, don't bother. NULL conn will tell caller not to use */
177 *conn = Quorum_StartIO(atrans, *ts);
182 ContactQuorum_rcode(int okcalls, afs_int32 rcode)
185 * return 0 if we successfully contacted a quorum, otherwise return error code.
186 * We don't have to contact ourselves (that was done locally)
188 if (okcalls + 1 >= ubik_quorum)
195 * \brief Perform an operation at a quorum, handling error conditions.
196 * \return 0 if all worked and a quorum was contacted successfully
197 * \return otherwise mark failing server as down and return #UERROR
199 * \note If any server misses an update, we must wait #BIGTIME seconds before
200 * allowing the transaction to commit, to ensure that the missing and
201 * possibly still functioning server times out and stops handing out old
202 * data. This is done in the commit code, where we wait for a server marked
203 * down to have stayed down for #BIGTIME seconds before we allow a transaction
204 * to commit. A server that fails but comes back up won't give out old data
205 * because it is sent the sync count along with the beacon message that
206 * marks it as \b really up (\p beaconSinceDown).
209 ContactQuorum_NoArguments(afs_int32 (*proc)(struct rx_connection *, ubik_tid *),
210 struct ubik_trans *atrans, int aflags)
212 struct ubik_server *ts = NULL;
213 afs_int32 code = 0, rcode, okcalls;
214 struct rx_connection *conn;
217 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
220 code = (*proc)(conn, &atrans->tid);
221 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
223 return ContactQuorum_rcode(okcalls, rcode);
228 ContactQuorum_DISK_Lock(struct ubik_trans *atrans, int aflags,afs_int32 file,
229 afs_int32 position, afs_int32 length, afs_int32 type)
231 struct ubik_server *ts = NULL;
232 afs_int32 code = 0, rcode, okcalls;
233 struct rx_connection *conn;
236 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
239 code = DISK_Lock(conn, &atrans->tid, file, position, length, type);
240 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
242 return ContactQuorum_rcode(okcalls, rcode);
247 ContactQuorum_DISK_Write(struct ubik_trans *atrans, int aflags,
248 afs_int32 file, afs_int32 position, bulkdata *data)
250 struct ubik_server *ts = NULL;
251 afs_int32 code = 0, rcode, okcalls;
252 struct rx_connection *conn;
255 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
258 code = DISK_Write(conn, &atrans->tid, file, position, data);
259 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
261 return ContactQuorum_rcode(okcalls, rcode);
266 ContactQuorum_DISK_Truncate(struct ubik_trans *atrans, int aflags,
267 afs_int32 file, afs_int32 length)
269 struct ubik_server *ts = NULL;
270 afs_int32 code = 0, rcode, okcalls;
271 struct rx_connection *conn;
274 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
277 code = DISK_Truncate(conn, &atrans->tid, file, length);
278 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
280 return ContactQuorum_rcode(okcalls, rcode);
285 ContactQuorum_DISK_WriteV(struct ubik_trans *atrans, int aflags,
286 iovec_wrt * io_vector, iovec_buf *io_buffer)
288 struct ubik_server *ts = NULL;
289 afs_int32 code = 0, rcode, okcalls;
290 struct rx_connection *conn;
293 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
296 code = DISK_WriteV(conn, &atrans->tid, io_vector, io_buffer);
297 if ((code <= -450) && (code > -500)) {
298 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
299 * Un-bulk the entries and do individual DISK_Write calls
300 * instead of DISK_WriteV.
302 struct ubik_iovec *iovec =
303 (struct ubik_iovec *)io_vector->iovec_wrt_val;
304 char *iobuf = (char *)io_buffer->iovec_buf_val;
308 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
309 /* Sanity check for going off end of buffer */
310 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
314 tcbs.bulkdata_len = iovec[i].length;
315 tcbs.bulkdata_val = &iobuf[offset];
316 code = DISK_Write(conn, &atrans->tid, iovec[i].file,
317 iovec[i].position, &tcbs);
320 offset += iovec[i].length;
324 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
326 return ContactQuorum_rcode(okcalls, rcode);
331 ContactQuorum_DISK_SetVersion(struct ubik_trans *atrans, int aflags,
332 ubik_version *OldVersion,
333 ubik_version *NewVersion)
335 struct ubik_server *ts = NULL;
336 afs_int32 code = 0, rcode, okcalls;
337 struct rx_connection *conn;
340 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
343 code = DISK_SetVersion(conn, &atrans->tid, OldVersion, NewVersion);
344 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
346 return ContactQuorum_rcode(okcalls, rcode);
349 #if defined(AFS_PTHREAD_ENV)
351 ubik_thread_create(pthread_attr_t *tattr, pthread_t *thread, void *proc) {
352 osi_Assert(pthread_attr_init(tattr) == 0);
353 osi_Assert(pthread_attr_setdetachstate(tattr, PTHREAD_CREATE_DETACHED) == 0);
354 osi_Assert(pthread_create(thread, tattr, proc, NULL) == 0);
360 * \brief This routine initializes the ubik system for a set of servers.
361 * \return 0 for success, or an error code on failure.
362 * \param serverList set of servers specified; nServers gives the number of entries in this array.
363 * \param pathName provides an initial prefix used for naming storage files used by this system.
364 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
366 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
368 * \warning The host named by myHost should not also be listed in serverList.
370 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
373 ubik_ServerInitCommon(afs_uint32 myHost, short myPort,
374 struct afsconf_cell *info, char clones[],
375 afs_uint32 serverList[], const char *pathName,
376 struct ubik_dbase **dbase)
378 struct ubik_dbase *tdb;
380 #ifdef AFS_PTHREAD_ENV
381 pthread_t rxServerThread; /* pthread variables */
382 pthread_t ubeacon_InteractThread;
383 pthread_t urecovery_InteractThread;
384 pthread_attr_t rxServer_tattr;
385 pthread_attr_t ubeacon_Interact_tattr;
386 pthread_attr_t urecovery_Interact_tattr;
389 extern int rx_stackSize;
393 struct rx_securityClass *secClass;
396 struct rx_service *tservice;
398 initialize_U_error_table();
400 tdb = (struct ubik_dbase *)malloc(sizeof(struct ubik_dbase));
401 tdb->pathName = (char *)malloc(strlen(pathName) + 1);
402 strcpy(tdb->pathName, pathName);
403 tdb->activeTrans = (struct ubik_trans *)0;
404 memset(&tdb->version, 0, sizeof(struct ubik_version));
405 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
406 #ifdef AFS_PTHREAD_ENV
407 MUTEX_INIT(&tdb->versionLock, "version lock", MUTEX_DEFAULT, 0);
408 MUTEX_INIT(&beacon_globals.beacon_lock, "beacon lock", MUTEX_DEFAULT, 0);
409 MUTEX_INIT(&vote_globals.vote_lock, "vote lock", MUTEX_DEFAULT, 0);
411 Lock_Init(&tdb->versionLock);
413 Lock_Init(&tdb->cache_lock);
415 tdb->read = uphys_read;
416 tdb->write = uphys_write;
417 tdb->truncate = uphys_truncate;
418 tdb->open = uphys_invalidate; /* this function isn't used any more */
419 tdb->sync = uphys_sync;
420 tdb->stat = uphys_stat;
421 tdb->getlabel = uphys_getlabel;
422 tdb->setlabel = uphys_setlabel;
423 tdb->getnfiles = uphys_getnfiles;
425 tdb->tidCounter = tdb->writeTidCounter = 0;
427 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
429 #ifdef AFS_PTHREAD_ENV
430 CV_INIT(&tdb->version_cond, "version", CV_DEFAULT, 0);
431 CV_INIT(&tdb->flags_cond, "flags", CV_DEFAULT, 0);
432 #endif /* AFS_PTHREAD_ENV */
436 /* the following call is idempotent so when/if it got called earlier,
437 * by whatever called us, it doesn't really matter -- klm */
438 code = rx_Init(myPort);
442 udisk_Init(ubik_nBuffers);
448 code = urecovery_Initialize(tdb);
452 code = ubeacon_InitServerListByInfo(myHost, info, clones);
454 code = ubeacon_InitServerList(myHost, serverList);
458 ubik_callPortal = myPort;
459 /* try to get an additional security object */
460 if (buildSecClassesProc == NULL) {
462 ubik_sc = calloc(numClasses, sizeof(struct rx_securityClass *));
463 ubik_sc[0] = rxnull_NewServerSecurityObject();
464 if (ubik_SRXSecurityProc) {
465 code = (*ubik_SRXSecurityProc) (ubik_SRXSecurityRock,
469 ubik_sc[secIndex] = secClass;
473 (*buildSecClassesProc) (securityRock, &ubik_sc, &numClasses);
475 /* for backwards compat this should keep working as it does now
479 rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, numClasses,
480 VOTE_ExecuteRequest);
481 if (tservice == (struct rx_service *)0) {
482 ubik_dprint("Could not create VOTE rx service!\n");
485 rx_SetMinProcs(tservice, 2);
486 rx_SetMaxProcs(tservice, 3);
489 rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, numClasses,
490 DISK_ExecuteRequest);
491 if (tservice == (struct rx_service *)0) {
492 ubik_dprint("Could not create DISK rx service!\n");
495 rx_SetMinProcs(tservice, 2);
496 rx_SetMaxProcs(tservice, 3);
498 /* start an rx_ServerProc to handle incoming RPC's in particular the
499 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
500 * the "steplock" problem in ubik initialization. Defect 11037.
502 #ifdef AFS_PTHREAD_ENV
503 ubik_thread_create(&rxServer_tattr, &rxServerThread, (void *)rx_ServerProc);
505 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
506 NULL, "rx_ServerProc", &junk);
509 /* now start up async processes */
510 #ifdef AFS_PTHREAD_ENV
511 ubik_thread_create(&ubeacon_Interact_tattr, &ubeacon_InteractThread,
512 (void *)ubeacon_Interact);
514 code = LWP_CreateProcess(ubeacon_Interact, 16384 /*8192 */ ,
515 LWP_MAX_PRIORITY - 1, (void *)0, "beacon",
521 #ifdef AFS_PTHREAD_ENV
522 ubik_thread_create(&urecovery_Interact_tattr, &urecovery_InteractThread,
523 (void *)urecovery_Interact);
524 return 0; /* is this correct? - klm */
526 code = LWP_CreateProcess(urecovery_Interact, 16384 /*8192 */ ,
527 LWP_MAX_PRIORITY - 1, (void *)0, "recovery",
535 * \see ubik_ServerInitCommon()
538 ubik_ServerInitByInfo(afs_uint32 myHost, short myPort,
539 struct afsconf_cell *info, char clones[],
540 const char *pathName, struct ubik_dbase **dbase)
545 ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName,
551 * \see ubik_ServerInitCommon()
554 ubik_ServerInit(afs_uint32 myHost, short myPort, afs_uint32 serverList[],
555 const char *pathName, struct ubik_dbase **dbase)
560 ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
561 serverList, pathName, dbase);
566 * \brief This routine begins a read or write transaction on the transaction
567 * identified by transPtr, in the dbase named by dbase.
569 * An open mode of ubik_READTRANS identifies this as a read transaction,
570 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
571 * transPtr is set to the returned transaction control block.
572 * The readAny flag is set to 0 or 1 or 2 by the wrapper functions
573 * ubik_BeginTrans() or ubik_BeginTransReadAny() or
574 * ubik_BeginTransReadAnyWrite() below.
576 * \note We can only begin transaction when we have an up-to-date database.
579 BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
580 struct ubik_trans **transPtr, int readAny)
582 struct ubik_trans *jt;
583 struct ubik_trans *tt;
586 if (readAny > 1 && ubik_SyncWriterCacheProc == NULL) {
587 /* it's not safe to use ubik_BeginTransReadAnyWrite without a
588 * cache-syncing function; fall back to ubik_BeginTransReadAny,
589 * which is safe but slower */
590 ubik_print("ubik_BeginTransReadAnyWrite called, but "
591 "ubik_SyncWriterCacheProc not set; pretending "
592 "ubik_BeginTransReadAny was called instead\n");
596 if ((transMode != UBIK_READTRANS) && readAny)
599 if (urecovery_AllBetter(dbase, readAny) == 0) {
603 /* otherwise we have a quorum, use it */
605 /* make sure that at most one write transaction occurs at any one time. This
606 * has nothing to do with transaction locking; that's enforced by the lock package. However,
607 * we can't even handle two non-conflicting writes, since our log and recovery modules
608 * don't know how to restore one without possibly picking up some data from the other. */
609 if (transMode == UBIK_WRITETRANS) {
610 /* if we're writing already, wait */
611 while (dbase->flags & DBWRITING) {
612 #ifdef AFS_PTHREAD_ENV
613 CV_WAIT(&dbase->flags_cond, &dbase->versionLock);
616 LWP_WaitProcess(&dbase->flags);
621 if (!ubeacon_AmSyncSite()) {
627 /* create the transaction */
628 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
629 tt = jt; /* move to a register */
630 if (code || tt == (struct ubik_trans *)NULL) {
635 tt->flags |= TRREADANY;
637 tt->flags |= TRREADWRITE;
640 /* label trans and dbase with new tid */
641 tt->tid.epoch = ubik_epochTime;
642 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
643 tt->tid.counter = (dbase->tidCounter += 2);
645 if (transMode == UBIK_WRITETRANS) {
646 /* for a write trans, we have to keep track of the write tid counter too */
647 dbase->writeTidCounter = tt->tid.counter;
649 /* next try to start transaction on appropriate number of machines */
650 code = ContactQuorum_NoArguments(DISK_Begin, tt, 0);
652 /* we must abort the operation */
654 ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
670 ubik_BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
671 struct ubik_trans **transPtr)
673 return BeginTrans(dbase, transMode, transPtr, 0);
680 ubik_BeginTransReadAny(struct ubik_dbase *dbase, afs_int32 transMode,
681 struct ubik_trans **transPtr)
683 return BeginTrans(dbase, transMode, transPtr, 1);
690 ubik_BeginTransReadAnyWrite(struct ubik_dbase *dbase, afs_int32 transMode,
691 struct ubik_trans **transPtr)
693 return BeginTrans(dbase, transMode, transPtr, 2);
697 * \brief This routine ends a read or write transaction by aborting it.
700 ubik_AbortTrans(struct ubik_trans *transPtr)
704 struct ubik_dbase *dbase;
706 dbase = transPtr->dbase;
708 if (transPtr->flags & TRCACHELOCKED) {
709 ReleaseReadLock(&dbase->cache_lock);
710 transPtr->flags &= ~TRCACHELOCKED;
713 ObtainWriteLock(&dbase->cache_lock);
716 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
718 ReleaseWriteLock(&dbase->cache_lock);
720 /* see if we're still up-to-date */
721 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
722 udisk_abort(transPtr);
728 if (transPtr->type == UBIK_READTRANS) {
729 code = udisk_abort(transPtr);
735 /* below here, we know we're doing a write transaction */
736 if (!ubeacon_AmSyncSite()) {
737 udisk_abort(transPtr);
743 /* now it is safe to try remote abort */
744 code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
745 code2 = udisk_abort(transPtr);
748 return (code ? code : code2);
752 WritebackApplicationCache(struct ubik_dbase *dbase)
755 if (ubik_SyncWriterCacheProc) {
756 code = ubik_SyncWriterCacheProc();
759 /* we failed to sync the local cache, so just invalidate the cache;
760 * we'll try to read the cache in again on the next read */
761 memset(&dbase->cachedVersion, 0, sizeof(dbase->cachedVersion));
763 memcpy(&dbase->cachedVersion, &dbase->version,
764 sizeof(dbase->cachedVersion));
769 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
770 * \return an error code.
773 ubik_EndTrans(struct ubik_trans *transPtr)
778 struct ubik_server *ts;
781 struct ubik_dbase *dbase;
783 if (transPtr->type == UBIK_WRITETRANS) {
784 code = ubik_Flush(transPtr);
786 ubik_AbortTrans(transPtr);
791 dbase = transPtr->dbase;
793 if (transPtr->flags & TRCACHELOCKED) {
794 ReleaseReadLock(&dbase->cache_lock);
795 transPtr->flags &= ~TRCACHELOCKED;
798 if (transPtr->type != UBIK_READTRANS) {
799 /* must hold cache_lock before DBHOLD'ing */
800 ObtainWriteLock(&dbase->cache_lock);
806 /* give up if no longer current */
807 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
808 udisk_abort(transPtr);
815 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
816 code = udisk_commit(transPtr);
818 goto success; /* update cachedVersion correctly */
824 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
825 udisk_abort(transPtr);
832 /* now it is safe to do commit */
833 code = udisk_commit(transPtr);
835 /* db data has been committed locally; update the local cache so
836 * readers can get at it */
837 WritebackApplicationCache(dbase);
839 ReleaseWriteLock(&dbase->cache_lock);
841 code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
844 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
845 ReleaseWriteLock(&dbase->cache_lock);
849 /* failed to commit, so must return failure. Try to clear locks first, just for fun
850 * Note that we don't know if this transaction will eventually commit at this point.
851 * If it made it to a site that will be present in the next quorum, we win, otherwise
852 * we lose. If we contact a majority of sites, then we won't be here: contacting
853 * a majority guarantees commit, since it guarantees that one dude will be a
854 * member of the next quorum. */
855 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
860 /* before we can start sending unlock messages, we must wait until all servers
861 * that are possibly still functioning on the other side of a network partition
862 * have timed out. Check the server structures, compute how long to wait, then
863 * start the unlocks */
864 realStart = FT_ApproxTime();
866 /* wait for all servers to time out */
868 now = FT_ApproxTime();
869 /* check if we're still sync site, the guy should either come up
870 * to us, or timeout. Put safety check in anyway */
871 if (now - realStart > 10 * BIGTIME) {
872 ubik_stats.escapes++;
873 ubik_print("ubik escaping from commit wait\n");
876 for (ts = ubik_servers; ts; ts = ts->next) {
878 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
881 /* this guy could have some damaged data, wait for him */
883 tv.tv_sec = 1; /* try again after a while (ha ha) */
886 #ifdef AFS_PTHREAD_ENV
887 /* we could release the dbase outside of the loop, but we do
888 * it here, in the loop, to avoid an unnecessary RELE/HOLD
889 * if all sites are up */
891 select(0, 0, 0, 0, &tv);
894 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
902 break; /* no down ones still pseudo-active */
905 /* finally, unlock all the dudes. We can return success independent of the number of servers
906 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
907 * The transaction is committed anyway, since we succeeded in contacting a quorum
908 * at the start (when invoking the DiskCommit function).
910 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
914 /* don't update cachedVersion here; it should have been updated way back
915 * in ubik_CheckCache, and earlier in this function for writes */
918 ReleaseWriteLock(&dbase->cache_lock);
924 ObtainWriteLock(&dbase->cache_lock);
926 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
927 ReleaseWriteLock(&dbase->cache_lock);
932 * \brief This routine reads length bytes into buffer from the current position in the database.
934 * The file pointer is updated appropriately (by adding the number of bytes actually transferred), and the length actually transferred is stored in the long integer pointed to by length. A short read returns zero for an error code.
936 * \note *length is an INOUT parameter: at the start it represents the size of the buffer, and when done, it contains the number of bytes actually transferred.
939 ubik_Read(struct ubik_trans *transPtr, void *buffer,
944 /* reads are easy to do: handle locally */
945 DBHOLD(transPtr->dbase);
946 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
947 DBRELE(transPtr->dbase);
952 udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
955 transPtr->seekPos += length;
957 DBRELE(transPtr->dbase);
962 * \brief This routine will flush the io data in the iovec structures.
964 * It first flushes to the local disk and then uses ContactQuorum to write it
965 * to the other servers.
968 ubik_Flush(struct ubik_trans *transPtr)
970 afs_int32 code, error = 0;
972 if (transPtr->type != UBIK_WRITETRANS)
974 if (!transPtr->iovec_info.iovec_wrt_len
975 || !transPtr->iovec_info.iovec_wrt_val)
978 DBHOLD(transPtr->dbase);
979 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
980 ERROR_EXIT(UNOQUORUM);
981 if (!ubeacon_AmSyncSite()) /* only sync site can write */
982 ERROR_EXIT(UNOTSYNC);
984 /* Update the rest of the servers in the quorum */
986 ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
987 &transPtr->iovec_data);
989 udisk_abort(transPtr);
990 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
991 transPtr->iovec_info.iovec_wrt_len = 0;
992 transPtr->iovec_data.iovec_buf_len = 0;
996 /* Wrote the buffers out, so start at scratch again */
997 transPtr->iovec_info.iovec_wrt_len = 0;
998 transPtr->iovec_data.iovec_buf_len = 0;
1001 DBRELE(transPtr->dbase);
1006 ubik_Write(struct ubik_trans *transPtr, void *vbuffer,
1009 struct ubik_iovec *iovec;
1010 afs_int32 code, error = 0;
1011 afs_int32 pos, len, size;
1012 char * buffer = (char *)vbuffer;
1014 if (transPtr->type != UBIK_WRITETRANS)
1019 if (length > IOVEC_MAXBUF) {
1020 for (pos = 0, len = length; len > 0; len -= size, pos += size) {
1021 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
1022 code = ubik_Write(transPtr, buffer+pos, size);
1029 if (!transPtr->iovec_info.iovec_wrt_val) {
1030 transPtr->iovec_info.iovec_wrt_len = 0;
1031 transPtr->iovec_info.iovec_wrt_val =
1032 (struct ubik_iovec *)malloc(IOVEC_MAXWRT *
1033 sizeof(struct ubik_iovec));
1034 transPtr->iovec_data.iovec_buf_len = 0;
1035 transPtr->iovec_data.iovec_buf_val = (char *)malloc(IOVEC_MAXBUF);
1036 if (!transPtr->iovec_info.iovec_wrt_val
1037 || !transPtr->iovec_data.iovec_buf_val) {
1038 if (transPtr->iovec_info.iovec_wrt_val)
1039 free(transPtr->iovec_info.iovec_wrt_val);
1040 transPtr->iovec_info.iovec_wrt_val = 0;
1041 if (transPtr->iovec_data.iovec_buf_val)
1042 free(transPtr->iovec_data.iovec_buf_val);
1043 transPtr->iovec_data.iovec_buf_val = 0;
1048 /* If this write won't fit in the structure, then flush it out and start anew */
1049 if ((transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT)
1050 || ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF)) {
1051 code = ubik_Flush(transPtr);
1056 DBHOLD(transPtr->dbase);
1057 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1058 ERROR_EXIT(UNOQUORUM);
1059 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1060 ERROR_EXIT(UNOTSYNC);
1062 /* Write to the local disk */
1064 udisk_write(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
1067 udisk_abort(transPtr);
1068 transPtr->iovec_info.iovec_wrt_len = 0;
1069 transPtr->iovec_data.iovec_buf_len = 0;
1070 DBRELE(transPtr->dbase);
1074 /* Collect writes for the other ubik servers (to be done in bulk) */
1075 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
1076 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
1077 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
1078 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
1080 memcpy(&transPtr->iovec_data.
1081 iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
1083 transPtr->iovec_info.iovec_wrt_len++;
1084 transPtr->iovec_data.iovec_buf_len += length;
1085 transPtr->seekPos += length;
1088 DBRELE(transPtr->dbase);
1093 * \brief This sets the file pointer associated with the current transaction
1094 * to the appropriate file and byte position.
1096 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1097 * and a byte position relative to the specified file \p position.
1100 ubik_Seek(struct ubik_trans *transPtr, afs_int32 fileid,
1105 DBHOLD(transPtr->dbase);
1106 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
1109 transPtr->seekFile = fileid;
1110 transPtr->seekPos = position;
1113 DBRELE(transPtr->dbase);
1118 * \brief This call returns the file pointer associated with the specified
1119 * transaction in \p fileid and \p position.
1122 ubik_Tell(struct ubik_trans *transPtr, afs_int32 * fileid,
1123 afs_int32 * position)
1125 DBHOLD(transPtr->dbase);
1126 *fileid = transPtr->seekFile;
1127 *position = transPtr->seekPos;
1128 DBRELE(transPtr->dbase);
1133 * \brief This sets the file size for the currently-selected file to \p length
1134 * bytes, if length is less than the file's current size.
1137 ubik_Truncate(struct ubik_trans *transPtr, afs_int32 length)
1139 afs_int32 code, error = 0;
1141 /* Will also catch if not UBIK_WRITETRANS */
1142 code = ubik_Flush(transPtr);
1146 DBHOLD(transPtr->dbase);
1147 /* first, check that quorum is still good, and that dbase is up-to-date */
1148 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1149 ERROR_EXIT(UNOQUORUM);
1150 if (!ubeacon_AmSyncSite())
1151 ERROR_EXIT(UNOTSYNC);
1153 /* now do the operation locally, and propagate it out */
1154 code = udisk_truncate(transPtr, transPtr->seekFile, length);
1157 ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
1161 /* we must abort the operation */
1162 udisk_abort(transPtr);
1163 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1168 DBRELE(transPtr->dbase);
1173 * \brief set a lock; all locks are released on transaction end (commit/abort)
1176 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
1179 afs_int32 code = 0, error = 0;
1181 if (atype == LOCKWRITE) {
1182 if (atrans->type == UBIK_READTRANS)
1184 code = ubik_Flush(atrans);
1189 DBHOLD(atrans->dbase);
1190 if (atype == LOCKREAD) {
1191 code = ulock_getLock(atrans, atype, 1);
1195 /* first, check that quorum is still good, and that dbase is up-to-date */
1196 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
1197 ERROR_EXIT(UNOQUORUM);
1198 if (!ubeacon_AmSyncSite())
1199 ERROR_EXIT(UNOTSYNC);
1201 /* now do the operation locally, and propagate it out */
1202 code = ulock_getLock(atrans, atype, 1);
1204 code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
1205 1 /*unused */ , LOCKWRITE);
1208 /* we must abort the operation */
1209 udisk_abort(atrans);
1210 ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
1216 DBRELE(atrans->dbase);
1221 * \brief utility to wait for a version # to change
1224 ubik_WaitVersion(struct ubik_dbase *adatabase,
1225 struct ubik_version *aversion)
1229 /* wait until version # changes, and then return */
1230 if (vcmp(*aversion, adatabase->version) != 0) {
1234 #ifdef AFS_PTHREAD_ENV
1235 CV_WAIT(&adatabase->version_cond, &adatabase->versionLock);
1238 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
1245 * \brief utility to get the version of the dbase a transaction is dealing with
1248 ubik_GetVersion(struct ubik_trans *atrans,
1249 struct ubik_version *avers)
1251 *avers = atrans->dbase->version;
1256 * \brief Facility to simplify database caching.
1257 * \return zero if last trans was done on the local server and was successful.
1258 * \return -1 means bad (NULL) argument.
1260 * If return value is non-zero and the caller is a server caching part of the
1261 * Ubik database, it should invalidate that cache.
1264 ubik_CacheUpdate(struct ubik_trans *atrans)
1266 if (!(atrans && atrans->dbase))
1268 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
1272 * check and possibly update cache of ubik db.
1274 * If the version of the cached db data is out of date, this calls (*check) to
1275 * update the cache. If (*check) returns success, we update the version of the
1278 * Checking the version of the cached db data is done under a read lock;
1279 * updating the cache (and thus calling (*check)) is done under a write lock
1280 * so is guaranteed not to interfere with another thread's (*check). On
1281 * successful return, a read lock on the cached db data is obtained, which
1282 * will be released by ubik_EndTrans or ubik_AbortTrans.
1284 * @param[in] atrans ubik transaction
1285 * @param[in] check function to call to check/update cache
1286 * @param[in] rock rock to pass to *check
1288 * @return operation status
1290 * @retval nonzero error; cachedVersion not updated
1292 * @post On success, application cache is read-locked, and cache data is
1296 ubik_CheckCache(struct ubik_trans *atrans, ubik_updatecache_func cbf, void *rock)
1300 if (!(atrans && atrans->dbase))
1303 ObtainReadLock(&atrans->dbase->cache_lock);
1305 while (ubik_CacheUpdate(atrans) != 0) {
1307 ReleaseReadLock(&atrans->dbase->cache_lock);
1308 ObtainSharedLock(&atrans->dbase->cache_lock);
1310 if (ubik_CacheUpdate(atrans) != 0) {
1312 BoostSharedLock(&atrans->dbase->cache_lock);
1314 ret = (*cbf) (atrans, rock);
1316 memcpy(&atrans->dbase->cachedVersion, &atrans->dbase->version,
1317 sizeof(atrans->dbase->cachedVersion));
1321 /* It would be nice if we could convert from a shared lock to a read
1322 * lock... instead, just release the shared and acquire the read */
1323 ReleaseSharedLock(&atrans->dbase->cache_lock);
1326 /* if we have an error, don't retry, and don't hold any locks */
1330 ObtainReadLock(&atrans->dbase->cache_lock);
1333 atrans->flags |= TRCACHELOCKED;
1339 * "Who said anything about panicking?" snapped Arthur.
1340 * "This is still just the culture shock. You wait till I've settled down
1341 * into the situation and found my bearings. \em Then I'll start panicking!"
1344 * \returns There is no return from panic.
1347 panic(char *format, ...)
1351 va_start(ap, format);
1352 ubik_print("Ubik PANIC: ");
1353 ubik_vprint(format, ap);
1357 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
1358 exit(1); /* never know, though */
1362 * This function takes an IP addresses as its parameter. It returns the
1363 * the primary IP address that is on the host passed in, or 0 if not found.
1366 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
1368 struct ubik_server *ts;
1371 for (ts = ubik_servers; ts; ts = ts->next)
1372 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
1373 if (ts->addr[j] == addr)
1374 return ts->addr[0]; /* net byte order */
1375 return 0; /* if not in server database, return error */
1379 ubik_CheckAuth(struct rx_call *acall)
1381 if (checkSecurityProc)
1382 return (*checkSecurityProc) (securityRock, acall);
1383 else if (ubik_CheckRXSecurityProc) {
1384 return (*ubik_CheckRXSecurityProc) (ubik_CheckRXSecurityRock, acall);
1390 ubik_SetServerSecurityProcs(void (*buildproc) (void *,
1391 struct rx_securityClass ***,
1393 int (*checkproc) (void *, struct rx_call *),
1396 buildSecClassesProc = buildproc;
1397 checkSecurityProc = checkproc;
1398 securityRock = rock;