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>
21 #include <afs/afsutil.h>
23 #define UBIK_INTERNALS
27 static void printServerInfo(void);
30 * routines for handling requests remotely-submitted by the sync site. These are
31 * only write transactions (we don't propagate read trans), and there is at most one
32 * write transaction extant at any one time.
35 struct ubik_trans *ubik_currentTrans = 0;
39 /* the rest of these guys handle remote execution of write
40 * transactions: this is the code executed on the other servers when a
41 * sync site is executing a write transaction.
44 SDISK_Begin(struct rx_call *rxcall, struct ubik_tid *atid)
48 if ((code = ubik_CheckAuth(rxcall))) {
52 urecovery_CheckTid(atid, 1);
53 code = udisk_begin(ubik_dbase, UBIK_WRITETRANS, &ubik_currentTrans);
54 if (!code && ubik_currentTrans) {
55 /* label this trans with the right trans id */
56 ubik_currentTrans->tid.epoch = atid->epoch;
57 ubik_currentTrans->tid.counter = atid->counter;
65 SDISK_Commit(struct rx_call *rxcall, struct ubik_tid *atid)
69 if ((code = ubik_CheckAuth(rxcall))) {
72 ObtainWriteLock(&ubik_dbase->cache_lock);
74 if (!ubik_currentTrans) {
79 * sanity check to make sure only write trans appear here
81 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
86 urecovery_CheckTid(atid, 0);
87 if (!ubik_currentTrans) {
92 code = udisk_commit(ubik_currentTrans);
94 /* sync site should now match */
95 uvote_set_dbVersion(ubik_dbase->version);
99 ReleaseWriteLock(&ubik_dbase->cache_lock);
104 SDISK_ReleaseLocks(struct rx_call *rxcall, struct ubik_tid *atid)
108 if ((code = ubik_CheckAuth(rxcall))) {
114 if (!ubik_currentTrans) {
118 /* sanity check to make sure only write trans appear here */
119 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
124 urecovery_CheckTid(atid, 0);
125 if (!ubik_currentTrans) {
130 /* If the thread is not waiting for lock - ok to end it */
131 if (ubik_currentTrans->locktype != LOCKWAIT) {
132 udisk_end(ubik_currentTrans);
134 ubik_currentTrans = (struct ubik_trans *)0;
141 SDISK_Abort(struct rx_call *rxcall, struct ubik_tid *atid)
145 if ((code = ubik_CheckAuth(rxcall))) {
149 if (!ubik_currentTrans) {
153 /* sanity check to make sure only write trans appear here */
154 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
159 urecovery_CheckTid(atid, 0);
160 if (!ubik_currentTrans) {
165 code = udisk_abort(ubik_currentTrans);
166 /* If the thread is not waiting for lock - ok to end it */
167 if (ubik_currentTrans->locktype != LOCKWAIT) {
168 udisk_end(ubik_currentTrans);
170 ubik_currentTrans = (struct ubik_trans *)0;
176 /* apos and alen are not used */
178 SDISK_Lock(struct rx_call *rxcall, struct ubik_tid *atid,
179 afs_int32 afile, afs_int32 apos, afs_int32 alen, afs_int32 atype)
182 struct ubik_trans *ubik_thisTrans;
184 if ((code = ubik_CheckAuth(rxcall))) {
188 if (!ubik_currentTrans) {
192 /* sanity check to make sure only write trans appear here */
193 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
201 urecovery_CheckTid(atid, 0);
202 if (!ubik_currentTrans) {
207 ubik_thisTrans = ubik_currentTrans;
208 code = ulock_getLock(ubik_currentTrans, atype, 1);
210 /* While waiting, the transaction may have been ended/
211 * aborted from under us (urecovery_CheckTid). In that
212 * case, end the transaction here.
214 if (!code && (ubik_currentTrans != ubik_thisTrans)) {
215 udisk_end(ubik_thisTrans);
224 * \brief Write a vector of data
227 SDISK_WriteV(struct rx_call *rxcall, struct ubik_tid *atid,
228 iovec_wrt *io_vector, iovec_buf *io_buffer)
230 afs_int32 code, i, offset;
231 struct ubik_iovec *iovec;
234 if ((code = ubik_CheckAuth(rxcall))) {
238 if (!ubik_currentTrans) {
242 /* sanity check to make sure only write trans appear here */
243 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
248 urecovery_CheckTid(atid, 0);
249 if (!ubik_currentTrans) {
254 iovec = (struct ubik_iovec *)io_vector->iovec_wrt_val;
255 iobuf = (char *)io_buffer->iovec_buf_val;
256 for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
257 /* Sanity check for going off end of buffer */
258 if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
262 udisk_write(ubik_currentTrans, iovec[i].file, &iobuf[offset],
263 iovec[i].position, iovec[i].length);
268 offset += iovec[i].length;
276 SDISK_Write(struct rx_call *rxcall, struct ubik_tid *atid,
277 afs_int32 afile, afs_int32 apos, bulkdata *adata)
281 if ((code = ubik_CheckAuth(rxcall))) {
285 if (!ubik_currentTrans) {
289 /* sanity check to make sure only write trans appear here */
290 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
295 urecovery_CheckTid(atid, 0);
296 if (!ubik_currentTrans) {
301 udisk_write(ubik_currentTrans, afile, adata->bulkdata_val, apos,
302 adata->bulkdata_len);
309 SDISK_Truncate(struct rx_call *rxcall, struct ubik_tid *atid,
310 afs_int32 afile, afs_int32 alen)
314 if ((code = ubik_CheckAuth(rxcall))) {
318 if (!ubik_currentTrans) {
322 /* sanity check to make sure only write trans appear here */
323 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
328 urecovery_CheckTid(atid, 0);
329 if (!ubik_currentTrans) {
333 code = udisk_truncate(ubik_currentTrans, afile, alen);
340 SDISK_GetVersion(struct rx_call *rxcall,
341 struct ubik_version *aversion)
345 if ((code = ubik_CheckAuth(rxcall))) {
350 * If we are the sync site, recovery shouldn't be running on any
351 * other site. We shouldn't be getting this RPC as long as we are
352 * the sync site. To prevent any unforseen activity, we should
353 * reject this RPC until we have recognized that we are not the
354 * sync site anymore, and/or if we have any pending WRITE
355 * transactions that have to complete. This way we can be assured
356 * that this RPC would not block any pending transactions that
357 * should either fail or pass. If we have recognized the fact that
358 * we are not the sync site any more, all write transactions would
359 * fail with UNOQUORUM anyway.
362 if (ubeacon_AmSyncSite()) {
367 code = (*ubik_dbase->getlabel) (ubik_dbase, 0, aversion);
370 /* tell other side there's no dbase */
372 aversion->counter = 0;
378 SDISK_GetFile(struct rx_call *rxcall, afs_int32 file,
379 struct ubik_version *version)
382 struct ubik_dbase *dbase;
384 struct ubik_stat ubikstat;
389 if ((code = ubik_CheckAuth(rxcall))) {
394 code = (*dbase->stat) (dbase, file, &ubikstat);
399 length = ubikstat.size;
400 tlen = htonl(length);
401 code = rx_Write(rxcall, (char *)&tlen, sizeof(afs_int32));
402 if (code != sizeof(afs_int32)) {
404 ubik_dprint("Rx-write length error=%d\n", code);
409 tlen = (length > sizeof(tbuffer) ? sizeof(tbuffer) : length);
410 code = (*dbase->read) (dbase, file, tbuffer, offset, tlen);
413 ubik_dprint("read failed error=%d\n", code);
416 code = rx_Write(rxcall, tbuffer, tlen);
419 ubik_dprint("Rx-write length error=%d\n", code);
425 code = (*dbase->getlabel) (dbase, file, version); /* return the dbase, too */
431 SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
432 afs_int32 length, struct ubik_version *avers)
435 struct ubik_dbase *dbase = NULL;
438 struct ubik_version tversion;
440 struct rx_peer *tpeer;
441 struct rx_connection *tconn;
442 afs_uint32 otherHost = 0;
449 /* send the file back to the requester */
453 if ((code = ubik_CheckAuth(rxcall))) {
458 /* next, we do a sanity check to see if the guy sending us the database is
459 * the guy we think is the sync site. It turns out that we might not have
460 * decided yet that someone's the sync site, but they could have enough
461 * votes from others to be sync site anyway, and could send us the database
462 * in advance of getting our votes. This is fine, what we're really trying
463 * to check is that some authenticated bogon isn't sending a random database
464 * into another configuration. This could happen on a bad configuration
465 * screwup. Thus, we only object if we're sure we know who the sync site
466 * is, and it ain't the guy talking to us.
468 offset = uvote_GetSyncSite();
469 tconn = rx_ConnectionOf(rxcall);
470 tpeer = rx_PeerOf(tconn);
471 otherHost = ubikGetPrimaryInterfaceAddr(rx_HostOf(tpeer));
472 if (offset && offset != otherHost) {
473 /* we *know* this is the wrong guy */
481 /* abort any active trans that may scribble over the database */
482 urecovery_AbortAll(dbase);
484 ubik_print("Ubik: Synchronize database with server %s\n",
485 afs_inet_ntoa_r(otherHost, hoststr));
489 epoch = tversion.epoch = 0; /* start off by labelling in-transit db as invalid */
490 (*dbase->setlabel) (dbase, file, &tversion); /* setlabel does sync */
491 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.TMP",
492 ubik_dbase->pathName, (file<0)?"SYS":"",
493 (file<0)?-file:file);
494 fd = open(pbuffer, O_CREAT | O_RDWR | O_TRUNC, 0600);
499 code = lseek(fd, HDRSIZE, 0);
500 if (code != HDRSIZE) {
505 memcpy(&ubik_dbase->version, &tversion, sizeof(struct ubik_version));
508 tlen = (length > sizeof(tbuffer) ? sizeof(tbuffer) : length);
509 #if !defined(AFS_PTHREAD_ENV)
513 code = rx_Read(rxcall, tbuffer, tlen);
515 ubik_dprint("Rx-read length error=%d\n", code);
520 code = write(fd, tbuffer, tlen);
523 ubik_dprint("write failed error=%d\n", code);
535 /* sync data first, then write label and resync (resync done by setlabel call).
536 * This way, good label is only on good database. */
537 snprintf(tbuffer, sizeof(tbuffer), "%s.DB%s%d",
538 ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
540 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.OLD",
541 ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
542 code = unlink(pbuffer);
544 code = rename(tbuffer, pbuffer);
545 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.TMP",
546 ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
549 code = rename(pbuffer, tbuffer);
552 (*ubik_dbase->open) (ubik_dbase, file);
553 code = (*ubik_dbase->setlabel) (dbase, file, avers);
556 snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.OLD",
557 ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
560 memcpy(&ubik_dbase->version, avers, sizeof(struct ubik_version));
561 udisk_Invalidate(dbase, file); /* new dbase, flush disk buffers */
562 #ifdef AFS_PTHREAD_ENV
563 opr_Assert(pthread_cond_broadcast(&dbase->version_cond) == 0);
565 LWP_NoYieldSignal(&dbase->version);
574 /* Failed to sync. Allow reads again for now. */
577 tversion.epoch = epoch;
578 (*dbase->setlabel) (dbase, file, &tversion);
582 ("Ubik: Synchronize database with server %s failed (error = %d)\n",
583 afs_inet_ntoa_r(otherHost, hoststr), code);
585 ubik_print("Ubik: Synchronize database completed\n");
593 SDISK_Probe(struct rx_call *rxcall)
599 * \brief Update remote machines addresses in my server list
601 * Send back my addresses to caller of this RPC
602 * \return zero on success, else 1.
605 SDISK_UpdateInterfaceAddr(struct rx_call *rxcall,
606 UbikInterfaceAddr *inAddr,
607 UbikInterfaceAddr *outAddr)
609 struct ubik_server *ts, *tmp;
610 afs_uint32 remoteAddr; /* in net byte order */
611 int i, j, found = 0, probableMatch = 0;
615 /* copy the output parameters */
616 for (i = 0; i < UBIK_MAX_INTERFACE_ADDR; i++)
617 outAddr->hostAddr[i] = ntohl(ubik_host[i]);
619 remoteAddr = htonl(inAddr->hostAddr[0]);
620 for (ts = ubik_servers; ts; ts = ts->next)
621 if (ts->addr[0] == remoteAddr) { /* both in net byte order */
627 /* verify that all addresses in the incoming RPC are
628 ** not part of other server entries in my CellServDB
630 for (i = 0; !found && (i < UBIK_MAX_INTERFACE_ADDR)
631 && inAddr->hostAddr[i]; i++) {
632 remoteAddr = htonl(inAddr->hostAddr[i]);
633 for (tmp = ubik_servers; (!found && tmp); tmp = tmp->next) {
634 if (ts == tmp) /* this is my server */
636 for (j = 0; (j < UBIK_MAX_INTERFACE_ADDR) && tmp->addr[j];
638 if (remoteAddr == tmp->addr[j]) {
646 /* if (probableMatch) */
647 /* inconsistent addresses in CellServDB */
648 if (!probableMatch || found) {
649 ubik_print("Inconsistent Cell Info from server: ");
650 for (i = 0; i < UBIK_MAX_INTERFACE_ADDR && inAddr->hostAddr[i]; i++)
651 ubik_print("%s ", afs_inet_ntoa_r(htonl(inAddr->hostAddr[i]), hoststr));
660 /* update our data structures */
661 for (i = 1; i < UBIK_MAX_INTERFACE_ADDR; i++)
662 ts->addr[i] = htonl(inAddr->hostAddr[i]);
664 ubik_print("ubik: A Remote Server has addresses: ");
665 for (i = 0; i < UBIK_MAX_INTERFACE_ADDR && ts->addr[i]; i++)
666 ubik_print("%s ", afs_inet_ntoa_r(ts->addr[i], hoststr));
674 printServerInfo(void)
676 struct ubik_server *ts;
680 ubik_print("Local CellServDB:");
681 for (ts = ubik_servers; ts; ts = ts->next, j++) {
682 ubik_print("Server %d: ", j);
683 for (i = 0; (i < UBIK_MAX_INTERFACE_ADDR) && ts->addr[i]; i++)
684 ubik_print("%s ", afs_inet_ntoa_r(ts->addr[i], hoststr));
690 SDISK_SetVersion(struct rx_call *rxcall, struct ubik_tid *atid,
691 struct ubik_version *oldversionp,
692 struct ubik_version *newversionp)
696 if ((code = ubik_CheckAuth(rxcall))) {
700 if (!ubik_currentTrans) {
704 /* sanity check to make sure only write trans appear here */
705 if (ubik_currentTrans->type != UBIK_WRITETRANS) {
710 /* Should not get this for the sync site */
711 if (ubeacon_AmSyncSite()) {
716 urecovery_CheckTid(atid, 0);
717 if (!ubik_currentTrans) {
722 /* Set the label if its version matches the sync-site's */
723 if (uvote_eq_dbVersion(*oldversionp)) {
725 code = (*ubik_dbase->setlabel) (ubik_dbase, 0, newversionp);
727 ubik_dbase->version = *newversionp;
728 uvote_set_dbVersion(*newversionp);