#include "db_config.h"
#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/hash.h"
#include "dbinc/fop.h"
#include "dbinc/lock.h"
#include "dbinc/log.h"
#include "dbinc/mp.h"
#include "dbinc/txn.h"
static int __db_txnlist_find_internal __P((ENV *, DB_TXNHEAD *,
db_txnlist_type, u_int32_t, DB_TXNLIST **,
int, u_int32_t *));
int
__db_dispatch(env, dtab, db, lsnp, redo, info)
ENV *env;
DB_DISTAB *dtab;
DBT *db;
DB_LSN *lsnp;
db_recops redo;
DB_TXNHEAD *info;
{
DB_ENV *dbenv;
DB_LSN prev_lsn;
u_int32_t rectype, status, txnid, urectype;
int make_call, ret;
dbenv = env->dbenv;
LOGCOPY_32(env, &rectype, db->data);
LOGCOPY_32(env, &txnid, (u_int8_t *)db->data + sizeof(rectype));
make_call = ret = 0;
DB_ASSERT(env, dtab != NULL);
switch (redo) {
case DB_TXN_ABORT:
case DB_TXN_APPLY:
case DB_TXN_PRINT:
make_call = 1;
break;
case DB_TXN_OPENFILES:
LOGCOPY_TOLSN(env, &prev_lsn, (u_int8_t *)db->data +
sizeof(rectype) + sizeof(txnid));
if (txnid != 0 && prev_lsn.file == 0 && (ret =
__db_txnlist_add(env, info, txnid, TXN_OK, NULL)) != 0)
return (ret);
case DB_TXN_POPENFILES:
if (rectype == DB___dbreg_register ||
rectype == DB___txn_child ||
rectype == DB___txn_ckp || rectype == DB___txn_recycle)
return ((dtab->int_dispatch[rectype])(env,
db, lsnp, redo, info));
break;
case DB_TXN_BACKWARD_ROLL:
switch (rectype) {
case DB___txn_regop:
case DB___txn_recycle:
case DB___txn_ckp:
make_call = 1;
break;
case DB___txn_child:
case DB___db_noop:
case DB___fop_file_remove:
case DB___dbreg_register:
make_call = 1;
default:
if (txnid == 0)
break;
ret = __db_txnlist_find(env, info, txnid, &status);
if (ret == DB_NOTFOUND)
return (__db_txnlist_add(env,
info, txnid, TXN_IGNORE, lsnp));
if (ret != 0)
return (ret);
if (status == TXN_IGNORE && rectype != DB___txn_child) {
make_call = 0;
break;
}
if (status == TXN_COMMIT)
break;
make_call = 1;
if (status == TXN_OK &&
(ret = __db_txnlist_update(env,
info, txnid, rectype == DB___txn_xa_regop ?
TXN_PREPARE : TXN_ABORT, NULL, &status, 0)) != 0)
return (ret);
}
break;
case DB_TXN_FORWARD_ROLL:
switch (rectype) {
case DB___txn_recycle:
case DB___txn_ckp:
case DB___db_noop:
case DB___dbreg_register:
make_call = 1;
break;
default:
if (txnid == 0)
status = 0;
else {
ret = __db_txnlist_find(env,
info, txnid, &status);
if (ret == DB_NOTFOUND)
;
else if (ret != 0)
return (ret);
else if (status == TXN_COMMIT) {
make_call = 1;
break;
}
}
}
break;
default:
return (__db_unknown_flag(
env, "__db_dispatch", (u_int32_t)redo));
}
if (make_call) {
if (rectype & DB_debug_FLAG) {
if (redo == DB_TXN_PRINT)
rectype &= ~DB_debug_FLAG;
else {
LOGCOPY_TOLSN(env, lsnp,
(u_int8_t *)db->data +
sizeof(rectype) +
sizeof(txnid));
return (0);
}
}
if (rectype >= DB_user_BEGIN) {
if (dbenv->app_dispatch != NULL)
return (dbenv->app_dispatch(dbenv,
db, lsnp, redo));
urectype = rectype - DB_user_BEGIN;
if (urectype > dtab->ext_size ||
dtab->ext_dispatch[urectype] == NULL) {
__db_errx(env,
"Illegal application-specific record type %lu in log",
(u_long)rectype);
return (EINVAL);
}
return ((dtab->ext_dispatch[urectype])(dbenv,
db, lsnp, redo));
} else {
if (rectype > dtab->int_size ||
dtab->int_dispatch[rectype] == NULL) {
__db_errx(env,
"Illegal record type %lu in log",
(u_long)rectype);
return (EINVAL);
}
return ((dtab->int_dispatch[rectype])(env,
db, lsnp, redo, info));
}
}
return (0);
}
int
__db_add_recovery(dbenv, dtab, func, ndx)
DB_ENV *dbenv;
DB_DISTAB *dtab;
int (*func) __P((DB_ENV *, DBT *, DB_LSN *, db_recops));
u_int32_t ndx;
{
size_t i, nsize;
int ret;
if (ndx < DB_user_BEGIN) {
__db_errx(dbenv->env,
"Attempting to add application-specific record with invalid type %lu",
(u_long)ndx);
return (EINVAL);
}
ndx -= DB_user_BEGIN;
if (ndx >= dtab->ext_size) {
nsize = ndx + 40;
if ((ret =
__os_realloc(dbenv->env, nsize *
sizeof((dtab->ext_dispatch)[0]), &dtab->ext_dispatch))
!= 0)
return (ret);
for (i = dtab->ext_size; i < nsize; ++i)
(dtab->ext_dispatch)[i] = NULL;
dtab->ext_size = nsize;
}
(dtab->ext_dispatch)[ndx] = func;
return (0);
}
int
__db_add_recovery_int(env, dtab, func, ndx)
ENV *env;
DB_DISTAB *dtab;
int (*func) __P((ENV *, DBT *, DB_LSN *, db_recops, void *));
u_int32_t ndx;
{
size_t i, nsize;
int ret;
if (ndx >= DB_user_BEGIN) {
__db_errx(env,
"Attempting to add internal record with invalid type %lu",
(u_long)ndx);
return (EINVAL);
}
if (ndx >= dtab->int_size) {
nsize = ndx + 40;
if ((ret =
__os_realloc(env, nsize * sizeof((dtab->int_dispatch)[0]),
&dtab->int_dispatch)) != 0)
return (ret);
for (i = dtab->int_size; i < nsize; ++i)
(dtab->int_dispatch)[i] = NULL;
dtab->int_size = nsize;
}
(dtab->int_dispatch)[ndx] = func;
return (0);
}
int
__db_txnlist_init(env, ip, low_txn, hi_txn, trunc_lsn, retp)
ENV *env;
DB_THREAD_INFO *ip;
u_int32_t low_txn, hi_txn;
DB_LSN *trunc_lsn;
DB_TXNHEAD **retp;
{
DB_TXNHEAD *headp;
u_int32_t size, tmp;
int ret;
if (low_txn == 0)
size = 1;
else {
if (hi_txn < low_txn) {
tmp = hi_txn;
hi_txn = low_txn;
low_txn = tmp;
}
tmp = hi_txn - low_txn;
if (tmp > (TXN_MAXIMUM - TXN_MINIMUM) / 2)
tmp = (low_txn - TXN_MINIMUM) + (TXN_MAXIMUM - hi_txn);
size = tmp / 5;
if (size < 100)
size = 100;
}
if ((ret = __os_malloc(env,
sizeof(DB_TXNHEAD) + size * sizeof(headp->head), &headp)) != 0)
return (ret);
memset(headp, 0, sizeof(DB_TXNHEAD) + size * sizeof(headp->head));
headp->maxid = hi_txn;
headp->generation = 0;
headp->nslots = size;
headp->gen_alloc = 8;
headp->thread_info = ip;
if ((ret = __os_malloc(env, headp->gen_alloc *
sizeof(headp->gen_array[0]), &headp->gen_array)) != 0) {
__os_free(env, headp);
return (ret);
}
headp->gen_array[0].generation = 0;
headp->gen_array[0].txn_min = TXN_MINIMUM;
headp->gen_array[0].txn_max = TXN_MAXIMUM;
if (trunc_lsn != NULL) {
headp->trunc_lsn = *trunc_lsn;
headp->maxlsn = *trunc_lsn;
} else {
ZERO_LSN(headp->trunc_lsn);
ZERO_LSN(headp->maxlsn);
}
ZERO_LSN(headp->ckplsn);
*retp = headp;
return (0);
}
#define FIND_GENERATION(hp, txnid, gen) do { \
u_int32_t __i; \
for (__i = 0; __i <= (hp)->generation; __i++) \
\
if ((hp)->gen_array[__i].txn_min < \
(hp)->gen_array[__i].txn_max ? \
((txnid) >= (hp)->gen_array[__i].txn_min && \
(txnid) <= (hp)->gen_array[__i].txn_max) : \
((txnid) >= (hp)->gen_array[__i].txn_min || \
(txnid) <= (hp)->gen_array[__i].txn_max)) \
break; \
DB_ASSERT(env, __i <= (hp)->generation); \
gen = (hp)->gen_array[__i].generation; \
} while (0)
int
__db_txnlist_add(env, hp, txnid, status, lsn)
ENV *env;
DB_TXNHEAD *hp;
u_int32_t txnid, status;
DB_LSN *lsn;
{
DB_TXNLIST *elp;
int ret;
if ((ret = __os_malloc(env, sizeof(DB_TXNLIST), &elp)) != 0)
return (ret);
LIST_INSERT_HEAD(&hp->head[DB_TXNLIST_MASK(hp, txnid)], elp, links);
FIND_GENERATION(hp, txnid, elp->u.t.generation);
elp->type = TXNLIST_TXNID;
elp->u.t.txnid = txnid;
elp->u.t.status = status;
if (txnid > hp->maxid)
hp->maxid = txnid;
if (lsn != NULL && IS_ZERO_LSN(hp->maxlsn) && status == TXN_COMMIT)
hp->maxlsn = *lsn;
DB_ASSERT(env, lsn == NULL ||
status != TXN_COMMIT || LOG_COMPARE(&hp->maxlsn, lsn) >= 0);
return (0);
}
int
__db_txnlist_remove(env, hp, txnid)
ENV *env;
DB_TXNHEAD *hp;
u_int32_t txnid;
{
DB_TXNLIST *entry;
u_int32_t status;
return (__db_txnlist_find_internal(env,
hp, TXNLIST_TXNID, txnid, &entry, 1, &status));
}
void
__db_txnlist_ckp(env, hp, ckp_lsn)
ENV *env;
DB_TXNHEAD *hp;
DB_LSN *ckp_lsn;
{
COMPQUIET(env, NULL);
if (IS_ZERO_LSN(hp->ckplsn) && !IS_ZERO_LSN(hp->maxlsn) &&
LOG_COMPARE(&hp->maxlsn, ckp_lsn) >= 0)
hp->ckplsn = *ckp_lsn;
}
void
__db_txnlist_end(env, hp)
ENV *env;
DB_TXNHEAD *hp;
{
u_int32_t i;
DB_TXNLIST *p;
if (hp == NULL)
return;
for (i = 0; i < hp->nslots; i++)
while (hp != NULL && (p = LIST_FIRST(&hp->head[i])) != NULL) {
switch (p->type) {
case TXNLIST_LSN:
__os_free(env, p->u.l.lsn_stack);
break;
case TXNLIST_DELETE:
case TXNLIST_TXNID:
default:
break;
}
LIST_REMOVE(p, links);
__os_free(env, p);
}
if (hp->gen_array != NULL)
__os_free(env, hp->gen_array);
__os_free(env, hp);
}
int
__db_txnlist_find(env, hp, txnid, statusp)
ENV *env;
DB_TXNHEAD *hp;
u_int32_t txnid, *statusp;
{
DB_TXNLIST *entry;
if (txnid == 0)
return (DB_NOTFOUND);
return (__db_txnlist_find_internal(env, hp,
TXNLIST_TXNID, txnid, &entry, 0, statusp));
}
int
__db_txnlist_update(env, hp, txnid, status, lsn, ret_status, add_ok)
ENV *env;
DB_TXNHEAD *hp;
u_int32_t txnid, status;
DB_LSN *lsn;
u_int32_t *ret_status;
int add_ok;
{
DB_TXNLIST *elp;
int ret;
if (txnid == 0)
return (DB_NOTFOUND);
ret = __db_txnlist_find_internal(env,
hp, TXNLIST_TXNID, txnid, &elp, 0, ret_status);
if (ret == DB_NOTFOUND && add_ok) {
*ret_status = status;
return (__db_txnlist_add(env, hp, txnid, status, lsn));
}
if (ret != 0)
return (ret);
if (*ret_status == TXN_IGNORE)
return (0);
elp->u.t.status = status;
if (lsn != NULL && IS_ZERO_LSN(hp->maxlsn) && status == TXN_COMMIT)
hp->maxlsn = *lsn;
return (ret);
}
static int
__db_txnlist_find_internal(env,
hp, type, txnid, txnlistp, delete, statusp)
ENV *env;
DB_TXNHEAD *hp;
db_txnlist_type type;
u_int32_t txnid;
DB_TXNLIST **txnlistp;
int delete;
u_int32_t *statusp;
{
struct __db_headlink *head;
DB_TXNLIST *p;
u_int32_t generation, hash;
int ret;
ret = 0;
if (hp == NULL)
return (DB_NOTFOUND);
switch (type) {
case TXNLIST_TXNID:
hash = txnid;
FIND_GENERATION(hp, txnid, generation);
break;
case TXNLIST_DELETE:
case TXNLIST_LSN:
default:
return (__env_panic(env, EINVAL));
}
head = &hp->head[DB_TXNLIST_MASK(hp, hash)];
LIST_FOREACH(p, head, links) {
if (p->type != type)
continue;
switch (type) {
case TXNLIST_TXNID:
if (p->u.t.txnid != txnid ||
generation != p->u.t.generation)
continue;
*statusp = p->u.t.status;
break;
case TXNLIST_DELETE:
case TXNLIST_LSN:
default:
return (__env_panic(env, EINVAL));
}
if (delete == 1) {
LIST_REMOVE(p, links);
__os_free(env, p);
*txnlistp = NULL;
} else if (p != LIST_FIRST(head)) {
LIST_REMOVE(p, links);
LIST_INSERT_HEAD(head, p, links);
*txnlistp = p;
} else
*txnlistp = p;
return (ret);
}
return (DB_NOTFOUND);
}
int
__db_txnlist_gen(env, hp, incr, min, max)
ENV *env;
DB_TXNHEAD *hp;
int incr;
u_int32_t min, max;
{
int ret;
if (incr < 0) {
--hp->generation;
memmove(hp->gen_array, &hp->gen_array[1],
(hp->generation + 1) * sizeof(hp->gen_array[0]));
} else {
++hp->generation;
if (hp->generation >= hp->gen_alloc) {
hp->gen_alloc *= 2;
if ((ret = __os_realloc(env, hp->gen_alloc *
sizeof(hp->gen_array[0]), &hp->gen_array)) != 0)
return (ret);
}
memmove(&hp->gen_array[1], &hp->gen_array[0],
hp->generation * sizeof(hp->gen_array[0]));
hp->gen_array[0].generation = hp->generation;
hp->gen_array[0].txn_min = min;
hp->gen_array[0].txn_max = max;
}
return (0);
}
int
__db_txnlist_lsnadd(env, hp, lsnp)
ENV *env;
DB_TXNHEAD *hp;
DB_LSN *lsnp;
{
DB_TXNLIST *elp;
int ret;
if (IS_ZERO_LSN(*lsnp))
return (0);
LIST_FOREACH(elp, &hp->head[0], links)
if (elp->type == TXNLIST_LSN)
break;
if (elp == NULL) {
if ((ret = __db_txnlist_lsninit(env, hp, lsnp)) != 0)
return (ret);
return (DB_SURPRISE_KID);
}
if (elp->u.l.stack_indx == elp->u.l.stack_size) {
elp->u.l.stack_size <<= 1;
if ((ret = __os_realloc(env, sizeof(DB_LSN) *
elp->u.l.stack_size, &elp->u.l.lsn_stack)) != 0) {
__db_txnlist_end(env, hp);
return (ret);
}
}
elp->u.l.lsn_stack[elp->u.l.stack_indx++] = *lsnp;
return (0);
}
int
__db_txnlist_lsnget(env, hp, lsnp, flags)
ENV *env;
DB_TXNHEAD *hp;
DB_LSN *lsnp;
u_int32_t flags;
{
DB_TXNLIST *elp;
COMPQUIET(env, NULL);
COMPQUIET(flags, 0);
LIST_FOREACH(elp, &hp->head[0], links)
if (elp->type == TXNLIST_LSN)
break;
if (elp == NULL || elp->u.l.stack_indx == 0) {
ZERO_LSN(*lsnp);
return (0);
}
*lsnp = elp->u.l.lsn_stack[--elp->u.l.stack_indx];
return (0);
}
int
__db_txnlist_lsninit(env, hp, lsnp)
ENV *env;
DB_TXNHEAD *hp;
DB_LSN *lsnp;
{
DB_TXNLIST *elp;
int ret;
elp = NULL;
if ((ret = __os_malloc(env, sizeof(DB_TXNLIST), &elp)) != 0)
goto err;
LIST_INSERT_HEAD(&hp->head[0], elp, links);
elp->type = TXNLIST_LSN;
if ((ret = __os_malloc(env,
sizeof(DB_LSN) * DB_LSN_STACK_SIZE, &elp->u.l.lsn_stack)) != 0)
goto err;
elp->u.l.stack_indx = 1;
elp->u.l.stack_size = DB_LSN_STACK_SIZE;
elp->u.l.lsn_stack[0] = *lsnp;
return (0);
err: __db_txnlist_end(env, hp);
return (ret);
}
#ifdef DEBUG
void
__db_txnlist_print(hp)
DB_TXNHEAD *hp;
{
DB_TXNLIST *p;
u_int32_t i;
char *txntype;
printf("Maxid: %lu Generation: %lu\n",
(u_long)hp->maxid, (u_long)hp->generation);
for (i = 0; i < hp->nslots; i++)
LIST_FOREACH(p, &hp->head[i], links) {
if (p->type != TXNLIST_TXNID) {
printf("Unrecognized type: %d\n", p->type);
continue;
}
switch (p->u.t.status) {
case TXN_OK:
txntype = "OK";
break;
case TXN_COMMIT:
txntype = "commit";
break;
case TXN_PREPARE:
txntype = "prepare";
break;
case TXN_ABORT:
txntype = "abort";
break;
case TXN_IGNORE:
txntype = "ignore";
break;
case TXN_EXPECTED:
txntype = "expected";
break;
case TXN_UNEXPECTED:
txntype = "unexpected";
break;
default:
txntype = "UNKNOWN";
break;
}
printf("TXNID: %lx(%lu): %s\n",
(u_long)p->u.t.txnid,
(u_long)p->u.t.generation, txntype);
}
}
#endif