2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1996-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
17
#if TIME_WITH_SYS_TIME
32
#include "dbinc/log.h"
33
#include "dbinc/txn.h"
35
static int __txn_findlastckp __P((DB_ENV *, DB_LSN *));
36
static int __txn_init __P((DB_ENV *, DB_TXNMGR *));
37
static size_t __txn_region_size __P((DB_ENV *));
41
* Open a transaction region.
43
* PUBLIC: int __txn_open __P((DB_ENV *));
52
/* Create/initialize the transaction manager structure. */
53
if ((ret = __os_calloc(dbenv, 1, sizeof(DB_TXNMGR), &tmgrp)) != 0)
55
TAILQ_INIT(&tmgrp->txn_chain);
58
/* Join/create the txn region. */
59
tmgrp->reginfo.type = REGION_TYPE_TXN;
60
tmgrp->reginfo.id = INVALID_REGION_ID;
61
tmgrp->reginfo.mode = dbenv->db_mode;
62
tmgrp->reginfo.flags = REGION_JOIN_OK;
63
if (F_ISSET(dbenv, DB_ENV_CREATE))
64
F_SET(&tmgrp->reginfo, REGION_CREATE_OK);
65
if ((ret = __db_r_attach(dbenv,
66
&tmgrp->reginfo, __txn_region_size(dbenv))) != 0)
69
/* If we created the region, initialize it. */
70
if (F_ISSET(&tmgrp->reginfo, REGION_CREATE))
71
if ((ret = __txn_init(dbenv, tmgrp)) != 0)
74
/* Set the local addresses. */
75
tmgrp->reginfo.primary =
76
R_ADDR(&tmgrp->reginfo, tmgrp->reginfo.rp->primary);
78
/* Acquire a mutex to protect the active TXN list. */
79
if (F_ISSET(dbenv, DB_ENV_THREAD) &&
80
(ret = __db_mutex_setup(dbenv, &tmgrp->reginfo, &tmgrp->mutexp,
81
MUTEX_ALLOC | MUTEX_NO_RLOCK | MUTEX_THREAD)) != 0)
84
R_UNLOCK(dbenv, &tmgrp->reginfo);
86
dbenv->tx_handle = tmgrp;
89
err: if (tmgrp->reginfo.addr != NULL) {
90
if (F_ISSET(&tmgrp->reginfo, REGION_CREATE))
91
ret = __db_panic(dbenv, ret);
92
R_UNLOCK(dbenv, &tmgrp->reginfo);
94
(void)__db_r_detach(dbenv, &tmgrp->reginfo, 0);
96
if (tmgrp->mutexp != NULL)
97
__db_mutex_free(dbenv, &tmgrp->reginfo, tmgrp->mutexp);
98
__os_free(dbenv, tmgrp);
104
* Initialize a transaction region in shared memory.
107
__txn_init(dbenv, tmgrp)
112
DB_TXNREGION *region;
114
#ifdef HAVE_MUTEX_SYSTEM_RESOURCES
119
* Find the last checkpoint in the log.
122
if (LOGGING_ON(dbenv)) {
124
* The log system has already walked through the last
125
* file. Get the LSN of a checkpoint it may have found.
127
__log_get_cached_ckp_lsn(dbenv, &last_ckp);
130
* If that didn't work, look backwards from the beginning of
131
* the last log file until we find the last checkpoint.
133
if (IS_ZERO_LSN(last_ckp) &&
134
(ret = __txn_findlastckp(dbenv, &last_ckp)) != 0)
138
if ((ret = __db_shalloc(tmgrp->reginfo.addr,
139
sizeof(DB_TXNREGION), 0, &tmgrp->reginfo.primary)) != 0) {
141
"Unable to allocate memory for the transaction region");
144
tmgrp->reginfo.rp->primary =
145
R_OFFSET(&tmgrp->reginfo, tmgrp->reginfo.primary);
146
region = tmgrp->reginfo.primary;
147
memset(region, 0, sizeof(*region));
149
region->maxtxns = dbenv->tx_max;
150
region->last_txnid = TXN_MINIMUM;
151
region->cur_maxid = TXN_MAXIMUM;
152
region->last_ckp = last_ckp;
153
region->time_ckp = time(NULL);
157
* If we ever do more types of locking and logging, this changes.
160
region->locktype = 0;
162
memset(®ion->stat, 0, sizeof(region->stat));
163
region->stat.st_maxtxns = region->maxtxns;
165
SH_TAILQ_INIT(®ion->active_txn);
166
#ifdef HAVE_MUTEX_SYSTEM_RESOURCES
167
/* Allocate room for the txn maintenance info and initialize it. */
168
if ((ret = __db_shalloc(tmgrp->reginfo.addr,
169
sizeof(REGMAINT) + TXN_MAINT_SIZE, 0, &addr)) != 0) {
171
"Unable to allocate memory for mutex maintenance");
174
__db_maintinit(&tmgrp->reginfo, addr, TXN_MAINT_SIZE);
175
region->maint_off = R_OFFSET(&tmgrp->reginfo, addr);
181
* __txn_findlastckp --
182
* Find the last checkpoint in the log, walking backwards from the
183
* beginning of the last log file. (The log system looked through
184
* the last log file when it started up.)
187
__txn_findlastckp(dbenv, lsnp)
197
if ((ret = dbenv->log_cursor(dbenv, &logc, 0)) != 0)
200
/* Get the last LSN. */
201
memset(&dbt, 0, sizeof(dbt));
202
if ((ret = logc->get(logc, &lsn, &dbt, DB_LAST)) != 0)
206
* Twiddle the last LSN so it points to the beginning of the last
207
* file; we know there's no checkpoint after that, since the log
208
* system already looked there.
212
/* Read backwards, looking for checkpoints. */
213
while ((ret = logc->get(logc, &lsn, &dbt, DB_PREV)) == 0) {
214
if (dbt.size < sizeof(u_int32_t))
216
memcpy(&rectype, dbt.data, sizeof(u_int32_t));
217
if (rectype == DB___txn_ckp) {
223
err: if ((t_ret = logc->close(logc, 0)) != 0 && ret == 0)
226
* Not finding a checkpoint is not an error; there may not exist
229
return ((ret == 0 || ret == DB_NOTFOUND) ? 0 : ret);
233
* __txn_dbenv_refresh --
234
* Clean up after the transaction system on a close or failed open.
235
* Called only from __dbenv_refresh. (Formerly called __txn_close.)
237
* PUBLIC: int __txn_dbenv_refresh __P((DB_ENV *));
240
__txn_dbenv_refresh(dbenv)
249
tmgrp = dbenv->tx_handle;
252
* This function can only be called once per process (i.e., not
253
* once per thread), so no synchronization is required.
255
* The caller is doing something wrong if close is called with
256
* active transactions. Try and abort any active transactions,
257
* but it's quite likely the aborts will fail because recovery
258
* won't find open files. If we can't abort any transaction,
259
* panic, we have to run recovery to get back to a known state.
261
if (TAILQ_FIRST(&tmgrp->txn_chain) != NULL) {
263
"Error: closing the transaction region with active transactions");
265
while ((txnp = TAILQ_FIRST(&tmgrp->txn_chain)) != NULL) {
267
if ((t_ret = txnp->abort(txnp)) != 0) {
269
"Unable to abort transaction 0x%x: %s",
270
txnid, db_strerror(t_ret));
271
ret = __db_panic(dbenv, t_ret);
278
if (LOGGING_ON(dbenv) &&
279
(t_ret = dbenv->log_flush(dbenv, NULL)) != 0 && ret == 0)
282
/* Discard the per-thread lock. */
283
if (tmgrp->mutexp != NULL)
284
__db_mutex_free(dbenv, &tmgrp->reginfo, tmgrp->mutexp);
286
/* Detach from the region. */
287
if ((t_ret = __db_r_detach(dbenv, &tmgrp->reginfo, 0)) != 0 && ret == 0)
290
__os_free(dbenv, tmgrp);
292
dbenv->tx_handle = NULL;
297
* __txn_region_size --
298
* Return the amount of space needed for the txn region. Make the
299
* region large enough to hold txn_max transaction detail structures
300
* plus some space to hold thread handles and the beginning of the
301
* shalloc region and anything we need for mutex system resource
305
__txn_region_size(dbenv)
310
s = sizeof(DB_TXNREGION) +
311
dbenv->tx_max * sizeof(TXN_DETAIL) + 10 * 1024;
312
#ifdef HAVE_MUTEX_SYSTEM_RESOURCES
313
if (F_ISSET(dbenv, DB_ENV_THREAD))
314
s += sizeof(REGMAINT) + TXN_MAINT_SIZE;
320
* __txn_region_destroy
321
* Destroy any region maintenance info.
323
* PUBLIC: void __txn_region_destroy __P((DB_ENV *, REGINFO *));
326
__txn_region_destroy(dbenv, infop)
330
__db_shlocks_destroy(infop, (REGMAINT *)R_ADDR(infop,
331
((DB_TXNREGION *)R_ADDR(infop, infop->rp->primary))->maint_off));
333
COMPQUIET(dbenv, NULL);
334
COMPQUIET(infop, NULL);
340
* Set the current transaction ID and current maximum unused ID (for
341
* testing purposes only).
343
* PUBLIC: int __txn_id_set __P((DB_ENV *, u_int32_t, u_int32_t));
346
__txn_id_set(dbenv, cur_txnid, max_txnid)
348
u_int32_t cur_txnid, max_txnid;
351
DB_TXNREGION *region;
354
ENV_REQUIRES_CONFIG(dbenv, dbenv->tx_handle, "txn_id_set", DB_INIT_TXN);
356
mgr = dbenv->tx_handle;
357
region = mgr->reginfo.primary;
358
region->last_txnid = cur_txnid;
359
region->cur_maxid = max_txnid;
362
if (cur_txnid < TXN_MINIMUM) {
363
__db_err(dbenv, "Current ID value %lu below minimum",
367
if (max_txnid < TXN_MINIMUM) {
368
__db_err(dbenv, "Maximum ID value %lu below minimum",