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 struct version_data version_globals;
103 #define CStampVersion 1 /* meaning set ts->version */
105 static_inline struct rx_connection *
106 Quorum_StartIO(struct ubik_trans *atrans, struct ubik_server *as)
108 struct rx_connection *conn;
111 conn = as->disk_rxcid;
113 #ifdef AFS_PTHREAD_ENV
114 rx_GetConnection(conn);
116 DBRELE(atrans->dbase);
119 #endif /* AFS_PTHREAD_ENV */
125 Quorum_EndIO(struct ubik_trans *atrans, struct rx_connection *aconn)
127 #ifdef AFS_PTHREAD_ENV
128 DBHOLD(atrans->dbase);
129 rx_PutConnection(aconn);
130 #endif /* AFS_PTHREAD_ENV */
135 * Iterate over all servers. Callers pass in *ts which is used to track
136 * the current server.
137 * - Returns 1 if there are no more servers
138 * - Returns 0 with conn set to the connection for the current server if
139 * it's up and current
142 ContactQuorum_iterate(struct ubik_trans *atrans, int aflags, struct ubik_server **ts,
143 struct rx_connection **conn, afs_int32 *rcode,
144 afs_int32 *okcalls, afs_int32 code)
147 /* Initial call - start iterating over servers */
154 Quorum_EndIO(atrans, *conn);
156 if (code) { /* failure */
159 (*ts)->up = 0; /* mark as down now; beacons will no longer be sent */
160 (*ts)->beaconSinceDown = 0;
162 (*ts)->currentDB = 0;
163 urecovery_LostServer(*ts); /* tell recovery to try to resend dbase later */
164 } else { /* success */
166 (*okcalls)++; /* count up how many worked */
167 if (aflags & CStampVersion) {
168 (*ts)->version = atrans->dbase->version;
177 if (!(*ts)->up || !(*ts)->currentDB) {
179 (*ts)->currentDB = 0; /* db is no longer current; we just missed an update */
180 return 0; /* not up-to-date, don't bother. NULL conn will tell caller not to use */
183 *conn = Quorum_StartIO(atrans, *ts);
188 ContactQuorum_rcode(int okcalls, afs_int32 rcode)
191 * return 0 if we successfully contacted a quorum, otherwise return error code.
192 * We don't have to contact ourselves (that was done locally)
194 if (okcalls + 1 >= ubik_quorum)
201 * \brief Perform an operation at a quorum, handling error conditions.
202 * \return 0 if all worked and a quorum was contacted successfully
203 * \return otherwise mark failing server as down and return #UERROR
205 * \note If any server misses an update, we must wait #BIGTIME seconds before
206 * allowing the transaction to commit, to ensure that the missing and
207 * possibly still functioning server times out and stops handing out old
208 * data. This is done in the commit code, where we wait for a server marked
209 * down to have stayed down for #BIGTIME seconds before we allow a transaction
210 * to commit. A server that fails but comes back up won't give out old data
211 * because it is sent the sync count along with the beacon message that
212 * marks it as \b really up (\p beaconSinceDown).
215 ContactQuorum_NoArguments(afs_int32 (*proc)(struct rx_connection *, ubik_tid *),
216 struct ubik_trans *atrans, int aflags)
218 struct ubik_server *ts = NULL;
219 afs_int32 code = 0, rcode, okcalls;
220 struct rx_connection *conn;
223 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
226 code = (*proc)(conn, &atrans->tid);
227 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
229 return ContactQuorum_rcode(okcalls, rcode);
234 ContactQuorum_DISK_Lock(struct ubik_trans *atrans, int aflags,afs_int32 file,
235 afs_int32 position, afs_int32 length, afs_int32 type)
237 struct ubik_server *ts = NULL;
238 afs_int32 code = 0, rcode, okcalls;
239 struct rx_connection *conn;
242 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
245 code = DISK_Lock(conn, &atrans->tid, file, position, length, type);
246 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
248 return ContactQuorum_rcode(okcalls, rcode);
253 ContactQuorum_DISK_Write(struct ubik_trans *atrans, int aflags,
254 afs_int32 file, afs_int32 position, bulkdata *data)
256 struct ubik_server *ts = NULL;
257 afs_int32 code = 0, rcode, okcalls;
258 struct rx_connection *conn;
261 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
264 code = DISK_Write(conn, &atrans->tid, file, position, data);
265 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
267 return ContactQuorum_rcode(okcalls, rcode);
272 ContactQuorum_DISK_Truncate(struct ubik_trans *atrans, int aflags,
273 afs_int32 file, afs_int32 length)
275 struct ubik_server *ts = NULL;
276 afs_int32 code = 0, rcode, okcalls;
277 struct rx_connection *conn;
280 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
283 code = DISK_Truncate(conn, &atrans->tid, file, length);
284 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
286 return ContactQuorum_rcode(okcalls, rcode);
291 ContactQuorum_DISK_WriteV(struct ubik_trans *atrans, int aflags,
292 iovec_wrt * io_vector, iovec_buf *io_buffer)
294 struct ubik_server *ts = NULL;
295 afs_int32 code = 0, rcode, okcalls;
296 struct rx_connection *conn;
299 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
302 code = DISK_WriteV(conn, &atrans->tid, io_vector, io_buffer);
303 if ((code <= -450) && (code > -500)) {
304 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
305 * Un-bulk the entries and do individual DISK_Write calls
306 * instead of DISK_WriteV.
308 struct ubik_iovec *iovec =
309 (struct ubik_iovec *)io_vector->iovec_wrt_val;
310 char *iobuf = (char *)io_buffer->iovec_buf_val;
314 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
315 /* Sanity check for going off end of buffer */
316 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
320 tcbs.bulkdata_len = iovec[i].length;
321 tcbs.bulkdata_val = &iobuf[offset];
322 code = DISK_Write(conn, &atrans->tid, iovec[i].file,
323 iovec[i].position, &tcbs);
326 offset += iovec[i].length;
330 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
332 return ContactQuorum_rcode(okcalls, rcode);
337 ContactQuorum_DISK_SetVersion(struct ubik_trans *atrans, int aflags,
338 ubik_version *OldVersion,
339 ubik_version *NewVersion)
341 struct ubik_server *ts = NULL;
342 afs_int32 code = 0, rcode, okcalls;
343 struct rx_connection *conn;
346 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
349 code = DISK_SetVersion(conn, &atrans->tid, OldVersion, NewVersion);
350 done = ContactQuorum_iterate(atrans, aflags, &ts, &conn, &rcode, &okcalls, code);
352 return ContactQuorum_rcode(okcalls, rcode);
355 #if defined(AFS_PTHREAD_ENV)
357 ubik_thread_create(pthread_attr_t *tattr, pthread_t *thread, void *proc) {
358 osi_Assert(pthread_attr_init(tattr) == 0);
359 osi_Assert(pthread_attr_setdetachstate(tattr, PTHREAD_CREATE_DETACHED) == 0);
360 osi_Assert(pthread_create(thread, tattr, proc, NULL) == 0);
366 * \brief This routine initializes the ubik system for a set of servers.
367 * \return 0 for success, or an error code on failure.
368 * \param serverList set of servers specified; nServers gives the number of entries in this array.
369 * \param pathName provides an initial prefix used for naming storage files used by this system.
370 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
372 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
374 * \warning The host named by myHost should not also be listed in serverList.
376 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
379 ubik_ServerInitCommon(afs_uint32 myHost, short myPort,
380 struct afsconf_cell *info, char clones[],
381 afs_uint32 serverList[], const char *pathName,
382 struct ubik_dbase **dbase)
384 struct ubik_dbase *tdb;
386 #ifdef AFS_PTHREAD_ENV
387 pthread_t rxServerThread; /* pthread variables */
388 pthread_t ubeacon_InteractThread;
389 pthread_t urecovery_InteractThread;
390 pthread_attr_t rxServer_tattr;
391 pthread_attr_t ubeacon_Interact_tattr;
392 pthread_attr_t urecovery_Interact_tattr;
395 extern int rx_stackSize;
399 struct rx_securityClass *secClass;
402 struct rx_service *tservice;
404 initialize_U_error_table();
406 tdb = (struct ubik_dbase *)malloc(sizeof(struct ubik_dbase));
407 tdb->pathName = (char *)malloc(strlen(pathName) + 1);
408 strcpy(tdb->pathName, pathName);
409 tdb->activeTrans = (struct ubik_trans *)0;
410 memset(&tdb->version, 0, sizeof(struct ubik_version));
411 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
412 #ifdef AFS_PTHREAD_ENV
413 MUTEX_INIT(&tdb->versionLock, "version lock", MUTEX_DEFAULT, 0);
414 MUTEX_INIT(&beacon_globals.beacon_lock, "beacon lock", MUTEX_DEFAULT, 0);
415 MUTEX_INIT(&vote_globals.vote_lock, "vote lock", MUTEX_DEFAULT, 0);
416 MUTEX_INIT(&addr_globals.addr_lock, "address lock", MUTEX_DEFAULT, 0);
417 MUTEX_INIT(&version_globals.version_lock, "version lock", MUTEX_DEFAULT, 0);
419 Lock_Init(&tdb->versionLock);
421 Lock_Init(&tdb->cache_lock);
423 tdb->read = uphys_read;
424 tdb->write = uphys_write;
425 tdb->truncate = uphys_truncate;
426 tdb->open = uphys_invalidate; /* this function isn't used any more */
427 tdb->sync = uphys_sync;
428 tdb->stat = uphys_stat;
429 tdb->getlabel = uphys_getlabel;
430 tdb->setlabel = uphys_setlabel;
431 tdb->getnfiles = uphys_getnfiles;
433 tdb->tidCounter = tdb->writeTidCounter = 0;
435 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
437 #ifdef AFS_PTHREAD_ENV
438 CV_INIT(&tdb->version_cond, "version", CV_DEFAULT, 0);
439 CV_INIT(&tdb->flags_cond, "flags", CV_DEFAULT, 0);
440 #endif /* AFS_PTHREAD_ENV */
444 /* the following call is idempotent so when/if it got called earlier,
445 * by whatever called us, it doesn't really matter -- klm */
446 code = rx_Init(myPort);
450 udisk_Init(ubik_nBuffers);
456 code = urecovery_Initialize(tdb);
460 code = ubeacon_InitServerListByInfo(myHost, info, clones);
462 code = ubeacon_InitServerList(myHost, serverList);
466 ubik_callPortal = myPort;
467 /* try to get an additional security object */
468 if (buildSecClassesProc == NULL) {
470 ubik_sc = calloc(numClasses, sizeof(struct rx_securityClass *));
471 ubik_sc[0] = rxnull_NewServerSecurityObject();
472 if (ubik_SRXSecurityProc) {
473 code = (*ubik_SRXSecurityProc) (ubik_SRXSecurityRock,
477 ubik_sc[secIndex] = secClass;
481 (*buildSecClassesProc) (securityRock, &ubik_sc, &numClasses);
483 /* for backwards compat this should keep working as it does now
487 rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, numClasses,
488 VOTE_ExecuteRequest);
489 if (tservice == (struct rx_service *)0) {
490 ubik_dprint("Could not create VOTE rx service!\n");
493 rx_SetMinProcs(tservice, 2);
494 rx_SetMaxProcs(tservice, 3);
497 rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, numClasses,
498 DISK_ExecuteRequest);
499 if (tservice == (struct rx_service *)0) {
500 ubik_dprint("Could not create DISK rx service!\n");
503 rx_SetMinProcs(tservice, 2);
504 rx_SetMaxProcs(tservice, 3);
506 /* start an rx_ServerProc to handle incoming RPC's in particular the
507 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
508 * the "steplock" problem in ubik initialization. Defect 11037.
510 #ifdef AFS_PTHREAD_ENV
511 ubik_thread_create(&rxServer_tattr, &rxServerThread, (void *)rx_ServerProc);
513 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
514 NULL, "rx_ServerProc", &junk);
517 /* send addrs to all other servers */
518 code = ubeacon_updateUbikNetworkAddress(ubik_host);
522 /* now start up async processes */
523 #ifdef AFS_PTHREAD_ENV
524 ubik_thread_create(&ubeacon_Interact_tattr, &ubeacon_InteractThread,
525 (void *)ubeacon_Interact);
527 code = LWP_CreateProcess(ubeacon_Interact, 16384 /*8192 */ ,
528 LWP_MAX_PRIORITY - 1, (void *)0, "beacon",
534 #ifdef AFS_PTHREAD_ENV
535 ubik_thread_create(&urecovery_Interact_tattr, &urecovery_InteractThread,
536 (void *)urecovery_Interact);
537 return 0; /* is this correct? - klm */
539 code = LWP_CreateProcess(urecovery_Interact, 16384 /*8192 */ ,
540 LWP_MAX_PRIORITY - 1, (void *)0, "recovery",
548 * \see ubik_ServerInitCommon()
551 ubik_ServerInitByInfo(afs_uint32 myHost, short myPort,
552 struct afsconf_cell *info, char clones[],
553 const char *pathName, struct ubik_dbase **dbase)
558 ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName,
564 * \see ubik_ServerInitCommon()
567 ubik_ServerInit(afs_uint32 myHost, short myPort, afs_uint32 serverList[],
568 const char *pathName, struct ubik_dbase **dbase)
573 ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
574 serverList, pathName, dbase);
579 * \brief This routine begins a read or write transaction on the transaction
580 * identified by transPtr, in the dbase named by dbase.
582 * An open mode of ubik_READTRANS identifies this as a read transaction,
583 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
584 * transPtr is set to the returned transaction control block.
585 * The readAny flag is set to 0 or 1 or 2 by the wrapper functions
586 * ubik_BeginTrans() or ubik_BeginTransReadAny() or
587 * ubik_BeginTransReadAnyWrite() below.
589 * \note We can only begin transaction when we have an up-to-date database.
592 BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
593 struct ubik_trans **transPtr, int readAny)
595 struct ubik_trans *jt;
596 struct ubik_trans *tt;
599 if (readAny > 1 && ubik_SyncWriterCacheProc == NULL) {
600 /* it's not safe to use ubik_BeginTransReadAnyWrite without a
601 * cache-syncing function; fall back to ubik_BeginTransReadAny,
602 * which is safe but slower */
603 ubik_print("ubik_BeginTransReadAnyWrite called, but "
604 "ubik_SyncWriterCacheProc not set; pretending "
605 "ubik_BeginTransReadAny was called instead\n");
609 if ((transMode != UBIK_READTRANS) && readAny)
612 if (urecovery_AllBetter(dbase, readAny) == 0) {
616 /* otherwise we have a quorum, use it */
618 /* make sure that at most one write transaction occurs at any one time. This
619 * has nothing to do with transaction locking; that's enforced by the lock package. However,
620 * we can't even handle two non-conflicting writes, since our log and recovery modules
621 * don't know how to restore one without possibly picking up some data from the other. */
622 if (transMode == UBIK_WRITETRANS) {
623 /* if we're writing already, wait */
624 while (dbase->flags & DBWRITING) {
625 #ifdef AFS_PTHREAD_ENV
626 CV_WAIT(&dbase->flags_cond, &dbase->versionLock);
629 LWP_WaitProcess(&dbase->flags);
634 if (!ubeacon_AmSyncSite()) {
640 /* create the transaction */
641 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
642 tt = jt; /* move to a register */
643 if (code || tt == (struct ubik_trans *)NULL) {
649 tt->flags |= TRREADANY;
651 tt->flags |= TRREADWRITE;
654 /* label trans and dbase with new tid */
655 tt->tid.epoch = ubik_epochTime;
656 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
657 tt->tid.counter = (dbase->tidCounter += 2);
659 if (transMode == UBIK_WRITETRANS) {
660 /* for a write trans, we have to keep track of the write tid counter too */
661 dbase->writeTidCounter = tt->tid.counter;
663 /* next try to start transaction on appropriate number of machines */
664 code = ContactQuorum_NoArguments(DISK_Begin, tt, 0);
666 /* we must abort the operation */
668 ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
686 ubik_BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
687 struct ubik_trans **transPtr)
689 return BeginTrans(dbase, transMode, transPtr, 0);
696 ubik_BeginTransReadAny(struct ubik_dbase *dbase, afs_int32 transMode,
697 struct ubik_trans **transPtr)
699 return BeginTrans(dbase, transMode, transPtr, 1);
706 ubik_BeginTransReadAnyWrite(struct ubik_dbase *dbase, afs_int32 transMode,
707 struct ubik_trans **transPtr)
709 return BeginTrans(dbase, transMode, transPtr, 2);
713 * \brief This routine ends a read or write transaction by aborting it.
716 ubik_AbortTrans(struct ubik_trans *transPtr)
720 struct ubik_dbase *dbase;
722 dbase = transPtr->dbase;
724 if (transPtr->flags & TRCACHELOCKED) {
725 ReleaseReadLock(&dbase->cache_lock);
726 transPtr->flags &= ~TRCACHELOCKED;
729 ObtainWriteLock(&dbase->cache_lock);
732 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
734 ReleaseWriteLock(&dbase->cache_lock);
736 /* see if we're still up-to-date */
737 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
738 udisk_abort(transPtr);
744 if (transPtr->type == UBIK_READTRANS) {
745 code = udisk_abort(transPtr);
751 /* below here, we know we're doing a write transaction */
752 if (!ubeacon_AmSyncSite()) {
753 udisk_abort(transPtr);
759 /* now it is safe to try remote abort */
760 code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
761 code2 = udisk_abort(transPtr);
764 return (code ? code : code2);
768 WritebackApplicationCache(struct ubik_dbase *dbase)
771 if (ubik_SyncWriterCacheProc) {
772 code = ubik_SyncWriterCacheProc();
775 /* we failed to sync the local cache, so just invalidate the cache;
776 * we'll try to read the cache in again on the next read */
777 memset(&dbase->cachedVersion, 0, sizeof(dbase->cachedVersion));
779 memcpy(&dbase->cachedVersion, &dbase->version,
780 sizeof(dbase->cachedVersion));
785 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
786 * \return an error code.
789 ubik_EndTrans(struct ubik_trans *transPtr)
794 struct ubik_server *ts;
797 struct ubik_dbase *dbase;
799 if (transPtr->type == UBIK_WRITETRANS) {
800 code = ubik_Flush(transPtr);
802 ubik_AbortTrans(transPtr);
807 dbase = transPtr->dbase;
809 if (transPtr->flags & TRCACHELOCKED) {
810 ReleaseReadLock(&dbase->cache_lock);
811 transPtr->flags &= ~TRCACHELOCKED;
814 if (transPtr->type != UBIK_READTRANS) {
815 /* must hold cache_lock before DBHOLD'ing */
816 ObtainWriteLock(&dbase->cache_lock);
822 /* give up if no longer current */
823 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
824 udisk_abort(transPtr);
831 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
832 code = udisk_commit(transPtr);
834 goto success; /* update cachedVersion correctly */
840 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
841 udisk_abort(transPtr);
848 /* now it is safe to do commit */
849 code = udisk_commit(transPtr);
851 /* db data has been committed locally; update the local cache so
852 * readers can get at it */
853 WritebackApplicationCache(dbase);
855 ReleaseWriteLock(&dbase->cache_lock);
857 code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
860 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
861 ReleaseWriteLock(&dbase->cache_lock);
865 /* failed to commit, so must return failure. Try to clear locks first, just for fun
866 * Note that we don't know if this transaction will eventually commit at this point.
867 * If it made it to a site that will be present in the next quorum, we win, otherwise
868 * we lose. If we contact a majority of sites, then we won't be here: contacting
869 * a majority guarantees commit, since it guarantees that one dude will be a
870 * member of the next quorum. */
871 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
876 /* before we can start sending unlock messages, we must wait until all servers
877 * that are possibly still functioning on the other side of a network partition
878 * have timed out. Check the server structures, compute how long to wait, then
879 * start the unlocks */
880 realStart = FT_ApproxTime();
882 /* wait for all servers to time out */
884 now = FT_ApproxTime();
885 /* check if we're still sync site, the guy should either come up
886 * to us, or timeout. Put safety check in anyway */
887 if (now - realStart > 10 * BIGTIME) {
888 ubik_stats.escapes++;
889 ubik_print("ubik escaping from commit wait\n");
892 for (ts = ubik_servers; ts; ts = ts->next) {
894 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
897 /* this guy could have some damaged data, wait for him */
899 tv.tv_sec = 1; /* try again after a while (ha ha) */
902 #ifdef AFS_PTHREAD_ENV
903 /* we could release the dbase outside of the loop, but we do
904 * it here, in the loop, to avoid an unnecessary RELE/HOLD
905 * if all sites are up */
907 select(0, 0, 0, 0, &tv);
910 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
918 break; /* no down ones still pseudo-active */
921 /* finally, unlock all the dudes. We can return success independent of the number of servers
922 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
923 * The transaction is committed anyway, since we succeeded in contacting a quorum
924 * at the start (when invoking the DiskCommit function).
926 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
930 /* don't update cachedVersion here; it should have been updated way back
931 * in ubik_CheckCache, and earlier in this function for writes */
934 ReleaseWriteLock(&dbase->cache_lock);
940 ObtainWriteLock(&dbase->cache_lock);
942 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
943 ReleaseWriteLock(&dbase->cache_lock);
948 * \brief This routine reads length bytes into buffer from the current position in the database.
950 * 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.
952 * \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.
955 ubik_Read(struct ubik_trans *transPtr, void *buffer,
960 /* reads are easy to do: handle locally */
961 DBHOLD(transPtr->dbase);
962 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
963 DBRELE(transPtr->dbase);
968 udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
971 transPtr->seekPos += length;
973 DBRELE(transPtr->dbase);
978 * \brief This routine will flush the io data in the iovec structures.
980 * It first flushes to the local disk and then uses ContactQuorum to write it
981 * to the other servers.
984 ubik_Flush(struct ubik_trans *transPtr)
986 afs_int32 code, error = 0;
988 if (transPtr->type != UBIK_WRITETRANS)
991 DBHOLD(transPtr->dbase);
992 if (!transPtr->iovec_info.iovec_wrt_len
993 || !transPtr->iovec_info.iovec_wrt_val) {
994 DBRELE(transPtr->dbase);
998 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
999 ERROR_EXIT(UNOQUORUM);
1000 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1001 ERROR_EXIT(UNOTSYNC);
1003 /* Update the rest of the servers in the quorum */
1005 ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
1006 &transPtr->iovec_data);
1008 udisk_abort(transPtr);
1009 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1010 transPtr->iovec_info.iovec_wrt_len = 0;
1011 transPtr->iovec_data.iovec_buf_len = 0;
1015 /* Wrote the buffers out, so start at scratch again */
1016 transPtr->iovec_info.iovec_wrt_len = 0;
1017 transPtr->iovec_data.iovec_buf_len = 0;
1020 DBRELE(transPtr->dbase);
1025 ubik_Write(struct ubik_trans *transPtr, void *vbuffer,
1028 struct ubik_iovec *iovec;
1029 afs_int32 code, error = 0;
1030 afs_int32 pos, len, size;
1031 char * buffer = (char *)vbuffer;
1033 if (transPtr->type != UBIK_WRITETRANS)
1038 if (length > IOVEC_MAXBUF) {
1039 for (pos = 0, len = length; len > 0; len -= size, pos += size) {
1040 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
1041 code = ubik_Write(transPtr, buffer+pos, size);
1048 DBHOLD(transPtr->dbase);
1049 if (!transPtr->iovec_info.iovec_wrt_val) {
1050 transPtr->iovec_info.iovec_wrt_len = 0;
1051 transPtr->iovec_info.iovec_wrt_val =
1052 (struct ubik_iovec *)malloc(IOVEC_MAXWRT *
1053 sizeof(struct ubik_iovec));
1054 transPtr->iovec_data.iovec_buf_len = 0;
1055 transPtr->iovec_data.iovec_buf_val = (char *)malloc(IOVEC_MAXBUF);
1056 if (!transPtr->iovec_info.iovec_wrt_val
1057 || !transPtr->iovec_data.iovec_buf_val) {
1058 if (transPtr->iovec_info.iovec_wrt_val)
1059 free(transPtr->iovec_info.iovec_wrt_val);
1060 transPtr->iovec_info.iovec_wrt_val = 0;
1061 if (transPtr->iovec_data.iovec_buf_val)
1062 free(transPtr->iovec_data.iovec_buf_val);
1063 transPtr->iovec_data.iovec_buf_val = 0;
1064 DBRELE(transPtr->dbase);
1069 /* If this write won't fit in the structure, then flush it out and start anew */
1070 if ((transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT)
1071 || ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF)) {
1072 /* Can't hold the DB lock over ubik_Flush */
1073 DBRELE(transPtr->dbase);
1074 code = ubik_Flush(transPtr);
1077 DBHOLD(transPtr->dbase);
1080 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1081 ERROR_EXIT(UNOQUORUM);
1082 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1083 ERROR_EXIT(UNOTSYNC);
1085 /* Write to the local disk */
1087 udisk_write(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
1090 udisk_abort(transPtr);
1091 transPtr->iovec_info.iovec_wrt_len = 0;
1092 transPtr->iovec_data.iovec_buf_len = 0;
1093 DBRELE(transPtr->dbase);
1097 /* Collect writes for the other ubik servers (to be done in bulk) */
1098 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
1099 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
1100 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
1101 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
1103 memcpy(&transPtr->iovec_data.
1104 iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
1106 transPtr->iovec_info.iovec_wrt_len++;
1107 transPtr->iovec_data.iovec_buf_len += length;
1108 transPtr->seekPos += length;
1111 DBRELE(transPtr->dbase);
1116 * \brief This sets the file pointer associated with the current transaction
1117 * to the appropriate file and byte position.
1119 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1120 * and a byte position relative to the specified file \p position.
1123 ubik_Seek(struct ubik_trans *transPtr, afs_int32 fileid,
1128 DBHOLD(transPtr->dbase);
1129 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
1132 transPtr->seekFile = fileid;
1133 transPtr->seekPos = position;
1136 DBRELE(transPtr->dbase);
1141 * \brief This call returns the file pointer associated with the specified
1142 * transaction in \p fileid and \p position.
1145 ubik_Tell(struct ubik_trans *transPtr, afs_int32 * fileid,
1146 afs_int32 * position)
1148 DBHOLD(transPtr->dbase);
1149 *fileid = transPtr->seekFile;
1150 *position = transPtr->seekPos;
1151 DBRELE(transPtr->dbase);
1156 * \brief This sets the file size for the currently-selected file to \p length
1157 * bytes, if length is less than the file's current size.
1160 ubik_Truncate(struct ubik_trans *transPtr, afs_int32 length)
1162 afs_int32 code, error = 0;
1164 /* Will also catch if not UBIK_WRITETRANS */
1165 code = ubik_Flush(transPtr);
1169 DBHOLD(transPtr->dbase);
1170 /* first, check that quorum is still good, and that dbase is up-to-date */
1171 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1172 ERROR_EXIT(UNOQUORUM);
1173 if (!ubeacon_AmSyncSite())
1174 ERROR_EXIT(UNOTSYNC);
1176 /* now do the operation locally, and propagate it out */
1177 code = udisk_truncate(transPtr, transPtr->seekFile, length);
1180 ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
1184 /* we must abort the operation */
1185 udisk_abort(transPtr);
1186 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1191 DBRELE(transPtr->dbase);
1196 * \brief set a lock; all locks are released on transaction end (commit/abort)
1199 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
1202 afs_int32 code = 0, error = 0;
1204 if (atype == LOCKWRITE) {
1205 if (atrans->type == UBIK_READTRANS)
1207 code = ubik_Flush(atrans);
1212 DBHOLD(atrans->dbase);
1213 if (atype == LOCKREAD) {
1214 code = ulock_getLock(atrans, atype, 1);
1218 /* first, check that quorum is still good, and that dbase is up-to-date */
1219 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
1220 ERROR_EXIT(UNOQUORUM);
1221 if (!ubeacon_AmSyncSite())
1222 ERROR_EXIT(UNOTSYNC);
1224 /* now do the operation locally, and propagate it out */
1225 code = ulock_getLock(atrans, atype, 1);
1227 code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
1228 1 /*unused */ , LOCKWRITE);
1231 /* we must abort the operation */
1232 udisk_abort(atrans);
1233 ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
1239 DBRELE(atrans->dbase);
1244 * \brief utility to wait for a version # to change
1247 ubik_WaitVersion(struct ubik_dbase *adatabase,
1248 struct ubik_version *aversion)
1252 /* wait until version # changes, and then return */
1253 if (vcmp(*aversion, adatabase->version) != 0) {
1257 #ifdef AFS_PTHREAD_ENV
1258 CV_WAIT(&adatabase->version_cond, &adatabase->versionLock);
1261 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
1268 * \brief utility to get the version of the dbase a transaction is dealing with
1271 ubik_GetVersion(struct ubik_trans *atrans,
1272 struct ubik_version *avers)
1274 DBHOLD(atrans->dbase);
1275 *avers = atrans->dbase->version;
1276 DBRELE(atrans->dbase);
1281 * \brief Facility to simplify database caching.
1282 * \return zero if last trans was done on the local server and was successful.
1283 * \return -1 means bad (NULL) argument.
1285 * If return value is non-zero and the caller is a server caching part of the
1286 * Ubik database, it should invalidate that cache.
1289 ubik_CacheUpdate(struct ubik_trans *atrans)
1291 if (!(atrans && atrans->dbase))
1293 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
1297 * check and possibly update cache of ubik db.
1299 * If the version of the cached db data is out of date, this calls (*check) to
1300 * update the cache. If (*check) returns success, we update the version of the
1303 * Checking the version of the cached db data is done under a read lock;
1304 * updating the cache (and thus calling (*check)) is done under a write lock
1305 * so is guaranteed not to interfere with another thread's (*check). On
1306 * successful return, a read lock on the cached db data is obtained, which
1307 * will be released by ubik_EndTrans or ubik_AbortTrans.
1309 * @param[in] atrans ubik transaction
1310 * @param[in] check function to call to check/update cache
1311 * @param[in] rock rock to pass to *check
1313 * @return operation status
1315 * @retval nonzero error; cachedVersion not updated
1317 * @post On success, application cache is read-locked, and cache data is
1321 ubik_CheckCache(struct ubik_trans *atrans, ubik_updatecache_func cbf, void *rock)
1325 if (!(atrans && atrans->dbase))
1328 ObtainReadLock(&atrans->dbase->cache_lock);
1330 while (ubik_CacheUpdate(atrans) != 0) {
1332 ReleaseReadLock(&atrans->dbase->cache_lock);
1333 ObtainSharedLock(&atrans->dbase->cache_lock);
1335 if (ubik_CacheUpdate(atrans) != 0) {
1337 BoostSharedLock(&atrans->dbase->cache_lock);
1339 ret = (*cbf) (atrans, rock);
1341 memcpy(&atrans->dbase->cachedVersion, &atrans->dbase->version,
1342 sizeof(atrans->dbase->cachedVersion));
1346 /* It would be nice if we could convert from a shared lock to a read
1347 * lock... instead, just release the shared and acquire the read */
1348 ReleaseSharedLock(&atrans->dbase->cache_lock);
1351 /* if we have an error, don't retry, and don't hold any locks */
1355 ObtainReadLock(&atrans->dbase->cache_lock);
1358 atrans->flags |= TRCACHELOCKED;
1364 * "Who said anything about panicking?" snapped Arthur.
1365 * "This is still just the culture shock. You wait till I've settled down
1366 * into the situation and found my bearings. \em Then I'll start panicking!"
1369 * \returns There is no return from panic.
1372 panic(char *format, ...)
1376 va_start(ap, format);
1377 ubik_print("Ubik PANIC: ");
1378 ubik_vprint(format, ap);
1382 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
1383 exit(1); /* never know, though */
1387 * This function takes an IP addresses as its parameter. It returns the
1388 * the primary IP address that is on the host passed in, or 0 if not found.
1391 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
1393 struct ubik_server *ts;
1397 for (ts = ubik_servers; ts; ts = ts->next)
1398 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
1399 if (ts->addr[j] == addr) {
1401 return ts->addr[0]; /* net byte order */
1404 return 0; /* if not in server database, return error */
1408 ubik_CheckAuth(struct rx_call *acall)
1410 if (checkSecurityProc)
1411 return (*checkSecurityProc) (securityRock, acall);
1412 else if (ubik_CheckRXSecurityProc) {
1413 return (*ubik_CheckRXSecurityProc) (ubik_CheckRXSecurityRock, acall);
1419 ubik_SetServerSecurityProcs(void (*buildproc) (void *,
1420 struct rx_securityClass ***,
1422 int (*checkproc) (void *, struct rx_call *),
1425 buildSecClassesProc = buildproc;
1426 checkSecurityProc = checkproc;
1427 securityRock = rock;