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);
344 * \brief This routine initializes the ubik system for a set of servers.
345 * \return 0 for success, or an error code on failure.
346 * \param serverList set of servers specified; nServers gives the number of entries in this array.
347 * \param pathName provides an initial prefix used for naming storage files used by this system.
348 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
350 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
352 * \warning The host named by myHost should not also be listed in serverList.
354 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
357 ubik_ServerInitCommon(afs_uint32 myHost, short myPort,
358 struct afsconf_cell *info, char clones[],
359 afs_uint32 serverList[], const char *pathName,
360 struct ubik_dbase **dbase)
362 struct ubik_dbase *tdb;
364 #ifdef AFS_PTHREAD_ENV
365 pthread_t rxServerThread; /* pthread variables */
366 pthread_t ubeacon_InteractThread;
367 pthread_t urecovery_InteractThread;
368 pthread_attr_t rxServer_tattr;
369 pthread_attr_t ubeacon_Interact_tattr;
370 pthread_attr_t urecovery_Interact_tattr;
373 extern int rx_stackSize;
377 struct rx_securityClass *secClass;
379 struct rx_service *tservice;
381 initialize_U_error_table();
383 tdb = (struct ubik_dbase *)malloc(sizeof(struct ubik_dbase));
384 tdb->pathName = (char *)malloc(strlen(pathName) + 1);
385 strcpy(tdb->pathName, pathName);
386 tdb->activeTrans = (struct ubik_trans *)0;
387 memset(&tdb->version, 0, sizeof(struct ubik_version));
388 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
389 #ifdef AFS_PTHREAD_ENV
390 MUTEX_INIT(&tdb->versionLock, "version lock", MUTEX_DEFAULT, 0);
392 Lock_Init(&tdb->versionLock);
394 Lock_Init(&tdb->cache_lock);
396 tdb->read = uphys_read;
397 tdb->write = uphys_write;
398 tdb->truncate = uphys_truncate;
399 tdb->open = uphys_invalidate; /* this function isn't used any more */
400 tdb->sync = uphys_sync;
401 tdb->stat = uphys_stat;
402 tdb->getlabel = uphys_getlabel;
403 tdb->setlabel = uphys_setlabel;
404 tdb->getnfiles = uphys_getnfiles;
406 tdb->tidCounter = tdb->writeTidCounter = 0;
408 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
410 #ifdef AFS_PTHREAD_ENV
411 CV_INIT(&tdb->version_cond, "version", CV_DEFAULT, 0);
412 CV_INIT(&tdb->flags_cond, "flags", CV_DEFAULT, 0);
413 #endif /* AFS_PTHREAD_ENV */
417 /* the following call is idempotent so when/if it got called earlier,
418 * by whatever called us, it doesn't really matter -- klm */
419 code = rx_Init(myPort);
423 udisk_Init(ubik_nBuffers);
426 ubik_callPortal = myPort;
427 /* try to get an additional security object */
428 ubik_sc[0] = rxnull_NewServerSecurityObject();
431 if (ubik_SRXSecurityProc) {
433 (*ubik_SRXSecurityProc) (ubik_SRXSecurityRock, &secClass,
436 ubik_sc[secIndex] = secClass;
439 /* for backwards compat this should keep working as it does now
442 /* This really needs to be up above, where I have put it. It works
443 * here when we're non-pthreaded, but the code above, when using
444 * pthreads may (and almost certainly does) end up calling on a
445 * pthread resource which gets initialized by rx_Init. The end
446 * result is that an assert fails and the program dies. -- klm
448 code = rx_Init(myPort);
454 rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, 3,
455 VOTE_ExecuteRequest);
456 if (tservice == (struct rx_service *)0) {
457 ubik_dprint("Could not create VOTE rx service!\n");
460 rx_SetMinProcs(tservice, 2);
461 rx_SetMaxProcs(tservice, 3);
464 rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, 3,
465 DISK_ExecuteRequest);
466 if (tservice == (struct rx_service *)0) {
467 ubik_dprint("Could not create DISK rx service!\n");
470 rx_SetMinProcs(tservice, 2);
471 rx_SetMaxProcs(tservice, 3);
473 /* start an rx_ServerProc to handle incoming RPC's in particular the
474 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
475 * the "steplock" problem in ubik initialization. Defect 11037.
477 #ifdef AFS_PTHREAD_ENV
478 /* do assert stuff */
479 osi_Assert(pthread_attr_init(&rxServer_tattr) == 0);
480 osi_Assert(pthread_attr_setdetachstate(&rxServer_tattr, PTHREAD_CREATE_DETACHED) == 0);
481 /* osi_Assert(pthread_attr_setstacksize(&rxServer_tattr, rx_stackSize) == 0); */
483 osi_Assert(pthread_create(&rxServerThread, &rxServer_tattr, (void *)rx_ServerProc, NULL) == 0);
485 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
486 NULL, "rx_ServerProc", &junk);
489 /* do basic initialization */
493 code = urecovery_Initialize(tdb);
497 code = ubeacon_InitServerListByInfo(myHost, info, clones);
499 code = ubeacon_InitServerList(myHost, serverList);
503 /* now start up async processes */
504 #ifdef AFS_PTHREAD_ENV
505 /* do assert stuff */
506 osi_Assert(pthread_attr_init(&ubeacon_Interact_tattr) == 0);
507 osi_Assert(pthread_attr_setdetachstate(&ubeacon_Interact_tattr, PTHREAD_CREATE_DETACHED) == 0);
508 /* osi_Assert(pthread_attr_setstacksize(&ubeacon_Interact_tattr, 16384) == 0); */
509 /* need another attr set here for priority??? - klm */
511 osi_Assert(pthread_create(&ubeacon_InteractThread, &ubeacon_Interact_tattr,
512 (void *)ubeacon_Interact, NULL) == 0);
514 code = LWP_CreateProcess(ubeacon_Interact, 16384 /*8192 */ ,
515 LWP_MAX_PRIORITY - 1, (void *)0, "beacon",
521 #ifdef AFS_PTHREAD_ENV
522 /* do assert stuff */
523 osi_Assert(pthread_attr_init(&urecovery_Interact_tattr) == 0);
524 osi_Assert(pthread_attr_setdetachstate(&urecovery_Interact_tattr, PTHREAD_CREATE_DETACHED) == 0);
525 /* osi_Assert(pthread_attr_setstacksize(&urecovery_Interact_tattr, 16384) == 0); */
526 /* need another attr set here for priority??? - klm */
528 osi_Assert(pthread_create(&urecovery_InteractThread, &urecovery_Interact_tattr,
529 (void *)urecovery_Interact, NULL) == 0);
531 return 0; /* is this correct? - klm */
533 code = LWP_CreateProcess(urecovery_Interact, 16384 /*8192 */ ,
534 LWP_MAX_PRIORITY - 1, (void *)0, "recovery",
542 * \see ubik_ServerInitCommon()
545 ubik_ServerInitByInfo(afs_uint32 myHost, short myPort,
546 struct afsconf_cell *info, char clones[],
547 const char *pathName, struct ubik_dbase **dbase)
552 ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName,
558 * \see ubik_ServerInitCommon()
561 ubik_ServerInit(afs_uint32 myHost, short myPort, afs_uint32 serverList[],
562 const char *pathName, struct ubik_dbase **dbase)
567 ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
568 serverList, pathName, dbase);
573 * \brief This routine begins a read or write transaction on the transaction
574 * identified by transPtr, in the dbase named by dbase.
576 * An open mode of ubik_READTRANS identifies this as a read transaction,
577 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
578 * transPtr is set to the returned transaction control block.
579 * The readAny flag is set to 0 or 1 or 2 by the wrapper functions
580 * ubik_BeginTrans() or ubik_BeginTransReadAny() or
581 * ubik_BeginTransReadAnyWrite() below.
583 * \note We can only begin transaction when we have an up-to-date database.
586 BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
587 struct ubik_trans **transPtr, int readAny)
589 struct ubik_trans *jt;
590 struct ubik_trans *tt;
593 if (readAny > 1 && ubik_SyncWriterCacheProc == NULL) {
594 /* it's not safe to use ubik_BeginTransReadAnyWrite without a
595 * cache-syncing function; fall back to ubik_BeginTransReadAny,
596 * which is safe but slower */
597 ubik_print("ubik_BeginTransReadAnyWrite called, but "
598 "ubik_SyncWriterCacheProc not set; pretending "
599 "ubik_BeginTransReadAny was called instead\n");
603 if ((transMode != UBIK_READTRANS) && readAny)
606 if (urecovery_AllBetter(dbase, readAny) == 0) {
610 /* otherwise we have a quorum, use it */
612 /* make sure that at most one write transaction occurs at any one time. This
613 * has nothing to do with transaction locking; that's enforced by the lock package. However,
614 * we can't even handle two non-conflicting writes, since our log and recovery modules
615 * don't know how to restore one without possibly picking up some data from the other. */
616 if (transMode == UBIK_WRITETRANS) {
617 /* if we're writing already, wait */
618 while (dbase->flags & DBWRITING) {
619 #ifdef AFS_PTHREAD_ENV
620 CV_WAIT(&dbase->flags_cond, &dbase->versionLock);
623 LWP_WaitProcess(&dbase->flags);
628 if (!ubeacon_AmSyncSite()) {
634 /* create the transaction */
635 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
636 tt = jt; /* move to a register */
637 if (code || tt == (struct ubik_trans *)NULL) {
642 tt->flags |= TRREADANY;
644 tt->flags |= TRREADWRITE;
647 /* label trans and dbase with new tid */
648 tt->tid.epoch = ubik_epochTime;
649 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
650 tt->tid.counter = (dbase->tidCounter += 2);
652 if (transMode == UBIK_WRITETRANS) {
653 /* for a write trans, we have to keep track of the write tid counter too */
654 dbase->writeTidCounter = tt->tid.counter;
656 /* next try to start transaction on appropriate number of machines */
657 code = ContactQuorum_NoArguments(DISK_Begin, tt, 0);
659 /* we must abort the operation */
661 ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
677 ubik_BeginTrans(struct ubik_dbase *dbase, afs_int32 transMode,
678 struct ubik_trans **transPtr)
680 return BeginTrans(dbase, transMode, transPtr, 0);
687 ubik_BeginTransReadAny(struct ubik_dbase *dbase, afs_int32 transMode,
688 struct ubik_trans **transPtr)
690 return BeginTrans(dbase, transMode, transPtr, 1);
697 ubik_BeginTransReadAnyWrite(struct ubik_dbase *dbase, afs_int32 transMode,
698 struct ubik_trans **transPtr)
700 return BeginTrans(dbase, transMode, transPtr, 2);
704 * \brief This routine ends a read or write transaction by aborting it.
707 ubik_AbortTrans(struct ubik_trans *transPtr)
711 struct ubik_dbase *dbase;
713 dbase = transPtr->dbase;
715 if (transPtr->flags & TRCACHELOCKED) {
716 ReleaseReadLock(&dbase->cache_lock);
717 transPtr->flags &= ~TRCACHELOCKED;
720 ObtainWriteLock(&dbase->cache_lock);
723 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
725 ReleaseWriteLock(&dbase->cache_lock);
727 /* see if we're still up-to-date */
728 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
729 udisk_abort(transPtr);
735 if (transPtr->type == UBIK_READTRANS) {
736 code = udisk_abort(transPtr);
742 /* below here, we know we're doing a write transaction */
743 if (!ubeacon_AmSyncSite()) {
744 udisk_abort(transPtr);
750 /* now it is safe to try remote abort */
751 code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
752 code2 = udisk_abort(transPtr);
755 return (code ? code : code2);
759 WritebackApplicationCache(struct ubik_dbase *dbase)
762 if (ubik_SyncWriterCacheProc) {
763 code = ubik_SyncWriterCacheProc();
766 /* we failed to sync the local cache, so just invalidate the cache;
767 * we'll try to read the cache in again on the next read */
768 memset(&dbase->cachedVersion, 0, sizeof(dbase->cachedVersion));
770 memcpy(&dbase->cachedVersion, &dbase->version,
771 sizeof(dbase->cachedVersion));
776 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
777 * \return an error code.
780 ubik_EndTrans(struct ubik_trans *transPtr)
785 struct ubik_server *ts;
788 struct ubik_dbase *dbase;
790 if (transPtr->type == UBIK_WRITETRANS) {
791 code = ubik_Flush(transPtr);
793 ubik_AbortTrans(transPtr);
798 dbase = transPtr->dbase;
800 if (transPtr->flags & TRCACHELOCKED) {
801 ReleaseReadLock(&dbase->cache_lock);
802 transPtr->flags &= ~TRCACHELOCKED;
805 if (transPtr->type != UBIK_READTRANS) {
806 /* must hold cache_lock before DBHOLD'ing */
807 ObtainWriteLock(&dbase->cache_lock);
813 /* give up if no longer current */
814 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
815 udisk_abort(transPtr);
822 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
823 code = udisk_commit(transPtr);
825 goto success; /* update cachedVersion correctly */
831 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
832 udisk_abort(transPtr);
839 /* now it is safe to do commit */
840 code = udisk_commit(transPtr);
842 /* db data has been committed locally; update the local cache so
843 * readers can get at it */
844 WritebackApplicationCache(dbase);
846 ReleaseWriteLock(&dbase->cache_lock);
848 code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
851 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
852 ReleaseWriteLock(&dbase->cache_lock);
856 /* failed to commit, so must return failure. Try to clear locks first, just for fun
857 * Note that we don't know if this transaction will eventually commit at this point.
858 * If it made it to a site that will be present in the next quorum, we win, otherwise
859 * we lose. If we contact a majority of sites, then we won't be here: contacting
860 * a majority guarantees commit, since it guarantees that one dude will be a
861 * member of the next quorum. */
862 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
867 /* before we can start sending unlock messages, we must wait until all servers
868 * that are possibly still functioning on the other side of a network partition
869 * have timed out. Check the server structures, compute how long to wait, then
870 * start the unlocks */
871 realStart = FT_ApproxTime();
873 /* wait for all servers to time out */
875 now = FT_ApproxTime();
876 /* check if we're still sync site, the guy should either come up
877 * to us, or timeout. Put safety check in anyway */
878 if (now - realStart > 10 * BIGTIME) {
879 ubik_stats.escapes++;
880 ubik_print("ubik escaping from commit wait\n");
883 for (ts = ubik_servers; ts; ts = ts->next) {
884 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
886 /* this guy could have some damaged data, wait for him */
888 tv.tv_sec = 1; /* try again after a while (ha ha) */
891 #ifdef AFS_PTHREAD_ENV
892 /* we could release the dbase outside of the loop, but we do
893 * it here, in the loop, to avoid an unnecessary RELE/HOLD
894 * if all sites are up */
896 select(0, 0, 0, 0, &tv);
899 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
906 break; /* no down ones still pseudo-active */
909 /* finally, unlock all the dudes. We can return success independent of the number of servers
910 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
911 * The transaction is committed anyway, since we succeeded in contacting a quorum
912 * at the start (when invoking the DiskCommit function).
914 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
918 /* don't update cachedVersion here; it should have been updated way back
919 * in ubik_CheckCache, and earlier in this function for writes */
922 ReleaseWriteLock(&dbase->cache_lock);
928 ObtainWriteLock(&dbase->cache_lock);
930 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
931 ReleaseWriteLock(&dbase->cache_lock);
936 * \brief This routine reads length bytes into buffer from the current position in the database.
938 * 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.
940 * \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.
943 ubik_Read(struct ubik_trans *transPtr, void *buffer,
948 /* reads are easy to do: handle locally */
949 DBHOLD(transPtr->dbase);
950 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
951 DBRELE(transPtr->dbase);
956 udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
959 transPtr->seekPos += length;
961 DBRELE(transPtr->dbase);
966 * \brief This routine will flush the io data in the iovec structures.
968 * It first flushes to the local disk and then uses ContactQuorum to write it
969 * to the other servers.
972 ubik_Flush(struct ubik_trans *transPtr)
974 afs_int32 code, error = 0;
976 if (transPtr->type != UBIK_WRITETRANS)
978 if (!transPtr->iovec_info.iovec_wrt_len
979 || !transPtr->iovec_info.iovec_wrt_val)
982 DBHOLD(transPtr->dbase);
983 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
984 ERROR_EXIT(UNOQUORUM);
985 if (!ubeacon_AmSyncSite()) /* only sync site can write */
986 ERROR_EXIT(UNOTSYNC);
988 /* Update the rest of the servers in the quorum */
990 ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
991 &transPtr->iovec_data);
993 udisk_abort(transPtr);
994 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
995 transPtr->iovec_info.iovec_wrt_len = 0;
996 transPtr->iovec_data.iovec_buf_len = 0;
1000 /* Wrote the buffers out, so start at scratch again */
1001 transPtr->iovec_info.iovec_wrt_len = 0;
1002 transPtr->iovec_data.iovec_buf_len = 0;
1005 DBRELE(transPtr->dbase);
1010 ubik_Write(struct ubik_trans *transPtr, void *vbuffer,
1013 struct ubik_iovec *iovec;
1014 afs_int32 code, error = 0;
1015 afs_int32 pos, len, size;
1016 char * buffer = (char *)vbuffer;
1018 if (transPtr->type != UBIK_WRITETRANS)
1023 if (length > IOVEC_MAXBUF) {
1024 for (pos = 0, len = length; len > 0; len -= size, pos += size) {
1025 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
1026 code = ubik_Write(transPtr, buffer+pos, size);
1033 if (!transPtr->iovec_info.iovec_wrt_val) {
1034 transPtr->iovec_info.iovec_wrt_len = 0;
1035 transPtr->iovec_info.iovec_wrt_val =
1036 (struct ubik_iovec *)malloc(IOVEC_MAXWRT *
1037 sizeof(struct ubik_iovec));
1038 transPtr->iovec_data.iovec_buf_len = 0;
1039 transPtr->iovec_data.iovec_buf_val = (char *)malloc(IOVEC_MAXBUF);
1040 if (!transPtr->iovec_info.iovec_wrt_val
1041 || !transPtr->iovec_data.iovec_buf_val) {
1042 if (transPtr->iovec_info.iovec_wrt_val)
1043 free(transPtr->iovec_info.iovec_wrt_val);
1044 transPtr->iovec_info.iovec_wrt_val = 0;
1045 if (transPtr->iovec_data.iovec_buf_val)
1046 free(transPtr->iovec_data.iovec_buf_val);
1047 transPtr->iovec_data.iovec_buf_val = 0;
1052 /* If this write won't fit in the structure, then flush it out and start anew */
1053 if ((transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT)
1054 || ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF)) {
1055 code = ubik_Flush(transPtr);
1060 DBHOLD(transPtr->dbase);
1061 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1062 ERROR_EXIT(UNOQUORUM);
1063 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1064 ERROR_EXIT(UNOTSYNC);
1066 /* Write to the local disk */
1068 udisk_write(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
1071 udisk_abort(transPtr);
1072 transPtr->iovec_info.iovec_wrt_len = 0;
1073 transPtr->iovec_data.iovec_buf_len = 0;
1074 DBRELE(transPtr->dbase);
1078 /* Collect writes for the other ubik servers (to be done in bulk) */
1079 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
1080 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
1081 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
1082 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
1084 memcpy(&transPtr->iovec_data.
1085 iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
1087 transPtr->iovec_info.iovec_wrt_len++;
1088 transPtr->iovec_data.iovec_buf_len += length;
1089 transPtr->seekPos += length;
1092 DBRELE(transPtr->dbase);
1097 * \brief This sets the file pointer associated with the current transaction
1098 * to the appropriate file and byte position.
1100 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1101 * and a byte position relative to the specified file \p position.
1104 ubik_Seek(struct ubik_trans *transPtr, afs_int32 fileid,
1109 DBHOLD(transPtr->dbase);
1110 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
1113 transPtr->seekFile = fileid;
1114 transPtr->seekPos = position;
1117 DBRELE(transPtr->dbase);
1122 * \brief This call returns the file pointer associated with the specified
1123 * transaction in \p fileid and \p position.
1126 ubik_Tell(struct ubik_trans *transPtr, afs_int32 * fileid,
1127 afs_int32 * position)
1129 DBHOLD(transPtr->dbase);
1130 *fileid = transPtr->seekFile;
1131 *position = transPtr->seekPos;
1132 DBRELE(transPtr->dbase);
1137 * \brief This sets the file size for the currently-selected file to \p length
1138 * bytes, if length is less than the file's current size.
1141 ubik_Truncate(struct ubik_trans *transPtr, afs_int32 length)
1143 afs_int32 code, error = 0;
1145 /* Will also catch if not UBIK_WRITETRANS */
1146 code = ubik_Flush(transPtr);
1150 DBHOLD(transPtr->dbase);
1151 /* first, check that quorum is still good, and that dbase is up-to-date */
1152 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1153 ERROR_EXIT(UNOQUORUM);
1154 if (!ubeacon_AmSyncSite())
1155 ERROR_EXIT(UNOTSYNC);
1157 /* now do the operation locally, and propagate it out */
1158 code = udisk_truncate(transPtr, transPtr->seekFile, length);
1161 ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
1165 /* we must abort the operation */
1166 udisk_abort(transPtr);
1167 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1172 DBRELE(transPtr->dbase);
1177 * \brief set a lock; all locks are released on transaction end (commit/abort)
1180 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
1183 afs_int32 code = 0, error = 0;
1185 if (atype == LOCKWRITE) {
1186 if (atrans->type == UBIK_READTRANS)
1188 code = ubik_Flush(atrans);
1193 DBHOLD(atrans->dbase);
1194 if (atype == LOCKREAD) {
1195 code = ulock_getLock(atrans, atype, 1);
1199 /* first, check that quorum is still good, and that dbase is up-to-date */
1200 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
1201 ERROR_EXIT(UNOQUORUM);
1202 if (!ubeacon_AmSyncSite())
1203 ERROR_EXIT(UNOTSYNC);
1205 /* now do the operation locally, and propagate it out */
1206 code = ulock_getLock(atrans, atype, 1);
1208 code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
1209 1 /*unused */ , LOCKWRITE);
1212 /* we must abort the operation */
1213 udisk_abort(atrans);
1214 ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
1220 DBRELE(atrans->dbase);
1225 * \brief utility to wait for a version # to change
1228 ubik_WaitVersion(struct ubik_dbase *adatabase,
1229 struct ubik_version *aversion)
1233 /* wait until version # changes, and then return */
1234 if (vcmp(*aversion, adatabase->version) != 0) {
1238 #ifdef AFS_PTHREAD_ENV
1239 CV_WAIT(&adatabase->version_cond, &adatabase->versionLock);
1242 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
1249 * \brief utility to get the version of the dbase a transaction is dealing with
1252 ubik_GetVersion(struct ubik_trans *atrans,
1253 struct ubik_version *avers)
1255 *avers = atrans->dbase->version;
1260 * \brief Facility to simplify database caching.
1261 * \return zero if last trans was done on the local server and was successful.
1262 * \return -1 means bad (NULL) argument.
1264 * If return value is non-zero and the caller is a server caching part of the
1265 * Ubik database, it should invalidate that cache.
1268 ubik_CacheUpdate(struct ubik_trans *atrans)
1270 if (!(atrans && atrans->dbase))
1272 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
1276 * check and possibly update cache of ubik db.
1278 * If the version of the cached db data is out of date, this calls (*check) to
1279 * update the cache. If (*check) returns success, we update the version of the
1282 * Checking the version of the cached db data is done under a read lock;
1283 * updating the cache (and thus calling (*check)) is done under a write lock
1284 * so is guaranteed not to interfere with another thread's (*check). On
1285 * successful return, a read lock on the cached db data is obtained, which
1286 * will be released by ubik_EndTrans or ubik_AbortTrans.
1288 * @param[in] atrans ubik transaction
1289 * @param[in] check function to call to check/update cache
1290 * @param[in] rock rock to pass to *check
1292 * @return operation status
1294 * @retval nonzero error; cachedVersion not updated
1296 * @post On success, application cache is read-locked, and cache data is
1300 ubik_CheckCache(struct ubik_trans *atrans, ubik_updatecache_func cbf, void *rock)
1304 if (!(atrans && atrans->dbase))
1307 ObtainReadLock(&atrans->dbase->cache_lock);
1309 while (ubik_CacheUpdate(atrans) != 0) {
1311 ReleaseReadLock(&atrans->dbase->cache_lock);
1312 ObtainSharedLock(&atrans->dbase->cache_lock);
1314 if (ubik_CacheUpdate(atrans) != 0) {
1316 BoostSharedLock(&atrans->dbase->cache_lock);
1318 ret = (*cbf) (atrans, rock);
1320 memcpy(&atrans->dbase->cachedVersion, &atrans->dbase->version,
1321 sizeof(atrans->dbase->cachedVersion));
1325 /* It would be nice if we could convert from a shared lock to a read
1326 * lock... instead, just release the shared and acquire the read */
1327 ReleaseSharedLock(&atrans->dbase->cache_lock);
1330 /* if we have an error, don't retry, and don't hold any locks */
1334 ObtainReadLock(&atrans->dbase->cache_lock);
1337 atrans->flags |= TRCACHELOCKED;
1343 * "Who said anything about panicking?" snapped Arthur.
1344 * "This is still just the culture shock. You wait till I've settled down
1345 * into the situation and found my bearings. \em Then I'll start panicking!"
1348 * \returns There is no return from panic.
1351 panic(char *format, ...)
1355 va_start(ap, format);
1356 ubik_print("Ubik PANIC: ");
1357 ubik_vprint(format, ap);
1361 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
1362 exit(1); /* never know, though */
1366 * This function takes an IP addresses as its parameter. It returns the
1367 * the primary IP address that is on the host passed in, or 0 if not found.
1370 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
1372 struct ubik_server *ts;
1375 for (ts = ubik_servers; ts; ts = ts->next)
1376 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
1377 if (ts->addr[j] == addr)
1378 return ts->addr[0]; /* net byte order */
1379 return 0; /* if not in server database, return error */