From 214c833e1cfcd07331e943a30d8d0aa2b7f6b76f Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Fri, 12 Dec 2014 11:22:47 +0100 Subject: [PATCH 1/4] tdb: allow transactions on on tdb's with TDB_MUTEX_LOCKING There's no real reason to disallow transactions as the allrecord lock is also available with mutexes enabled. E.g. ctdbd requires transactions also on non-persistent databases opened with TDB_CLEAR_IF_FIRST and TDB_MUTEX_LOCKING. Bug: https://bugzilla.samba.org/show_bug.cgi?id=11004 Signed-off-by: Stefan Metzmacher Reviewed-by: Amitay Isaacs (cherry picked from commit d0839af9d60a6fdf0720cd0b1a859e0e2241903f) --- lib/tdb/common/transaction.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tdb/common/transaction.c b/lib/tdb/common/transaction.c index caef0be..0dd057b 100644 --- a/lib/tdb/common/transaction.c +++ b/lib/tdb/common/transaction.c @@ -421,7 +421,7 @@ static int _tdb_transaction_start(struct tdb_context *tdb, enum tdb_lock_flags lockflags) { /* some sanity checks */ - if (tdb->read_only || (tdb->flags & (TDB_INTERNAL|TDB_MUTEX_LOCKING)) + if (tdb->read_only || (tdb->flags & TDB_INTERNAL) || tdb->traverse_read) { 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; -- 1.9.1 From d439c7c36a1878d8be66883a59ef3b4f72928b22 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Fri, 12 Dec 2014 12:24:50 +0100 Subject: [PATCH 2/4] tdb/test: add tdb1-run-mutex-transaction1 test Bug: https://bugzilla.samba.org/show_bug.cgi?id=11004 Signed-off-by: Stefan Metzmacher Reviewed-by: Amitay Isaacs (cherry picked from commit 5d5d91fae55a806261375b87bf7109a980471799) --- lib/tdb/test/run-mutex-transaction1.c | 236 ++++++++++++++++++++++++++++++++++ lib/tdb/wscript | 1 + 2 files changed, 237 insertions(+) create mode 100644 lib/tdb/test/run-mutex-transaction1.c diff --git a/lib/tdb/test/run-mutex-transaction1.c b/lib/tdb/test/run-mutex-transaction1.c new file mode 100644 index 0000000..7b9f7b1 --- /dev/null +++ b/lib/tdb/test/run-mutex-transaction1.c @@ -0,0 +1,236 @@ +#include "../common/tdb_private.h" +#include "../common/io.c" +#include "../common/tdb.c" +#include "../common/lock.c" +#include "../common/freelist.c" +#include "../common/traverse.c" +#include "../common/transaction.c" +#include "../common/error.c" +#include "../common/open.c" +#include "../common/check.c" +#include "../common/hash.c" +#include "../common/mutex.c" +#include "tap-interface.h" +#include +#include +#include +#include + +static TDB_DATA key, data; + +static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, + const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + +static int do_child(int tdb_flags, int to, int from) +{ + struct tdb_context *tdb; + unsigned int log_count; + struct tdb_logging_context log_ctx = { log_fn, &log_count }; + int ret; + char c = 0; + + tdb = tdb_open_ex("mutex-transaction1.tdb", 3, tdb_flags, + O_RDWR|O_CREAT, 0755, &log_ctx, NULL); + ok(tdb, "tdb_open_ex should succeed"); + + ret = tdb_transaction_start(tdb); + ok(ret == 0, "tdb_transaction_start should succeed"); + + ret = tdb_store(tdb, key, data, TDB_INSERT); + ok(ret == 0, "tdb_store(tdb, key, data, TDB_INSERT) should succeed"); + + write(to, &c, sizeof(c)); + read(from, &c, sizeof(c)); + + ret = tdb_transaction_cancel(tdb); + ok(ret == 0, "tdb_transaction_cancel should succeed"); + + write(to, &c, sizeof(c)); + read(from, &c, sizeof(c)); + + ret = tdb_transaction_start(tdb); + ok(ret == 0, "tdb_transaction_start should succeed"); + + ret = tdb_store(tdb, key, data, TDB_INSERT); + ok(ret == 0, "tdb_store(tdb, key, data, TDB_INSERT) should succeed"); + + write(to, &c, sizeof(c)); + read(from, &c, sizeof(c)); + + ret = tdb_transaction_commit(tdb); + ok(ret == 0, "tdb_transaction_commit should succeed"); + + write(to, &c, sizeof(c)); + read(from, &c, sizeof(c)); + + ret = tdb_transaction_start(tdb); + ok(ret == 0, "tdb_transaction_start should succeed"); + + ret = tdb_store(tdb, key, key, TDB_REPLACE); + ok(ret == 0, "tdb_store(tdb, key, data, TDB_REPLACE) should succeed"); + + write(to, &c, sizeof(c)); + read(from, &c, sizeof(c)); + + ret = tdb_transaction_commit(tdb); + ok(ret == 0, "tdb_transaction_commit should succeed"); + + write(to, &c, sizeof(c)); + read(from, &c, sizeof(c)); + + return 0; +} + +/* The code should barf on TDBs created with rwlocks. */ +int main(int argc, char *argv[]) +{ + struct tdb_context *tdb; + unsigned int log_count; + struct tdb_logging_context log_ctx = { log_fn, &log_count }; + int ret, status; + pid_t child, wait_ret; + int fromchild[2]; + int tochild[2]; + TDB_DATA val; + char c; + int tdb_flags; + bool runtime_support; + + runtime_support = tdb_runtime_check_for_robust_mutexes(); + + if (!runtime_support) { + skip(1, "No robust mutex support"); + return exit_status(); + } + + key.dsize = strlen("hi"); + key.dptr = discard_const_p(uint8_t, "hi"); + data.dsize = strlen("world"); + data.dptr = discard_const_p(uint8_t, "world"); + + pipe(fromchild); + pipe(tochild); + + tdb_flags = TDB_INCOMPATIBLE_HASH| + TDB_MUTEX_LOCKING| + TDB_CLEAR_IF_FIRST; + + child = fork(); + if (child == 0) { + close(fromchild[0]); + close(tochild[1]); + return do_child(tdb_flags, fromchild[1], tochild[0]); + } + close(fromchild[1]); + close(tochild[0]); + + read(fromchild[0], &c, sizeof(c)); + + tdb = tdb_open_ex("mutex-transaction1.tdb", 0, + tdb_flags, O_RDWR|O_CREAT, 0755, + &log_ctx, NULL); + ok(tdb, "tdb_open_ex should succeed"); + + /* + * The child has the transaction running + */ + ret = tdb_transaction_start_nonblock(tdb); + ok(ret == -1, "tdb_transaction_start_nonblock not succeed"); + + ret = tdb_chainlock_nonblock(tdb, key); + ok(ret == -1, "tdb_chainlock_nonblock should not succeed"); + + /* + * We can still read + */ + ret = tdb_exists(tdb, key); + ok(ret == 0, "tdb_exists(tdb, key) should return 0"); + + val = tdb_fetch(tdb, key); + ok(val.dsize == 0, "tdb_fetch(tdb, key) should return an empty value"); + + write(tochild[1], &c, sizeof(c)); + + /* + * When the child canceled we can start... + */ + ret = tdb_transaction_start(tdb); + ok(ret == 0, "tdb_transaction_start should succeed"); + + read(fromchild[0], &c, sizeof(c)); + write(tochild[1], &c, sizeof(c)); + + ret = tdb_transaction_cancel(tdb); + ok(ret == 0, "tdb_transaction_cancel should succeed"); + + /* + * When we canceled the child can start and store... + */ + read(fromchild[0], &c, sizeof(c)); + + /* + * We still see the old values before the child commits... + */ + ret = tdb_exists(tdb, key); + ok(ret == 0, "tdb_exists(tdb, key) should return 0"); + + val = tdb_fetch(tdb, key); + ok(val.dsize == 0, "tdb_fetch(tdb, key) should return an empty value"); + + write(tochild[1], &c, sizeof(c)); + read(fromchild[0], &c, sizeof(c)); + + /* + * We see the new values after the commit... + */ + ret = tdb_exists(tdb, key); + ok(ret == 1, "tdb_exists(tdb, key) should return 1"); + + val = tdb_fetch(tdb, key); + ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value"); + ok(val.dsize == data.dsize, "tdb_fetch(tdb, key) should return a value"); + ok(memcmp(val.dptr, data.dptr, data.dsize) == 0, "tdb_fetch(tdb, key) should return a value"); + + write(tochild[1], &c, sizeof(c)); + read(fromchild[0], &c, sizeof(c)); + + /* + * The child started a new transaction and replaces the value, + * but we still see the old values before the child commits... + */ + ret = tdb_exists(tdb, key); + ok(ret == 1, "tdb_exists(tdb, key) should return 1"); + + val = tdb_fetch(tdb, key); + ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value"); + ok(val.dsize == data.dsize, "tdb_fetch(tdb, key) should return a value"); + ok(memcmp(val.dptr, data.dptr, data.dsize) == 0, "tdb_fetch(tdb, key) should return a value"); + + write(tochild[1], &c, sizeof(c)); + read(fromchild[0], &c, sizeof(c)); + + /* + * We see the new values after the commit... + */ + ret = tdb_exists(tdb, key); + ok(ret == 1, "tdb_exists(tdb, key) should return 1"); + + val = tdb_fetch(tdb, key); + ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value"); + ok(val.dsize == key.dsize, "tdb_fetch(tdb, key) should return a value"); + ok(memcmp(val.dptr, key.dptr, key.dsize) == 0, "tdb_fetch(tdb, key) should return a value"); + + write(tochild[1], &c, sizeof(c)); + + wait_ret = wait(&status); + ok(wait_ret == child, "child should have exited correctly"); + + diag("done"); + return exit_status(); +} diff --git a/lib/tdb/wscript b/lib/tdb/wscript index d129b24..bae5f37 100644 --- a/lib/tdb/wscript +++ b/lib/tdb/wscript @@ -46,6 +46,7 @@ tdb1_unit_tests = [ 'run-mutex-allrecord-bench', 'run-mutex-allrecord-trylock', 'run-mutex-allrecord-block', + 'run-mutex-transaction1', 'run-mutex-die', 'run-mutex1', ] -- 1.9.1 From 85028321e08330dddf7cebe3b6846558d077a7dd Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Fri, 12 Dec 2014 12:53:37 +0100 Subject: [PATCH 3/4] tdb/toos: allow transactions with TDB_MUTEX_LOCKING Bug: https://bugzilla.samba.org/show_bug.cgi?id=11004 Signed-off-by: Stefan Metzmacher Reviewed-by: Amitay Isaacs (cherry picked from commit f32747ddd06445b5c51aa3f1b56fead48285a735) --- lib/tdb/tools/tdbtorture.c | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/tdb/tools/tdbtorture.c b/lib/tdb/tools/tdbtorture.c index 9278688..e4b8f69 100644 --- a/lib/tdb/tools/tdbtorture.c +++ b/lib/tdb/tools/tdbtorture.c @@ -120,7 +120,6 @@ static void addrec_db(void) #if TRANSACTION_PROB if (in_transaction == 0 && - ((tdb_get_flags(db) & TDB_MUTEX_LOCKING) == 0) && (always_transaction || random() % TRANSACTION_PROB == 0)) { if (tdb_transaction_start(db) != 0) { fatal("tdb_transaction_start failed"); -- 1.9.1 From 2bd4ce0263a64833515449abde7d7d3d25fafa40 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Fri, 12 Dec 2014 12:28:47 +0100 Subject: [PATCH 4/4] tdb: version 1.3.4 Transactions are supported with TDB_MUTEX_LOCKING. This fixes https://bugzilla.samba.org/show_bug.cgi?id=11004 Signed-off-by: Stefan Metzmacher Reviewed-by: Amitay Isaacs Autobuild-User(master): Amitay Isaacs Autobuild-Date(master): Fri Dec 19 11:41:26 CET 2014 on sn-devel-104 (cherry picked from commit a1a90f74eb10a10ac9d508028ed998f8c843f88a) --- lib/tdb/ABI/tdb-1.3.4.sigs | 68 ++++++++++++++++++++++++++++++++++++++++++++++ lib/tdb/wscript | 2 +- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 lib/tdb/ABI/tdb-1.3.4.sigs diff --git a/lib/tdb/ABI/tdb-1.3.4.sigs b/lib/tdb/ABI/tdb-1.3.4.sigs new file mode 100644 index 0000000..7d3e469 --- /dev/null +++ b/lib/tdb/ABI/tdb-1.3.4.sigs @@ -0,0 +1,68 @@ +tdb_add_flags: void (struct tdb_context *, unsigned int) +tdb_append: int (struct tdb_context *, TDB_DATA, TDB_DATA) +tdb_chainlock: int (struct tdb_context *, TDB_DATA) +tdb_chainlock_mark: int (struct tdb_context *, TDB_DATA) +tdb_chainlock_nonblock: int (struct tdb_context *, TDB_DATA) +tdb_chainlock_read: int (struct tdb_context *, TDB_DATA) +tdb_chainlock_unmark: int (struct tdb_context *, TDB_DATA) +tdb_chainunlock: int (struct tdb_context *, TDB_DATA) +tdb_chainunlock_read: int (struct tdb_context *, TDB_DATA) +tdb_check: int (struct tdb_context *, int (*)(TDB_DATA, TDB_DATA, void *), void *) +tdb_close: int (struct tdb_context *) +tdb_delete: int (struct tdb_context *, TDB_DATA) +tdb_dump_all: void (struct tdb_context *) +tdb_enable_seqnum: void (struct tdb_context *) +tdb_error: enum TDB_ERROR (struct tdb_context *) +tdb_errorstr: const char *(struct tdb_context *) +tdb_exists: int (struct tdb_context *, TDB_DATA) +tdb_fd: int (struct tdb_context *) +tdb_fetch: TDB_DATA (struct tdb_context *, TDB_DATA) +tdb_firstkey: TDB_DATA (struct tdb_context *) +tdb_freelist_size: int (struct tdb_context *) +tdb_get_flags: int (struct tdb_context *) +tdb_get_logging_private: void *(struct tdb_context *) +tdb_get_seqnum: int (struct tdb_context *) +tdb_hash_size: int (struct tdb_context *) +tdb_increment_seqnum_nonblock: void (struct tdb_context *) +tdb_jenkins_hash: unsigned int (TDB_DATA *) +tdb_lock_nonblock: int (struct tdb_context *, int, int) +tdb_lockall: int (struct tdb_context *) +tdb_lockall_mark: int (struct tdb_context *) +tdb_lockall_nonblock: int (struct tdb_context *) +tdb_lockall_read: int (struct tdb_context *) +tdb_lockall_read_nonblock: int (struct tdb_context *) +tdb_lockall_unmark: int (struct tdb_context *) +tdb_log_fn: tdb_log_func (struct tdb_context *) +tdb_map_size: size_t (struct tdb_context *) +tdb_name: const char *(struct tdb_context *) +tdb_nextkey: TDB_DATA (struct tdb_context *, TDB_DATA) +tdb_null: dptr = 0xXXXX, dsize = 0 +tdb_open: struct tdb_context *(const char *, int, int, int, mode_t) +tdb_open_ex: struct tdb_context *(const char *, int, int, int, mode_t, const struct tdb_logging_context *, tdb_hash_func) +tdb_parse_record: int (struct tdb_context *, TDB_DATA, int (*)(TDB_DATA, TDB_DATA, void *), void *) +tdb_printfreelist: int (struct tdb_context *) +tdb_remove_flags: void (struct tdb_context *, unsigned int) +tdb_reopen: int (struct tdb_context *) +tdb_reopen_all: int (int) +tdb_repack: int (struct tdb_context *) +tdb_rescue: int (struct tdb_context *, void (*)(TDB_DATA, TDB_DATA, void *), void *) +tdb_runtime_check_for_robust_mutexes: bool (void) +tdb_set_logging_function: void (struct tdb_context *, const struct tdb_logging_context *) +tdb_set_max_dead: void (struct tdb_context *, int) +tdb_setalarm_sigptr: void (struct tdb_context *, volatile sig_atomic_t *) +tdb_store: int (struct tdb_context *, TDB_DATA, TDB_DATA, int) +tdb_summary: char *(struct tdb_context *) +tdb_transaction_cancel: int (struct tdb_context *) +tdb_transaction_commit: int (struct tdb_context *) +tdb_transaction_prepare_commit: int (struct tdb_context *) +tdb_transaction_start: int (struct tdb_context *) +tdb_transaction_start_nonblock: int (struct tdb_context *) +tdb_transaction_write_lock_mark: int (struct tdb_context *) +tdb_transaction_write_lock_unmark: int (struct tdb_context *) +tdb_traverse: int (struct tdb_context *, tdb_traverse_func, void *) +tdb_traverse_read: int (struct tdb_context *, tdb_traverse_func, void *) +tdb_unlock: int (struct tdb_context *, int, int) +tdb_unlockall: int (struct tdb_context *) +tdb_unlockall_read: int (struct tdb_context *) +tdb_validate_freelist: int (struct tdb_context *, int *) +tdb_wipe_all: int (struct tdb_context *) diff --git a/lib/tdb/wscript b/lib/tdb/wscript index bae5f37..b283795 100644 --- a/lib/tdb/wscript +++ b/lib/tdb/wscript @@ -1,7 +1,7 @@ #!/usr/bin/env python APPNAME = 'tdb' -VERSION = '1.3.3' +VERSION = '1.3.4' blddir = 'bin' -- 1.9.1