about summary refs log tree commit diff
path: root/db2/txn
diff options
context:
space:
mode:
Diffstat (limited to 'db2/txn')
-rw-r--r--db2/txn/txn.c445
-rw-r--r--db2/txn/txn.src36
-rw-r--r--db2/txn/txn_auto.c357
-rw-r--r--db2/txn/txn_rec.c196
4 files changed, 874 insertions, 160 deletions
diff --git a/db2/txn/txn.c b/db2/txn/txn.c
index 4f3ffd8ed2..aa0b3652ce 100644
--- a/db2/txn/txn.c
+++ b/db2/txn/txn.c
@@ -43,7 +43,7 @@
 #include "config.h"
 
 #ifndef lint
-static const char sccsid[] = "@(#)txn.c	10.58 (Sleepycat) 5/31/98";
+static const char sccsid[] = "@(#)txn.c	10.66 (Sleepycat) 1/3/99";
 #endif /* not lint */
 
 
@@ -66,12 +66,14 @@ static const char sccsid[] = "@(#)txn.c	10.58 (Sleepycat) 5/31/98";
 #include "db_am.h"
 #include "common_ext.h"
 
-static int __txn_check_running __P((const DB_TXN *));
-static int __txn_end __P((DB_TXN *, int));
-static int __txn_grow_region __P((DB_TXNMGR *));
-static int __txn_init __P((DB_TXNREGION *));
-static int __txn_undo __P((DB_TXN *));
-static int __txn_validate_region __P((DB_TXNMGR *));
+static int  __txn_begin __P((DB_TXN *));
+static int  __txn_check_running __P((const DB_TXN *, TXN_DETAIL **));
+static int  __txn_end __P((DB_TXN *, int));
+static void __txn_freekids __P((DB_TXN *));
+static int  __txn_grow_region __P((DB_TXNMGR *));
+static int  __txn_init __P((DB_TXNREGION *));
+static int  __txn_undo __P((DB_TXN *));
+static int  __txn_validate_region __P((DB_TXNMGR *));
 
 /*
  * This file contains the top level routines of the transaction library.
@@ -93,7 +95,10 @@ __txn_init(txn_region)
 	txn_region->magic = DB_TXNMAGIC;
 	txn_region->version = DB_TXNVERSION;
 	txn_region->last_txnid = TXN_MINIMUM;
-	/* XXX If we ever do more types of locking and logging, this changes. */
+	/*
+	 * XXX
+	 * If we ever do more types of locking and logging, this changes.
+	 */
 	txn_region->logtype = 0;
 	txn_region->locktype = 0;
 	txn_region->time_ckp = now;
@@ -132,10 +137,8 @@ txn_open(path, flags, mode, dbenv, mgrpp)
 	maxtxns = dbenv->tx_max != 0 ? dbenv->tx_max : 20;
 
 	/* Now, create the transaction manager structure and set its fields. */
-	if ((tmgrp = (DB_TXNMGR *)__db_calloc(1, sizeof(DB_TXNMGR))) == NULL) {
-		__db_err(dbenv, "txn_open: %s", strerror(ENOMEM));
-		return (ENOMEM);
-	}
+	if ((ret = __os_calloc(1, sizeof(DB_TXNMGR), &tmgrp)) != 0)
+		return (ret);
 
 	/* Initialize the transaction manager structure. */
 	tmgrp->mutexp = NULL;
@@ -151,7 +154,7 @@ txn_open(path, flags, mode, dbenv, mgrpp)
 	if (path == NULL)
 		tmgrp->reginfo.path = NULL;
 	else
-		if ((tmgrp->reginfo.path = (char *)__db_strdup(path)) == NULL)
+		if ((ret = __os_strdup(path, &tmgrp->reginfo.path)) != 0)
 			goto err;
 	tmgrp->reginfo.file = DEFAULT_TXN_FILE;
 	tmgrp->reginfo.mode = mode;
@@ -207,36 +210,96 @@ err:	if (tmgrp->reginfo.addr != NULL) {
 	}
 
 	if (tmgrp->reginfo.path != NULL)
-		FREES(tmgrp->reginfo.path);
-	FREE(tmgrp, sizeof(*tmgrp));
+		__os_freestr(tmgrp->reginfo.path);
+	__os_free(tmgrp, sizeof(*tmgrp));
 	return (ret);
 }
 
 /*
- * Internally, we use TXN_DETAIL structures, but we allocate and return
- * DB_TXN structures that provide access to the transaction ID and the
- * offset in the transaction region of the TXN_DETAIL structure.
+ * __txn_panic --
+ *	Panic a transaction region.
+ *
+ * PUBLIC: void __txn_panic __P((DB_ENV *));
+ */
+void
+__txn_panic(dbenv)
+	DB_ENV *dbenv;
+{
+	if (dbenv->tx_info != NULL)
+		dbenv->tx_info->region->hdr.panic = 1;
+}
+
+/*
+ * txn_begin --
+ *	This is a wrapper to the actual begin process.  Normal txn_begin()
+ * allocates a DB_TXN structure for the caller, while txn_xa_begin() does
+ * not.  Other than that, both call into the common __txn_begin code().
+ *
+ * Internally, we use TXN_DETAIL structures, but the DB_TXN structure
+ * provides access to the transaction ID and the offset in the transaction
+ * region of the TXN_DETAIL structure.
  */
 int
 txn_begin(tmgrp, parent, txnpp)
 	DB_TXNMGR *tmgrp;
-	DB_TXN *parent;
-	DB_TXN **txnpp;
+	DB_TXN *parent, **txnpp;
 {
-	DB_LSN begin_lsn;
-	DB_TXN *retp;
-	TXN_DETAIL *txnp;
-	size_t off;
-	u_int32_t id;
+	DB_TXN *txn;
 	int ret;
 
-	txnp = NULL;
-	*txnpp = NULL;
+	TXN_PANIC_CHECK(tmgrp);
 
-	if ((retp = (DB_TXN *)__db_malloc(sizeof(DB_TXN))) == NULL) {
-		__db_err(tmgrp->dbenv, "txn_begin : %s", strerror(ENOMEM));
-		return (ENOMEM);
+	if ((ret = __os_calloc(1, sizeof(DB_TXN), &txn)) != 0)
+		return (ret);
+
+	txn->parent = parent;
+	TAILQ_INIT(&txn->kids);
+	txn->mgrp = tmgrp;
+	txn->flags = TXN_MALLOC;
+	if ((ret = __txn_begin(txn)) != 0) {
+		__os_free(txn, sizeof(DB_TXN));
+		txn = NULL;
 	}
+	if (txn != NULL && parent != NULL)
+		TAILQ_INSERT_HEAD(&parent->kids, txn, klinks);
+	*txnpp = txn;
+	return (ret);
+}
+
+/*
+ * __txn_xa_begin --
+ *	XA version of txn_begin.
+ *
+ * PUBLIC: int __txn_xa_begin __P((DB_ENV *, DB_TXN *));
+ */
+int
+__txn_xa_begin(dbenv, txn)
+	DB_ENV *dbenv;
+	DB_TXN *txn;
+{
+	TXN_PANIC_CHECK(dbenv->tx_info);
+
+	memset(txn, 0, sizeof(DB_TXN));
+
+	txn->mgrp = dbenv->tx_info;
+
+	return (__txn_begin(txn));
+}
+
+/*
+ * __txn_begin --
+ *	Normal DB version of txn_begin.
+ */
+static int
+__txn_begin(txn)
+	DB_TXN *txn;
+{
+	DB_LSN begin_lsn;
+	DB_TXNMGR *mgr;
+	TXN_DETAIL *td;
+	size_t off;
+	u_int32_t id;
+	int ret;
 
 	/*
 	 * We do not have to write begin records (and if we do not, then we
@@ -244,65 +307,67 @@ txn_begin(tmgrp, parent, txnpp)
 	 * we do need to find the current LSN so that we can store it in the
 	 * transaction structure, so we can know where to take checkpoints.
 	 */
-	if (tmgrp->dbenv->lg_info != NULL && (ret =
-	    log_put(tmgrp->dbenv->lg_info, &begin_lsn, NULL, DB_CURLSN)) != 0)
+	mgr = txn->mgrp;
+	if (mgr->dbenv->lg_info != NULL && (ret =
+	    log_put(mgr->dbenv->lg_info, &begin_lsn, NULL, DB_CURLSN)) != 0)
 		goto err2;
 
-	LOCK_TXNREGION(tmgrp);
+	LOCK_TXNREGION(mgr);
 
 	/* Make sure that last_txnid is not going to wrap around. */
-	if (tmgrp->region->last_txnid == TXN_INVALID) {
-		__db_err(tmgrp->dbenv, "txn_begin: %s  %s",
+	if (mgr->region->last_txnid == TXN_INVALID) {
+		__db_err(mgr->dbenv, "txn_begin: %s  %s",
 		    "Transaction ID wrapping.",
 		    "Snapshot your database and start a new log.");
 		ret = EINVAL;
 		goto err1;
 	}
 
-	if ((ret = __txn_validate_region(tmgrp)) != 0)
+	if ((ret = __txn_validate_region(mgr)) != 0)
 		goto err1;
 
 	/* Allocate a new transaction detail structure. */
-	if ((ret = __db_shalloc(tmgrp->mem, sizeof(TXN_DETAIL), 0, &txnp)) != 0
-	    && ret == ENOMEM && (ret = __txn_grow_region(tmgrp)) == 0)
-	    	ret = __db_shalloc(tmgrp->mem, sizeof(TXN_DETAIL), 0, &txnp);
+	if ((ret = __db_shalloc(mgr->mem, sizeof(TXN_DETAIL), 0, &td)) != 0
+	    && ret == ENOMEM && (ret = __txn_grow_region(mgr)) == 0)
+	    	ret = __db_shalloc(mgr->mem, sizeof(TXN_DETAIL), 0, &td);
 	if (ret != 0)
 		goto err1;
 
 	/* Place transaction on active transaction list. */
-	SH_TAILQ_INSERT_HEAD(&tmgrp->region->active_txn,
-	    txnp, links, __txn_detail);
-
-	id = ++tmgrp->region->last_txnid;
-	tmgrp->region->nbegins++;
-
-	txnp->txnid = id;
-	txnp->begin_lsn = begin_lsn;
-	ZERO_LSN(txnp->last_lsn);
-	txnp->last_lock = 0;
-	txnp->status = TXN_RUNNING;
-	off = (u_int8_t *)txnp - (u_int8_t *)tmgrp->region;
-	UNLOCK_TXNREGION(tmgrp);
+	SH_TAILQ_INSERT_HEAD(&mgr->region->active_txn, td, links, __txn_detail);
+
+	id = ++mgr->region->last_txnid;
+	++mgr->region->nbegins;
+
+	td->txnid = id;
+	td->begin_lsn = begin_lsn;
+	ZERO_LSN(td->last_lsn);
+	td->last_lock = 0;
+	td->status = TXN_RUNNING;
+	if (txn->parent != NULL)
+		td->parent = txn->parent->off;
+	else
+		td->parent = 0;
 
-	ZERO_LSN(retp->last_lsn);
-	retp->txnid = id;
-	retp->parent = parent;
-	retp->mgrp = tmgrp;
-	retp->off = off;
+	off = (u_int8_t *)td - (u_int8_t *)mgr->region;
+	UNLOCK_TXNREGION(mgr);
+
+	ZERO_LSN(txn->last_lsn);
+	txn->txnid = id;
+	txn->off = off;
 
-	LOCK_TXNTHREAD(tmgrp);
-	TAILQ_INSERT_TAIL(&tmgrp->txn_chain, retp, links);
-	UNLOCK_TXNTHREAD(tmgrp);
+	if (F_ISSET(txn, TXN_MALLOC)) {
+		LOCK_TXNTHREAD(mgr);
+		TAILQ_INSERT_TAIL(&mgr->txn_chain, txn, links);
+		UNLOCK_TXNTHREAD(mgr);
+	}
 
-	*txnpp = retp;
 	return (0);
 
-err1:	UNLOCK_TXNREGION(tmgrp);
+err1:	UNLOCK_TXNREGION(mgr);
 
-err2:	__db_free(retp);
-	return (ret);
+err2:	return (ret);
 }
-
 /*
  * txn_commit --
  *	Commit a transaction.
@@ -312,21 +377,43 @@ txn_commit(txnp)
 	DB_TXN *txnp;
 {
 	DB_LOG *logp;
+	DB_TXNMGR *mgr;
 	int ret;
 
-	if ((ret = __txn_check_running(txnp)) != 0)
+	mgr = txnp->mgrp;
+
+	TXN_PANIC_CHECK(mgr);
+	if ((ret = __txn_check_running(txnp, NULL)) != 0)
 		return (ret);
 
 	/*
 	 * If there are any log records, write a log record and sync
-	 * the log, else do no log writes.
+	 * the log, else do no log writes.  If the commit is for a child
+	 * transaction, we do not need to commit the child synchronously
+	 * since if its parent aborts, it will abort too and its parent
+	 * (or ultimate ancestor) will write synchronously.
 	 */
-	if ((logp = txnp->mgrp->dbenv->lg_info) != NULL &&
-	    !IS_ZERO_LSN(txnp->last_lsn) &&
-	    (ret = __txn_regop_log(logp, txnp, &txnp->last_lsn,
-	    F_ISSET(txnp->mgrp, DB_TXN_NOSYNC) ? 0 : DB_FLUSH,
-	    TXN_COMMIT)) != 0)
-		return (ret);
+	if ((logp = mgr->dbenv->lg_info) != NULL &&
+	    !IS_ZERO_LSN(txnp->last_lsn)) {
+		if (txnp->parent == NULL)
+	    		ret = __txn_regop_log(logp, txnp, &txnp->last_lsn,
+			    F_ISSET(mgr, DB_TXN_NOSYNC) ? 0 : DB_FLUSH,
+			    TXN_COMMIT);
+		else
+	    		ret = __txn_child_log(logp, txnp, &txnp->last_lsn, 0,
+			    TXN_COMMIT, txnp->parent->txnid);
+		if (ret != 0)
+			return (ret);
+	}
+
+	/*
+	 * If this is the senior ancestor (i.e., it has no children), then we
+	 * can release all the child transactions since everyone is committing.
+	 * Then we can release this transaction.  If this is not the ultimate
+	 * ancestor, then we can neither free it or its children.
+	 */
+	if (txnp->parent == NULL)
+		__txn_freekids(txnp);
 
 	return (__txn_end(txnp, 1));
 }
@@ -340,10 +427,17 @@ txn_abort(txnp)
 	DB_TXN *txnp;
 {
 	int ret;
+	DB_TXN *kids;
 
-	if ((ret = __txn_check_running(txnp)) != 0)
+	TXN_PANIC_CHECK(txnp->mgrp);
+	if ((ret = __txn_check_running(txnp, NULL)) != 0)
 		return (ret);
 
+	for (kids = TAILQ_FIRST(&txnp->kids);
+	    kids != NULL;
+	    kids = TAILQ_FIRST(&txnp->kids))
+		txn_abort(kids);
+
 	if ((ret = __txn_undo(txnp)) != 0) {
 		__db_err(txnp->mgrp->dbenv,
 		    "txn_abort: Log undo failed %s", strerror(ret));
@@ -353,30 +447,45 @@ txn_abort(txnp)
 }
 
 /*
- * Flush the log so a future commit is guaranteed to succeed.
+ * txn_prepare --
+ *	Flush the log so a future commit is guaranteed to succeed.
  */
 int
 txn_prepare(txnp)
 	DB_TXN *txnp;
 {
-	TXN_DETAIL *tp;
+	DBT xid;
+	DB_ENV *dbenv;
+	TXN_DETAIL *td;
 	int ret;
 
-	if ((ret = __txn_check_running(txnp)) != 0)
+	if ((ret = __txn_check_running(txnp, &td)) != 0)
 		return (ret);
 
-	if (txnp->mgrp->dbenv->lg_info != NULL) {
-		if ((ret = log_flush(txnp->mgrp->dbenv->lg_info,
-		    &txnp->last_lsn)) != 0)
-			__db_err(txnp->mgrp->dbenv,
-			    "txn_prepare: log_flush failed %s\n",
-			    strerror(ret));
+	dbenv = txnp->mgrp->dbenv;
+	memset(&xid, 0, sizeof(xid));
+	xid.data = td->xid;
+	/*
+	 * We indicate that a transaction is an XA transaction by putting
+	 * a valid size in the xid.size fiels.  XA requires that the transaction
+	 * be either ENDED or SUSPENDED when prepare is called, so we know
+	 * that if the xa_status isn't in one of those states, but we are
+	 * calling prepare that we are not an XA transaction.
+	 */
+	xid.size =
+	    td->xa_status != TXN_XA_ENDED && td->xa_status != TXN_XA_SUSPENDED ?
+	    0 : sizeof(td->xid);
+	if (dbenv->lg_info != NULL &&
+	    (ret = __txn_xa_regop_log(dbenv->lg_info, txnp, &txnp->last_lsn,
+	    F_ISSET(txnp->mgrp, DB_TXN_NOSYNC) ? 0 : DB_FLUSH, TXN_PREPARE,
+	    &xid, td->format, td->gtrid, td->bqual, &td->begin_lsn)) != 0) {
+		__db_err(dbenv,
+		    "txn_prepare: log_write failed %s\n", strerror(ret));
 		return (ret);
 	}
 
 	LOCK_TXNTHREAD(txnp->mgrp);
-	tp = (TXN_DETAIL *)((u_int8_t *)txnp->mgrp->region + txnp->off);
-	tp->status = TXN_PREPARED;
+	td->status = TXN_PREPARED;
 	UNLOCK_TXNTHREAD(txnp->mgrp);
 	return (ret);
 }
@@ -402,6 +511,8 @@ txn_close(tmgrp)
 	DB_TXN *txnp;
 	int ret, t_ret;
 
+	TXN_PANIC_CHECK(tmgrp);
+
 	ret = 0;
 
 	/*
@@ -431,8 +542,8 @@ txn_close(tmgrp)
 		ret = t_ret;
 
 	if (tmgrp->reginfo.path != NULL)
-		FREES(tmgrp->reginfo.path);
-	FREE(tmgrp, sizeof(*tmgrp));
+		__os_freestr(tmgrp->reginfo.path);
+	__os_free(tmgrp, sizeof(*tmgrp));
 
 	return (ret);
 }
@@ -453,12 +564,12 @@ txn_unlink(path, force, dbenv)
 	memset(&reginfo, 0, sizeof(reginfo));
 	reginfo.dbenv = dbenv;
 	reginfo.appname = DB_APP_NONE;
-	if (path != NULL && (reginfo.path = (char *)__db_strdup(path)) == NULL)
-		return (ENOMEM);
+	if (path != NULL && (ret = __os_strdup(path, &reginfo.path)) != 0)
+		return (ret);
 	reginfo.file = DEFAULT_TXN_FILE;
 	ret = __db_runlink(&reginfo, force);
 	if (reginfo.path != NULL)
-		FREES(reginfo.path);
+		__os_freestr(reginfo.path);
 	return (ret);
 }
 
@@ -468,16 +579,23 @@ txn_unlink(path, force, dbenv)
  * Return 0 if the txnp is reasonable, otherwise returns EINVAL.
  */
 static int
-__txn_check_running(txnp)
+__txn_check_running(txnp, tdp)
 	const DB_TXN *txnp;
+	TXN_DETAIL **tdp;
 {
 	TXN_DETAIL *tp;
 
 	tp = NULL;
 	if (txnp != NULL && txnp->mgrp != NULL && txnp->mgrp->region != NULL) {
 		tp = (TXN_DETAIL *)((u_int8_t *)txnp->mgrp->region + txnp->off);
-		if (tp->status != TXN_RUNNING)
+		/*
+		 * Child transactions could be marked committed which is OK.
+		 */
+		if (tp->status != TXN_RUNNING &&
+		    tp->status != TXN_PREPARED && tp->status != TXN_COMMITTED)
 			tp = NULL;
+		if (tdp != NULL)
+			*tdp = tp;
 	}
 
 	return (tp == NULL ? EINVAL : 0);
@@ -488,25 +606,22 @@ __txn_end(txnp, is_commit)
 	DB_TXN *txnp;
 	int is_commit;
 {
+	DB_LOCKREQ request;
 	DB_TXNMGR *mgr;
 	TXN_DETAIL *tp;
-	DB_LOCKREQ request;
-	int ret;
 	u_int32_t locker;
+	int ret;
 
 	mgr = txnp->mgrp;
 
-	LOCK_TXNTHREAD(mgr);
-	TAILQ_REMOVE(&mgr->txn_chain, txnp, links);
-	UNLOCK_TXNTHREAD(mgr);
-
 	/* Release the locks. */
 	locker = txnp->txnid;
-	request.op = DB_LOCK_PUT_ALL;
+	request.op = txnp->parent == NULL ||
+	    is_commit == 0 ? DB_LOCK_PUT_ALL : DB_LOCK_INHERIT;
 
 	if (mgr->dbenv->lk_info) {
-		ret = lock_vec(mgr->dbenv->lk_info, locker, 0,
-		    &request, 1, NULL);
+		ret =
+		    lock_tvec(mgr->dbenv->lk_info, txnp, 0, &request, 1, NULL);
 		if (ret != 0 && (ret != DB_LOCK_DEADLOCK || is_commit)) {
 			__db_err(mgr->dbenv, "%s: release locks failed %s",
 			    is_commit ? "txn_commit" : "txn_abort",
@@ -517,16 +632,44 @@ __txn_end(txnp, is_commit)
 
 	/* End the transaction. */
 	LOCK_TXNREGION(mgr);
+
+	/*
+	 * Child transactions that are committing cannot be released until
+	 * the parent commits, since the parent may abort, causing the child
+	 * to abort as well.
+	 */
 	tp = (TXN_DETAIL *)((u_int8_t *)mgr->region + txnp->off);
-	SH_TAILQ_REMOVE(&mgr->region->active_txn, tp, links, __txn_detail);
-	__db_shalloc_free(mgr->mem, tp);
+	if (txnp->parent == NULL || !is_commit) {
+		SH_TAILQ_REMOVE(&mgr->region->active_txn,
+		    tp, links, __txn_detail);
+
+		__db_shalloc_free(mgr->mem, tp);
+	} else
+		tp->status = is_commit ? TXN_COMMITTED : TXN_ABORTED;
+
 	if (is_commit)
 		mgr->region->ncommits++;
 	else
 		mgr->region->naborts++;
+
 	UNLOCK_TXNREGION(mgr);
 
-	FREE(txnp, sizeof(*txnp));
+	/*
+	 * If the transaction aborted, we can remove it from its parent links.
+	 * If it committed, then we need to leave it on, since the parent can
+	 * still abort.
+	 */
+	if (txnp->parent != NULL && !is_commit)
+		TAILQ_REMOVE(&txnp->parent->kids, txnp, klinks);
+
+	/* Free the space. */
+	if (F_ISSET(txnp, TXN_MALLOC) && (txnp->parent == NULL || !is_commit)) {
+		LOCK_TXNTHREAD(mgr);
+		TAILQ_REMOVE(&mgr->txn_chain, txnp, links);
+		UNLOCK_TXNTHREAD(mgr);
+
+		__os_free(txnp, sizeof(*txnp));
+	}
 
 	return (0);
 }
@@ -571,7 +714,7 @@ __txn_undo(txnp)
 			ret =
 			    mgr->recover(logp, &rdbt, &key_lsn, TXN_UNDO, NULL);
 			if (F_ISSET(logp, DB_AM_THREAD) && rdbt.data != NULL) {
-				__db_free(rdbt.data);
+				__os_free(rdbt.data, rdbt.size);
 				rdbt.data = NULL;
 			}
 		}
@@ -597,13 +740,15 @@ txn_checkpoint(mgr, kbytes, minutes)
 	const DB_TXNMGR *mgr;
 	u_int32_t kbytes, minutes;
 {
-	TXN_DETAIL *txnp;
-	DB_LSN ckp_lsn, last_ckp;
 	DB_LOG *dblp;
-	u_int32_t kbytes_written;
+	DB_LSN ckp_lsn, sync_lsn, last_ckp;
+	TXN_DETAIL *txnp;
 	time_t last_ckp_time, now;
+	u_int32_t kbytes_written;
 	int ret;
 
+	TXN_PANIC_CHECK(mgr);
+
 	/*
 	 * Check if we need to run recovery.
 	 */
@@ -672,8 +817,13 @@ do_ckp:
 	mgr->region->pending_ckp = ckp_lsn;
 	UNLOCK_TXNREGION(mgr);
 
+	/*
+	 * memp_sync may change the lsn you pass it, so don't pass it
+	 * the actual ckp_lsn, pass it a temp instead.
+	 */
+	sync_lsn = ckp_lsn;
 	if (mgr->dbenv->mp_info != NULL &&
-	    (ret = memp_sync(mgr->dbenv->mp_info, &ckp_lsn)) != 0) {
+	    (ret = memp_sync(mgr->dbenv->mp_info, &sync_lsn)) != 0) {
 		/*
 		 * ret == DB_INCOMPLETE means that there are still buffers to
 		 * flush, the checkpoint is not complete.  Wait and try again.
@@ -776,6 +926,9 @@ txn_stat(mgr, statp, db_malloc)
 	TXN_DETAIL *txnp;
 	size_t nbytes;
 	u_int32_t nactive, ndx;
+	int ret;
+
+	TXN_PANIC_CHECK(mgr);
 
 	LOCK_TXNREGION(mgr);
 	nactive = mgr->region->nbegins -
@@ -787,13 +940,8 @@ txn_stat(mgr, statp, db_malloc)
 	 * that have been created since we unlocked the region.
 	 */
 	nbytes = sizeof(DB_TXN_STAT) + sizeof(DB_TXN_ACTIVE) * (nactive + 200);
-	if (db_malloc == NULL)
-		stats = (DB_TXN_STAT *)__db_malloc(nbytes);
-	else
-		stats = (DB_TXN_STAT *)db_malloc(nbytes);
-
-	if (stats == NULL)
-		return (ENOMEM);
+	if ((ret = __os_malloc(nbytes, db_malloc, &stats)) != 0)
+		return (ret);
 
 	LOCK_TXNREGION(mgr);
 	stats->st_last_txnid = mgr->region->last_txnid;
@@ -831,3 +979,68 @@ txn_stat(mgr, statp, db_malloc)
 	*statp = stats;
 	return (0);
 }
+
+static void
+__txn_freekids(txnp)
+	DB_TXN *txnp;
+{
+	DB_TXNMGR *mgr;
+	TXN_DETAIL *tp;
+	DB_TXN *kids;
+
+	mgr = txnp->mgrp;
+
+	for (kids = TAILQ_FIRST(&txnp->kids);
+	    kids != NULL;
+	    kids = TAILQ_FIRST(&txnp->kids)) {
+		/* Free any children of this transaction. */
+		__txn_freekids(kids);
+
+		/* Free the transaction detail in the region. */
+		LOCK_TXNREGION(mgr);
+		tp = (TXN_DETAIL *)((u_int8_t *)mgr->region + kids->off);
+		SH_TAILQ_REMOVE(&mgr->region->active_txn,
+		    tp, links, __txn_detail);
+
+		__db_shalloc_free(mgr->mem, tp);
+		UNLOCK_TXNREGION(mgr);
+
+		/* Now remove from its parent. */
+		TAILQ_REMOVE(&txnp->kids, kids, klinks);
+		if (F_ISSET(txnp, TXN_MALLOC)) {
+			LOCK_TXNTHREAD(mgr);
+			TAILQ_REMOVE(&mgr->txn_chain, kids, links);
+			UNLOCK_TXNTHREAD(mgr);
+			__os_free(kids, sizeof(*kids));
+		}
+	}
+}
+
+/*
+ * __txn_is_ancestor --
+ * 	Determine if a transaction is an ancestor of another transaction.
+ * This is used during lock promotion when we do not have the per-process
+ * data structures that link parents together.  Instead, we'll have to
+ * follow the links in the transaction region.
+ *
+ * PUBLIC: int __txn_is_ancestor __P((DB_TXNMGR *, size_t, size_t));
+ */
+int
+__txn_is_ancestor(mgr, hold_off, req_off)
+	DB_TXNMGR *mgr;
+	size_t hold_off, req_off;
+{
+	TXN_DETAIL *hold_tp, *req_tp;
+
+	hold_tp = (TXN_DETAIL *)((u_int8_t *)mgr->region + hold_off);
+	req_tp = (TXN_DETAIL *)((u_int8_t *)mgr->region + req_off);
+
+	while (req_tp->parent != 0) {
+		req_tp =
+		    (TXN_DETAIL *)((u_int8_t *)mgr->region + req_tp->parent);
+		if (req_tp->txnid == hold_tp->txnid)
+			return (1);
+	}
+
+	return (0);
+}
diff --git a/db2/txn/txn.src b/db2/txn/txn.src
index 04809b69d6..c9614f6d6b 100644
--- a/db2/txn/txn.src
+++ b/db2/txn/txn.src
@@ -4,26 +4,52 @@
  * Copyright (c) 1996, 1997, 1998
  *	Sleepycat Software.  All rights reserved.
  *
- *	@(#)txn.src	10.3 (Sleepycat) 4/10/98
+ *	@(#)txn.src	10.6 (Sleepycat) 1/3/99
  */
 
 PREFIX	txn
 
 /*
- * Everything except for checkpointing takes the same logging routine.
+ * This is the standard log operation for commit.
  */
 BEGIN	regop
 ARG	opcode		u_int32_t	lu
 END
 
 /*
- * This is the checkpoint record. It contains the lsn that the checkpoint
- * guarantees and a pointer to the last checkpoint so that we can walk
- * backwards by checkpoint.
+ * This is the checkpoint record.  It contains the lsn that the checkpoint
+ * guarantees and a pointer to the last checkpoint so we can walk backwards
+ * by checkpoint.
+ *
  * ckp_lsn:
+ *	The lsn in the log of the most recent point at which all begun
+ *	transactions have been aborted.  This is the point for which
+ *	the checkpoint is relevant.
  * last_ckp:
+ *	The previous checkpoint.
  */
 BEGIN	ckp
 POINTER	ckp_lsn		DB_LSN *	lu
 POINTER	last_ckp	DB_LSN *	lu
 END
+
+/*
+ * This is the standard log operation for prepare (since right now
+ * we only use prepare in an XA environment).
+ */
+BEGIN	xa_regop
+ARG	opcode		u_int32_t	lu
+DBT	xid		DBT		s
+ARG	formatID	int32_t		ld
+ARG	gtrid		u_int32_t	u
+ARG	bqual		u_int32_t	u
+POINTER	begin_lsn	DB_LSN *	lu		
+END
+
+/*
+ * This is the log operation for a child commit.
+ */
+BEGIN	child
+ARG	opcode		u_int32_t	lu
+ARG	parent		u_int32_t	lu
+END
diff --git a/db2/txn/txn_auto.c b/db2/txn/txn_auto.c
index f03a52991f..e6d431f089 100644
--- a/db2/txn/txn_auto.c
+++ b/db2/txn/txn_auto.c
@@ -10,7 +10,6 @@
 #endif
 
 #include "db_int.h"
-#include "shqueue.h"
 #include "db_page.h"
 #include "db_dispatch.h"
 #include "txn.h"
@@ -37,15 +36,14 @@ int __txn_regop_log(logp, txnid, ret_lsnp, flags,
 	rectype = DB_txn_regop;
 	txn_num = txnid == NULL ? 0 : txnid->txnid;
 	if (txnid == NULL) {
-		null_lsn.file = 0;
-		null_lsn.offset = 0;
+		ZERO_LSN(null_lsn);
 		lsnp = &null_lsn;
 	} else
 		lsnp = &txnid->last_lsn;
 	logrec.size = sizeof(rectype) + sizeof(txn_num) + sizeof(DB_LSN)
 	    + sizeof(opcode);
-	if ((logrec.data = (void *)__db_malloc(logrec.size)) == NULL)
-		return (ENOMEM);
+	if ((ret = __os_malloc(logrec.size, NULL, &logrec.data)) != 0)
+		return (ret);
 
 	bp = logrec.data;
 	memcpy(bp, &rectype, sizeof(rectype));
@@ -63,7 +61,7 @@ int __txn_regop_log(logp, txnid, ret_lsnp, flags,
 	ret = log_put(logp, ret_lsnp, (DBT *)&logrec, flags);
 	if (txnid != NULL)
 		txnid->last_lsn = *ret_lsnp;
-	__db_free(logrec.data);
+	__os_free(logrec.data, 0);
 	return (ret);
 }
 
@@ -101,7 +99,7 @@ __txn_regop_print(notused1, dbtp, lsnp, notused2, notused3)
 	    (u_long)argp->prev_lsn.offset);
 	printf("\topcode: %lu\n", (u_long)argp->opcode);
 	printf("\n");
-	__db_free(argp);
+	__os_free(argp, 0);
 	return (0);
 }
 
@@ -115,11 +113,12 @@ __txn_regop_read(recbuf, argpp)
 {
 	__txn_regop_args *argp;
 	u_int8_t *bp;
+	int ret;
 
-	argp = (__txn_regop_args *)__db_malloc(sizeof(__txn_regop_args) +
-	    sizeof(DB_TXN));
-	if (argp == NULL)
-		return (ENOMEM);
+	ret = __os_malloc(sizeof(__txn_regop_args) +
+	    sizeof(DB_TXN), NULL, &argp);
+	if (ret != 0)
+		return (ret);
 	argp->txnid = (DB_TXN *)&argp[1];
 	bp = recbuf;
 	memcpy(&argp->type, bp, sizeof(argp->type));
@@ -157,16 +156,15 @@ int __txn_ckp_log(logp, txnid, ret_lsnp, flags,
 	rectype = DB_txn_ckp;
 	txn_num = txnid == NULL ? 0 : txnid->txnid;
 	if (txnid == NULL) {
-		null_lsn.file = 0;
-		null_lsn.offset = 0;
+		ZERO_LSN(null_lsn);
 		lsnp = &null_lsn;
 	} else
 		lsnp = &txnid->last_lsn;
 	logrec.size = sizeof(rectype) + sizeof(txn_num) + sizeof(DB_LSN)
 	    + sizeof(*ckp_lsn)
 	    + sizeof(*last_ckp);
-	if ((logrec.data = (void *)__db_malloc(logrec.size)) == NULL)
-		return (ENOMEM);
+	if ((ret = __os_malloc(logrec.size, NULL, &logrec.data)) != 0)
+		return (ret);
 
 	bp = logrec.data;
 	memcpy(bp, &rectype, sizeof(rectype));
@@ -192,7 +190,7 @@ int __txn_ckp_log(logp, txnid, ret_lsnp, flags,
 	ret = log_put(logp, ret_lsnp, (DBT *)&logrec, flags);
 	if (txnid != NULL)
 		txnid->last_lsn = *ret_lsnp;
-	__db_free(logrec.data);
+	__os_free(logrec.data, 0);
 	return (ret);
 }
 
@@ -233,7 +231,7 @@ __txn_ckp_print(notused1, dbtp, lsnp, notused2, notused3)
 	printf("\tlast_ckp: [%lu][%lu]\n",
 	    (u_long)argp->last_ckp.file, (u_long)argp->last_ckp.offset);
 	printf("\n");
-	__db_free(argp);
+	__os_free(argp, 0);
 	return (0);
 }
 
@@ -247,11 +245,12 @@ __txn_ckp_read(recbuf, argpp)
 {
 	__txn_ckp_args *argp;
 	u_int8_t *bp;
+	int ret;
 
-	argp = (__txn_ckp_args *)__db_malloc(sizeof(__txn_ckp_args) +
-	    sizeof(DB_TXN));
-	if (argp == NULL)
-		return (ENOMEM);
+	ret = __os_malloc(sizeof(__txn_ckp_args) +
+	    sizeof(DB_TXN), NULL, &argp);
+	if (ret != 0)
+		return (ret);
 	argp->txnid = (DB_TXN *)&argp[1];
 	bp = recbuf;
 	memcpy(&argp->type, bp, sizeof(argp->type));
@@ -269,6 +268,310 @@ __txn_ckp_read(recbuf, argpp)
 }
 
 /*
+ * PUBLIC: int __txn_xa_regop_log
+ * PUBLIC:     __P((DB_LOG *, DB_TXN *, DB_LSN *, u_int32_t,
+ * PUBLIC:     u_int32_t, const DBT *, int32_t, u_int32_t,
+ * PUBLIC:     u_int32_t, DB_LSN *));
+ */
+int __txn_xa_regop_log(logp, txnid, ret_lsnp, flags,
+	opcode, xid, formatID, gtrid, bqual, begin_lsn)
+	DB_LOG *logp;
+	DB_TXN *txnid;
+	DB_LSN *ret_lsnp;
+	u_int32_t flags;
+	u_int32_t opcode;
+	const DBT *xid;
+	int32_t formatID;
+	u_int32_t gtrid;
+	u_int32_t bqual;
+	DB_LSN * begin_lsn;
+{
+	DBT logrec;
+	DB_LSN *lsnp, null_lsn;
+	u_int32_t zero;
+	u_int32_t rectype, txn_num;
+	int ret;
+	u_int8_t *bp;
+
+	rectype = DB_txn_xa_regop;
+	txn_num = txnid == NULL ? 0 : txnid->txnid;
+	if (txnid == NULL) {
+		ZERO_LSN(null_lsn);
+		lsnp = &null_lsn;
+	} else
+		lsnp = &txnid->last_lsn;
+	logrec.size = sizeof(rectype) + sizeof(txn_num) + sizeof(DB_LSN)
+	    + sizeof(opcode)
+	    + sizeof(u_int32_t) + (xid == NULL ? 0 : xid->size)
+	    + sizeof(formatID)
+	    + sizeof(gtrid)
+	    + sizeof(bqual)
+	    + sizeof(*begin_lsn);
+	if ((ret = __os_malloc(logrec.size, NULL, &logrec.data)) != 0)
+		return (ret);
+
+	bp = logrec.data;
+	memcpy(bp, &rectype, sizeof(rectype));
+	bp += sizeof(rectype);
+	memcpy(bp, &txn_num, sizeof(txn_num));
+	bp += sizeof(txn_num);
+	memcpy(bp, lsnp, sizeof(DB_LSN));
+	bp += sizeof(DB_LSN);
+	memcpy(bp, &opcode, sizeof(opcode));
+	bp += sizeof(opcode);
+	if (xid == NULL) {
+		zero = 0;
+		memcpy(bp, &zero, sizeof(u_int32_t));
+		bp += sizeof(u_int32_t);
+	} else {
+		memcpy(bp, &xid->size, sizeof(xid->size));
+		bp += sizeof(xid->size);
+		memcpy(bp, xid->data, xid->size);
+		bp += xid->size;
+	}
+	memcpy(bp, &formatID, sizeof(formatID));
+	bp += sizeof(formatID);
+	memcpy(bp, &gtrid, sizeof(gtrid));
+	bp += sizeof(gtrid);
+	memcpy(bp, &bqual, sizeof(bqual));
+	bp += sizeof(bqual);
+	if (begin_lsn != NULL)
+		memcpy(bp, begin_lsn, sizeof(*begin_lsn));
+	else
+		memset(bp, 0, sizeof(*begin_lsn));
+	bp += sizeof(*begin_lsn);
+#ifdef DIAGNOSTIC
+	if ((u_int32_t)(bp - (u_int8_t *)logrec.data) != logrec.size)
+		fprintf(stderr, "Error in log record length");
+#endif
+	ret = log_put(logp, ret_lsnp, (DBT *)&logrec, flags);
+	if (txnid != NULL)
+		txnid->last_lsn = *ret_lsnp;
+	__os_free(logrec.data, 0);
+	return (ret);
+}
+
+/*
+ * PUBLIC: int __txn_xa_regop_print
+ * PUBLIC:    __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
+ */
+int
+__txn_xa_regop_print(notused1, dbtp, lsnp, notused2, notused3)
+	DB_LOG *notused1;
+	DBT *dbtp;
+	DB_LSN *lsnp;
+	int notused2;
+	void *notused3;
+{
+	__txn_xa_regop_args *argp;
+	u_int32_t i;
+	u_int ch;
+	int ret;
+
+	i = 0;
+	ch = 0;
+	notused1 = NULL;
+	notused2 = 0;
+	notused3 = NULL;
+
+	if ((ret = __txn_xa_regop_read(dbtp->data, &argp)) != 0)
+		return (ret);
+	printf("[%lu][%lu]txn_xa_regop: rec: %lu txnid %lx prevlsn [%lu][%lu]\n",
+	    (u_long)lsnp->file,
+	    (u_long)lsnp->offset,
+	    (u_long)argp->type,
+	    (u_long)argp->txnid->txnid,
+	    (u_long)argp->prev_lsn.file,
+	    (u_long)argp->prev_lsn.offset);
+	printf("\topcode: %lu\n", (u_long)argp->opcode);
+	printf("\txid: ");
+	for (i = 0; i < argp->xid.size; i++) {
+		ch = ((u_int8_t *)argp->xid.data)[i];
+		if (isprint(ch) || ch == 0xa)
+			putchar(ch);
+		else
+			printf("%#x ", ch);
+	}
+	printf("\n");
+	printf("\tformatID: %ld\n", (long)argp->formatID);
+	printf("\tgtrid: %u\n", argp->gtrid);
+	printf("\tbqual: %u\n", argp->bqual);
+	printf("\tbegin_lsn: [%lu][%lu]\n",
+	    (u_long)argp->begin_lsn.file, (u_long)argp->begin_lsn.offset);
+	printf("\n");
+	__os_free(argp, 0);
+	return (0);
+}
+
+/*
+ * PUBLIC: int __txn_xa_regop_read __P((void *, __txn_xa_regop_args **));
+ */
+int
+__txn_xa_regop_read(recbuf, argpp)
+	void *recbuf;
+	__txn_xa_regop_args **argpp;
+{
+	__txn_xa_regop_args *argp;
+	u_int8_t *bp;
+	int ret;
+
+	ret = __os_malloc(sizeof(__txn_xa_regop_args) +
+	    sizeof(DB_TXN), NULL, &argp);
+	if (ret != 0)
+		return (ret);
+	argp->txnid = (DB_TXN *)&argp[1];
+	bp = recbuf;
+	memcpy(&argp->type, bp, sizeof(argp->type));
+	bp += sizeof(argp->type);
+	memcpy(&argp->txnid->txnid,  bp, sizeof(argp->txnid->txnid));
+	bp += sizeof(argp->txnid->txnid);
+	memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN));
+	bp += sizeof(DB_LSN);
+	memcpy(&argp->opcode, bp, sizeof(argp->opcode));
+	bp += sizeof(argp->opcode);
+	memcpy(&argp->xid.size, bp, sizeof(u_int32_t));
+	bp += sizeof(u_int32_t);
+	argp->xid.data = bp;
+	bp += argp->xid.size;
+	memcpy(&argp->formatID, bp, sizeof(argp->formatID));
+	bp += sizeof(argp->formatID);
+	memcpy(&argp->gtrid, bp, sizeof(argp->gtrid));
+	bp += sizeof(argp->gtrid);
+	memcpy(&argp->bqual, bp, sizeof(argp->bqual));
+	bp += sizeof(argp->bqual);
+	memcpy(&argp->begin_lsn, bp,  sizeof(argp->begin_lsn));
+	bp += sizeof(argp->begin_lsn);
+	*argpp = argp;
+	return (0);
+}
+
+/*
+ * PUBLIC: int __txn_child_log
+ * PUBLIC:     __P((DB_LOG *, DB_TXN *, DB_LSN *, u_int32_t,
+ * PUBLIC:     u_int32_t, u_int32_t));
+ */
+int __txn_child_log(logp, txnid, ret_lsnp, flags,
+	opcode, parent)
+	DB_LOG *logp;
+	DB_TXN *txnid;
+	DB_LSN *ret_lsnp;
+	u_int32_t flags;
+	u_int32_t opcode;
+	u_int32_t parent;
+{
+	DBT logrec;
+	DB_LSN *lsnp, null_lsn;
+	u_int32_t rectype, txn_num;
+	int ret;
+	u_int8_t *bp;
+
+	rectype = DB_txn_child;
+	txn_num = txnid == NULL ? 0 : txnid->txnid;
+	if (txnid == NULL) {
+		ZERO_LSN(null_lsn);
+		lsnp = &null_lsn;
+	} else
+		lsnp = &txnid->last_lsn;
+	logrec.size = sizeof(rectype) + sizeof(txn_num) + sizeof(DB_LSN)
+	    + sizeof(opcode)
+	    + sizeof(parent);
+	if ((ret = __os_malloc(logrec.size, NULL, &logrec.data)) != 0)
+		return (ret);
+
+	bp = logrec.data;
+	memcpy(bp, &rectype, sizeof(rectype));
+	bp += sizeof(rectype);
+	memcpy(bp, &txn_num, sizeof(txn_num));
+	bp += sizeof(txn_num);
+	memcpy(bp, lsnp, sizeof(DB_LSN));
+	bp += sizeof(DB_LSN);
+	memcpy(bp, &opcode, sizeof(opcode));
+	bp += sizeof(opcode);
+	memcpy(bp, &parent, sizeof(parent));
+	bp += sizeof(parent);
+#ifdef DIAGNOSTIC
+	if ((u_int32_t)(bp - (u_int8_t *)logrec.data) != logrec.size)
+		fprintf(stderr, "Error in log record length");
+#endif
+	ret = log_put(logp, ret_lsnp, (DBT *)&logrec, flags);
+	if (txnid != NULL)
+		txnid->last_lsn = *ret_lsnp;
+	__os_free(logrec.data, 0);
+	return (ret);
+}
+
+/*
+ * PUBLIC: int __txn_child_print
+ * PUBLIC:    __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
+ */
+int
+__txn_child_print(notused1, dbtp, lsnp, notused2, notused3)
+	DB_LOG *notused1;
+	DBT *dbtp;
+	DB_LSN *lsnp;
+	int notused2;
+	void *notused3;
+{
+	__txn_child_args *argp;
+	u_int32_t i;
+	u_int ch;
+	int ret;
+
+	i = 0;
+	ch = 0;
+	notused1 = NULL;
+	notused2 = 0;
+	notused3 = NULL;
+
+	if ((ret = __txn_child_read(dbtp->data, &argp)) != 0)
+		return (ret);
+	printf("[%lu][%lu]txn_child: rec: %lu txnid %lx prevlsn [%lu][%lu]\n",
+	    (u_long)lsnp->file,
+	    (u_long)lsnp->offset,
+	    (u_long)argp->type,
+	    (u_long)argp->txnid->txnid,
+	    (u_long)argp->prev_lsn.file,
+	    (u_long)argp->prev_lsn.offset);
+	printf("\topcode: %lu\n", (u_long)argp->opcode);
+	printf("\tparent: %lu\n", (u_long)argp->parent);
+	printf("\n");
+	__os_free(argp, 0);
+	return (0);
+}
+
+/*
+ * PUBLIC: int __txn_child_read __P((void *, __txn_child_args **));
+ */
+int
+__txn_child_read(recbuf, argpp)
+	void *recbuf;
+	__txn_child_args **argpp;
+{
+	__txn_child_args *argp;
+	u_int8_t *bp;
+	int ret;
+
+	ret = __os_malloc(sizeof(__txn_child_args) +
+	    sizeof(DB_TXN), NULL, &argp);
+	if (ret != 0)
+		return (ret);
+	argp->txnid = (DB_TXN *)&argp[1];
+	bp = recbuf;
+	memcpy(&argp->type, bp, sizeof(argp->type));
+	bp += sizeof(argp->type);
+	memcpy(&argp->txnid->txnid,  bp, sizeof(argp->txnid->txnid));
+	bp += sizeof(argp->txnid->txnid);
+	memcpy(&argp->prev_lsn, bp, sizeof(DB_LSN));
+	bp += sizeof(DB_LSN);
+	memcpy(&argp->opcode, bp, sizeof(argp->opcode));
+	bp += sizeof(argp->opcode);
+	memcpy(&argp->parent, bp, sizeof(argp->parent));
+	bp += sizeof(argp->parent);
+	*argpp = argp;
+	return (0);
+}
+
+/*
  * PUBLIC: int __txn_init_print __P((DB_ENV *));
  */
 int
@@ -283,6 +586,12 @@ __txn_init_print(dbenv)
 	if ((ret = __db_add_recovery(dbenv,
 	    __txn_ckp_print, DB_txn_ckp)) != 0)
 		return (ret);
+	if ((ret = __db_add_recovery(dbenv,
+	    __txn_xa_regop_print, DB_txn_xa_regop)) != 0)
+		return (ret);
+	if ((ret = __db_add_recovery(dbenv,
+	    __txn_child_print, DB_txn_child)) != 0)
+		return (ret);
 	return (0);
 }
 
@@ -301,6 +610,12 @@ __txn_init_recover(dbenv)
 	if ((ret = __db_add_recovery(dbenv,
 	    __txn_ckp_recover, DB_txn_ckp)) != 0)
 		return (ret);
+	if ((ret = __db_add_recovery(dbenv,
+	    __txn_xa_regop_recover, DB_txn_xa_regop)) != 0)
+		return (ret);
+	if ((ret = __db_add_recovery(dbenv,
+	    __txn_child_recover, DB_txn_child)) != 0)
+		return (ret);
 	return (0);
 }
 
diff --git a/db2/txn/txn_rec.c b/db2/txn/txn_rec.c
index e53dc5f3b7..f21a0f92c8 100644
--- a/db2/txn/txn_rec.c
+++ b/db2/txn/txn_rec.c
@@ -40,7 +40,7 @@
 #include "config.h"
 
 #ifndef lint
-static const char sccsid[] = "@(#)txn_rec.c	10.11 (Sleepycat) 5/3/98";
+static const char sccsid[] = "@(#)txn_rec.c	10.15 (Sleepycat) 1/3/99";
 #endif /* not lint */
 
 #ifndef NO_SYSTEM_INCLUDES
@@ -54,10 +54,18 @@ static const char sccsid[] = "@(#)txn_rec.c	10.11 (Sleepycat) 5/3/98";
 #include "shqueue.h"
 #include "txn.h"
 #include "db_am.h"
+#include "log.h"
+#include "common_ext.h"
 
+static int __txn_restore_txn __P((DB_ENV *, DB_LSN *, __txn_xa_regop_args *));
+
+#define	IS_XA_TXN(R) (R->xid.size != 0)
+	
 /*
  * PUBLIC: int __txn_regop_recover
- * PUBLIC:     __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
+ * PUBLIC:    __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
+ *
+ * These records are only ever written for commits.
  */
 int
 __txn_regop_recover(logp, dbtp, lsnp, redo, info)
@@ -79,24 +87,80 @@ __txn_regop_recover(logp, dbtp, lsnp, redo, info)
 	if ((ret = __txn_regop_read(dbtp->data, &argp)) != 0)
 		return (ret);
 
-	switch (argp->opcode) {
-	case TXN_COMMIT:
-		if (__db_txnlist_find(info,
-		    argp->txnid->txnid) == DB_NOTFOUND)
-			__db_txnlist_add(info, argp->txnid->txnid);
-		break;
-	case TXN_PREPARE:	/* Nothing to do. */
-		/* Call __db_txnlist_find so that we update the maxid. */
-		(void)__db_txnlist_find(info, argp->txnid->txnid);
-		break;
-	default:
+	if (argp->opcode != TXN_COMMIT)
+		ret = EINVAL;
+	else
+		if (__db_txnlist_find(info, argp->txnid->txnid) == DB_NOTFOUND)
+			ret = __db_txnlist_add(info, argp->txnid->txnid);
+
+	if (ret == 0)
+		*lsnp = argp->prev_lsn;
+	__os_free(argp, 0);
+
+	return (ret);
+}
+
+/*
+ * PUBLIC: int __txn_xa_regop_recover
+ * PUBLIC:    __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
+ *
+ * These records are only ever written for prepares.
+ */
+int
+__txn_xa_regop_recover(logp, dbtp, lsnp, redo, info)
+	DB_LOG *logp;
+	DBT *dbtp;
+	DB_LSN *lsnp;
+	int redo;
+	void *info;
+{
+	__txn_xa_regop_args *argp;
+	int ret;
+
+#ifdef DEBUG_RECOVER
+	(void)__txn_xa_regop_print(logp, dbtp, lsnp, redo, info);
+#endif
+	COMPQUIET(redo, 0);
+	COMPQUIET(logp, NULL);
+
+	if ((ret = __txn_xa_regop_read(dbtp->data, &argp)) != 0)
+		return (ret);
+
+	if (argp->opcode != TXN_PREPARE)
 		ret = EINVAL;
-		break;
+	else {
+		/*
+		 * Whether we are in XA or not, we need to call
+		 * __db_txnlist_find so that we update the maxid.
+		 * If this is an XA transaction, then we treat
+		 * prepares like commits so that we roll forward to
+		 * a point where we can handle commit/abort calls
+		 * from the TMS.  If this isn't XA, then a prepare
+		 * is treated like a No-op; we only care about the
+		 * commit.
+		 */
+		ret = __db_txnlist_find(info, argp->txnid->txnid);
+		if (IS_XA_TXN(argp) && ret == DB_NOTFOUND) {
+			/*
+			 * This is an XA prepared, but not yet committed
+			 * transaction.  We need to add it to the
+			 * transaction list, so that it gets rolled
+			 * forward. We also have to add it to the region's
+			 * internal state so it can be properly aborted
+			 * or recovered.
+			 */
+			ret = __db_txnlist_add(info, argp->txnid->txnid);
+			if (ret == 0)
+				ret = __txn_restore_txn(logp->dbenv,
+				    lsnp, argp);
+		}
 	}
 
-	*lsnp = argp->prev_lsn;
-	__db_free(argp);
-	return (0);
+	if (ret == 0)
+		*lsnp = argp->prev_lsn;
+	__os_free(argp, 0);
+
+	return (ret);
 }
 
 /*
@@ -130,7 +194,103 @@ __txn_ckp_recover(logp, dbtp, lsnp, redo, info)
 	if (argp->ckp_lsn.file == lsnp->file &&
 	    argp->ckp_lsn.offset == lsnp->offset)
 		__db_txnlist_gen(info, redo ? -1 : 1);
+
 	*lsnp = argp->last_ckp;
-	__db_free(argp);
+	__os_free(argp, 0);
 	return (DB_TXN_CKP);
 }
+
+/*
+ * __txn_child_recover
+ *	Recover a commit record for a child transaction.
+ *
+ * PUBLIC: int __txn_child_recover
+ * PUBLIC:    __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
+ */
+int
+__txn_child_recover(logp, dbtp, lsnp, redo, info)
+	DB_LOG *logp;
+	DBT *dbtp;
+	DB_LSN *lsnp;
+	int redo;
+	void *info;
+{
+	__txn_child_args *argp;
+	int ret;
+
+#ifdef DEBUG_RECOVER
+	(void)__txn_child_print(logp, dbtp, lsnp, redo, info);
+#endif
+	COMPQUIET(redo, 0);
+	COMPQUIET(logp, NULL);
+
+	if ((ret = __txn_child_read(dbtp->data, &argp)) != 0)
+		return (ret);
+
+	/*
+	 * We count the child as committed only if its parent committed.
+	 * So, if we are not yet in the transaction list, but our parent
+	 * is, then we should go ahead and commit.
+	 */
+	if (argp->opcode != TXN_COMMIT)
+		ret = EINVAL;
+	else
+		if (__db_txnlist_find(info, argp->parent) == 0 &&
+		    __db_txnlist_find(info, argp->txnid->txnid) == DB_NOTFOUND)
+			ret = __db_txnlist_add(info, argp->txnid->txnid);
+
+	if (ret == 0)
+		*lsnp = argp->prev_lsn;
+	__os_free(argp, 0);
+
+	return (ret);
+}
+
+/*
+ * __txn_restore_txn --
+ *	Using only during XA recovery.  If we find any transactions that are
+ * prepared, but not yet committed, then we need to restore the transaction's
+ * state into the shared region, because the TM is going to issue a txn_abort
+ * or txn_commit and we need to respond correctly.
+ *
+ * lsnp is the LSN of the returned LSN
+ * argp is the perpare record (in an appropriate structure)
+ */
+static int
+__txn_restore_txn(dbenv, lsnp, argp)
+	DB_ENV *dbenv;
+	DB_LSN *lsnp;
+	__txn_xa_regop_args *argp;
+{
+	DB_TXNMGR *mgr;
+	TXN_DETAIL *td;
+	int ret;
+
+	if (argp->xid.size == 0)
+		return(0);
+
+	mgr = dbenv->tx_info;
+	LOCK_TXNREGION(mgr);
+
+	/* Allocate a new transaction detail structure. */
+	if ((ret = __db_shalloc(mgr->mem, sizeof(TXN_DETAIL), 0, &td)) != 0)
+		return (ret);
+
+	/* Place transaction on active transaction list. */
+	SH_TAILQ_INSERT_HEAD(&mgr->region->active_txn, td, links, __txn_detail);
+
+	td->txnid = argp->txnid->txnid;
+	td->begin_lsn = argp->begin_lsn;
+	td->last_lsn = *lsnp;
+	td->last_lock = 0;
+	td->parent = 0;
+	td->status = TXN_PREPARED;
+	td->xa_status = TXN_XA_PREPARED;
+	memcpy(td->xid, argp->xid.data, argp->xid.size);
+	td->bqual = argp->bqual;
+	td->gtrid = argp->gtrid;
+	td->format = argp->formatID;
+
+	UNLOCK_TXNREGION(mgr);
+	return (0);
+}