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>
14 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <sys/param.h>
30 #include <afs/cellconfig.h>
32 #define UBIK_INTERNALS
36 #include <lwp.h> /* temporary hack by klm */
38 #define ERROR_EXIT(code) {error=(code); goto error_exit;}
42 * This system is organized in a hierarchical set of related modules. Modules
43 * at one level can only call modules at the same level or below.
45 * At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
46 * operating system primitives.
48 * At the next level (1) we have
50 * \li VOTER--The module responsible for casting votes when asked. It is also
51 * responsible for determining whether this server should try to become
52 * a synchronization site.
53 * \li BEACONER--The module responsible for sending keep-alives out when a
54 * server is actually the sync site, or trying to become a sync site.
55 * \li DISK--The module responsible for representing atomic transactions
56 * on the local disk. It maintains a new-value only log.
57 * \li LOCK--The module responsible for locking byte ranges in the database file.
59 * At the next level (2) we have
61 * \li RECOVERY--The module responsible for ensuring that all members of a quorum
62 * have the same up-to-date database after a new synchronization site is
63 * elected. This module runs only on the synchronization site.
65 * At the next level (3) we have
67 * \li REMOTE--The module responsible for interpreting requests from the sync
68 * site and applying them to the database, after obtaining the appropriate
71 * At the next level (4) we have
73 * \li UBIK--The module users call to perform operations on the database.
78 afs_int32 ubik_quorum = 0;
79 struct ubik_dbase *ubik_dbase = 0;
80 struct ubik_stats ubik_stats;
81 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
82 afs_int32 ubik_epochTime = 0;
83 afs_int32 urecovery_state = 0;
84 int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **, afs_int32 *);
85 void *ubik_SRXSecurityRock;
86 struct ubik_server *ubik_servers;
87 short ubik_callPortal;
89 static int BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
90 struct ubik_trans **transPtr, int readAny);
92 struct rx_securityClass *ubik_sc[3];
94 #define CStampVersion 1 /* meaning set ts->version */
97 * \brief Perform an operation at a quorum, handling error conditions.
98 * \return 0 if all worked and a quorum was contacted successfully
99 * \return otherwise mark failing server as down and return #UERROR
101 * \note If any server misses an update, we must wait #BIGTIME seconds before
102 * allowing the transaction to commit, to ensure that the missing and
103 * possibly still functioning server times out and stops handing out old
104 * data. This is done in the commit code, where we wait for a server marked
105 * down to have stayed down for #BIGTIME seconds before we allow a transaction
106 * to commit. A server that fails but comes back up won't give out old data
107 * because it is sent the sync count along with the beacon message that
108 * marks it as \b really up (\p beaconSinceDown).
111 ContactQuorum_NoArguments(afs_int32 (*proc)(struct rx_connection *, ubik_tid *),
112 register struct ubik_trans *atrans, int aflags)
114 register struct ubik_server *ts;
115 register afs_int32 code;
116 afs_int32 rcode, okcalls;
120 for (ts = ubik_servers; ts; ts = ts->next) {
121 /* for each server */
122 if (!ts->up || !ts->currentDB) {
123 ts->currentDB = 0; /* db is no longer current; we just missed an update */
124 continue; /* not up-to-date, don't bother */
126 code = (*proc)(ts->disk_rxcid, &atrans->tid);
127 if (code) { /* failure */
129 ts->up = 0; /* mark as down now; beacons will no longer be sent */
131 ts->beaconSinceDown = 0;
132 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
133 } else { /* success */
135 okcalls++; /* count up how many worked */
136 if (aflags & CStampVersion) {
137 ts->version = atrans->dbase->version;
141 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
142 if (okcalls + 1 >= ubik_quorum)
149 ContactQuorum_DISK_Lock(register struct ubik_trans *atrans, int aflags,afs_int32 file,
150 afs_int32 position, afs_int32 length, afs_int32 type)
152 register struct ubik_server *ts;
153 register afs_int32 code;
154 afs_int32 rcode, okcalls;
158 for (ts = ubik_servers; ts; ts = ts->next) {
159 /* for each server */
160 if (!ts->up || !ts->currentDB) {
161 ts->currentDB = 0; /* db is no longer current; we just missed an update */
162 continue; /* not up-to-date, don't bother */
164 code = DISK_Lock(ts->disk_rxcid, &atrans->tid, file, position, length,
166 if (code) { /* failure */
168 ts->up = 0; /* mark as down now; beacons will no longer be sent */
170 ts->beaconSinceDown = 0;
171 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
172 } else { /* success */
174 okcalls++; /* count up how many worked */
175 if (aflags & CStampVersion) {
176 ts->version = atrans->dbase->version;
180 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
181 if (okcalls + 1 >= ubik_quorum)
188 ContactQuorum_DISK_Write(register struct ubik_trans *atrans, int aflags,
189 afs_int32 file, afs_int32 position, bulkdata *data)
191 register struct ubik_server *ts;
192 register afs_int32 code;
193 afs_int32 rcode, okcalls;
197 for (ts = ubik_servers; ts; ts = ts->next) {
198 /* for each server */
199 if (!ts->up || !ts->currentDB) {
200 ts->currentDB = 0; /* db is no longer current; we just missed an update */
201 continue; /* not up-to-date, don't bother */
203 code = DISK_Write(ts->disk_rxcid, &atrans->tid, file, position, data);
204 if (code) { /* failure */
206 ts->up = 0; /* mark as down now; beacons will no longer be sent */
208 ts->beaconSinceDown = 0;
209 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
210 } else { /* success */
212 okcalls++; /* count up how many worked */
213 if (aflags & CStampVersion) {
214 ts->version = atrans->dbase->version;
218 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
219 if (okcalls + 1 >= ubik_quorum)
226 ContactQuorum_DISK_Truncate(register struct ubik_trans *atrans, int aflags,
227 afs_int32 file, afs_int32 length)
229 register struct ubik_server *ts;
230 register afs_int32 code;
231 afs_int32 rcode, okcalls;
235 for (ts = ubik_servers; ts; ts = ts->next) {
236 /* for each server */
237 if (!ts->up || !ts->currentDB) {
238 ts->currentDB = 0; /* db is no longer current; we just missed an update */
239 continue; /* not up-to-date, don't bother */
241 code = DISK_Truncate(ts->disk_rxcid, &atrans->tid, file, length);
242 if (code) { /* failure */
244 ts->up = 0; /* mark as down now; beacons will no longer be sent */
246 ts->beaconSinceDown = 0;
247 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
248 } else { /* success */
250 okcalls++; /* count up how many worked */
251 if (aflags & CStampVersion) {
252 ts->version = atrans->dbase->version;
256 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
257 if (okcalls + 1 >= ubik_quorum)
264 ContactQuorum_DISK_WriteV(register struct ubik_trans *atrans, int aflags,
265 iovec_wrt * io_vector, iovec_buf *io_buffer)
267 register struct ubik_server *ts;
268 register afs_int32 code;
269 afs_int32 rcode, okcalls;
273 for (ts = ubik_servers; ts; ts = ts->next) {
274 /* for each server */
275 if (!ts->up || !ts->currentDB) {
276 ts->currentDB = 0; /* db is no longer current; we just missed an update */
277 continue; /* not up-to-date, don't bother */
280 code = DISK_WriteV(ts->disk_rxcid, &atrans->tid, io_vector, io_buffer);
282 if ((code <= -450) && (code > -500)) {
283 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
284 * Un-bulk the entries and do individual DISK_Write calls
285 * instead of DISK_WriteV.
287 struct ubik_iovec *iovec =
288 (struct ubik_iovec *)io_vector->iovec_wrt_val;
289 char *iobuf = (char *)io_buffer->iovec_buf_val;
293 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
294 /* Sanity check for going off end of buffer */
295 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
299 tcbs.bulkdata_len = iovec[i].length;
300 tcbs.bulkdata_val = &iobuf[offset];
302 DISK_Write(ts->disk_rxcid, &atrans->tid, iovec[i].file,
303 iovec[i].position, &tcbs);
307 offset += iovec[i].length;
311 if (code) { /* failure */
313 ts->up = 0; /* mark as down now; beacons will no longer be sent */
315 ts->beaconSinceDown = 0;
316 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
317 } else { /* success */
319 okcalls++; /* count up how many worked */
320 if (aflags & CStampVersion) {
321 ts->version = atrans->dbase->version;
325 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
326 if (okcalls + 1 >= ubik_quorum)
333 ContactQuorum_DISK_SetVersion(register struct ubik_trans *atrans, int aflags,
334 ubik_version *OldVersion,
335 ubik_version *NewVersion)
337 register struct ubik_server *ts;
338 register afs_int32 code;
339 afs_int32 rcode, okcalls;
343 for (ts = ubik_servers; ts; ts = ts->next) {
344 /* for each server */
345 if (!ts->up || !ts->currentDB) {
346 ts->currentDB = 0; /* db is no longer current; we just missed an update */
347 continue; /* not up-to-date, don't bother */
349 code = DISK_SetVersion(ts->disk_rxcid, &atrans->tid, OldVersion,
351 if (code) { /* failure */
353 ts->up = 0; /* mark as down now; beacons will no longer be sent */
355 ts->beaconSinceDown = 0;
356 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
357 } else { /* success */
359 okcalls++; /* count up how many worked */
360 if (aflags & CStampVersion) {
361 ts->version = atrans->dbase->version;
365 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
366 if (okcalls + 1 >= ubik_quorum)
373 * \brief This routine initializes the ubik system for a set of servers.
374 * \return 0 for success, or an error code on failure.
375 * \param serverList set of servers specified; nServers gives the number of entries in this array.
376 * \param pathName provides an initial prefix used for naming storage files used by this system.
377 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
379 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
381 * \warning The host named by myHost should not also be listed in serverList.
383 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
386 ubik_ServerInitCommon(afs_int32 myHost, short myPort,
387 struct afsconf_cell *info, char clones[],
388 afs_int32 serverList[], const char *pathName,
389 struct ubik_dbase **dbase)
391 register struct ubik_dbase *tdb;
392 register afs_int32 code;
393 #ifdef AFS_PTHREAD_ENV
394 pthread_t rxServerThread; /* pthread variables */
395 pthread_t ubeacon_InteractThread;
396 pthread_t urecovery_InteractThread;
397 pthread_attr_t rxServer_tattr;
398 pthread_attr_t ubeacon_Interact_tattr;
399 pthread_attr_t urecovery_Interact_tattr;
402 extern int rx_stackSize;
406 struct rx_securityClass *secClass;
408 struct rx_service *tservice;
410 initialize_U_error_table();
412 tdb = (struct ubik_dbase *)malloc(sizeof(struct ubik_dbase));
413 tdb->pathName = (char *)malloc(strlen(pathName) + 1);
414 strcpy(tdb->pathName, pathName);
415 tdb->activeTrans = (struct ubik_trans *)0;
416 memset(&tdb->version, 0, sizeof(struct ubik_version));
417 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
418 #ifdef AFS_PTHREAD_ENV
419 assert(pthread_mutex_init(&tdb->versionLock, NULL) == 0);
421 Lock_Init(&tdb->versionLock);
424 tdb->read = uphys_read;
425 tdb->write = uphys_write;
426 tdb->truncate = uphys_truncate;
427 tdb->open = uphys_invalidate; /* this function isn't used any more */
428 tdb->sync = uphys_sync;
429 tdb->stat = uphys_stat;
430 tdb->getlabel = uphys_getlabel;
431 tdb->setlabel = uphys_setlabel;
432 tdb->getnfiles = uphys_getnfiles;
434 tdb->tidCounter = tdb->writeTidCounter = 0;
436 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
438 #ifdef AFS_PTHREAD_ENV
439 assert(pthread_cond_init(&tdb->version_cond, NULL) == 0);
440 assert(pthread_cond_init(&tdb->flags_cond, NULL) == 0);
441 #endif /* AFS_PTHREAD_ENV */
445 /* the following call is idempotent so when/if it got called earlier,
446 * by whatever called us, it doesn't really matter -- klm */
447 code = rx_Init(myPort);
451 ubik_callPortal = myPort;
452 /* try to get an additional security object */
453 ubik_sc[0] = rxnull_NewServerSecurityObject();
456 if (ubik_SRXSecurityProc) {
458 (*ubik_SRXSecurityProc) (ubik_SRXSecurityRock, &secClass,
461 ubik_sc[secIndex] = secClass;
464 /* for backwards compat this should keep working as it does now
467 /* This really needs to be up above, where I have put it. It works
468 * here when we're non-pthreaded, but the code above, when using
469 * pthreads may (and almost certainly does) end up calling on a
470 * pthread resource which gets initialized by rx_Init. The end
471 * result is that an assert fails and the program dies. -- klm
473 code = rx_Init(myPort);
479 rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, 3,
480 VOTE_ExecuteRequest);
481 if (tservice == (struct rx_service *)0) {
482 ubik_dprint("Could not create VOTE rx service!\n");
485 rx_SetMinProcs(tservice, 2);
486 rx_SetMaxProcs(tservice, 3);
489 rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, 3,
490 DISK_ExecuteRequest);
491 if (tservice == (struct rx_service *)0) {
492 ubik_dprint("Could not create DISK rx service!\n");
495 rx_SetMinProcs(tservice, 2);
496 rx_SetMaxProcs(tservice, 3);
498 /* start an rx_ServerProc to handle incoming RPC's in particular the
499 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
500 * the "steplock" problem in ubik initialization. Defect 11037.
502 #ifdef AFS_PTHREAD_ENV
503 /* do assert stuff */
504 assert(pthread_attr_init(&rxServer_tattr) == 0);
505 assert(pthread_attr_setdetachstate(&rxServer_tattr, PTHREAD_CREATE_DETACHED) == 0);
506 /* assert(pthread_attr_setstacksize(&rxServer_tattr, rx_stackSize) == 0); */
508 assert(pthread_create(&rxServerThread, &rxServer_tattr, (void *)rx_ServerProc, NULL) == 0);
510 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
511 NULL, "rx_ServerProc", &junk);
514 /* do basic initialization */
518 code = urecovery_Initialize(tdb);
522 code = ubeacon_InitServerListByInfo(myHost, info, clones);
524 code = ubeacon_InitServerList(myHost, serverList);
528 /* now start up async processes */
529 #ifdef AFS_PTHREAD_ENV
530 /* do assert stuff */
531 assert(pthread_attr_init(&ubeacon_Interact_tattr) == 0);
532 assert(pthread_attr_setdetachstate(&ubeacon_Interact_tattr, PTHREAD_CREATE_DETACHED) == 0);
533 /* assert(pthread_attr_setstacksize(&ubeacon_Interact_tattr, 16384) == 0); */
534 /* need another attr set here for priority??? - klm */
536 assert(pthread_create(&ubeacon_InteractThread, &ubeacon_Interact_tattr,
537 (void *)ubeacon_Interact, NULL) == 0);
539 code = LWP_CreateProcess(ubeacon_Interact, 16384 /*8192 */ ,
540 LWP_MAX_PRIORITY - 1, (void *)0, "beacon",
546 #ifdef AFS_PTHREAD_ENV
547 /* do assert stuff */
548 assert(pthread_attr_init(&urecovery_Interact_tattr) == 0);
549 assert(pthread_attr_setdetachstate(&urecovery_Interact_tattr, PTHREAD_CREATE_DETACHED) == 0);
550 /* assert(pthread_attr_setstacksize(&urecovery_Interact_tattr, 16384) == 0); */
551 /* need another attr set here for priority??? - klm */
553 assert(pthread_create(&urecovery_InteractThread, &urecovery_Interact_tattr,
554 (void *)urecovery_Interact, NULL) == 0);
556 return 0; /* is this correct? - klm */
558 code = LWP_CreateProcess(urecovery_Interact, 16384 /*8192 */ ,
559 LWP_MAX_PRIORITY - 1, (void *)0, "recovery",
567 * \see ubik_ServerInitCommon()
570 ubik_ServerInitByInfo(afs_int32 myHost, short myPort,
571 struct afsconf_cell *info, char clones[],
572 const char *pathName, struct ubik_dbase **dbase)
577 ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName,
583 * \see ubik_ServerInitCommon()
586 ubik_ServerInit(afs_int32 myHost, short myPort, afs_int32 serverList[],
587 const char *pathName, struct ubik_dbase **dbase)
592 ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
593 serverList, pathName, dbase);
598 * \brief This routine begins a read or write transaction on the transaction
599 * identified by transPtr, in the dbase named by dbase.
601 * An open mode of ubik_READTRANS identifies this as a read transaction,
602 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
603 * transPtr is set to the returned transaction control block.
604 * The readAny flag is set to 0 or 1 by the wrapper functions ubik_BeginTrans() or
605 * ubik_BeginTransReadAny() below.
607 * \note We can only begin transaction when we have an up-to-date database.
610 BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
611 struct ubik_trans **transPtr, int readAny)
613 struct ubik_trans *jt;
614 register struct ubik_trans *tt;
615 register afs_int32 code;
616 #if defined(UBIK_PAUSE)
618 #endif /* UBIK_PAUSE */
620 if ((transMode != UBIK_READTRANS) && readAny)
623 #if defined(UBIK_PAUSE)
624 /* if we're polling the slave sites, wait until the returns
625 * are all in. Otherwise, the urecovery_CheckTid call may
628 if (transMode == UBIK_WRITETRANS)
629 for (count = 75; dbase->flags & DBVOTING; --count) {
631 #ifdef GRAND_PAUSE_DEBUGGING
634 "%ld: myport=%d: BeginTrans is waiting 'cause of voting conflict\n",
635 time(0), ntohs(ubik_callPortal));
641 "%ld: myport=%d: BeginTrans failed because of voting conflict\n",
642 time(0), ntohs(ubik_callPortal));
644 return UNOQUORUM; /* a white lie */
646 #ifdef AFS_PTHREAD_ENV
653 #endif /* UBIK_PAUSE */
654 if (urecovery_AllBetter(dbase, readAny) == 0) {
658 /* otherwise we have a quorum, use it */
660 /* make sure that at most one write transaction occurs at any one time. This
661 * has nothing to do with transaction locking; that's enforced by the lock package. However,
662 * we can't even handle two non-conflicting writes, since our log and recovery modules
663 * don't know how to restore one without possibly picking up some data from the other. */
664 if (transMode == UBIK_WRITETRANS) {
665 /* if we're writing already, wait */
666 while (dbase->flags & DBWRITING) {
667 #ifdef AFS_PTHREAD_ENV
668 assert(pthread_cond_wait(&dbase->flags_cond, &dbase->versionLock) == 0);
671 LWP_WaitProcess(&dbase->flags);
676 if (!ubeacon_AmSyncSite()) {
682 /* create the transaction */
683 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
684 tt = jt; /* move to a register */
685 if (code || tt == (struct ubik_trans *)NULL) {
690 tt->flags |= TRREADANY;
691 /* label trans and dbase with new tid */
692 tt->tid.epoch = ubik_epochTime;
693 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
694 tt->tid.counter = (dbase->tidCounter += 2);
696 if (transMode == UBIK_WRITETRANS) {
697 /* for a write trans, we have to keep track of the write tid counter too */
698 #if defined(UBIK_PAUSE)
699 dbase->writeTidCounter = tt->tid.counter;
701 dbase->writeTidCounter += 2;
702 #endif /* UBIK_PAUSE */
704 /* next try to start transaction on appropriate number of machines */
705 code = ContactQuorum_NoArguments(DISK_Begin, tt, 0);
707 /* we must abort the operation */
709 ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
725 ubik_BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
726 struct ubik_trans **transPtr)
728 return BeginTrans(dbase, transMode, transPtr, 0);
735 ubik_BeginTransReadAny(register struct ubik_dbase *dbase, afs_int32 transMode,
736 struct ubik_trans **transPtr)
738 return BeginTrans(dbase, transMode, transPtr, 1);
742 * \brief This routine ends a read or write transaction by aborting it.
745 ubik_AbortTrans(register struct ubik_trans *transPtr)
747 register afs_int32 code;
749 register struct ubik_dbase *dbase;
751 dbase = transPtr->dbase;
753 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
754 /* see if we're still up-to-date */
755 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
756 udisk_abort(transPtr);
762 if (transPtr->type == UBIK_READTRANS) {
763 code = udisk_abort(transPtr);
769 /* below here, we know we're doing a write transaction */
770 if (!ubeacon_AmSyncSite()) {
771 udisk_abort(transPtr);
777 /* now it is safe to try remote abort */
778 code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
779 code2 = udisk_abort(transPtr);
782 return (code ? code : code2);
786 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
787 * \return an error code.
790 ubik_EndTrans(register struct ubik_trans *transPtr)
792 register afs_int32 code;
795 register struct ubik_server *ts;
797 register struct ubik_dbase *dbase;
799 if (transPtr->type == UBIK_WRITETRANS) {
800 code = ubik_Flush(transPtr);
802 ubik_AbortTrans(transPtr);
807 dbase = transPtr->dbase;
809 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
811 /* give up if no longer current */
812 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
813 udisk_abort(transPtr);
819 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
820 code = udisk_commit(transPtr);
822 goto success; /* update cachedVersion correctly */
828 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
829 udisk_abort(transPtr);
835 /* now it is safe to do commit */
836 code = udisk_commit(transPtr);
838 code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
840 /* failed to commit, so must return failure. Try to clear locks first, just for fun
841 * Note that we don't know if this transaction will eventually commit at this point.
842 * If it made it to a site that will be present in the next quorum, we win, otherwise
843 * we lose. If we contact a majority of sites, then we won't be here: contacting
844 * a majority guarantees commit, since it guarantees that one dude will be a
845 * member of the next quorum. */
846 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
851 /* before we can start sending unlock messages, we must wait until all servers
852 * that are possibly still functioning on the other side of a network partition
853 * have timed out. Check the server structures, compute how long to wait, then
854 * start the unlocks */
855 realStart = FT_ApproxTime();
857 /* wait for all servers to time out */
859 now = FT_ApproxTime();
860 /* check if we're still sync site, the guy should either come up
861 * to us, or timeout. Put safety check in anyway */
862 if (now - realStart > 10 * BIGTIME) {
863 ubik_stats.escapes++;
864 ubik_print("ubik escaping from commit wait\n");
867 for (ts = ubik_servers; ts; ts = ts->next) {
868 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
869 /* this guy could have some damaged data, wait for him */
871 tv.tv_sec = 1; /* try again after a while (ha ha) */
873 #ifdef AFS_PTHREAD_ENV
874 select(0, 0, 0, 0, &tv);
876 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
882 break; /* no down ones still pseudo-active */
885 /* finally, unlock all the dudes. We can return success independent of the number of servers
886 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
887 * The transaction is committed anyway, since we succeeded in contacting a quorum
888 * at the start (when invoking the DiskCommit function).
890 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
894 /* update version on successful EndTrans */
895 memcpy(&dbase->cachedVersion, &dbase->version,
896 sizeof(struct ubik_version));
903 * \brief This routine reads length bytes into buffer from the current position in the database.
905 * 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.
907 * \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.
910 ubik_Read(register struct ubik_trans *transPtr, void *buffer,
913 register afs_int32 code;
915 /* reads are easy to do: handle locally */
916 DBHOLD(transPtr->dbase);
917 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
918 DBRELE(transPtr->dbase);
923 udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
926 transPtr->seekPos += length;
928 DBRELE(transPtr->dbase);
933 * \brief This routine will flush the io data in the iovec structures.
935 * It first flushes to the local disk and then uses ContactQuorum to write it
936 * to the other servers.
939 ubik_Flush(struct ubik_trans *transPtr)
941 afs_int32 code, error = 0;
943 if (transPtr->type != UBIK_WRITETRANS)
945 if (!transPtr->iovec_info.iovec_wrt_len
946 || !transPtr->iovec_info.iovec_wrt_val)
949 DBHOLD(transPtr->dbase);
950 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
951 ERROR_EXIT(UNOQUORUM);
952 if (!ubeacon_AmSyncSite()) /* only sync site can write */
953 ERROR_EXIT(UNOTSYNC);
955 /* Update the rest of the servers in the quorum */
957 ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
958 &transPtr->iovec_data);
960 udisk_abort(transPtr);
961 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
962 transPtr->iovec_info.iovec_wrt_len = 0;
963 transPtr->iovec_data.iovec_buf_len = 0;
967 /* Wrote the buffers out, so start at scratch again */
968 transPtr->iovec_info.iovec_wrt_len = 0;
969 transPtr->iovec_data.iovec_buf_len = 0;
972 DBRELE(transPtr->dbase);
977 ubik_Write(register struct ubik_trans *transPtr, void *vbuffer,
980 struct ubik_iovec *iovec;
981 afs_int32 code, error = 0;
982 afs_int32 pos, len, size;
983 char * buffer = (char *)vbuffer;
985 if (transPtr->type != UBIK_WRITETRANS)
990 if (length > IOVEC_MAXBUF) {
991 for (pos = 0, len = length; len > 0; len -= size, pos += size) {
992 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
993 code = ubik_Write(transPtr, buffer+pos, size);
1000 if (!transPtr->iovec_info.iovec_wrt_val) {
1001 transPtr->iovec_info.iovec_wrt_len = 0;
1002 transPtr->iovec_info.iovec_wrt_val =
1003 (struct ubik_iovec *)malloc(IOVEC_MAXWRT *
1004 sizeof(struct ubik_iovec));
1005 transPtr->iovec_data.iovec_buf_len = 0;
1006 transPtr->iovec_data.iovec_buf_val = (char *)malloc(IOVEC_MAXBUF);
1007 if (!transPtr->iovec_info.iovec_wrt_val
1008 || !transPtr->iovec_data.iovec_buf_val) {
1009 if (transPtr->iovec_info.iovec_wrt_val)
1010 free(transPtr->iovec_info.iovec_wrt_val);
1011 transPtr->iovec_info.iovec_wrt_val = 0;
1012 if (transPtr->iovec_data.iovec_buf_val)
1013 free(transPtr->iovec_data.iovec_buf_val);
1014 transPtr->iovec_data.iovec_buf_val = 0;
1019 /* If this write won't fit in the structure, then flush it out and start anew */
1020 if ((transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT)
1021 || ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF)) {
1022 code = ubik_Flush(transPtr);
1027 DBHOLD(transPtr->dbase);
1028 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1029 ERROR_EXIT(UNOQUORUM);
1030 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1031 ERROR_EXIT(UNOTSYNC);
1033 /* Write to the local disk */
1035 udisk_write(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
1038 udisk_abort(transPtr);
1039 transPtr->iovec_info.iovec_wrt_len = 0;
1040 transPtr->iovec_data.iovec_buf_len = 0;
1041 DBRELE(transPtr->dbase);
1045 /* Collect writes for the other ubik servers (to be done in bulk) */
1046 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
1047 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
1048 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
1049 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
1051 memcpy(&transPtr->iovec_data.
1052 iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
1054 transPtr->iovec_info.iovec_wrt_len++;
1055 transPtr->iovec_data.iovec_buf_len += length;
1056 transPtr->seekPos += length;
1059 DBRELE(transPtr->dbase);
1064 * \brief This sets the file pointer associated with the current transaction
1065 * to the appropriate file and byte position.
1067 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1068 * and a byte position relative to the specified file \p position.
1071 ubik_Seek(register struct ubik_trans *transPtr, afs_int32 fileid,
1074 register afs_int32 code;
1076 DBHOLD(transPtr->dbase);
1077 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
1080 transPtr->seekFile = fileid;
1081 transPtr->seekPos = position;
1084 DBRELE(transPtr->dbase);
1089 * \brief This call returns the file pointer associated with the specified
1090 * transaction in \p fileid and \p position.
1093 ubik_Tell(register struct ubik_trans *transPtr, afs_int32 * fileid,
1094 afs_int32 * position)
1096 DBHOLD(transPtr->dbase);
1097 *fileid = transPtr->seekFile;
1098 *position = transPtr->seekPos;
1099 DBRELE(transPtr->dbase);
1104 * \brief This sets the file size for the currently-selected file to \p length
1105 * bytes, if length is less than the file's current size.
1108 ubik_Truncate(register struct ubik_trans *transPtr, afs_int32 length)
1110 afs_int32 code, error = 0;
1112 /* Will also catch if not UBIK_WRITETRANS */
1113 code = ubik_Flush(transPtr);
1117 DBHOLD(transPtr->dbase);
1118 /* first, check that quorum is still good, and that dbase is up-to-date */
1119 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1120 ERROR_EXIT(UNOQUORUM);
1121 if (!ubeacon_AmSyncSite())
1122 ERROR_EXIT(UNOTSYNC);
1124 /* now do the operation locally, and propagate it out */
1125 code = udisk_truncate(transPtr, transPtr->seekFile, length);
1128 ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
1132 /* we must abort the operation */
1133 udisk_abort(transPtr);
1134 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1139 DBRELE(transPtr->dbase);
1144 * \brief set a lock; all locks are released on transaction end (commit/abort)
1147 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
1150 afs_int32 code = 0, error = 0;
1152 if (atype == LOCKWRITE) {
1153 if (atrans->type == UBIK_READTRANS)
1155 code = ubik_Flush(atrans);
1160 DBHOLD(atrans->dbase);
1161 if (atype == LOCKREAD) {
1162 code = ulock_getLock(atrans, atype, 1);
1166 /* first, check that quorum is still good, and that dbase is up-to-date */
1167 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
1168 ERROR_EXIT(UNOQUORUM);
1169 if (!ubeacon_AmSyncSite())
1170 ERROR_EXIT(UNOTSYNC);
1172 /* now do the operation locally, and propagate it out */
1173 code = ulock_getLock(atrans, atype, 1);
1175 code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
1176 1 /*unused */ , LOCKWRITE);
1179 /* we must abort the operation */
1180 udisk_abort(atrans);
1181 ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
1187 DBRELE(atrans->dbase);
1192 * \brief utility to wait for a version # to change
1195 ubik_WaitVersion(register struct ubik_dbase *adatabase,
1196 register struct ubik_version *aversion)
1200 /* wait until version # changes, and then return */
1201 if (vcmp(*aversion, adatabase->version) != 0) {
1205 #ifdef AFS_PTHREAD_ENV
1206 assert(pthread_cond_wait(&adatabase->version_cond, &adatabase->versionLock) == 0);
1209 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
1216 * \brief utility to get the version of the dbase a transaction is dealing with
1219 ubik_GetVersion(register struct ubik_trans *atrans,
1220 register struct ubik_version *avers)
1222 *avers = atrans->dbase->version;
1227 * \brief Facility to simplify database caching.
1228 * \return zero if last trans was done on the local server and was successful.
1229 * \return -1 means bad (NULL) argument.
1231 * If return value is non-zero and the caller is a server caching part of the
1232 * Ubik database, it should invalidate that cache.
1235 ubik_CacheUpdate(register struct ubik_trans *atrans)
1237 if (!(atrans && atrans->dbase))
1239 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
1243 * "Who said anything about panicking?" snapped Arthur.
1244 * "This is still just the culture shock. You wait till I've settled down
1245 * into the situation and found my bearings. \em Then I'll start panicking!"
1248 * \returns There is no return from panic.
1251 panic(char *format, ...)
1255 va_start(ap, format);
1256 ubik_print("Ubik PANIC: ");
1257 ubik_vprint(format, ap);
1261 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
1262 exit(1); /* never know, though */
1266 * This function takes an IP addresses as its parameter. It returns the
1267 * the primary IP address that is on the host passed in, or 0 if not found.
1270 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
1272 struct ubik_server *ts;
1275 for (ts = ubik_servers; ts; ts = ts->next)
1276 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
1277 if (ts->addr[j] == addr)
1278 return ts->addr[0]; /* net byte order */
1279 return 0; /* if not in server database, return error */