r22041: merge trivial changes from samba3
[ira/wip.git] / source / lib / tdb / common / transaction.c
index cf0f378dcedb36fc76e87c6b3ef5432c2ace76f0..eb296206f9f4be905a6f33bd54172c80b6a2d5af 100644 (file)
@@ -39,7 +39,7 @@
     by the header. This removes the need for extra journal files as
     used by some other databases
 
-  - dymacially allocated the transaction recover record, re-using it
+  - dynamically allocated the transaction recover record, re-using it
     for subsequent transactions. If a larger record is needed then
     tdb_free() the old record to place it on the normal tdb freelist
     before allocating the new record
 
 */
 
+struct tdb_transaction_el {
+       struct tdb_transaction_el *next, *prev;
+       tdb_off_t offset;
+       tdb_len_t length;
+       unsigned char *data;
+};
 
 /*
   hold the context of any current transaction
@@ -105,12 +111,7 @@ struct tdb_transaction {
           ordered, with first element at the front of the list. It
           needs to be doubly linked as the read/write traversals need
           to be backwards, while the commit needs to be forwards */
-       struct tdb_transaction_el {
-               struct tdb_transaction_el *next, *prev;
-               tdb_off_t offset;
-               tdb_len_t length;
-               unsigned char *data;
-       } *elements, *elements_last;
+       struct tdb_transaction_el *elements, *elements_last;
 
        /* non-zero when an internal transaction error has
           occurred. All write operations will then fail until the
@@ -182,7 +183,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 +196,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 +214,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,8 +253,31 @@ 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 = (unsigned char *)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));
+       el = (struct tdb_transaction_el *)malloc(sizeof(*el));
        if (el == NULL) {
                tdb->ecode = TDB_ERR_OOM;
                tdb->transaction->transaction_error = 1;                
@@ -259,7 +287,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;
@@ -280,7 +308,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 +359,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 +382,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 +390,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,12 +408,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;
@@ -394,8 +423,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,22 +432,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;
        }
@@ -433,11 +463,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 +489,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,14 +509,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->locked[h].count = 0;
-                       }
+               int i;
+               for (i=0;i<tdb->num_lockrecs;i++) {
+                       tdb_brlock(tdb,FREELIST_TOP+4*tdb->lockrecs[i].list,
+                                  F_UNLCK,F_SETLKW, 0, 1);
                }
                tdb->num_locks = 0;
        }
@@ -485,8 +528,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 +543,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 +552,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 +595,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 +603,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 +623,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 +639,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 +655,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;
        }
 
@@ -642,7 +686,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;
@@ -665,7 +709,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 +737,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 +759,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 +782,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 +808,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_len(tdb, FREELIST_TOP, F_WRLCK, F_SETLKW, 0, 0) == -1) {
-               TDB_LOG((tdb, 0, "tdb_transaction_start: failed to upgrade hash locks\n"));
+       if (tdb_brlock_upgrade(tdb, FREELIST_TOP, 0) == -1) {
+               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 +825,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 +835,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 +848,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 +862,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 +871,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 +889,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 +899,22 @@ 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
+          not be backed up (as tdb rounding to block sizes means that
+          file size changes are quite rare too). The following forces
+          mtime changes when a transaction completes */
+#ifdef HAVE_UTIME
+       utime(tdb->name, NULL);
+#endif
 
        /* use a transaction cancel to free memory and remove the
           transaction locks */
@@ -878,7 +937,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;
        }
@@ -891,7 +950,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;
        }
@@ -902,16 +961,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;
        }
@@ -919,7 +978,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;
        }
@@ -936,7 +995,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;
                }
@@ -946,7 +1005,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;
        }
@@ -954,7 +1013,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;                      
                }
@@ -963,7 +1022,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;                      
        }
@@ -971,7 +1030,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;                      
        }
@@ -979,12 +1038,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 */