Fix early transaction unlock when traverse done inside transaction.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 14 Jul 2009 12:34:36 +0000 (22:04 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 14 Jul 2009 12:34:36 +0000 (22:04 +0930)
Generalizes traverse in traverse fix from rusty@rustcorp.com.au-20090629073630-3eduhyypx2tp6u80

ccan/tdb/lock.c
ccan/tdb/tdb_private.h
ccan/tdb/test/run-traverse-in-transaction.c [new file with mode: 0644]
ccan/tdb/traverse.c

index 473a9bf52c3b0b855d892cf1c1b611dede6c3547..6809ce37d87513986cc54fab82251004379557cb 100644 (file)
@@ -302,6 +302,7 @@ int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
 int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
 {
        if (tdb->have_transaction_lock || tdb->global_lock.count) {
+               tdb->have_transaction_lock++;
                return 0;
        }
        if (tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, ltype, 
@@ -310,7 +311,7 @@ int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
                tdb->ecode = TDB_ERR_LOCK;
                return -1;
        }
-       tdb->have_transaction_lock = 1;
+       tdb->have_transaction_lock++;
        return 0;
 }
 
@@ -319,15 +320,10 @@ int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
  */
 int tdb_transaction_unlock(struct tdb_context *tdb)
 {
-       int ret;
-       if (!tdb->have_transaction_lock) {
+       if (--tdb->have_transaction_lock) {
                return 0;
        }
-       ret = tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
-       if (ret == 0) {
-               tdb->have_transaction_lock = 0;
-       }
-       return ret;
+       return tdb->methods->tdb_brlock(tdb, TRANSACTION_LOCK, F_UNLCK, F_SETLKW, 0, 1);
 }
 
 
index 0ee5eb650c6fbbaca8f2807605d861490fe8ae7e..4836472864006d3cab1f5726553399d7bf4054f3 100644 (file)
@@ -215,7 +215,7 @@ struct tdb_context {
        struct tdb_transaction *transaction;
        int page_size;
        int max_dead_records;
-       bool have_transaction_lock;
+       int have_transaction_lock;
        int tracefd;
        volatile sig_atomic_t *interrupt_sig_ptr;
 };
diff --git a/ccan/tdb/test/run-traverse-in-transaction.c b/ccan/tdb/test/run-traverse-in-transaction.c
new file mode 100644 (file)
index 0000000..b184474
--- /dev/null
@@ -0,0 +1,70 @@
+#define _XOPEN_SOURCE 500
+#include "tdb/tdb.h"
+#include "tdb/io.c"
+#include "tdb/tdb.c"
+#include "tdb/lock.c"
+#include "tdb/freelist.c"
+#include "tdb/traverse.c"
+#include "tdb/transaction.c"
+#include "tdb/error.c"
+#include "tdb/open.c"
+#include "tap/tap.h"
+#include <stdlib.h>
+#include <stdbool.h>
+#include <err.h>
+
+static bool correct_key(TDB_DATA key)
+{
+       return key.dsize == strlen("hi")
+               && memcmp(key.dptr, "hi", key.dsize) == 0;
+}
+
+static bool correct_data(TDB_DATA data)
+{
+       return data.dsize == strlen("world")
+               && memcmp(data.dptr, "world", data.dsize) == 0;
+}
+
+static int traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
+                    void *p)
+{
+       ok1(correct_key(key));
+       ok1(correct_data(data));
+       return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       struct tdb_context *tdb;
+       TDB_DATA key, data;
+
+       plan_tests(12);
+       tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST,
+                      O_CREAT|O_TRUNC|O_RDWR, 0600);
+       ok1(tdb);
+
+       /* Tickle bug on appending zero length buffer to zero length buffer. */
+       key.dsize = strlen("hi");
+       key.dptr = (void *)"hi";
+       data.dptr = (void *)"world";
+       data.dsize = strlen("world");
+
+       ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
+
+       ok1(tdb_transaction_start(tdb) == 0);
+       ok(tdb->have_transaction_lock, "Transaction lock in transaction");
+       tdb_traverse(tdb, traverse, NULL);
+
+       /* That should *not* release the transaction lock! */
+       ok(tdb->have_transaction_lock, "Transaction lock after traverse");
+       tdb_traverse_read(tdb, traverse, NULL);
+
+       /* That should *not* release the transaction lock! */
+       ok(tdb->have_transaction_lock, "Transaction lock after traverse_read");
+       ok1(tdb_transaction_commit(tdb) == 0);
+       ok(!tdb->have_transaction_lock, "Transaction unlock");
+
+       tdb_close(tdb);
+
+       return exit_status();
+}
index 3159b014a22283f71ebbdb5978d5a7322839805a..d8b15aff4aa39f22f76fd6080e8e251404be4d75 100644 (file)
@@ -211,7 +211,7 @@ int tdb_traverse_read(struct tdb_context *tdb,
 
        /* we need to get a read lock on the transaction lock here to
           cope with the lock ordering semantics of solaris10 */
-       if (tdb->traverse_read == 0 && tdb_transaction_lock(tdb, F_RDLCK)) {
+       if (tdb_transaction_lock(tdb, F_RDLCK)) {
                return -1;
        }
 
@@ -220,9 +220,7 @@ int tdb_traverse_read(struct tdb_context *tdb,
        ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
        tdb->traverse_read--;
 
-       if (tdb->traverse_read == 0) {
-               tdb_transaction_unlock(tdb);
-       }
+       tdb_transaction_unlock(tdb);
 
        return ret;
 }
@@ -244,8 +242,7 @@ int tdb_traverse(struct tdb_context *tdb,
                return tdb_traverse_read(tdb, fn, private_data);
        }
 
-       /* Nested traversals: transaction lock doesn't nest. */
-       if (tdb->traverse_write == 0 && tdb_transaction_lock(tdb, F_WRLCK)) {
+       if (tdb_transaction_lock(tdb, F_WRLCK)) {
                return -1;
        }
 
@@ -254,9 +251,7 @@ int tdb_traverse(struct tdb_context *tdb,
        ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
        tdb->traverse_write--;
 
-       if (tdb->traverse_write == 0) {
-               tdb_transaction_unlock(tdb);
-       }
+       tdb_transaction_unlock(tdb);
 
        return ret;
 }