From e1fd1d9623ef2f944374d67b573a6f1cf7a9ed12 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 24 Mar 2011 11:42:21 +1030 Subject: [PATCH] tdb2: fix two transaction bugs. One but were we didn't update the map_size if we expanded the transaction but didn't create a new recovery area (most easily reproduced by setting the TDB_NOSYNC flag). Another out-by-one bug in transaction_direct where we would give read-access to the underlying file despite the last block having been modified. Both these were found by tdbtorture. --- ccan/tdb2/test/run-expand-in-transaction.c | 45 ++++++++++++++++++++++ ccan/tdb2/transaction.c | 5 ++- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 ccan/tdb2/test/run-expand-in-transaction.c diff --git a/ccan/tdb2/test/run-expand-in-transaction.c b/ccan/tdb2/test/run-expand-in-transaction.c new file mode 100644 index 00000000..49ba03c9 --- /dev/null +++ b/ccan/tdb2/test/run-expand-in-transaction.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "logging.h" + +int main(int argc, char *argv[]) +{ + unsigned int i; + struct tdb_context *tdb; + int flags[] = { TDB_DEFAULT, TDB_NOMMAP, + TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT, + TDB_CONVERT|TDB_NOSYNC, + TDB_NOMMAP|TDB_CONVERT|TDB_NOSYNC }; + struct tdb_data key = tdb_mkdata("key", 3); + struct tdb_data data = tdb_mkdata("data", 4); + + plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1); + + for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) { + size_t size; + tdb = tdb_open("run-expand-in-transaction.tdb", flags[i], + O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr); + ok1(tdb); + if (!tdb) + continue; + + size = tdb->file->map_size; + ok1(tdb_transaction_start(tdb) == 0); + ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0); + ok1(tdb->file->map_size > size); + ok1(tdb_transaction_commit(tdb) == 0); + ok1(tdb->file->map_size > size); + ok1(tdb_check(tdb, NULL, NULL) == 0); + tdb_close(tdb); + } + + ok1(tap_log_messages == 0); + return exit_status(); +} diff --git a/ccan/tdb2/transaction.c b/ccan/tdb2/transaction.c index b80cede7..976ebd74 100644 --- a/ccan/tdb2/transaction.c +++ b/ccan/tdb2/transaction.c @@ -402,7 +402,7 @@ static void *transaction_direct(struct tdb_context *tdb, tdb_off_t off, return tdb->transaction->blocks[blk] + off % getpagesize(); /* Otherwise must be all not copied. */ - while (blk < end_blk) { + while (blk <= end_blk) { if (blk >= tdb->transaction->num_blocks) break; if (tdb->transaction->blocks[blk]) @@ -1080,7 +1080,8 @@ enum TDB_ERROR tdb_transaction_commit(struct tdb_context *tdb) #endif /* use a transaction cancel to free memory and remove the - transaction locks */ + transaction locks: it "restores" map_size, too. */ + tdb->transaction->old_map_size = tdb->file->map_size; _tdb_transaction_cancel(tdb); return tdb->last_error = TDB_SUCCESS; -- 2.39.2