Commit 1ad66fed authored by Rusty Russell's avatar Rusty Russell

tdb2: move file operations into separate structure

This moves the fd and locking information into a new 'struct tdb_file',
opening the way for it to be shared by multiple tdb_open calls on the
same file.
parent ba370093
......@@ -661,7 +661,7 @@ static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
/* 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");
}
......
......@@ -49,7 +49,7 @@ void tdb_mmap(struct tdb_context *tdb)
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 !!!!
......@@ -96,7 +96,7 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len,
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);
......@@ -245,7 +245,7 @@ static enum TDB_ERROR tdb_write(struct tdb_context *tdb, tdb_off_t off,
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)
......@@ -274,7 +274,7 @@ static enum TDB_ERROR tdb_read(struct tdb_context *tdb, tdb_off_t off,
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 "
......@@ -374,7 +374,7 @@ static enum TDB_ERROR fill(struct tdb_context *tdb,
{
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;
......@@ -418,7 +418,7 @@ static enum TDB_ERROR tdb_expand_file(struct tdb_context *tdb,
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
......
This diff is collapsed.
#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)
......@@ -143,19 +142,19 @@ static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
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;
......@@ -190,16 +189,15 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
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) {
......@@ -266,18 +264,56 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
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);
......@@ -286,7 +322,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
}
/* 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) {
......@@ -330,24 +366,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
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,
......@@ -365,8 +383,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
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. */
......@@ -390,8 +406,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
goto fail;
}
tdb->next = tdbs;
tdbs = tdb;
/* Add to linked list. */
files = tdb->file;
return tdb;
fail:
......@@ -426,13 +442,16 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
} 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;
......@@ -440,7 +459,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
int tdb_close(struct tdb_context *tdb)
{
struct tdb_context **i;
int ret = 0;
tdb_trace(tdb, "tdb_close");
......@@ -456,18 +474,19 @@ int tdb_close(struct tdb_context *tdb)
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
......
......@@ -315,6 +315,23 @@ struct tdb_access_hdr {
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;
......@@ -325,9 +342,6 @@ struct tdb_context {
/* 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;
......@@ -365,20 +379,13 @@ struct tdb_context {
/* 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 {
......@@ -521,8 +528,6 @@ void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val);
} 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,
......
......@@ -4,7 +4,7 @@
#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
......
......@@ -94,7 +94,7 @@ int main(int argc, char *argv[])
/* 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);
......@@ -117,7 +117,7 @@ int main(int argc, char *argv[])
/* 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);
......@@ -141,7 +141,8 @@ int main(int argc, char *argv[])
/* 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);
......
......@@ -60,9 +60,9 @@ int main(int argc, char *argv[])
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. */
......@@ -101,9 +101,9 @@ int main(int argc, char *argv[])
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,
......@@ -127,9 +127,9 @@ int main(int argc, char *argv[])
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. */
......@@ -165,9 +165,9 @@ int main(int argc, char *argv[])
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. */
......@@ -196,9 +196,9 @@ int main(int argc, char *argv[])
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);
......
......@@ -180,7 +180,8 @@ int main(int argc, char *argv[])
/* 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. */
......
......@@ -69,7 +69,8 @@ int main(int argc, char *argv[])
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);
......@@ -100,7 +101,8 @@ int main(int argc, char *argv[])
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);
......@@ -122,7 +124,8 @@ int main(int argc, char *argv[])
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);
}
......
......@@ -43,7 +43,8 @@ int main(int argc, char *argv[])
/* 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);
......@@ -57,7 +58,8 @@ int main(int argc, char *argv[])
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);
......
......@@ -153,7 +153,7 @@ reset:
if (setjmp(jmpbuf) != 0) {
/* We're partway through. Simulate our death. */
close(tdb->fd);
close(tdb->file->fd);
forget_locking();
in_transaction = false;
......
......@@ -51,7 +51,7 @@ int main(int argc, char *argv[])
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);
......
......@@ -430,7 +430,7 @@ static enum TDB_ERROR transaction_sync(struct tdb_context *tdb,
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));
......@@ -495,8 +495,8 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
}
}
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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment