#include "tdb_private.h"
static struct tdb_context *tdbs = NULL;
static unsigned int default_tdb_hash(TDB_DATA *key)
{
u32 value;
u32 i;
for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
value = (value + (key->dptr[i] << (i*5 % 24)));
return (1103515243 * value + 12345);
}
static int tdb_new_database(struct tdb_context *tdb, int hash_size)
{
struct tdb_header *newdb;
size_t size;
int ret = -1;
ssize_t written;
size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
if (!(newdb = (struct tdb_header *)calloc(size, 1)))
return TDB_ERRCODE(TDB_ERR_OOM, -1);
newdb->version = TDB_VERSION;
newdb->hash_size = hash_size;
if (tdb->flags & TDB_INTERNAL) {
tdb->map_size = size;
tdb->map_ptr = (char *)newdb;
memcpy(&tdb->header, newdb, sizeof(tdb->header));
CONVERT(*newdb);
return 0;
}
if (lseek(tdb->fd, 0, SEEK_SET) == -1)
goto fail;
if (ftruncate(tdb->fd, 0) == -1)
goto fail;
CONVERT(*newdb);
memcpy(&tdb->header, newdb, sizeof(tdb->header));
memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
written = write(tdb->fd, newdb, size);
if (written == size) {
ret = 0;
} else if (written != -1) {
size -= written;
written = write(tdb->fd, newdb+written, size);
if (written == size) {
ret = 0;
} else if (written >= 0) {
errno = ENOSPC;
}
}
fail:
SAFE_FREE(newdb);
return ret;
}
static int tdb_already_open(dev_t device,
ino_t ino)
{
struct tdb_context *i;
for (i = tdbs; i; i = i->next) {
if (i->device == device && i->inode == ino) {
return 1;
}
}
return 0;
}
struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
int open_flags, mode_t mode)
{
return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL, NULL);
}
static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...) PRINTF_ATTRIBUTE(3, 4);
static void null_log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
{
}
struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
int open_flags, mode_t mode,
const struct tdb_logging_context *log_ctx,
tdb_hash_func hash_fn)
{
struct tdb_context *tdb;
struct stat st;
int rev = 0, locked = 0;
unsigned char *vp;
u32 vertest;
if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
errno = ENOMEM;
goto fail;
}
tdb_io_init(tdb);
tdb->fd = -1;
tdb->name = NULL;
tdb->map_ptr = NULL;
tdb->flags = tdb_flags;
tdb->open_flags = open_flags;
if (log_ctx) {
tdb->log = *log_ctx;
} else {
tdb->log.log_fn = null_log_fn;
tdb->log.log_private = NULL;
}
tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash;
tdb->page_size = getpagesize();
if (tdb->page_size <= 0) {
tdb->page_size = 0x2000;
}
if ((open_flags & O_ACCMODE) == O_WRONLY) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: can't open tdb %s write-only\n",
name));
errno = EINVAL;
goto fail;
}
if (hash_size == 0)
hash_size = DEFAULT_HASH_SIZE;
if ((open_flags & O_ACCMODE) == O_RDONLY) {
tdb->read_only = 1;
tdb->flags |= TDB_NOLOCK;
tdb->flags &= ~TDB_CLEAR_IF_FIRST;
}
if (tdb->flags & TDB_INTERNAL) {
tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
tdb->flags &= ~TDB_CLEAR_IF_FIRST;
if (tdb_new_database(tdb, hash_size) != 0) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: tdb_new_database failed!"));
goto fail;
}
goto internal;
}
if ((tdb->fd = open(name, open_flags, mode)) == -1) {
TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_open_ex: could not open file %s: %s\n",
name, strerror(errno)));
goto fail;
}
if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0, 1) == -1) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to get global lock on %s: %s\n",
name, strerror(errno)));
goto fail;
}
if ((tdb_flags & TDB_CLEAR_IF_FIRST) &&
(locked = (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0, 1) == 0))) {
open_flags |= O_CREAT;
if (ftruncate(tdb->fd, 0) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
"failed to truncate %s: %s\n",
name, strerror(errno)));
goto fail;
}
}
errno = 0;
if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
|| strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0
|| (tdb->header.version != TDB_VERSION
&& !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) {
if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) {
if (errno == 0) {
errno = EIO;
}
goto fail;
}
rev = (tdb->flags & TDB_CONVERT);
}
vp = (unsigned char *)&tdb->header.version;
vertest = (((u32)vp[0]) << 24) | (((u32)vp[1]) << 16) |
(((u32)vp[2]) << 8) | (u32)vp[3];
tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0;
if (!rev)
tdb->flags &= ~TDB_CONVERT;
else {
tdb->flags |= TDB_CONVERT;
tdb_convert(&tdb->header, sizeof(tdb->header));
}
if (fstat(tdb->fd, &st) == -1)
goto fail;
if (tdb->header.rwlocks != 0) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: spinlocks no longer supported, clearing spinlock flag\n"));
tdb->header.rwlocks = 0;
}
if (tdb_already_open(st.st_dev, st.st_ino)) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
"%s (%d,%d) is already open in this process\n",
name, (int)st.st_dev, (int)st.st_ino));
errno = EBUSY;
goto fail;
}
if (!(tdb->name = (char *)strdup(name))) {
errno = ENOMEM;
goto fail;
}
tdb->map_size = st.st_size;
tdb->device = st.st_dev;
tdb->inode = st.st_ino;
tdb->max_dead_records = 0;
tdb_mmap(tdb);
if (locked) {
if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0, 1) == -1) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: "
"failed to take ACTIVE_LOCK on %s: %s\n",
name, strerror(errno)));
goto fail;
}
}
if (tdb_flags & TDB_CLEAR_IF_FIRST) {
if (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)
goto fail;
}
if (tdb_transaction_recover(tdb) == -1) {
goto fail;
}
internal:
if (tdb->methods->tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0, 1) == -1)
goto fail;
tdb->next = tdbs;
tdbs = tdb;
return tdb;
fail:
{ int save_errno = errno;
if (!tdb)
return NULL;
if (tdb->map_ptr) {
if (tdb->flags & TDB_INTERNAL)
SAFE_FREE(tdb->map_ptr);
else
tdb_munmap(tdb);
}
SAFE_FREE(tdb->name);
if (tdb->fd != -1)
if (close(tdb->fd) != 0)
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_open_ex: failed to close tdb->fd on error!\n"));
SAFE_FREE(tdb);
errno = save_errno;
return NULL;
}
}
void tdb_set_max_dead(struct tdb_context *tdb, int max_dead)
{
tdb->max_dead_records = max_dead;
}
int tdb_close(struct tdb_context *tdb)
{
struct tdb_context **i;
int ret = 0;
if (tdb->transaction) {
tdb_transaction_cancel(tdb);
}
if (tdb->map_ptr) {
if (tdb->flags & TDB_INTERNAL)
SAFE_FREE(tdb->map_ptr);
else
tdb_munmap(tdb);
}
SAFE_FREE(tdb->name);
if (tdb->fd != -1)
ret = close(tdb->fd);
SAFE_FREE(tdb->lockrecs);
for (i = &tdbs; *i; i = &(*i)->next) {
if (*i == tdb) {
*i = tdb->next;
break;
}
}
memset(tdb, 0, sizeof(*tdb));
SAFE_FREE(tdb);
return ret;
}
void tdb_set_logging_function(struct tdb_context *tdb,
const struct tdb_logging_context *log_ctx)
{
tdb->log = *log_ctx;
}
void *tdb_get_logging_private(struct tdb_context *tdb)
{
return tdb->log.log_private;
}
int tdb_reopen(struct tdb_context *tdb)
{
struct stat st;
if (tdb->flags & TDB_INTERNAL) {
return 0;
}
if (tdb->num_locks != 0 || tdb->global_lock.count) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
goto fail;
}
if (tdb->transaction != 0) {
TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed inside a transaction\n"));
goto fail;
}
if (tdb_munmap(tdb) != 0) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
goto fail;
}
if (close(tdb->fd) != 0)
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
if (tdb->fd == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: open failed (%s)\n", strerror(errno)));
goto fail;
}
if ((tdb->flags & TDB_CLEAR_IF_FIRST) &&
(tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: failed to obtain active lock\n"));
goto fail;
}
if (fstat(tdb->fd, &st) != 0) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
goto fail;
}
if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: file dev/inode has changed!\n"));
goto fail;
}
tdb_mmap(tdb);
return 0;
fail:
tdb_close(tdb);
return -1;
}
int tdb_reopen_all(int parent_longlived)
{
struct tdb_context *tdb;
for (tdb=tdbs; tdb; tdb = tdb->next) {
if (parent_longlived) {
tdb->flags &= ~TDB_CLEAR_IF_FIRST;
}
if (tdb_reopen(tdb) != 0)
return -1;
}
return 0;
}