r19401: make tdb_lockall() much more efficient, and add a tdb_lockall_read()
[ira/wip.git] / source / lib / tdb / common / transaction.c
index becc6cd0591afda4227316cd45eddba376ad0196..526aef7d0389e4ca9ee74e0633d5df539f0e2d35 100644 (file)
@@ -182,7 +182,7 @@ static int transaction_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
        return tdb->transaction->io_methods->tdb_read(tdb, off, buf, len, cv);
 
 fail:
-       TDB_LOG((tdb, 0, "transaction_read: failed at off=%d len=%d\n", off, len));
+       TDB_LOG((tdb, TDB_DEBUG_FATAL, "transaction_read: failed at off=%d len=%d\n", off, len));
        tdb->ecode = TDB_ERR_IO;
        tdb->transaction->transaction_error = 1;
        return -1;
@@ -195,7 +195,7 @@ fail:
 static int transaction_write(struct tdb_context *tdb, tdb_off_t off, 
                             const void *buf, tdb_len_t len)
 {
-       struct tdb_transaction_el *el;
+       struct tdb_transaction_el *el, *best_el=NULL;
 
        if (len == 0) {
                return 0;
@@ -213,6 +213,10 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
        for (el=tdb->transaction->elements_last;el;el=el->prev) {
                tdb_len_t partial;
 
+               if (best_el == NULL && off == el->offset+el->length) {
+                       best_el = el;
+               }
+
                if (off+len <= el->offset) {
                        continue;
                }
@@ -248,6 +252,28 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
                return 0;
        }
 
+       /* see if we can append the new entry to an existing entry */
+       if (best_el && best_el->offset + best_el->length == off && 
+           (off+len < tdb->transaction->old_map_size ||
+            off > tdb->transaction->old_map_size)) {
+               unsigned char *data = best_el->data;
+               el = best_el;
+               el->data = realloc(el->data, el->length + len);
+               if (el->data == NULL) {
+                       tdb->ecode = TDB_ERR_OOM;
+                       tdb->transaction->transaction_error = 1;
+                       el->data = data;
+                       return -1;
+               }
+               if (buf) {
+                       memcpy(el->data + el->length, buf, len);
+               } else {
+                       memset(el->data + el->length, TDB_PAD_BYTE, len);
+               }
+               el->length += len;
+               return 0;
+       }
+
        /* add a new entry at the end of the list */
        el = malloc(sizeof(*el));
        if (el == NULL) {
@@ -280,7 +306,7 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
        return 0;
 
 fail:
-       TDB_LOG((tdb, 0, "transaction_write: failed at off=%d len=%d\n", off, len));
+       TDB_LOG((tdb, TDB_DEBUG_FATAL, "transaction_write: failed at off=%d len=%d\n", off, len));
        tdb->ecode = TDB_ERR_IO;
        tdb->transaction->transaction_error = 1;
        return -1;
@@ -331,7 +357,7 @@ static int transaction_expand_file(struct tdb_context *tdb, tdb_off_t size,
   brlock during a transaction - ignore them
 */
 int transaction_brlock(struct tdb_context *tdb, tdb_off_t offset, 
-                      int rw_type, int lck_type, int probe)
+                      int rw_type, int lck_type, int probe, size_t len)
 {
        return 0;
 }
@@ -354,7 +380,7 @@ int tdb_transaction_start(struct tdb_context *tdb)
 {
        /* some sanity checks */
        if (tdb->read_only || (tdb->flags & TDB_INTERNAL) || tdb->traverse_read) {
-               TDB_LOG((tdb, 0, "tdb_transaction_start: cannot start a transaction on a read-only or internal db\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction on a read-only or internal db\n"));
                tdb->ecode = TDB_ERR_EINVAL;
                return -1;
        }
@@ -362,16 +388,16 @@ int tdb_transaction_start(struct tdb_context *tdb)
        /* cope with nested tdb_transaction_start() calls */
        if (tdb->transaction != NULL) {
                tdb->transaction->nesting++;
-               TDB_LOG((tdb, 0, "tdb_transaction_start: nesting %d\n", 
+               TDB_LOG((tdb, TDB_DEBUG_TRACE, "tdb_transaction_start: nesting %d\n", 
                         tdb->transaction->nesting));
                return 0;
        }
 
-       if (tdb->num_locks != 0) {
+       if (tdb->num_locks != 0 || tdb->global_lock.count) {
                /* 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_LOG((tdb, 0, "tdb_transaction_start: cannot start a transaction with locks held\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction with locks held\n"));
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
@@ -380,7 +406,7 @@ int tdb_transaction_start(struct tdb_context *tdb)
                /* you cannot use transactions inside a traverse (although you can use
                   traverse inside a transaction) as otherwise you can end up with
                   deadlock */
-               TDB_LOG((tdb, 0, "tdb_transaction_start: cannot start a transaction within a traverse\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction within a traverse\n"));
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
@@ -394,8 +420,8 @@ int tdb_transaction_start(struct tdb_context *tdb)
        /* get the transaction write lock. This is a blocking lock. As
           discussed with Volker, there are a number of ways we could
           make this async, which we will probably do in the future */
-       if (tdb_brlock_len(tdb, TRANSACTION_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_start: failed to get transaction lock\n"));
+       if (tdb_brlock(tdb, TRANSACTION_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: failed to get transaction lock\n"));
                tdb->ecode = TDB_ERR_LOCK;
                SAFE_FREE(tdb->transaction);
                return -1;
@@ -403,8 +429,8 @@ int tdb_transaction_start(struct tdb_context *tdb)
        
        /* get a read lock from the freelist to the end of file. This
           is upgraded to a write lock during the commit */
-       if (tdb_brlock_len(tdb, FREELIST_TOP, F_RDLCK, F_SETLKW, 0, 0) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_start: failed to get hash locks\n"));
+       if (tdb_brlock(tdb, FREELIST_TOP, F_RDLCK, F_SETLKW, 0, 0) == -1) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: failed to get hash locks\n"));
                tdb->ecode = TDB_ERR_LOCK;
                goto fail;
        }
@@ -418,7 +444,7 @@ int tdb_transaction_start(struct tdb_context *tdb)
        }
        if (tdb->methods->tdb_read(tdb, FREELIST_TOP, tdb->transaction->hash_heads,
                                   TDB_HASHTABLE_SIZE(tdb), 0) != 0) {
-               TDB_LOG((tdb, 0, "tdb_transaction_start: failed to read hash heads\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_start: failed to read hash heads\n"));
                tdb->ecode = TDB_ERR_IO;
                goto fail;
        }
@@ -433,11 +459,20 @@ int tdb_transaction_start(struct tdb_context *tdb)
        tdb->transaction->io_methods = tdb->methods;
        tdb->methods = &transaction_methods;
 
+       /* by calling this transaction write here, we ensure that we don't grow the
+          transaction linked list due to hash table updates */
+       if (transaction_write(tdb, FREELIST_TOP, tdb->transaction->hash_heads, 
+                             TDB_HASHTABLE_SIZE(tdb)) != 0) {
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_start: failed to prime hash table\n"));
+               tdb->ecode = TDB_ERR_IO;
+               goto fail;
+       }
+
        return 0;
        
 fail:
-       tdb_brlock_len(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 0, 0);
-       tdb_brlock_len(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+       tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 0, 0);
+       tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
        SAFE_FREE(tdb->transaction->hash_heads);
        SAFE_FREE(tdb->transaction);
        return -1;
@@ -450,7 +485,7 @@ fail:
 int tdb_transaction_cancel(struct tdb_context *tdb)
 {      
        if (tdb->transaction == NULL) {
-               TDB_LOG((tdb, 0, "tdb_transaction_cancel: no transaction\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_cancel: no transaction\n"));
                return -1;
        }
 
@@ -470,12 +505,18 @@ int tdb_transaction_cancel(struct tdb_context *tdb)
                free(el);
        }
 
+       /* remove any global lock created during the transaction */
+       if (tdb->global_lock.count != 0) {
+               tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 0, 4*tdb->header.hash_size);
+               tdb->global_lock.count = 0;
+       }
+
        /* remove any locks created during the transaction */
        if (tdb->num_locks != 0) {
                int h;
                for (h=0;h<tdb->header.hash_size+1;h++) {
                        if (tdb->locked[h].count != 0) {
-                               tdb_brlock_len(tdb,FREELIST_TOP+4*h,F_UNLCK,F_SETLKW, 0, 1);
+                               tdb_brlock(tdb,FREELIST_TOP+4*h,F_UNLCK,F_SETLKW, 0, 1);
                                tdb->locked[h].count = 0;
                        }
                }
@@ -485,8 +526,8 @@ int tdb_transaction_cancel(struct tdb_context *tdb)
        /* restore the normal io methods */
        tdb->methods = tdb->transaction->io_methods;
 
-       tdb_brlock_len(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 0, 0);
-       tdb_brlock_len(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+       tdb_brlock(tdb, FREELIST_TOP, F_UNLCK, F_SETLKW, 0, 0);
+       tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
        SAFE_FREE(tdb->transaction->hash_heads);
        SAFE_FREE(tdb->transaction);
        
@@ -500,7 +541,7 @@ 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, 0, "tdb_transaction: fsync failed\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction: fsync failed\n"));
                return -1;
        }
 #ifdef MS_SYNC
@@ -509,7 +550,8 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t
                if (msync(moffset + (char *)tdb->map_ptr, 
                          length + (offset - moffset), MS_SYNC) != 0) {
                        tdb->ecode = TDB_ERR_IO;
-                       TDB_LOG((tdb, 0, "tdb_transaction: msync failed\n"));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction: msync failed - %s\n",
+                                strerror(errno)));
                        return -1;
                }
        }
@@ -551,7 +593,7 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
        tdb_off_t recovery_head;
 
        if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
-               TDB_LOG((tdb, 0, "tdb_recovery_allocate: failed to read recovery head\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery head\n"));
                return -1;
        }
 
@@ -559,7 +601,7 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
 
        if (recovery_head != 0 && 
            methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
-               TDB_LOG((tdb, 0, "tdb_recovery_allocate: failed to read recovery record\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery record\n"));
                return -1;
        }
 
@@ -579,7 +621,7 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
           the transaction) */
        if (recovery_head != 0) {
                if (tdb_free(tdb, recovery_head, &rec) == -1) {
-                       TDB_LOG((tdb, 0, "tdb_recovery_allocate: failed to free previous recovery area\n"));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to free previous recovery area\n"));
                        return -1;
                }
        }
@@ -595,7 +637,7 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
        if (methods->tdb_expand_file(tdb, tdb->transaction->old_map_size, 
                                     (tdb->map_size - tdb->transaction->old_map_size) +
                                     sizeof(rec) + *recovery_max_size) == -1) {
-               TDB_LOG((tdb, 0, "tdb_recovery_allocate: failed to create recovery area\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to create recovery area\n"));
                return -1;
        }
 
@@ -611,7 +653,7 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
        CONVERT(recovery_head);
        if (methods->tdb_write(tdb, TDB_RECOVERY_HEAD, 
                               &recovery_head, sizeof(tdb_off_t)) == -1) {
-               TDB_LOG((tdb, 0, "tdb_recovery_allocate: failed to write recovery head\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to write recovery head\n"));
                return -1;
        }
 
@@ -665,7 +707,7 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
                        continue;
                }
                if (el->offset + el->length > tdb->transaction->old_map_size) {
-                       TDB_LOG((tdb, 0, "tdb_transaction_commit: transaction data over new region boundary\n"));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: transaction data over new region boundary\n"));
                        free(data);
                        tdb->ecode = TDB_ERR_CORRUPT;
                        return -1;
@@ -693,7 +735,7 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
 
        /* write the recovery data to the recovery area */
        if (methods->tdb_write(tdb, recovery_offset, data, sizeof(*rec) + recovery_size) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_commit: failed to write recovery data\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write recovery data\n"));
                free(data);
                tdb->ecode = TDB_ERR_IO;
                return -1;
@@ -715,7 +757,7 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
        *magic_offset = recovery_offset + offsetof(struct list_struct, magic);
 
        if (methods->tdb_write(tdb, *magic_offset, &magic, sizeof(magic)) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_commit: failed to write recovery magic\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write recovery magic\n"));
                tdb->ecode = TDB_ERR_IO;
                return -1;
        }
@@ -738,14 +780,14 @@ int tdb_transaction_commit(struct tdb_context *tdb)
        u32 zero = 0;
 
        if (tdb->transaction == NULL) {
-               TDB_LOG((tdb, 0, "tdb_transaction_commit: no transaction\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: no transaction\n"));
                return -1;
        }
 
        if (tdb->transaction->transaction_error) {
                tdb->ecode = TDB_ERR_IO;
                tdb_transaction_cancel(tdb);
-               TDB_LOG((tdb, 0, "tdb_transaction_commit: transaction error pending\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: transaction error pending\n"));
                return -1;
        }
 
@@ -764,16 +806,16 @@ int tdb_transaction_commit(struct tdb_context *tdb)
        
        /* if there are any locks pending then the caller has not
           nested their locks properly, so fail the transaction */
-       if (tdb->num_locks) {
+       if (tdb->num_locks || tdb->global_lock.count) {
                tdb->ecode = TDB_ERR_LOCK;
-               TDB_LOG((tdb, 0, "tdb_transaction_commit: locks pending on commit\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: locks pending on commit\n"));
                tdb_transaction_cancel(tdb);
                return -1;
        }
 
        /* upgrade the main transaction lock region to a write lock */
        if (tdb_brlock_upgrade(tdb, FREELIST_TOP, 0) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_start: failed to upgrade hash locks\n"));
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: failed to upgrade hash locks\n"));
                tdb->ecode = TDB_ERR_LOCK;
                tdb_transaction_cancel(tdb);
                return -1;
@@ -781,8 +823,8 @@ int tdb_transaction_commit(struct tdb_context *tdb)
 
        /* get the global lock - this prevents new users attaching to the database
           during the commit */
-       if (tdb_brlock_len(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_commit: failed to get global lock\n"));
+       if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
+               TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: failed to get global lock\n"));
                tdb->ecode = TDB_ERR_LOCK;
                tdb_transaction_cancel(tdb);
                return -1;
@@ -791,8 +833,8 @@ int tdb_transaction_commit(struct tdb_context *tdb)
        if (!(tdb->flags & TDB_NOSYNC)) {
                /* write the recovery data to the end of the file */
                if (transaction_setup_recovery(tdb, &magic_offset) == -1) {
-                       TDB_LOG((tdb, 0, "tdb_transaction_commit: failed to setup recovery data\n"));
-                       tdb_brlock_len(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: failed to setup recovery data\n"));
+                       tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
                        tdb_transaction_cancel(tdb);
                        return -1;
                }
@@ -804,8 +846,8 @@ int tdb_transaction_commit(struct tdb_context *tdb)
                                             tdb->map_size - 
                                             tdb->transaction->old_map_size) == -1) {
                        tdb->ecode = TDB_ERR_IO;
-                       TDB_LOG((tdb, 0, "tdb_transaction_commit: expansion failed\n"));
-                       tdb_brlock_len(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: expansion failed\n"));
+                       tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
                        tdb_transaction_cancel(tdb);
                        return -1;
                }
@@ -818,7 +860,7 @@ int tdb_transaction_commit(struct tdb_context *tdb)
                struct tdb_transaction_el *el = tdb->transaction->elements;
 
                if (methods->tdb_write(tdb, el->offset, el->data, el->length) == -1) {
-                       TDB_LOG((tdb, 0, "tdb_transaction_commit: write failed during commit\n"));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: write failed during commit\n"));
                        
                        /* we've overwritten part of the data and
                           possibly expanded the file, so we need to
@@ -827,9 +869,9 @@ int tdb_transaction_commit(struct tdb_context *tdb)
                        tdb_transaction_recover(tdb); 
 
                        tdb_transaction_cancel(tdb);
-                       tdb_brlock_len(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+                       tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
 
-                       TDB_LOG((tdb, 0, "tdb_transaction_commit: write failed\n"));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: write failed\n"));
                        return -1;
                }
                tdb->transaction->elements = el->next;
@@ -845,7 +887,7 @@ int tdb_transaction_commit(struct tdb_context *tdb)
 
                /* remove the recovery marker */
                if (methods->tdb_write(tdb, magic_offset, &zero, 4) == -1) {
-                       TDB_LOG((tdb, 0, "tdb_transaction_commit: failed to remove recovery magic\n"));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: failed to remove recovery magic\n"));
                        return -1;
                }
 
@@ -855,7 +897,13 @@ int tdb_transaction_commit(struct tdb_context *tdb)
                }
        }
 
-       tdb_brlock_len(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+       tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1);
+
+       /*
+         TODO: maybe write to some dummy hdr field, or write to magic
+         offset without mmap, before the last sync, instead of the
+         utime() call
+       */
 
        /* on some systems (like Linux 2.6.x) changes via mmap/msync
           don't change the mtime of the file, this means the file may
@@ -887,7 +935,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
 
        /* find the recovery area */
        if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to read recovery head\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery head\n"));
                tdb->ecode = TDB_ERR_IO;
                return -1;
        }
@@ -900,7 +948,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        /* read the recovery record */
        if (tdb->methods->tdb_read(tdb, recovery_head, &rec, 
                                   sizeof(rec), DOCONV()) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to read recovery record\n"));         
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery record\n"));           
                tdb->ecode = TDB_ERR_IO;
                return -1;
        }
@@ -911,7 +959,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        }
 
        if (tdb->read_only) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: attempt to recover read only database\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: attempt to recover read only database\n"));
                tdb->ecode = TDB_ERR_CORRUPT;
                return -1;
        }
@@ -920,7 +968,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
 
        data = malloc(rec.data_len);
        if (data == NULL) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to allocate recovery data\n"));               
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to allocate recovery data\n"));         
                tdb->ecode = TDB_ERR_OOM;
                return -1;
        }
@@ -928,7 +976,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        /* read the full recovery data */
        if (tdb->methods->tdb_read(tdb, recovery_head + sizeof(rec), data,
                                   rec.data_len, 0) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to read recovery data\n"));           
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery data\n"));             
                tdb->ecode = TDB_ERR_IO;
                return -1;
        }
@@ -945,7 +993,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
 
                if (tdb->methods->tdb_write(tdb, ofs, p+8, len) == -1) {
                        free(data);
-                       TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to recover %d bytes at offset %d\n", len, ofs));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to recover %d bytes at offset %d\n", len, ofs));
                        tdb->ecode = TDB_ERR_IO;
                        return -1;
                }
@@ -955,7 +1003,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        free(data);
 
        if (transaction_sync(tdb, 0, tdb->map_size) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to sync recovery\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to sync recovery\n"));
                tdb->ecode = TDB_ERR_IO;
                return -1;
        }
@@ -963,7 +1011,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        /* if the recovery area is after the recovered eof then remove it */
        if (recovery_eof <= recovery_head) {
                if (tdb_ofs_write(tdb, TDB_RECOVERY_HEAD, &zero) == -1) {
-                       TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to remove recovery head\n"));
+                       TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to remove recovery head\n"));
                        tdb->ecode = TDB_ERR_IO;
                        return -1;                      
                }
@@ -972,7 +1020,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        /* remove the recovery magic */
        if (tdb_ofs_write(tdb, recovery_head + offsetof(struct list_struct, magic), 
                          &zero) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to remove recovery magic\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to remove recovery magic\n"));
                tdb->ecode = TDB_ERR_IO;
                return -1;                      
        }
@@ -980,7 +1028,7 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        /* reduce the file size to the old size */
        tdb_munmap(tdb);
        if (ftruncate(tdb->fd, recovery_eof) != 0) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to reduce to recovery size\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to reduce to recovery size\n"));
                tdb->ecode = TDB_ERR_IO;
                return -1;                      
        }
@@ -988,12 +1036,12 @@ int tdb_transaction_recover(struct tdb_context *tdb)
        tdb_mmap(tdb);
 
        if (transaction_sync(tdb, 0, recovery_eof) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_recover: failed to sync2 recovery\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to sync2 recovery\n"));
                tdb->ecode = TDB_ERR_IO;
                return -1;
        }
 
-       TDB_LOG((tdb, 0, "tdb_transaction_recover: recovered %d byte database\n", 
+       TDB_LOG((tdb, TDB_DEBUG_TRACE, "tdb_transaction_recover: recovered %d byte database\n", 
                 recovery_eof));
 
        /* all done */