From 3d82f786ecdd6747e90fe480a15de8c3fcea5f7b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 17 Feb 2013 20:56:34 +1030 Subject: ntdb: fix database corruption when transaction doesn't change anything. ntdb's transaction code has an optimization which tdb's doesnt: it only writes the parts of blocks whose contents have changed. This means we can actually have a transaction which turns out to need no recovery region. This breaks the recovery setup logic, which sets the current recovery size to 0 if there's no recovery area, and assumes that we'll always create a new recovery area since the recovery will always need > 0 bytes. In fact, if we really haven't changed anything, we can skip the transaction commit altogether: since this happens at least once with Samba, it's worth doing. Signed-off-by: Rusty Russell Reviewed-by: Andrew Bartlett --- lib/ntdb/test/api-60-noop-transaction.c | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/ntdb/test/api-60-noop-transaction.c (limited to 'lib/ntdb/test') diff --git a/lib/ntdb/test/api-60-noop-transaction.c b/lib/ntdb/test/api-60-noop-transaction.c new file mode 100644 index 0000000000..a429e67907 --- /dev/null +++ b/lib/ntdb/test/api-60-noop-transaction.c @@ -0,0 +1,58 @@ +#include "private.h" // struct ntdb_context +#include "ntdb.h" +#include "tap-interface.h" +#include +#include +#include +#include +#include "logging.h" + +int main(int argc, char *argv[]) +{ + unsigned int i; + struct ntdb_context *ntdb; + int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP, + NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT }; + NTDB_DATA key = ntdb_mkdata("key", 3); + NTDB_DATA data = ntdb_mkdata("data", 4), d; + + plan_tests(sizeof(flags) / sizeof(flags[0]) * 12 + 1); + + for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) { + ntdb = ntdb_open("api-60-transaction.ntdb", + flags[i]|MAYBE_NOSYNC, + O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr); + ok1(ntdb); + if (!ntdb) + continue; + + ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0); + + ok1(ntdb_transaction_start(ntdb) == 0); + /* Do an identical replace. */ + ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0); + ok1(ntdb_transaction_commit(ntdb) == 0); + + ok1(ntdb_check(ntdb, NULL, NULL) == 0); + ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS); + ok1(ntdb_deq(data, d)); + free(d.dptr); + ntdb_close(ntdb); + + /* Reopen, fetch. */ + ntdb = ntdb_open("api-60-transaction.ntdb", + flags[i]|MAYBE_NOSYNC, + O_RDWR, 0600, &tap_log_attr); + ok1(ntdb); + if (!ntdb) + continue; + ok1(ntdb_check(ntdb, NULL, NULL) == 0); + ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS); + ok1(ntdb_deq(data, d)); + free(d.dptr); + ntdb_close(ntdb); + } + + ok1(tap_log_messages == 0); + return exit_status(); +} -- cgit