/* Need to hold a hash lock to expand DB: transactions rely on it. */
if (!(tdb->flags & TDB_NOLOCK)
- && !tdb->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
+ && !tdb->file->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_expand: must hold lock during expand");
}
return;
tdb->map_ptr = mmap(NULL, tdb->map_size, tdb->mmap_flags,
- MAP_SHARED, tdb->fd, 0);
+ MAP_SHARED, tdb->file->fd, 0);
/*
* NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
return ecode;
}
- if (fstat(tdb->fd, &st) != 0) {
+ if (fstat(tdb->file->fd, &st) != 0) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"Failed to fstat file: %s", strerror(errno));
tdb_unlock_expand(tdb, F_RDLCK);
memcpy(off + (char *)tdb->map_ptr, buf, len);
} else {
ssize_t ret;
- ret = pwrite(tdb->fd, buf, len, off);
+ ret = pwrite(tdb->file->fd, buf, len, off);
if (ret != len) {
/* This shouldn't happen: we avoid sparse files. */
if (ret >= 0)
if (tdb->map_ptr) {
memcpy(buf, off + (char *)tdb->map_ptr, len);
} else {
- ssize_t r = pread(tdb->fd, buf, len, off);
+ ssize_t r = pread(tdb->file->fd, buf, len, off);
if (r != len) {
return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_read failed with %zi at %zu "
{
while (len) {
size_t n = len > size ? size : len;
- ssize_t ret = pwrite(tdb->fd, buf, n, off);
+ ssize_t ret = pwrite(tdb->file->fd, buf, n, off);
if (ret != n) {
if (ret >= 0)
errno = ENOSPC;
tdb_munmap(tdb);
/* If this fails, we try to fill anyway. */
- if (ftruncate(tdb->fd, tdb->map_size + addition))
+ if (ftruncate(tdb->file->fd, tdb->map_size + addition))
;
/* now fill the file with something. This ensures that the
add_stat(tdb, lock_lowlevel, 1);
if (waitflag)
- return fcntl(tdb->fd, F_SETLKW, &fl);
+ return fcntl(tdb->file->fd, F_SETLKW, &fl);
else {
add_stat(tdb, lock_nonblock, 1);
- return fcntl(tdb->fd, F_SETLK, &fl);
+ return fcntl(tdb->file->fd, F_SETLK, &fl);
}
}
fl.l_len = len;
fl.l_pid = 0;
- return fcntl(tdb->fd, F_SETLKW, &fl);
+ return fcntl(tdb->file->fd, F_SETLKW, &fl);
}
/* a byte range locking function - return 0 on success
"tdb_brlock failed (fd=%d) at"
" offset %zu rw_type=%d flags=%d len=%zu:"
" %s",
- tdb->fd, (size_t)offset, rw_type,
+ tdb->file->fd, (size_t)offset, rw_type,
flags, (size_t)len, strerror(errno));
}
return TDB_ERR_LOCK;
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_brunlock failed (fd=%d) at offset %zu"
" rw_type=%d len=%zu",
- tdb->fd, (size_t)offset, rw_type,
+ tdb->file->fd, (size_t)offset, rw_type,
(size_t)len);
}
return TDB_SUCCESS;
{
int count = 1000;
- if (tdb->allrecord_lock.count != 1) {
+ if (tdb->file->allrecord_lock.count != 1) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_upgrade failed:"
" count %u too high",
- tdb->allrecord_lock.count);
+ tdb->file->allrecord_lock.count);
}
- if (tdb->allrecord_lock.off != 1) {
+ if (tdb->file->allrecord_lock.off != 1) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_upgrade failed:"
" already upgraded?");
if (tdb_brlock(tdb, F_WRLCK,
TDB_HASH_LOCK_START, 0,
TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) {
- tdb->allrecord_lock.ltype = F_WRLCK;
- tdb->allrecord_lock.off = 0;
+ tdb->file->allrecord_lock.ltype = F_WRLCK;
+ tdb->file->allrecord_lock.off = 0;
return TDB_SUCCESS;
}
if (errno != EDEADLK) {
{
unsigned int i;
- for (i=0; i<tdb->num_lockrecs; i++) {
- if (tdb->lockrecs[i].off == offset) {
- return &tdb->lockrecs[i];
+ for (i=0; i<tdb->file->num_lockrecs; i++) {
+ if (tdb->file->lockrecs[i].off == offset) {
+ return &tdb->file->lockrecs[i];
}
}
return NULL;
return TDB_SUCCESS;
}
- if (tdb->num_lockrecs
+ if (tdb->file->num_lockrecs
&& offset >= TDB_HASH_LOCK_START
&& offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
}
new_lck = (struct tdb_lock_type *)realloc(
- tdb->lockrecs,
- sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
+ tdb->file->lockrecs,
+ sizeof(*tdb->file->lockrecs) * (tdb->file->num_lockrecs+1));
if (new_lck == NULL) {
return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"tdb_nest_lock:"
" unable to allocate %zu lock struct",
- tdb->num_lockrecs + 1);
+ tdb->file->num_lockrecs + 1);
}
- tdb->lockrecs = new_lck;
+ tdb->file->lockrecs = new_lck;
/* Since fcntl locks don't nest, we do a lock for the first one,
and simply bump the count for future ones */
/* First time we grab a lock, perhaps someone died in commit? */
if (!(flags & TDB_LOCK_NOCHECK)
- && tdb->num_lockrecs == 0) {
+ && tdb->file->num_lockrecs == 0) {
tdb_bool_err berr = tdb_needs_recovery(tdb);
if (berr != false) {
tdb_brunlock(tdb, ltype, offset, 1);
}
}
- tdb->lockrecs[tdb->num_lockrecs].off = offset;
- tdb->lockrecs[tdb->num_lockrecs].count = 1;
- tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
- tdb->num_lockrecs++;
+ tdb->file->lockrecs[tdb->file->num_lockrecs].off = offset;
+ tdb->file->lockrecs[tdb->file->num_lockrecs].count = 1;
+ tdb->file->lockrecs[tdb->file->num_lockrecs].ltype = ltype;
+ tdb->file->num_lockrecs++;
return TDB_SUCCESS;
}
* Shrink the array by overwriting the element just unlocked with the
* last array element.
*/
- *lck = tdb->lockrecs[--tdb->num_lockrecs];
+ *lck = tdb->file->lockrecs[--tdb->file->num_lockrecs];
return ecode;
}
enum TDB_ERROR ecode;
tdb_bool_err berr;
- if (tdb->allrecord_lock.count
- && (ltype == F_RDLCK || tdb->allrecord_lock.ltype == F_WRLCK)) {
- tdb->allrecord_lock.count++;
+ if (tdb->file->allrecord_lock.count
+ && (ltype == F_RDLCK
+ || tdb->file->allrecord_lock.ltype == F_WRLCK)) {
+ tdb->file->allrecord_lock.count++;
return TDB_SUCCESS;
}
- if (tdb->allrecord_lock.count) {
+ if (tdb->file->allrecord_lock.count) {
/* a global lock of a different type exists */
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_allrecord_lock: already have %s lock",
- tdb->allrecord_lock.ltype == F_RDLCK
+ tdb->file->allrecord_lock.ltype == F_RDLCK
? "read" : "write");
}
return ecode;
}
- tdb->allrecord_lock.count = 1;
+ tdb->file->allrecord_lock.count = 1;
/* If it's upgradable, it's actually exclusive so we can treat
* it as a write lock. */
- tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
- tdb->allrecord_lock.off = upgradable;
+ tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
+ tdb->file->allrecord_lock.off = upgradable;
/* Now check for needing recovery. */
if (flags & TDB_LOCK_NOCHECK)
bool tdb_has_open_lock(struct tdb_context *tdb)
{
- return find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
+ return !(tdb->flags & TDB_NOLOCK)
+ && find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
}
enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype)
/* unlock entire db */
void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
{
- if (tdb->allrecord_lock.count == 0) {
+ if (tdb->file->allrecord_lock.count == 0) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_allrecord_unlock: not locked!");
return;
}
/* Upgradable locks are marked as write locks. */
- if (tdb->allrecord_lock.ltype != ltype
- && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
+ if (tdb->file->allrecord_lock.ltype != ltype
+ && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_unlock: have %s lock",
tdb->allrecord_lock.ltype == F_RDLCK
return;
}
- if (tdb->allrecord_lock.count > 1) {
- tdb->allrecord_lock.count--;
+ if (tdb->file->allrecord_lock.count > 1) {
+ tdb->file->allrecord_lock.count--;
return;
}
- tdb->allrecord_lock.count = 0;
- tdb->allrecord_lock.ltype = 0;
+ tdb->file->allrecord_lock.count = 0;
+ tdb->file->allrecord_lock.ltype = 0;
tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
}
{
unsigned int i;
- for (i=0; i<tdb->num_lockrecs; i++) {
- if (tdb->lockrecs[i].off >= TDB_HASH_LOCK_START
- && tdb->lockrecs[i].off < (TDB_HASH_LOCK_START
- + TDB_HASH_LOCK_RANGE))
+ for (i=0; i<tdb->file->num_lockrecs; i++) {
+ if (tdb->file->lockrecs[i].off >= TDB_HASH_LOCK_START
+ && tdb->file->lockrecs[i].off < (TDB_HASH_LOCK_START
+ + TDB_HASH_LOCK_RANGE))
return true;
}
return false;
{
unsigned int i;
- for (i=0; i<tdb->num_lockrecs; i++) {
- if (tdb->lockrecs[i].off
+ if (tdb->flags & TDB_NOLOCK)
+ return false;
+
+ for (i=0; i<tdb->file->num_lockrecs; i++) {
+ if (tdb->file->lockrecs[i].off
> TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
return true;
}
+ (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
/* a allrecord lock allows us to avoid per chain locks */
- if (tdb->allrecord_lock.count &&
- (ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) {
+ if (tdb->file->allrecord_lock.count &&
+ (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
return TDB_SUCCESS;
}
- if (tdb->allrecord_lock.count) {
+ if (tdb->file->allrecord_lock.count) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_lock_hashes:"
" already have %s allrecordlock",
- tdb->allrecord_lock.ltype == F_RDLCK
+ tdb->file->allrecord_lock.ltype == F_RDLCK
? "read" : "write");
}
unsigned lock = TDB_HASH_LOCK_START
+ (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
+ if (tdb->flags & TDB_NOLOCK)
+ return 0;
+
/* a allrecord lock allows us to avoid per chain locks */
- if (tdb->allrecord_lock.count) {
- if (tdb->allrecord_lock.ltype == F_RDLCK
+ if (tdb->file->allrecord_lock.count) {
+ if (tdb->file->allrecord_lock.ltype == F_RDLCK
&& ltype == F_WRLCK) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_unlock_hashes RO allrecord!");
{
assert(b_off >= sizeof(struct tdb_header));
+ if (tdb->flags & TDB_NOLOCK)
+ return 0;
+
/* a allrecord lock allows us to avoid per chain locks */
- if (tdb->allrecord_lock.count) {
- if (tdb->allrecord_lock.ltype == F_WRLCK)
+ if (tdb->file->allrecord_lock.count) {
+ if (tdb->file->allrecord_lock.ltype == F_WRLCK)
return 0;
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_lock_free_bucket with"
void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
{
- if (tdb->allrecord_lock.count)
+ if (tdb->file->allrecord_lock.count)
return;
tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
}
-
-void tdb_lock_init(struct tdb_context *tdb)
-{
- tdb->num_lockrecs = 0;
- tdb->lockrecs = NULL;
- tdb->allrecord_lock.count = 0;
-}
#include "private.h"
-/* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
-static struct tdb_context *tdbs = NULL;
+/* all lock info, to detect double-opens (fcntl file don't nest!) */
+static struct tdb_file *files = NULL;
-static bool tdb_already_open(dev_t device, ino_t ino)
+static struct tdb_file *find_file(dev_t device, ino_t ino)
{
- struct tdb_context *i;
+ struct tdb_file *i;
- for (i = tdbs; i; i = i->next) {
+ for (i = files; i; i = i->next) {
if (i->device == device && i->inode == ino) {
- return true;
+ break;
}
}
-
- return false;
+ return i;
}
static bool read_all(int fd, void *buf, size_t len)
memcpy(tdb->map_ptr, &newdb, tdb->map_size);
return TDB_SUCCESS;
}
- if (lseek(tdb->fd, 0, SEEK_SET) == -1) {
+ if (lseek(tdb->file->fd, 0, SEEK_SET) == -1) {
return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_new_database:"
" failed to seek: %s", strerror(errno));
}
- if (ftruncate(tdb->fd, 0) == -1) {
+ if (ftruncate(tdb->file->fd, 0) == -1) {
return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_new_database:"
" failed to truncate: %s", strerror(errno));
}
- rlen = write(tdb->fd, &newdb, sizeof(newdb));
+ rlen = write(tdb->file->fd, &newdb, sizeof(newdb));
if (rlen != sizeof(newdb)) {
if (rlen >= 0)
errno = ENOSPC;
tdb->name = NULL;
tdb->map_ptr = NULL;
tdb->direct_access = 0;
- tdb->fd = -1;
tdb->map_size = sizeof(struct tdb_header);
tdb->flags = tdb_flags;
tdb->logfn = NULL;
tdb->transaction = NULL;
tdb->stats = NULL;
tdb->access = NULL;
+ tdb->file = NULL;
tdb_hash_init(tdb);
tdb_io_init(tdb);
- tdb_lock_init(tdb);
while (attr) {
switch (attr->base.attr) {
return tdb;
}
- if ((tdb->fd = open(name, open_flags, mode)) == -1) {
- /* errno set by open(2) */
- saved_errno = errno;
- ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
+ if (stat(name, &st) != -1)
+ tdb->file = find_file(st.st_dev, st.st_ino);
+
+ if (!tdb->file) {
+ int fd;
+
+ if ((fd = open(name, open_flags, mode)) == -1) {
+ /* errno set by open(2) */
+ saved_errno = errno;
+ tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: could not open file %s: %s",
name, strerror(errno));
- goto fail;
- }
+ goto fail;
+ }
+
+ /* on exec, don't inherit the fd */
+ v = fcntl(fd, F_GETFD, 0);
+ fcntl(fd, F_SETFD, v | FD_CLOEXEC);
+
+ if (fstat(fd, &st) == -1) {
+ saved_errno = errno;
+ tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
+ "tdb_open: could not stat open %s: %s",
+ name, strerror(errno));
+ goto fail;
+ }
+
+ tdb->file = malloc(sizeof(*tdb->file));
+ if (!tdb->file) {
+ saved_errno = ENOMEM;
+ tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
+ "tdb_open: could alloc file");
+ goto fail;
+ }
- /* on exec, don't inherit the fd */
- v = fcntl(tdb->fd, F_GETFD, 0);
- fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
+ tdb->file->next = files;
+ tdb->file->num_lockrecs = 0;
+ tdb->file->lockrecs = NULL;
+ tdb->file->allrecord_lock.count = 0;
+ tdb->file->fd = fd;
+ tdb->file->device = st.st_dev;
+ tdb->file->inode = st.st_ino;
+ } else {
+ /* FIXME */
+ ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
+ "tdb_open: %s (%d,%d) is already open in"
+ " this process",
+ name, (int)st.st_dev, (int)st.st_ino);
+ goto fail;
+ }
/* ensure there is only one process initialising at once */
ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
}
/* If they used O_TRUNC, read will return 0. */
- rlen = read(tdb->fd, &hdr, sizeof(hdr));
+ rlen = read(tdb->file->fd, &hdr, sizeof(hdr));
if (rlen == 0 && (open_flags & O_CREAT)) {
ecode = tdb_new_database(tdb, seed, &hdr);
if (ecode != TDB_SUCCESS) {
goto fail;
}
- if (fstat(tdb->fd, &st) == -1) {
- saved_errno = errno;
- ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb_open: could not stat open %s: %s",
- name, strerror(errno));
- goto fail;
- }
-
- /* Is it already in the open list? If so, fail. */
- if (tdb_already_open(st.st_dev, st.st_ino)) {
- /* FIXME */
- ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR,
- "tdb_open: %s (%d,%d) is already open"
- " in this process",
- name, (int)st.st_dev, (int)st.st_ino);
- goto fail;
- }
-
tdb->name = strdup(name);
if (!tdb->name) {
ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
goto fail;
}
- tdb->device = st.st_dev;
- tdb->inode = st.st_ino;
tdb_unlock_open(tdb);
/* This make sure we have current map_size and mmap. */
goto fail;
}
- tdb->next = tdbs;
- tdbs = tdb;
+ /* Add to linked list. */
+ files = tdb->file;
return tdb;
fail:
} else
tdb_munmap(tdb);
}
- free(tdb->lockrecs);
free((char *)tdb->name);
- if (tdb->fd != -1)
- if (close(tdb->fd) != 0)
+ if (tdb->file) {
+ if (close(tdb->file->fd) != 0)
tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
- "tdb_open: failed to close tdb->fd"
+ "tdb_open: failed to close tdb fd"
" on error: %s", strerror(errno));
+ free(tdb->file->lockrecs);
+ free(tdb->file);
+ }
+
free(tdb);
errno = saved_errno;
return NULL;
int tdb_close(struct tdb_context *tdb)
{
- struct tdb_context **i;
int ret = 0;
tdb_trace(tdb, "tdb_close");
tdb_munmap(tdb);
}
free((char *)tdb->name);
- if (tdb->fd != -1) {
- ret = close(tdb->fd);
- tdb->fd = -1;
- }
- free(tdb->lockrecs);
-
- /* Remove from contexts list */
- for (i = &tdbs; *i; i = &(*i)->next) {
- if (*i == tdb) {
- *i = tdb->next;
- break;
+ if (tdb->file) {
+ struct tdb_file **i;
+ ret = close(tdb->file->fd);
+
+ /* Remove from files list */
+ for (i = &files; *i; i = &(*i)->next) {
+ if (*i == tdb->file) {
+ *i = tdb->file->next;
+ break;
+ }
}
+ free(tdb->file->lockrecs);
+ free(tdb->file);
}
#ifdef TDB_TRACE
bool convert;
};
+struct tdb_file {
+ /* Single list of all TDBs, to detect multiple opens. */
+ struct tdb_file *next;
+
+ /* The file descriptor. */
+ int fd;
+
+ /* Lock information */
+ struct tdb_lock_type allrecord_lock;
+ size_t num_lockrecs;
+ struct tdb_lock_type *lockrecs;
+
+ /* Identity of this file. */
+ dev_t device;
+ ino_t inode;
+};
+
struct tdb_context {
/* Filename of the database. */
const char *name;
/* Are we accessing directly? (debugging check). */
int direct_access;
- /* Open file descriptor (undefined for TDB_INTERNAL). */
- int fd;
-
/* How much space has been mapped (<= current file size) */
tdb_len_t map_size;
/* IO methods: changes for transactions. */
const struct tdb_methods *methods;
- /* Lock information */
- struct tdb_lock_type allrecord_lock;
- size_t num_lockrecs;
- struct tdb_lock_type *lockrecs;
-
struct tdb_attribute_stats *stats;
/* Direct access information */
struct tdb_access_hdr *access;
- /* Single list of all TDBs, to avoid multiple opens. */
- struct tdb_context *next;
- dev_t device;
- ino_t inode;
+ /* The actual file information */
+ struct tdb_file *file;
};
struct tdb_methods {
} while (0)
/* lock.c: */
-void tdb_lock_init(struct tdb_context *tdb);
-
/* Lock/unlock a range of hashes. */
enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
tdb_off_t hash_lock, tdb_len_t hash_range,
#include <stdbool.h>
/* FIXME: Check these! */
-#define INITIAL_TDB_MALLOC "open.c", 184, FAILTEST_MALLOC
+#define INITIAL_TDB_MALLOC "open.c", 183, FAILTEST_MALLOC
#define URANDOM_OPEN "open.c", 43, FAILTEST_OPEN
#define URANDOM_READ "open.c", 23, FAILTEST_READ
/* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
ok1(free_record_length(tdb, layout->elem[1].base.off)
== 1024 + sizeof(struct tdb_used_record) + 2048);
ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0);
ok1(free_record_length(tdb, layout->elem[1].base.off)
== 1024 + sizeof(struct tdb_used_record) + 512);
ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1);
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(tdb->file->allrecord_lock.count == 0
+ && tdb->file->num_lockrecs == 0);
ok1(free_record_length(tdb, layout->elem[1].base.off)
== 1024 + sizeof(struct tdb_used_record) + 512
+ sizeof(struct tdb_used_record) + 256);
ok1(h.hlock_start == 0);
ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
- ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+ ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
ok1((tdb->flags & TDB_NOLOCK)
- || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+ || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
/* FIXME: Check lock length */
/* Allocate a new record. */
ok1(h.hlock_start == 0);
ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
- ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+ ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
ok1((tdb->flags & TDB_NOLOCK)
- || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+ || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
/* FIXME: Check lock length */
ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
ok1(h.hlock_start == 0);
ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
- ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+ ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
ok1((tdb->flags & TDB_NOLOCK)
- || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+ || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
/* FIXME: Check lock length */
/* Make it expand 0'th bucket. */
ok1(h.hlock_start == 0);
ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
- ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+ ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
ok1((tdb->flags & TDB_NOLOCK)
- || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+ || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
/* FIXME: Check lock length */
/* Simple delete should work. */
ok1(h.hlock_start == 0);
ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS)));
- ok1((tdb->flags & TDB_NOLOCK) || tdb->num_lockrecs == 1);
+ ok1((tdb->flags & TDB_NOLOCK) || tdb->file->num_lockrecs == 1);
ok1((tdb->flags & TDB_NOLOCK)
- || tdb->lockrecs[0].off == TDB_HASH_LOCK_START);
+ || tdb->file->lockrecs[0].off == TDB_HASH_LOCK_START);
/* FIXME: Check lock length */
ok1(expand_group(tdb, &h) == 0);
/* Check mixed bitpattern. */
test_val(tdb, 0x123456789ABCDEF0ULL);
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+ && tdb->file->num_lockrecs == 0));
tdb_close(tdb);
/* Deleting these entries in the db gave problems. */
moves++;
oldoff = newoff;
}
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+ && tdb->file->num_lockrecs == 0));
/* We should increase by 50% each time... */
ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
tdb_close(tdb);
moves++;
oldoff = newoff;
}
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+ && tdb->file->num_lockrecs == 0));
/* We should increase by 50% each time... */
ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
tdb_close(tdb);
ok1(data.dsize == MAX_SIZE);
ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
free(data.dptr);
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
+ && tdb->file->num_lockrecs == 0));
tdb_close(tdb);
}
/* Cancelling a transaction means no store */
tdb_transaction_cancel(tdb);
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(tdb->file->allrecord_lock.count == 0
+ && tdb->file->num_lockrecs == 0);
ok1(tdb_check(tdb, NULL, NULL) == 0);
ok1(tdb_fetch(tdb, key, &data) == TDB_ERR_NOEXIST);
ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
free(data.dptr);
ok1(tdb_transaction_commit(tdb) == 0);
- ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
+ ok1(tdb->file->allrecord_lock.count == 0
+ && tdb->file->num_lockrecs == 0);
ok1(tdb_check(tdb, NULL, NULL) == 0);
ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
ok1(data.dsize == 1000);
if (setjmp(jmpbuf) != 0) {
/* We're partway through. Simulate our death. */
- close(tdb->fd);
+ close(tdb->file->fd);
forget_locking();
in_transaction = false;
O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
ok1(external_agent_operation(agent, OPEN, filename) == SUCCESS);
- i = add_records_to_grow(agent, tdb->fd, tdb->map_size);
+ i = add_records_to_grow(agent, tdb->file->fd, tdb->map_size);
/* Do a traverse. */
ok1(tdb_traverse(tdb, NULL, NULL) == i);
return TDB_SUCCESS;
}
- if (fsync(tdb->fd) != 0) {
+ if (fsync(tdb->file->fd) != 0) {
return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_transaction: fsync failed: %s",
strerror(errno));
}
}
- if (tdb->allrecord_lock.count)
- tdb_allrecord_unlock(tdb, tdb->allrecord_lock.ltype);
+ if (tdb->file->allrecord_lock.count)
+ tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);
/* restore the normal io methods */
tdb->methods = tdb->transaction->io_methods;