2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
15 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <sys/param.h>
34 #include <afs/cellconfig.h>
36 #define UBIK_INTERNALS
40 #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 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.
54 BEACONER--The module responsible for sending keep-alives out when a
55 server is actually the sync site, or trying to become a sync site.
57 DISK--The module responsible for representing atomic transactions
58 on the local disk. It maintains a new-value only log.
60 LOCK--The module responsible for locking byte ranges in the database file.
62 At the next level (2) we have
64 RECOVERY--The module responsible for ensuring that all members of a quorum
65 have the same up-to-date database after a new synchronization site is
66 elected. This module runs only on the synchronization site.
68 At the next level (3) we have
70 REMOTE--The module responsible for interpreting requests from the sync
71 site and applying them to the database, after obtaining the appropriate
74 At the next level (4) we have
76 UBIK--The module users call to perform operations on the database.
81 afs_int32 ubik_quorum=0;
82 struct ubik_dbase *ubik_dbase=0;
83 struct ubik_stats ubik_stats;
84 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
85 afs_int32 ubik_epochTime = 0;
86 afs_int32 urecovery_state = 0;
87 int (*ubik_SRXSecurityProc)();
88 char *ubik_SRXSecurityRock;
89 struct ubik_server *ubik_servers;
90 short ubik_callPortal;
92 static int BeginTrans();
94 struct rx_securityClass *ubik_sc[3];
96 /* perform an operation at a quorum, handling error conditions. return 0 if
97 all worked, otherwise mark failing server as down and return UERROR
99 Note that if any server misses an update, we must wait BIGTIME seconds before
100 allowing the transaction to commit, to ensure that the missing and possibly still
101 functioning server times out and stop handing out old data. This is done in the commit
102 code, where we wait for a server marked down to have stayed down for BIGTIME seconds
103 before we allow a transaction to commit. A server that fails but comes back up won't give
104 out old data because it is sent the sync count along with the beacon message that
105 marks it as *really* up (beaconSinceDown).
107 #define CStampVersion 1 /* meaning set ts->version */
108 afs_int32 ContactQuorum(aproc, atrans, aflags, aparm0, aparm1, aparm2, aparm3, aparm4, aparm5)
111 register struct ubik_trans *atrans;
112 long aparm0, aparm1, aparm2, aparm3, aparm4, aparm5; {
113 register struct ubik_server *ts;
114 register afs_int32 code;
115 afs_int32 rcode, okcalls;
119 for(ts = ubik_servers; ts; ts=ts->next) {
120 /* for each server */
121 if (!ts->up || !ts->currentDB) {
122 ts->currentDB = 0; /* db is no longer current; we just missed an update */
123 continue; /* not up-to-date, don't bother */
125 code = (*aproc) (ts->disk_rxcid, &atrans->tid, aparm0, aparm1, aparm2, aparm3, aparm4, aparm5);
126 if ( (aproc == DISK_WriteV) && (code <= -450) && (code > -500) ) {
127 /* An RPC interface mismatch (as defined in comerr/error_msg.c).
128 * Un-bulk the entries and do individual DISK_Write calls
129 * instead of DISK_WriteV.
131 iovec_wrt *iovec_infoP = (iovec_wrt *)aparm0;
132 iovec_buf *iovec_dataP = (iovec_buf *)aparm1;
133 struct ubik_iovec *iovec = (struct ubik_iovec *)iovec_infoP->iovec_wrt_val;
134 char *iobuf = (char *)iovec_dataP->iovec_buf_val;
138 for (i=0, offset=0; i<iovec_infoP->iovec_wrt_len; i++) {
139 /* Sanity check for going off end of buffer */
140 if ((offset + iovec[i].length) > iovec_dataP->iovec_buf_len) {
144 tcbs.bulkdata_len = iovec[i].length;
145 tcbs.bulkdata_val = &iobuf[offset];
146 code = DISK_Write(ts->disk_rxcid, &atrans->tid,
147 iovec[i].file, iovec[i].position, &tcbs);
150 offset += iovec[i].length;
153 if (code) { /* failure */
155 ts->up = 0; /* mark as down now; beacons will no longer be sent */
157 ts->beaconSinceDown = 0;
158 urecovery_LostServer(); /* tell recovery to try to resend dbase later */
159 } else { /* success */
161 okcalls++; /* count up how many worked */
162 if (aflags & CStampVersion) {
163 ts->version = atrans->dbase->version;
167 /* return 0 if we successfully contacted a quorum, otherwise return error code. We don't have to contact ourselves (that was done locally) */
168 if (okcalls+1 >= ubik_quorum) return 0;
172 /* This routine initializes the ubik system for a set of servers. It returns 0 for success, or an error code on failure. The set of servers is specified by serverList; nServers gives the number of entries in this array. Finally, dbase is the returned structure representing this instance of a ubik; it is passed to various calls below. The variable pathName provides an initial prefix used for naming storage files used by this system. It should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
174 Note that the host named by myHost should not also be listed in serverList.
177 int ubik_ServerInitByInfo(myHost, myPort, info, clones, pathName, dbase)
178 struct afsconf_cell *info; /* in */
182 char *pathName; /* in */
183 struct ubik_dbase **dbase; /* out */
187 code = ubik_ServerInitCommon(myHost, myPort, info, clones, 0, pathName, dbase);
191 int ubik_ServerInit(myHost, myPort, serverList, pathName, dbase)
192 afs_int32 serverList[]; /* in */
195 char *pathName; /* in */
196 struct ubik_dbase **dbase; /* out */
200 code = ubik_ServerInitCommon(myHost, myPort, (struct afsconf_cell *)0, 0,
201 serverList, pathName, dbase);
205 int ubik_ServerInitCommon(myHost, myPort, info, clones, serverList, pathName, dbase)
208 struct afsconf_cell *info; /* in */
210 afs_int32 serverList[]; /* in */
211 char *pathName; /* in */
212 struct ubik_dbase **dbase; /* out */
214 register struct ubik_dbase *tdb;
215 register afs_int32 code;
218 struct rx_securityClass *secClass;
220 struct rx_service *tservice;
221 extern struct rx_securityClass *rxnull_NewServerSecurityObject();
222 extern int VOTE_ExecuteRequest(), DISK_ExecuteRequest();
223 extern void rx_ServerProc();
224 extern int rx_stackSize;
226 initialize_U_error_table();
228 tdb = (struct ubik_dbase *) malloc(sizeof(struct ubik_dbase));
229 tdb->pathName = (char *) malloc(strlen(pathName)+1);
230 strcpy(tdb->pathName, pathName);
231 tdb->activeTrans = (struct ubik_trans *) 0;
232 memset(&tdb->version, 0, sizeof(struct ubik_version));
233 memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
234 Lock_Init(&tdb->versionLock);
236 tdb->read = uphys_read;
237 tdb->write = uphys_write;
238 tdb->truncate = uphys_truncate;
239 tdb->open = 0; /* this function isn't used any more */
240 tdb->sync = uphys_sync;
241 tdb->stat = uphys_stat;
242 tdb->getlabel = uphys_getlabel;
243 tdb->setlabel = uphys_setlabel;
244 tdb->getnfiles = uphys_getnfiles;
246 tdb->tidCounter=tdb->writeTidCounter=0;
248 ubik_dbase = tdb; /* for now, only one db per server; can fix later when we have names for the other dbases */
251 ubik_callPortal = myPort;
252 /* try to get an additional security object */
253 ubik_sc[0] = rxnull_NewServerSecurityObject();
256 if (ubik_SRXSecurityProc) {
257 code = (*ubik_SRXSecurityProc)(ubik_SRXSecurityRock, &secClass, &secIndex);
259 ubik_sc[secIndex] = secClass;
262 code = rx_Init(myPort);
263 if (code < 0) return code;
264 tservice = rx_NewService(0, VOTE_SERVICE_ID, "VOTE", ubik_sc, 3, VOTE_ExecuteRequest);
265 if (tservice == (struct rx_service *)0) {
266 ubik_dprint("Could not create VOTE rx service!\n");
269 rx_SetMinProcs(tservice, 2);
270 rx_SetMaxProcs(tservice, 3);
272 tservice = rx_NewService(0, DISK_SERVICE_ID, "DISK", ubik_sc, 3, DISK_ExecuteRequest);
273 if (tservice == (struct rx_service *)0) {
274 ubik_dprint("Could not create DISK rx service!\n");
277 rx_SetMinProcs(tservice, 2);
278 rx_SetMaxProcs(tservice, 3);
280 /* start an rx_ServerProc to handle incoming RPC's in particular the
281 * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
282 * the "steplock" problem in ubik initialization. Defect 11037.
284 LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
285 0, "rx_ServerProc", &junk);
287 /* do basic initialization */
289 if (code) return code;
290 code = urecovery_Initialize(tdb);
291 if (code) return code;
293 code = ubeacon_InitServerListByInfo(myHost, info, clones);
295 code = ubeacon_InitServerList(myHost, serverList);
296 if (code) return code;
298 /* now start up async processes */
299 code = LWP_CreateProcess(ubeacon_Interact, 16384/*8192*/, LWP_MAX_PRIORITY-1,
301 if (code) return code;
302 code = LWP_CreateProcess(urecovery_Interact, 16384/*8192*/, LWP_MAX_PRIORITY-1,
303 0, "recovery", &junk);
307 /* This routine begins a read or write transaction on the transaction
308 identified by transPtr, in the dbase named by dbase. An open mode of
309 ubik_READTRANS identifies this as a read transaction, while a mode of
310 ubik_WRITETRANS identifies this as a write transaction. transPtr
311 is set to the returned transaction control block. The readAny flag is
312 set to 0 or 1 by the wrapper functions ubik_BeginTrans() or
313 ubik_BeginTransReadAny() below.
315 We can only begin transaction when we have an up-to-date database.
318 static int BeginTrans(dbase, transMode, transPtr, readAny)
319 register struct ubik_dbase *dbase; /* in */
321 afs_int32 transMode; /* in */
322 struct ubik_trans **transPtr; /* out */ {
323 struct ubik_trans *jt;
324 register struct ubik_trans *tt;
325 register afs_int32 code;
326 #if defined(UBIK_PAUSE)
328 #endif /* UBIK_PAUSE */
330 if ((transMode != UBIK_READTRANS) && readAny) return UBADTYPE;
332 #if defined(UBIK_PAUSE)
333 /* if we're polling the slave sites, wait until the returns
334 * are all in. Otherwise, the urecovery_CheckTid call may
337 if (transMode == UBIK_WRITETRANS)
338 for (count = 75; dbase->flags & DBVOTING; --count) {
340 #ifdef GRAND_PAUSE_DEBUGGING
342 fprintf (stderr,"%ld: myport=%d: BeginTrans is waiting 'cause of voting conflict\n", time(0), ntohs(ubik_callPortal));
347 fprintf (stderr,"%ld: myport=%d: BeginTrans failed because of voting conflict\n", time(0), ntohs(ubik_callPortal));
349 return UNOQUORUM; /* a white lie */
354 #endif /* UBIK_PAUSE */
355 if (urecovery_AllBetter(dbase, readAny)==0) {
359 /* otherwise we have a quorum, use it */
361 /* make sure that at most one write transaction occurs at any one time. This
362 has nothing to do with transaction locking; that's enforced by the lock package. However,
363 we can't even handle two non-conflicting writes, since our log and recovery modules
364 don't know how to restore one without possibly picking up some data from the other. */
365 if (transMode == UBIK_WRITETRANS) {
366 /* if we're writing already, wait */
367 while(dbase->flags & DBWRITING) {
369 LWP_WaitProcess(&dbase->flags);
372 if (!ubeacon_AmSyncSite()) {
378 /* create the transaction */
379 code = udisk_begin(dbase, transMode, &jt); /* can't take address of register var */
380 tt = jt; /* move to a register */
381 if (code || tt == (struct ubik_trans *)NULL) {
385 if (readAny) tt->flags |= TRREADANY;
386 /* label trans and dbase with new tid */
387 tt->tid.epoch = ubik_epochTime;
388 /* bump by two, since tidCounter+1 means trans id'd by tidCounter has finished */
389 tt->tid.counter = (dbase->tidCounter += 2);
391 if (transMode == UBIK_WRITETRANS) {
392 /* for a write trans, we have to keep track of the write tid counter too */
393 #if defined(UBIK_PAUSE)
394 dbase->writeTidCounter = tt->tid.counter;
396 dbase->writeTidCounter += 2;
397 #endif /* UBIK_PAUSE */
399 /* next try to start transaction on appropriate number of machines */
400 code = ContactQuorum(DISK_Begin, tt, 0);
402 /* we must abort the operation */
404 ContactQuorum(DISK_Abort, tt, 0); /* force aborts to the others */
416 int ubik_BeginTrans(dbase, transMode, transPtr)
417 register struct ubik_dbase *dbase; /* in */
418 afs_int32 transMode; /* in */
419 struct ubik_trans **transPtr; /* out */ {
420 return BeginTrans(dbase, transMode, transPtr, 0);
423 int ubik_BeginTransReadAny(dbase, transMode, transPtr)
424 register struct ubik_dbase *dbase; /* in */
425 afs_int32 transMode; /* in */
426 struct ubik_trans **transPtr; /* out */ {
427 return BeginTrans(dbase, transMode, transPtr, 1);
430 /* this routine ends a read or write transaction by aborting it */
431 int ubik_AbortTrans(transPtr)
432 register struct ubik_trans *transPtr; /* in */ {
433 register afs_int32 code;
435 register struct ubik_dbase *dbase;
437 dbase = transPtr->dbase;
439 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
440 /* see if we're still up-to-date */
441 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
442 udisk_abort(transPtr);
448 if (transPtr->type == UBIK_READTRANS) {
449 code = udisk_abort(transPtr);
455 /* below here, we know we're doing a write transaction */
456 if (!ubeacon_AmSyncSite()) {
457 udisk_abort(transPtr);
463 /* now it is safe to try remote abort */
464 code = ContactQuorum(DISK_Abort, transPtr, 0);
465 code2 = udisk_abort(transPtr);
468 return (code? code : code2);
471 /* This routine ends a read or write transaction on the open transaction identified by transPtr. It returns an error code. */
472 int ubik_EndTrans(transPtr)
473 register struct ubik_trans *transPtr; /* in */ {
474 register afs_int32 code;
477 register struct ubik_server *ts;
479 register struct ubik_dbase *dbase;
481 if (transPtr->type == UBIK_WRITETRANS) {
482 code = ubik_Flush(transPtr);
484 ubik_AbortTrans(transPtr);
489 dbase = transPtr->dbase;
491 memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
493 /* give up if no longer current */
494 if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
495 udisk_abort(transPtr);
501 if (transPtr->type == UBIK_READTRANS) { /* reads are easy */
502 code = udisk_commit(transPtr);
503 if (code == 0) goto success; /* update cachedVersion correctly */
509 if (!ubeacon_AmSyncSite()) { /* no longer sync site */
510 udisk_abort(transPtr);
516 /* now it is safe to do commit */
517 code = udisk_commit(transPtr);
518 if (code == 0) code = ContactQuorum(DISK_Commit, transPtr, CStampVersion);
520 /* failed to commit, so must return failure. Try to clear locks first, just for fun
521 Note that we don't know if this transaction will eventually commit at this point.
522 If it made it to a site that will be present in the next quorum, we win, otherwise
523 we lose. If we contact a majority of sites, then we won't be here: contacting
524 a majority guarantees commit, since it guarantees that one dude will be a
525 member of the next quorum. */
526 ContactQuorum(DISK_ReleaseLocks, transPtr, 0);
531 /* before we can start sending unlock messages, we must wait until all servers
532 that are possibly still functioning on the other side of a network partition
533 have timed out. Check the server structures, compute how long to wait, then
535 realStart = FT_ApproxTime();
537 /* wait for all servers to time out */
539 now = FT_ApproxTime();
540 /* check if we're still sync site, the guy should either come up
541 to us, or timeout. Put safety check in anyway */
542 if (now - realStart > 10 * BIGTIME) {
543 ubik_stats.escapes++;
544 ubik_print("ubik escaping from commit wait\n");
547 for(ts = ubik_servers; ts; ts=ts->next) {
548 if (!ts->beaconSinceDown && now <= ts->lastBeaconSent + BIGTIME) {
549 /* this guy could have some damaged data, wait for him */
551 tv.tv_sec = 1; /* try again after a while (ha ha) */
553 IOMGR_Select(0, 0, 0, 0, &tv); /* poll, should we wait on something? */
557 if (code == 0) break; /* no down ones still pseudo-active */
560 /* finally, unlock all the dudes. We can return success independent of the number of servers
561 that really unlock the dbase; the others will do it if/when they elect a new sync site.
562 The transaction is committed anyway, since we succeeded in contacting a quorum
563 at the start (when invoking the DiskCommit function).
565 ContactQuorum(DISK_ReleaseLocks, transPtr, 0);
569 /* update version on successful EndTrans */
570 memcpy(&dbase->cachedVersion, &dbase->version, sizeof(struct ubik_version));
576 /* This routine reads length bytes into buffer from the current position in the database. 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. Note that *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. A short read returns zero for an error code. */
578 int ubik_Read(transPtr, buffer, length)
579 register struct ubik_trans *transPtr; /* in */
580 char *buffer; /* in */
581 afs_int32 length; /* in */ {
582 register afs_int32 code;
584 /* reads are easy to do: handle locally */
585 DBHOLD(transPtr->dbase);
586 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
587 DBRELE(transPtr->dbase);
591 code = udisk_read(transPtr, transPtr->seekFile, buffer, transPtr->seekPos, length);
593 transPtr->seekPos += length;
595 DBRELE(transPtr->dbase);
599 /* This routine will flush the io data in the iovec structures. It first
600 * flushes to the local disk and then uses ContactQuorum to write it to
603 int ubik_Flush(transPtr)
604 struct ubik_trans *transPtr;
606 afs_int32 code, error=0;
608 if (transPtr->type != UBIK_WRITETRANS)
610 if (!transPtr->iovec_info.iovec_wrt_len || !transPtr->iovec_info.iovec_wrt_val)
613 DBHOLD(transPtr->dbase);
614 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
615 ERROR_EXIT(UNOQUORUM);
616 if (!ubeacon_AmSyncSite()) /* only sync site can write */
617 ERROR_EXIT(UNOTSYNC);
619 /* Update the rest of the servers in the quorum */
620 code = ContactQuorum(DISK_WriteV, transPtr, 0,
621 &transPtr->iovec_info, &transPtr->iovec_data);
623 udisk_abort(transPtr);
624 ContactQuorum(DISK_Abort, transPtr, 0); /* force aborts to the others */
625 transPtr->iovec_info.iovec_wrt_len = 0;
626 transPtr->iovec_data.iovec_buf_len = 0;
630 /* Wrote the buffers out, so start at scratch again */
631 transPtr->iovec_info.iovec_wrt_len = 0;
632 transPtr->iovec_data.iovec_buf_len = 0;
635 DBRELE(transPtr->dbase);
639 int ubik_Write(transPtr, buffer, length)
640 register struct ubik_trans *transPtr; /* in */
641 char *buffer; /* in */
642 afs_int32 length; /* in */
644 struct ubik_iovec *iovec;
645 afs_int32 code, error=0;
646 afs_int32 pos, len, size;
648 if (transPtr->type != UBIK_WRITETRANS)
653 if (length > IOVEC_MAXBUF) {
654 for (pos=0, len=length; len>0; len-=size, pos+=size) {
655 size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
656 code = ubik_Write(transPtr, &buffer[pos], size);
657 if (code) return (code);
662 if (!transPtr->iovec_info.iovec_wrt_val) {
663 transPtr->iovec_info.iovec_wrt_len = 0;
664 transPtr->iovec_info.iovec_wrt_val =
665 (struct ubik_iovec *)malloc(IOVEC_MAXWRT*sizeof(struct ubik_iovec));
666 transPtr->iovec_data.iovec_buf_len = 0;
667 transPtr->iovec_data.iovec_buf_val = (char *)malloc(IOVEC_MAXBUF);
668 if (!transPtr->iovec_info.iovec_wrt_val || !transPtr->iovec_data.iovec_buf_val) {
669 if (transPtr->iovec_info.iovec_wrt_val) free(transPtr->iovec_info.iovec_wrt_val);
670 transPtr->iovec_info.iovec_wrt_val = 0;
671 if (transPtr->iovec_data.iovec_buf_val) free(transPtr->iovec_data.iovec_buf_val);
672 transPtr->iovec_data.iovec_buf_val = 0;
677 /* If this write won't fit in the structure, then flush it out and start anew */
678 if ( (transPtr->iovec_info.iovec_wrt_len >= IOVEC_MAXWRT) ||
679 ((length + transPtr->iovec_data.iovec_buf_len) > IOVEC_MAXBUF) ) {
680 code = ubik_Flush(transPtr);
681 if (code) return (code);
684 DBHOLD(transPtr->dbase);
685 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
686 ERROR_EXIT(UNOQUORUM);
687 if (!ubeacon_AmSyncSite()) /* only sync site can write */
688 ERROR_EXIT(UNOTSYNC);
690 /* Write to the local disk */
691 code = udisk_write(transPtr, transPtr->seekFile, buffer,
692 transPtr->seekPos, length);
694 udisk_abort(transPtr);
695 transPtr->iovec_info.iovec_wrt_len = 0;
696 transPtr->iovec_data.iovec_buf_len = 0;
697 DBRELE(transPtr->dbase);
701 /* Collect writes for the other ubik servers (to be done in bulk) */
702 iovec = (struct ubik_iovec *)transPtr->iovec_info.iovec_wrt_val;
703 iovec[transPtr->iovec_info.iovec_wrt_len].file = transPtr->seekFile;
704 iovec[transPtr->iovec_info.iovec_wrt_len].position = transPtr->seekPos;
705 iovec[transPtr->iovec_info.iovec_wrt_len].length = length;
707 memcpy(&transPtr->iovec_data.iovec_buf_val[transPtr->iovec_data.iovec_buf_len], buffer, length);
709 transPtr->iovec_info.iovec_wrt_len++;
710 transPtr->iovec_data.iovec_buf_len += length;
711 transPtr->seekPos += length;
714 DBRELE(transPtr->dbase);
718 /* This sets the file pointer associated with the current transaction to the appropriate file and byte position. Unlike Unix files, a transaction is labelled by both a file number (fileid) and a byte position relative to the specified file (position). */
720 int ubik_Seek(transPtr, fileid, position)
721 register struct ubik_trans *transPtr; /* IN */
722 afs_int32 fileid; /* IN */
723 afs_int32 position; /* IN */ {
724 register afs_int32 code;
726 DBHOLD(transPtr->dbase);
727 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY)) {
730 transPtr->seekFile = fileid;
731 transPtr->seekPos = position;
734 DBRELE(transPtr->dbase);
738 /* This call returns the file pointer associated with the specified transaction in fileid and position. */
740 int ubik_Tell(transPtr, fileid, position)
741 register struct ubik_trans *transPtr; /* IN */
742 afs_int32 *fileid; /* OUT */
743 afs_int32 *position; /* OUT */ {
744 DBHOLD(transPtr->dbase);
745 *fileid = transPtr->seekFile;
746 *position = transPtr->seekPos;
747 DBRELE(transPtr->dbase);
751 /* This sets the file size for the currently-selected file to length bytes, if length is less than the file's current size. */
753 int ubik_Truncate(transPtr, length)
754 register struct ubik_trans *transPtr; /* in */
755 afs_int32 length; /* in */ {
756 afs_int32 code, error=0;
758 /* Will also catch if not UBIK_WRITETRANS */
759 code = ubik_Flush(transPtr);
760 if (code) return(code);
762 DBHOLD(transPtr->dbase);
763 /* first, check that quorum is still good, and that dbase is up-to-date */
764 if (!urecovery_AllBetter(transPtr->dbase, transPtr->flags & TRREADANY))
765 ERROR_EXIT(UNOQUORUM);
766 if (!ubeacon_AmSyncSite())
767 ERROR_EXIT(UNOTSYNC);
769 /* now do the operation locally, and propagate it out */
770 code = udisk_truncate(transPtr, transPtr->seekFile, length);
772 code = ContactQuorum(DISK_Truncate, transPtr, 0, transPtr->seekFile, length);
775 /* we must abort the operation */
776 udisk_abort(transPtr);
777 ContactQuorum(DISK_Abort, transPtr, 0); /* force aborts to the others */
782 DBRELE(transPtr->dbase);
786 /* set a lock; all locks are released on transaction end (commit/abort) */
787 ubik_SetLock(atrans, apos, alen, atype)
788 struct ubik_trans *atrans;
789 afs_int32 apos, alen; /* apos and alen are not used */
791 afs_int32 code=0, error=0;
793 if (atype == LOCKWRITE) {
794 if (atrans->type == UBIK_READTRANS) return UBADTYPE;
795 code = ubik_Flush(atrans);
796 if (code) return(code);
799 DBHOLD(atrans->dbase);
800 if (atype == LOCKREAD) {
801 code = ulock_getLock(atrans, atype, 1);
802 if (code) ERROR_EXIT(code);
805 /* first, check that quorum is still good, and that dbase is up-to-date */
806 if (!urecovery_AllBetter(atrans->dbase, atrans->flags & TRREADANY))
807 ERROR_EXIT(UNOQUORUM);
808 if (!ubeacon_AmSyncSite())
809 ERROR_EXIT(UNOTSYNC);
811 /* now do the operation locally, and propagate it out */
812 code = ulock_getLock(atrans, atype, 1);
814 code = ContactQuorum(DISK_Lock, atrans, 0, 0,
815 1/*unused*/, 1/*unused*/, LOCKWRITE);
818 /* we must abort the operation */
820 ContactQuorum(DISK_Abort, atrans, 0); /* force aborts to the others */
826 DBRELE(atrans->dbase);
830 /* utility to wait for a version # to change */
831 int ubik_WaitVersion(adatabase, aversion)
832 register struct ubik_version *aversion;
833 register struct ubik_dbase *adatabase; {
835 /* wait until version # changes, and then return */
836 if (vcmp(*aversion, adatabase->version) != 0)
838 LWP_WaitProcess(&adatabase->version); /* same vers, just wait */
842 /* utility to get the version of the dbase a transaction is dealing with */
843 int ubik_GetVersion(atrans, avers)
844 register struct ubik_trans *atrans;
845 register struct ubik_version *avers; {
846 *avers = atrans->dbase->version;
850 /* Facility to simplify database caching. Returns zero if last trans was done
851 on the local server and was successful. If return value is non-zero and the
852 caller is a server caching part of the Ubik database, it should invalidate
853 that cache. A return value of -1 means bad (NULL) argument. */
855 int ubik_CacheUpdate (atrans)
856 register struct ubik_trans *atrans;
858 if (!(atrans && atrans->dbase)) return -1;
859 return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
862 int panic(a, b, c, d)
865 ubik_print("Ubik PANIC: ");
866 ubik_print(a, b, c, d);
868 ubik_print("BACK FROM ABORT\n"); /* shouldn't come back */
869 exit(1); /* never know, though */
873 ** This functions takes an IP addresses as its parameter. It returns the
874 ** the primary IP address that is on the host passed in.
877 ubikGetPrimaryInterfaceAddr(addr)
878 afs_uint32 addr; /* network byte order */
880 struct ubik_server *ts;
883 for ( ts=ubik_servers; ts; ts=ts->next )
884 for ( j=0; j < UBIK_MAX_INTERFACE_ADDR; j++)
885 if ( ts->addr[j] == addr )
886 return ts->addr[0]; /* net byte order */
887 return 0; /* if not in server database, return error */