|
@@ -169,10 +169,9 @@ static int transaction_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
|
|
|
return 0;
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
fail:
|
|
|
- tdb->ecode = TDB_ERR_IO;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "transaction_read: failed at off=%llu len=%llu\n",
|
|
|
|
|
- (long long)off, (long long)len);
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "transaction_read: failed at off=%zu len=%zu",
|
|
|
|
|
+ (size_t)off, (size_t)len);
|
|
|
tdb->transaction->transaction_error = 1;
|
|
tdb->transaction->transaction_error = 1;
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -188,12 +187,10 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
|
|
|
|
|
|
|
|
/* Only a commit is allowed on a prepared transaction */
|
|
/* Only a commit is allowed on a prepared transaction */
|
|
|
if (tdb->transaction->prepared) {
|
|
if (tdb->transaction->prepared) {
|
|
|
- tdb->ecode = TDB_ERR_EINVAL;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_FATAL,
|
|
|
"transaction_write: transaction already prepared,"
|
|
"transaction_write: transaction already prepared,"
|
|
|
- " write not allowed\n");
|
|
|
|
|
- tdb->transaction->transaction_error = 1;
|
|
|
|
|
- return -1;
|
|
|
|
|
|
|
+ " write not allowed");
|
|
|
|
|
+ goto fail;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* break it up into block sized chunks */
|
|
/* break it up into block sized chunks */
|
|
@@ -228,7 +225,8 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
|
|
|
(blk+1)*sizeof(uint8_t *));
|
|
(blk+1)*sizeof(uint8_t *));
|
|
|
}
|
|
}
|
|
|
if (new_blocks == NULL) {
|
|
if (new_blocks == NULL) {
|
|
|
- tdb->ecode = TDB_ERR_OOM;
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "transaction_write: failed to allocate");
|
|
|
goto fail;
|
|
goto fail;
|
|
|
}
|
|
}
|
|
|
memset(&new_blocks[tdb->transaction->num_blocks], 0,
|
|
memset(&new_blocks[tdb->transaction->num_blocks], 0,
|
|
@@ -242,9 +240,9 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
|
|
|
if (tdb->transaction->blocks[blk] == NULL) {
|
|
if (tdb->transaction->blocks[blk] == NULL) {
|
|
|
tdb->transaction->blocks[blk] = (uint8_t *)calloc(getpagesize(), 1);
|
|
tdb->transaction->blocks[blk] = (uint8_t *)calloc(getpagesize(), 1);
|
|
|
if (tdb->transaction->blocks[blk] == NULL) {
|
|
if (tdb->transaction->blocks[blk] == NULL) {
|
|
|
- tdb->ecode = TDB_ERR_OOM;
|
|
|
|
|
- tdb->transaction->transaction_error = 1;
|
|
|
|
|
- return -1;
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "transaction_write: failed to allocate");
|
|
|
|
|
+ goto fail;
|
|
|
}
|
|
}
|
|
|
if (tdb->transaction->old_map_size > blk * getpagesize()) {
|
|
if (tdb->transaction->old_map_size > blk * getpagesize()) {
|
|
|
tdb_len_t len2 = getpagesize();
|
|
tdb_len_t len2 = getpagesize();
|
|
@@ -254,6 +252,10 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
|
|
|
if (tdb->transaction->io_methods->read(tdb, blk * getpagesize(),
|
|
if (tdb->transaction->io_methods->read(tdb, blk * getpagesize(),
|
|
|
tdb->transaction->blocks[blk],
|
|
tdb->transaction->blocks[blk],
|
|
|
len2) != 0) {
|
|
len2) != 0) {
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "transaction_write: failed to"
|
|
|
|
|
+ " read old block: %s",
|
|
|
|
|
+ strerror(errno));
|
|
|
SAFE_FREE(tdb->transaction->blocks[blk]);
|
|
SAFE_FREE(tdb->transaction->blocks[blk]);
|
|
|
goto fail;
|
|
goto fail;
|
|
|
}
|
|
}
|
|
@@ -278,10 +280,6 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
|
|
|
return 0;
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
fail:
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "transaction_write: failed at off=%llu len=%llu\n",
|
|
|
|
|
- (long long)((blk*getpagesize()) + off),
|
|
|
|
|
- (long long)len);
|
|
|
|
|
tdb->transaction->transaction_error = 1;
|
|
tdb->transaction->transaction_error = 1;
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -341,6 +339,12 @@ static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
tdb->ecode = TDB_ERR_IO;
|
|
tdb->ecode = TDB_ERR_IO;
|
|
|
|
|
+ if (!probe) {
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_oob len %lld beyond transaction size %lld",
|
|
|
|
|
+ (long long)len,
|
|
|
|
|
+ (long long)tdb->map_size);
|
|
|
|
|
+ }
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -359,10 +363,39 @@ static int transaction_expand_file(struct tdb_context *tdb, tdb_off_t addition)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void *transaction_direct(struct tdb_context *tdb, tdb_off_t off,
|
|
static void *transaction_direct(struct tdb_context *tdb, tdb_off_t off,
|
|
|
- size_t len)
|
|
|
|
|
|
|
+ size_t len, bool write)
|
|
|
{
|
|
{
|
|
|
- /* FIXME */
|
|
|
|
|
- return NULL;
|
|
|
|
|
|
|
+ size_t blk = off / getpagesize(), end_blk;
|
|
|
|
|
+
|
|
|
|
|
+ /* This is wrong for zero-length blocks, but will fail gracefully */
|
|
|
|
|
+ end_blk = (off + len - 1) / getpagesize();
|
|
|
|
|
+
|
|
|
|
|
+ /* Can only do direct if in single block and we've already copied. */
|
|
|
|
|
+ if (write) {
|
|
|
|
|
+ if (blk != end_blk)
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ if (blk >= tdb->transaction->num_blocks)
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ if (tdb->transaction->blocks[blk] == NULL)
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ return tdb->transaction->blocks[blk] + off % getpagesize();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /* Single which we have copied? */
|
|
|
|
|
+ if (blk == end_blk
|
|
|
|
|
+ && blk < tdb->transaction->num_blocks
|
|
|
|
|
+ && tdb->transaction->blocks[blk])
|
|
|
|
|
+ return tdb->transaction->blocks[blk] + off % getpagesize();
|
|
|
|
|
+
|
|
|
|
|
+ /* Otherwise must be all not copied. */
|
|
|
|
|
+ while (blk < end_blk) {
|
|
|
|
|
+ if (blk >= tdb->transaction->num_blocks)
|
|
|
|
|
+ break;
|
|
|
|
|
+ if (tdb->transaction->blocks[blk])
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ blk++;
|
|
|
|
|
+ }
|
|
|
|
|
+ return tdb->transaction->io_methods->direct(tdb, off, len, write);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static const struct tdb_methods transaction_methods = {
|
|
static const struct tdb_methods transaction_methods = {
|
|
@@ -383,9 +416,9 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (fsync(tdb->fd) != 0) {
|
|
if (fsync(tdb->fd) != 0) {
|
|
|
- tdb->ecode = TDB_ERR_IO;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction: fsync failed\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction: fsync failed: %s",
|
|
|
|
|
+ strerror(errno));
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
#ifdef MS_SYNC
|
|
#ifdef MS_SYNC
|
|
@@ -393,10 +426,9 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t
|
|
|
tdb_off_t moffset = offset & ~(getpagesize()-1);
|
|
tdb_off_t moffset = offset & ~(getpagesize()-1);
|
|
|
if (msync(moffset + (char *)tdb->map_ptr,
|
|
if (msync(moffset + (char *)tdb->map_ptr,
|
|
|
length + (offset - moffset), MS_SYNC) != 0) {
|
|
length + (offset - moffset), MS_SYNC) != 0) {
|
|
|
- tdb->ecode = TDB_ERR_IO;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction: msync failed - %s\n",
|
|
|
|
|
- strerror(errno));
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction: msync failed: %s",
|
|
|
|
|
+ strerror(errno));
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -410,9 +442,8 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
|
|
|
int i;
|
|
int i;
|
|
|
|
|
|
|
|
if (tdb->transaction == NULL) {
|
|
if (tdb->transaction == NULL) {
|
|
|
- tdb->ecode = TDB_ERR_EINVAL;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_cancel: no transaction\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_cancel: no transaction");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -441,9 +472,9 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
|
|
|
&invalid, sizeof(invalid)) == -1 ||
|
|
&invalid, sizeof(invalid)) == -1 ||
|
|
|
transaction_sync(tdb, tdb->transaction->magic_offset,
|
|
transaction_sync(tdb, tdb->transaction->magic_offset,
|
|
|
sizeof(invalid)) == -1) {
|
|
sizeof(invalid)) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_cancel: failed to remove"
|
|
|
|
|
- " recovery magic\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_cancel: failed to remove"
|
|
|
|
|
+ " recovery magic");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -469,16 +500,17 @@ int tdb_transaction_start(struct tdb_context *tdb)
|
|
|
{
|
|
{
|
|
|
/* some sanity checks */
|
|
/* some sanity checks */
|
|
|
if (tdb->read_only || (tdb->flags & TDB_INTERNAL)) {
|
|
if (tdb->read_only || (tdb->flags & TDB_INTERNAL)) {
|
|
|
- tdb->ecode = TDB_ERR_EINVAL;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_start: cannot start a transaction"
|
|
|
|
|
- " on a read-only or internal db\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_start: cannot start a transaction"
|
|
|
|
|
+ " on a read-only or internal db");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* cope with nested tdb_transaction_start() calls */
|
|
/* cope with nested tdb_transaction_start() calls */
|
|
|
if (tdb->transaction != NULL) {
|
|
if (tdb->transaction != NULL) {
|
|
|
- tdb->ecode = TDB_ERR_NESTING;
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_NESTING, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_start:"
|
|
|
|
|
+ " already inside transaction");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -486,17 +518,17 @@ int tdb_transaction_start(struct tdb_context *tdb)
|
|
|
/* the caller must not have any locks when starting a
|
|
/* the caller must not have any locks when starting a
|
|
|
transaction as otherwise we'll be screwed by lack
|
|
transaction as otherwise we'll be screwed by lack
|
|
|
of nested locks in posix */
|
|
of nested locks in posix */
|
|
|
- tdb->ecode = TDB_ERR_LOCK;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_start: cannot start a transaction"
|
|
|
|
|
- " with locks held\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_start: cannot start a transaction"
|
|
|
|
|
+ " with locks held");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
tdb->transaction = (struct tdb_transaction *)
|
|
tdb->transaction = (struct tdb_transaction *)
|
|
|
calloc(sizeof(struct tdb_transaction), 1);
|
|
calloc(sizeof(struct tdb_transaction), 1);
|
|
|
if (tdb->transaction == NULL) {
|
|
if (tdb->transaction == NULL) {
|
|
|
- tdb->ecode = TDB_ERR_OOM;
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_start: cannot allocate");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -585,17 +617,17 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
|
|
|
|
|
|
|
|
recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery));
|
|
recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery));
|
|
|
if (recovery_head == TDB_OFF_ERR) {
|
|
if (recovery_head == TDB_OFF_ERR) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_recovery_allocate:"
|
|
"tdb_recovery_allocate:"
|
|
|
- " failed to read recovery head\n");
|
|
|
|
|
|
|
+ " failed to read recovery head");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (recovery_head != 0) {
|
|
if (recovery_head != 0) {
|
|
|
if (methods->read(tdb, recovery_head, &rec, sizeof(rec))) {
|
|
if (methods->read(tdb, recovery_head, &rec, sizeof(rec))) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_recovery_allocate:"
|
|
"tdb_recovery_allocate:"
|
|
|
- " failed to read recovery record\n");
|
|
|
|
|
|
|
+ " failed to read recovery record");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
tdb_convert(tdb, &rec, sizeof(rec));
|
|
tdb_convert(tdb, &rec, sizeof(rec));
|
|
@@ -621,11 +653,12 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
|
|
|
us an area that is being currently used (as of the start of
|
|
us an area that is being currently used (as of the start of
|
|
|
the transaction) */
|
|
the transaction) */
|
|
|
if (recovery_head != 0) {
|
|
if (recovery_head != 0) {
|
|
|
|
|
+ add_stat(tdb, frees, 1);
|
|
|
if (add_free_record(tdb, recovery_head,
|
|
if (add_free_record(tdb, recovery_head,
|
|
|
sizeof(rec) + rec.max_len) != 0) {
|
|
sizeof(rec) + rec.max_len) != 0) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_recovery_allocate:"
|
|
|
|
|
- " failed to free previous recovery area\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_recovery_allocate:"
|
|
|
|
|
+ " failed to free previous recovery area");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -649,9 +682,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
|
|
|
sizeof(rec) + *recovery_max_size;
|
|
sizeof(rec) + *recovery_max_size;
|
|
|
tdb->map_size = tdb->transaction->old_map_size;
|
|
tdb->map_size = tdb->transaction->old_map_size;
|
|
|
if (methods->expand_file(tdb, addition) == -1) {
|
|
if (methods->expand_file(tdb, addition) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_recovery_allocate:"
|
|
"tdb_recovery_allocate:"
|
|
|
- " failed to create recovery area\n");
|
|
|
|
|
|
|
+ " failed to create recovery area");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -665,9 +698,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
|
|
|
tdb_convert(tdb, &recovery_head, sizeof(recovery_head));
|
|
tdb_convert(tdb, &recovery_head, sizeof(recovery_head));
|
|
|
if (methods->write(tdb, offsetof(struct tdb_header, recovery),
|
|
if (methods->write(tdb, offsetof(struct tdb_header, recovery),
|
|
|
&recovery_head, sizeof(tdb_off_t)) == -1) {
|
|
&recovery_head, sizeof(tdb_off_t)) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_recovery_allocate:"
|
|
"tdb_recovery_allocate:"
|
|
|
- " failed to write recovery head\n");
|
|
|
|
|
|
|
+ " failed to write recovery head");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
transaction_write_existing(tdb, offsetof(struct tdb_header, recovery),
|
|
transaction_write_existing(tdb, offsetof(struct tdb_header, recovery),
|
|
@@ -713,7 +746,8 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
|
|
|
|
|
|
|
|
data = (unsigned char *)malloc(recovery_size + sizeof(*rec));
|
|
data = (unsigned char *)malloc(recovery_size + sizeof(*rec));
|
|
|
if (data == NULL) {
|
|
if (data == NULL) {
|
|
|
- tdb->ecode = TDB_ERR_OOM;
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "transaction_setup_recovery: cannot allocate");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -743,10 +777,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
if (offset + length > tdb->map_size) {
|
|
if (offset + length > tdb->map_size) {
|
|
|
- tdb->ecode = TDB_ERR_CORRUPT;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_setup_recovery:"
|
|
|
|
|
- " transaction data over new region boundary\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_setup_recovery:"
|
|
|
|
|
+ " transaction data over new region boundary");
|
|
|
free(data);
|
|
free(data);
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -774,9 +807,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
|
|
|
/* write the recovery data to the recovery area */
|
|
/* write the recovery data to the recovery area */
|
|
|
if (methods->write(tdb, recovery_offset, data,
|
|
if (methods->write(tdb, recovery_offset, data,
|
|
|
sizeof(*rec) + recovery_size) == -1) {
|
|
sizeof(*rec) + recovery_size) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_transaction_setup_recovery:"
|
|
"tdb_transaction_setup_recovery:"
|
|
|
- " failed to write recovery data\n");
|
|
|
|
|
|
|
+ " failed to write recovery data");
|
|
|
free(data);
|
|
free(data);
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -801,9 +834,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
|
|
|
magic);
|
|
magic);
|
|
|
|
|
|
|
|
if (methods->write(tdb, *magic_offset, &magic, sizeof(magic)) == -1) {
|
|
if (methods->write(tdb, *magic_offset, &magic, sizeof(magic)) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_transaction_setup_recovery:"
|
|
"tdb_transaction_setup_recovery:"
|
|
|
- " failed to write recovery magic\n");
|
|
|
|
|
|
|
+ " failed to write recovery magic");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
transaction_write_existing(tdb, *magic_offset, &magic, sizeof(magic));
|
|
transaction_write_existing(tdb, *magic_offset, &magic, sizeof(magic));
|
|
@@ -821,27 +854,24 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
|
|
|
const struct tdb_methods *methods;
|
|
const struct tdb_methods *methods;
|
|
|
|
|
|
|
|
if (tdb->transaction == NULL) {
|
|
if (tdb->transaction == NULL) {
|
|
|
- tdb->ecode = TDB_ERR_EINVAL;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_prepare_commit: no transaction\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_prepare_commit: no transaction");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (tdb->transaction->prepared) {
|
|
if (tdb->transaction->prepared) {
|
|
|
- tdb->ecode = TDB_ERR_EINVAL;
|
|
|
|
|
_tdb_transaction_cancel(tdb);
|
|
_tdb_transaction_cancel(tdb);
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_prepare_commit:"
|
|
|
|
|
- " transaction already prepared\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_prepare_commit:"
|
|
|
|
|
+ " transaction already prepared");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (tdb->transaction->transaction_error) {
|
|
if (tdb->transaction->transaction_error) {
|
|
|
- tdb->ecode = TDB_ERR_IO;
|
|
|
|
|
_tdb_transaction_cancel(tdb);
|
|
_tdb_transaction_cancel(tdb);
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_prepare_commit:"
|
|
|
|
|
- " transaction error pending\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_prepare_commit:"
|
|
|
|
|
+ " transaction error pending");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -860,9 +890,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
|
|
|
|
|
|
|
|
/* upgrade the main transaction lock region to a write lock */
|
|
/* upgrade the main transaction lock region to a write lock */
|
|
|
if (tdb_allrecord_upgrade(tdb) == -1) {
|
|
if (tdb_allrecord_upgrade(tdb) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
|
|
|
"tdb_transaction_prepare_commit:"
|
|
"tdb_transaction_prepare_commit:"
|
|
|
- " failed to upgrade hash locks\n");
|
|
|
|
|
|
|
+ " failed to upgrade hash locks");
|
|
|
_tdb_transaction_cancel(tdb);
|
|
_tdb_transaction_cancel(tdb);
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -870,9 +900,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
|
|
|
/* get the open lock - this prevents new users attaching to the database
|
|
/* get the open lock - this prevents new users attaching to the database
|
|
|
during the commit */
|
|
during the commit */
|
|
|
if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
|
|
if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
|
|
|
"tdb_transaction_prepare_commit:"
|
|
"tdb_transaction_prepare_commit:"
|
|
|
- " failed to get open lock\n");
|
|
|
|
|
|
|
+ " failed to get open lock");
|
|
|
_tdb_transaction_cancel(tdb);
|
|
_tdb_transaction_cancel(tdb);
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -881,9 +911,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
|
|
|
if (!(tdb->flags & TDB_NOSYNC)) {
|
|
if (!(tdb->flags & TDB_NOSYNC)) {
|
|
|
/* write the recovery data to the end of the file */
|
|
/* write the recovery data to the end of the file */
|
|
|
if (transaction_setup_recovery(tdb, &tdb->transaction->magic_offset) == -1) {
|
|
if (transaction_setup_recovery(tdb, &tdb->transaction->magic_offset) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_transaction_prepare_commit:"
|
|
"tdb_transaction_prepare_commit:"
|
|
|
- " failed to setup recovery data\n");
|
|
|
|
|
|
|
+ " failed to setup recovery data");
|
|
|
_tdb_transaction_cancel(tdb);
|
|
_tdb_transaction_cancel(tdb);
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -897,9 +927,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
|
|
|
/* Restore original map size for tdb_expand_file */
|
|
/* Restore original map size for tdb_expand_file */
|
|
|
tdb->map_size = tdb->transaction->old_map_size;
|
|
tdb->map_size = tdb->transaction->old_map_size;
|
|
|
if (methods->expand_file(tdb, add) == -1) {
|
|
if (methods->expand_file(tdb, add) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
|
|
|
"tdb_transaction_prepare_commit:"
|
|
"tdb_transaction_prepare_commit:"
|
|
|
- " expansion failed\n");
|
|
|
|
|
|
|
+ " expansion failed");
|
|
|
_tdb_transaction_cancel(tdb);
|
|
_tdb_transaction_cancel(tdb);
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -927,19 +957,18 @@ int tdb_transaction_commit(struct tdb_context *tdb)
|
|
|
int i;
|
|
int i;
|
|
|
|
|
|
|
|
if (tdb->transaction == NULL) {
|
|
if (tdb->transaction == NULL) {
|
|
|
- tdb->ecode = TDB_ERR_EINVAL;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_commit: no transaction\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_commit: no transaction");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
tdb_trace(tdb, "tdb_transaction_commit");
|
|
tdb_trace(tdb, "tdb_transaction_commit");
|
|
|
|
|
|
|
|
if (tdb->transaction->transaction_error) {
|
|
if (tdb->transaction->transaction_error) {
|
|
|
- tdb->ecode = TDB_ERR_IO;
|
|
|
|
|
tdb_transaction_cancel(tdb);
|
|
tdb_transaction_cancel(tdb);
|
|
|
- tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_commit: transaction error pending\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
|
|
|
|
|
+ "tdb_transaction_commit:"
|
|
|
|
|
+ " transaction error pending");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -980,9 +1009,9 @@ int tdb_transaction_commit(struct tdb_context *tdb)
|
|
|
|
|
|
|
|
if (methods->write(tdb, offset, tdb->transaction->blocks[i],
|
|
if (methods->write(tdb, offset, tdb->transaction->blocks[i],
|
|
|
length) == -1) {
|
|
length) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_commit:"
|
|
|
|
|
- " write failed during commit\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_commit:"
|
|
|
|
|
+ " write failed during commit");
|
|
|
|
|
|
|
|
/* we've overwritten part of the data and
|
|
/* we've overwritten part of the data and
|
|
|
possibly expanded the file, so we need to
|
|
possibly expanded the file, so we need to
|
|
@@ -1042,9 +1071,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
|
|
|
/* find the recovery area */
|
|
/* find the recovery area */
|
|
|
recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery));
|
|
recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery));
|
|
|
if (recovery_head == TDB_OFF_ERR) {
|
|
if (recovery_head == TDB_OFF_ERR) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_transaction_recover:"
|
|
"tdb_transaction_recover:"
|
|
|
- " failed to read recovery head\n");
|
|
|
|
|
|
|
+ " failed to read recovery head");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1055,9 +1084,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
|
|
|
|
|
|
|
|
/* read the recovery record */
|
|
/* read the recovery record */
|
|
|
if (tdb_read_convert(tdb, recovery_head, &rec, sizeof(rec)) == -1) {
|
|
if (tdb_read_convert(tdb, recovery_head, &rec, sizeof(rec)) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_recover:"
|
|
|
|
|
- " failed to read recovery record\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_recover:"
|
|
|
|
|
+ " failed to read recovery record");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1067,10 +1096,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (tdb->read_only) {
|
|
if (tdb->read_only) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_recover:"
|
|
|
|
|
- " attempt to recover read only database\n");
|
|
|
|
|
- tdb->ecode = TDB_ERR_CORRUPT;
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_recover:"
|
|
|
|
|
+ " attempt to recover read only database");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1078,19 +1106,18 @@ int tdb_transaction_recover(struct tdb_context *tdb)
|
|
|
|
|
|
|
|
data = (unsigned char *)malloc(rec.len);
|
|
data = (unsigned char *)malloc(rec.len);
|
|
|
if (data == NULL) {
|
|
if (data == NULL) {
|
|
|
- tdb->ecode = TDB_ERR_OOM;
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_recover:"
|
|
|
|
|
- " failed to allocate recovery data\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_recover:"
|
|
|
|
|
+ " failed to allocate recovery data");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* read the full recovery data */
|
|
/* read the full recovery data */
|
|
|
if (tdb->methods->read(tdb, recovery_head + sizeof(rec), data,
|
|
if (tdb->methods->read(tdb, recovery_head + sizeof(rec), data,
|
|
|
rec.len) == -1) {
|
|
rec.len) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_recover:"
|
|
|
|
|
- " failed to read recovery data\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_recover:"
|
|
|
|
|
+ " failed to read recovery data");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1106,9 +1133,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
|
|
|
|
|
|
|
|
if (tdb->methods->write(tdb, ofs, p, len) == -1) {
|
|
if (tdb->methods->write(tdb, ofs, p, len) == -1) {
|
|
|
free(data);
|
|
free(data);
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_transaction_recover:"
|
|
"tdb_transaction_recover:"
|
|
|
- " failed to recover %zu bytes at offset %zu\n",
|
|
|
|
|
|
|
+ " failed to recover %zu bytes at offset %zu",
|
|
|
(size_t)len, (size_t)ofs);
|
|
(size_t)len, (size_t)ofs);
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
@@ -1118,8 +1145,8 @@ int tdb_transaction_recover(struct tdb_context *tdb)
|
|
|
free(data);
|
|
free(data);
|
|
|
|
|
|
|
|
if (transaction_sync(tdb, 0, tdb->map_size) == -1) {
|
|
if (transaction_sync(tdb, 0, tdb->map_size) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_recover: failed to sync recovery\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_recover: failed to sync recovery");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1127,9 +1154,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
|
|
|
if (recovery_eof <= recovery_head) {
|
|
if (recovery_eof <= recovery_head) {
|
|
|
if (tdb_write_off(tdb, offsetof(struct tdb_header,recovery), 0)
|
|
if (tdb_write_off(tdb, offsetof(struct tdb_header,recovery), 0)
|
|
|
== -1) {
|
|
== -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_transaction_recover:"
|
|
"tdb_transaction_recover:"
|
|
|
- " failed to remove recovery head\n");
|
|
|
|
|
|
|
+ " failed to remove recovery head");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -1139,21 +1166,21 @@ int tdb_transaction_recover(struct tdb_context *tdb)
|
|
|
recovery_head
|
|
recovery_head
|
|
|
+ offsetof(struct tdb_recovery_record, magic),
|
|
+ offsetof(struct tdb_recovery_record, magic),
|
|
|
TDB_RECOVERY_INVALID_MAGIC) == -1) {
|
|
TDB_RECOVERY_INVALID_MAGIC) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
"tdb_transaction_recover:"
|
|
"tdb_transaction_recover:"
|
|
|
- " failed to remove recovery magic\n");
|
|
|
|
|
|
|
+ " failed to remove recovery magic");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (transaction_sync(tdb, 0, recovery_eof) == -1) {
|
|
if (transaction_sync(tdb, 0, recovery_eof) == -1) {
|
|
|
- tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_recover: failed to sync2 recovery\n");
|
|
|
|
|
|
|
+ tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
|
|
|
|
|
+ "tdb_transaction_recover: failed to sync2 recovery");
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
|
|
|
|
|
- "tdb_transaction_recover: recovered %zu byte database\n",
|
|
|
|
|
- (size_t)recovery_eof);
|
|
|
|
|
|
|
+ tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
|
|
|
|
|
+ "tdb_transaction_recover: recovered %zu byte database",
|
|
|
|
|
+ (size_t)recovery_eof);
|
|
|
|
|
|
|
|
/* all done */
|
|
/* all done */
|
|
|
return 0;
|
|
return 0;
|