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>
17 #ifdef AFS_PTHREAD_ENV
18 # include <opr/lock.h>
20 # include <opr/lockstub.h>
25 #include <afs/cellconfig.h>
28 #define UBIK_INTERNALS
32 #include <lwp.h> /* temporary hack by klm */
34 #define ERROR_EXIT(code) do { \
41 * This system is organized in a hierarchical set of related modules. Modules
42 * at one level can only call modules at the same level or below.
44 * At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
45 * operating system primitives.
47 * At the next level (1) we have
49 * \li VOTER--The module responsible for casting votes when asked. It is also
50 * responsible for determining whether this server should try to become
51 * a synchronization site.
52 * \li BEACONER--The module responsible for sending keep-alives out when a
53 * server is actually the sync site, or trying to become a sync site.
54 * \li DISK--The module responsible for representing atomic transactions
55 * on the local disk. It maintains a new-value only log.
56 * \li LOCK--The module responsible for locking byte ranges in the database file.
58 * At the next level (2) we have
60 * \li RECOVERY--The module responsible for ensuring that all members of a quorum
61 * have the same up-to-date database after a new synchronization site is
62 * elected. This module runs only on the synchronization site.
64 * At the next level (3) we have
66 * \li REMOTE--The module responsible for interpreting requests from the sync
67 * site and applying them to the database, after obtaining the appropriate
70 * At the next level (4) we have
72 * \li UBIK--The module users call to perform operations on the database.
77 afs_int32 ubik_quorum
= 0;
78 struct ubik_dbase
*ubik_dbase
= 0;
79 struct ubik_stats ubik_stats
;
80 afs_uint32 ubik_host
[UBIK_MAX_INTERFACE_ADDR
];
81 afs_int32 urecovery_state
= 0;
82 int (*ubik_SyncWriterCacheProc
) (void);
83 struct ubik_server
*ubik_servers
;
84 short ubik_callPortal
;
86 /* These global variables were used to control the server security layers.
87 * They are retained for backwards compatibility with legacy callers.
89 * The ubik_SetServerSecurityProcs() interface should be used instead.
92 int (*ubik_SRXSecurityProc
) (void *, struct rx_securityClass
**, afs_int32
*);
93 void *ubik_SRXSecurityRock
;
94 int (*ubik_CheckRXSecurityProc
) (void *, struct rx_call
*);
95 void *ubik_CheckRXSecurityRock
;
99 static int BeginTrans(struct ubik_dbase
*dbase
, afs_int32 transMode
,
100 struct ubik_trans
**transPtr
, int readAny
);
102 static struct rx_securityClass
**ubik_sc
= NULL
;
103 static void (*buildSecClassesProc
)(void *, struct rx_securityClass
***,
105 static int (*checkSecurityProc
)(void *, struct rx_call
*) = NULL
;
106 static void *securityRock
= NULL
;
108 struct version_data version_globals
;
110 #define CStampVersion 1 /* meaning set ts->version */
112 static_inline
struct rx_connection
*
113 Quorum_StartIO(struct ubik_trans
*atrans
, struct ubik_server
*as
)
115 struct rx_connection
*conn
;
118 conn
= as
->disk_rxcid
;
120 #ifdef AFS_PTHREAD_ENV
121 rx_GetConnection(conn
);
123 DBRELE(atrans
->dbase
);
126 #endif /* AFS_PTHREAD_ENV */
132 Quorum_EndIO(struct ubik_trans
*atrans
, struct rx_connection
*aconn
)
134 #ifdef AFS_PTHREAD_ENV
135 DBHOLD(atrans
->dbase
);
136 rx_PutConnection(aconn
);
137 #endif /* AFS_PTHREAD_ENV */
142 * Iterate over all servers. Callers pass in *ts which is used to track
143 * the current server.
144 * - Returns 1 if there are no more servers
145 * - Returns 0 with conn set to the connection for the current server if
146 * it's up and current
149 ContactQuorum_iterate(struct ubik_trans
*atrans
, int aflags
, struct ubik_server
**ts
,
150 struct rx_connection
**conn
, afs_int32
*rcode
,
151 afs_int32
*okcalls
, afs_int32 code
)
154 /* Initial call - start iterating over servers */
161 Quorum_EndIO(atrans
, *conn
);
163 if (code
) { /* failure */
166 (*ts
)->up
= 0; /* mark as down now; beacons will no longer be sent */
167 (*ts
)->beaconSinceDown
= 0;
169 (*ts
)->currentDB
= 0;
170 urecovery_LostServer(*ts
); /* tell recovery to try to resend dbase later */
171 } else { /* success */
173 (*okcalls
)++; /* count up how many worked */
174 if (aflags
& CStampVersion
) {
175 (*ts
)->version
= atrans
->dbase
->version
;
184 if (!(*ts
)->up
|| !(*ts
)->currentDB
) {
186 (*ts
)->currentDB
= 0; /* db is no longer current; we just missed an update */
187 return 0; /* not up-to-date, don't bother. NULL conn will tell caller not to use */
190 *conn
= Quorum_StartIO(atrans
, *ts
);
195 ContactQuorum_rcode(int okcalls
, afs_int32 rcode
)
198 * return 0 if we successfully contacted a quorum, otherwise return error code.
199 * We don't have to contact ourselves (that was done locally)
201 if (okcalls
+ 1 >= ubik_quorum
)
204 return (rcode
!= 0) ? rcode
: UNOQUORUM
;
208 * \brief Perform an operation at a quorum, handling error conditions.
209 * \return 0 if all worked and a quorum was contacted successfully
210 * \return otherwise mark failing server as down and return #UERROR
212 * \note If any server misses an update, we must wait #BIGTIME seconds before
213 * allowing the transaction to commit, to ensure that the missing and
214 * possibly still functioning server times out and stops handing out old
215 * data. This is done in the commit code, where we wait for a server marked
216 * down to have stayed down for #BIGTIME seconds before we allow a transaction
217 * to commit. A server that fails but comes back up won't give out old data
218 * because it is sent the sync count along with the beacon message that
219 * marks it as \b really up (\p beaconSinceDown).
222 ContactQuorum_NoArguments(afs_int32 (*proc
)(struct rx_connection
*, ubik_tid
*),
223 struct ubik_trans
*atrans
, int aflags
)
225 struct ubik_server
*ts
= NULL
;
226 afs_int32 code
= 0, rcode
, okcalls
;
227 struct rx_connection
*conn
;
230 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
233 code
= (*proc
)(conn
, &atrans
->tid
);
234 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
236 return ContactQuorum_rcode(okcalls
, rcode
);
241 ContactQuorum_DISK_Lock(struct ubik_trans
*atrans
, int aflags
,afs_int32 file
,
242 afs_int32 position
, afs_int32 length
, afs_int32 type
)
244 struct ubik_server
*ts
= NULL
;
245 afs_int32 code
= 0, rcode
, okcalls
;
246 struct rx_connection
*conn
;
249 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
252 code
= DISK_Lock(conn
, &atrans
->tid
, file
, position
, length
, type
);
253 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
255 return ContactQuorum_rcode(okcalls
, rcode
);
260 ContactQuorum_DISK_Write(struct ubik_trans
*atrans
, int aflags
,
261 afs_int32 file
, afs_int32 position
, bulkdata
*data
)
263 struct ubik_server
*ts
= NULL
;
264 afs_int32 code
= 0, rcode
, okcalls
;
265 struct rx_connection
*conn
;
268 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
271 code
= DISK_Write(conn
, &atrans
->tid
, file
, position
, data
);
272 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
274 return ContactQuorum_rcode(okcalls
, rcode
);
279 ContactQuorum_DISK_Truncate(struct ubik_trans
*atrans
, int aflags
,
280 afs_int32 file
, afs_int32 length
)
282 struct ubik_server
*ts
= NULL
;
283 afs_int32 code
= 0, rcode
, okcalls
;
284 struct rx_connection
*conn
;
287 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
290 code
= DISK_Truncate(conn
, &atrans
->tid
, file
, length
);
291 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
293 return ContactQuorum_rcode(okcalls
, rcode
);
298 ContactQuorum_DISK_WriteV(struct ubik_trans
*atrans
, int aflags
,
299 iovec_wrt
* io_vector
, iovec_buf
*io_buffer
)
301 struct ubik_server
*ts
= NULL
;
302 afs_int32 code
= 0, rcode
, okcalls
;
303 struct rx_connection
*conn
;
306 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
309 code
= DISK_WriteV(conn
, &atrans
->tid
, io_vector
, io_buffer
);
310 if ((code
<= -450) && (code
> -500)) {
311 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
312 * Un-bulk the entries and do individual DISK_Write calls
313 * instead of DISK_WriteV.
315 struct ubik_iovec
*iovec
=
316 (struct ubik_iovec
*)io_vector
->iovec_wrt_val
;
317 char *iobuf
= (char *)io_buffer
->iovec_buf_val
;
321 for (i
= 0, offset
= 0; i
< io_vector
->iovec_wrt_len
; i
++) {
322 /* Sanity check for going off end of buffer */
323 if ((offset
+ iovec
[i
].length
) > io_buffer
->iovec_buf_len
) {
327 tcbs
.bulkdata_len
= iovec
[i
].length
;
328 tcbs
.bulkdata_val
= &iobuf
[offset
];
329 code
= DISK_Write(conn
, &atrans
->tid
, iovec
[i
].file
,
330 iovec
[i
].position
, &tcbs
);
333 offset
+= iovec
[i
].length
;
337 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
339 return ContactQuorum_rcode(okcalls
, rcode
);
344 ContactQuorum_DISK_SetVersion(struct ubik_trans
*atrans
, int aflags
,
345 ubik_version
*OldVersion
,
346 ubik_version
*NewVersion
)
348 struct ubik_server
*ts
= NULL
;
349 afs_int32 code
= 0, rcode
, okcalls
;
350 struct rx_connection
*conn
;
353 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
356 code
= DISK_SetVersion(conn
, &atrans
->tid
, OldVersion
, NewVersion
);
357 done
= ContactQuorum_iterate(atrans
, aflags
, &ts
, &conn
, &rcode
, &okcalls
, code
);
359 return ContactQuorum_rcode(okcalls
, rcode
);
362 #if defined(AFS_PTHREAD_ENV)
364 ubik_thread_create(pthread_attr_t
*tattr
, pthread_t
*thread
, void *proc
) {
365 opr_Verify(pthread_attr_init(tattr
) == 0);
366 opr_Verify(pthread_attr_setdetachstate(tattr
,
367 PTHREAD_CREATE_DETACHED
) == 0);
368 opr_Verify(pthread_create(thread
, tattr
, proc
, NULL
) == 0);
374 * \brief This routine initializes the ubik system for a set of servers.
375 * \return 0 for success, or an error code on failure.
376 * \param serverList set of servers specified; nServers gives the number of entries in this array.
377 * \param pathName provides an initial prefix used for naming storage files used by this system.
378 * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.
380 * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
382 * \warning The host named by myHost should not also be listed in serverList.
384 * \see ubik_ServerInit(), ubik_ServerInitByInfo()
387 ubik_ServerInitCommon(afs_uint32 myHost
, short myPort
,
388 struct afsconf_cell
*info
, char clones
[],
389 afs_uint32 serverList
[], const char *pathName
,
390 struct ubik_dbase
**dbase
)
392 struct ubik_dbase
*tdb
;
394 #ifdef AFS_PTHREAD_ENV
395 pthread_t rxServerThread
; /* pthread variables */
396 pthread_t ubeacon_InteractThread
;
397 pthread_t urecovery_InteractThread
;
398 pthread_attr_t rxServer_tattr
;
399 pthread_attr_t ubeacon_Interact_tattr
;
400 pthread_attr_t urecovery_Interact_tattr
;
403 extern int rx_stackSize
;
407 struct rx_securityClass
*secClass
;
410 struct rx_service
*tservice
;
412 initialize_U_error_table();
414 tdb
= malloc(sizeof(struct ubik_dbase
));
415 tdb
->pathName
= strdup(pathName
);
416 tdb
->activeTrans
= (struct ubik_trans
*)0;
417 memset(&tdb
->version
, 0, sizeof(struct ubik_version
));
418 memset(&tdb
->cachedVersion
, 0, sizeof(struct ubik_version
));
419 #ifdef AFS_PTHREAD_ENV
420 opr_mutex_init(&tdb
->versionLock
);
421 opr_mutex_init(&beacon_globals
.beacon_lock
);
422 opr_mutex_init(&vote_globals
.vote_lock
);
423 opr_mutex_init(&addr_globals
.addr_lock
);
424 opr_mutex_init(&version_globals
.version_lock
);
426 Lock_Init(&tdb
->versionLock
);
428 Lock_Init(&tdb
->cache_lock
);
430 tdb
->read
= uphys_read
;
431 tdb
->write
= uphys_write
;
432 tdb
->truncate
= uphys_truncate
;
433 tdb
->open
= uphys_invalidate
; /* this function isn't used any more */
434 tdb
->sync
= uphys_sync
;
435 tdb
->stat
= uphys_stat
;
436 tdb
->getlabel
= uphys_getlabel
;
437 tdb
->setlabel
= uphys_setlabel
;
438 tdb
->getnfiles
= uphys_getnfiles
;
440 tdb
->tidCounter
= tdb
->writeTidCounter
= 0;
442 ubik_dbase
= tdb
; /* for now, only one db per server; can fix later when we have names for the other dbases */
444 #ifdef AFS_PTHREAD_ENV
445 opr_cv_init(&tdb
->version_cond
);
446 opr_cv_init(&tdb
->flags_cond
);
447 #endif /* AFS_PTHREAD_ENV */
451 /* the following call is idempotent so when/if it got called earlier,
452 * by whatever called us, it doesn't really matter -- klm */
453 code
= rx_Init(myPort
);
457 ubik_callPortal
= myPort
;
459 udisk_Init(ubik_nBuffers
);
465 code
= urecovery_Initialize(tdb
);
469 code
= ubeacon_InitServerListByInfo(myHost
, info
, clones
);
471 code
= ubeacon_InitServerList(myHost
, serverList
);
475 /* try to get an additional security object */
476 if (buildSecClassesProc
== NULL
) {
478 ubik_sc
= calloc(numClasses
, sizeof(struct rx_securityClass
*));
479 ubik_sc
[0] = rxnull_NewServerSecurityObject();
480 if (ubik_SRXSecurityProc
) {
481 code
= (*ubik_SRXSecurityProc
) (ubik_SRXSecurityRock
,
485 ubik_sc
[secIndex
] = secClass
;
489 (*buildSecClassesProc
) (securityRock
, &ubik_sc
, &numClasses
);
491 /* for backwards compat this should keep working as it does now
495 rx_NewService(0, VOTE_SERVICE_ID
, "VOTE", ubik_sc
, numClasses
,
496 VOTE_ExecuteRequest
);
497 if (tservice
== (struct rx_service
*)0) {
498 ubik_dprint("Could not create VOTE rx service!\n");
501 rx_SetMinProcs(tservice
, 2);
502 rx_SetMaxProcs(tservice
, 3);
505 rx_NewService(0, DISK_SERVICE_ID
, "DISK", ubik_sc
, numClasses
,
506 DISK_ExecuteRequest
);
507 if (tservice
== (struct rx_service
*)0) {
508 ubik_dprint("Could not create DISK rx service!\n");
511 rx_SetMinProcs(tservice
, 2);
512 rx_SetMaxProcs(tservice
, 3);
514 /* start an rx_ServerProc to handle incoming RPC's in particular the
515 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
516 * the "steplock" problem in ubik initialization. Defect 11037.
518 #ifdef AFS_PTHREAD_ENV
519 ubik_thread_create(&rxServer_tattr
, &rxServerThread
, (void *)rx_ServerProc
);
521 LWP_CreateProcess(rx_ServerProc
, rx_stackSize
, RX_PROCESS_PRIORITY
,
522 NULL
, "rx_ServerProc", &junk
);
525 /* send addrs to all other servers */
526 code
= ubeacon_updateUbikNetworkAddress(ubik_host
);
530 /* now start up async processes */
531 #ifdef AFS_PTHREAD_ENV
532 ubik_thread_create(&ubeacon_Interact_tattr
, &ubeacon_InteractThread
,
533 (void *)ubeacon_Interact
);
535 code
= LWP_CreateProcess(ubeacon_Interact
, 16384 /*8192 */ ,
536 LWP_MAX_PRIORITY
- 1, (void *)0, "beacon",
542 #ifdef AFS_PTHREAD_ENV
543 ubik_thread_create(&urecovery_Interact_tattr
, &urecovery_InteractThread
,
544 (void *)urecovery_Interact
);
545 return 0; /* is this correct? - klm */
547 code
= LWP_CreateProcess(urecovery_Interact
, 16384 /*8192 */ ,
548 LWP_MAX_PRIORITY
- 1, (void *)0, "recovery",
556 * \see ubik_ServerInitCommon()
559 ubik_ServerInitByInfo(afs_uint32 myHost
, short myPort
,
560 struct afsconf_cell
*info
, char clones
[],
561 const char *pathName
, struct ubik_dbase
**dbase
)
566 ubik_ServerInitCommon(myHost
, myPort
, info
, clones
, 0, pathName
,
572 * \see ubik_ServerInitCommon()
575 ubik_ServerInit(afs_uint32 myHost
, short myPort
, afs_uint32 serverList
[],
576 const char *pathName
, struct ubik_dbase
**dbase
)
581 ubik_ServerInitCommon(myHost
, myPort
, (struct afsconf_cell
*)0, 0,
582 serverList
, pathName
, dbase
);
587 * \brief This routine begins a read or write transaction on the transaction
588 * identified by transPtr, in the dbase named by dbase.
590 * An open mode of ubik_READTRANS identifies this as a read transaction,
591 * while a mode of ubik_WRITETRANS identifies this as a write transaction.
592 * transPtr is set to the returned transaction control block.
593 * The readAny flag is set to 0 or 1 or 2 by the wrapper functions
594 * ubik_BeginTrans() or ubik_BeginTransReadAny() or
595 * ubik_BeginTransReadAnyWrite() below.
597 * \note We can only begin transaction when we have an up-to-date database.
600 BeginTrans(struct ubik_dbase
*dbase
, afs_int32 transMode
,
601 struct ubik_trans
**transPtr
, int readAny
)
603 struct ubik_trans
*jt
;
604 struct ubik_trans
*tt
;
607 if (readAny
> 1 && ubik_SyncWriterCacheProc
== NULL
) {
608 /* it's not safe to use ubik_BeginTransReadAnyWrite without a
609 * cache-syncing function; fall back to ubik_BeginTransReadAny,
610 * which is safe but slower */
611 ubik_print("ubik_BeginTransReadAnyWrite called, but "
612 "ubik_SyncWriterCacheProc not set; pretending "
613 "ubik_BeginTransReadAny was called instead\n");
617 if ((transMode
!= UBIK_READTRANS
) && readAny
)
620 if (urecovery_AllBetter(dbase
, readAny
) == 0) {
624 /* otherwise we have a quorum, use it */
626 /* make sure that at most one write transaction occurs at any one time. This
627 * has nothing to do with transaction locking; that's enforced by the lock package. However,
628 * we can't even handle two non-conflicting writes, since our log and recovery modules
629 * don't know how to restore one without possibly picking up some data from the other. */
630 if (transMode
== UBIK_WRITETRANS
) {
631 /* if we're writing already, wait */
632 while (dbase
->flags
& DBWRITING
) {
633 #ifdef AFS_PTHREAD_ENV
634 opr_cv_wait(&dbase
->flags_cond
, &dbase
->versionLock
);
637 LWP_WaitProcess(&dbase
->flags
);
642 if (!ubeacon_AmSyncSite()) {
646 if (!ubeacon_SyncSiteAdvertised()) {
647 /* i am the sync-site but the remotes are not aware yet */
653 /* create the transaction */
654 code
= udisk_begin(dbase
, transMode
, &jt
); /* can't take address of register var */
655 tt
= jt
; /* move to a register */
656 if (code
|| tt
== NULL
) {
662 tt
->flags
|= TRREADANY
;
664 tt
->flags
|= TRREADWRITE
;
667 /* label trans and dbase with new tid */
668 tt
->tid
.epoch
= version_globals
.ubik_epochTime
;
669 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
670 tt
->tid
.counter
= (dbase
->tidCounter
+= 2);
672 if (transMode
== UBIK_WRITETRANS
) {
673 /* for a write trans, we have to keep track of the write tid counter too */
674 dbase
->writeTidCounter
= tt
->tid
.counter
;
679 if (transMode
== UBIK_WRITETRANS
) {
680 /* next try to start transaction on appropriate number of machines */
681 code
= ContactQuorum_NoArguments(DISK_Begin
, tt
, 0);
683 /* we must abort the operation */
685 ContactQuorum_NoArguments(DISK_Abort
, tt
, 0); /* force aborts to the others */
701 ubik_BeginTrans(struct ubik_dbase
*dbase
, afs_int32 transMode
,
702 struct ubik_trans
**transPtr
)
704 return BeginTrans(dbase
, transMode
, transPtr
, 0);
711 ubik_BeginTransReadAny(struct ubik_dbase
*dbase
, afs_int32 transMode
,
712 struct ubik_trans
**transPtr
)
714 return BeginTrans(dbase
, transMode
, transPtr
, 1);
721 ubik_BeginTransReadAnyWrite(struct ubik_dbase
*dbase
, afs_int32 transMode
,
722 struct ubik_trans
**transPtr
)
724 return BeginTrans(dbase
, transMode
, transPtr
, 2);
728 * \brief This routine ends a read or write transaction by aborting it.
731 ubik_AbortTrans(struct ubik_trans
*transPtr
)
735 struct ubik_dbase
*dbase
;
737 dbase
= transPtr
->dbase
;
739 if (transPtr
->flags
& TRCACHELOCKED
) {
740 ReleaseReadLock(&dbase
->cache_lock
);
741 transPtr
->flags
&= ~TRCACHELOCKED
;
744 ObtainWriteLock(&dbase
->cache_lock
);
747 memset(&dbase
->cachedVersion
, 0, sizeof(struct ubik_version
));
749 ReleaseWriteLock(&dbase
->cache_lock
);
751 /* see if we're still up-to-date */
752 if (!urecovery_AllBetter(dbase
, transPtr
->flags
& TRREADANY
)) {
753 udisk_abort(transPtr
);
759 if (transPtr
->type
== UBIK_READTRANS
) {
760 code
= udisk_abort(transPtr
);
766 /* below here, we know we're doing a write transaction */
767 if (!ubeacon_AmSyncSite()) {
768 udisk_abort(transPtr
);
774 /* now it is safe to try remote abort */
775 code
= ContactQuorum_NoArguments(DISK_Abort
, transPtr
, 0);
776 code2
= udisk_abort(transPtr
);
779 return (code
? code
: code2
);
783 WritebackApplicationCache(struct ubik_dbase
*dbase
)
786 if (ubik_SyncWriterCacheProc
) {
787 code
= ubik_SyncWriterCacheProc();
790 /* we failed to sync the local cache, so just invalidate the cache;
791 * we'll try to read the cache in again on the next read */
792 memset(&dbase
->cachedVersion
, 0, sizeof(dbase
->cachedVersion
));
794 memcpy(&dbase
->cachedVersion
, &dbase
->version
,
795 sizeof(dbase
->cachedVersion
));
800 * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
801 * \return an error code.
804 ubik_EndTrans(struct ubik_trans
*transPtr
)
809 struct ubik_server
*ts
;
812 struct ubik_dbase
*dbase
;
814 if (transPtr
->type
== UBIK_WRITETRANS
) {
815 code
= ubik_Flush(transPtr
);
817 ubik_AbortTrans(transPtr
);
822 dbase
= transPtr
->dbase
;
824 if (transPtr
->flags
& TRCACHELOCKED
) {
825 ReleaseReadLock(&dbase
->cache_lock
);
826 transPtr
->flags
&= ~TRCACHELOCKED
;
829 if (transPtr
->type
!= UBIK_READTRANS
) {
830 /* must hold cache_lock before DBHOLD'ing */
831 ObtainWriteLock(&dbase
->cache_lock
);
837 /* give up if no longer current */
838 if (!urecovery_AllBetter(dbase
, transPtr
->flags
& TRREADANY
)) {
839 udisk_abort(transPtr
);
846 if (transPtr
->type
== UBIK_READTRANS
) { /* reads are easy */
847 code
= udisk_commit(transPtr
);
849 goto success
; /* update cachedVersion correctly */
855 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
856 udisk_abort(transPtr
);
863 /* now it is safe to do commit */
864 code
= udisk_commit(transPtr
);
866 /* db data has been committed locally; update the local cache so
867 * readers can get at it */
868 WritebackApplicationCache(dbase
);
870 ReleaseWriteLock(&dbase
->cache_lock
);
872 code
= ContactQuorum_NoArguments(DISK_Commit
, transPtr
, CStampVersion
);
875 memset(&dbase
->cachedVersion
, 0, sizeof(struct ubik_version
));
876 ReleaseWriteLock(&dbase
->cache_lock
);
880 /* failed to commit, so must return failure. Try to clear locks first, just for fun
881 * Note that we don't know if this transaction will eventually commit at this point.
882 * If it made it to a site that will be present in the next quorum, we win, otherwise
883 * we lose. If we contact a majority of sites, then we won't be here: contacting
884 * a majority guarantees commit, since it guarantees that one dude will be a
885 * member of the next quorum. */
886 ContactQuorum_NoArguments(DISK_ReleaseLocks
, transPtr
, 0);
891 /* before we can start sending unlock messages, we must wait until all servers
892 * that are possibly still functioning on the other side of a network partition
893 * have timed out. Check the server structures, compute how long to wait, then
894 * start the unlocks */
895 realStart
= FT_ApproxTime();
897 /* wait for all servers to time out */
899 now
= FT_ApproxTime();
900 /* check if we're still sync site, the guy should either come up
901 * to us, or timeout. Put safety check in anyway */
902 if (now
- realStart
> 10 * BIGTIME
) {
903 ubik_stats
.escapes
++;
904 ubik_print("ubik escaping from commit wait\n");
907 for (ts
= ubik_servers
; ts
; ts
= ts
->next
) {
909 if (!ts
->beaconSinceDown
&& now
<= ts
->lastBeaconSent
+ BIGTIME
) {
912 /* this guy could have some damaged data, wait for him */
914 tv
.tv_sec
= 1; /* try again after a while (ha ha) */
917 #ifdef AFS_PTHREAD_ENV
918 /* we could release the dbase outside of the loop, but we do
919 * it here, in the loop, to avoid an unnecessary RELE/HOLD
920 * if all sites are up */
922 select(0, 0, 0, 0, &tv
);
925 IOMGR_Select(0, 0, 0, 0, &tv
); /* poll, should we wait on something? */
933 break; /* no down ones still pseudo-active */
936 /* finally, unlock all the dudes. We can return success independent of the number of servers
937 * that really unlock the dbase; the others will do it if/when they elect a new sync site.
938 * The transaction is committed anyway, since we succeeded in contacting a quorum
939 * at the start (when invoking the DiskCommit function).
941 ContactQuorum_NoArguments(DISK_ReleaseLocks
, transPtr
, 0);
945 /* don't update cachedVersion here; it should have been updated way back
946 * in ubik_CheckCache, and earlier in this function for writes */
949 ReleaseWriteLock(&dbase
->cache_lock
);
955 ObtainWriteLock(&dbase
->cache_lock
);
957 memset(&dbase
->cachedVersion
, 0, sizeof(struct ubik_version
));
958 ReleaseWriteLock(&dbase
->cache_lock
);
963 * \brief This routine reads length bytes into buffer from the current position in the database.
965 * 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.
967 * \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.
970 ubik_Read(struct ubik_trans
*transPtr
, void *buffer
,
975 /* reads are easy to do: handle locally */
976 DBHOLD(transPtr
->dbase
);
977 if (!urecovery_AllBetter(transPtr
->dbase
, transPtr
->flags
& TRREADANY
)) {
978 DBRELE(transPtr
->dbase
);
983 udisk_read(transPtr
, transPtr
->seekFile
, buffer
, transPtr
->seekPos
,
986 transPtr
->seekPos
+= length
;
988 DBRELE(transPtr
->dbase
);
993 * \brief This routine will flush the io data in the iovec structures.
995 * It first flushes to the local disk and then uses ContactQuorum to write it
996 * to the other servers.
999 ubik_Flush(struct ubik_trans
*transPtr
)
1001 afs_int32 code
, error
= 0;
1003 if (transPtr
->type
!= UBIK_WRITETRANS
)
1006 DBHOLD(transPtr
->dbase
);
1007 if (!transPtr
->iovec_info
.iovec_wrt_len
1008 || !transPtr
->iovec_info
.iovec_wrt_val
) {
1009 DBRELE(transPtr
->dbase
);
1013 if (!urecovery_AllBetter(transPtr
->dbase
, transPtr
->flags
& TRREADANY
))
1014 ERROR_EXIT(UNOQUORUM
);
1015 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1016 ERROR_EXIT(UNOTSYNC
);
1018 /* Update the rest of the servers in the quorum */
1020 ContactQuorum_DISK_WriteV(transPtr
, 0, &transPtr
->iovec_info
,
1021 &transPtr
->iovec_data
);
1023 udisk_abort(transPtr
);
1024 ContactQuorum_NoArguments(DISK_Abort
, transPtr
, 0); /* force aborts to the others */
1025 transPtr
->iovec_info
.iovec_wrt_len
= 0;
1026 transPtr
->iovec_data
.iovec_buf_len
= 0;
1030 /* Wrote the buffers out, so start at scratch again */
1031 transPtr
->iovec_info
.iovec_wrt_len
= 0;
1032 transPtr
->iovec_data
.iovec_buf_len
= 0;
1035 DBRELE(transPtr
->dbase
);
1040 ubik_Write(struct ubik_trans
*transPtr
, void *vbuffer
,
1043 struct ubik_iovec
*iovec
;
1044 afs_int32 code
, error
= 0;
1045 afs_int32 pos
, len
, size
;
1046 char * buffer
= (char *)vbuffer
;
1048 if (transPtr
->type
!= UBIK_WRITETRANS
)
1053 if (length
> IOVEC_MAXBUF
) {
1054 for (pos
= 0, len
= length
; len
> 0; len
-= size
, pos
+= size
) {
1055 size
= ((len
< IOVEC_MAXBUF
) ? len
: IOVEC_MAXBUF
);
1056 code
= ubik_Write(transPtr
, buffer
+pos
, size
);
1063 DBHOLD(transPtr
->dbase
);
1064 if (!transPtr
->iovec_info
.iovec_wrt_val
) {
1065 transPtr
->iovec_info
.iovec_wrt_len
= 0;
1066 transPtr
->iovec_info
.iovec_wrt_val
=
1067 malloc(IOVEC_MAXWRT
* sizeof(struct ubik_iovec
));
1068 transPtr
->iovec_data
.iovec_buf_len
= 0;
1069 transPtr
->iovec_data
.iovec_buf_val
= malloc(IOVEC_MAXBUF
);
1070 if (!transPtr
->iovec_info
.iovec_wrt_val
1071 || !transPtr
->iovec_data
.iovec_buf_val
) {
1072 if (transPtr
->iovec_info
.iovec_wrt_val
)
1073 free(transPtr
->iovec_info
.iovec_wrt_val
);
1074 transPtr
->iovec_info
.iovec_wrt_val
= 0;
1075 if (transPtr
->iovec_data
.iovec_buf_val
)
1076 free(transPtr
->iovec_data
.iovec_buf_val
);
1077 transPtr
->iovec_data
.iovec_buf_val
= 0;
1078 DBRELE(transPtr
->dbase
);
1083 /* If this write won't fit in the structure, then flush it out and start anew */
1084 if ((transPtr
->iovec_info
.iovec_wrt_len
>= IOVEC_MAXWRT
)
1085 || ((length
+ transPtr
->iovec_data
.iovec_buf_len
) > IOVEC_MAXBUF
)) {
1086 /* Can't hold the DB lock over ubik_Flush */
1087 DBRELE(transPtr
->dbase
);
1088 code
= ubik_Flush(transPtr
);
1091 DBHOLD(transPtr
->dbase
);
1094 if (!urecovery_AllBetter(transPtr
->dbase
, transPtr
->flags
& TRREADANY
))
1095 ERROR_EXIT(UNOQUORUM
);
1096 if (!ubeacon_AmSyncSite()) /* only sync site can write */
1097 ERROR_EXIT(UNOTSYNC
);
1099 /* Write to the local disk */
1101 udisk_write(transPtr
, transPtr
->seekFile
, buffer
, transPtr
->seekPos
,
1104 udisk_abort(transPtr
);
1105 transPtr
->iovec_info
.iovec_wrt_len
= 0;
1106 transPtr
->iovec_data
.iovec_buf_len
= 0;
1107 DBRELE(transPtr
->dbase
);
1111 /* Collect writes for the other ubik servers (to be done in bulk) */
1112 iovec
= (struct ubik_iovec
*)transPtr
->iovec_info
.iovec_wrt_val
;
1113 iovec
[transPtr
->iovec_info
.iovec_wrt_len
].file
= transPtr
->seekFile
;
1114 iovec
[transPtr
->iovec_info
.iovec_wrt_len
].position
= transPtr
->seekPos
;
1115 iovec
[transPtr
->iovec_info
.iovec_wrt_len
].length
= length
;
1117 memcpy(&transPtr
->iovec_data
.
1118 iovec_buf_val
[transPtr
->iovec_data
.iovec_buf_len
], buffer
, length
);
1120 transPtr
->iovec_info
.iovec_wrt_len
++;
1121 transPtr
->iovec_data
.iovec_buf_len
+= length
;
1122 transPtr
->seekPos
+= length
;
1125 DBRELE(transPtr
->dbase
);
1130 * \brief This sets the file pointer associated with the current transaction
1131 * to the appropriate file and byte position.
1133 * Unlike Unix files, a transaction is labelled by both a file number \p fileid
1134 * and a byte position relative to the specified file \p position.
1137 ubik_Seek(struct ubik_trans
*transPtr
, afs_int32 fileid
,
1142 DBHOLD(transPtr
->dbase
);
1143 if (!urecovery_AllBetter(transPtr
->dbase
, transPtr
->flags
& TRREADANY
)) {
1146 transPtr
->seekFile
= fileid
;
1147 transPtr
->seekPos
= position
;
1150 DBRELE(transPtr
->dbase
);
1155 * \brief This call returns the file pointer associated with the specified
1156 * transaction in \p fileid and \p position.
1159 ubik_Tell(struct ubik_trans
*transPtr
, afs_int32
* fileid
,
1160 afs_int32
* position
)
1162 DBHOLD(transPtr
->dbase
);
1163 *fileid
= transPtr
->seekFile
;
1164 *position
= transPtr
->seekPos
;
1165 DBRELE(transPtr
->dbase
);
1170 * \brief This sets the file size for the currently-selected file to \p length
1171 * bytes, if length is less than the file's current size.
1174 ubik_Truncate(struct ubik_trans
*transPtr
, afs_int32 length
)
1176 afs_int32 code
, error
= 0;
1178 /* Will also catch if not UBIK_WRITETRANS */
1179 code
= ubik_Flush(transPtr
);
1183 DBHOLD(transPtr
->dbase
);
1184 /* first, check that quorum is still good, and that dbase is up-to-date */
1185 if (!urecovery_AllBetter(transPtr
->dbase
, transPtr
->flags
& TRREADANY
))
1186 ERROR_EXIT(UNOQUORUM
);
1187 if (!ubeacon_AmSyncSite())
1188 ERROR_EXIT(UNOTSYNC
);
1190 /* now do the operation locally, and propagate it out */
1191 code
= udisk_truncate(transPtr
, transPtr
->seekFile
, length
);
1194 ContactQuorum_DISK_Truncate(transPtr
, 0, transPtr
->seekFile
,
1198 /* we must abort the operation */
1199 udisk_abort(transPtr
);
1200 ContactQuorum_NoArguments(DISK_Abort
, transPtr
, 0); /* force aborts to the others */
1205 DBRELE(transPtr
->dbase
);
1210 * \brief set a lock; all locks are released on transaction end (commit/abort)
1213 ubik_SetLock(struct ubik_trans
*atrans
, afs_int32 apos
, afs_int32 alen
,
1216 afs_int32 code
= 0, error
= 0;
1218 if (atype
== LOCKWRITE
) {
1219 if (atrans
->type
== UBIK_READTRANS
)
1221 code
= ubik_Flush(atrans
);
1226 DBHOLD(atrans
->dbase
);
1227 if (atype
== LOCKREAD
) {
1228 code
= ulock_getLock(atrans
, atype
, 1);
1232 /* first, check that quorum is still good, and that dbase is up-to-date */
1233 if (!urecovery_AllBetter(atrans
->dbase
, atrans
->flags
& TRREADANY
))
1234 ERROR_EXIT(UNOQUORUM
);
1235 if (!ubeacon_AmSyncSite())
1236 ERROR_EXIT(UNOTSYNC
);
1238 /* now do the operation locally, and propagate it out */
1239 code
= ulock_getLock(atrans
, atype
, 1);
1241 code
= ContactQuorum_DISK_Lock(atrans
, 0, 0, 1 /*unused */ ,
1242 1 /*unused */ , LOCKWRITE
);
1245 /* we must abort the operation */
1246 udisk_abort(atrans
);
1247 ContactQuorum_NoArguments(DISK_Abort
, atrans
, 0); /* force aborts to the others */
1253 DBRELE(atrans
->dbase
);
1258 * \brief utility to wait for a version # to change
1261 ubik_WaitVersion(struct ubik_dbase
*adatabase
,
1262 struct ubik_version
*aversion
)
1266 /* wait until version # changes, and then return */
1267 if (vcmp(*aversion
, adatabase
->version
) != 0) {
1271 #ifdef AFS_PTHREAD_ENV
1272 opr_cv_wait(&adatabase
->version_cond
, &adatabase
->versionLock
);
1275 LWP_WaitProcess(&adatabase
->version
); /* same vers, just wait */
1282 * \brief utility to get the version of the dbase a transaction is dealing with
1285 ubik_GetVersion(struct ubik_trans
*atrans
,
1286 struct ubik_version
*avers
)
1288 DBHOLD(atrans
->dbase
);
1289 *avers
= atrans
->dbase
->version
;
1290 DBRELE(atrans
->dbase
);
1295 * \brief Facility to simplify database caching.
1296 * \return zero if last trans was done on the local server and was successful.
1297 * \return -1 means bad (NULL) argument.
1299 * If return value is non-zero and the caller is a server caching part of the
1300 * Ubik database, it should invalidate that cache.
1303 ubik_CacheUpdate(struct ubik_trans
*atrans
)
1305 if (!(atrans
&& atrans
->dbase
))
1307 return vcmp(atrans
->dbase
->cachedVersion
, atrans
->dbase
->version
) != 0;
1311 * check and possibly update cache of ubik db.
1313 * If the version of the cached db data is out of date, this calls (*check) to
1314 * update the cache. If (*check) returns success, we update the version of the
1317 * Checking the version of the cached db data is done under a read lock;
1318 * updating the cache (and thus calling (*check)) is done under a write lock
1319 * so is guaranteed not to interfere with another thread's (*check). On
1320 * successful return, a read lock on the cached db data is obtained, which
1321 * will be released by ubik_EndTrans or ubik_AbortTrans.
1323 * @param[in] atrans ubik transaction
1324 * @param[in] check function to call to check/update cache
1325 * @param[in] rock rock to pass to *check
1327 * @return operation status
1329 * @retval nonzero error; cachedVersion not updated
1331 * @post On success, application cache is read-locked, and cache data is
1335 ubik_CheckCache(struct ubik_trans
*atrans
, ubik_updatecache_func cbf
, void *rock
)
1339 if (!(atrans
&& atrans
->dbase
))
1342 ObtainReadLock(&atrans
->dbase
->cache_lock
);
1344 while (ubik_CacheUpdate(atrans
) != 0) {
1346 ReleaseReadLock(&atrans
->dbase
->cache_lock
);
1347 ObtainSharedLock(&atrans
->dbase
->cache_lock
);
1349 if (ubik_CacheUpdate(atrans
) != 0) {
1351 BoostSharedLock(&atrans
->dbase
->cache_lock
);
1353 ret
= (*cbf
) (atrans
, rock
);
1355 memcpy(&atrans
->dbase
->cachedVersion
, &atrans
->dbase
->version
,
1356 sizeof(atrans
->dbase
->cachedVersion
));
1360 /* It would be nice if we could convert from a shared lock to a read
1361 * lock... instead, just release the shared and acquire the read */
1362 ReleaseSharedLock(&atrans
->dbase
->cache_lock
);
1365 /* if we have an error, don't retry, and don't hold any locks */
1369 ObtainReadLock(&atrans
->dbase
->cache_lock
);
1372 atrans
->flags
|= TRCACHELOCKED
;
1378 * "Who said anything about panicking?" snapped Arthur.
1379 * "This is still just the culture shock. You wait till I've settled down
1380 * into the situation and found my bearings. \em Then I'll start panicking!"
1383 * \returns There is no return from panic.
1386 panic(char *format
, ...)
1390 va_start(ap
, format
);
1391 ubik_print("Ubik PANIC:\n");
1392 ubik_vprint(format
, ap
);
1396 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
1397 exit(1); /* never know, though */
1401 * This function takes an IP addresses as its parameter. It returns the
1402 * the primary IP address that is on the host passed in, or 0 if not found.
1405 ubikGetPrimaryInterfaceAddr(afs_uint32 addr
)
1407 struct ubik_server
*ts
;
1411 for (ts
= ubik_servers
; ts
; ts
= ts
->next
)
1412 for (j
= 0; j
< UBIK_MAX_INTERFACE_ADDR
; j
++)
1413 if (ts
->addr
[j
] == addr
) {
1415 return ts
->addr
[0]; /* net byte order */
1418 return 0; /* if not in server database, return error */
1422 ubik_CheckAuth(struct rx_call
*acall
)
1424 if (checkSecurityProc
)
1425 return (*checkSecurityProc
) (securityRock
, acall
);
1426 else if (ubik_CheckRXSecurityProc
) {
1427 return (*ubik_CheckRXSecurityProc
) (ubik_CheckRXSecurityRock
, acall
);
1433 ubik_SetServerSecurityProcs(void (*buildproc
) (void *,
1434 struct rx_securityClass
***,
1436 int (*checkproc
) (void *, struct rx_call
*),
1439 buildSecClassesProc
= buildproc
;
1440 checkSecurityProc
= checkproc
;
1441 securityRock
= rock
;