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>
16 #include <sys/types.h>
25 #include <netinet/in.h>
26 #include <sys/param.h>
32 #include <afs/cellconfig.h>
34 #define UBIK_INTERNALS
38 #include <lwp.h> /* temporary hack by klm */
40 #define ERROR_EXIT(code) {error=(code); goto error_exit;}
44 * This system is organized in a hierarchical set of related modules. Modules
45 * at one level can only call modules at the same level or below.
47 * At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
48 * operating system primitives.
50 * At the next level (1) we have
52 * \li VOTER--The module responsible for casting votes when asked. It is also
53 * responsible for determining whether this server should try to become
54 * a synchronization site.
55 * \li BEACONER--The module responsible for sending keep-alives out when a
56 * server is actually the sync site, or trying to become a sync site.
57 * \li DISK--The module responsible for representing atomic transactions
58 * on the local disk. It maintains a new-value only log.
59 * \li LOCK--The module responsible for locking byte ranges in the database file.
61 * At the next level (2) we have
63 * \li RECOVERY--The module responsible for ensuring that all members of a quorum
64 * have the same up-to-date database after a new synchronization site is
65 * elected. This module runs only on the synchronization site.
67 * At the next level (3) we have
69 * \li REMOTE--The module responsible for interpreting requests from the sync
70 * site and applying them to the database, after obtaining the appropriate
73 * At the next level (4) we have
75 * \li UBIK--The module users call to perform operations on the database.
80 afs_int32 ubik_quorum = 0;
81 struct ubik_dbase *ubik_dbase = 0;
82 struct ubik_stats ubik_stats;
83 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
84 afs_int32 ubik_epochTime = 0;
85 afs_int32 urecovery_state = 0;
86 int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **, afs_int32 *);
87 void *ubik_SRXSecurityRock;
88 struct ubik_server *ubik_servers;
89 short ubik_callPortal;
91 static int BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
92 struct ubik_trans **transPtr, int readAny);
94 struct rx_securityClass *ubik_sc[3];
96 #define CStampVersion 1 /* meaning set ts->version */
99 * \brief Perform an operation at a quorum, handling error conditions.
100 * \return 0 if all worked and a quorum was contacted successfully
101 * \return otherwise mark failing server as down and return #UERROR
103 * \note If any server misses an update, we must wait #BIGTIME seconds before
104 * allowing the transaction to commit, to ensure that the missing and
105 * possibly still functioning server times out and stops handing out old
106 * data. This is done in the commit code, where we wait for a server marked
107 * down to have stayed down for #BIGTIME seconds before we allow a transaction
108 * to commit. A server that fails but comes back up won't give out old data
109 * because it is sent the sync count along with the beacon message that
110 * marks it as \b really up (\p beaconSinceDown).
113 ContactQuorum_NoArguments(afs_int32 (*proc)(struct rx_connection *, ubik_tid *),
114 register struct ubik_trans *atrans, int aflags)
116 register struct ubik_server *ts;
117 register afs_int32 code;
118 afs_int32 rcode, okcalls;
122 for (ts = ubik_servers; ts; ts = ts->next) {
123 /* for each server */
124 if (!ts->up || !ts->currentDB) {
125 ts->currentDB = 0; /* db is no longer current; we just missed an update */
126 continue; /* not up-to-date, don't bother */
128 code = (*proc)(ts->disk_rxcid, &atrans->tid);
129 if (code) { /* failure */
131 ts->up = 0; /* mark as down now; beacons will no longer be sent */
133 ts->beaconSinceDown = 0;
134 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
135 } else { /* success */
137 okcalls++; /* count up how many worked */
138 if (aflags & CStampVersion) {
139 ts->version = atrans->dbase->version;
143 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
144 if (okcalls + 1 >= ubik_quorum)
151 ContactQuorum_DISK_Lock(register struct ubik_trans *atrans, int aflags,afs_int32 file,
152 afs_int32 position, afs_int32 length, afs_int32 type)
154 register struct ubik_server *ts;
155 register afs_int32 code;
156 afs_int32 rcode, okcalls;
160 for (ts = ubik_servers; ts; ts = ts->next) {
161 /* for each server */
162 if (!ts->up || !ts->currentDB) {
163 ts->currentDB = 0; /* db is no longer current; we just missed an update */
164 continue; /* not up-to-date, don't bother */
166 code = DISK_Lock(ts->disk_rxcid, &atrans->tid, file, position, length,
168 if (code) { /* failure */
170 ts->up = 0; /* mark as down now; beacons will no longer be sent */
172 ts->beaconSinceDown = 0;
173 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
174 } else { /* success */
176 okcalls++; /* count up how many worked */
177 if (aflags & CStampVersion) {
178 ts->version = atrans->dbase->version;
182 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
183 if (okcalls + 1 >= ubik_quorum)
190 ContactQuorum_DISK_Write(register struct ubik_trans *atrans, int aflags,
191 afs_int32 file, afs_int32 position, bulkdata *data)
193 register struct ubik_server *ts;
194 register afs_int32 code;
195 afs_int32 rcode, okcalls;
199 for (ts = ubik_servers; ts; ts = ts->next) {
200 /* for each server */
201 if (!ts->up || !ts->currentDB) {
202 ts->currentDB = 0; /* db is no longer current; we just missed an update */
203 continue; /* not up-to-date, don't bother */
205 code = DISK_Write(ts->disk_rxcid, &atrans->tid, file, position, data);
206 if (code) { /* failure */
208 ts->up = 0; /* mark as down now; beacons will no longer be sent */
210 ts->beaconSinceDown = 0;
211 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
212 } else { /* success */
214 okcalls++; /* count up how many worked */
215 if (aflags & CStampVersion) {
216 ts->version = atrans->dbase->version;
220 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
221 if (okcalls + 1 >= ubik_quorum)
228 ContactQuorum_DISK_Truncate(register struct ubik_trans *atrans, int aflags,
229 afs_int32 file, afs_int32 length)
231 register struct ubik_server *ts;
232 register afs_int32 code;
233 afs_int32 rcode, okcalls;
237 for (ts = ubik_servers; ts; ts = ts->next) {
238 /* for each server */
239 if (!ts->up || !ts->currentDB) {
240 ts->currentDB = 0; /* db is no longer current; we just missed an update */
241 continue; /* not up-to-date, don't bother */
243 code = DISK_Truncate(ts->disk_rxcid, &atrans->tid, file, length);
244 if (code) { /* failure */
246 ts->up = 0; /* mark as down now; beacons will no longer be sent */
248 ts->beaconSinceDown = 0;
249 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
250 } else { /* success */
252 okcalls++; /* count up how many worked */
253 if (aflags & CStampVersion) {
254 ts->version = atrans->dbase->version;
258 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
259 if (okcalls + 1 >= ubik_quorum)
266 ContactQuorum_DISK_WriteV(register struct ubik_trans *atrans, int aflags,
267 iovec_wrt * io_vector, iovec_buf *io_buffer)
269 register struct ubik_server *ts;
270 register afs_int32 code;
271 afs_int32 rcode, okcalls;
275 for (ts = ubik_servers; ts; ts = ts->next) {
276 /* for each server */
277 if (!ts->up || !ts->currentDB) {
278 ts->currentDB = 0; /* db is no longer current; we just missed an update */
279 continue; /* not up-to-date, don't bother */
282 code = DISK_WriteV(ts->disk_rxcid, &atrans->tid, io_vector, io_buffer);
284 if ((code <= -450) && (code > -500)) {
285 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
286 * Un-bulk the entries and do individual DISK_Write calls
287 * instead of DISK_WriteV.
289 struct ubik_iovec *iovec =
290 (struct ubik_iovec *)io_vector->iovec_wrt_val;
291 char *iobuf = (char *)io_buffer->iovec_buf_val;
295 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
296 /* Sanity check for going off end of buffer */
297 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
301 tcbs.bulkdata_len = iovec[i].length;
302 tcbs.bulkdata_val = &iobuf[offset];
304 DISK_Write(ts->disk_rxcid, &atrans->tid, iovec[i].file,
305 iovec[i].position, &tcbs);
309 offset += iovec[i].length;
313 if (code) { /* failure */
315 ts->up = 0; /* mark as down now; beacons will no longer be sent */
317 ts->beaconSinceDown = 0;
318 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
319 } else { /* success */
321 okcalls++; /* count up how many worked */
322 if (aflags & CStampVersion) {
323 ts->version = atrans->dbase->version;
327 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
328 if (okcalls + 1 >= ubik_quorum)
335 ContactQuorum_DISK_SetVersion(register struct ubik_trans *atrans, int aflags,
336 ubik_version *OldVersion,
337 ubik_version *NewVersion)
339 register struct ubik_server *ts;
340 register afs_int32 code;
341 afs_int32 rcode, okcalls;
345 for (ts = ubik_servers; ts; ts = ts->next) {
346 /* for each server */
347 if (!ts->up || !ts->currentDB) {
348 ts->currentDB = 0; /* db is no longer current; we just missed an update */
349 continue; /* not up-to-date, don't bother */
351 code = DISK_SetVersion(ts->disk_rxcid, &atrans->tid, OldVersion,
353 if (code) { /* failure */
355 ts->up = 0; /* mark as down now; beacons will no longer be sent */
357 ts->beaconSinceDown = 0;
358 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
359 } else { /* success */
361 okcalls++; /* count up how many worked */
362 if (aflags & CStampVersion) {
363 ts->version = atrans->dbase->version;
367 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
368 if (okcalls + 1 >= ubik_quorum)
375 * \brief This routine initializes the ubik system for a set of servers.
376 * \return 0 for success, or an error code on failure.
377 * \param serverList set of servers specified; nServers gives the number of entries in this array.
378 * \param pathName provides an initial prefix used for naming storage files used by this system.
379 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
381 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
383 * \warning The host named by myHost should not also be listed in serverList.
385 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
388 ubik_ServerInitCommon(afs_int32 myHost, short myPort,
389 struct afsconf_cell *info, char clones[],
390 afs_int32 serverList[], char *pathName,
391 struct ubik_dbase **dbase)
393 register struct ubik_dbase *tdb;
394 register afs_int32 code;
395 #ifdef AFS_PTHREAD_ENV
396 pthread_t rxServerThread; /* pthread variables */
397 pthread_t ubeacon_InteractThread;
398 pthread_t urecovery_InteractThread;
399 pthread_attr_t rxServer_tattr;
400 pthread_attr_t ubeacon_Interact_tattr;
401 pthread_attr_t urecovery_Interact_tattr;
407 struct rx_securityClass *secClass;
409 struct rx_service *tservice;
410 extern int rx_stackSize;
412 initialize_U_error_table();
414 tdb = (struct ubik_dbase *)malloc(sizeof(struct ubik_dbase));
415 tdb->pathName = (char *)malloc(strlen(pathName) + 1);
416 strcpy(tdb->pathName, pathName);
417 tdb->activeTrans = (struct ubik_trans *)0;
418 memset(&tdb->version, 0, sizeof(struct ubik_version));
419 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
420 Lock_Init(&tdb->versionLock);
422 tdb->read = uphys_read;
423 tdb->write = uphys_write;
424 tdb->truncate = uphys_truncate;
425 tdb->open = uphys_invalidate; /* this function isn't used any more */
426 tdb->sync = uphys_sync;
427 tdb->stat = uphys_stat;
428 tdb->getlabel = uphys_getlabel;
429 tdb->setlabel = uphys_setlabel;
430 tdb->getnfiles = uphys_getnfiles;
432 tdb->tidCounter = tdb->writeTidCounter = 0;
434 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
438 /* the following call is idempotent so when/if it got called earlier,
439 * by whatever called us, it doesn't really matter -- klm */
440 code = rx_Init(myPort);
444 ubik_callPortal = myPort;
445 /* try to get an additional security object */
446 ubik_sc[0] = rxnull_NewServerSecurityObject();
449 if (ubik_SRXSecurityProc) {
451 (*ubik_SRXSecurityProc) (ubik_SRXSecurityRock, &secClass,
454 ubik_sc[secIndex] = secClass;
457 /* for backwards compat this should keep working as it does now
460 /* This really needs to be up above, where I have put it. It works
461 * here when we're non-pthreaded, but the code above, when using
462 * pthreads may (and almost certainly does) end up calling on a
463 * pthread resource which gets initialized by rx_Init. The end
464 * result is that an assert fails and the program dies. -- klm
466 code = rx_Init(myPort);
472 rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, 3,
473 VOTE_ExecuteRequest);
474 if (tservice == (struct rx_service *)0) {
475 ubik_dprint("Could not create VOTE rx service!\n");
478 rx_SetMinProcs(tservice, 2);
479 rx_SetMaxProcs(tservice, 3);
482 rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, 3,
483 DISK_ExecuteRequest);
484 if (tservice == (struct rx_service *)0) {
485 ubik_dprint("Could not create DISK rx service!\n");
488 rx_SetMinProcs(tservice, 2);
489 rx_SetMaxProcs(tservice, 3);
491 /* start an rx_ServerProc to handle incoming RPC's in particular the
492 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
493 * the "steplock" problem in ubik initialization. Defect 11037.
495 #ifdef AFS_PTHREAD_ENV
496 /* do assert stuff */
497 assert(pthread_attr_init(&rxServer_tattr) == 0);
498 assert(pthread_attr_setdetachstate(&rxServer_tattr, PTHREAD_CREATE_DETACHED) == 0);
499 /* assert(pthread_attr_setstacksize(&rxServer_tattr, rx_stackSize) == 0); */
501 assert(pthread_create(&rxServerThread, &rxServer_tattr, (void *)rx_ServerProc, NULL) == 0);
503 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
504 NULL, "rx_ServerProc", &junk);
507 /* do basic initialization */
511 code = urecovery_Initialize(tdb);
515 code = ubeacon_InitServerListByInfo(myHost, info, clones);
517 code = ubeacon_InitServerList(myHost, serverList);
521 /* now start up async processes */
522 #ifdef AFS_PTHREAD_ENV
523 /* do assert stuff */
524 assert(pthread_attr_init(&ubeacon_Interact_tattr) == 0);
525 assert(pthread_attr_setdetachstate(&ubeacon_Interact_tattr, PTHREAD_CREATE_DETACHED) == 0);
526 /* assert(pthread_attr_setstacksize(&ubeacon_Interact_tattr, 16384) == 0); */
527 /* need another attr set here for priority??? - klm */
529 assert(pthread_create(&ubeacon_InteractThread, &ubeacon_Interact_tattr,
530 (void *)ubeacon_Interact, NULL) == 0);
532 code = LWP_CreateProcess(ubeacon_Interact, 16384 /*8192 */ ,
533 LWP_MAX_PRIORITY - 1, (void *)0, "beacon",
539 #ifdef AFS_PTHREAD_ENV
540 /* do assert stuff */
541 assert(pthread_attr_init(&urecovery_Interact_tattr) == 0);
542 assert(pthread_attr_setdetachstate(&urecovery_Interact_tattr, PTHREAD_CREATE_DETACHED) == 0);
543 /* assert(pthread_attr_setstacksize(&urecovery_Interact_tattr, 16384) == 0); */
544 /* need another attr set here for priority??? - klm */
546 assert(pthread_create(&urecovery_InteractThread, &urecovery_Interact_tattr,
547 (void *)urecovery_Interact, NULL) == 0);
549 return 0; /* is this correct? - klm */
551 code = LWP_CreateProcess(urecovery_Interact, 16384 /*8192 */ ,
552 LWP_MAX_PRIORITY - 1, (void *)0, "recovery",
560 * \see ubik_ServerInitCommon()
563 ubik_ServerInitByInfo(afs_int32 myHost, short myPort,
564 struct afsconf_cell *info, char clones[],
565 char *pathName, struct ubik_dbase **dbase)
570 ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName,
576 * \see ubik_ServerInitCommon()
579 ubik_ServerInit(afs_int32 myHost, short myPort, afs_int32 serverList[],
580 char *pathName, struct ubik_dbase **dbase)
585 ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
586 serverList, pathName, dbase);
591 * \brief This routine begins a read or write transaction on the transaction
592 * identified by transPtr, in the dbase named by dbase.
594 * An open mode of ubik_READTRANS identifies this as a read transaction,
595 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
596 * transPtr is set to the returned transaction control block.
597 * The readAny flag is set to 0 or 1 by the wrapper functions ubik_BeginTrans() or
598 * ubik_BeginTransReadAny() below.
600 * \note We can only begin transaction when we have an up-to-date database.
603 BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
604 struct ubik_trans **transPtr, int readAny)
606 struct ubik_trans *jt;
607 register struct ubik_trans *tt;
608 register afs_int32 code;
609 #if defined(UBIK_PAUSE)
611 #endif /* UBIK_PAUSE */
613 if ((transMode != UBIK_READTRANS) && readAny)
616 #if defined(UBIK_PAUSE)
617 /* if we're polling the slave sites, wait until the returns
618 * are all in. Otherwise, the urecovery_CheckTid call may
621 if (transMode == UBIK_WRITETRANS)
622 for (count = 75; dbase->flags & DBVOTING; --count) {
624 #ifdef GRAND_PAUSE_DEBUGGING
627 "%ld: myport=%d: BeginTrans is waiting 'cause of voting conflict\n",
628 time(0), ntohs(ubik_callPortal));
634 "%ld: myport=%d: BeginTrans failed because of voting conflict\n",
635 time(0), ntohs(ubik_callPortal));
637 return UNOQUORUM; /* a white lie */
639 #ifdef AFS_PTHREAD_ENV
646 #endif /* UBIK_PAUSE */
647 if (urecovery_AllBetter(dbase, readAny) == 0) {
651 /* otherwise we have a quorum, use it */
653 /* make sure that at most one write transaction occurs at any one time. This
654 * has nothing to do with transaction locking; that's enforced by the lock package. However,
655 * we can't even handle two non-conflicting writes, since our log and recovery modules
656 * don't know how to restore one without possibly picking up some data from the other. */
657 if (transMode == UBIK_WRITETRANS) {
658 /* if we're writing already, wait */
659 while (dbase->flags & DBWRITING) {
661 #ifdef AFS_PTHREAD_ENV
662 assert(pthread_mutex_lock(&dbase->flags_mutex) == 0);
663 assert(pthread_cond_wait(&dbase->flags_cond, &dbase->flags_mutex) == 0);
664 assert(pthread_mutex_unlock(&dbase->flags_mutex) == 0);
666 LWP_WaitProcess(&dbase->flags);
670 if (!ubeacon_AmSyncSite()) {
676 /* create the transaction */
677 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
678 tt = jt; /* move to a register */
679 if (code || tt == (struct ubik_trans *)NULL) {
684 tt->flags |= TRREADANY;
685 /* label trans and dbase with new tid */
686 tt->tid.epoch = ubik_epochTime;
687 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
688 tt->tid.counter = (dbase->tidCounter += 2);
690 if (transMode == UBIK_WRITETRANS) {
691 /* for a write trans, we have to keep track of the write tid counter too */
692 #if defined(UBIK_PAUSE)
693 dbase->writeTidCounter = tt->tid.counter;
695 dbase->writeTidCounter += 2;
696 #endif /* UBIK_PAUSE */
698 /* next try to start transaction on appropriate number of machines */
699 code = ContactQuorum_NoArguments(DISK_Begin, tt, 0);
701 /* we must abort the operation */
703 ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
719 ubik_BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
720 struct ubik_trans **transPtr)
722 return BeginTrans(dbase, transMode, transPtr, 0);
729 ubik_BeginTransReadAny(register struct ubik_dbase *dbase, afs_int32 transMode,
730 struct ubik_trans **transPtr)
732 return BeginTrans(dbase, transMode, transPtr, 1);
736 * \brief This routine ends a read or write transaction by aborting it.
739 ubik_AbortTrans(register struct ubik_trans *transPtr)
741 register afs_int32 code;
743 register struct ubik_dbase *dbase;
745 dbase = transPtr->dbase;
747 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
748 /* see if we're still up-to-date */
749 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
750 udisk_abort(transPtr);
756 if (transPtr->type == UBIK_READTRANS) {
757 code = udisk_abort(transPtr);
763 /* below here, we know we're doing a write transaction */
764 if (!ubeacon_AmSyncSite()) {
765 udisk_abort(transPtr);
771 /* now it is safe to try remote abort */
772 code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
773 code2 = udisk_abort(transPtr);
776 return (code ? code : code2);
780 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
781 * \return an error code.
784 ubik_EndTrans(register struct ubik_trans *transPtr)
786 register afs_int32 code;
789 register struct ubik_server *ts;
791 register struct ubik_dbase *dbase;
793 if (transPtr->type == UBIK_WRITETRANS) {
794 code = ubik_Flush(transPtr);
796 ubik_AbortTrans(transPtr);
801 dbase = transPtr->dbase;
803 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
805 /* give up if no longer current */
806 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
807 udisk_abort(transPtr);
813 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
814 code = udisk_commit(transPtr);
816 goto success; /* update cachedVersion correctly */
822 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
823 udisk_abort(transPtr);
829 /* now it is safe to do commit */
830 code = udisk_commit(transPtr);
832 code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
834 /* failed to commit, so must return failure. Try to clear locks first, just for fun
835 * Note that we don't know if this transaction will eventually commit at this point.
836 * If it made it to a site that will be present in the next quorum, we win, otherwise
837 * we lose. If we contact a majority of sites, then we won't be here: contacting
838 * a majority guarantees commit, since it guarantees that one dude will be a
839 * member of the next quorum. */
840 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
845 /* before we can start sending unlock messages, we must wait until all servers
846 * that are possibly still functioning on the other side of a network partition
847 * have timed out. Check the server structures, compute how long to wait, then
848 * start the unlocks */
849 realStart = FT_ApproxTime();
851 /* wait for all servers to time out */
853 now = FT_ApproxTime();
854 /* check if we're still sync site, the guy should either come up
855 * to us, or timeout. Put safety check in anyway */
856 if (now - realStart > 10 * BIGTIME) {
857 ubik_stats.escapes++;
858 ubik_print("ubik escaping from commit wait\n");
861 for (ts = ubik_servers; ts; ts = ts->next) {
862 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
863 /* this guy could have some damaged data, wait for him */
865 tv.tv_sec = 1; /* try again after a while (ha ha) */
867 #ifdef AFS_PTHREAD_ENV
868 select(0, 0, 0, 0, &tv);
870 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
876 break; /* no down ones still pseudo-active */
879 /* finally, unlock all the dudes. We can return success independent of the number of servers
880 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
881 * The transaction is committed anyway, since we succeeded in contacting a quorum
882 * at the start (when invoking the DiskCommit function).
884 ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
888 /* update version on successful EndTrans */
889 memcpy(&dbase->cachedVersion, &dbase->version,
890 sizeof(struct ubik_version));
897 * \brief This routine reads length bytes into buffer from the current position in the database.
899 * 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.
901 * \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.
904 ubik_Read(register struct ubik_trans *transPtr, void *buffer,
907 register afs_int32 code;
909 /* reads are easy to do: handle locally */
910 DBHOLD(transPtr->dbase);
911 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
912 DBRELE(transPtr->dbase);
917 udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
920 transPtr->seekPos += length;
922 DBRELE(transPtr->dbase);
927 * \brief This routine will flush the io data in the iovec structures.
929 * It first flushes to the local disk and then uses ContactQuorum to write it
930 * to the other servers.
933 ubik_Flush(struct ubik_trans *transPtr)
935 afs_int32 code, error = 0;
937 if (transPtr->type != UBIK_WRITETRANS)
939 if (!transPtr->iovec_info.iovec_wrt_len
940 || !transPtr->iovec_info.iovec_wrt_val)
943 DBHOLD(transPtr->dbase);
944 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
945 ERROR_EXIT(UNOQUORUM);
946 if (!ubeacon_AmSyncSite()) /* only sync site can write */
947 ERROR_EXIT(UNOTSYNC);
949 /* Update the rest of the servers in the quorum */
951 ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
952 &transPtr->iovec_data);
954 udisk_abort(transPtr);
955 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
956 transPtr->iovec_info.iovec_wrt_len = 0;
957 transPtr->iovec_data.iovec_buf_len = 0;
961 /* Wrote the buffers out, so start at scratch again */
962 transPtr->iovec_info.iovec_wrt_len = 0;
963 transPtr->iovec_data.iovec_buf_len = 0;
966 DBRELE(transPtr->dbase);
971 ubik_Write(register struct ubik_trans *transPtr, void *buffer,
974 struct ubik_iovec *iovec;
975 afs_int32 code, error = 0;
976 afs_int32 pos, len, size;
978 if (transPtr->type != UBIK_WRITETRANS)
983 if (length > IOVEC_MAXBUF) {
984 for (pos = 0, len = length; len > 0; len -= size, pos += size) {
985 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
986 code = ubik_Write(transPtr, buffer+pos, size);
993 if (!transPtr->iovec_info.iovec_wrt_val) {
994 transPtr->iovec_info.iovec_wrt_len = 0;
995 transPtr->iovec_info.iovec_wrt_val =
996 (struct ubik_iovec *)malloc(IOVEC_MAXWRT *
997 sizeof(struct ubik_iovec));
998 transPtr->iovec_data.iovec_buf_len = 0;
999 transPtr->iovec_data.iovec_buf_val = (char *)malloc(IOVEC_MAXBUF);
1000 if (!transPtr->iovec_info.iovec_wrt_val
1001 || !transPtr->iovec_data.iovec_buf_val) {
1002 if (transPtr->iovec_info.iovec_wrt_val)
1003 free(transPtr->iovec_info.iovec_wrt_val);
1004 transPtr->iovec_info.iovec_wrt_val = 0;
1005 if (transPtr->iovec_data.iovec_buf_val)
1006 free(transPtr->iovec_data.iovec_buf_val);
1007 transPtr->iovec_data.iovec_buf_val = 0;
1012 /* If this write won't fit in the structure, then flush it out and start anew */
1013 if ((transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT)
1014 || ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF)) {
1015 code = ubik_Flush(transPtr);
1020 DBHOLD(transPtr->dbase);
1021 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1022 ERROR_EXIT(UNOQUORUM);
1023 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1024 ERROR_EXIT(UNOTSYNC);
1026 /* Write to the local disk */
1028 udisk_write(transPtr, transPtr->seekFile, buffer, transPtr->seekPos,
1031 udisk_abort(transPtr);
1032 transPtr->iovec_info.iovec_wrt_len = 0;
1033 transPtr->iovec_data.iovec_buf_len = 0;
1034 DBRELE(transPtr->dbase);
1038 /* Collect writes for the other ubik servers (to be done in bulk) */
1039 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
1040 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
1041 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
1042 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
1044 memcpy(&transPtr->iovec_data.
1045 iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
1047 transPtr->iovec_info.iovec_wrt_len++;
1048 transPtr->iovec_data.iovec_buf_len += length;
1049 transPtr->seekPos += length;
1052 DBRELE(transPtr->dbase);
1057 * \brief This sets the file pointer associated with the current transaction
1058 * to the appropriate file and byte position.
1060 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1061 * and a byte position relative to the specified file \p position.
1064 ubik_Seek(register struct ubik_trans *transPtr, afs_int32 fileid,
1067 register afs_int32 code;
1069 DBHOLD(transPtr->dbase);
1070 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
1073 transPtr->seekFile = fileid;
1074 transPtr->seekPos = position;
1077 DBRELE(transPtr->dbase);
1082 * \brief This call returns the file pointer associated with the specified
1083 * transaction in \p fileid and \p position.
1086 ubik_Tell(register struct ubik_trans *transPtr, afs_int32 * fileid,
1087 afs_int32 * position)
1089 DBHOLD(transPtr->dbase);
1090 *fileid = transPtr->seekFile;
1091 *position = transPtr->seekPos;
1092 DBRELE(transPtr->dbase);
1097 * \brief This sets the file size for the currently-selected file to \p length
1098 * bytes, if length is less than the file's current size.
1101 ubik_Truncate(register struct ubik_trans *transPtr, afs_int32 length)
1103 afs_int32 code, error = 0;
1105 /* Will also catch if not UBIK_WRITETRANS */
1106 code = ubik_Flush(transPtr);
1110 DBHOLD(transPtr->dbase);
1111 /* first, check that quorum is still good, and that dbase is up-to-date */
1112 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
1113 ERROR_EXIT(UNOQUORUM);
1114 if (!ubeacon_AmSyncSite())
1115 ERROR_EXIT(UNOTSYNC);
1117 /* now do the operation locally, and propagate it out */
1118 code = udisk_truncate(transPtr, transPtr->seekFile, length);
1121 ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
1125 /* we must abort the operation */
1126 udisk_abort(transPtr);
1127 ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
1132 DBRELE(transPtr->dbase);
1137 * \brief set a lock; all locks are released on transaction end (commit/abort)
1140 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
1143 afs_int32 code = 0, error = 0;
1145 if (atype == LOCKWRITE) {
1146 if (atrans->type == UBIK_READTRANS)
1148 code = ubik_Flush(atrans);
1153 DBHOLD(atrans->dbase);
1154 if (atype == LOCKREAD) {
1155 code = ulock_getLock(atrans, atype, 1);
1159 /* first, check that quorum is still good, and that dbase is up-to-date */
1160 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
1161 ERROR_EXIT(UNOQUORUM);
1162 if (!ubeacon_AmSyncSite())
1163 ERROR_EXIT(UNOTSYNC);
1165 /* now do the operation locally, and propagate it out */
1166 code = ulock_getLock(atrans, atype, 1);
1168 code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
1169 1 /*unused */ , LOCKWRITE);
1172 /* we must abort the operation */
1173 udisk_abort(atrans);
1174 ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
1180 DBRELE(atrans->dbase);
1185 * \brief utility to wait for a version # to change
1188 ubik_WaitVersion(register struct ubik_dbase *adatabase,
1189 register struct ubik_version *aversion)
1192 /* wait until version # changes, and then return */
1193 if (vcmp(*aversion, adatabase->version) != 0)
1195 #ifdef AFS_PTHREAD_ENV
1196 assert(pthread_mutex_lock(&adatabase->version_mutex) == 0);
1197 assert(pthread_cond_wait(&adatabase->version_cond,&adatabase->version_mutex) == 0);
1198 assert(pthread_mutex_unlock(&adatabase->version_mutex) == 0);
1200 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
1206 * \brief utility to get the version of the dbase a transaction is dealing with
1209 ubik_GetVersion(register struct ubik_trans *atrans,
1210 register struct ubik_version *avers)
1212 *avers = atrans->dbase->version;
1217 * \brief Facility to simplify database caching.
1218 * \return zero if last trans was done on the local server and was successful.
1219 * \return -1 means bad (NULL) argument.
1221 * If return value is non-zero and the caller is a server caching part of the
1222 * Ubik database, it should invalidate that cache.
1225 ubik_CacheUpdate(register struct ubik_trans *atrans)
1227 if (!(atrans && atrans->dbase))
1229 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
1233 * "Who said anything about panicking?" snapped Arthur.
1234 * "This is still just the culture shock. You wait till I've settled down
1235 * into the situation and found my bearings. \em Then I'll start panicking!"
1238 * \returns There is no return from panic.
1241 panic(char *format, ...)
1245 va_start(ap, format);
1246 ubik_print("Ubik PANIC: ");
1247 ubik_vprint(format, ap);
1251 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
1252 exit(1); /* never know, though */
1256 * This function takes an IP addresses as its parameter. It returns the
1257 * the primary IP address that is on the host passed in, or 0 if not found.
1260 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
1262 struct ubik_server *ts;
1265 for (ts = ubik_servers; ts; ts = ts->next)
1266 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
1267 if (ts->addr[j] == addr)
1268 return ts->addr[0]; /* net byte order */
1269 return 0; /* if not in server database, return error */