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>
17 #ifdef AFS_PTHREAD_ENV
18 # include <opr/lock.h>
20 # include <opr/lockstub.h>
25 #include <afs/cellconfig.h>
28 #define UBIK_INTERNALS
32 #include <lwp.h> /* temporary hack by klm */
34 #define ERROR_EXIT(code) do { \
41 * This system is organized in a hierarchical set of related modules. Modules
42 * at one level can only call modules at the same level or below.
44 * At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
45 * operating system primitives.
47 * At the next level (1) we have
49 * \li VOTER--The module responsible for casting votes when asked. It is also
50 * responsible for determining whether this server should try to become
51 * a synchronization site.
52 * \li BEACONER--The module responsible for sending keep-alives out when a
53 * server is actually the sync site, or trying to become a sync site.
54 * \li DISK--The module responsible for representing atomic transactions
55 * on the local disk. It maintains a new-value only log.
56 * \li LOCK--The module responsible for locking byte ranges in the database file.
58 * At the next level (2) we have
60 * \li RECOVERY--The module responsible for ensuring that all members of a quorum
61 * have the same up-to-date database after a new synchronization site is
62 * elected. This module runs only on the synchronization site.
64 * At the next level (3) we have
66 * \li REMOTE--The module responsible for interpreting requests from the sync
67 * site and applying them to the database, after obtaining the appropriate
70 * At the next level (4) we have
72 * \li UBIK--The module users call to perform operations on the database.
77 afs_int32 ubik_quorum = 0;
78 struct ubik_dbase *ubik_dbase = 0;
79 struct ubik_stats ubik_stats;
80 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
81 afs_int32 urecovery_state = 0;
82 int (*ubik_SyncWriterCacheProc) (void);
83 struct ubik_server *ubik_servers;
84 short ubik_callPortal;
86 /* These global variables were used to control the server security layers.
87 * They are retained for backwards compatibility with legacy callers.
89 * The ubik_SetServerSecurityProcs() interface should be used instead.
92 int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **, afs_int32 *);
93 void *ubik_SRXSecurityRock;
94 int (*ubik_CheckRXSecurityProc) (void *, struct rx_call *);
95 void *ubik_CheckRXSecurityRock;
99 static int BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
100 struct ubik_trans **transPtr, int readAny);
102 static struct rx_securityClass **ubik_sc = NULL;
103 static void (*buildSecClassesProc)(void *, struct rx_securityClass ***,
105 static int (*checkSecurityProc)(void *, struct rx_call *) = NULL;
106 static void *securityRock = NULL;
108 struct version_data version_globals;
110 #define CStampVersion 1 /* meaning set ts->version */
111 #define CCheckSyncAdvertised 2 /* check if the remote knows we are the sync-site */
113 static_inline struct rx_connection *
114 Quorum_StartIO(struct ubik_trans *atrans, struct ubik_server *as)
116 struct rx_connection *conn;
119 conn = as->disk_rxcid;
121 #ifdef AFS_PTHREAD_ENV
122 rx_GetConnection(conn);
124 DBRELE(atrans->dbase);
127 #endif /* AFS_PTHREAD_ENV */
133 Quorum_EndIO(struct ubik_trans *atrans, struct rx_connection *aconn)
135 #ifdef AFS_PTHREAD_ENV
136 DBHOLD(atrans->dbase);
137 rx_PutConnection(aconn);
138 #endif /* AFS_PTHREAD_ENV */
143 * Iterate over all servers. Callers pass in *ts which is used to track
144 * the current server.
145 * - Returns 1 if there are no more servers
146 * - Returns 0 with conn set to the connection for the current server if
147 * it's up and current
150 ContactQuorum_iterate(struct ubik_trans *atrans, int aflags, struct ubik_server **ts,
151 struct rx_connection **conn, afs_int32 *rcode,
152 afs_int32 *okcalls, afs_int32 code)
155 /* Initial call - start iterating over servers */
162 Quorum_EndIO(atrans, *conn);
164 if (code) { /* failure */
167 (*ts)->up = 0; /* mark as down now; beacons will no longer be sent */
168 (*ts)->beaconSinceDown = 0;
170 (*ts)->currentDB = 0;
171 urecovery_LostServer(*ts); /* tell recovery to try to resend dbase later */
172 } else { /* success */
174 (*okcalls)++; /* count up how many worked */
175 if (aflags & CStampVersion) {
176 (*ts)->version = atrans->dbase->version;
185 if (!(*ts)->up || !(*ts)->currentDB ||
186 /* do not call DISK_Begin until we know that lastYesState is set on the
187 * remote in question; otherwise, DISK_Begin will fail. */
188 ((aflags & CCheckSyncAdvertised) && !((*ts)->beaconSinceDown && (*ts)->lastVote))) {
190 (*ts)->currentDB = 0; /* db is no longer current; we just missed an update */
191 return 0; /* not up-to-date, don't bother. NULL conn will tell caller not to use */
194 *conn = Quorum_StartIO(atrans, *ts);
199 ContactQuorum_rcode(int okcalls, afs_int32 rcode)
202 * return 0 if we successfully contacted a quorum, otherwise return error code.
203 * We don't have to contact ourselves (that was done locally)
205 if (okcalls + 1 >= ubik_quorum)
208 return (rcode != 0) ? rcode : UNOQUORUM;
212 * \brief Perform an operation at a quorum, handling error conditions.
213 * \return 0 if all worked and a quorum was contacted successfully
214 * \return otherwise mark failing server as down and return #UERROR
216 * \note If any server misses an update, we must wait #BIGTIME seconds before
217 * allowing the transaction to commit, to ensure that the missing and
218 * possibly still functioning server times out and stops handing out old
219 * data. This is done in the commit code, where we wait for a server marked
220 * down to have stayed down for #BIGTIME seconds before we allow a transaction
221 * to commit. A server that fails but comes back up won't give out old data
222 * because it is sent the sync count along with the beacon message that
223 * marks it as \b really up (\p beaconSinceDown).
226 ContactQuorum_NoArguments(afs_int32 (*proc)(struct rx_connection *, ubik_tid *),
227 struct ubik_trans *atrans, int aflags)
229 struct ubik_server *ts = NULL;
230 afs_int32 code = 0, rcode, okcalls;
231 struct rx_connection *conn;
234 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
237 code = (*proc)(conn, &atrans->tid);
238 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
240 return ContactQuorum_rcode(okcalls, rcode);
245 ContactQuorum_DISK_Lock(struct ubik_trans *atrans, int aflags,afs_int32 file,
246 afs_int32 position, afs_int32 length, afs_int32 type)
248 struct ubik_server *ts = NULL;
249 afs_int32 code = 0, rcode, okcalls;
250 struct rx_connection *conn;
253 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
256 code = DISK_Lock(conn, &atrans->tid, file, position, length, type);
257 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
259 return ContactQuorum_rcode(okcalls, rcode);
264 ContactQuorum_DISK_Write(struct ubik_trans *atrans, int aflags,
265 afs_int32 file, afs_int32 position, bulkdata *data)
267 struct ubik_server *ts = NULL;
268 afs_int32 code = 0, rcode, okcalls;
269 struct rx_connection *conn;
272 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
275 code = DISK_Write(conn, &atrans->tid, file, position, data);
276 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
278 return ContactQuorum_rcode(okcalls, rcode);
283 ContactQuorum_DISK_Truncate(struct ubik_trans *atrans, int aflags,
284 afs_int32 file, afs_int32 length)
286 struct ubik_server *ts = NULL;
287 afs_int32 code = 0, rcode, okcalls;
288 struct rx_connection *conn;
291 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
294 code = DISK_Truncate(conn, &atrans->tid, file, length);
295 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
297 return ContactQuorum_rcode(okcalls, rcode);
302 ContactQuorum_DISK_WriteV(struct ubik_trans *atrans, int aflags,
303 iovec_wrt * io_vector, iovec_buf *io_buffer)
305 struct ubik_server *ts = NULL;
306 afs_int32 code = 0, rcode, okcalls;
307 struct rx_connection *conn;
310 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
313 code = DISK_WriteV(conn, &atrans->tid, io_vector, io_buffer);
314 if ((code <= -450) && (code > -500)) {
315 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
316 * Un-bulk the entries and do individual DISK_Write calls
317 * instead of DISK_WriteV.
319 struct ubik_iovec *iovec =
320 (struct ubik_iovec *)io_vector->iovec_wrt_val;
321 char *iobuf = (char *)io_buffer->iovec_buf_val;
325 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
326 /* Sanity check for going off end of buffer */
327 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
331 tcbs.bulkdata_len = iovec[i].length;
332 tcbs.bulkdata_val = &iobuf[offset];
333 code = DISK_Write(conn, &atrans->tid, iovec[i].file,
334 iovec[i].position, &tcbs);
337 offset += iovec[i].length;
341 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
343 return ContactQuorum_rcode(okcalls, rcode);
348 ContactQuorum_DISK_SetVersion(struct ubik_trans *atrans, int aflags,
349 ubik_version *OldVersion,
350 ubik_version *NewVersion)
352 struct ubik_server *ts = NULL;
353 afs_int32 code = 0, rcode, okcalls;
354 struct rx_connection *conn;
357 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
360 code = DISK_SetVersion(conn, &atrans->tid, OldVersion, NewVersion);
361 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
363 return ContactQuorum_rcode(okcalls, rcode);
366 #if defined(AFS_PTHREAD_ENV)
368 ubik_thread_create(pthread_attr_t *tattr, pthread_t *thread, void *proc) {
369 opr_Verify(pthread_attr_init(tattr) == 0);
370 opr_Verify(pthread_attr_setdetachstate(tattr,
371 PTHREAD_CREATE_DETACHED) == 0);
372 opr_Verify(pthread_create(thread, tattr, proc, NULL) == 0);
378 * \brief This routine initializes the ubik system for a set of servers.
379 * \return 0 for success, or an error code on failure.
380 * \param serverList set of servers specified; nServers gives the number of entries in this array.
381 * \param pathName provides an initial prefix used for naming storage files used by this system.
382 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
384 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
386 * \warning The host named by myHost should not also be listed in serverList.
388 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
391 ubik_ServerInitCommon(afs_uint32 myHost, short myPort,
392 struct afsconf_cell *info, char clones[],
393 afs_uint32 serverList[], const char *pathName,
394 struct ubik_dbase **dbase)
396 struct ubik_dbase *tdb;
398 #ifdef AFS_PTHREAD_ENV
399 pthread_t rxServerThread; /* pthread variables */
400 pthread_t ubeacon_InteractThread;
401 pthread_t urecovery_InteractThread;
402 pthread_attr_t rxServer_tattr;
403 pthread_attr_t ubeacon_Interact_tattr;
404 pthread_attr_t urecovery_Interact_tattr;
407 extern int rx_stackSize;
411 struct rx_securityClass *secClass;
414 struct rx_service *tservice;
416 initialize_U_error_table();
418 tdb = malloc(sizeof(struct ubik_dbase));
419 tdb->pathName = strdup(pathName);
420 tdb->activeTrans = (struct ubik_trans *)0;
421 memset(&tdb->version, 0, sizeof(struct ubik_version));
422 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
423 #ifdef AFS_PTHREAD_ENV
424 opr_mutex_init(&tdb->versionLock);
425 opr_mutex_init(&beacon_globals.beacon_lock);
426 opr_mutex_init(&vote_globals.vote_lock);
427 opr_mutex_init(&addr_globals.addr_lock);
428 opr_mutex_init(&version_globals.version_lock);
430 Lock_Init(&tdb->versionLock);
432 Lock_Init(&tdb->cache_lock);
434 tdb->read = uphys_read;
435 tdb->write = uphys_write;
436 tdb->truncate = uphys_truncate;
437 tdb->open = uphys_invalidate; /* this function isn't used any more */
438 tdb->sync = uphys_sync;
439 tdb->stat = uphys_stat;
440 tdb->getlabel = uphys_getlabel;
441 tdb->setlabel = uphys_setlabel;
442 tdb->getnfiles = uphys_getnfiles;
444 tdb->tidCounter = tdb->writeTidCounter = 0;
446 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
448 #ifdef AFS_PTHREAD_ENV
449 opr_cv_init(&tdb->version_cond);
450 opr_cv_init(&tdb->flags_cond);
451 #endif /* AFS_PTHREAD_ENV */
455 /* the following call is idempotent so when/if it got called earlier,
456 * by whatever called us, it doesn't really matter -- klm */
457 code = rx_Init(myPort);
461 ubik_callPortal = myPort;
463 udisk_Init(ubik_nBuffers);
469 code = urecovery_Initialize(tdb);
473 code = ubeacon_InitServerListByInfo(myHost, info, clones);
475 code = ubeacon_InitServerList(myHost, serverList);
479 /* try to get an additional security object */
480 if (buildSecClassesProc == NULL) {
482 ubik_sc = calloc(numClasses, sizeof(struct rx_securityClass *));
483 ubik_sc[0] = rxnull_NewServerSecurityObject();
484 if (ubik_SRXSecurityProc) {
485 code = (*ubik_SRXSecurityProc) (ubik_SRXSecurityRock,
489 ubik_sc[secIndex] = secClass;
493 (*buildSecClassesProc) (securityRock, &ubik_sc, &numClasses);
495 /* for backwards compat this should keep working as it does now
499 rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, numClasses,
500 VOTE_ExecuteRequest);
501 if (tservice == (struct rx_service *)0) {
502 ubik_dprint("Could not create VOTE rx service!\n");
505 rx_SetMinProcs(tservice, 2);
506 rx_SetMaxProcs(tservice, 3);
509 rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, numClasses,
510 DISK_ExecuteRequest);
511 if (tservice == (struct rx_service *)0) {
512 ubik_dprint("Could not create DISK rx service!\n");
515 rx_SetMinProcs(tservice, 2);
516 rx_SetMaxProcs(tservice, 3);
518 /* start an rx_ServerProc to handle incoming RPC's in particular the
519 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
520 * the "steplock" problem in ubik initialization. Defect 11037.
522 #ifdef AFS_PTHREAD_ENV
523 ubik_thread_create(&rxServer_tattr, &rxServerThread, (void *)rx_ServerProc);
525 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
526 NULL, "rx_ServerProc", &junk);
529 /* send addrs to all other servers */
530 code = ubeacon_updateUbikNetworkAddress(ubik_host);
534 /* now start up async processes */
535 #ifdef AFS_PTHREAD_ENV
536 ubik_thread_create(&ubeacon_Interact_tattr, &ubeacon_InteractThread,
537 (void *)ubeacon_Interact);
539 code = LWP_CreateProcess(ubeacon_Interact, 16384 /*8192 */ ,
540 LWP_MAX_PRIORITY - 1, (void *)0, "beacon",
546 #ifdef AFS_PTHREAD_ENV
547 ubik_thread_create(&urecovery_Interact_tattr, &urecovery_InteractThread,
548 (void *)urecovery_Interact);
549 return 0; /* is this correct? - klm */
551 code = LWP_CreateProcess(urecovery_Interact, 16384 /*8192 */ ,
552 LWP_MAX_PRIORITY - 1, (void *)0, "recovery",
560 * \see ubik_ServerInitCommon()
563 ubik_ServerInitByInfo(afs_uint32 myHost, short myPort,
564 struct afsconf_cell *info, char clones[],
565 const char *pathName, struct ubik_dbase **dbase)
570 ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName,
576 * \see ubik_ServerInitCommon()
579 ubik_ServerInit(afs_uint32 myHost, short myPort, afs_uint32 serverList[],
580 const char *pathName, struct ubik_dbase **dbase)
585 ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
586 serverList, pathName, dbase);
591 * \brief This routine begins a read or write transaction on the transaction
592 * identified by transPtr, in the dbase named by dbase.
594 * An open mode of ubik_READTRANS identifies this as a read transaction,
595 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
596 * transPtr is set to the returned transaction control block.
597 * The readAny flag is set to 0 or 1 or 2 by the wrapper functions
598 * ubik_BeginTrans() or ubik_BeginTransReadAny() or
599 * ubik_BeginTransReadAnyWrite() below.
601 * \note We can only begin transaction when we have an up-to-date database.
604 BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
605 struct ubik_trans **transPtr, int readAny)
607 struct ubik_trans *jt;
608 struct ubik_trans *tt;
611 if (readAny > 1 && ubik_SyncWriterCacheProc == NULL) {
612 /* it's not safe to use ubik_BeginTransReadAnyWrite without a
613 * cache-syncing function; fall back to ubik_BeginTransReadAny,
614 * which is safe but slower */
615 ubik_print("ubik_BeginTransReadAnyWrite called, but "
616 "ubik_SyncWriterCacheProc not set; pretending "
617 "ubik_BeginTransReadAny was called instead\n");
621 if ((transMode != UBIK_READTRANS) && readAny)
624 if (urecovery_AllBetter(dbase, readAny) == 0) {
628 /* otherwise we have a quorum, use it */
630 /* make sure that at most one write transaction occurs at any one time. This
631 * has nothing to do with transaction locking; that's enforced by the lock package. However,
632 * we can't even handle two non-conflicting writes, since our log and recovery modules
633 * don't know how to restore one without possibly picking up some data from the other. */
634 if (transMode == UBIK_WRITETRANS) {
635 /* if we're writing already, wait */
636 while (dbase->flags & DBWRITING) {
637 #ifdef AFS_PTHREAD_ENV
638 opr_cv_wait(&dbase->flags_cond, &dbase->versionLock);
641 LWP_WaitProcess(&dbase->flags);
646 if (!ubeacon_AmSyncSite()) {
650 if (!ubeacon_SyncSiteAdvertised()) {
651 /* i am the sync-site but the remotes are not aware yet */
657 /* create the transaction */
658 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
659 tt = jt; /* move to a register */
660 if (code || tt == NULL) {
666 tt->flags |= TRREADANY;
668 tt->flags |= TRREADWRITE;
671 /* label trans and dbase with new tid */
672 tt->tid.epoch = version_globals.ubik_epochTime;
673 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
674 tt->tid.counter = (dbase->tidCounter += 2);
676 if (transMode == UBIK_WRITETRANS) {
677 /* for a write trans, we have to keep track of the write tid counter too */
678 dbase->writeTidCounter = tt->tid.counter;
683 if (transMode == UBIK_WRITETRANS) {
684 /* next try to start transaction on appropriate number of machines */
685 code = ContactQuorum_NoArguments(DISK_Begin, tt, CCheckSyncAdvertised);
687 /* we must abort the operation */
689 ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
705 ubik_BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
706 struct ubik_trans **transPtr)
708 return BeginTrans(dbase, transMode, transPtr, 0);
715 ubik_BeginTransReadAny(struct ubik_dbase *dbase, afs_int32 transMode,
716 struct ubik_trans **transPtr)
718 return BeginTrans(dbase, transMode, transPtr, 1);
725 ubik_BeginTransReadAnyWrite(struct ubik_dbase *dbase, afs_int32 transMode,
726 struct ubik_trans **transPtr)
728 return BeginTrans(dbase, transMode, transPtr, 2);
732 * \brief This routine ends a read or write transaction by aborting it.
735 ubik_AbortTrans(struct ubik_trans *transPtr)
739 struct ubik_dbase *dbase;
741 dbase = transPtr->dbase;
743 if (transPtr->flags & TRCACHELOCKED) {
744 ReleaseReadLock(&dbase->cache_lock);
745 transPtr->flags &= ~TRCACHELOCKED;
748 ObtainWriteLock(&dbase->cache_lock);
751 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
753 ReleaseWriteLock(&dbase->cache_lock);
755 /* see if we're still up-to-date */
756 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
757 udisk_abort(transPtr);
763 if (transPtr->type == UBIK_READTRANS) {
764 code = udisk_abort(transPtr);
770 /* below here, we know we're doing a write transaction */
771 if (!ubeacon_AmSyncSite()) {
772 udisk_abort(transPtr);
778 /* now it is safe to try remote abort */
779 code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
780 code2 = udisk_abort(transPtr);
783 return (code ? code : code2);
787 WritebackApplicationCache(struct ubik_dbase *dbase)
790 if (ubik_SyncWriterCacheProc) {
791 code = ubik_SyncWriterCacheProc();
794 /* we failed to sync the local cache, so just invalidate the cache;
795 * we'll try to read the cache in again on the next read */
796 memset(&dbase->cachedVersion, 0, sizeof(dbase->cachedVersion));
798 memcpy(&dbase->cachedVersion, &dbase->version,
799 sizeof(dbase->cachedVersion));
804 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
805 * \return an error code.
808 ubik_EndTrans(struct ubik_trans *transPtr)
813 struct ubik_server *ts;
816 struct ubik_dbase *dbase;
818 if (transPtr->type == UBIK_WRITETRANS) {
819 code = ubik_Flush(transPtr);
821 ubik_AbortTrans(transPtr);
826 dbase = transPtr->dbase;
828 if (transPtr->flags & TRCACHELOCKED) {
829 ReleaseReadLock(&dbase->cache_lock);
830 transPtr->flags &= ~TRCACHELOCKED;
833 if (transPtr->type != UBIK_READTRANS) {
834 /* must hold cache_lock before DBHOLD'ing */
835 ObtainWriteLock(&dbase->cache_lock);
841 /* give up if no longer current */
842 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
843 udisk_abort(transPtr);
850 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
851 code = udisk_commit(transPtr);
853 goto success; /* update cachedVersion correctly */
859 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
860 udisk_abort(transPtr);
867 /* now it is safe to do commit */
868 code = udisk_commit(transPtr);
870 /* db data has been committed locally; update the local cache so
871 * readers can get at it */
872 WritebackApplicationCache(dbase);
874 ReleaseWriteLock(&dbase->cache_lock);
876 code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
879 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
880 ReleaseWriteLock(&dbase->cache_lock);
884 /* failed to commit, so must return failure. Try to clear locks first, just for fun
885 * Note that we don't know if this transaction will eventually commit at this point.
886 * If it made it to a site that will be present in the next quorum, we win, otherwise
887 * we lose. If we contact a majority of sites, then we won't be here: contacting
888 * a majority guarantees commit, since it guarantees that one dude will be a
889 * member of the next quorum. */
890 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
895 /* before we can start sending unlock messages, we must wait until all servers
896 * that are possibly still functioning on the other side of a network partition
897 * have timed out. Check the server structures, compute how long to wait, then
898 * start the unlocks */
899 realStart = FT_ApproxTime();
901 /* wait for all servers to time out */
903 now = FT_ApproxTime();
904 /* check if we're still sync site, the guy should either come up
905 * to us, or timeout. Put safety check in anyway */
906 if (now - realStart > 10 * BIGTIME) {
907 ubik_stats.escapes++;
908 ubik_print("ubik escaping from commit wait\n");
911 for (ts = ubik_servers; ts; ts = ts->next) {
913 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
916 /* this guy could have some damaged data, wait for him */
918 tv.tv_sec = 1; /* try again after a while (ha ha) */
921 #ifdef AFS_PTHREAD_ENV
922 /* we could release the dbase outside of the loop, but we do
923 * it here, in the loop, to avoid an unnecessary RELE/HOLD
924 * if all sites are up */
926 select(0, 0, 0, 0, &tv);
929 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
937 break; /* no down ones still pseudo-active */
940 /* finally, unlock all the dudes. We can return success independent of the number of servers
941 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
942 * The transaction is committed anyway, since we succeeded in contacting a quorum
943 * at the start (when invoking the DiskCommit function).
945 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
949 /* don't update cachedVersion here; it should have been updated way back
950 * in ubik_CheckCache, and earlier in this function for writes */
953 ReleaseWriteLock(&dbase->cache_lock);
959 ObtainWriteLock(&dbase->cache_lock);
961 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
962 ReleaseWriteLock(&dbase->cache_lock);
967 * \brief This routine reads length bytes into buffer from the current position in the database.
969 * 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.
971 * \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.
974 ubik_Read(struct ubik_trans *transPtr, void *buffer,
979 /* reads are easy to do: handle locally */
980 DBHOLD(transPtr->dbase);
981 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
982 DBRELE(transPtr->dbase);
987 udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
990 transPtr->seekPos += length;
992 DBRELE(transPtr->dbase);
997 * \brief This routine will flush the io data in the iovec structures.
999 * It first flushes to the local disk and then uses ContactQuorum to write it
1000 * to the other servers.
1003 ubik_Flush(struct ubik_trans *transPtr)
1005 afs_int32 code, error = 0;
1007 if (transPtr->type != UBIK_WRITETRANS)
1010 DBHOLD(transPtr->dbase);
1011 if (!transPtr->iovec_info.iovec_wrt_len
1012 || !transPtr->iovec_info.iovec_wrt_val) {
1013 DBRELE(transPtr->dbase);
1017 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1018 ERROR_EXIT(UNOQUORUM);
1019 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1020 ERROR_EXIT(UNOTSYNC);
1022 /* Update the rest of the servers in the quorum */
1024 ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
1025 &transPtr->iovec_data);
1027 udisk_abort(transPtr);
1028 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1029 transPtr->iovec_info.iovec_wrt_len = 0;
1030 transPtr->iovec_data.iovec_buf_len = 0;
1034 /* Wrote the buffers out, so start at scratch again */
1035 transPtr->iovec_info.iovec_wrt_len = 0;
1036 transPtr->iovec_data.iovec_buf_len = 0;
1039 DBRELE(transPtr->dbase);
1044 ubik_Write(struct ubik_trans *transPtr, void *vbuffer,
1047 struct ubik_iovec *iovec;
1048 afs_int32 code, error = 0;
1049 afs_int32 pos, len, size;
1050 char * buffer = (char *)vbuffer;
1052 if (transPtr->type != UBIK_WRITETRANS)
1057 if (length > IOVEC_MAXBUF) {
1058 for (pos = 0, len = length; len > 0; len -= size, pos += size) {
1059 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
1060 code = ubik_Write(transPtr, buffer+pos, size);
1067 DBHOLD(transPtr->dbase);
1068 if (!transPtr->iovec_info.iovec_wrt_val) {
1069 transPtr->iovec_info.iovec_wrt_len = 0;
1070 transPtr->iovec_info.iovec_wrt_val =
1071 malloc(IOVEC_MAXWRT * sizeof(struct ubik_iovec));
1072 transPtr->iovec_data.iovec_buf_len = 0;
1073 transPtr->iovec_data.iovec_buf_val = malloc(IOVEC_MAXBUF);
1074 if (!transPtr->iovec_info.iovec_wrt_val
1075 || !transPtr->iovec_data.iovec_buf_val) {
1076 if (transPtr->iovec_info.iovec_wrt_val)
1077 free(transPtr->iovec_info.iovec_wrt_val);
1078 transPtr->iovec_info.iovec_wrt_val = 0;
1079 if (transPtr->iovec_data.iovec_buf_val)
1080 free(transPtr->iovec_data.iovec_buf_val);
1081 transPtr->iovec_data.iovec_buf_val = 0;
1082 DBRELE(transPtr->dbase);
1087 /* If this write won't fit in the structure, then flush it out and start anew */
1088 if ((transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT)
1089 || ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF)) {
1090 /* Can't hold the DB lock over ubik_Flush */
1091 DBRELE(transPtr->dbase);
1092 code = ubik_Flush(transPtr);
1095 DBHOLD(transPtr->dbase);
1098 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1099 ERROR_EXIT(UNOQUORUM);
1100 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1101 ERROR_EXIT(UNOTSYNC);
1103 /* Write to the local disk */
1105 udisk_write(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
1108 udisk_abort(transPtr);
1109 transPtr->iovec_info.iovec_wrt_len = 0;
1110 transPtr->iovec_data.iovec_buf_len = 0;
1111 DBRELE(transPtr->dbase);
1115 /* Collect writes for the other ubik servers (to be done in bulk) */
1116 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
1117 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
1118 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
1119 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
1121 memcpy(&transPtr->iovec_data.
1122 iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
1124 transPtr->iovec_info.iovec_wrt_len++;
1125 transPtr->iovec_data.iovec_buf_len += length;
1126 transPtr->seekPos += length;
1129 DBRELE(transPtr->dbase);
1134 * \brief This sets the file pointer associated with the current transaction
1135 * to the appropriate file and byte position.
1137 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1138 * and a byte position relative to the specified file \p position.
1141 ubik_Seek(struct ubik_trans *transPtr, afs_int32 fileid,
1146 DBHOLD(transPtr->dbase);
1147 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
1150 transPtr->seekFile = fileid;
1151 transPtr->seekPos = position;
1154 DBRELE(transPtr->dbase);
1159 * \brief This call returns the file pointer associated with the specified
1160 * transaction in \p fileid and \p position.
1163 ubik_Tell(struct ubik_trans *transPtr, afs_int32 * fileid,
1164 afs_int32 * position)
1166 DBHOLD(transPtr->dbase);
1167 *fileid = transPtr->seekFile;
1168 *position = transPtr->seekPos;
1169 DBRELE(transPtr->dbase);
1174 * \brief This sets the file size for the currently-selected file to \p length
1175 * bytes, if length is less than the file's current size.
1178 ubik_Truncate(struct ubik_trans *transPtr, afs_int32 length)
1180 afs_int32 code, error = 0;
1182 /* Will also catch if not UBIK_WRITETRANS */
1183 code = ubik_Flush(transPtr);
1187 DBHOLD(transPtr->dbase);
1188 /* first, check that quorum is still good, and that dbase is up-to-date */
1189 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1190 ERROR_EXIT(UNOQUORUM);
1191 if (!ubeacon_AmSyncSite())
1192 ERROR_EXIT(UNOTSYNC);
1194 /* now do the operation locally, and propagate it out */
1195 code = udisk_truncate(transPtr, transPtr->seekFile, length);
1198 ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
1202 /* we must abort the operation */
1203 udisk_abort(transPtr);
1204 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1209 DBRELE(transPtr->dbase);
1214 * \brief set a lock; all locks are released on transaction end (commit/abort)
1217 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
1220 afs_int32 code = 0, error = 0;
1222 if (atype == LOCKWRITE) {
1223 if (atrans->type == UBIK_READTRANS)
1225 code = ubik_Flush(atrans);
1230 DBHOLD(atrans->dbase);
1231 if (atype == LOCKREAD) {
1232 code = ulock_getLock(atrans, atype, 1);
1236 /* first, check that quorum is still good, and that dbase is up-to-date */
1237 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
1238 ERROR_EXIT(UNOQUORUM);
1239 if (!ubeacon_AmSyncSite())
1240 ERROR_EXIT(UNOTSYNC);
1242 /* now do the operation locally, and propagate it out */
1243 code = ulock_getLock(atrans, atype, 1);
1245 code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
1246 1 /*unused */ , LOCKWRITE);
1249 /* we must abort the operation */
1250 udisk_abort(atrans);
1251 ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
1257 DBRELE(atrans->dbase);
1262 * \brief utility to wait for a version # to change
1265 ubik_WaitVersion(struct ubik_dbase *adatabase,
1266 struct ubik_version *aversion)
1270 /* wait until version # changes, and then return */
1271 if (vcmp(*aversion, adatabase->version) != 0) {
1275 #ifdef AFS_PTHREAD_ENV
1276 opr_cv_wait(&adatabase->version_cond, &adatabase->versionLock);
1279 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
1286 * \brief utility to get the version of the dbase a transaction is dealing with
1289 ubik_GetVersion(struct ubik_trans *atrans,
1290 struct ubik_version *avers)
1292 DBHOLD(atrans->dbase);
1293 *avers = atrans->dbase->version;
1294 DBRELE(atrans->dbase);
1299 * \brief Facility to simplify database caching.
1300 * \return zero if last trans was done on the local server and was successful.
1301 * \return -1 means bad (NULL) argument.
1303 * If return value is non-zero and the caller is a server caching part of the
1304 * Ubik database, it should invalidate that cache.
1307 ubik_CacheUpdate(struct ubik_trans *atrans)
1309 if (!(atrans && atrans->dbase))
1311 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
1315 * check and possibly update cache of ubik db.
1317 * If the version of the cached db data is out of date, this calls (*check) to
1318 * update the cache. If (*check) returns success, we update the version of the
1321 * Checking the version of the cached db data is done under a read lock;
1322 * updating the cache (and thus calling (*check)) is done under a write lock
1323 * so is guaranteed not to interfere with another thread's (*check). On
1324 * successful return, a read lock on the cached db data is obtained, which
1325 * will be released by ubik_EndTrans or ubik_AbortTrans.
1327 * @param[in] atrans ubik transaction
1328 * @param[in] check function to call to check/update cache
1329 * @param[in] rock rock to pass to *check
1331 * @return operation status
1333 * @retval nonzero error; cachedVersion not updated
1335 * @post On success, application cache is read-locked, and cache data is
1339 ubik_CheckCache(struct ubik_trans *atrans, ubik_updatecache_func cbf, void *rock)
1343 if (!(atrans && atrans->dbase))
1346 ObtainReadLock(&atrans->dbase->cache_lock);
1348 while (ubik_CacheUpdate(atrans) != 0) {
1350 ReleaseReadLock(&atrans->dbase->cache_lock);
1351 ObtainSharedLock(&atrans->dbase->cache_lock);
1353 if (ubik_CacheUpdate(atrans) != 0) {
1355 BoostSharedLock(&atrans->dbase->cache_lock);
1357 ret = (*cbf) (atrans, rock);
1359 memcpy(&atrans->dbase->cachedVersion, &atrans->dbase->version,
1360 sizeof(atrans->dbase->cachedVersion));
1364 /* It would be nice if we could convert from a shared lock to a read
1365 * lock... instead, just release the shared and acquire the read */
1366 ReleaseSharedLock(&atrans->dbase->cache_lock);
1369 /* if we have an error, don't retry, and don't hold any locks */
1373 ObtainReadLock(&atrans->dbase->cache_lock);
1376 atrans->flags |= TRCACHELOCKED;
1382 * "Who said anything about panicking?" snapped Arthur.
1383 * "This is still just the culture shock. You wait till I've settled down
1384 * into the situation and found my bearings. \em Then I'll start panicking!"
1387 * \returns There is no return from panic.
1390 panic(char *format, ...)
1394 va_start(ap, format);
1395 ubik_print("Ubik PANIC:\n");
1396 ubik_vprint(format, ap);
1400 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
1401 exit(1); /* never know, though */
1405 * This function takes an IP addresses as its parameter. It returns the
1406 * the primary IP address that is on the host passed in, or 0 if not found.
1409 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
1411 struct ubik_server *ts;
1415 for (ts = ubik_servers; ts; ts = ts->next)
1416 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
1417 if (ts->addr[j] == addr) {
1419 return ts->addr[0]; /* net byte order */
1422 return 0; /* if not in server database, return error */
1426 ubik_CheckAuth(struct rx_call *acall)
1428 if (checkSecurityProc)
1429 return (*checkSecurityProc) (securityRock, acall);
1430 else if (ubik_CheckRXSecurityProc) {
1431 return (*ubik_CheckRXSecurityProc) (ubik_CheckRXSecurityRock, acall);
1437 ubik_SetServerSecurityProcs(void (*buildproc) (void *,
1438 struct rx_securityClass ***,
1440 int (*checkproc) (void *, struct rx_call *),
1443 buildSecClassesProc = buildproc;
1444 checkSecurityProc = checkproc;
1445 securityRock = rock;