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>
19 #include <afs/afsutil.h>
21 #define UBIK_INTERNALS
25 static void printServerInfo(void);
28 * routines for handling requests remotely-submitted by the sync site. These are
29 * only write transactions (we don't propagate read trans), and there is at most one
30 * write transaction extant at any one time.
33 struct ubik_trans *ubik_currentTrans = 0;
37 /* the rest of these guys handle remote execution of write
38 * transactions: this is the code executed on the other servers when a
39 * sync site is executing a write transaction.
42 SDISK_Begin(struct rx_call *rxcall, struct ubik_tid *atid)
46 if ((code = ubik_CheckAuth(rxcall))) {
50 urecovery_CheckTid(atid, 1);
51 code = udisk_begin(ubik_dbase, UBIK_WRITETRANS, &ubik_currentTrans);
52 if (!code && ubik_currentTrans) {
53 /* label this trans with the right trans id */
54 ubik_currentTrans->tid.epoch = atid->epoch;
55 ubik_currentTrans->tid.counter = atid->counter;
63 SDISK_Commit(struct rx_call *rxcall, struct ubik_tid *atid)
67 if ((code = ubik_CheckAuth(rxcall))) {
70 ObtainWriteLock(&ubik_dbase->cache_lock);
72 if (!ubik_currentTrans) {
77 * sanity check to make sure only write trans appear here
79 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
84 urecovery_CheckTid(atid, 0);
85 if (!ubik_currentTrans) {
90 code = udisk_commit(ubik_currentTrans);
92 /* sync site should now match */
93 uvote_set_dbVersion(ubik_dbase->version);
97 ReleaseWriteLock(&ubik_dbase->cache_lock);
102 SDISK_ReleaseLocks(struct rx_call *rxcall, struct ubik_tid *atid)
106 if ((code = ubik_CheckAuth(rxcall))) {
112 if (!ubik_currentTrans) {
116 /* sanity check to make sure only write trans appear here */
117 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
122 urecovery_CheckTid(atid, 0);
123 if (!ubik_currentTrans) {
128 /* If the thread is not waiting for lock - ok to end it */
129 if (ubik_currentTrans->locktype != LOCKWAIT) {
130 udisk_end(ubik_currentTrans);
132 ubik_currentTrans = (struct ubik_trans *)0;
139 SDISK_Abort(struct rx_call *rxcall, struct ubik_tid *atid)
143 if ((code = ubik_CheckAuth(rxcall))) {
147 if (!ubik_currentTrans) {
151 /* sanity check to make sure only write trans appear here */
152 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
157 urecovery_CheckTid(atid, 0);
158 if (!ubik_currentTrans) {
163 code = udisk_abort(ubik_currentTrans);
164 /* If the thread is not waiting for lock - ok to end it */
165 if (ubik_currentTrans->locktype != LOCKWAIT) {
166 udisk_end(ubik_currentTrans);
168 ubik_currentTrans = (struct ubik_trans *)0;
174 /* apos and alen are not used */
176 SDISK_Lock(struct rx_call *rxcall, struct ubik_tid *atid,
177 afs_int32 afile, afs_int32 apos, afs_int32 alen, afs_int32 atype)
180 struct ubik_trans *ubik_thisTrans;
182 if ((code = ubik_CheckAuth(rxcall))) {
186 if (!ubik_currentTrans) {
190 /* sanity check to make sure only write trans appear here */
191 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
199 urecovery_CheckTid(atid, 0);
200 if (!ubik_currentTrans) {
205 ubik_thisTrans = ubik_currentTrans;
206 code = ulock_getLock(ubik_currentTrans, atype, 1);
208 /* While waiting, the transaction may have been ended/
209 * aborted from under us (urecovery_CheckTid). In that
210 * case, end the transaction here.
212 if (!code && (ubik_currentTrans != ubik_thisTrans)) {
213 udisk_end(ubik_thisTrans);
222 * \brief Write a vector of data
225 SDISK_WriteV(struct rx_call *rxcall, struct ubik_tid *atid,
226 iovec_wrt *io_vector, iovec_buf *io_buffer)
228 afs_int32 code, i, offset;
229 struct ubik_iovec *iovec;
232 if ((code = ubik_CheckAuth(rxcall))) {
236 if (!ubik_currentTrans) {
240 /* sanity check to make sure only write trans appear here */
241 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
246 urecovery_CheckTid(atid, 0);
247 if (!ubik_currentTrans) {
252 iovec = (struct ubik_iovec *)io_vector->iovec_wrt_val;
253 iobuf = (char *)io_buffer->iovec_buf_val;
254 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
255 /* Sanity check for going off end of buffer */
256 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
260 udisk_write(ubik_currentTrans, iovec[i].file, &iobuf[offset],
261 iovec[i].position, iovec[i].length);
266 offset += iovec[i].length;
274 SDISK_Write(struct rx_call *rxcall, struct ubik_tid *atid,
275 afs_int32 afile, afs_int32 apos, bulkdata *adata)
279 if ((code = ubik_CheckAuth(rxcall))) {
283 if (!ubik_currentTrans) {
287 /* sanity check to make sure only write trans appear here */
288 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
293 urecovery_CheckTid(atid, 0);
294 if (!ubik_currentTrans) {
299 udisk_write(ubik_currentTrans, afile, adata->bulkdata_val, apos,
300 adata->bulkdata_len);
307 SDISK_Truncate(struct rx_call *rxcall, struct ubik_tid *atid,
308 afs_int32 afile, afs_int32 alen)
312 if ((code = ubik_CheckAuth(rxcall))) {
316 if (!ubik_currentTrans) {
320 /* sanity check to make sure only write trans appear here */
321 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
326 urecovery_CheckTid(atid, 0);
327 if (!ubik_currentTrans) {
331 code = udisk_truncate(ubik_currentTrans, afile, alen);
338 SDISK_GetVersion(struct rx_call *rxcall,
339 struct ubik_version *aversion)
343 if ((code = ubik_CheckAuth(rxcall))) {
348 * If we are the sync site, recovery shouldn't be running on any
349 * other site. We shouldn't be getting this RPC as long as we are
350 * the sync site. To prevent any unforseen activity, we should
351 * reject this RPC until we have recognized that we are not the
352 * sync site anymore, and/or if we have any pending WRITE
353 * transactions that have to complete. This way we can be assured
354 * that this RPC would not block any pending transactions that
355 * should either fail or pass. If we have recognized the fact that
356 * we are not the sync site any more, all write transactions would
357 * fail with UNOQUORUM anyway.
360 if (ubeacon_AmSyncSite()) {
365 code = (*ubik_dbase->getlabel) (ubik_dbase, 0, aversion);
368 /* tell other side there's no dbase */
370 aversion->counter = 0;
376 SDISK_GetFile(struct rx_call *rxcall, afs_int32 file,
377 struct ubik_version *version)
380 struct ubik_dbase *dbase;
382 struct ubik_stat ubikstat;
387 if ((code = ubik_CheckAuth(rxcall))) {
392 code = (*dbase->stat) (dbase, file, &ubikstat);
397 length = ubikstat.size;
398 tlen = htonl(length);
399 code = rx_Write(rxcall, (char *)&tlen, sizeof(afs_int32));
400 if (code != sizeof(afs_int32)) {
402 ubik_dprint("Rx-write length error=%d\n", code);
407 tlen = (length > sizeof(tbuffer) ? sizeof(tbuffer) : length);
408 code = (*dbase->read) (dbase, file, tbuffer, offset, tlen);
411 ubik_dprint("read failed error=%d\n", code);
414 code = rx_Write(rxcall, tbuffer, tlen);
417 ubik_dprint("Rx-write length error=%d\n", code);
423 code = (*dbase->getlabel) (dbase, file, version); /* return the dbase, too */
429 SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
430 afs_int32 length, struct ubik_version *avers)
433 struct ubik_dbase *dbase = NULL;
436 struct ubik_version tversion;
438 struct rx_peer *tpeer;
439 struct rx_connection *tconn;
440 afs_uint32 otherHost = 0;
447 /* send the file back to the requester */
451 if ((code = ubik_CheckAuth(rxcall))) {
456 /* next, we do a sanity check to see if the guy sending us the database is
457 * the guy we think is the sync site. It turns out that we might not have
458 * decided yet that someone's the sync site, but they could have enough
459 * votes from others to be sync site anyway, and could send us the database
460 * in advance of getting our votes. This is fine, what we're really trying
461 * to check is that some authenticated bogon isn't sending a random database
462 * into another configuration. This could happen on a bad configuration
463 * screwup. Thus, we only object if we're sure we know who the sync site
464 * is, and it ain't the guy talking to us.
466 offset = uvote_GetSyncSite();
467 tconn = rx_ConnectionOf(rxcall);
468 tpeer = rx_PeerOf(tconn);
469 otherHost = ubikGetPrimaryInterfaceAddr(rx_HostOf(tpeer));
470 if (offset && offset != otherHost) {
471 /* we *know* this is the wrong guy */
479 /* abort any active trans that may scribble over the database */
480 urecovery_AbortAll(dbase);
482 ubik_print("Ubik: Synchronize database with server %s\n",
483 afs_inet_ntoa_r(otherHost, hoststr));
487 epoch = tversion.epoch = 0; /* start off by labelling in-transit db as invalid */
488 (*dbase->setlabel) (dbase, file, &tversion); /* setlabel does sync */
489 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.TMP",
490 ubik_dbase->pathName, (file<0)?"SYS":"",
491 (file<0)?-file:file);
492 fd = open(pbuffer, O_CREAT | O_RDWR | O_TRUNC, 0600);
497 code = lseek(fd, HDRSIZE, 0);
498 if (code != HDRSIZE) {
503 memcpy(&ubik_dbase->version, &tversion, sizeof(struct ubik_version));
506 tlen = (length > sizeof(tbuffer) ? sizeof(tbuffer) : length);
507 #if !defined(AFS_PTHREAD_ENV)
511 code = rx_Read(rxcall, tbuffer, tlen);
513 ubik_dprint("Rx-read length error=%d\n", code);
518 code = write(fd, tbuffer, tlen);
521 ubik_dprint("write failed error=%d\n", code);
533 /* sync data first, then write label and resync (resync done by setlabel call).
534 * This way, good label is only on good database. */
535 snprintf(tbuffer, sizeof(tbuffer), "%s.DB%s%d",
536 ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
538 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.OLD",
539 ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
540 code = unlink(pbuffer);
542 code = rename(tbuffer, pbuffer);
543 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.TMP",
544 ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
547 code = rename(pbuffer, tbuffer);
550 (*ubik_dbase->open) (ubik_dbase, file);
551 code = (*ubik_dbase->setlabel) (dbase, file, avers);
554 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.OLD",
555 ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
558 memcpy(&ubik_dbase->version, avers, sizeof(struct ubik_version));
559 udisk_Invalidate(dbase, file); /* new dbase, flush disk buffers */
560 #ifdef AFS_PTHREAD_ENV
561 assert(pthread_cond_broadcast(&dbase->version_cond) == 0);
563 LWP_NoYieldSignal(&dbase->version);
572 /* Failed to sync. Allow reads again for now. */
575 tversion.epoch = epoch;
576 (*dbase->setlabel) (dbase, file, &tversion);
580 ("Ubik: Synchronize database with server %s failed (error = %d)\n",
581 afs_inet_ntoa_r(otherHost, hoststr), code);
583 ubik_print("Ubik: Synchronize database completed\n");
591 SDISK_Probe(struct rx_call *rxcall)
597 * \brief Update remote machines addresses in my server list
599 * Send back my addresses to caller of this RPC
600 * \return zero on success, else 1.
603 SDISK_UpdateInterfaceAddr(struct rx_call *rxcall,
604 UbikInterfaceAddr *inAddr,
605 UbikInterfaceAddr *outAddr)
607 struct ubik_server *ts, *tmp;
608 afs_uint32 remoteAddr; /* in net byte order */
609 int i, j, found = 0, probableMatch = 0;
613 /* copy the output parameters */
614 for (i = 0; i < UBIK_MAX_INTERFACE_ADDR; i++)
615 outAddr->hostAddr[i] = ntohl(ubik_host[i]);
617 remoteAddr = htonl(inAddr->hostAddr[0]);
618 for (ts = ubik_servers; ts; ts = ts->next)
619 if (ts->addr[0] == remoteAddr) { /* both in net byte order */
625 /* verify that all addresses in the incoming RPC are
626 ** not part of other server entries in my CellServDB
628 for (i = 0; !found && (i < UBIK_MAX_INTERFACE_ADDR)
629 && inAddr->hostAddr[i]; i++) {
630 remoteAddr = htonl(inAddr->hostAddr[i]);
631 for (tmp = ubik_servers; (!found && tmp); tmp = tmp->next) {
632 if (ts == tmp) /* this is my server */
634 for (j = 0; (j < UBIK_MAX_INTERFACE_ADDR) && tmp->addr[j];
636 if (remoteAddr == tmp->addr[j]) {
644 /* if (probableMatch) */
645 /* inconsistent addresses in CellServDB */
646 if (!probableMatch || found) {
647 ubik_print("Inconsistent Cell Info from server: ");
648 for (i = 0; i < UBIK_MAX_INTERFACE_ADDR && inAddr->hostAddr[i]; i++)
649 ubik_print("%s ", afs_inet_ntoa_r(htonl(inAddr->hostAddr[i]), hoststr));
658 /* update our data structures */
659 for (i = 1; i < UBIK_MAX_INTERFACE_ADDR; i++)
660 ts->addr[i] = htonl(inAddr->hostAddr[i]);
662 ubik_print("ubik: A Remote Server has addresses: ");
663 for (i = 0; i < UBIK_MAX_INTERFACE_ADDR && ts->addr[i]; i++)
664 ubik_print("%s ", afs_inet_ntoa_r(ts->addr[i], hoststr));
672 printServerInfo(void)
674 struct ubik_server *ts;
678 ubik_print("Local CellServDB:");
679 for (ts = ubik_servers; ts; ts = ts->next, j++) {
680 ubik_print("Server %d: ", j);
681 for (i = 0; (i < UBIK_MAX_INTERFACE_ADDR) && ts->addr[i]; i++)
682 ubik_print("%s ", afs_inet_ntoa_r(ts->addr[i], hoststr));
688 SDISK_SetVersion(struct rx_call *rxcall, struct ubik_tid *atid,
689 struct ubik_version *oldversionp,
690 struct ubik_version *newversionp)
694 if ((code = ubik_CheckAuth(rxcall))) {
698 if (!ubik_currentTrans) {
702 /* sanity check to make sure only write trans appear here */
703 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
708 /* Should not get this for the sync site */
709 if (ubeacon_AmSyncSite()) {
714 urecovery_CheckTid(atid, 0);
715 if (!ubik_currentTrans) {
720 /* Set the label if its version matches the sync-site's */
721 if (uvote_eq_dbVersion(*oldversionp)) {
723 code = (*ubik_dbase->setlabel) (ubik_dbase, 0, newversionp);
725 ubik_dbase->version = *newversionp;
726 uvote_set_dbVersion(*newversionp);