Commit bbeb528e authored by Rusty Russell's avatar Rusty Russell

tdb2: merge tdb1_context into tdb_context.

Finally, we split out the tdb2-specific parts of tdb_context, and put
them into a "tdb2" sub-struct; the tdb1 parts go into a "tdb1"
sub-struct.  We get rido of tdb1_context and use tdb_context
everywhere.
parent 4dc29a33
......@@ -478,7 +478,7 @@ static enum TDB_ERROR check_free(struct tdb_context *tdb,
}
ecode = tdb->methods->oob(tdb, off
ecode = tdb->tdb2.io->oob(tdb, off
+ frec_len(frec)
+ sizeof(struct tdb_used_record),
false);
......@@ -587,7 +587,7 @@ tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off)
for (len = 0; off + len < tdb->file->map_size; len++) {
char c;
ecode = tdb->methods->tread(tdb, off, &c, 1);
ecode = tdb->tdb2.io->tread(tdb, off, &c, 1);
if (ecode != TDB_SUCCESS) {
return ecode;
}
......
......@@ -65,8 +65,8 @@ enum TDB_ERROR tdb_ftable_init(struct tdb_context *tdb)
unsigned int rnd, max = 0, count = 0;
tdb_off_t off;
tdb->ftable_off = off = first_ftable(tdb);
tdb->ftable = 0;
tdb->tdb2.ftable_off = off = first_ftable(tdb);
tdb->tdb2.ftable = 0;
while (off) {
if (TDB_OFF_IS_ERR(off)) {
......@@ -75,8 +75,8 @@ enum TDB_ERROR tdb_ftable_init(struct tdb_context *tdb)
rnd = random();
if (rnd >= max) {
tdb->ftable_off = off;
tdb->ftable = count;
tdb->tdb2.ftable_off = off;
tdb->tdb2.ftable = count;
max = rnd;
}
......@@ -218,7 +218,7 @@ static enum TDB_ERROR enqueue_in_free(struct tdb_context *tdb,
return head;
/* We only need to set ftable_and_len; rest is set in enqueue_in_free */
new.ftable_and_len = ((uint64_t)tdb->ftable << (64 - TDB_OFF_UPPER_STEAL))
new.ftable_and_len = ((uint64_t)tdb->tdb2.ftable << (64 - TDB_OFF_UPPER_STEAL))
| len;
/* new->next = head. */
......@@ -287,8 +287,8 @@ static tdb_off_t ftable_offset(struct tdb_context *tdb, unsigned int ftable)
tdb_off_t off;
unsigned int i;
if (likely(tdb->ftable == ftable))
return tdb->ftable_off;
if (likely(tdb->tdb2.ftable == ftable))
return tdb->tdb2.ftable_off;
off = first_ftable(tdb);
for (i = 0; i < ftable; i++) {
......@@ -595,7 +595,7 @@ enum TDB_ERROR add_free_record(struct tdb_context *tdb,
len = len_with_header - sizeof(struct tdb_used_record);
b_off = bucket_off(tdb->ftable_off, size_to_bucket(len));
b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(len));
ecode = tdb_lock_free_bucket(tdb, b_off, waitflag);
if (ecode != TDB_SUCCESS) {
return ecode;
......@@ -606,7 +606,7 @@ enum TDB_ERROR add_free_record(struct tdb_context *tdb,
/* Coalescing unlocks free list. */
if (!ecode && coalesce)
ecode = coalesce_list(tdb, tdb->ftable_off, b_off, 2);
ecode = coalesce_list(tdb, tdb->tdb2.ftable_off, b_off, 2);
else
tdb_unlock_free_bucket(tdb, b_off);
return ecode;
......@@ -752,7 +752,7 @@ static tdb_off_t lock_and_alloc(struct tdb_context *tdb,
/* For futureproofing, we put a 0 in any unused space. */
if (rec_extra_padding(&rec)) {
ecode = tdb->methods->twrite(tdb, best_off + sizeof(rec)
ecode = tdb->tdb2.io->twrite(tdb, best_off + sizeof(rec)
+ keylen + datalen, "", 1);
if (ecode != TDB_SUCCESS) {
goto unlock_err;
......@@ -800,9 +800,9 @@ static tdb_off_t get_free(struct tdb_context *tdb,
else
start_b = size_to_bucket(adjust_size(keylen, datalen));
ftable_off = tdb->ftable_off;
ftable = tdb->ftable;
while (!wrapped || ftable_off != tdb->ftable_off) {
ftable_off = tdb->tdb2.ftable_off;
ftable = tdb->tdb2.ftable;
while (!wrapped || ftable_off != tdb->tdb2.ftable_off) {
/* Start at exact size bucket, and search up... */
for (b = find_free_head(tdb, ftable_off, start_b);
b < TDB_FREE_BUCKETS;
......@@ -819,8 +819,8 @@ static tdb_off_t get_free(struct tdb_context *tdb,
if (b == TDB_FREE_BUCKETS - 1)
tdb->stats.alloc_bucket_max++;
/* Worked? Stay using this list. */
tdb->ftable_off = ftable_off;
tdb->ftable = ftable;
tdb->tdb2.ftable_off = ftable_off;
tdb->tdb2.ftable = ftable;
return off;
}
/* Didn't work. Try next bucket. */
......@@ -898,7 +898,7 @@ static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
/* Someone else may have expanded the file, so retry. */
old_size = tdb->file->map_size;
tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
tdb->tdb2.io->oob(tdb, tdb->file->map_size + 1, true);
if (tdb->file->map_size != old_size) {
tdb_unlock_expand(tdb, F_WRLCK);
return TDB_SUCCESS;
......@@ -930,7 +930,7 @@ static enum TDB_ERROR tdb_expand(struct tdb_context *tdb, tdb_len_t size)
/* We need room for the record header too. */
wanted = adjust_size(0, sizeof(struct tdb_used_record) + wanted);
ecode = tdb->methods->expand_file(tdb, wanted);
ecode = tdb->tdb2.io->expand_file(tdb, wanted);
if (ecode != TDB_SUCCESS) {
tdb_unlock_expand(tdb, F_WRLCK);
return ecode;
......@@ -950,7 +950,7 @@ tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
tdb_off_t off;
/* We can't hold pointers during this: we could unmap! */
assert(!tdb->direct_access);
assert(!tdb->tdb2.direct_access);
for (;;) {
enum TDB_ERROR ecode;
......
......@@ -88,7 +88,7 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len,
enum TDB_ERROR ecode;
/* We can't hold pointers during this: we could unmap! */
assert(!tdb->direct_access
assert(!tdb->tdb2.direct_access
|| (tdb->flags & TDB_NOLOCK)
|| tdb_has_expansion_lock(tdb));
......@@ -197,7 +197,7 @@ uint64_t tdb_find_zero_off(struct tdb_context *tdb, tdb_off_t off,
enum TDB_ERROR zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len)
{
char buf[8192] = { 0 };
void *p = tdb->methods->direct(tdb, off, len, true);
void *p = tdb->tdb2.io->direct(tdb, off, len, true);
enum TDB_ERROR ecode = TDB_SUCCESS;
assert(!(tdb->flags & TDB_RDONLY));
......@@ -210,7 +210,7 @@ enum TDB_ERROR zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len)
}
while (len) {
unsigned todo = len < sizeof(buf) ? len : sizeof(buf);
ecode = tdb->methods->twrite(tdb, off, buf, todo);
ecode = tdb->tdb2.io->twrite(tdb, off, buf, todo);
if (ecode != TDB_SUCCESS) {
break;
}
......@@ -226,7 +226,7 @@ tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off)
enum TDB_ERROR ecode;
if (likely(!(tdb->flags & TDB_CONVERT))) {
tdb_off_t *p = tdb->methods->direct(tdb, off, sizeof(*p),
tdb_off_t *p = tdb->tdb2.io->direct(tdb, off, sizeof(*p),
false);
if (TDB_PTR_IS_ERR(p)) {
return TDB_PTR_ERR(p);
......@@ -253,7 +253,7 @@ static enum TDB_ERROR tdb_write(struct tdb_context *tdb, tdb_off_t off,
"Write to read-only database");
}
ecode = tdb->methods->oob(tdb, off + len, false);
ecode = tdb->tdb2.io->oob(tdb, off + len, false);
if (ecode != TDB_SUCCESS) {
return ecode;
}
......@@ -283,7 +283,7 @@ static enum TDB_ERROR tdb_read(struct tdb_context *tdb, tdb_off_t off,
{
enum TDB_ERROR ecode;
ecode = tdb->methods->oob(tdb, off + len, false);
ecode = tdb->tdb2.io->oob(tdb, off + len, false);
if (ecode != TDB_SUCCESS) {
return ecode;
}
......@@ -317,11 +317,11 @@ enum TDB_ERROR tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
" %zu bytes", len);
}
memcpy(conv, rec, len);
ecode = tdb->methods->twrite(tdb, off,
ecode = tdb->tdb2.io->twrite(tdb, off,
tdb_convert(tdb, conv, len), len);
free(conv);
} else {
ecode = tdb->methods->twrite(tdb, off, rec, len);
ecode = tdb->tdb2.io->twrite(tdb, off, rec, len);
}
return ecode;
}
......@@ -329,7 +329,7 @@ enum TDB_ERROR tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
enum TDB_ERROR tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
void *rec, size_t len)
{
enum TDB_ERROR ecode = tdb->methods->tread(tdb, off, rec, len);
enum TDB_ERROR ecode = tdb->tdb2.io->tread(tdb, off, rec, len);
tdb_convert(tdb, rec, len);
return ecode;
}
......@@ -343,7 +343,7 @@ enum TDB_ERROR tdb_write_off(struct tdb_context *tdb,
}
if (likely(!(tdb->flags & TDB_CONVERT))) {
tdb_off_t *p = tdb->methods->direct(tdb, off, sizeof(*p),
tdb_off_t *p = tdb->tdb2.io->direct(tdb, off, sizeof(*p),
true);
if (TDB_PTR_IS_ERR(p)) {
return TDB_PTR_ERR(p);
......@@ -370,7 +370,7 @@ static void *_tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset,
(size_t)(prefix + len));
return TDB_ERR_PTR(TDB_ERR_OOM);
} else {
ecode = tdb->methods->tread(tdb, offset, buf+prefix, len);
ecode = tdb->tdb2.io->tread(tdb, offset, buf+prefix, len);
if (unlikely(ecode != TDB_SUCCESS)) {
free(buf);
return TDB_ERR_PTR(ecode);
......@@ -459,7 +459,7 @@ const void *tdb_access_read(struct tdb_context *tdb,
void *ret = NULL;
if (likely(!(tdb->flags & TDB_CONVERT))) {
ret = tdb->methods->direct(tdb, off, len, false);
ret = tdb->tdb2.io->direct(tdb, off, len, false);
if (TDB_PTR_IS_ERR(ret)) {
return ret;
......@@ -471,14 +471,14 @@ const void *tdb_access_read(struct tdb_context *tdb,
if (TDB_PTR_IS_ERR(hdr)) {
return hdr;
}
hdr->next = tdb->access;
tdb->access = hdr;
hdr->next = tdb->tdb2.access;
tdb->tdb2.access = hdr;
ret = hdr + 1;
if (convert) {
tdb_convert(tdb, (void *)ret, len);
}
} else
tdb->direct_access++;
tdb->tdb2.direct_access++;
return ret;
}
......@@ -495,7 +495,7 @@ void *tdb_access_write(struct tdb_context *tdb,
}
if (likely(!(tdb->flags & TDB_CONVERT))) {
ret = tdb->methods->direct(tdb, off, len, true);
ret = tdb->tdb2.io->direct(tdb, off, len, true);
if (TDB_PTR_IS_ERR(ret)) {
return ret;
......@@ -508,8 +508,8 @@ void *tdb_access_write(struct tdb_context *tdb,
if (TDB_PTR_IS_ERR(hdr)) {
return hdr;
}
hdr->next = tdb->access;
tdb->access = hdr;
hdr->next = tdb->tdb2.access;
tdb->tdb2.access = hdr;
hdr->off = off;
hdr->len = len;
hdr->convert = convert;
......@@ -517,7 +517,7 @@ void *tdb_access_write(struct tdb_context *tdb,
if (convert)
tdb_convert(tdb, (void *)ret, len);
} else
tdb->direct_access++;
tdb->tdb2.direct_access++;
return ret;
}
......@@ -526,7 +526,7 @@ static struct tdb_access_hdr **find_hdr(struct tdb_context *tdb, const void *p)
{
struct tdb_access_hdr **hp;
for (hp = &tdb->access; *hp; hp = &(*hp)->next) {
for (hp = &tdb->tdb2.access; *hp; hp = &(*hp)->next) {
if (*hp + 1 == p)
return hp;
}
......@@ -542,7 +542,7 @@ void tdb_access_release(struct tdb_context *tdb, const void *p)
*hp = hdr->next;
free(hdr);
} else
tdb->direct_access--;
tdb->tdb2.direct_access--;
}
enum TDB_ERROR tdb_access_commit(struct tdb_context *tdb, void *p)
......@@ -559,7 +559,7 @@ enum TDB_ERROR tdb_access_commit(struct tdb_context *tdb, void *p)
*hp = hdr->next;
free(hdr);
} else {
tdb->direct_access--;
tdb->tdb2.direct_access--;
ecode = TDB_SUCCESS;
}
......@@ -587,7 +587,7 @@ void tdb_inc_seqnum(struct tdb_context *tdb)
if (likely(!(tdb->flags & TDB_CONVERT))) {
int64_t *direct;
direct = tdb->methods->direct(tdb,
direct = tdb->tdb2.io->direct(tdb,
offsetof(struct tdb_header,
seqnum),
sizeof(*direct), true);
......@@ -622,5 +622,5 @@ static const struct tdb_methods io_methods = {
*/
void tdb_io_init(struct tdb_context *tdb)
{
tdb->methods = &io_methods;
tdb->tdb2.io = &io_methods;
}
......@@ -363,11 +363,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
} else {
tdb->name = NULL;
}
tdb->direct_access = 0;
tdb->flags = tdb_flags;
tdb->log_fn = NULL;
tdb->transaction = NULL;
tdb->access = NULL;
tdb->open_flags = open_flags;
tdb->last_error = TDB_SUCCESS;
tdb->file = NULL;
......@@ -379,6 +376,9 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->stats.base.attr = TDB_ATTRIBUTE_STATS;
tdb->stats.size = sizeof(tdb->stats);
tdb_io_init(tdb);
tdb->tdb2.direct_access = 0;
tdb->tdb2.transaction = NULL;
tdb->tdb2.access = NULL;
while (attr) {
switch (attr->base.attr) {
......@@ -573,7 +573,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb_unlock_open(tdb, openlock);
/* This make sure we have current map_size and mmap. */
ecode = tdb->methods->oob(tdb, tdb->file->map_size + 1, true);
ecode = tdb->tdb2.io->oob(tdb, tdb->file->map_size + 1, true);
if (unlikely(ecode != TDB_SUCCESS))
goto fail;
......@@ -655,7 +655,7 @@ int tdb_close(struct tdb_context *tdb)
tdb_trace(tdb, "tdb_close");
if (tdb->transaction) {
if (tdb->tdb2.transaction) {
tdb_transaction_cancel(tdb);
}
......
......@@ -271,6 +271,9 @@ struct traverse_info {
tdb_off_t prev;
};
typedef uint32_t tdb1_len_t;
typedef uint32_t tdb1_off_t;
enum tdb_lock_flags {
/* WAIT == F_SETLKW, NOWAIT == F_SETLK */
TDB_LOCK_NOWAIT = 0,
......@@ -321,68 +324,6 @@ struct tdb_file {
ino_t inode;
};
struct tdb_context {
/* Single list of all TDBs, to detect multiple opens. */
struct tdb_context *next;
/* Filename of the database. */
const char *name;
/* Logging function */
void (*log_fn)(struct tdb_context *tdb,
enum tdb_log_level level,
enum TDB_ERROR ecode,
const char *message,
void *data);
void *log_data;
/* Last error we returned. */
enum TDB_ERROR last_error;
/* The actual file information */
struct tdb_file *file;
/* Open flags passed to tdb_open. */
int open_flags;
/* low level (fnctl) lock functions. */
int (*lock_fn)(int fd, int rw, off_t off, off_t len, bool w, void *);
int (*unlock_fn)(int fd, int rw, off_t off, off_t len, void *);
void *lock_data;
/* the flags passed to tdb_open. */
uint32_t flags;
/* Our statistics. */
struct tdb_attribute_stats stats;
/* Hash function. */
uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed, void *);
void *hash_data;
uint64_t hash_seed;
/* Are we accessing directly? (debugging check). */
int direct_access;
/* Set if we are in a transaction. */
struct tdb_transaction *transaction;
/* What free table are we using? */
tdb_off_t ftable_off;
unsigned int ftable;
/* Our open hook, if any. */
enum TDB_ERROR (*openhook)(int fd, void *data);
void *openhook_data;
/* IO methods: changes for transactions. */
const struct tdb_methods *methods;
/* Direct access information */
struct tdb_access_hdr *access;
};
struct tdb_methods {
enum TDB_ERROR (*tread)(struct tdb_context *, tdb_off_t, void *,
tdb_len_t);
......@@ -588,6 +529,102 @@ int tdb_fcntl_unlock(int fd, int rw, off_t off, off_t len, void *);
enum TDB_ERROR tdb_transaction_recover(struct tdb_context *tdb);
tdb_bool_err tdb_needs_recovery(struct tdb_context *tdb);
/* this is stored at the front of every database */
struct tdb1_header {
char magic_food[32]; /* for /etc/magic */
uint32_t version; /* version of the code */
uint32_t hash_size; /* number of hash entries */
tdb1_off_t rwlocks; /* obsolete - kept to detect old formats */
tdb1_off_t recovery_start; /* offset of transaction recovery region */
tdb1_off_t sequence_number; /* used when TDB1_SEQNUM is set */
uint32_t magic1_hash; /* hash of TDB_MAGIC_FOOD. */
uint32_t magic2_hash; /* hash of TDB1_MAGIC. */
tdb1_off_t reserved[27];
};
struct tdb1_traverse_lock {
struct tdb1_traverse_lock *next;
uint32_t off;
uint32_t hash;
int lock_rw;
};
struct tdb_context {
/* Single list of all TDBs, to detect multiple opens. */
struct tdb_context *next;
/* Filename of the database. */
const char *name;
/* Logging function */
void (*log_fn)(struct tdb_context *tdb,
enum tdb_log_level level,
enum TDB_ERROR ecode,
const char *message,
void *data);
void *log_data;
/* Open flags passed to tdb_open. */
int open_flags;
/* low level (fnctl) lock functions. */
int (*lock_fn)(int fd, int rw, off_t off, off_t len, bool w, void *);
int (*unlock_fn)(int fd, int rw, off_t off, off_t len, void *);
void *lock_data;
/* the tdb flags passed to tdb_open. */
uint32_t flags;
/* Our statistics. */
struct tdb_attribute_stats stats;
/* The actual file information */
struct tdb_file *file;
/* Hash function. */
uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed, void *);
void *hash_data;
uint64_t hash_seed;
/* Our open hook, if any. */
enum TDB_ERROR (*openhook)(int fd, void *data);
void *openhook_data;
/* Last error we returned. */
enum TDB_ERROR last_error;
struct {
/* Are we accessing directly? (debugging check). */
int direct_access;
/* Set if we are in a transaction. */
struct tdb_transaction *transaction;
/* What free table are we using? */
tdb_off_t ftable_off;
unsigned int ftable;
/* IO methods: changes for transactions. */
const struct tdb_methods *io;
/* Direct access information */
struct tdb_access_hdr *access;
} tdb2;
struct {
int traverse_read; /* read-only traversal */
int traverse_write; /* read-write traversal */
struct tdb1_header header; /* a cached copy of the header */
struct tdb1_traverse_lock travlocks; /* current traversal locks */
const struct tdb1_methods *io;
struct tdb1_transaction *transaction;
int page_size;
int max_dead_records;
} tdb1;
};
/* tdb.c: */
enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR ecode,
......
......@@ -70,13 +70,13 @@ static enum TDB_ERROR replace_data(struct tdb_context *tdb,
}
new_off += sizeof(struct tdb_used_record);
ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
ecode = tdb->tdb2.io->twrite(tdb, new_off, key.dptr, key.dsize);
if (ecode != TDB_SUCCESS) {
return ecode;
}
new_off += key.dsize;
ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
ecode = tdb->tdb2.io->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
if (ecode != TDB_SUCCESS) {
return ecode;
}
......@@ -94,10 +94,10 @@ static enum TDB_ERROR update_data(struct tdb_context *tdb,
{
enum TDB_ERROR ecode;
ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
ecode = tdb->tdb2.io->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
if (ecode == TDB_SUCCESS && extra) {
/* Put a zero in; future versions may append other data. */
ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1);
ecode = tdb->tdb2.io->twrite(tdb, off + dbuf.dsize, "", 1);
}
if (tdb->flags & TDB_SEQNUM)
tdb_inc_seqnum(tdb);
......@@ -211,7 +211,7 @@ enum TDB_ERROR tdb_append(struct tdb_context *tdb,
+ dbuf.dsize));
goto out;
}
ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
ecode = tdb->tdb2.io->tread(tdb, off + sizeof(rec) + key.dsize,
newdata, old_dlen);
if (ecode != TDB_SUCCESS) {
goto out_free_newdata;
......@@ -324,9 +324,17 @@ unsigned int tdb_get_flags(struct tdb_context *tdb)
return tdb->flags;
}
static bool inside_transaction(const struct tdb_context *tdb)
{
if (tdb->flags & TDB_VERSION1)
return tdb->tdb1.transaction != NULL;
else
return tdb->tdb2.transaction != NULL;
}
static bool readonly_changable(struct tdb_context *tdb, const char *caller)
{
if (tdb->transaction) {
if (inside_transaction(tdb)) {
tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
TDB_LOG_USE_ERROR,
"%s: can't change"
......
......@@ -36,11 +36,8 @@
#endif
/** This is the context structure that is returned from a db open. */
typedef struct tdb1_context TDB1_CONTEXT;
typedef int (*tdb1_traverse_func)(struct tdb1_context *, TDB_DATA, TDB_DATA, void *);
typedef void (*tdb1_log_func)(struct tdb1_context *, enum tdb_log_level, enum TDB_ERROR,
typedef int (*tdb1_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
typedef void (*tdb1_log_func)(struct tdb_context *, enum tdb_log_level, enum TDB_ERROR,
const char *, void *);
typedef uint64_t (*tdb1_hash_func)(const void *key, size_t len, uint64_t seed,
void *data);
......@@ -50,84 +47,84 @@ struct tdb1_logging_context {
void *log_private;
};
struct tdb1_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
struct tdb_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
int open_flags, mode_t mode);
struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
struct tdb_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
int open_flags, mode_t mode,
const struct tdb1_logging_context *log_ctx,
tdb1_hash_func hash_fn);
void tdb1_set_max_dead(struct tdb1_context *tdb, int max_dead);
void tdb1_set_max_dead(struct tdb_context *tdb, int max_dead);
TDB_DATA tdb1_fetch(struct tdb1_context *tdb, TDB_DATA key);
TDB_DATA tdb1_fetch(struct tdb_context *tdb, TDB_DATA key);
int tdb1_parse_record(struct tdb1_context *tdb, TDB_DATA key,
int tdb1_parse_record(struct tdb_context *tdb, TDB_DATA key,
int (*parser)(TDB_DATA key, TDB_DATA data,
void *private_data),
void *private_data);
int tdb1_delete(struct tdb1_context *tdb, TDB_DATA key);
int tdb1_delete(struct tdb_context *tdb, TDB_DATA key);
int tdb1_store(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
int tdb1_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
int tdb1_append(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
int tdb1_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
int tdb1_close(struct tdb1_context *tdb);
int tdb1_close(struct tdb_context *tdb);
TDB_DATA tdb1_firstkey(struct tdb1_context *tdb);
TDB_DATA tdb1_firstkey(struct tdb_context *tdb);
TDB_DATA tdb1_nextkey(struct tdb1_context *tdb, TDB_DATA key);
TDB_DATA tdb1_nextkey(struct tdb_context *tdb, TDB_DATA key);
int tdb1_traverse(struct tdb1_context *tdb, tdb1_traverse_func fn, void *private_data);
int tdb1_traverse(struct tdb_context *tdb, tdb1_traverse_func fn, void *private_data);
int tdb1_traverse_read(struct tdb1_context *tdb, tdb1_traverse_func fn, void *private_data);
int tdb1_traverse_read(struct tdb_context *tdb, tdb1_traverse_func fn, void *private_data);
int tdb1_exists(struct tdb1_context *tdb, TDB_DATA key);
int tdb1_exists(struct tdb_context *tdb, TDB_DATA key);
int tdb1_lockall(struct tdb1_context *tdb);
int tdb1_lockall(struct tdb_context *tdb);
int tdb1_unlockall(struct tdb1_context *tdb);
int tdb1_unlockall(struct tdb_context *tdb);
int tdb1_lockall_read(struct tdb1_context *tdb);
int tdb1_lockall_read(struct tdb_context *tdb);
int tdb1_unlockall_read(struct tdb1_context *tdb);
int tdb1_unlockall_read(struct tdb_context *tdb);
int tdb1_transaction_start(struct tdb1_context *tdb);
int tdb1_transaction_start(struct tdb_context *tdb);
int tdb1_transaction_prepare_commit(struct tdb1_context *tdb);
int tdb1_transaction_prepare_commit(struct tdb_context *tdb);
int tdb1_transaction_commit(struct tdb1_context *tdb);
int tdb1_transaction_commit(struct tdb_context *tdb);
int tdb1_transaction_cancel(struct tdb1_context *tdb);
int tdb1_transaction_cancel(struct tdb_context *tdb);
int tdb1_get_seqnum(struct tdb1_context *tdb);
int tdb1_get_seqnum(struct tdb_context *tdb);
int tdb1_hash_size(struct tdb1_context *tdb);
int tdb1_hash_size(struct tdb_context *tdb);
void tdb1_increment_seqnum_nonblock(struct tdb1_context *tdb);
void tdb1_increment_seqnum_nonblock(struct tdb_context *tdb);
uint64_t tdb1_incompatible_hash(const void *key, size_t len, uint64_t seed, void *);
int tdb1_check(struct tdb1_context *tdb,
int tdb1_check(struct tdb_context *tdb,
int (*check) (TDB_DATA key, TDB_DATA data, void *private_data),
void *private_data);
/* @} ******************************************************************/
/* Low level locking functions: use with care */
int tdb1_chainlock(struct tdb1_context *tdb, TDB_DATA key);
int tdb1_chainunlock(struct tdb1_context *tdb, TDB_DATA key);
int tdb1_chainlock_read(struct tdb1_context *tdb, TDB_DATA key);
int tdb1_chainunlock_read(struct tdb1_context *tdb, TDB_DATA key);
int tdb1_chainlock(struct tdb_context *tdb, TDB_DATA key);
int tdb1_chainunlock(struct tdb_context *tdb, TDB_DATA key);
int tdb1_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
int tdb1_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
/* wipe and repack */
int tdb1_wipe_all(struct tdb1_context *tdb);
int tdb1_repack(struct tdb1_context *tdb);
int tdb1_wipe_all(struct tdb_context *tdb);
int tdb1_repack(struct tdb_context *tdb);
/* Debug functions. Not used in production. */
char *tdb1_summary(struct tdb1_context *tdb);
char *tdb1_summary(struct tdb_context *tdb);
extern TDB_DATA tdb1_null;
......
......@@ -25,12 +25,12 @@
#include "tdb1_private.h"
/* Since we opened it, these shouldn't fail unless it's recent corruption. */
static bool tdb1_check_header(struct tdb1_context *tdb, tdb1_off_t *recovery)
static bool tdb1_check_header(struct tdb_context *tdb, tdb1_off_t *recovery)
{
struct tdb1_header hdr;
uint32_t h1, h2;
if (tdb->methods->tdb1_read(tdb, 0, &hdr, sizeof(hdr), 0) == -1)
if (tdb->tdb1.io->tdb1_read(tdb, 0, &hdr, sizeof(hdr), 0) == -1)
return false;
if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0)
goto corrupt;
......@@ -50,11 +50,11 @@ static bool tdb1_check_header(struct tdb1_context *tdb, tdb1_off_t *recovery)
if (hdr.hash_size == 0)
goto corrupt;
if (hdr.hash_size != tdb->header.hash_size)
if (hdr.hash_size != tdb->tdb1.header.hash_size)
goto corrupt;
if (hdr.recovery_start != 0 &&
hdr.recovery_start < TDB1_DATA_START(tdb->header.hash_size))
hdr.recovery_start < TDB1_DATA_START(tdb->tdb1.header.hash_size))
goto corrupt;
*recovery = hdr.recovery_start;
......@@ -67,14 +67,14 @@ corrupt:
}
/* Generic record header check. */
static bool tdb1_check_record(struct tdb1_context *tdb,
static bool tdb1_check_record(struct tdb_context *tdb,
tdb1_off_t off,
const struct tdb1_record *rec)
{
tdb1_off_t tailer;
/* Check rec->next: 0 or points to record offset, aligned. */
if (rec->next > 0 && rec->next < TDB1_DATA_START(tdb->header.hash_size)){
if (rec->next > 0 && rec->next < TDB1_DATA_START(tdb->tdb1.header.hash_size)){
tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"Record offset %d too small next %d\n",
off, rec->next);
......@@ -92,7 +92,7 @@ static bool tdb1_check_record(struct tdb1_context *tdb,
off, rec->next);
goto corrupt;
}
if (tdb->methods->tdb1_oob(tdb, rec->next+sizeof(*rec), 0))
if (tdb->tdb1.io->tdb1_oob(tdb, rec->next+sizeof(*rec), 0))
goto corrupt;
/* Check rec_len: similar to rec->next, implies next record. */
......@@ -110,7 +110,7 @@ static bool tdb1_check_record(struct tdb1_context *tdb,
goto corrupt;
}
/* OOB allows "right at the end" access, so this works for last rec. */
if (tdb->methods->tdb1_oob(tdb, off+sizeof(*rec)+rec->rec_len, 0))
if (tdb->tdb1.io->tdb1_oob(tdb, off+sizeof(*rec)+rec->rec_len, 0))
goto corrupt;
/* Check tailer. */
......@@ -132,14 +132,14 @@ corrupt:
/* Grab some bytes: may copy if can't use mmap.
Caller has already done bounds check. */
static TDB_DATA get_bytes(struct tdb1_context *tdb,
static TDB_DATA get_bytes(struct tdb_context *tdb,
tdb1_off_t off, tdb1_len_t len)
{
TDB_DATA d;
d.dsize = len;
if (tdb->transaction == NULL && tdb->file->map_ptr != NULL)
if (tdb->tdb1.transaction == NULL && tdb->file->map_ptr != NULL)
d.dptr = (unsigned char *)tdb->file->map_ptr + off;
else
d.dptr = tdb1_alloc_read(tdb, off, d.dsize);
......@@ -147,9 +147,9 @@ static TDB_DATA get_bytes(struct tdb1_context *tdb,
}
/* Frees data if we're not able to simply use mmap. */
static void put_bytes(struct tdb1_context *tdb, TDB_DATA d)
static void put_bytes(struct tdb_context *tdb, TDB_DATA d)
{
if (tdb->transaction == NULL && tdb->file->map_ptr != NULL)
if (tdb->tdb1.transaction == NULL && tdb->file->map_ptr != NULL)
return;
free(d.dptr);
}
......@@ -232,7 +232,7 @@ static void record_offset(unsigned char bits[], tdb1_off_t off)
}
/* Check that an in-use record is valid. */
static bool tdb1_check_used_record(struct tdb1_context *tdb,
static bool tdb1_check_used_record(struct tdb_context *tdb,
tdb1_off_t off,
const struct tdb1_record *rec,
unsigned char **hashes,
......@@ -291,7 +291,7 @@ fail_put_key:
}
/* Check that an unused record is valid. */
static bool tdb1_check_free_record(struct tdb1_context *tdb,
static bool tdb1_check_free_record(struct tdb_context *tdb,
tdb1_off_t off,
const struct tdb1_record *rec,
unsigned char **hashes)
......@@ -308,13 +308,13 @@ static bool tdb1_check_free_record(struct tdb1_context *tdb,
}
/* Slow, but should be very rare. */
size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off)
size_t tdb1_dead_space(struct tdb_context *tdb, tdb1_off_t off)
{
size_t len;
for (len = 0; off + len < tdb->file->map_size; len++) {
char c;
if (tdb->methods->tdb1_read(tdb, off, &c, 1, 0))
if (tdb->tdb1.io->tdb1_read(tdb, off, &c, 1, 0))
return 0;
if (c != 0 && c != 0x42)
break;
......@@ -322,7 +322,7 @@ size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off)
return len;
}
int tdb1_check(struct tdb1_context *tdb,
int tdb1_check(struct tdb_context *tdb,
int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
void *private_data)
{
......@@ -344,14 +344,14 @@ int tdb1_check(struct tdb1_context *tdb,
}
/* Make sure we know true size of the underlying file. */
tdb->methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
tdb->tdb1.io->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
/* Header must be OK: also gets us the recovery ptr, if any. */
if (!tdb1_check_header(tdb, &recovery_start))
goto unlock;
/* We should have the whole header, too. */
if (tdb->file->map_size < TDB1_DATA_START(tdb->header.hash_size)) {
if (tdb->file->map_size < TDB1_DATA_START(tdb->tdb1.header.hash_size)) {
tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"File too short for hashes\n");
goto unlock;
......@@ -359,20 +359,20 @@ int tdb1_check(struct tdb1_context *tdb,
/* One big malloc: pointers then bit arrays. */
hashes = (unsigned char **)calloc(
1, sizeof(hashes[0]) * (1+tdb->header.hash_size)
+ BITMAP_BITS / CHAR_BIT * (1+tdb->header.hash_size));
1, sizeof(hashes[0]) * (1+tdb->tdb1.header.hash_size)
+ BITMAP_BITS / CHAR_BIT * (1+tdb->tdb1.header.hash_size));
if (!hashes) {
tdb->last_error = TDB_ERR_OOM;
goto unlock;
}
/* Initialize pointers */
hashes[0] = (unsigned char *)(&hashes[1+tdb->header.hash_size]);
for (h = 1; h < 1+tdb->header.hash_size; h++)
hashes[0] = (unsigned char *)(&hashes[1+tdb->tdb1.header.hash_size]);
for (h = 1; h < 1+tdb->tdb1.header.hash_size; h++)
hashes[h] = hashes[h-1] + BITMAP_BITS / CHAR_BIT;
/* Freelist and hash headers are all in a row: read them. */
for (h = 0; h < 1+tdb->header.hash_size; h++) {
for (h = 0; h < 1+tdb->tdb1.header.hash_size; h++) {
if (tdb1_ofs_read(tdb, TDB1_FREELIST_TOP + h*sizeof(tdb1_off_t),
&off) == -1)
goto free;
......@@ -381,10 +381,10 @@ int tdb1_check(struct tdb1_context *tdb,
}
/* For each record, read it in and check it's ok. */
for (off = TDB1_DATA_START(tdb->header.hash_size);
for (off = TDB1_DATA_START(tdb->tdb1.header.hash_size);
off < tdb->file->map_size;
off += sizeof(rec) + rec.rec_len) {
if (tdb->methods->tdb1_read(tdb, off, &rec, sizeof(rec),
if (tdb->tdb1.io->tdb1_read(tdb, off, &rec, sizeof(rec),
TDB1_DOCONV()) == -1)
goto free;
switch (rec.magic) {
......@@ -434,7 +434,7 @@ int tdb1_check(struct tdb1_context *tdb,
/* Now, hashes should all be empty: each record exists and is referred
* to by one other. */
for (h = 0; h < 1+tdb->header.hash_size; h++) {
for (h = 0; h < 1+tdb->tdb1.header.hash_size; h++) {
unsigned int i;
for (i = 0; i < BITMAP_BITS / CHAR_BIT; i++) {
if (hashes[h][i] != 0) {
......
......@@ -28,9 +28,9 @@
#include "tdb1_private.h"
/* read a freelist record and check for simple errors */
int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off, struct tdb1_record *rec)
int tdb1_rec_free_read(struct tdb_context *tdb, tdb1_off_t off, struct tdb1_record *rec)
{
if (tdb->methods->tdb1_read(tdb, off, rec, sizeof(*rec),TDB1_DOCONV()) == -1)
if (tdb->tdb1.io->tdb1_read(tdb, off, rec, sizeof(*rec),TDB1_DOCONV()) == -1)
return -1;
if (rec->magic == TDB1_MAGIC) {
......@@ -40,7 +40,7 @@ int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off, struct tdb1_rec
"tdb1_rec_free_read non-free magic 0x%x at offset=%d - fixing\n",
rec->magic, off);
rec->magic = TDB1_FREE_MAGIC;
if (tdb->methods->tdb1_write(tdb, off, rec, sizeof(*rec)) == -1)
if (tdb->tdb1.io->tdb1_write(tdb, off, rec, sizeof(*rec)) == -1)
return -1;
}
......@@ -50,14 +50,14 @@ int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off, struct tdb1_rec
rec->magic, off);
return -1;
}
if (tdb->methods->tdb1_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
if (tdb->tdb1.io->tdb1_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
return -1;
return 0;
}
/* update a record tailer (must hold allocation lock) */
static int update_tailer(struct tdb1_context *tdb, tdb1_off_t offset,
static int update_tailer(struct tdb_context *tdb, tdb1_off_t offset,
const struct tdb1_record *rec)
{
tdb1_off_t totalsize;
......@@ -70,7 +70,7 @@ static int update_tailer(struct tdb1_context *tdb, tdb1_off_t offset,
/* Add an element into the freelist. Merge adjacent records if
necessary. */
int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec)
int tdb1_free(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec)
{
/* Allocation and tailer lock */
if (tdb1_lock(tdb, -1, F_WRLCK) != 0)
......@@ -84,7 +84,7 @@ int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *r
}
/* Look left */
if (offset - sizeof(tdb1_off_t) > TDB1_DATA_START(tdb->header.hash_size)) {
if (offset - sizeof(tdb1_off_t) > TDB1_DATA_START(tdb->tdb1.header.hash_size)) {
tdb1_off_t left = offset - sizeof(tdb1_off_t);
struct tdb1_record l;
tdb1_off_t leftsize;
......@@ -104,12 +104,12 @@ int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *r
left = offset - leftsize;
if (leftsize > offset ||
left < TDB1_DATA_START(tdb->header.hash_size)) {
left < TDB1_DATA_START(tdb->tdb1.header.hash_size)) {
goto update;
}
/* Now read in the left record */
if (tdb->methods->tdb1_read(tdb, left, &l, sizeof(l), TDB1_DOCONV()) == -1) {
if (tdb->tdb1.io->tdb1_read(tdb, left, &l, sizeof(l), TDB1_DOCONV()) == -1) {
tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
"tdb1_free: left read failed at %u (%u)", left, leftsize);
goto update;
......@@ -169,7 +169,7 @@ update:
not the beginning. This is so the left merge in a free is more likely to be
able to free up the record without fragmentation
*/
static tdb1_off_t tdb1_allocate_ofs(struct tdb1_context *tdb,
static tdb1_off_t tdb1_allocate_ofs(struct tdb_context *tdb,
tdb1_len_t length, tdb1_off_t rec_ptr,
struct tdb1_record *rec, tdb1_off_t last_ptr)
{
......@@ -224,7 +224,7 @@ static tdb1_off_t tdb1_allocate_ofs(struct tdb1_context *tdb,
0 is returned if the space could not be allocated
*/
tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb1_record *rec)
tdb1_off_t tdb1_allocate(struct tdb_context *tdb, tdb1_len_t length, struct tdb1_record *rec)
{
tdb1_off_t rec_ptr, last_ptr, newrec_ptr;
struct {
......
......@@ -36,7 +36,7 @@
if necessary
note that "len" is the minimum length needed for the db
*/
static int tdb1_oob(struct tdb1_context *tdb, tdb1_off_t len, int probe)
static int tdb1_oob(struct tdb_context *tdb, tdb1_off_t len, int probe)
{
struct stat st;
if (len <= tdb->file->map_size)
......@@ -75,19 +75,19 @@ static int tdb1_oob(struct tdb1_context *tdb, tdb1_off_t len, int probe)
}
/* write a lump of data at a specified offset */
static int tdb1_write(struct tdb1_context *tdb, tdb1_off_t off,
static int tdb1_write(struct tdb_context *tdb, tdb1_off_t off,
const void *buf, tdb1_len_t len)
{
if (len == 0) {
return 0;
}
if ((tdb->flags & TDB_RDONLY) || tdb->traverse_read) {
if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) {
tdb->last_error = TDB_ERR_RDONLY;
return -1;
}
if (tdb->methods->tdb1_oob(tdb, off + len, 0) != 0)
if (tdb->tdb1.io->tdb1_oob(tdb, off + len, 0) != 0)
return -1;
if (tdb->file->map_ptr) {
......@@ -133,10 +133,10 @@ void *tdb1_convert(void *buf, uint32_t size)
/* read a lump of data at a specified offset, maybe convert */
static int tdb1_read(struct tdb1_context *tdb, tdb1_off_t off, void *buf,
static int tdb1_read(struct tdb_context *tdb, tdb1_off_t off, void *buf,
tdb1_len_t len, int cv)
{
if (tdb->methods->tdb1_oob(tdb, off + len, 0) != 0) {
if (tdb->tdb1.io->tdb1_oob(tdb, off + len, 0) != 0) {
return -1;
}
......@@ -167,18 +167,18 @@ static int tdb1_read(struct tdb1_context *tdb, tdb1_off_t off, void *buf,
do an unlocked scan of the hash table heads to find the next non-zero head. The value
will then be confirmed with the lock held
*/
static void tdb1_next_hash_chain(struct tdb1_context *tdb, uint32_t *chain)
static void tdb1_next_hash_chain(struct tdb_context *tdb, uint32_t *chain)
{
uint32_t h = *chain;
if (tdb->file->map_ptr) {
for (;h < tdb->header.hash_size;h++) {
for (;h < tdb->tdb1.header.hash_size;h++) {
if (0 != *(uint32_t *)(TDB1_HASH_TOP(h) + (unsigned char *)tdb->file->map_ptr)) {
break;
}
}
} else {
uint32_t off=0;
for (;h < tdb->header.hash_size;h++) {
for (;h < tdb->tdb1.header.hash_size;h++) {
if (tdb1_ofs_read(tdb, TDB1_HASH_TOP(h), &off) != 0 || off != 0) {
break;
}
......@@ -188,7 +188,7 @@ static void tdb1_next_hash_chain(struct tdb1_context *tdb, uint32_t *chain)
}
int tdb1_munmap(struct tdb1_context *tdb)
int tdb1_munmap(struct tdb_context *tdb)
{
if (tdb->flags & TDB_INTERNAL)
return 0;
......@@ -206,7 +206,7 @@ int tdb1_munmap(struct tdb1_context *tdb)
return 0;
}
void tdb1_mmap(struct tdb1_context *tdb)
void tdb1_mmap(struct tdb_context *tdb)
{
if (tdb->flags & TDB_INTERNAL)
return;
......@@ -243,11 +243,11 @@ void tdb1_mmap(struct tdb1_context *tdb)
/* expand a file. we prefer to use ftruncate, as that is what posix
says to use for mmap expansion */
static int tdb1_expand_file(struct tdb1_context *tdb, tdb1_off_t size, tdb1_off_t addition)
static int tdb1_expand_file(struct tdb_context *tdb, tdb1_off_t size, tdb1_off_t addition)
{
char buf[8192];
if ((tdb->flags & TDB_RDONLY) || tdb->traverse_read) {
if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) {
tdb->last_error = TDB_ERR_RDONLY;
return -1;
}
......@@ -313,7 +313,7 @@ static int tdb1_expand_file(struct tdb1_context *tdb, tdb1_off_t size, tdb1_off_
/* expand the database at least size bytes by expanding the underlying
file and doing the mmap again if necessary */
int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size)
int tdb1_expand(struct tdb_context *tdb, tdb1_off_t size)
{
struct tdb1_record rec;
tdb1_off_t offset, new_size, top_size, map_size;
......@@ -325,7 +325,7 @@ int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size)
}
/* must know about any previous expansions by another process */
tdb->methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
tdb->tdb1.io->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
/* limit size in order to avoid using up huge amounts of memory for
* in memory tdbs if an oddball huge record creeps in */
......@@ -346,7 +346,7 @@ int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size)
/* Round the database up to a multiple of the page size */
new_size = MAX(top_size, map_size);
size = TDB1_ALIGN(new_size, tdb->page_size) - tdb->file->map_size;
size = TDB1_ALIGN(new_size, tdb->tdb1.page_size) - tdb->file->map_size;
if (!(tdb->flags & TDB_INTERNAL))
tdb1_munmap(tdb);
......@@ -359,7 +359,7 @@ int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size)
/* expand the file itself */
if (!(tdb->flags & TDB_INTERNAL)) {
if (tdb->methods->tdb1_expand_file(tdb, tdb->file->map_size, size) != 0)
if (tdb->tdb1.io->tdb1_expand_file(tdb, tdb->file->map_size, size) != 0)
goto fail;
}
......@@ -401,20 +401,20 @@ int tdb1_expand(struct tdb1_context *tdb, tdb1_off_t size)
}
/* read/write a tdb1_off_t */
int tdb1_ofs_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d)
int tdb1_ofs_read(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d)
{
return tdb->methods->tdb1_read(tdb, offset, (char*)d, sizeof(*d), TDB1_DOCONV());
return tdb->tdb1.io->tdb1_read(tdb, offset, (char*)d, sizeof(*d), TDB1_DOCONV());
}
int tdb1_ofs_write(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_off_t *d)
int tdb1_ofs_write(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d)
{
tdb1_off_t off = *d;
return tdb->methods->tdb1_write(tdb, offset, TDB1_CONV(off), sizeof(*d));
return tdb->tdb1.io->tdb1_write(tdb, offset, TDB1_CONV(off), sizeof(*d));
}
/* read a lump of data, allocating the space for it */
unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_len_t len)
unsigned char *tdb1_alloc_read(struct tdb_context *tdb, tdb1_off_t offset, tdb1_len_t len)
{
unsigned char *buf;
......@@ -427,7 +427,7 @@ unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1
len, strerror(errno));
return NULL;
}
if (tdb->methods->tdb1_read(tdb, offset, buf, len, 0) == -1) {
if (tdb->tdb1.io->tdb1_read(tdb, offset, buf, len, 0) == -1) {
SAFE_FREE(buf);
return NULL;
}
......@@ -436,7 +436,7 @@ unsigned char *tdb1_alloc_read(struct tdb1_context *tdb, tdb1_off_t offset, tdb1
/* Give a piece of tdb data to a parser */
int tdb1_parse_data(struct tdb1_context *tdb, TDB_DATA key,
int tdb1_parse_data(struct tdb_context *tdb, TDB_DATA key,
tdb1_off_t offset, tdb1_len_t len,
int (*parser)(TDB_DATA key, TDB_DATA data,
void *private_data),
......@@ -447,12 +447,12 @@ int tdb1_parse_data(struct tdb1_context *tdb, TDB_DATA key,
data.dsize = len;
if ((tdb->transaction == NULL) && (tdb->file->map_ptr != NULL)) {
if ((tdb->tdb1.transaction == NULL) && (tdb->file->map_ptr != NULL)) {
/*
* Optimize by avoiding the malloc/memcpy/free, point the
* parser directly at the mmap area.
*/
if (tdb->methods->tdb1_oob(tdb, offset+len, 0) != 0) {
if (tdb->tdb1.io->tdb1_oob(tdb, offset+len, 0) != 0) {
return -1;
}
data.dptr = offset + (unsigned char *)tdb->file->map_ptr;
......@@ -469,9 +469,9 @@ int tdb1_parse_data(struct tdb1_context *tdb, TDB_DATA key,
}
/* read/write a record */
int tdb1_rec_read(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec)
int tdb1_rec_read(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec)
{
if (tdb->methods->tdb1_read(tdb, offset, rec, sizeof(*rec),TDB1_DOCONV()) == -1)
if (tdb->tdb1.io->tdb1_read(tdb, offset, rec, sizeof(*rec),TDB1_DOCONV()) == -1)
return -1;
if (TDB1_BAD_MAGIC(rec)) {
tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
......@@ -479,13 +479,13 @@ int tdb1_rec_read(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_recor
rec->magic, offset);
return -1;
}
return tdb->methods->tdb1_oob(tdb, rec->next+sizeof(*rec), 0);
return tdb->tdb1.io->tdb1_oob(tdb, rec->next+sizeof(*rec), 0);
}
int tdb1_rec_write(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec)
int tdb1_rec_write(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec)
{
struct tdb1_record r = *rec;
return tdb->methods->tdb1_write(tdb, offset, TDB1_CONV(r), sizeof(r));
return tdb->tdb1.io->tdb1_write(tdb, offset, TDB1_CONV(r), sizeof(r));
}
static const struct tdb1_methods io1_methods = {
......@@ -499,7 +499,7 @@ static const struct tdb1_methods io1_methods = {
/*
initialise the default methods table
*/
void tdb1_io_init(struct tdb1_context *tdb)
void tdb1_io_init(struct tdb_context *tdb)
{
tdb->methods = &io1_methods;
tdb->tdb1.io = &io1_methods;
}
This diff is collapsed.
......@@ -28,10 +28,10 @@
#include "tdb1_private.h"
/* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
static struct tdb1_context *tdb1s = NULL;
static struct tdb_context *tdb1s = NULL;
/* We use two hashes to double-check they're using the right hash function. */
void tdb1_header_hash(struct tdb1_context *tdb,
void tdb1_header_hash(struct tdb_context *tdb,
uint32_t *magic1_hash, uint32_t *magic2_hash)
{
uint32_t tdb1_magic = TDB1_MAGIC;
......@@ -45,7 +45,7 @@ void tdb1_header_hash(struct tdb1_context *tdb,
}
/* initialise a new database with a specified hash size */
static int tdb1_new_database(struct tdb1_context *tdb, int hash_size)
static int tdb1_new_database(struct tdb_context *tdb, int hash_size)
{
struct tdb1_header *newdb;
size_t size;
......@@ -73,7 +73,7 @@ static int tdb1_new_database(struct tdb1_context *tdb, int hash_size)
tdb->file->fd = -1;
tdb->file->map_size = size;
tdb->file->map_ptr = (char *)newdb;
memcpy(&tdb->header, newdb, sizeof(tdb->header));
memcpy(&tdb->tdb1.header, newdb, sizeof(tdb->tdb1.header));
/* Convert the `ondisk' version if asked. */
TDB1_CONV(*newdb);
return 0;
......@@ -86,7 +86,7 @@ static int tdb1_new_database(struct tdb1_context *tdb, int hash_size)
/* This creates an endian-converted header, as if read from disk */
TDB1_CONV(*newdb);
memcpy(&tdb->header, newdb, sizeof(tdb->header));
memcpy(&tdb->tdb1.header, newdb, sizeof(tdb->tdb1.header));
/* Don't endian-convert the magic food! */
memcpy(newdb->magic_food, TDB_MAGIC_FOOD, strlen(TDB_MAGIC_FOOD)+1);
/* we still have "ret == -1" here */
......@@ -103,7 +103,7 @@ static int tdb1_new_database(struct tdb1_context *tdb, int hash_size)
static int tdb1_already_open(dev_t device,
ino_t ino)
{
struct tdb1_context *i;
struct tdb_context *i;
for (i = tdb1s; i; i = i->next) {
if (i->file->device == device && i->file->inode == ino) {
......@@ -124,21 +124,21 @@ static int tdb1_already_open(dev_t device,
try to call tdb1_error or tdb1_errname, just do strerror(errno).
@param name may be NULL for internal databases. */
struct tdb1_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
struct tdb_context *tdb1_open(const char *name, int hash_size, int tdb1_flags,
int open_flags, mode_t mode)
{
return tdb1_open_ex(name, hash_size, tdb1_flags, open_flags, mode, NULL, NULL);
}
static bool hash_correct(struct tdb1_context *tdb,
static bool hash_correct(struct tdb_context *tdb,
uint32_t *m1, uint32_t *m2)
{
tdb1_header_hash(tdb, m1, m2);
return (tdb->header.magic1_hash == *m1 &&
tdb->header.magic2_hash == *m2);
return (tdb->tdb1.header.magic1_hash == *m1 &&
tdb->tdb1.header.magic2_hash == *m2);
}
static bool check_header_hash(struct tdb1_context *tdb,
static bool check_header_hash(struct tdb_context *tdb,
uint32_t *m1, uint32_t *m2)
{
if (hash_correct(tdb, m1, m2))
......@@ -154,19 +154,19 @@ static bool check_header_hash(struct tdb1_context *tdb,
return hash_correct(tdb, m1, m2);
}
struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
struct tdb_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flags,
int open_flags, mode_t mode,
const struct tdb1_logging_context *log_ctx,
tdb1_hash_func hash_fn)
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
struct stat st;
int rev = 0;
unsigned v;
const char *hash_alg;
uint32_t magic1, magic2;
if (!(tdb = (struct tdb1_context *)calloc(1, sizeof *tdb))) {
if (!(tdb = (struct tdb_context *)calloc(1, sizeof *tdb))) {
/* Can't log this */
errno = ENOMEM;
goto fail;
......@@ -231,13 +231,13 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag
}
/* cache the page size */
tdb->page_size = getpagesize();
if (tdb->page_size <= 0) {
tdb->page_size = 0x2000;
tdb->tdb1.page_size = getpagesize();
if (tdb->tdb1.page_size <= 0) {
tdb->tdb1.page_size = 0x2000;
}
/* FIXME: Used to be 5 for TDB_VOLATILE. */
tdb->max_dead_records = 0;
tdb->tdb1.max_dead_records = 0;
if ((open_flags & O_ACCMODE) == O_WRONLY) {
tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
......@@ -286,8 +286,8 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag
}
errno = 0;
if (read(tdb->file->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header)
|| strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) {
if (read(tdb->file->fd, &tdb->tdb1.header, sizeof(tdb->tdb1.header)) != sizeof(tdb->tdb1.header)
|| strcmp(tdb->tdb1.header.magic_food, TDB_MAGIC_FOOD) != 0) {
if (!(open_flags & O_CREAT) || tdb1_new_database(tdb, hash_size) == -1) {
if (errno == 0) {
errno = EIO; /* ie bad format or something */
......@@ -295,8 +295,8 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag
goto fail;
}
rev = (tdb->flags & TDB_CONVERT);
} else if (tdb->header.version != TDB1_VERSION
&& !(rev = (tdb->header.version==TDB1_BYTEREV(TDB1_VERSION)))) {
} else if (tdb->tdb1.header.version != TDB1_VERSION
&& !(rev = (tdb->tdb1.header.version==TDB1_BYTEREV(TDB1_VERSION)))) {
/* wrong version */
errno = EIO;
goto fail;
......@@ -305,19 +305,19 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag
tdb->flags &= ~TDB_CONVERT;
else {
tdb->flags |= TDB_CONVERT;
tdb1_convert(&tdb->header, sizeof(tdb->header));
tdb1_convert(&tdb->tdb1.header, sizeof(tdb->tdb1.header));
}
if (fstat(tdb->file->fd, &st) == -1)
goto fail;
if (tdb->header.rwlocks != 0 &&
tdb->header.rwlocks != TDB1_HASH_RWLOCK_MAGIC) {
if (tdb->tdb1.header.rwlocks != 0 &&
tdb->tdb1.header.rwlocks != TDB1_HASH_RWLOCK_MAGIC) {
tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"tdb1_open_ex: spinlocks no longer supported");
goto fail;
}
if ((tdb->header.magic1_hash == 0) && (tdb->header.magic2_hash == 0)) {
if ((tdb->tdb1.header.magic1_hash == 0) && (tdb->tdb1.header.magic2_hash == 0)) {
/* older TDB without magic hash references */
tdb->hash_fn = tdb1_old_hash;
} else if (!check_header_hash(tdb, &magic1, &magic2)) {
......@@ -327,11 +327,11 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag
"magic1_hash[0x%08X %s 0x%08X] "
"magic2_hash[0x%08X %s 0x%08X]",
name, hash_alg,
tdb->header.magic1_hash,
(tdb->header.magic1_hash == magic1) ? "==" : "!=",
tdb->tdb1.header.magic1_hash,
(tdb->tdb1.header.magic1_hash == magic1) ? "==" : "!=",
magic1,
tdb->header.magic2_hash,
(tdb->header.magic2_hash == magic2) ? "==" : "!=",
tdb->tdb1.header.magic2_hash,
(tdb->tdb1.header.magic2_hash == magic2) ? "==" : "!=",
magic2);
errno = EINVAL;
goto fail;
......@@ -399,9 +399,9 @@ struct tdb1_context *tdb1_open_ex(const char *name, int hash_size, int tdb1_flag
* Set the maximum number of dead records per hash chain
*/
void tdb1_set_max_dead(struct tdb1_context *tdb, int max_dead)
void tdb1_set_max_dead(struct tdb_context *tdb, int max_dead)
{
tdb->max_dead_records = max_dead;
tdb->tdb1.max_dead_records = max_dead;
}
/**
......@@ -409,12 +409,12 @@ void tdb1_set_max_dead(struct tdb1_context *tdb, int max_dead)
*
* @returns -1 for error; 0 for success.
**/
int tdb1_close(struct tdb1_context *tdb)
int tdb1_close(struct tdb_context *tdb)
{
struct tdb1_context **i;
struct tdb_context **i;
int ret = 0;
if (tdb->transaction) {
if (tdb->tdb1.transaction) {
tdb1_transaction_cancel(tdb);
}
......
This diff is collapsed.
......@@ -65,7 +65,7 @@ static size_t tally1_mean(const struct tally *tally)
return tally->total / tally->num;
}
static size_t get_hash_length(struct tdb1_context *tdb, unsigned int i)
static size_t get_hash_length(struct tdb_context *tdb, unsigned int i)
{
tdb1_off_t rec_ptr;
size_t count = 0;
......@@ -84,7 +84,7 @@ static size_t get_hash_length(struct tdb1_context *tdb, unsigned int i)
return count;
}
char *tdb1_summary(struct tdb1_context *tdb)
char *tdb1_summary(struct tdb_context *tdb)
{
tdb1_off_t off, rec_off;
struct tally freet, keys, data, dead, extra, hash, uncoal;
......@@ -103,7 +103,7 @@ char *tdb1_summary(struct tdb1_context *tdb)
locked = true;
}
if (tdb1_recovery_area(tdb, tdb->methods, &rec_off, &recovery) != 0) {
if (tdb1_recovery_area(tdb, tdb->tdb1.io, &rec_off, &recovery) != 0) {
goto unlock;
}
......@@ -115,10 +115,10 @@ char *tdb1_summary(struct tdb1_context *tdb)
tally1_init(&hash);
tally1_init(&uncoal);
for (off = TDB1_DATA_START(tdb->header.hash_size);
for (off = TDB1_DATA_START(tdb->tdb1.header.hash_size);
off < tdb->file->map_size - 1;
off += sizeof(rec) + rec.rec_len) {
if (tdb->methods->tdb1_read(tdb, off, &rec, sizeof(rec),
if (tdb->tdb1.io->tdb1_read(tdb, off, &rec, sizeof(rec),
TDB1_DOCONV()) == -1)
goto unlock;
switch (rec.magic) {
......@@ -160,7 +160,7 @@ char *tdb1_summary(struct tdb1_context *tdb)
if (unc > 1)
tally1_add(&uncoal, unc - 1);
for (off = 0; off < tdb->header.hash_size; off++)
for (off = 0; off < tdb->tdb1.header.hash_size; off++)
tally1_add(&hash, get_hash_length(tdb, off));
/* 20 is max length of a %zu. */
......@@ -191,7 +191,7 @@ char *tdb1_summary(struct tdb1_context *tdb)
(keys.num + freet.num + dead.num)
* (sizeof(struct tdb1_record) + sizeof(uint32_t))
* 100.0 / tdb->file->map_size,
tdb->header.hash_size * sizeof(tdb1_off_t)
tdb->tdb1.header.hash_size * sizeof(tdb1_off_t)
* 100.0 / (tdb1_len_t)tdb->file->map_size);
unlock:
......
This diff is collapsed.
This diff is collapsed.
......@@ -31,13 +31,13 @@
/* Uses traverse lock: 0 = finish, TDB1_NEXT_LOCK_ERR = error,
other = record offset */
static tdb1_off_t tdb1_next_lock(struct tdb1_context *tdb, struct tdb1_traverse_lock *tlock,
static tdb1_off_t tdb1_next_lock(struct tdb_context *tdb, struct tdb1_traverse_lock *tlock,
struct tdb1_record *rec)
{
int want_next = (tlock->off != 0);
/* Lock each chain from the start one. */
for (; tlock->hash < tdb->header.hash_size; tlock->hash++) {
for (; tlock->hash < tdb->tdb1.header.hash_size; tlock->hash++) {
if (!tlock->off && tlock->hash != 0) {
/* this is an optimisation for the common case where
the hash chain is empty, which is particularly
......@@ -67,8 +67,8 @@ static tdb1_off_t tdb1_next_lock(struct tdb1_context *tdb, struct tdb1_traverse_
factor of around 80 in speed on a linux 2.6.x
system (testing using ldbtest).
*/
tdb->methods->next_hash_chain(tdb, &tlock->hash);
if (tlock->hash == tdb->header.hash_size) {
tdb->tdb1.io->next_hash_chain(tdb, &tlock->hash);
if (tlock->hash == tdb->tdb1.header.hash_size) {
continue;
}
}
......@@ -119,7 +119,7 @@ static tdb1_off_t tdb1_next_lock(struct tdb1_context *tdb, struct tdb1_traverse_
/* Try to clean dead ones from old traverses */
current = tlock->off;
tlock->off = rec->next;
if (!((tdb->flags & TDB_RDONLY) || tdb->traverse_read) &&
if (!((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) &&
tdb1_do_delete(tdb, current, rec) != 0)
goto fail;
}
......@@ -143,7 +143,7 @@ static tdb1_off_t tdb1_next_lock(struct tdb1_context *tdb, struct tdb1_traverse_
if fn is NULL then it is not called
a non-zero return value from fn() indicates that the traversal should stop
*/
static int tdb1_traverse_internal(struct tdb1_context *tdb,
static int tdb1_traverse_internal(struct tdb_context *tdb,
tdb1_traverse_func fn, void *private_data,
struct tdb1_traverse_lock *tl)
{
......@@ -155,10 +155,10 @@ static int tdb1_traverse_internal(struct tdb1_context *tdb,
/* This was in the initializaton, above, but the IRIX compiler
* did not like it. crh
*/
tl->next = tdb->travlocks.next;
tl->next = tdb->tdb1.travlocks.next;
/* fcntl locks don't stack: beware traverse inside traverse */
tdb->travlocks.next = tl;
tdb->tdb1.travlocks.next = tl;
/* tdb1_next_lock places locks on the record returned, and its chain */
while ((off = tdb1_next_lock(tdb, tl, &rec)) != 0) {
......@@ -204,7 +204,7 @@ static int tdb1_traverse_internal(struct tdb1_context *tdb,
SAFE_FREE(key.dptr);
}
out:
tdb->travlocks.next = tl->next;
tdb->tdb1.travlocks.next = tl->next;
if (ret < 0)
return -1;
else
......@@ -215,7 +215,7 @@ out:
/*
a write style traverse - temporarily marks the db read only
*/
int tdb1_traverse_read(struct tdb1_context *tdb,
int tdb1_traverse_read(struct tdb_context *tdb,
tdb1_traverse_func fn, void *private_data)
{
struct tdb1_traverse_lock tl = { NULL, 0, 0, F_RDLCK };
......@@ -227,9 +227,9 @@ int tdb1_traverse_read(struct tdb1_context *tdb,
return -1;
}
tdb->traverse_read++;
tdb->tdb1.traverse_read++;
ret = tdb1_traverse_internal(tdb, fn, private_data, &tl);
tdb->traverse_read--;
tdb->tdb1.traverse_read--;
tdb1_transaction_unlock(tdb, F_RDLCK);
......@@ -243,13 +243,13 @@ int tdb1_traverse_read(struct tdb1_context *tdb,
WARNING: The data buffer given to the callback fn does NOT meet the
alignment restrictions malloc gives you.
*/
int tdb1_traverse(struct tdb1_context *tdb,
int tdb1_traverse(struct tdb_context *tdb,
tdb1_traverse_func fn, void *private_data)
{
struct tdb1_traverse_lock tl = { NULL, 0, 0, F_WRLCK };
int ret;
if ((tdb->flags & TDB_RDONLY) || tdb->traverse_read) {
if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) {
return tdb1_traverse_read(tdb, fn, private_data);
}
......@@ -257,9 +257,9 @@ int tdb1_traverse(struct tdb1_context *tdb,
return -1;
}
tdb->traverse_write++;
tdb->tdb1.traverse_write++;
ret = tdb1_traverse_internal(tdb, fn, private_data, &tl);
tdb->traverse_write--;
tdb->tdb1.traverse_write--;
tdb1_transaction_unlock(tdb, F_WRLCK);
......@@ -268,29 +268,29 @@ int tdb1_traverse(struct tdb1_context *tdb,
/* find the first entry in the database and return its key */
TDB_DATA tdb1_firstkey(struct tdb1_context *tdb)
TDB_DATA tdb1_firstkey(struct tdb_context *tdb)
{
TDB_DATA key;
struct tdb1_record rec;
tdb1_off_t off;
/* release any old lock */
if (tdb1_unlock_record(tdb, tdb->travlocks.off) != 0)
if (tdb1_unlock_record(tdb, tdb->tdb1.travlocks.off) != 0)
return tdb1_null;
tdb->travlocks.off = tdb->travlocks.hash = 0;
tdb->travlocks.lock_rw = F_RDLCK;
tdb->tdb1.travlocks.off = tdb->tdb1.travlocks.hash = 0;
tdb->tdb1.travlocks.lock_rw = F_RDLCK;
/* Grab first record: locks chain and returned record. */
off = tdb1_next_lock(tdb, &tdb->travlocks, &rec);
off = tdb1_next_lock(tdb, &tdb->tdb1.travlocks, &rec);
if (off == 0 || off == TDB1_NEXT_LOCK_ERR) {
return tdb1_null;
}
/* now read the key */
key.dsize = rec.key_len;
key.dptr =tdb1_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),key.dsize);
key.dptr =tdb1_alloc_read(tdb,tdb->tdb1.travlocks.off+sizeof(rec),key.dsize);
/* Unlock the hash chain of the record we just read. */
if (tdb1_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
if (tdb1_unlock(tdb, tdb->tdb1.travlocks.hash, tdb->tdb1.travlocks.lock_rw) != 0)
tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
"tdb1_firstkey:"
" error occurred while tdb1_unlocking!");
......@@ -298,7 +298,7 @@ TDB_DATA tdb1_firstkey(struct tdb1_context *tdb)
}
/* find the next entry in the database, returning its key */
TDB_DATA tdb1_nextkey(struct tdb1_context *tdb, TDB_DATA oldkey)
TDB_DATA tdb1_nextkey(struct tdb_context *tdb, TDB_DATA oldkey)
{
uint32_t oldhash;
TDB_DATA key = tdb1_null;
......@@ -307,58 +307,58 @@ TDB_DATA tdb1_nextkey(struct tdb1_context *tdb, TDB_DATA oldkey)
tdb1_off_t off;
/* Is locked key the old key? If so, traverse will be reliable. */
if (tdb->travlocks.off) {
if (tdb1_lock(tdb,tdb->travlocks.hash,tdb->travlocks.lock_rw))
if (tdb->tdb1.travlocks.off) {
if (tdb1_lock(tdb,tdb->tdb1.travlocks.hash,tdb->tdb1.travlocks.lock_rw))
return tdb1_null;
if (tdb1_rec_read(tdb, tdb->travlocks.off, &rec) == -1
|| !(k = tdb1_alloc_read(tdb,tdb->travlocks.off+sizeof(rec),
if (tdb1_rec_read(tdb, tdb->tdb1.travlocks.off, &rec) == -1
|| !(k = tdb1_alloc_read(tdb,tdb->tdb1.travlocks.off+sizeof(rec),
rec.key_len))
|| memcmp(k, oldkey.dptr, oldkey.dsize) != 0) {
/* No, it wasn't: unlock it and start from scratch */
if (tdb1_unlock_record(tdb, tdb->travlocks.off) != 0) {
if (tdb1_unlock_record(tdb, tdb->tdb1.travlocks.off) != 0) {
SAFE_FREE(k);
return tdb1_null;
}
if (tdb1_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0) {
if (tdb1_unlock(tdb, tdb->tdb1.travlocks.hash, tdb->tdb1.travlocks.lock_rw) != 0) {
SAFE_FREE(k);
return tdb1_null;
}
tdb->travlocks.off = 0;
tdb->tdb1.travlocks.off = 0;
}
SAFE_FREE(k);
}
if (!tdb->travlocks.off) {
if (!tdb->tdb1.travlocks.off) {
/* No previous element: do normal find, and lock record */
tdb->travlocks.off = tdb1_find_lock_hash(tdb, oldkey, tdb_hash(tdb, oldkey.dptr, oldkey.dsize), tdb->travlocks.lock_rw, &rec);
if (!tdb->travlocks.off) {
tdb->tdb1.travlocks.off = tdb1_find_lock_hash(tdb, oldkey, tdb_hash(tdb, oldkey.dptr, oldkey.dsize), tdb->tdb1.travlocks.lock_rw, &rec);
if (!tdb->tdb1.travlocks.off) {
return tdb1_null;
}
tdb->travlocks.hash = TDB1_BUCKET(rec.full_hash);
if (tdb1_lock_record(tdb, tdb->travlocks.off) != 0) {
tdb->tdb1.travlocks.hash = TDB1_BUCKET(rec.full_hash);
if (tdb1_lock_record(tdb, tdb->tdb1.travlocks.off) != 0) {
tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
"tdb1_nextkey: lock_record failed (%s)!",
strerror(errno));
return tdb1_null;
}
}
oldhash = tdb->travlocks.hash;
oldhash = tdb->tdb1.travlocks.hash;
/* Grab next record: locks chain and returned record,
unlocks old record */
off = tdb1_next_lock(tdb, &tdb->travlocks, &rec);
off = tdb1_next_lock(tdb, &tdb->tdb1.travlocks, &rec);
if (off != TDB1_NEXT_LOCK_ERR && off != 0) {
key.dsize = rec.key_len;
key.dptr = tdb1_alloc_read(tdb, tdb->travlocks.off+sizeof(rec),
key.dptr = tdb1_alloc_read(tdb, tdb->tdb1.travlocks.off+sizeof(rec),
key.dsize);
/* Unlock the chain of this new record */
if (tdb1_unlock(tdb, tdb->travlocks.hash, tdb->travlocks.lock_rw) != 0)
if (tdb1_unlock(tdb, tdb->tdb1.travlocks.hash, tdb->tdb1.travlocks.lock_rw) != 0)
tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
"tdb1_nextkey: WARNING tdb1_unlock failed!");
}
/* Unlock the chain of old record */
if (tdb1_unlock(tdb, TDB1_BUCKET(oldhash), tdb->travlocks.lock_rw) != 0)
if (tdb1_unlock(tdb, TDB1_BUCKET(oldhash), tdb->tdb1.travlocks.lock_rw) != 0)
tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
"tdb1_nextkey: WARNING tdb1_unlock failed!");
return key;
......
......@@ -147,8 +147,8 @@ static void add_to_freetable(struct tdb_context *tdb,
unsigned ftable,
struct tle_freetable *freetable)
{
tdb->ftable_off = freetable->base.off;
tdb->ftable = ftable;
tdb->tdb2.ftable_off = freetable->base.off;
tdb->tdb2.ftable = ftable;
add_free_record(tdb, eoff, sizeof(struct tdb_used_record) + elen,
TDB_LOCK_WAIT, false);
}
......@@ -313,7 +313,7 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout,
}
}
tdb->ftable_off = find_ftable(layout, 0)->base.off;
tdb->tdb2.ftable_off = find_ftable(layout, 0)->base.off;
/* Get physical if they asked for it. */
if (layout->filename) {
......
......@@ -41,7 +41,7 @@ int main(int argc, char *argv[])
ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
/* Figure out which bucket free entry is. */
b_off = bucket_off(tdb->ftable_off, size_to_bucket(len));
b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(len));
/* Lock and fail to coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
test = layout->elem[1].base.off;
......@@ -64,7 +64,7 @@ int main(int argc, char *argv[])
ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Figure out which bucket free entry is. */
b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024));
/* Lock and fail to coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
test = layout->elem[1].base.off;
......@@ -88,7 +88,7 @@ int main(int argc, char *argv[])
ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Figure out which bucket (first) free entry is. */
b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024));
/* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
test = layout->elem[2].base.off;
......@@ -115,7 +115,7 @@ int main(int argc, char *argv[])
ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Figure out which bucket free entry is. */
b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024));
/* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
test = layout->elem[2].base.off;
......@@ -142,7 +142,7 @@ int main(int argc, char *argv[])
ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Figure out which bucket free entry is. */
b_off = bucket_off(tdb->ftable_off, size_to_bucket(1024));
b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024));
/* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
test = layout->elem[2].base.off;
......
......@@ -68,9 +68,9 @@ int main(int argc, char *argv[])
/* Make sure we fill it in for later finding. */
off = new_off + sizeof(struct tdb_used_record);
ok1(!tdb->methods->twrite(tdb, off, key.dptr, key.dsize));
ok1(!tdb->tdb2.io->twrite(tdb, off, key.dptr, key.dsize));
off += key.dsize;
ok1(!tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize));
ok1(!tdb->tdb2.io->twrite(tdb, off, dbuf.dptr, dbuf.dsize));
/* We should be able to unlock that OK. */
ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
......@@ -228,9 +228,9 @@ int main(int argc, char *argv[])
/* Make sure we fill it in for later finding. */
off = new_off + sizeof(struct tdb_used_record);
ok1(!tdb->methods->twrite(tdb, off, key.dptr, key.dsize));
ok1(!tdb->tdb2.io->twrite(tdb, off, key.dptr, key.dsize));
off += key.dsize;
ok1(!tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize));
ok1(!tdb->tdb2.io->twrite(tdb, off, dbuf.dptr, dbuf.dsize));
/* We should be able to unlock that OK. */
ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
......
......@@ -9,7 +9,7 @@ static bool empty_freetable(struct tdb_context *tdb)
unsigned int i;
/* Now, free table should be completely exhausted in zone 0 */
if (tdb_read_convert(tdb, tdb->ftable_off, &ftab, sizeof(ftab)) != 0)
if (tdb_read_convert(tdb, tdb->tdb2.ftable_off, &ftab, sizeof(ftab)) != 0)
abort();
for (i = 0; i < sizeof(ftab.buckets)/sizeof(ftab.buckets[0]); i++) {
......
......@@ -41,22 +41,22 @@ int main(int argc, char *argv[])
off = get_free(tdb, 0, 80 - sizeof(struct tdb_used_record), 0,
TDB_USED_MAGIC, 0);
ok1(off == layout->elem[3].base.off);
ok1(tdb->ftable_off == layout->elem[0].base.off);
ok1(tdb->tdb2.ftable_off == layout->elem[0].base.off);
off = get_free(tdb, 0, 160 - sizeof(struct tdb_used_record), 0,
TDB_USED_MAGIC, 0);
ok1(off == layout->elem[5].base.off);
ok1(tdb->ftable_off == layout->elem[1].base.off);
ok1(tdb->tdb2.ftable_off == layout->elem[1].base.off);
off = get_free(tdb, 0, 320 - sizeof(struct tdb_used_record), 0,
TDB_USED_MAGIC, 0);
ok1(off == layout->elem[7].base.off);
ok1(tdb->ftable_off == layout->elem[2].base.off);
ok1(tdb->tdb2.ftable_off == layout->elem[2].base.off);
off = get_free(tdb, 0, 40 - sizeof(struct tdb_used_record), 0,
TDB_USED_MAGIC, 0);
ok1(off == layout->elem[9].base.off);
ok1(tdb->ftable_off == layout->elem[0].base.off);
ok1(tdb->tdb2.ftable_off == layout->elem[0].base.off);
/* Now we fail. */
off = get_free(tdb, 0, 0, 1, TDB_USED_MAGIC, 0);
......
......@@ -6,11 +6,11 @@
#include <err.h>
#include "tdb1-logging.h"
static int tdb1_expand_file_sparse(struct tdb1_context *tdb,
static int tdb1_expand_file_sparse(struct tdb_context *tdb,
tdb1_off_t size,
tdb1_off_t addition)
{
if ((tdb->flags & TDB_RDONLY) || tdb->traverse_read) {
if ((tdb->flags & TDB_RDONLY) || tdb->tdb1.traverse_read) {
tdb->last_error = TDB_ERR_RDONLY;
return -1;
}
......@@ -46,7 +46,7 @@ static const struct tdb1_methods large_io_methods = {
tdb1_expand_file_sparse
};
static int test_traverse(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
static int test_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
void *_data)
{
TDB_DATA *expect = _data;
......@@ -59,7 +59,7 @@ static int test_traverse(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, orig_data, data;
uint32_t hash;
tdb1_off_t rec_ptr;
......@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
ok1(tdb);
tdb->methods = &large_io_methods;
tdb->tdb1.io = &large_io_methods;
/* Enlarge the file (internally multiplies by 2). */
ok1(tdb1_expand(tdb, 1500000000) == 0);
......
......@@ -6,7 +6,7 @@
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
struct tdb1_header hdr;
int fd;
......
......@@ -6,7 +6,7 @@
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(13);
......
......@@ -23,7 +23,7 @@ static int check(TDB_DATA key, TDB_DATA data, void *private)
return 0;
}
static void tdb1_flip_bit(struct tdb1_context *tdb, unsigned int bit)
static void tdb1_flip_bit(struct tdb_context *tdb, unsigned int bit)
{
unsigned int off = bit / CHAR_BIT;
unsigned char mask = (1 << (bit % CHAR_BIT));
......@@ -40,7 +40,7 @@ static void tdb1_flip_bit(struct tdb1_context *tdb, unsigned int bit)
}
}
static void check_test(struct tdb1_context *tdb)
static void check_test(struct tdb_context *tdb)
{
TDB_DATA key, data;
unsigned int i, verifiable, corrupt, sizes[2], dsize, ksize;
......@@ -93,7 +93,7 @@ static void check_test(struct tdb1_context *tdb)
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
plan_tests(4);
/* This should use mmap. */
......
......@@ -81,7 +81,7 @@ static int ftruncate_check(int fd, off_t length)
static bool test_death(enum operation op, struct agent *agent)
{
struct tdb1_context *tdb = NULL;
struct tdb_context *tdb = NULL;
TDB_DATA key;
enum agent_return ret;
int needed_recovery = 0;
......
......@@ -6,7 +6,7 @@
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(13);
......
......@@ -9,7 +9,7 @@ static uint64_t tdb1_dumb_hash(const void *key, size_t len, uint64_t seed,
return len;
}
static void log_fn(struct tdb1_context *tdb, enum tdb_log_level level,
static void log_fn(struct tdb_context *tdb, enum tdb_log_level level,
enum TDB_ERROR ecode, const char *message, void *priv)
{
unsigned int *count = priv;
......@@ -46,7 +46,7 @@ static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
unsigned int log_count, flags;
TDB_DATA d;
struct tdb1_logging_context log_ctx = { log_fn, &log_count };
......
......@@ -7,7 +7,7 @@
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(27);
......
......@@ -23,7 +23,7 @@ static bool correct_data(TDB_DATA data)
&& memcmp(data.dptr, "world", data.dsize) == 0;
}
static int traverse2(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
static int traverse2(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
void *p)
{
ok1(correct_key(key));
......@@ -31,7 +31,7 @@ static int traverse2(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
return 0;
}
static int traverse1(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
static int traverse1(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
void *p)
{
ok1(correct_key(key));
......@@ -48,7 +48,7 @@ static int traverse1(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(17);
......
......@@ -14,7 +14,7 @@
#define NUM_ENTRIES 10
static bool prepare_entries(struct tdb1_context *tdb)
static bool prepare_entries(struct tdb_context *tdb)
{
unsigned int i;
TDB_DATA key, data;
......@@ -31,7 +31,7 @@ static bool prepare_entries(struct tdb1_context *tdb)
return true;
}
static void delete_entries(struct tdb1_context *tdb)
static void delete_entries(struct tdb_context *tdb)
{
unsigned int i;
TDB_DATA key;
......@@ -45,7 +45,7 @@ static void delete_entries(struct tdb1_context *tdb)
}
/* We don't know how many times this will run. */
static int delete_other(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
static int delete_other(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
void *private_data)
{
unsigned int i;
......@@ -57,7 +57,7 @@ static int delete_other(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
return 0;
}
static int delete_self(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
static int delete_self(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
void *private_data)
{
ok1(tdb1_delete(tdb, key) == 0);
......@@ -66,7 +66,7 @@ static int delete_self(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
int errors = 0;
plan_tests(41);
......
......@@ -6,7 +6,7 @@
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
plan_tests(8);
......
......@@ -131,7 +131,7 @@ int main(int argc, char *argv[])
const int flags[] = { TDB_DEFAULT,
TDB_NOMMAP };
int i;
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(10);
......
......@@ -8,7 +8,7 @@
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(11);
......
......@@ -3,7 +3,7 @@
#include <stdlib.h>
#include <err.h>
static void log_fn(struct tdb1_context *tdb, enum tdb_log_level level,
static void log_fn(struct tdb_context *tdb, enum tdb_log_level level,
enum TDB_ERROR ecode, const char *message, void *priv)
{
unsigned int *count = priv;
......@@ -14,7 +14,7 @@ static void log_fn(struct tdb1_context *tdb, enum tdb_log_level level,
/* The code should barf on TDBs created with rwlocks. */
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
unsigned int log_count;
struct tdb1_logging_context log_ctx = { log_fn, &log_count };
......
......@@ -6,7 +6,7 @@
int main(int argc, char *argv[])
{
unsigned int i, j;
struct tdb1_context *tdb;
struct tdb_context *tdb;
int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
TDB_NOMMAP|TDB_CONVERT };
......
......@@ -24,7 +24,7 @@ static bool correct_data(TDB_DATA data)
&& memcmp(data.dptr, "world", data.dsize) == 0;
}
static int traverse(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
static int traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
void *p)
{
ok1(correct_key(key));
......@@ -34,7 +34,7 @@ static int traverse(struct tdb1_context *tdb, TDB_DATA key, TDB_DATA data,
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(13);
......
......@@ -3,7 +3,7 @@
#include <stdlib.h>
#include <err.h>
static void log_fn(struct tdb1_context *tdb, enum tdb_log_level level,
static void log_fn(struct tdb_context *tdb, enum tdb_log_level level,
enum TDB_ERROR ecode, const char *message, void *priv)
{
unsigned int *count = priv;
......@@ -27,7 +27,7 @@ static uint64_t old_hash(const void *key, size_t len, uint64_t seed,
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
unsigned int log_count;
TDB_DATA d;
struct tdb1_logging_context log_ctx = { log_fn, &log_count };
......
......@@ -6,7 +6,7 @@
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(4);
......
......@@ -6,7 +6,7 @@
int main(int argc, char *argv[])
{
struct tdb1_context *tdb;
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(10);
......
......@@ -16,7 +16,7 @@
#include <stdio.h>
#include <stdarg.h>
static struct tdb1_context *tdb;
static struct tdb_context *tdb;
static enum agent_return do_operation(enum operation op, const char *name)
{
......
......@@ -6,7 +6,7 @@
#include <stdio.h>
/* Turn log messages into tap diag messages. */
static void taplog(struct tdb1_context *tdb,
static void taplog(struct tdb_context *tdb,
enum tdb_log_level level,
enum TDB_ERROR ecode,
const char *message,
......
This diff is collapsed.
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