tdb2: update documentation
[ccan] / ccan / tdb2 / transaction.c
index 53bcc21c62f45ae687f6a9c6876e015f9e297cf1..6e0b1669fcd9fa1e2276282e621856f1f8236c3c 100644 (file)
@@ -169,10 +169,9 @@ static int transaction_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
        return 0;
 
 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;
        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 */
        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,"
-                        " write not allowed\n");
-               tdb->transaction->transaction_error = 1;
-               return -1;
+                        " write not allowed");
+               goto fail;
        }
 
        /* 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 *));
                }
                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;
                }
                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) {
                tdb->transaction->blocks[blk] = (uint8_t *)calloc(getpagesize(), 1);
                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()) {
                        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(),
                                                               tdb->transaction->blocks[blk],
                                                               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]);
                                goto fail;
                        }
@@ -278,10 +280,6 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
        return 0;
 
 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;
        return -1;
 }
@@ -341,6 +339,12 @@ static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
                return 0;
        }
        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;
 }
 
@@ -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,
-                               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 = {
@@ -383,9 +416,9 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t
        }
 
        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;
        }
 #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);
                if (msync(moffset + (char *)tdb->map_ptr,
                          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;
                }
        }
@@ -410,9 +442,8 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
        int i;
 
        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;
        }
 
@@ -441,9 +472,9 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
                                   &invalid, sizeof(invalid)) == -1 ||
                    transaction_sync(tdb, tdb->transaction->magic_offset,
                                     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");
                }
        }
 
@@ -454,7 +485,6 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
        tdb->methods = tdb->transaction->io_methods;
 
        tdb_transaction_unlock(tdb, F_WRLCK);
-       tdb_unlock_expand(tdb, F_WRLCK);
 
        if (tdb_has_open_lock(tdb))
                tdb_unlock_open(tdb);
@@ -470,16 +500,17 @@ int tdb_transaction_start(struct tdb_context *tdb)
 {
        /* some sanity checks */
        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;
        }
 
        /* cope with nested tdb_transaction_start() calls */
        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;
        }
 
@@ -487,17 +518,17 @@ int tdb_transaction_start(struct tdb_context *tdb)
                /* the caller must not have any locks when starting a
                   transaction as otherwise we'll be screwed by lack
                   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;
        }
 
        tdb->transaction = (struct tdb_transaction *)
                calloc(sizeof(struct tdb_transaction), 1);
        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;
        }
 
@@ -516,10 +547,6 @@ int tdb_transaction_start(struct tdb_context *tdb)
                goto fail_allrecord_lock;
        }
 
-       if (tdb_lock_expand(tdb, F_WRLCK) != 0) {
-               goto fail_expand_lock;
-       }
-
        /* make sure we know about any file expansions already done by
           anyone else */
        tdb->methods->oob(tdb, tdb->map_size + 1, true);
@@ -531,8 +558,6 @@ int tdb_transaction_start(struct tdb_context *tdb)
        tdb->methods = &transaction_methods;
        return 0;
 
-fail_expand_lock:
-       tdb_allrecord_unlock(tdb, F_RDLCK);
 fail_allrecord_lock:
        tdb_transaction_unlock(tdb, F_WRLCK);
        SAFE_FREE(tdb->transaction->blocks);
@@ -592,17 +617,17 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
 
        recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery));
        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:"
-                        " failed to read recovery head\n");
+                        " failed to read recovery head");
                return -1;
        }
 
        if (recovery_head != 0) {
                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:"
-                                " failed to read recovery record\n");
+                                " failed to read recovery record");
                        return -1;
                }
                tdb_convert(tdb, &rec, sizeof(rec));
@@ -628,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
           the transaction) */
        if (recovery_head != 0) {
+               add_stat(tdb, frees, 1);
                if (add_free_record(tdb, recovery_head,
                                    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;
                }
        }
@@ -656,9 +682,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
                sizeof(rec) + *recovery_max_size;
        tdb->map_size = tdb->transaction->old_map_size;
        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:"
-                        " failed to create recovery area\n");
+                        " failed to create recovery area");
                return -1;
        }
 
@@ -672,9 +698,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
        tdb_convert(tdb, &recovery_head, sizeof(recovery_head));
        if (methods->write(tdb, offsetof(struct tdb_header, recovery),
                           &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:"
-                        " failed to write recovery head\n");
+                        " failed to write recovery head");
                return -1;
        }
        transaction_write_existing(tdb, offsetof(struct tdb_header, recovery),
@@ -720,7 +746,8 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
 
        data = (unsigned char *)malloc(recovery_size + sizeof(*rec));
        if (data == NULL) {
-               tdb->ecode = TDB_ERR_OOM;
+               tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
+                          "transaction_setup_recovery: cannot allocate");
                return -1;
        }
 
@@ -750,10 +777,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
                        continue;
                }
                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);
                        return -1;
                }
@@ -781,9 +807,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
        /* write the recovery data to the recovery area */
        if (methods->write(tdb, recovery_offset, data,
                           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:"
-                        " failed to write recovery data\n");
+                        " failed to write recovery data");
                free(data);
                return -1;
        }
@@ -808,9 +834,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
                                                   magic);
 
        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:"
-                        " failed to write recovery magic\n");
+                        " failed to write recovery magic");
                return -1;
        }
        transaction_write_existing(tdb, *magic_offset, &magic, sizeof(magic));
@@ -828,27 +854,24 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
        const struct tdb_methods *methods;
 
        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;
        }
 
        if (tdb->transaction->prepared) {
-               tdb->ecode = TDB_ERR_EINVAL;
                _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;
        }
 
        if (tdb->transaction->transaction_error) {
-               tdb->ecode = TDB_ERR_IO;
                _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;
        }
 
@@ -867,9 +890,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
 
        /* upgrade the main transaction lock region to a write lock */
        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:"
-                        " failed to upgrade hash locks\n");
+                        " failed to upgrade hash locks");
                _tdb_transaction_cancel(tdb);
                return -1;
        }
@@ -877,19 +900,20 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
        /* get the open lock - this prevents new users attaching to the database
           during the commit */
        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:"
-                        " failed to get open lock\n");
+                        " failed to get open lock");
                _tdb_transaction_cancel(tdb);
                return -1;
        }
 
+       /* Since we have whole db locked, we don't need the expansion lock. */
        if (!(tdb->flags & TDB_NOSYNC)) {
                /* write the recovery data to the end of the file */
                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:"
-                                " failed to setup recovery data\n");
+                                " failed to setup recovery data");
                        _tdb_transaction_cancel(tdb);
                        return -1;
                }
@@ -903,9 +927,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
                /* Restore original map size for tdb_expand_file */
                tdb->map_size = tdb->transaction->old_map_size;
                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:"
-                                " expansion failed\n");
+                                " expansion failed");
                        _tdb_transaction_cancel(tdb);
                        return -1;
                }
@@ -933,19 +957,18 @@ int tdb_transaction_commit(struct tdb_context *tdb)
        int i;
 
        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;
        }
 
        tdb_trace(tdb, "tdb_transaction_commit");
 
        if (tdb->transaction->transaction_error) {
-               tdb->ecode = TDB_ERR_IO;
                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;
        }
 
@@ -986,9 +1009,9 @@ int tdb_transaction_commit(struct tdb_context *tdb)
 
                if (methods->write(tdb, offset, tdb->transaction->blocks[i],
                                   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
                           possibly expanded the file, so we need to
@@ -1048,9 +1071,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        /* find the recovery area */
        recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery));
        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:"
-                        " failed to read recovery head\n");
+                        " failed to read recovery head");
                return -1;
        }
 
@@ -1061,9 +1084,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
 
        /* read the recovery record */
        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;
        }
 
@@ -1073,10 +1096,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        }
 
        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;
        }
 
@@ -1084,19 +1106,18 @@ int tdb_transaction_recover(struct tdb_context *tdb)
 
        data = (unsigned char *)malloc(rec.len);
        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;
        }
 
        /* read the full recovery data */
        if (tdb->methods->read(tdb, recovery_head + sizeof(rec), data,
                               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;
        }
 
@@ -1112,9 +1133,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
 
                if (tdb->methods->write(tdb, ofs, p, len) == -1) {
                        free(data);
-                       tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
+                       tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
                                 "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);
                        return -1;
                }
@@ -1124,8 +1145,8 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        free(data);
 
        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;
        }
 
@@ -1133,9 +1154,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        if (recovery_eof <= recovery_head) {
                if (tdb_write_off(tdb, offsetof(struct tdb_header,recovery), 0)
                    == -1) {
-                       tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
+                       tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
                                 "tdb_transaction_recover:"
-                                " failed to remove recovery head\n");
+                                " failed to remove recovery head");
                        return -1;
                }
        }
@@ -1145,21 +1166,21 @@ int tdb_transaction_recover(struct tdb_context *tdb)
                          recovery_head
                          + offsetof(struct tdb_recovery_record, magic),
                          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:"
-                        " failed to remove recovery magic\n");
+                        " failed to remove recovery magic");
                return -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;
        }
 
-       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 */
        return 0;