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[] = "@(#)db_dispatch.c 10.20 (Sleepycat) 10/10/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <shqueue.h>
1N/A#include <stddef.h>
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "db_page.h"
1N/A#include "db_dispatch.h"
1N/A#include "db_am.h"
1N/A#include "common_ext.h"
1N/A#include "log_auto.h"
1N/A#include "txn.h"
1N/A#include "txn_auto.h"
1N/A
1N/A/*
1N/A * Data structures to manage the DB dispatch table. The dispatch table
1N/A * is a dynamically allocated array of pointers to dispatch functions.
1N/A * The dispatch_size is the number of entries possible in the current
1N/A * dispatch table and the dispatch_valid is the number of valid entries
1N/A * in the dispatch table.
1N/A */
1N/Astatic int (**dispatch_table) __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
1N/Astatic u_int32_t dispatch_size = 0;
1N/A
1N/A/*
1N/A * __db_dispatch --
1N/A *
1N/A * This is the transaction dispatch function used by the db access methods.
1N/A * It is designed to handle the record format used by all the access
1N/A * methods (the one automatically generated by the db_{h,log,read}.sh
1N/A * scripts in the tools directory). An application using a different
1N/A * recovery paradigm will supply a different dispatch function to txn_open.
1N/A *
1N/A * PUBLIC: int __db_dispatch __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
1N/A */
1N/Aint
1N/A__db_dispatch(logp, db, lsnp, redo, info)
1N/A DB_LOG *logp; /* The log file. */
1N/A DBT *db; /* The log record upon which to dispatch. */
1N/A DB_LSN *lsnp; /* The lsn of the record being dispatched. */
1N/A int redo; /* Redo this op (or undo it). */
1N/A void *info;
1N/A{
1N/A u_int32_t rectype, txnid;
1N/A
1N/A memcpy(&rectype, db->data, sizeof(rectype));
1N/A memcpy(&txnid, (u_int8_t *)db->data + sizeof(rectype), sizeof(txnid));
1N/A
1N/A switch (redo) {
1N/A case TXN_REDO:
1N/A case TXN_UNDO:
1N/A return ((dispatch_table[rectype])(logp, db, lsnp, redo, info));
1N/A case TXN_OPENFILES:
1N/A if (rectype < DB_txn_BEGIN )
1N/A return ((dispatch_table[rectype])(logp,
1N/A db, lsnp, redo, info));
1N/A break;
1N/A case TXN_BACKWARD_ROLL:
1N/A /*
1N/A * Running full recovery in the backward pass. If we've
1N/A * seen this txnid before and added to it our commit list,
1N/A * then we do nothing during this pass. If we've never
1N/A * seen it, then we call the appropriate recovery routine
1N/A * in "abort mode".
1N/A */
1N/A if (rectype == DB_log_register || rectype == DB_txn_ckp ||
1N/A (__db_txnlist_find(info, txnid) == DB_NOTFOUND &&
1N/A txnid != 0))
1N/A return ((dispatch_table[rectype])(logp,
1N/A db, lsnp, TXN_UNDO, info));
1N/A break;
1N/A case TXN_FORWARD_ROLL:
1N/A /*
1N/A * In the forward pass, if we haven't seen the transaction,
1N/A * do nothing, else recovery it.
1N/A */
1N/A if (rectype == DB_log_register || rectype == DB_txn_ckp ||
1N/A __db_txnlist_find(info, txnid) != DB_NOTFOUND)
1N/A return ((dispatch_table[rectype])(logp,
1N/A db, lsnp, TXN_REDO, info));
1N/A break;
1N/A default:
1N/A abort();
1N/A }
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_add_recovery --
1N/A *
1N/A * PUBLIC: int __db_add_recovery __P((DB_ENV *,
1N/A * PUBLIC: int (*)(DB_LOG *, DBT *, DB_LSN *, int, void *), u_int32_t));
1N/A */
1N/Aint
1N/A__db_add_recovery(dbenv, func, ndx)
1N/A DB_ENV *dbenv;
1N/A int (*func) __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
1N/A u_int32_t ndx;
1N/A{
1N/A u_int32_t i;
1N/A int ret;
1N/A
1N/A COMPQUIET(dbenv, NULL); /* !!!: not currently used. */
1N/A
1N/A /* Check if we have to grow the table. */
1N/A if (ndx >= dispatch_size) {
1N/A if ((ret = __os_realloc(&dispatch_table,
1N/A (DB_user_BEGIN + dispatch_size) *
1N/A sizeof(dispatch_table[0]))) != 0)
1N/A return (ret);
1N/A for (i = dispatch_size,
1N/A dispatch_size += DB_user_BEGIN; i < dispatch_size; ++i)
1N/A dispatch_table[i] = NULL;
1N/A }
1N/A
1N/A dispatch_table[ndx] = func;
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_txnlist_init --
1N/A * Initialize transaction linked list.
1N/A *
1N/A * PUBLIC: int __db_txnlist_init __P((void *));
1N/A */
1N/Aint
1N/A__db_txnlist_init(retp)
1N/A void *retp;
1N/A{
1N/A DB_TXNHEAD *headp;
1N/A int ret;
1N/A
1N/A if ((ret = __os_malloc(sizeof(DB_TXNHEAD), NULL, &headp)) != 0)
1N/A return (ret);
1N/A
1N/A LIST_INIT(&headp->head);
1N/A headp->maxid = 0;
1N/A headp->generation = 1;
1N/A
1N/A *(void **)retp = headp;
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_txnlist_add --
1N/A * Add an element to our transaction linked list.
1N/A *
1N/A * PUBLIC: int __db_txnlist_add __P((void *, u_int32_t));
1N/A */
1N/Aint
1N/A__db_txnlist_add(listp, txnid)
1N/A void *listp;
1N/A u_int32_t txnid;
1N/A{
1N/A DB_TXNHEAD *hp;
1N/A DB_TXNLIST *elp;
1N/A int ret;
1N/A
1N/A if ((ret = __os_malloc(sizeof(DB_TXNLIST), NULL, &elp)) != 0)
1N/A return (ret);
1N/A
1N/A elp->txnid = txnid;
1N/A hp = (DB_TXNHEAD *)listp;
1N/A LIST_INSERT_HEAD(&hp->head, elp, links);
1N/A if (txnid > hp->maxid)
1N/A hp->maxid = txnid;
1N/A elp->generation = hp->generation;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_txnlist_find --
1N/A * Checks to see if a txnid with the current generation is in the
1N/A * txnid list.
1N/A *
1N/A * PUBLIC: int __db_txnlist_find __P((void *, u_int32_t));
1N/A */
1N/Aint
1N/A__db_txnlist_find(listp, txnid)
1N/A void *listp;
1N/A u_int32_t txnid;
1N/A{
1N/A DB_TXNHEAD *hp;
1N/A DB_TXNLIST *p;
1N/A
1N/A if ((hp = (DB_TXNHEAD *)listp) == NULL)
1N/A return (DB_NOTFOUND);
1N/A
1N/A for (p = hp->head.lh_first; p != NULL; p = p->links.le_next)
1N/A if (p->txnid == txnid && hp->generation == p->generation)
1N/A return (0);
1N/A
1N/A return (DB_NOTFOUND);
1N/A}
1N/A
1N/A/*
1N/A * __db_txnlist_end --
1N/A * Discard transaction linked list.
1N/A *
1N/A * PUBLIC: void __db_txnlist_end __P((void *));
1N/A */
1N/Avoid
1N/A__db_txnlist_end(listp)
1N/A void *listp;
1N/A{
1N/A DB_TXNHEAD *hp;
1N/A DB_TXNLIST *p;
1N/A
1N/A hp = (DB_TXNHEAD *)listp;
1N/A while ((p = LIST_FIRST(&hp->head)) != LIST_END(&hp->head)) {
1N/A LIST_REMOVE(p, links);
1N/A __os_free(p, 0);
1N/A }
1N/A __os_free(listp, sizeof(DB_TXNHEAD));
1N/A}
1N/A
1N/A/*
1N/A * __db_txnlist_gen --
1N/A * Change the current generation number.
1N/A *
1N/A * PUBLIC: void __db_txnlist_gen __P((void *, int));
1N/A */
1N/Avoid
1N/A__db_txnlist_gen(listp, incr)
1N/A void *listp;
1N/A int incr;
1N/A{
1N/A DB_TXNHEAD *hp;
1N/A
1N/A /*
1N/A * During recovery generation numbers keep track of how many "restart"
1N/A * checkpoints we've seen. Restart checkpoints occur whenever we take
1N/A * a checkpoint and there are no outstanding transactions. When that
1N/A * happens, we can reset transaction IDs back to 1. It always happens
1N/A * at recovery and it prevents us from exhausting the transaction IDs
1N/A * name space.
1N/A */
1N/A hp = (DB_TXNHEAD *)listp;
1N/A hp->generation += incr;
1N/A}
1N/A
1N/A#ifdef DEBUG
1N/A/*
1N/A * __db_txnlist_print --
1N/A * Print out the transaction list.
1N/A *
1N/A * PUBLIC: void __db_txnlist_print __P((void *));
1N/A */
1N/Avoid
1N/A__db_txnlist_print(listp)
1N/A void *listp;
1N/A{
1N/A DB_TXNHEAD *hp;
1N/A DB_TXNLIST *p;
1N/A
1N/A hp = (DB_TXNHEAD *)listp;
1N/A printf("Maxid: %lu Generation: %lu\n", (u_long)hp->maxid,
1N/A (u_long)hp->generation);
1N/A for (p = hp->head.lh_first; p != NULL; p = p->links.le_next)
1N/A printf("TXNID: %lu(%lu)\n", (u_long)p->txnid,
1N/A (u_long)p->generation);
1N/A}
1N/A#endif