1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1996, 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A/*
1N/A * Copyright (c) 1995, 1996
1N/A * The President and Fellows of Harvard University. All rights reserved.
1N/A *
1N/A * This code is derived from software contributed to Berkeley by
1N/A * Margo Seltzer.
1N/A *
1N/A * Redistribution and use in source and binary forms, with or without
1N/A * modification, are permitted provided that the following conditions
1N/A * are met:
1N/A * 1. Redistributions of source code must retain the above copyright
1N/A * notice, this list of conditions and the following disclaimer.
1N/A * 2. Redistributions in binary form must reproduce the above copyright
1N/A * notice, this list of conditions and the following disclaimer in the
1N/A * documentation and/or other materials provided with the distribution.
1N/A * 3. All advertising materials mentioning features or use of this software
1N/A * must display the following acknowledgement:
1N/A * This product includes software developed by the University of
1N/A * California, Berkeley and its contributors.
1N/A * 4. Neither the name of the University nor the names of its contributors
1N/A * may be used to endorse or promote products derived from this software
1N/A * without specific prior written permission.
1N/A *
1N/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1N/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1N/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1N/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1N/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1N/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1N/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1N/A * SUCH DAMAGE.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)txn.c 10.66 (Sleepycat) 1/3/99";
1N/A#endif /* not lint */
1N/A
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <string.h>
1N/A#include <time.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "shqueue.h"
1N/A#include "db_page.h"
1N/A#include "db_shash.h"
1N/A#include "txn.h"
1N/A#include "db_dispatch.h"
1N/A#include "lock.h"
1N/A#include "log.h"
1N/A#include "db_am.h"
1N/A#include "common_ext.h"
1N/A
1N/Astatic int __txn_begin __P((DB_TXN *));
1N/Astatic int __txn_check_running __P((const DB_TXN *, TXN_DETAIL **));
1N/Astatic int __txn_end __P((DB_TXN *, int));
1N/Astatic void __txn_freekids __P((DB_TXN *));
1N/Astatic int __txn_grow_region __P((DB_TXNMGR *));
1N/Astatic int __txn_init __P((DB_TXNREGION *));
1N/Astatic int __txn_undo __P((DB_TXN *));
1N/Astatic int __txn_validate_region __P((DB_TXNMGR *));
1N/A
1N/A/*
1N/A * This file contains the top level routines of the transaction library.
1N/A * It assumes that a lock manager and log manager that conform to the db_log(3)
1N/A * and db_lock(3) interfaces exist.
1N/A *
1N/A * Initialize a transaction region in shared memory.
1N/A * Return 0 on success, errno on failure.
1N/A */
1N/Astatic int
1N/A__txn_init(txn_region)
1N/A DB_TXNREGION *txn_region;
1N/A{
1N/A time_t now;
1N/A
1N/A (void)time(&now);
1N/A
1N/A /* maxtxns is already initialized. */
1N/A txn_region->magic = DB_TXNMAGIC;
1N/A txn_region->version = DB_TXNVERSION;
1N/A txn_region->last_txnid = TXN_MINIMUM;
1N/A /*
1N/A * XXX
1N/A * If we ever do more types of locking and logging, this changes.
1N/A */
1N/A txn_region->logtype = 0;
1N/A txn_region->locktype = 0;
1N/A txn_region->time_ckp = now;
1N/A ZERO_LSN(txn_region->last_ckp);
1N/A ZERO_LSN(txn_region->pending_ckp);
1N/A SH_TAILQ_INIT(&txn_region->active_txn);
1N/A __db_shalloc_init((void *)&txn_region[1],
1N/A TXN_REGION_SIZE(txn_region->maxtxns) - sizeof(DB_TXNREGION));
1N/A
1N/A return (0);
1N/A}
1N/A
1N/Aint
1N/Atxn_open(path, flags, mode, dbenv, mgrpp)
1N/A const char *path;
1N/A u_int32_t flags;
1N/A int mode;
1N/A DB_ENV *dbenv;
1N/A DB_TXNMGR **mgrpp;
1N/A{
1N/A DB_TXNMGR *tmgrp;
1N/A u_int32_t maxtxns;
1N/A int ret;
1N/A
1N/A /* Validate arguments. */
1N/A if (dbenv == NULL)
1N/A return (EINVAL);
1N/A#ifdef HAVE_SPINLOCKS
1N/A#define OKFLAGS (DB_CREATE | DB_THREAD | DB_TXN_NOSYNC)
1N/A#else
1N/A#define OKFLAGS (DB_CREATE | DB_TXN_NOSYNC)
1N/A#endif
1N/A if ((ret = __db_fchk(dbenv, "txn_open", flags, OKFLAGS)) != 0)
1N/A return (ret);
1N/A
1N/A maxtxns = dbenv->tx_max != 0 ? dbenv->tx_max : 20;
1N/A
1N/A /* Now, create the transaction manager structure and set its fields. */
1N/A if ((ret = __os_calloc(1, sizeof(DB_TXNMGR), &tmgrp)) != 0)
1N/A return (ret);
1N/A
1N/A /* Initialize the transaction manager structure. */
1N/A tmgrp->mutexp = NULL;
1N/A tmgrp->dbenv = dbenv;
1N/A tmgrp->recover =
1N/A dbenv->tx_recover == NULL ? __db_dispatch : dbenv->tx_recover;
1N/A tmgrp->flags = LF_ISSET(DB_TXN_NOSYNC | DB_THREAD);
1N/A TAILQ_INIT(&tmgrp->txn_chain);
1N/A
1N/A /* Join/create the txn region. */
1N/A tmgrp->reginfo.dbenv = dbenv;
1N/A tmgrp->reginfo.appname = DB_APP_NONE;
1N/A if (path == NULL)
1N/A tmgrp->reginfo.path = NULL;
1N/A else
1N/A if ((ret = __os_strdup(path, &tmgrp->reginfo.path)) != 0)
1N/A goto err;
1N/A tmgrp->reginfo.file = DEFAULT_TXN_FILE;
1N/A tmgrp->reginfo.mode = mode;
1N/A tmgrp->reginfo.size = TXN_REGION_SIZE(maxtxns);
1N/A tmgrp->reginfo.dbflags = flags;
1N/A tmgrp->reginfo.addr = NULL;
1N/A tmgrp->reginfo.fd = -1;
1N/A tmgrp->reginfo.flags = dbenv->tx_max == 0 ? REGION_SIZEDEF : 0;
1N/A if ((ret = __db_rattach(&tmgrp->reginfo)) != 0)
1N/A goto err;
1N/A
1N/A /* Fill in region-related fields. */
1N/A tmgrp->region = tmgrp->reginfo.addr;
1N/A tmgrp->mem = &tmgrp->region[1];
1N/A
1N/A if (F_ISSET(&tmgrp->reginfo, REGION_CREATED)) {
1N/A tmgrp->region->maxtxns = maxtxns;
1N/A if ((ret = __txn_init(tmgrp->region)) != 0)
1N/A goto err;
1N/A
1N/A } else if (tmgrp->region->magic != DB_TXNMAGIC) {
1N/A /* Check if valid region. */
1N/A __db_err(dbenv, "txn_open: Bad magic number");
1N/A ret = EINVAL;
1N/A goto err;
1N/A }
1N/A
1N/A if (LF_ISSET(DB_THREAD)) {
1N/A if ((ret = __db_shalloc(tmgrp->mem, sizeof(db_mutex_t),
1N/A MUTEX_ALIGNMENT, &tmgrp->mutexp)) == 0)
1N/A /*
1N/A * Since we only get here if threading is turned on, we
1N/A * know that we have spinlocks, so the offset is going
1N/A * to be ignored. We put 0 here as a valid placeholder.
1N/A */
1N/A __db_mutex_init(tmgrp->mutexp, 0);
1N/A if (ret != 0)
1N/A goto err;
1N/A }
1N/A
1N/A UNLOCK_TXNREGION(tmgrp);
1N/A *mgrpp = tmgrp;
1N/A return (0);
1N/A
1N/Aerr: if (tmgrp->reginfo.addr != NULL) {
1N/A if (tmgrp->mutexp != NULL)
1N/A __db_shalloc_free(tmgrp->mem, tmgrp->mutexp);
1N/A
1N/A UNLOCK_TXNREGION(tmgrp);
1N/A (void)__db_rdetach(&tmgrp->reginfo);
1N/A if (F_ISSET(&tmgrp->reginfo, REGION_CREATED))
1N/A (void)txn_unlink(path, 1, dbenv);
1N/A }
1N/A
1N/A if (tmgrp->reginfo.path != NULL)
1N/A __os_freestr(tmgrp->reginfo.path);
1N/A __os_free(tmgrp, sizeof(*tmgrp));
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __txn_panic --
1N/A * Panic a transaction region.
1N/A *
1N/A * PUBLIC: void __txn_panic __P((DB_ENV *));
1N/A */
1N/Avoid
1N/A__txn_panic(dbenv)
1N/A DB_ENV *dbenv;
1N/A{
1N/A if (dbenv->tx_info != NULL)
1N/A dbenv->tx_info->region->hdr.panic = 1;
1N/A}
1N/A
1N/A/*
1N/A * txn_begin --
1N/A * This is a wrapper to the actual begin process. Normal txn_begin()
1N/A * allocates a DB_TXN structure for the caller, while txn_xa_begin() does
1N/A * not. Other than that, both call into the common __txn_begin code().
1N/A *
1N/A * Internally, we use TXN_DETAIL structures, but the DB_TXN structure
1N/A * provides access to the transaction ID and the offset in the transaction
1N/A * region of the TXN_DETAIL structure.
1N/A */
1N/Aint
1N/Atxn_begin(tmgrp, parent, txnpp)
1N/A DB_TXNMGR *tmgrp;
1N/A DB_TXN *parent, **txnpp;
1N/A{
1N/A DB_TXN *txn;
1N/A int ret;
1N/A
1N/A TXN_PANIC_CHECK(tmgrp);
1N/A
1N/A if ((ret = __os_calloc(1, sizeof(DB_TXN), &txn)) != 0)
1N/A return (ret);
1N/A
1N/A txn->parent = parent;
1N/A TAILQ_INIT(&txn->kids);
1N/A txn->mgrp = tmgrp;
1N/A txn->flags = TXN_MALLOC;
1N/A if ((ret = __txn_begin(txn)) != 0) {
1N/A __os_free(txn, sizeof(DB_TXN));
1N/A txn = NULL;
1N/A }
1N/A if (txn != NULL && parent != NULL)
1N/A TAILQ_INSERT_HEAD(&parent->kids, txn, klinks);
1N/A *txnpp = txn;
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __txn_xa_begin --
1N/A * XA version of txn_begin.
1N/A *
1N/A * PUBLIC: int __txn_xa_begin __P((DB_ENV *, DB_TXN *));
1N/A */
1N/Aint
1N/A__txn_xa_begin(dbenv, txn)
1N/A DB_ENV *dbenv;
1N/A DB_TXN *txn;
1N/A{
1N/A TXN_PANIC_CHECK(dbenv->tx_info);
1N/A
1N/A memset(txn, 0, sizeof(DB_TXN));
1N/A
1N/A txn->mgrp = dbenv->tx_info;
1N/A
1N/A return (__txn_begin(txn));
1N/A}
1N/A
1N/A/*
1N/A * __txn_begin --
1N/A * Normal DB version of txn_begin.
1N/A */
1N/Astatic int
1N/A__txn_begin(txn)
1N/A DB_TXN *txn;
1N/A{
1N/A DB_LSN begin_lsn;
1N/A DB_TXNMGR *mgr;
1N/A TXN_DETAIL *td;
1N/A size_t off;
1N/A u_int32_t id;
1N/A int ret;
1N/A
1N/A /*
1N/A * We do not have to write begin records (and if we do not, then we
1N/A * need never write records for read-only transactions). However,
1N/A * we do need to find the current LSN so that we can store it in the
1N/A * transaction structure, so we can know where to take checkpoints.
1N/A */
1N/A mgr = txn->mgrp;
1N/A if (mgr->dbenv->lg_info != NULL && (ret =
1N/A log_put(mgr->dbenv->lg_info, &begin_lsn, NULL, DB_CURLSN)) != 0)
1N/A goto err2;
1N/A
1N/A LOCK_TXNREGION(mgr);
1N/A
1N/A /* Make sure that last_txnid is not going to wrap around. */
1N/A if (mgr->region->last_txnid == TXN_INVALID) {
1N/A __db_err(mgr->dbenv, "txn_begin: %s %s",
1N/A "Transaction ID wrapping.",
1N/A "Snapshot your database and start a new log.");
1N/A ret = EINVAL;
1N/A goto err1;
1N/A }
1N/A
1N/A if ((ret = __txn_validate_region(mgr)) != 0)
1N/A goto err1;
1N/A
1N/A /* Allocate a new transaction detail structure. */
1N/A if ((ret = __db_shalloc(mgr->mem, sizeof(TXN_DETAIL), 0, &td)) != 0
1N/A && ret == ENOMEM && (ret = __txn_grow_region(mgr)) == 0)
1N/A ret = __db_shalloc(mgr->mem, sizeof(TXN_DETAIL), 0, &td);
1N/A if (ret != 0)
1N/A goto err1;
1N/A
1N/A /* Place transaction on active transaction list. */
1N/A SH_TAILQ_INSERT_HEAD(&mgr->region->active_txn, td, links, __txn_detail);
1N/A
1N/A id = ++mgr->region->last_txnid;
1N/A ++mgr->region->nbegins;
1N/A
1N/A td->txnid = id;
1N/A td->begin_lsn = begin_lsn;
1N/A ZERO_LSN(td->last_lsn);
1N/A td->last_lock = 0;
1N/A td->status = TXN_RUNNING;
1N/A if (txn->parent != NULL)
1N/A td->parent = txn->parent->off;
1N/A else
1N/A td->parent = 0;
1N/A
1N/A off = (u_int8_t *)td - (u_int8_t *)mgr->region;
1N/A UNLOCK_TXNREGION(mgr);
1N/A
1N/A ZERO_LSN(txn->last_lsn);
1N/A txn->txnid = id;
1N/A txn->off = off;
1N/A
1N/A if (F_ISSET(txn, TXN_MALLOC)) {
1N/A LOCK_TXNTHREAD(mgr);
1N/A TAILQ_INSERT_TAIL(&mgr->txn_chain, txn, links);
1N/A UNLOCK_TXNTHREAD(mgr);
1N/A }
1N/A
1N/A return (0);
1N/A
1N/Aerr1: UNLOCK_TXNREGION(mgr);
1N/A
1N/Aerr2: return (ret);
1N/A}
1N/A/*
1N/A * txn_commit --
1N/A * Commit a transaction.
1N/A */
1N/Aint
1N/Atxn_commit(txnp)
1N/A DB_TXN *txnp;
1N/A{
1N/A DB_LOG *logp;
1N/A DB_TXNMGR *mgr;
1N/A int ret;
1N/A
1N/A mgr = txnp->mgrp;
1N/A
1N/A TXN_PANIC_CHECK(mgr);
1N/A if ((ret = __txn_check_running(txnp, NULL)) != 0)
1N/A return (ret);
1N/A
1N/A /*
1N/A * If there are any log records, write a log record and sync
1N/A * the log, else do no log writes. If the commit is for a child
1N/A * transaction, we do not need to commit the child synchronously
1N/A * since if its parent aborts, it will abort too and its parent
1N/A * (or ultimate ancestor) will write synchronously.
1N/A */
1N/A if ((logp = mgr->dbenv->lg_info) != NULL &&
1N/A !IS_ZERO_LSN(txnp->last_lsn)) {
1N/A if (txnp->parent == NULL)
1N/A ret = __txn_regop_log(logp, txnp, &txnp->last_lsn,
1N/A F_ISSET(mgr, DB_TXN_NOSYNC) ? 0 : DB_FLUSH,
1N/A TXN_COMMIT);
1N/A else
1N/A ret = __txn_child_log(logp, txnp, &txnp->last_lsn, 0,
1N/A TXN_COMMIT, txnp->parent->txnid);
1N/A if (ret != 0)
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * If this is the senior ancestor (i.e., it has no children), then we
1N/A * can release all the child transactions since everyone is committing.
1N/A * Then we can release this transaction. If this is not the ultimate
1N/A * ancestor, then we can neither free it or its children.
1N/A */
1N/A if (txnp->parent == NULL)
1N/A __txn_freekids(txnp);
1N/A
1N/A return (__txn_end(txnp, 1));
1N/A}
1N/A
1N/A/*
1N/A * txn_abort --
1N/A * Abort a transcation.
1N/A */
1N/Aint
1N/Atxn_abort(txnp)
1N/A DB_TXN *txnp;
1N/A{
1N/A int ret;
1N/A DB_TXN *kids;
1N/A
1N/A TXN_PANIC_CHECK(txnp->mgrp);
1N/A if ((ret = __txn_check_running(txnp, NULL)) != 0)
1N/A return (ret);
1N/A
1N/A for (kids = TAILQ_FIRST(&txnp->kids);
1N/A kids != NULL;
1N/A kids = TAILQ_FIRST(&txnp->kids))
1N/A txn_abort(kids);
1N/A
1N/A if ((ret = __txn_undo(txnp)) != 0) {
1N/A __db_err(txnp->mgrp->dbenv,
1N/A "txn_abort: Log undo failed %s", strerror(ret));
1N/A return (ret);
1N/A }
1N/A return (__txn_end(txnp, 0));
1N/A}
1N/A
1N/A/*
1N/A * txn_prepare --
1N/A * Flush the log so a future commit is guaranteed to succeed.
1N/A */
1N/Aint
1N/Atxn_prepare(txnp)
1N/A DB_TXN *txnp;
1N/A{
1N/A DBT xid;
1N/A DB_ENV *dbenv;
1N/A TXN_DETAIL *td;
1N/A int ret;
1N/A
1N/A if ((ret = __txn_check_running(txnp, &td)) != 0)
1N/A return (ret);
1N/A
1N/A dbenv = txnp->mgrp->dbenv;
1N/A memset(&xid, 0, sizeof(xid));
1N/A xid.data = td->xid;
1N/A /*
1N/A * We indicate that a transaction is an XA transaction by putting
1N/A * a valid size in the xid.size fiels. XA requires that the transaction
1N/A * be either ENDED or SUSPENDED when prepare is called, so we know
1N/A * that if the xa_status isn't in one of those states, but we are
1N/A * calling prepare that we are not an XA transaction.
1N/A */
1N/A xid.size =
1N/A td->xa_status != TXN_XA_ENDED && td->xa_status != TXN_XA_SUSPENDED ?
1N/A 0 : sizeof(td->xid);
1N/A if (dbenv->lg_info != NULL &&
1N/A (ret = __txn_xa_regop_log(dbenv->lg_info, txnp, &txnp->last_lsn,
1N/A F_ISSET(txnp->mgrp, DB_TXN_NOSYNC) ? 0 : DB_FLUSH, TXN_PREPARE,
1N/A &xid, td->format, td->gtrid, td->bqual, &td->begin_lsn)) != 0) {
1N/A __db_err(dbenv,
1N/A "txn_prepare: log_write failed %s\n", strerror(ret));
1N/A return (ret);
1N/A }
1N/A
1N/A LOCK_TXNTHREAD(txnp->mgrp);
1N/A td->status = TXN_PREPARED;
1N/A UNLOCK_TXNTHREAD(txnp->mgrp);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * Return the transaction ID associated with a particular transaction
1N/A */
1N/Au_int32_t
1N/Atxn_id(txnp)
1N/A DB_TXN *txnp;
1N/A{
1N/A return (txnp->txnid);
1N/A}
1N/A
1N/A/*
1N/A * txn_close --
1N/A * Close the transaction region, does not imply a checkpoint.
1N/A */
1N/Aint
1N/Atxn_close(tmgrp)
1N/A DB_TXNMGR *tmgrp;
1N/A{
1N/A DB_TXN *txnp;
1N/A int ret, t_ret;
1N/A
1N/A TXN_PANIC_CHECK(tmgrp);
1N/A
1N/A ret = 0;
1N/A
1N/A /*
1N/A * This function had better only be called once per process
1N/A * (i.e., not per thread), so there should be no synchronization
1N/A * required.
1N/A */
1N/A while ((txnp =
1N/A TAILQ_FIRST(&tmgrp->txn_chain)) != TAILQ_END(&tmgrp->txn_chain))
1N/A if ((t_ret = txn_abort(txnp)) != 0) {
1N/A __txn_end(txnp, 0);
1N/A if (ret == 0)
1N/A ret = t_ret;
1N/A }
1N/A
1N/A if (tmgrp->dbenv->lg_info &&
1N/A (t_ret = log_flush(tmgrp->dbenv->lg_info, NULL)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A if (tmgrp->mutexp != NULL) {
1N/A LOCK_TXNREGION(tmgrp);
1N/A __db_shalloc_free(tmgrp->mem, tmgrp->mutexp);
1N/A UNLOCK_TXNREGION(tmgrp);
1N/A }
1N/A
1N/A if ((t_ret = __db_rdetach(&tmgrp->reginfo)) != 0 && ret == 0)
1N/A ret = t_ret;
1N/A
1N/A if (tmgrp->reginfo.path != NULL)
1N/A __os_freestr(tmgrp->reginfo.path);
1N/A __os_free(tmgrp, sizeof(*tmgrp));
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * txn_unlink --
1N/A * Remove the transaction region.
1N/A */
1N/Aint
1N/Atxn_unlink(path, force, dbenv)
1N/A const char *path;
1N/A int force;
1N/A DB_ENV *dbenv;
1N/A{
1N/A REGINFO reginfo;
1N/A int ret;
1N/A
1N/A memset(&reginfo, 0, sizeof(reginfo));
1N/A reginfo.dbenv = dbenv;
1N/A reginfo.appname = DB_APP_NONE;
1N/A if (path != NULL && (ret = __os_strdup(path, &reginfo.path)) != 0)
1N/A return (ret);
1N/A reginfo.file = DEFAULT_TXN_FILE;
1N/A ret = __db_runlink(&reginfo, force);
1N/A if (reginfo.path != NULL)
1N/A __os_freestr(reginfo.path);
1N/A return (ret);
1N/A}
1N/A
1N/A/* Internal routines. */
1N/A
1N/A/*
1N/A * Return 0 if the txnp is reasonable, otherwise returns EINVAL.
1N/A */
1N/Astatic int
1N/A__txn_check_running(txnp, tdp)
1N/A const DB_TXN *txnp;
1N/A TXN_DETAIL **tdp;
1N/A{
1N/A TXN_DETAIL *tp;
1N/A
1N/A tp = NULL;
1N/A if (txnp != NULL && txnp->mgrp != NULL && txnp->mgrp->region != NULL) {
1N/A tp = (TXN_DETAIL *)((u_int8_t *)txnp->mgrp->region + txnp->off);
1N/A /*
1N/A * Child transactions could be marked committed which is OK.
1N/A */
1N/A if (tp->status != TXN_RUNNING &&
1N/A tp->status != TXN_PREPARED && tp->status != TXN_COMMITTED)
1N/A tp = NULL;
1N/A if (tdp != NULL)
1N/A *tdp = tp;
1N/A }
1N/A
1N/A return (tp == NULL ? EINVAL : 0);
1N/A}
1N/A
1N/Astatic int
1N/A__txn_end(txnp, is_commit)
1N/A DB_TXN *txnp;
1N/A int is_commit;
1N/A{
1N/A DB_LOCKREQ request;
1N/A DB_TXNMGR *mgr;
1N/A TXN_DETAIL *tp;
1N/A u_int32_t locker;
1N/A int ret;
1N/A
1N/A mgr = txnp->mgrp;
1N/A
1N/A /* Release the locks. */
1N/A locker = txnp->txnid;
1N/A request.op = txnp->parent == NULL ||
1N/A is_commit == 0 ? DB_LOCK_PUT_ALL : DB_LOCK_INHERIT;
1N/A
1N/A if (mgr->dbenv->lk_info) {
1N/A ret =
1N/A lock_tvec(mgr->dbenv->lk_info, txnp, 0, &request, 1, NULL);
1N/A if (ret != 0 && (ret != DB_LOCK_DEADLOCK || is_commit)) {
1N/A __db_err(mgr->dbenv, "%s: release locks failed %s",
1N/A is_commit ? "txn_commit" : "txn_abort",
1N/A strerror(ret));
1N/A return (ret);
1N/A }
1N/A }
1N/A
1N/A /* End the transaction. */
1N/A LOCK_TXNREGION(mgr);
1N/A
1N/A /*
1N/A * Child transactions that are committing cannot be released until
1N/A * the parent commits, since the parent may abort, causing the child
1N/A * to abort as well.
1N/A */
1N/A tp = (TXN_DETAIL *)((u_int8_t *)mgr->region + txnp->off);
1N/A if (txnp->parent == NULL || !is_commit) {
1N/A SH_TAILQ_REMOVE(&mgr->region->active_txn,
1N/A tp, links, __txn_detail);
1N/A
1N/A __db_shalloc_free(mgr->mem, tp);
1N/A } else
1N/A tp->status = is_commit ? TXN_COMMITTED : TXN_ABORTED;
1N/A
1N/A if (is_commit)
1N/A mgr->region->ncommits++;
1N/A else
1N/A mgr->region->naborts++;
1N/A
1N/A UNLOCK_TXNREGION(mgr);
1N/A
1N/A /*
1N/A * If the transaction aborted, we can remove it from its parent links.
1N/A * If it committed, then we need to leave it on, since the parent can
1N/A * still abort.
1N/A */
1N/A if (txnp->parent != NULL && !is_commit)
1N/A TAILQ_REMOVE(&txnp->parent->kids, txnp, klinks);
1N/A
1N/A /* Free the space. */
1N/A if (F_ISSET(txnp, TXN_MALLOC) && (txnp->parent == NULL || !is_commit)) {
1N/A LOCK_TXNTHREAD(mgr);
1N/A TAILQ_REMOVE(&mgr->txn_chain, txnp, links);
1N/A UNLOCK_TXNTHREAD(mgr);
1N/A
1N/A __os_free(txnp, sizeof(*txnp));
1N/A }
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A
1N/A/*
1N/A * __txn_undo --
1N/A * Undo the transaction with id txnid. Returns 0 on success and
1N/A * errno on failure.
1N/A */
1N/Astatic int
1N/A__txn_undo(txnp)
1N/A DB_TXN *txnp;
1N/A{
1N/A DBT rdbt;
1N/A DB_LOG *logp;
1N/A DB_LSN key_lsn;
1N/A DB_TXNMGR *mgr;
1N/A int ret;
1N/A
1N/A mgr = txnp->mgrp;
1N/A logp = mgr->dbenv->lg_info;
1N/A if (logp == NULL)
1N/A return (0);
1N/A
1N/A /*
1N/A * This is the simplest way to code this, but if the mallocs during
1N/A * recovery turn out to be a performance issue, we can do the
1N/A * allocation here and use DB_DBT_USERMEM.
1N/A */
1N/A memset(&rdbt, 0, sizeof(rdbt));
1N/A if (F_ISSET(logp, DB_AM_THREAD))
1N/A F_SET(&rdbt, DB_DBT_MALLOC);
1N/A
1N/A key_lsn = txnp->last_lsn; /* structure assignment */
1N/A for (ret = 0; ret == 0 && !IS_ZERO_LSN(key_lsn);) {
1N/A /*
1N/A * The dispatch routine returns the lsn of the record
1N/A * before the current one in the key_lsn argument.
1N/A */
1N/A if ((ret = log_get(logp, &key_lsn, &rdbt, DB_SET)) == 0) {
1N/A ret =
1N/A mgr->recover(logp, &rdbt, &key_lsn, TXN_UNDO, NULL);
1N/A if (F_ISSET(logp, DB_AM_THREAD) && rdbt.data != NULL) {
1N/A __os_free(rdbt.data, rdbt.size);
1N/A rdbt.data = NULL;
1N/A }
1N/A }
1N/A if (ret != 0)
1N/A return (ret);
1N/A }
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * Transaction checkpoint.
1N/A * If either kbytes or minutes is non-zero, then we only take the checkpoint
1N/A * more than "minutes" minutes have passed since the last checkpoint or if
1N/A * more than "kbytes" of log data have been written since the last checkpoint.
1N/A * When taking a checkpoint, find the oldest active transaction and figure out
1N/A * its first LSN. This is the lowest LSN we can checkpoint, since any record
1N/A * written after since that point may be involved in a transaction and may
1N/A * therefore need to be undone in the case of an abort.
1N/A */
1N/Aint
1N/Atxn_checkpoint(mgr, kbytes, minutes)
1N/A const DB_TXNMGR *mgr;
1N/A u_int32_t kbytes, minutes;
1N/A{
1N/A DB_LOG *dblp;
1N/A DB_LSN ckp_lsn, sync_lsn, last_ckp;
1N/A TXN_DETAIL *txnp;
1N/A time_t last_ckp_time, now;
1N/A u_int32_t kbytes_written;
1N/A int ret;
1N/A
1N/A TXN_PANIC_CHECK(mgr);
1N/A
1N/A /*
1N/A * Check if we need to run recovery.
1N/A */
1N/A ZERO_LSN(ckp_lsn);
1N/A if (minutes != 0) {
1N/A (void)time(&now);
1N/A
1N/A LOCK_TXNREGION(mgr);
1N/A last_ckp_time = mgr->region->time_ckp;
1N/A UNLOCK_TXNREGION(mgr);
1N/A
1N/A if (now - last_ckp_time >= (time_t)(minutes * 60))
1N/A goto do_ckp;
1N/A }
1N/A
1N/A if (kbytes != 0) {
1N/A dblp = mgr->dbenv->lg_info;
1N/A LOCK_LOGREGION(dblp);
1N/A kbytes_written =
1N/A dblp->lp->stat.st_wc_mbytes * 1024 +
1N/A dblp->lp->stat.st_wc_bytes / 1024;
1N/A ckp_lsn = dblp->lp->lsn;
1N/A UNLOCK_LOGREGION(dblp);
1N/A if (kbytes_written >= (u_int32_t)kbytes)
1N/A goto do_ckp;
1N/A }
1N/A
1N/A /*
1N/A * If we checked time and data and didn't go to checkpoint,
1N/A * we're done.
1N/A */
1N/A if (minutes != 0 || kbytes != 0)
1N/A return (0);
1N/A
1N/Ado_ckp:
1N/A if (IS_ZERO_LSN(ckp_lsn)) {
1N/A dblp = mgr->dbenv->lg_info;
1N/A LOCK_LOGREGION(dblp);
1N/A ckp_lsn = dblp->lp->lsn;
1N/A UNLOCK_LOGREGION(dblp);
1N/A }
1N/A
1N/A /*
1N/A * We have to find an LSN such that all transactions begun
1N/A * before that LSN are complete.
1N/A */
1N/A LOCK_TXNREGION(mgr);
1N/A
1N/A if (!IS_ZERO_LSN(mgr->region->pending_ckp))
1N/A ckp_lsn = mgr->region->pending_ckp;
1N/A else
1N/A for (txnp =
1N/A SH_TAILQ_FIRST(&mgr->region->active_txn, __txn_detail);
1N/A txnp != NULL;
1N/A txnp = SH_TAILQ_NEXT(txnp, links, __txn_detail)) {
1N/A
1N/A /*
1N/A * Look through the active transactions for the
1N/A * lowest begin lsn.
1N/A */
1N/A if (!IS_ZERO_LSN(txnp->begin_lsn) &&
1N/A log_compare(&txnp->begin_lsn, &ckp_lsn) < 0)
1N/A ckp_lsn = txnp->begin_lsn;
1N/A }
1N/A
1N/A mgr->region->pending_ckp = ckp_lsn;
1N/A UNLOCK_TXNREGION(mgr);
1N/A
1N/A /*
1N/A * memp_sync may change the lsn you pass it, so don't pass it
1N/A * the actual ckp_lsn, pass it a temp instead.
1N/A */
1N/A sync_lsn = ckp_lsn;
1N/A if (mgr->dbenv->mp_info != NULL &&
1N/A (ret = memp_sync(mgr->dbenv->mp_info, &sync_lsn)) != 0) {
1N/A /*
1N/A * ret == DB_INCOMPLETE means that there are still buffers to
1N/A * flush, the checkpoint is not complete. Wait and try again.
1N/A */
1N/A if (ret > 0)
1N/A __db_err(mgr->dbenv,
1N/A "txn_checkpoint: system failure in memp_sync %s\n",
1N/A strerror(ret));
1N/A return (ret);
1N/A }
1N/A if (mgr->dbenv->lg_info != NULL) {
1N/A LOCK_TXNREGION(mgr);
1N/A last_ckp = mgr->region->last_ckp;
1N/A ZERO_LSN(mgr->region->pending_ckp);
1N/A UNLOCK_TXNREGION(mgr);
1N/A
1N/A if ((ret = __txn_ckp_log(mgr->dbenv->lg_info,
1N/A NULL, &ckp_lsn, DB_CHECKPOINT, &ckp_lsn, &last_ckp)) != 0) {
1N/A __db_err(mgr->dbenv,
1N/A "txn_checkpoint: log failed at LSN [%ld %ld] %s\n",
1N/A (long)ckp_lsn.file, (long)ckp_lsn.offset,
1N/A strerror(ret));
1N/A return (ret);
1N/A }
1N/A
1N/A LOCK_TXNREGION(mgr);
1N/A mgr->region->last_ckp = ckp_lsn;
1N/A (void)time(&mgr->region->time_ckp);
1N/A UNLOCK_TXNREGION(mgr);
1N/A }
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __txn_validate_region --
1N/A * Called at every interface to verify if the region has changed size,
1N/A * and if so, to remap the region in and reset the process' pointers.
1N/A */
1N/Astatic int
1N/A__txn_validate_region(tp)
1N/A DB_TXNMGR *tp;
1N/A{
1N/A int ret;
1N/A
1N/A if (tp->reginfo.size == tp->region->hdr.size)
1N/A return (0);
1N/A
1N/A /* Detach/reattach the region. */
1N/A if ((ret = __db_rreattach(&tp->reginfo, tp->region->hdr.size)) != 0)
1N/A return (ret);
1N/A
1N/A /* Reset region information. */
1N/A tp->region = tp->reginfo.addr;
1N/A tp->mem = &tp->region[1];
1N/A
1N/A return (0);
1N/A}
1N/A
1N/Astatic int
1N/A__txn_grow_region(tp)
1N/A DB_TXNMGR *tp;
1N/A{
1N/A size_t incr, oldsize;
1N/A u_int32_t mutex_offset, oldmax;
1N/A u_int8_t *curaddr;
1N/A int ret;
1N/A
1N/A oldmax = tp->region->maxtxns;
1N/A incr = oldmax * sizeof(DB_TXN);
1N/A mutex_offset = tp->mutexp != NULL ?
1N/A (u_int8_t *)tp->mutexp - (u_int8_t *)tp->region : 0;
1N/A
1N/A oldsize = tp->reginfo.size;
1N/A if ((ret = __db_rgrow(&tp->reginfo, oldsize + incr)) != 0)
1N/A return (ret);
1N/A tp->region = tp->reginfo.addr;
1N/A
1N/A /* Throw the new space on the free list. */
1N/A curaddr = (u_int8_t *)tp->region + oldsize;
1N/A tp->mem = &tp->region[1];
1N/A tp->mutexp = mutex_offset != 0 ?
1N/A (db_mutex_t *)((u_int8_t *)tp->region + mutex_offset) : NULL;
1N/A
1N/A *((size_t *)curaddr) = incr - sizeof(size_t);
1N/A curaddr += sizeof(size_t);
1N/A __db_shalloc_free(tp->mem, curaddr);
1N/A
1N/A tp->region->maxtxns = 2 * oldmax;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/Aint
1N/Atxn_stat(mgr, statp, db_malloc)
1N/A DB_TXNMGR *mgr;
1N/A DB_TXN_STAT **statp;
1N/A void *(*db_malloc) __P((size_t));
1N/A{
1N/A DB_TXN_STAT *stats;
1N/A TXN_DETAIL *txnp;
1N/A size_t nbytes;
1N/A u_int32_t nactive, ndx;
1N/A int ret;
1N/A
1N/A TXN_PANIC_CHECK(mgr);
1N/A
1N/A LOCK_TXNREGION(mgr);
1N/A nactive = mgr->region->nbegins -
1N/A mgr->region->naborts - mgr->region->ncommits;
1N/A UNLOCK_TXNREGION(mgr);
1N/A
1N/A /*
1N/A * Allocate a bunch of extra active structures to handle any
1N/A * that have been created since we unlocked the region.
1N/A */
1N/A nbytes = sizeof(DB_TXN_STAT) + sizeof(DB_TXN_ACTIVE) * (nactive + 200);
1N/A if ((ret = __os_malloc(nbytes, db_malloc, &stats)) != 0)
1N/A return (ret);
1N/A
1N/A LOCK_TXNREGION(mgr);
1N/A stats->st_last_txnid = mgr->region->last_txnid;
1N/A stats->st_last_ckp = mgr->region->last_ckp;
1N/A stats->st_maxtxns = mgr->region->maxtxns;
1N/A stats->st_naborts = mgr->region->naborts;
1N/A stats->st_nbegins = mgr->region->nbegins;
1N/A stats->st_ncommits = mgr->region->ncommits;
1N/A stats->st_pending_ckp = mgr->region->pending_ckp;
1N/A stats->st_time_ckp = mgr->region->time_ckp;
1N/A stats->st_nactive = stats->st_nbegins -
1N/A stats->st_naborts - stats->st_ncommits;
1N/A if (stats->st_nactive > nactive + 200)
1N/A stats->st_nactive = nactive + 200;
1N/A stats->st_txnarray = (DB_TXN_ACTIVE *)&stats[1];
1N/A
1N/A ndx = 0;
1N/A for (txnp = SH_TAILQ_FIRST(&mgr->region->active_txn, __txn_detail);
1N/A txnp != NULL;
1N/A txnp = SH_TAILQ_NEXT(txnp, links, __txn_detail)) {
1N/A stats->st_txnarray[ndx].txnid = txnp->txnid;
1N/A stats->st_txnarray[ndx].lsn = txnp->begin_lsn;
1N/A ndx++;
1N/A
1N/A if (ndx >= stats->st_nactive)
1N/A break;
1N/A }
1N/A
1N/A stats->st_region_wait = mgr->region->hdr.lock.mutex_set_wait;
1N/A stats->st_region_nowait = mgr->region->hdr.lock.mutex_set_nowait;
1N/A stats->st_refcnt = mgr->region->hdr.refcnt;
1N/A stats->st_regsize = mgr->region->hdr.size;
1N/A
1N/A UNLOCK_TXNREGION(mgr);
1N/A *statp = stats;
1N/A return (0);
1N/A}
1N/A
1N/Astatic void
1N/A__txn_freekids(txnp)
1N/A DB_TXN *txnp;
1N/A{
1N/A DB_TXNMGR *mgr;
1N/A TXN_DETAIL *tp;
1N/A DB_TXN *kids;
1N/A
1N/A mgr = txnp->mgrp;
1N/A
1N/A for (kids = TAILQ_FIRST(&txnp->kids);
1N/A kids != NULL;
1N/A kids = TAILQ_FIRST(&txnp->kids)) {
1N/A /* Free any children of this transaction. */
1N/A __txn_freekids(kids);
1N/A
1N/A /* Free the transaction detail in the region. */
1N/A LOCK_TXNREGION(mgr);
1N/A tp = (TXN_DETAIL *)((u_int8_t *)mgr->region + kids->off);
1N/A SH_TAILQ_REMOVE(&mgr->region->active_txn,
1N/A tp, links, __txn_detail);
1N/A
1N/A __db_shalloc_free(mgr->mem, tp);
1N/A UNLOCK_TXNREGION(mgr);
1N/A
1N/A /* Now remove from its parent. */
1N/A TAILQ_REMOVE(&txnp->kids, kids, klinks);
1N/A if (F_ISSET(txnp, TXN_MALLOC)) {
1N/A LOCK_TXNTHREAD(mgr);
1N/A TAILQ_REMOVE(&mgr->txn_chain, kids, links);
1N/A UNLOCK_TXNTHREAD(mgr);
1N/A __os_free(kids, sizeof(*kids));
1N/A }
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * __txn_is_ancestor --
1N/A * Determine if a transaction is an ancestor of another transaction.
1N/A * This is used during lock promotion when we do not have the per-process
1N/A * data structures that link parents together. Instead, we'll have to
1N/A * follow the links in the transaction region.
1N/A *
1N/A * PUBLIC: int __txn_is_ancestor __P((DB_TXNMGR *, size_t, size_t));
1N/A */
1N/Aint
1N/A__txn_is_ancestor(mgr, hold_off, req_off)
1N/A DB_TXNMGR *mgr;
1N/A size_t hold_off, req_off;
1N/A{
1N/A TXN_DETAIL *hold_tp, *req_tp;
1N/A
1N/A hold_tp = (TXN_DETAIL *)((u_int8_t *)mgr->region + hold_off);
1N/A req_tp = (TXN_DETAIL *)((u_int8_t *)mgr->region + req_off);
1N/A
1N/A while (req_tp->parent != 0) {
1N/A req_tp =
1N/A (TXN_DETAIL *)((u_int8_t *)mgr->region + req_tp->parent);
1N/A if (req_tp->txnid == hold_tp->txnid)
1N/A return (1);
1N/A }
1N/A
1N/A return (0);
1N/A}