r19423: merge some tdb changes from SAMBA_3_0 to SAMBA_4_0
[ira/wip.git] / source / lib / tdb / common / transaction.c
index 6960a02ad833b3713a1a0b24bab8dc08ee89c882..7dff9a95e345ec9df1ce9c07adb537a9ac18cfaa 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;
@@ -258,7 +258,8 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
             off > tdb->transaction->old_map_size)) {
                unsigned char *data = best_el->data;
                el = best_el;
-               el->data = realloc(el->data, el->length + len);
+               el->data = (unsigned char *)realloc(el->data,
+                                                   el->length + len);
                if (el->data == NULL) {
                        tdb->ecode = TDB_ERR_OOM;
                        tdb->transaction->transaction_error = 1;
@@ -275,7 +276,7 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
        }
 
        /* add a new entry at the end of the list */
-       el = malloc(sizeof(*el));
+       el = (struct tdb_transaction_el *)malloc(sizeof(*el));
        if (el == NULL) {
                tdb->ecode = TDB_ERR_OOM;
                tdb->transaction->transaction_error = 1;                
@@ -285,7 +286,7 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
        el->prev = tdb->transaction->elements_last;
        el->offset = off;
        el->length = len;
-       el->data = malloc(len);
+       el->data = (unsigned char *)malloc(len);
        if (el->data == NULL) {
                free(el);
                tdb->ecode = TDB_ERR_OOM;
@@ -306,7 +307,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;
@@ -357,7 +358,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;
 }
@@ -380,7 +381,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;
        }
@@ -388,16 +389,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;
        }
@@ -406,12 +407,13 @@ 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;
        }
 
-       tdb->transaction = calloc(sizeof(struct tdb_transaction), 1);
+       tdb->transaction = (struct tdb_transaction *)
+               calloc(sizeof(struct tdb_transaction), 1);
        if (tdb->transaction == NULL) {
                tdb->ecode = TDB_ERR_OOM;
                return -1;
@@ -420,8 +422,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;
@@ -429,22 +431,23 @@ 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;
        }
 
        /* setup a copy of the hash table heads so the hash scan in
           traverse can be fast */
-       tdb->transaction->hash_heads = calloc(tdb->header.hash_size+1, sizeof(tdb_off_t));
+       tdb->transaction->hash_heads = (u32 *)
+               calloc(tdb->header.hash_size+1, sizeof(u32));
        if (tdb->transaction->hash_heads == NULL) {
                tdb->ecode = TDB_ERR_OOM;
                goto fail;
        }
        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;
        }
@@ -463,7 +466,7 @@ int tdb_transaction_start(struct tdb_context *tdb)
           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, 0, "tdb_transaction_start: failed to prime hash table\n"));
+               TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_start: failed to prime hash table\n"));
                tdb->ecode = TDB_ERR_IO;
                goto fail;
        }
@@ -471,8 +474,8 @@ int tdb_transaction_start(struct tdb_context *tdb)
        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;
@@ -485,7 +488,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;
        }
 
@@ -505,12 +508,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;
                        }
                }
@@ -520,8 +529,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);
        
@@ -535,7 +544,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
@@ -544,7 +553,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;
                }
        }
@@ -586,7 +596,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;
        }
 
@@ -594,7 +604,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;
        }
 
@@ -614,7 +624,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;
                }
        }
@@ -630,7 +640,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;
        }
 
@@ -646,7 +656,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;
        }
 
@@ -677,7 +687,7 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
                return -1;
        }
 
-       data = malloc(recovery_size + sizeof(*rec));
+       data = (unsigned char *)malloc(recovery_size + sizeof(*rec));
        if (data == NULL) {
                tdb->ecode = TDB_ERR_OOM;
                return -1;
@@ -700,7 +710,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;
@@ -728,7 +738,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;
@@ -750,7 +760,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;
        }
@@ -773,14 +783,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;
        }
 
@@ -799,16 +809,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;
@@ -816,8 +826,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;
@@ -826,8 +836,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;
                }
@@ -839,8 +849,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;
                }
@@ -853,7 +863,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
@@ -862,9 +872,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;
@@ -880,7 +890,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;
                }
 
@@ -890,7 +900,7 @@ 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
@@ -928,7 +938,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;
        }
@@ -941,7 +951,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;
        }
@@ -952,16 +962,16 @@ 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;
        }
 
        recovery_eof = rec.key_len;
 
-       data = malloc(rec.data_len);
+       data = (unsigned char *)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;
        }
@@ -969,7 +979,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;
        }
@@ -986,7 +996,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;
                }
@@ -996,7 +1006,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;
        }
@@ -1004,7 +1014,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;                      
                }
@@ -1013,7 +1023,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;                      
        }
@@ -1021,7 +1031,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;                      
        }
@@ -1029,12 +1039,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 */