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>
15 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <sys/param.h>
31 #include <afs/cellconfig.h>
33 #define UBIK_INTERNALS
37 #include <lwp.h> /* temporary hack by klm */
39 #define ERROR_EXIT(code) do { \
46 * This system is organized in a hierarchical set of related modules. Modules
47 * at one level can only call modules at the same level or below.
49 * At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
50 * operating system primitives.
52 * At the next level (1) we have
54 * \li VOTER--The module responsible for casting votes when asked. It is also
55 * responsible for determining whether this server should try to become
56 * a synchronization site.
57 * \li BEACONER--The module responsible for sending keep-alives out when a
58 * server is actually the sync site, or trying to become a sync site.
59 * \li DISK--The module responsible for representing atomic transactions
60 * on the local disk. It maintains a new-value only log.
61 * \li LOCK--The module responsible for locking byte ranges in the database file.
63 * At the next level (2) we have
65 * \li RECOVERY--The module responsible for ensuring that all members of a quorum
66 * have the same up-to-date database after a new synchronization site is
67 * elected. This module runs only on the synchronization site.
69 * At the next level (3) we have
71 * \li REMOTE--The module responsible for interpreting requests from the sync
72 * site and applying them to the database, after obtaining the appropriate
75 * At the next level (4) we have
77 * \li UBIK--The module users call to perform operations on the database.
82 afs_int32 ubik_quorum = 0;
83 struct ubik_dbase *ubik_dbase = 0;
84 struct ubik_stats ubik_stats;
85 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
86 afs_int32 ubik_epochTime = 0;
87 afs_int32 urecovery_state = 0;
88 int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **, afs_int32 *);
89 void *ubik_SRXSecurityRock;
90 int (*ubik_SyncWriterCacheProc) (void);
91 struct ubik_server *ubik_servers;
92 short ubik_callPortal;
94 static int BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
95 struct ubik_trans **transPtr, int readAny);
97 struct rx_securityClass *ubik_sc[3];
99 #define CStampVersion 1 /* meaning set ts->version */
101 static_inline struct rx_connection *
102 Quorum_StartIO(struct ubik_trans *atrans, struct ubik_server *as)
104 struct rx_connection *conn;
106 conn = as->disk_rxcid;
108 #ifdef AFS_PTHREAD_ENV
109 rx_GetConnection(conn);
110 DBRELE(atrans->dbase);
111 #endif /* AFS_PTHREAD_ENV */
117 Quorum_EndIO(struct ubik_trans *atrans, struct rx_connection *aconn)
119 #ifdef AFS_PTHREAD_ENV
120 DBHOLD(atrans->dbase);
121 rx_PutConnection(aconn);
122 #endif /* AFS_PTHREAD_ENV */
127 * Iterate over all servers. Callers pass in *ts which is used to track
128 * the current server.
129 * - Returns 1 if there are no more servers
130 * - Returns 0 with conn set to the connection for the current server if
131 * it's up and current
134 ContactQuorum_iterate(struct ubik_trans *atrans, int aflags, struct ubik_server **ts,
135 struct rx_connection **conn, afs_int32 *rcode,
136 afs_int32 *okcalls, afs_int32 code)
139 /* Initial call - start iterating over servers */
146 Quorum_EndIO(atrans, *conn);
148 if (code) { /* failure */
150 (*ts)->up = 0; /* mark as down now; beacons will no longer be sent */
151 (*ts)->beaconSinceDown = 0;
152 (*ts)->currentDB = 0;
153 urecovery_LostServer(*ts); /* tell recovery to try to resend dbase later */
154 } else { /* success */
156 (*okcalls)++; /* count up how many worked */
157 if (aflags & CStampVersion) {
158 (*ts)->version = atrans->dbase->version;
166 if (!(*ts)->up || !(*ts)->currentDB) {
167 (*ts)->currentDB = 0; /* db is no longer current; we just missed an update */
168 return 0; /* not up-to-date, don't bother. NULL conn will tell caller not to use */
170 *conn = Quorum_StartIO(atrans, *ts);
175 ContactQuorum_rcode(int okcalls, afs_int32 rcode)
178 * return 0 if we successfully contacted a quorum, otherwise return error code.
179 * We don't have to contact ourselves (that was done locally)
181 if (okcalls + 1 >= ubik_quorum)
188 * \brief Perform an operation at a quorum, handling error conditions.
189 * \return 0 if all worked and a quorum was contacted successfully
190 * \return otherwise mark failing server as down and return #UERROR
192 * \note If any server misses an update, we must wait #BIGTIME seconds before
193 * allowing the transaction to commit, to ensure that the missing and
194 * possibly still functioning server times out and stops handing out old
195 * data. This is done in the commit code, where we wait for a server marked
196 * down to have stayed down for #BIGTIME seconds before we allow a transaction
197 * to commit. A server that fails but comes back up won't give out old data
198 * because it is sent the sync count along with the beacon message that
199 * marks it as \b really up (\p beaconSinceDown).
202 ContactQuorum_NoArguments(afs_int32 (*proc)(struct rx_connection *, ubik_tid *),
203 struct ubik_trans *atrans, int aflags)
205 struct ubik_server *ts = NULL;
206 afs_int32 code = 0, rcode, okcalls;
207 struct rx_connection *conn;
210 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
213 code = (*proc)(conn, &atrans->tid);
214 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
216 return ContactQuorum_rcode(okcalls, rcode);
221 ContactQuorum_DISK_Lock(struct ubik_trans *atrans, int aflags,afs_int32 file,
222 afs_int32 position, afs_int32 length, afs_int32 type)
224 struct ubik_server *ts = NULL;
225 afs_int32 code = 0, rcode, okcalls;
226 struct rx_connection *conn;
229 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
232 code = DISK_Lock(conn, &atrans->tid, file, position, length, type);
233 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
235 return ContactQuorum_rcode(okcalls, rcode);
240 ContactQuorum_DISK_Write(struct ubik_trans *atrans, int aflags,
241 afs_int32 file, afs_int32 position, bulkdata *data)
243 struct ubik_server *ts = NULL;
244 afs_int32 code = 0, rcode, okcalls;
245 struct rx_connection *conn;
248 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
251 code = DISK_Write(conn, &atrans->tid, file, position, data);
252 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
254 return ContactQuorum_rcode(okcalls, rcode);
259 ContactQuorum_DISK_Truncate(struct ubik_trans *atrans, int aflags,
260 afs_int32 file, afs_int32 length)
262 struct ubik_server *ts = NULL;
263 afs_int32 code = 0, rcode, okcalls;
264 struct rx_connection *conn;
267 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
270 code = DISK_Truncate(conn, &atrans->tid, file, length);
271 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
273 return ContactQuorum_rcode(okcalls, rcode);
278 ContactQuorum_DISK_WriteV(struct ubik_trans *atrans, int aflags,
279 iovec_wrt * io_vector, iovec_buf *io_buffer)
281 struct ubik_server *ts = NULL;
282 afs_int32 code = 0, rcode, okcalls;
283 struct rx_connection *conn;
286 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
289 code = DISK_WriteV(conn, &atrans->tid, io_vector, io_buffer);
290 if ((code <= -450) && (code > -500)) {
291 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
292 * Un-bulk the entries and do individual DISK_Write calls
293 * instead of DISK_WriteV.
295 struct ubik_iovec *iovec =
296 (struct ubik_iovec *)io_vector->iovec_wrt_val;
297 char *iobuf = (char *)io_buffer->iovec_buf_val;
301 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
302 /* Sanity check for going off end of buffer */
303 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
307 tcbs.bulkdata_len = iovec[i].length;
308 tcbs.bulkdata_val = &iobuf[offset];
309 code = DISK_Write(conn, &atrans->tid, iovec[i].file,
310 iovec[i].position, &tcbs);
313 offset += iovec[i].length;
317 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
319 return ContactQuorum_rcode(okcalls, rcode);
324 ContactQuorum_DISK_SetVersion(struct ubik_trans *atrans, int aflags,
325 ubik_version *OldVersion,
326 ubik_version *NewVersion)
328 struct ubik_server *ts = NULL;
329 afs_int32 code = 0, rcode, okcalls;
330 struct rx_connection *conn;
333 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
336 code = DISK_SetVersion(conn, &atrans->tid, OldVersion, NewVersion);
337 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
339 return ContactQuorum_rcode(okcalls, rcode);
342 #if defined(AFS_PTHREAD_ENV)
344 ubik_thread_create(pthread_attr_t *tattr, pthread_t *thread, void *proc) {
345 osi_Assert(pthread_attr_init(tattr) == 0);
346 osi_Assert(pthread_attr_setdetachstate(tattr, PTHREAD_CREATE_DETACHED) == 0);
347 osi_Assert(pthread_create(thread, tattr, proc, NULL) == 0);
353 * \brief This routine initializes the ubik system for a set of servers.
354 * \return 0 for success, or an error code on failure.
355 * \param serverList set of servers specified; nServers gives the number of entries in this array.
356 * \param pathName provides an initial prefix used for naming storage files used by this system.
357 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
359 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
361 * \warning The host named by myHost should not also be listed in serverList.
363 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
366 ubik_ServerInitCommon(afs_uint32 myHost, short myPort,
367 struct afsconf_cell *info, char clones[],
368 afs_uint32 serverList[], const char *pathName,
369 struct ubik_dbase **dbase)
371 struct ubik_dbase *tdb;
373 #ifdef AFS_PTHREAD_ENV
374 pthread_t rxServerThread; /* pthread variables */
375 pthread_t ubeacon_InteractThread;
376 pthread_t urecovery_InteractThread;
377 pthread_attr_t rxServer_tattr;
378 pthread_attr_t ubeacon_Interact_tattr;
379 pthread_attr_t urecovery_Interact_tattr;
382 extern int rx_stackSize;
386 struct rx_securityClass *secClass;
388 struct rx_service *tservice;
390 initialize_U_error_table();
392 tdb = (struct ubik_dbase *)malloc(sizeof(struct ubik_dbase));
393 tdb->pathName = (char *)malloc(strlen(pathName) + 1);
394 strcpy(tdb->pathName, pathName);
395 tdb->activeTrans = (struct ubik_trans *)0;
396 memset(&tdb->version, 0, sizeof(struct ubik_version));
397 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
398 #ifdef AFS_PTHREAD_ENV
399 MUTEX_INIT(&tdb->versionLock, "version lock", MUTEX_DEFAULT, 0);
401 Lock_Init(&tdb->versionLock);
403 Lock_Init(&tdb->cache_lock);
405 tdb->read = uphys_read;
406 tdb->write = uphys_write;
407 tdb->truncate = uphys_truncate;
408 tdb->open = uphys_invalidate; /* this function isn't used any more */
409 tdb->sync = uphys_sync;
410 tdb->stat = uphys_stat;
411 tdb->getlabel = uphys_getlabel;
412 tdb->setlabel = uphys_setlabel;
413 tdb->getnfiles = uphys_getnfiles;
415 tdb->tidCounter = tdb->writeTidCounter = 0;
417 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
419 #ifdef AFS_PTHREAD_ENV
420 CV_INIT(&tdb->version_cond, "version", CV_DEFAULT, 0);
421 CV_INIT(&tdb->flags_cond, "flags", CV_DEFAULT, 0);
422 #endif /* AFS_PTHREAD_ENV */
426 /* the following call is idempotent so when/if it got called earlier,
427 * by whatever called us, it doesn't really matter -- klm */
428 code = rx_Init(myPort);
432 udisk_Init(ubik_nBuffers);
438 code = urecovery_Initialize(tdb);
442 code = ubeacon_InitServerListByInfo(myHost, info, clones);
444 code = ubeacon_InitServerList(myHost, serverList);
448 ubik_callPortal = myPort;
449 /* try to get an additional security object */
450 ubik_sc[0] = rxnull_NewServerSecurityObject();
453 if (ubik_SRXSecurityProc) {
455 (*ubik_SRXSecurityProc) (ubik_SRXSecurityRock, &secClass,
458 ubik_sc[secIndex] = secClass;
461 /* for backwards compat this should keep working as it does now
465 rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, 3,
466 VOTE_ExecuteRequest);
467 if (tservice == (struct rx_service *)0) {
468 ubik_dprint("Could not create VOTE rx service!\n");
471 rx_SetMinProcs(tservice, 2);
472 rx_SetMaxProcs(tservice, 3);
475 rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, 3,
476 DISK_ExecuteRequest);
477 if (tservice == (struct rx_service *)0) {
478 ubik_dprint("Could not create DISK rx service!\n");
481 rx_SetMinProcs(tservice, 2);
482 rx_SetMaxProcs(tservice, 3);
484 /* start an rx_ServerProc to handle incoming RPC's in particular the
485 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
486 * the "steplock" problem in ubik initialization. Defect 11037.
488 #ifdef AFS_PTHREAD_ENV
489 ubik_thread_create(&rxServer_tattr, &rxServerThread, (void *)rx_ServerProc);
491 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
492 NULL, "rx_ServerProc", &junk);
495 /* now start up async processes */
496 #ifdef AFS_PTHREAD_ENV
497 ubik_thread_create(&ubeacon_Interact_tattr, &ubeacon_InteractThread,
498 (void *)ubeacon_Interact);
500 code = LWP_CreateProcess(ubeacon_Interact, 16384 /*8192 */ ,
501 LWP_MAX_PRIORITY - 1, (void *)0, "beacon",
507 #ifdef AFS_PTHREAD_ENV
508 ubik_thread_create(&urecovery_Interact_tattr, &urecovery_InteractThread,
509 (void *)urecovery_Interact);
510 return 0; /* is this correct? - klm */
512 code = LWP_CreateProcess(urecovery_Interact, 16384 /*8192 */ ,
513 LWP_MAX_PRIORITY - 1, (void *)0, "recovery",
521 * \see ubik_ServerInitCommon()
524 ubik_ServerInitByInfo(afs_uint32 myHost, short myPort,
525 struct afsconf_cell *info, char clones[],
526 const char *pathName, struct ubik_dbase **dbase)
531 ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName,
537 * \see ubik_ServerInitCommon()
540 ubik_ServerInit(afs_uint32 myHost, short myPort, afs_uint32 serverList[],
541 const char *pathName, struct ubik_dbase **dbase)
546 ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
547 serverList, pathName, dbase);
552 * \brief This routine begins a read or write transaction on the transaction
553 * identified by transPtr, in the dbase named by dbase.
555 * An open mode of ubik_READTRANS identifies this as a read transaction,
556 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
557 * transPtr is set to the returned transaction control block.
558 * The readAny flag is set to 0 or 1 or 2 by the wrapper functions
559 * ubik_BeginTrans() or ubik_BeginTransReadAny() or
560 * ubik_BeginTransReadAnyWrite() below.
562 * \note We can only begin transaction when we have an up-to-date database.
565 BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
566 struct ubik_trans **transPtr, int readAny)
568 struct ubik_trans *jt;
569 struct ubik_trans *tt;
572 if (readAny > 1 && ubik_SyncWriterCacheProc == NULL) {
573 /* it's not safe to use ubik_BeginTransReadAnyWrite without a
574 * cache-syncing function; fall back to ubik_BeginTransReadAny,
575 * which is safe but slower */
576 ubik_print("ubik_BeginTransReadAnyWrite called, but "
577 "ubik_SyncWriterCacheProc not set; pretending "
578 "ubik_BeginTransReadAny was called instead\n");
582 if ((transMode != UBIK_READTRANS) && readAny)
585 if (urecovery_AllBetter(dbase, readAny) == 0) {
589 /* otherwise we have a quorum, use it */
591 /* make sure that at most one write transaction occurs at any one time. This
592 * has nothing to do with transaction locking; that's enforced by the lock package. However,
593 * we can't even handle two non-conflicting writes, since our log and recovery modules
594 * don't know how to restore one without possibly picking up some data from the other. */
595 if (transMode == UBIK_WRITETRANS) {
596 /* if we're writing already, wait */
597 while (dbase->flags & DBWRITING) {
598 #ifdef AFS_PTHREAD_ENV
599 CV_WAIT(&dbase->flags_cond, &dbase->versionLock);
602 LWP_WaitProcess(&dbase->flags);
607 if (!ubeacon_AmSyncSite()) {
613 /* create the transaction */
614 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
615 tt = jt; /* move to a register */
616 if (code || tt == (struct ubik_trans *)NULL) {
621 tt->flags |= TRREADANY;
623 tt->flags |= TRREADWRITE;
626 /* label trans and dbase with new tid */
627 tt->tid.epoch = ubik_epochTime;
628 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
629 tt->tid.counter = (dbase->tidCounter += 2);
631 if (transMode == UBIK_WRITETRANS) {
632 /* for a write trans, we have to keep track of the write tid counter too */
633 dbase->writeTidCounter = tt->tid.counter;
635 /* next try to start transaction on appropriate number of machines */
636 code = ContactQuorum_NoArguments(DISK_Begin, tt, 0);
638 /* we must abort the operation */
640 ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
656 ubik_BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
657 struct ubik_trans **transPtr)
659 return BeginTrans(dbase, transMode, transPtr, 0);
666 ubik_BeginTransReadAny(struct ubik_dbase *dbase, afs_int32 transMode,
667 struct ubik_trans **transPtr)
669 return BeginTrans(dbase, transMode, transPtr, 1);
676 ubik_BeginTransReadAnyWrite(struct ubik_dbase *dbase, afs_int32 transMode,
677 struct ubik_trans **transPtr)
679 return BeginTrans(dbase, transMode, transPtr, 2);
683 * \brief This routine ends a read or write transaction by aborting it.
686 ubik_AbortTrans(struct ubik_trans *transPtr)
690 struct ubik_dbase *dbase;
692 dbase = transPtr->dbase;
694 if (transPtr->flags & TRCACHELOCKED) {
695 ReleaseReadLock(&dbase->cache_lock);
696 transPtr->flags &= ~TRCACHELOCKED;
699 ObtainWriteLock(&dbase->cache_lock);
702 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
704 ReleaseWriteLock(&dbase->cache_lock);
706 /* see if we're still up-to-date */
707 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
708 udisk_abort(transPtr);
714 if (transPtr->type == UBIK_READTRANS) {
715 code = udisk_abort(transPtr);
721 /* below here, we know we're doing a write transaction */
722 if (!ubeacon_AmSyncSite()) {
723 udisk_abort(transPtr);
729 /* now it is safe to try remote abort */
730 code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
731 code2 = udisk_abort(transPtr);
734 return (code ? code : code2);
738 WritebackApplicationCache(struct ubik_dbase *dbase)
741 if (ubik_SyncWriterCacheProc) {
742 code = ubik_SyncWriterCacheProc();
745 /* we failed to sync the local cache, so just invalidate the cache;
746 * we'll try to read the cache in again on the next read */
747 memset(&dbase->cachedVersion, 0, sizeof(dbase->cachedVersion));
749 memcpy(&dbase->cachedVersion, &dbase->version,
750 sizeof(dbase->cachedVersion));
755 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
756 * \return an error code.
759 ubik_EndTrans(struct ubik_trans *transPtr)
764 struct ubik_server *ts;
767 struct ubik_dbase *dbase;
769 if (transPtr->type == UBIK_WRITETRANS) {
770 code = ubik_Flush(transPtr);
772 ubik_AbortTrans(transPtr);
777 dbase = transPtr->dbase;
779 if (transPtr->flags & TRCACHELOCKED) {
780 ReleaseReadLock(&dbase->cache_lock);
781 transPtr->flags &= ~TRCACHELOCKED;
784 if (transPtr->type != UBIK_READTRANS) {
785 /* must hold cache_lock before DBHOLD'ing */
786 ObtainWriteLock(&dbase->cache_lock);
792 /* give up if no longer current */
793 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
794 udisk_abort(transPtr);
801 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
802 code = udisk_commit(transPtr);
804 goto success; /* update cachedVersion correctly */
810 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
811 udisk_abort(transPtr);
818 /* now it is safe to do commit */
819 code = udisk_commit(transPtr);
821 /* db data has been committed locally; update the local cache so
822 * readers can get at it */
823 WritebackApplicationCache(dbase);
825 ReleaseWriteLock(&dbase->cache_lock);
827 code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
830 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
831 ReleaseWriteLock(&dbase->cache_lock);
835 /* failed to commit, so must return failure. Try to clear locks first, just for fun
836 * Note that we don't know if this transaction will eventually commit at this point.
837 * If it made it to a site that will be present in the next quorum, we win, otherwise
838 * we lose. If we contact a majority of sites, then we won't be here: contacting
839 * a majority guarantees commit, since it guarantees that one dude will be a
840 * member of the next quorum. */
841 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
846 /* before we can start sending unlock messages, we must wait until all servers
847 * that are possibly still functioning on the other side of a network partition
848 * have timed out. Check the server structures, compute how long to wait, then
849 * start the unlocks */
850 realStart = FT_ApproxTime();
852 /* wait for all servers to time out */
854 now = FT_ApproxTime();
855 /* check if we're still sync site, the guy should either come up
856 * to us, or timeout. Put safety check in anyway */
857 if (now - realStart > 10 * BIGTIME) {
858 ubik_stats.escapes++;
859 ubik_print("ubik escaping from commit wait\n");
862 for (ts = ubik_servers; ts; ts = ts->next) {
863 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
865 /* this guy could have some damaged data, wait for him */
867 tv.tv_sec = 1; /* try again after a while (ha ha) */
870 #ifdef AFS_PTHREAD_ENV
871 /* we could release the dbase outside of the loop, but we do
872 * it here, in the loop, to avoid an unnecessary RELE/HOLD
873 * if all sites are up */
875 select(0, 0, 0, 0, &tv);
878 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
885 break; /* no down ones still pseudo-active */
888 /* finally, unlock all the dudes. We can return success independent of the number of servers
889 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
890 * The transaction is committed anyway, since we succeeded in contacting a quorum
891 * at the start (when invoking the DiskCommit function).
893 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
897 /* don't update cachedVersion here; it should have been updated way back
898 * in ubik_CheckCache, and earlier in this function for writes */
901 ReleaseWriteLock(&dbase->cache_lock);
907 ObtainWriteLock(&dbase->cache_lock);
909 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
910 ReleaseWriteLock(&dbase->cache_lock);
915 * \brief This routine reads length bytes into buffer from the current position in the database.
917 * 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.
919 * \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.
922 ubik_Read(struct ubik_trans *transPtr, void *buffer,
927 /* reads are easy to do: handle locally */
928 DBHOLD(transPtr->dbase);
929 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
930 DBRELE(transPtr->dbase);
935 udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
938 transPtr->seekPos += length;
940 DBRELE(transPtr->dbase);
945 * \brief This routine will flush the io data in the iovec structures.
947 * It first flushes to the local disk and then uses ContactQuorum to write it
948 * to the other servers.
951 ubik_Flush(struct ubik_trans *transPtr)
953 afs_int32 code, error = 0;
955 if (transPtr->type != UBIK_WRITETRANS)
957 if (!transPtr->iovec_info.iovec_wrt_len
958 || !transPtr->iovec_info.iovec_wrt_val)
961 DBHOLD(transPtr->dbase);
962 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
963 ERROR_EXIT(UNOQUORUM);
964 if (!ubeacon_AmSyncSite()) /* only sync site can write */
965 ERROR_EXIT(UNOTSYNC);
967 /* Update the rest of the servers in the quorum */
969 ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
970 &transPtr->iovec_data);
972 udisk_abort(transPtr);
973 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
974 transPtr->iovec_info.iovec_wrt_len = 0;
975 transPtr->iovec_data.iovec_buf_len = 0;
979 /* Wrote the buffers out, so start at scratch again */
980 transPtr->iovec_info.iovec_wrt_len = 0;
981 transPtr->iovec_data.iovec_buf_len = 0;
984 DBRELE(transPtr->dbase);
989 ubik_Write(struct ubik_trans *transPtr, void *vbuffer,
992 struct ubik_iovec *iovec;
993 afs_int32 code, error = 0;
994 afs_int32 pos, len, size;
995 char * buffer = (char *)vbuffer;
997 if (transPtr->type != UBIK_WRITETRANS)
1002 if (length > IOVEC_MAXBUF) {
1003 for (pos = 0, len = length; len > 0; len -= size, pos += size) {
1004 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
1005 code = ubik_Write(transPtr, buffer+pos, size);
1012 if (!transPtr->iovec_info.iovec_wrt_val) {
1013 transPtr->iovec_info.iovec_wrt_len = 0;
1014 transPtr->iovec_info.iovec_wrt_val =
1015 (struct ubik_iovec *)malloc(IOVEC_MAXWRT *
1016 sizeof(struct ubik_iovec));
1017 transPtr->iovec_data.iovec_buf_len = 0;
1018 transPtr->iovec_data.iovec_buf_val = (char *)malloc(IOVEC_MAXBUF);
1019 if (!transPtr->iovec_info.iovec_wrt_val
1020 || !transPtr->iovec_data.iovec_buf_val) {
1021 if (transPtr->iovec_info.iovec_wrt_val)
1022 free(transPtr->iovec_info.iovec_wrt_val);
1023 transPtr->iovec_info.iovec_wrt_val = 0;
1024 if (transPtr->iovec_data.iovec_buf_val)
1025 free(transPtr->iovec_data.iovec_buf_val);
1026 transPtr->iovec_data.iovec_buf_val = 0;
1031 /* If this write won't fit in the structure, then flush it out and start anew */
1032 if ((transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT)
1033 || ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF)) {
1034 code = ubik_Flush(transPtr);
1039 DBHOLD(transPtr->dbase);
1040 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1041 ERROR_EXIT(UNOQUORUM);
1042 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1043 ERROR_EXIT(UNOTSYNC);
1045 /* Write to the local disk */
1047 udisk_write(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
1050 udisk_abort(transPtr);
1051 transPtr->iovec_info.iovec_wrt_len = 0;
1052 transPtr->iovec_data.iovec_buf_len = 0;
1053 DBRELE(transPtr->dbase);
1057 /* Collect writes for the other ubik servers (to be done in bulk) */
1058 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
1059 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
1060 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
1061 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
1063 memcpy(&transPtr->iovec_data.
1064 iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
1066 transPtr->iovec_info.iovec_wrt_len++;
1067 transPtr->iovec_data.iovec_buf_len += length;
1068 transPtr->seekPos += length;
1071 DBRELE(transPtr->dbase);
1076 * \brief This sets the file pointer associated with the current transaction
1077 * to the appropriate file and byte position.
1079 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1080 * and a byte position relative to the specified file \p position.
1083 ubik_Seek(struct ubik_trans *transPtr, afs_int32 fileid,
1088 DBHOLD(transPtr->dbase);
1089 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
1092 transPtr->seekFile = fileid;
1093 transPtr->seekPos = position;
1096 DBRELE(transPtr->dbase);
1101 * \brief This call returns the file pointer associated with the specified
1102 * transaction in \p fileid and \p position.
1105 ubik_Tell(struct ubik_trans *transPtr, afs_int32 * fileid,
1106 afs_int32 * position)
1108 DBHOLD(transPtr->dbase);
1109 *fileid = transPtr->seekFile;
1110 *position = transPtr->seekPos;
1111 DBRELE(transPtr->dbase);
1116 * \brief This sets the file size for the currently-selected file to \p length
1117 * bytes, if length is less than the file's current size.
1120 ubik_Truncate(struct ubik_trans *transPtr, afs_int32 length)
1122 afs_int32 code, error = 0;
1124 /* Will also catch if not UBIK_WRITETRANS */
1125 code = ubik_Flush(transPtr);
1129 DBHOLD(transPtr->dbase);
1130 /* first, check that quorum is still good, and that dbase is up-to-date */
1131 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1132 ERROR_EXIT(UNOQUORUM);
1133 if (!ubeacon_AmSyncSite())
1134 ERROR_EXIT(UNOTSYNC);
1136 /* now do the operation locally, and propagate it out */
1137 code = udisk_truncate(transPtr, transPtr->seekFile, length);
1140 ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
1144 /* we must abort the operation */
1145 udisk_abort(transPtr);
1146 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1151 DBRELE(transPtr->dbase);
1156 * \brief set a lock; all locks are released on transaction end (commit/abort)
1159 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
1162 afs_int32 code = 0, error = 0;
1164 if (atype == LOCKWRITE) {
1165 if (atrans->type == UBIK_READTRANS)
1167 code = ubik_Flush(atrans);
1172 DBHOLD(atrans->dbase);
1173 if (atype == LOCKREAD) {
1174 code = ulock_getLock(atrans, atype, 1);
1178 /* first, check that quorum is still good, and that dbase is up-to-date */
1179 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
1180 ERROR_EXIT(UNOQUORUM);
1181 if (!ubeacon_AmSyncSite())
1182 ERROR_EXIT(UNOTSYNC);
1184 /* now do the operation locally, and propagate it out */
1185 code = ulock_getLock(atrans, atype, 1);
1187 code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
1188 1 /*unused */ , LOCKWRITE);
1191 /* we must abort the operation */
1192 udisk_abort(atrans);
1193 ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
1199 DBRELE(atrans->dbase);
1204 * \brief utility to wait for a version # to change
1207 ubik_WaitVersion(struct ubik_dbase *adatabase,
1208 struct ubik_version *aversion)
1212 /* wait until version # changes, and then return */
1213 if (vcmp(*aversion, adatabase->version) != 0) {
1217 #ifdef AFS_PTHREAD_ENV
1218 CV_WAIT(&adatabase->version_cond, &adatabase->versionLock);
1221 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
1228 * \brief utility to get the version of the dbase a transaction is dealing with
1231 ubik_GetVersion(struct ubik_trans *atrans,
1232 struct ubik_version *avers)
1234 *avers = atrans->dbase->version;
1239 * \brief Facility to simplify database caching.
1240 * \return zero if last trans was done on the local server and was successful.
1241 * \return -1 means bad (NULL) argument.
1243 * If return value is non-zero and the caller is a server caching part of the
1244 * Ubik database, it should invalidate that cache.
1247 ubik_CacheUpdate(struct ubik_trans *atrans)
1249 if (!(atrans && atrans->dbase))
1251 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
1255 * check and possibly update cache of ubik db.
1257 * If the version of the cached db data is out of date, this calls (*check) to
1258 * update the cache. If (*check) returns success, we update the version of the
1261 * Checking the version of the cached db data is done under a read lock;
1262 * updating the cache (and thus calling (*check)) is done under a write lock
1263 * so is guaranteed not to interfere with another thread's (*check). On
1264 * successful return, a read lock on the cached db data is obtained, which
1265 * will be released by ubik_EndTrans or ubik_AbortTrans.
1267 * @param[in] atrans ubik transaction
1268 * @param[in] check function to call to check/update cache
1269 * @param[in] rock rock to pass to *check
1271 * @return operation status
1273 * @retval nonzero error; cachedVersion not updated
1275 * @post On success, application cache is read-locked, and cache data is
1279 ubik_CheckCache(struct ubik_trans *atrans, ubik_updatecache_func cbf, void *rock)
1283 if (!(atrans && atrans->dbase))
1286 ObtainReadLock(&atrans->dbase->cache_lock);
1288 while (ubik_CacheUpdate(atrans) != 0) {
1290 ReleaseReadLock(&atrans->dbase->cache_lock);
1291 ObtainSharedLock(&atrans->dbase->cache_lock);
1293 if (ubik_CacheUpdate(atrans) != 0) {
1295 BoostSharedLock(&atrans->dbase->cache_lock);
1297 ret = (*cbf) (atrans, rock);
1299 memcpy(&atrans->dbase->cachedVersion, &atrans->dbase->version,
1300 sizeof(atrans->dbase->cachedVersion));
1304 /* It would be nice if we could convert from a shared lock to a read
1305 * lock... instead, just release the shared and acquire the read */
1306 ReleaseSharedLock(&atrans->dbase->cache_lock);
1309 /* if we have an error, don't retry, and don't hold any locks */
1313 ObtainReadLock(&atrans->dbase->cache_lock);
1316 atrans->flags |= TRCACHELOCKED;
1322 * "Who said anything about panicking?" snapped Arthur.
1323 * "This is still just the culture shock. You wait till I've settled down
1324 * into the situation and found my bearings. \em Then I'll start panicking!"
1327 * \returns There is no return from panic.
1330 panic(char *format, ...)
1334 va_start(ap, format);
1335 ubik_print("Ubik PANIC: ");
1336 ubik_vprint(format, ap);
1340 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
1341 exit(1); /* never know, though */
1345 * This function takes an IP addresses as its parameter. It returns the
1346 * the primary IP address that is on the host passed in, or 0 if not found.
1349 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
1351 struct ubik_server *ts;
1354 for (ts = ubik_servers; ts; ts = ts->next)
1355 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
1356 if (ts->addr[j] == addr)
1357 return ts->addr[0]; /* net byte order */
1358 return 0; /* if not in server database, return error */