summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2012-06-18 22:30:29 +0930
committerRusty Russell <rusty@rustcorp.com.au>2012-06-19 05:38:06 +0200
commitdb2508840d55842ebaf0c0d7a2fa3c855498e75f (patch)
tree6123390e0616ed98eb173422e94e8c5c0e862459
parent9396757676c304398a3e94ad01f2657e667b113c (diff)
downloadsamba-db2508840d55842ebaf0c0d7a2fa3c855498e75f.tar.gz
samba-db2508840d55842ebaf0c0d7a2fa3c855498e75f.tar.bz2
samba-db2508840d55842ebaf0c0d7a2fa3c855498e75f.zip
ntdb: create initial database to be multiple of NTDB_PGSIZE.
As copied from tdb1, there is logic in the transaction code to handle a non-PGSIZE multiple db, but in fact this only happens for a completely unused database: as soon as we add anything to it, it is expanded to a NTDB_PGSIZE multiple. If we create the database with a free record which pads it out to NTDB_PGSIZE, we can remove this last-page-is-different logic. Of course, the fake ntdbs we create in our tests now also need to be multiples of NTDB_PGSIZE, so we change some numbers there too. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
-rw-r--r--lib/ntdb/open.c125
-rw-r--r--lib/ntdb/test/api-91-get-stats.c8
-rw-r--r--lib/ntdb/test/failtest_helper.h2
-rw-r--r--lib/ntdb/test/run-03-coalesce.c36
-rw-r--r--lib/ntdb/test/run-30-exhaust-before-expand.c17
-rw-r--r--lib/ntdb/test/run-64-bit-tdb.c11
-rw-r--r--lib/ntdb/test/run-capabilities.c16
-rw-r--r--lib/ntdb/test/run-expand-in-transaction.c11
8 files changed, 138 insertions, 88 deletions
diff --git a/lib/ntdb/open.c b/lib/ntdb/open.c
index 56b504b5c0..5a781c9328 100644
--- a/lib/ntdb/open.c
+++ b/lib/ntdb/open.c
@@ -108,6 +108,7 @@ static void ntdb_context_init(struct ntdb_context *ntdb)
struct new_database {
struct ntdb_header hdr;
struct ntdb_freetable ftable;
+ struct ntdb_free_record remainder;
};
/* initialise a new database */
@@ -116,84 +117,109 @@ static enum NTDB_ERROR ntdb_new_database(struct ntdb_context *ntdb,
struct ntdb_header *hdr)
{
/* We make it up in memory, then write it out if not internal */
- struct new_database newdb;
+ struct new_database *newdb;
unsigned int magic_len;
ssize_t rlen;
+ size_t dbsize, remaindersize;
enum NTDB_ERROR ecode;
+ /* Always make db a multiple of NTDB_PGSIZE */
+ dbsize = (sizeof(*newdb) + NTDB_PGSIZE-1) & ~(NTDB_PGSIZE-1);
+ remaindersize = dbsize - sizeof(*newdb);
+ newdb = malloc(dbsize);
+ if (!newdb) {
+ return ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
+ "ntdb_new_database: failed to allocate");
+ }
+
/* Fill in the header */
- newdb.hdr.version = NTDB_VERSION;
+ newdb->hdr.version = NTDB_VERSION;
if (seed)
- newdb.hdr.hash_seed = seed->seed;
+ newdb->hdr.hash_seed = seed->seed;
else
- newdb.hdr.hash_seed = random_number(ntdb);
- newdb.hdr.hash_test = NTDB_HASH_MAGIC;
- newdb.hdr.hash_test = ntdb->hash_fn(&newdb.hdr.hash_test,
- sizeof(newdb.hdr.hash_test),
- newdb.hdr.hash_seed,
+ newdb->hdr.hash_seed = random_number(ntdb);
+ newdb->hdr.hash_test = NTDB_HASH_MAGIC;
+ newdb->hdr.hash_test = ntdb->hash_fn(&newdb->hdr.hash_test,
+ sizeof(newdb->hdr.hash_test),
+ newdb->hdr.hash_seed,
ntdb->hash_data);
- newdb.hdr.recovery = 0;
- newdb.hdr.features_used = newdb.hdr.features_offered = NTDB_FEATURE_MASK;
- newdb.hdr.seqnum = 0;
- newdb.hdr.capabilities = 0;
- memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
+ newdb->hdr.recovery = 0;
+ newdb->hdr.features_used = newdb->hdr.features_offered = NTDB_FEATURE_MASK;
+ newdb->hdr.seqnum = 0;
+ newdb->hdr.capabilities = 0;
+ memset(newdb->hdr.reserved, 0, sizeof(newdb->hdr.reserved));
/* Initial hashes are empty. */
- memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
+ memset(newdb->hdr.hashtable, 0, sizeof(newdb->hdr.hashtable));
/* Free is empty. */
- newdb.hdr.free_table = offsetof(struct new_database, ftable);
- memset(&newdb.ftable, 0, sizeof(newdb.ftable));
- ecode = set_header(NULL, &newdb.ftable.hdr, NTDB_FTABLE_MAGIC, 0,
- sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
- sizeof(newdb.ftable) - sizeof(newdb.ftable.hdr),
+ newdb->hdr.free_table = offsetof(struct new_database, ftable);
+ memset(&newdb->ftable, 0, sizeof(newdb->ftable));
+ ecode = set_header(NULL, &newdb->ftable.hdr, NTDB_FTABLE_MAGIC, 0,
+ sizeof(newdb->ftable) - sizeof(newdb->ftable.hdr),
+ sizeof(newdb->ftable) - sizeof(newdb->ftable.hdr),
0);
if (ecode != NTDB_SUCCESS) {
- return ecode;
+ goto out;
}
+ /* Rest of database is a free record, containing junk. */
+ newdb->remainder.ftable_and_len
+ = (remaindersize + sizeof(newdb->remainder)
+ - sizeof(struct ntdb_used_record));
+ newdb->remainder.next = 0;
+ newdb->remainder.magic_and_prev
+ = (NTDB_FREE_MAGIC << (64-NTDB_OFF_UPPER_STEAL))
+ | offsetof(struct new_database, remainder);
+ memset(&newdb->remainder + 1, 0x43, remaindersize);
+
+ /* Put in our single free entry. */
+ newdb->ftable.buckets[size_to_bucket(remaindersize)] =
+ offsetof(struct new_database, remainder);
+
/* Magic food */
- memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
- strcpy(newdb.hdr.magic_food, NTDB_MAGIC_FOOD);
+ memset(newdb->hdr.magic_food, 0, sizeof(newdb->hdr.magic_food));
+ strcpy(newdb->hdr.magic_food, NTDB_MAGIC_FOOD);
/* This creates an endian-converted database, as if read from disk */
- magic_len = sizeof(newdb.hdr.magic_food);
+ magic_len = sizeof(newdb->hdr.magic_food);
ntdb_convert(ntdb,
- (char *)&newdb.hdr + magic_len, sizeof(newdb) - magic_len);
+ (char *)&newdb->hdr + magic_len,
+ sizeof(*newdb) - magic_len);
- *hdr = newdb.hdr;
+ *hdr = newdb->hdr;
if (ntdb->flags & NTDB_INTERNAL) {
- ntdb->file->map_size = sizeof(newdb);
- ntdb->file->map_ptr = malloc(ntdb->file->map_size);
- if (!ntdb->file->map_ptr) {
- return ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
- "ntdb_new_database:"
- " failed to allocate");
- }
- memcpy(ntdb->file->map_ptr, &newdb, ntdb->file->map_size);
+ ntdb->file->map_size = dbsize;
+ ntdb->file->map_ptr = newdb;
return NTDB_SUCCESS;
}
if (lseek(ntdb->file->fd, 0, SEEK_SET) == -1) {
- return ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
- "ntdb_new_database:"
- " failed to seek: %s", strerror(errno));
+ ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
+ "ntdb_new_database:"
+ " failed to seek: %s", strerror(errno));
+ goto out;
}
if (ftruncate(ntdb->file->fd, 0) == -1) {
- return ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
- "ntdb_new_database:"
- " failed to truncate: %s", strerror(errno));
+ ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
+ "ntdb_new_database:"
+ " failed to truncate: %s", strerror(errno));
+ goto out;
}
- rlen = write(ntdb->file->fd, &newdb, sizeof(newdb));
- if (rlen != sizeof(newdb)) {
+ rlen = write(ntdb->file->fd, newdb, dbsize);
+ if (rlen != dbsize) {
if (rlen >= 0)
errno = ENOSPC;
- return ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
- "ntdb_new_database: %zi writing header: %s",
- rlen, strerror(errno));
+ ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
+ "ntdb_new_database: %zi writing header: %s",
+ rlen, strerror(errno));
+ goto out;
}
- return NTDB_SUCCESS;
+
+out:
+ free(newdb);
+ return ecode;
}
static enum NTDB_ERROR ntdb_new_file(struct ntdb_context *ntdb)
@@ -654,6 +680,15 @@ _PUBLIC_ struct ntdb_context *ntdb_open(const char *name, int ntdb_flags,
if (unlikely(ecode != NTDB_SUCCESS))
goto fail;
+ if (ntdb->file->map_size % NTDB_PGSIZE != 0) {
+ ecode = ntdb_logerr(ntdb, NTDB_ERR_IO, NTDB_LOG_ERROR,
+ "ntdb_open:"
+ " %s size %llu isn't a multiple of %u",
+ name, (long long)ntdb->file->map_size,
+ NTDB_PGSIZE);
+ goto fail;
+ }
+
/* Now it's fully formed, recover if necessary. */
berr = ntdb_needs_recovery(ntdb);
if (unlikely(berr != false)) {
diff --git a/lib/ntdb/test/api-91-get-stats.c b/lib/ntdb/test/api-91-get-stats.c
index 786885b44c..e44b8a5061 100644
--- a/lib/ntdb/test/api-91-get-stats.c
+++ b/lib/ntdb/test/api-91-get-stats.c
@@ -19,12 +19,16 @@ int main(int argc, char *argv[])
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
union ntdb_attribute *attr;
- NTDB_DATA key = ntdb_mkdata("key", 3);
+ NTDB_DATA key = ntdb_mkdata("key", 3), data;
ntdb = ntdb_open("run-91-get-stats.ntdb", flags[i],
O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
ok1(ntdb);
- ok1(ntdb_store(ntdb, key, key, NTDB_REPLACE) == 0);
+ /* Force an expansion */
+ data.dsize = 65536;
+ data.dptr = calloc(data.dsize, 1);
+ ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
+ free(data.dptr);
/* Use malloc so valgrind will catch overruns. */
attr = malloc(sizeof *attr);
diff --git a/lib/ntdb/test/failtest_helper.h b/lib/ntdb/test/failtest_helper.h
index d347d75fba..5f9166d8d5 100644
--- a/lib/ntdb/test/failtest_helper.h
+++ b/lib/ntdb/test/failtest_helper.h
@@ -4,7 +4,7 @@
#include <stdbool.h>
/* FIXME: Check these! */
-#define INITIAL_NTDB_MALLOC "open.c", 399, FAILTEST_MALLOC
+#define INITIAL_NTDB_MALLOC "open.c", 425, FAILTEST_MALLOC
#define URANDOM_OPEN "open.c", 62, FAILTEST_OPEN
#define URANDOM_READ "open.c", 42, FAILTEST_READ
diff --git a/lib/ntdb/test/run-03-coalesce.c b/lib/ntdb/test/run-03-coalesce.c
index f93b33a1c3..22a6817881 100644
--- a/lib/ntdb/test/run-03-coalesce.c
+++ b/lib/ntdb/test/run-03-coalesce.c
@@ -34,7 +34,7 @@ int main(int argc, char *argv[])
/* No coalescing can be done due to EOF */
layout = new_ntdb_layout();
ntdb_layout_add_freetable(layout);
- len = 1024;
+ len = 56544;
ntdb_layout_add_free(layout, len, 0);
ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
/* NOMMAP is for lockcheck. */
@@ -60,24 +60,24 @@ int main(int argc, char *argv[])
/* No coalescing can be done due to used record */
layout = new_ntdb_layout();
ntdb_layout_add_freetable(layout);
- ntdb_layout_add_free(layout, 1024, 0);
+ ntdb_layout_add_free(layout, 56512, 0);
ntdb_layout_add_used(layout, key, data, 6);
ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
/* NOMMAP is for lockcheck. */
ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
&tap_log_attr);
- ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
+ ok1(free_record_length(ntdb, layout->elem[1].base.off) == 56512);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
/* Figure out which bucket free entry is. */
- b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
+ b_off = bucket_off(ntdb->ftable_off, size_to_bucket(56512));
/* Lock and fail to coalesce. */
ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
test = layout->elem[1].base.off;
- ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
+ ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 56512, &test)
== 0);
ntdb_unlock_free_bucket(ntdb, b_off);
- ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
+ ok1(free_record_length(ntdb, layout->elem[1].base.off) == 56512);
ok1(test == layout->elem[1].base.off);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
ntdb_close(ntdb);
@@ -87,13 +87,13 @@ int main(int argc, char *argv[])
layout = new_ntdb_layout();
ntdb_layout_add_freetable(layout);
ntdb_layout_add_free(layout, 1024, 0);
- ntdb_layout_add_free(layout, 2048, 0);
+ ntdb_layout_add_free(layout, 55504, 0);
ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
/* NOMMAP is for lockcheck. */
ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
&tap_log_attr);
ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
- ok1(free_record_length(ntdb, layout->elem[2].base.off) == 2048);
+ ok1(free_record_length(ntdb, layout->elem[2].base.off) == 55504);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
/* Figure out which bucket (first) free entry is. */
@@ -102,12 +102,12 @@ int main(int argc, char *argv[])
ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
test = layout->elem[2].base.off;
ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
- == 1024 + sizeof(struct ntdb_used_record) + 2048);
+ == 1024 + sizeof(struct ntdb_used_record) + 55504);
/* Should tell us it's erased this one... */
ok1(test == NTDB_ERR_NOEXIST);
ok1(ntdb->file->allrecord_lock.count == 0 && ntdb->file->num_lockrecs == 0);
ok1(free_record_length(ntdb, layout->elem[1].base.off)
- == 1024 + sizeof(struct ntdb_used_record) + 2048);
+ == 1024 + sizeof(struct ntdb_used_record) + 55504);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
ntdb_close(ntdb);
ntdb_layout_free(layout);
@@ -116,14 +116,14 @@ int main(int argc, char *argv[])
layout = new_ntdb_layout();
ntdb_layout_add_freetable(layout);
ntdb_layout_add_free(layout, 1024, 0);
- ntdb_layout_add_free(layout, 512, 0);
+ ntdb_layout_add_free(layout, 55472, 0);
ntdb_layout_add_used(layout, key, data, 6);
ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
/* NOMMAP is for lockcheck. */
ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
&tap_log_attr);
ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
- ok1(free_record_length(ntdb, layout->elem[2].base.off) == 512);
+ ok1(free_record_length(ntdb, layout->elem[2].base.off) == 55472);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
/* Figure out which bucket free entry is. */
@@ -132,10 +132,10 @@ int main(int argc, char *argv[])
ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
test = layout->elem[2].base.off;
ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
- == 1024 + sizeof(struct ntdb_used_record) + 512);
+ == 1024 + sizeof(struct ntdb_used_record) + 55472);
ok1(ntdb->file->allrecord_lock.count == 0 && ntdb->file->num_lockrecs == 0);
ok1(free_record_length(ntdb, layout->elem[1].base.off)
- == 1024 + sizeof(struct ntdb_used_record) + 512);
+ == 1024 + sizeof(struct ntdb_used_record) + 55472);
ok1(test == NTDB_ERR_NOEXIST);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
ntdb_close(ntdb);
@@ -146,14 +146,14 @@ int main(int argc, char *argv[])
ntdb_layout_add_freetable(layout);
ntdb_layout_add_free(layout, 1024, 0);
ntdb_layout_add_free(layout, 512, 0);
- ntdb_layout_add_free(layout, 256, 0);
+ ntdb_layout_add_free(layout, 54976, 0);
ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
/* NOMMAP is for lockcheck. */
ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
&tap_log_attr);
ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
ok1(free_record_length(ntdb, layout->elem[2].base.off) == 512);
- ok1(free_record_length(ntdb, layout->elem[3].base.off) == 256);
+ ok1(free_record_length(ntdb, layout->elem[3].base.off) == 54976);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
/* Figure out which bucket free entry is. */
@@ -163,12 +163,12 @@ int main(int argc, char *argv[])
test = layout->elem[2].base.off;
ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
== 1024 + sizeof(struct ntdb_used_record) + 512
- + sizeof(struct ntdb_used_record) + 256);
+ + sizeof(struct ntdb_used_record) + 54976);
ok1(ntdb->file->allrecord_lock.count == 0
&& ntdb->file->num_lockrecs == 0);
ok1(free_record_length(ntdb, layout->elem[1].base.off)
== 1024 + sizeof(struct ntdb_used_record) + 512
- + sizeof(struct ntdb_used_record) + 256);
+ + sizeof(struct ntdb_used_record) + 54976);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
ntdb_close(ntdb);
ntdb_layout_free(layout);
diff --git a/lib/ntdb/test/run-30-exhaust-before-expand.c b/lib/ntdb/test/run-30-exhaust-before-expand.c
index e3831f51a7..24c48b005a 100644
--- a/lib/ntdb/test/run-30-exhaust-before-expand.c
+++ b/lib/ntdb/test/run-30-exhaust-before-expand.c
@@ -27,11 +27,11 @@ int main(int argc, char *argv[])
NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
NTDB_NOMMAP|NTDB_CONVERT };
- plan_tests(sizeof(flags) / sizeof(flags[0]) * 11 + 1);
+ plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
NTDB_DATA k, d;
- uint64_t size, old_size;
+ uint64_t size;
bool was_empty = false;
k.dptr = (void *)&j;
@@ -43,22 +43,15 @@ int main(int argc, char *argv[])
if (!ntdb)
continue;
- old_size = ntdb->file->map_size;
-
- ok1(empty_freetable(ntdb));
- /* Need some hash lock for expand. */
- ok1(ntdb_lock_hashes(ntdb, 0, 1, F_WRLCK, NTDB_LOCK_WAIT) == 0);
- /* Create some free space. */
- ok1(ntdb_expand(ntdb, 1) == 0);
- ok1(ntdb_unlock_hashes(ntdb, 0, 1, F_WRLCK) == 0);
ok1(ntdb_check(ntdb, NULL, NULL) == 0);
+ /* There's one empty record in initial db. */
ok1(!empty_freetable(ntdb));
size = ntdb->file->map_size;
/* Create one record to chew up most space. */
- d.dsize = (size - old_size - 32);
- d.dptr = malloc(d.dsize);
+ d.dsize = size - sizeof(struct new_database) - 32;
+ d.dptr = calloc(d.dsize, 1);
j = 0;
ok1(ntdb_store(ntdb, k, d, NTDB_INSERT) == 0);
ok1(ntdb->file->map_size == size);
diff --git a/lib/ntdb/test/run-64-bit-tdb.c b/lib/ntdb/test/run-64-bit-tdb.c
index b36f422a97..a85a4af56c 100644
--- a/lib/ntdb/test/run-64-bit-tdb.c
+++ b/lib/ntdb/test/run-64-bit-tdb.c
@@ -21,7 +21,7 @@ int main(int argc, char *argv[])
return exit_status();
}
- plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
+ plan_tests(sizeof(flags) / sizeof(flags[0]) * 16);
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
off_t old_size;
NTDB_DATA k, d;
@@ -37,6 +37,15 @@ int main(int argc, char *argv[])
old_size = ntdb->file->map_size;
+ /* Add a fake record to chew up the existing free space. */
+ k = ntdb_mkdata("fake", 4);
+ d.dsize = ntdb->file->map_size - sizeof(struct new_database)- 8;
+ d.dptr = malloc(d.dsize);
+ memset(d.dptr, 0, d.dsize);
+ ok1(ntdb_store(ntdb, k, d, NTDB_INSERT) == 0);
+ ok1(ntdb->file->map_size == old_size);
+ free(d.dptr);
+
/* This makes a sparse file */
ok1(ftruncate(ntdb->file->fd, ALMOST_4G) == 0);
ok1(add_free_record(ntdb, old_size, ALMOST_4G - old_size,
diff --git a/lib/ntdb/test/run-capabilities.c b/lib/ntdb/test/run-capabilities.c
index c2c6aa15db..52c4fac0e6 100644
--- a/lib/ntdb/test/run-capabilities.c
+++ b/lib/ntdb/test/run-capabilities.c
@@ -29,7 +29,7 @@ static void create_ntdb(const char *name,
va_list ap;
struct ntdb_layout *layout;
struct ntdb_context *ntdb;
- int fd;
+ int fd, clen;
key = ntdb_mkdata("Hello", 5);
data = ntdb_mkdata("world", 5);
@@ -38,10 +38,11 @@ static void create_ntdb(const char *name,
layout = new_ntdb_layout();
ntdb_layout_add_freetable(layout);
ntdb_layout_add_used(layout, key, data, 6);
- ntdb_layout_add_free(layout, 80, 0);
+ clen = len_of(breaks_check, breaks_write, breaks_open);
+ ntdb_layout_add_free(layout, 56480 - clen, 0);
ntdb_layout_add_capability(layout, cap,
- breaks_write, breaks_check, breaks_open,
- len_of(breaks_check, breaks_write, breaks_open));
+ breaks_write, breaks_check, breaks_open,
+ clen);
va_start(ap, breaks_open);
while ((cap = va_arg(ap, int)) != 0) {
@@ -51,12 +52,11 @@ static void create_ntdb(const char *name,
key.dsize--;
ntdb_layout_add_used(layout, key, data, 11 - key.dsize);
- ntdb_layout_add_free(layout, 80, 0);
+ clen = len_of(breaks_check, breaks_write, breaks_open);
+ ntdb_layout_add_free(layout, 65456 - clen, 0);
ntdb_layout_add_capability(layout, cap,
breaks_write, breaks_check,
- breaks_open,
- len_of(breaks_check, breaks_write,
- breaks_open));
+ breaks_open, clen);
}
va_end(ap);
diff --git a/lib/ntdb/test/run-expand-in-transaction.c b/lib/ntdb/test/run-expand-in-transaction.c
index dadbec7922..54f9d81dec 100644
--- a/lib/ntdb/test/run-expand-in-transaction.c
+++ b/lib/ntdb/test/run-expand-in-transaction.c
@@ -11,10 +11,11 @@ int main(int argc, char *argv[])
NTDB_DATA key = ntdb_mkdata("key", 3);
NTDB_DATA data = ntdb_mkdata("data", 4);
- plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
+ plan_tests(sizeof(flags) / sizeof(flags[0]) * 9 + 1);
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
size_t size;
+ NTDB_DATA k, d;
ntdb = ntdb_open("run-expand-in-transaction.ntdb", flags[i],
O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
ok1(ntdb);
@@ -22,6 +23,14 @@ int main(int argc, char *argv[])
continue;
size = ntdb->file->map_size;
+ /* Add a fake record to chew up the existing free space. */
+ k = ntdb_mkdata("fake", 4);
+ d.dsize = ntdb->file->map_size - sizeof(struct new_database)- 8;
+ d.dptr = malloc(d.dsize);
+ memset(d.dptr, 0, d.dsize);
+ ok1(ntdb_store(ntdb, k, d, NTDB_INSERT) == 0);
+ ok1(ntdb->file->map_size == size);
+ free(d.dptr);
ok1(ntdb_transaction_start(ntdb) == 0);
ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
ok1(ntdb->file->map_size > size);