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) ...@@ -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. */ /* Need to hold a hash lock to expand DB: transactions rely on it. */
if (!(tdb->flags & TDB_NOLOCK) 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, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_expand: must hold lock during expand"); "tdb_expand: must hold lock during expand");
} }
......
...@@ -49,7 +49,7 @@ void tdb_mmap(struct tdb_context *tdb) ...@@ -49,7 +49,7 @@ void tdb_mmap(struct tdb_context *tdb)
return; return;
tdb->map_ptr = mmap(NULL, tdb->map_size, tdb->mmap_flags, 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 !!!! * 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, ...@@ -96,7 +96,7 @@ static enum TDB_ERROR tdb_oob(struct tdb_context *tdb, tdb_off_t len,
return ecode; return ecode;
} }
if (fstat(tdb->fd, &st) != 0) { if (fstat(tdb->file->fd, &st) != 0) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"Failed to fstat file: %s", strerror(errno)); "Failed to fstat file: %s", strerror(errno));
tdb_unlock_expand(tdb, F_RDLCK); tdb_unlock_expand(tdb, F_RDLCK);
...@@ -245,7 +245,7 @@ static enum TDB_ERROR tdb_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -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); memcpy(off + (char *)tdb->map_ptr, buf, len);
} else { } else {
ssize_t ret; ssize_t ret;
ret = pwrite(tdb->fd, buf, len, off); ret = pwrite(tdb->file->fd, buf, len, off);
if (ret != len) { if (ret != len) {
/* This shouldn't happen: we avoid sparse files. */ /* This shouldn't happen: we avoid sparse files. */
if (ret >= 0) if (ret >= 0)
...@@ -274,7 +274,7 @@ static enum TDB_ERROR tdb_read(struct tdb_context *tdb, tdb_off_t off, ...@@ -274,7 +274,7 @@ static enum TDB_ERROR tdb_read(struct tdb_context *tdb, tdb_off_t off,
if (tdb->map_ptr) { if (tdb->map_ptr) {
memcpy(buf, off + (char *)tdb->map_ptr, len); memcpy(buf, off + (char *)tdb->map_ptr, len);
} else { } else {
ssize_t r = pread(tdb->fd, buf, len, off); ssize_t r = pread(tdb->file->fd, buf, len, off);
if (r != len) { if (r != len) {
return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_read failed with %zi at %zu " "tdb_read failed with %zi at %zu "
...@@ -374,7 +374,7 @@ static enum TDB_ERROR fill(struct tdb_context *tdb, ...@@ -374,7 +374,7 @@ static enum TDB_ERROR fill(struct tdb_context *tdb,
{ {
while (len) { while (len) {
size_t n = len > size ? size : 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 != n) {
if (ret >= 0) if (ret >= 0)
errno = ENOSPC; errno = ENOSPC;
...@@ -418,7 +418,7 @@ static enum TDB_ERROR tdb_expand_file(struct tdb_context *tdb, ...@@ -418,7 +418,7 @@ static enum TDB_ERROR tdb_expand_file(struct tdb_context *tdb,
tdb_munmap(tdb); tdb_munmap(tdb);
/* If this fails, we try to fill anyway. */ /* 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 /* now fill the file with something. This ensures that the
......
...@@ -42,10 +42,10 @@ static int fcntl_lock(struct tdb_context *tdb, ...@@ -42,10 +42,10 @@ static int fcntl_lock(struct tdb_context *tdb,
add_stat(tdb, lock_lowlevel, 1); add_stat(tdb, lock_lowlevel, 1);
if (waitflag) if (waitflag)
return fcntl(tdb->fd, F_SETLKW, &fl); return fcntl(tdb->file->fd, F_SETLKW, &fl);
else { else {
add_stat(tdb, lock_nonblock, 1); add_stat(tdb, lock_nonblock, 1);
return fcntl(tdb->fd, F_SETLK, &fl); return fcntl(tdb->file->fd, F_SETLK, &fl);
} }
} }
...@@ -116,7 +116,7 @@ static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len) ...@@ -116,7 +116,7 @@ static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
fl.l_len = len; fl.l_len = len;
fl.l_pid = 0; fl.l_pid = 0;
return fcntl(tdb->fd, F_SETLKW, &fl); return fcntl(tdb->file->fd, F_SETLKW, &fl);
} }
/* a byte range locking function - return 0 on success /* a byte range locking function - return 0 on success
...@@ -161,7 +161,7 @@ static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb, ...@@ -161,7 +161,7 @@ static enum TDB_ERROR tdb_brlock(struct tdb_context *tdb,
"tdb_brlock failed (fd=%d) at" "tdb_brlock failed (fd=%d) at"
" offset %zu rw_type=%d flags=%d len=%zu:" " offset %zu rw_type=%d flags=%d len=%zu:"
" %s", " %s",
tdb->fd, (size_t)offset, rw_type, tdb->file->fd, (size_t)offset, rw_type,
flags, (size_t)len, strerror(errno)); flags, (size_t)len, strerror(errno));
} }
return TDB_ERR_LOCK; return TDB_ERR_LOCK;
...@@ -186,7 +186,7 @@ static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb, ...@@ -186,7 +186,7 @@ static enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_brunlock failed (fd=%d) at offset %zu" "tdb_brunlock failed (fd=%d) at offset %zu"
" rw_type=%d len=%zu", " rw_type=%d len=%zu",
tdb->fd, (size_t)offset, rw_type, tdb->file->fd, (size_t)offset, rw_type,
(size_t)len); (size_t)len);
} }
return TDB_SUCCESS; return TDB_SUCCESS;
...@@ -202,14 +202,14 @@ enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb) ...@@ -202,14 +202,14 @@ enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb)
{ {
int count = 1000; int count = 1000;
if (tdb->allrecord_lock.count != 1) { if (tdb->file->allrecord_lock.count != 1) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_upgrade failed:" "tdb_allrecord_upgrade failed:"
" count %u too high", " count %u too high",
tdb->allrecord_lock.count); tdb->file->allrecord_lock.count);
} }
if (tdb->allrecord_lock.off != 1) { if (tdb->file->allrecord_lock.off != 1) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_upgrade failed:" "tdb_allrecord_upgrade failed:"
" already upgraded?"); " already upgraded?");
...@@ -220,8 +220,8 @@ enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb) ...@@ -220,8 +220,8 @@ enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb)
if (tdb_brlock(tdb, F_WRLCK, if (tdb_brlock(tdb, F_WRLCK,
TDB_HASH_LOCK_START, 0, TDB_HASH_LOCK_START, 0,
TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) { TDB_LOCK_WAIT|TDB_LOCK_PROBE) == TDB_SUCCESS) {
tdb->allrecord_lock.ltype = F_WRLCK; tdb->file->allrecord_lock.ltype = F_WRLCK;
tdb->allrecord_lock.off = 0; tdb->file->allrecord_lock.off = 0;
return TDB_SUCCESS; return TDB_SUCCESS;
} }
if (errno != EDEADLK) { if (errno != EDEADLK) {
...@@ -241,9 +241,9 @@ static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb, ...@@ -241,9 +241,9 @@ static struct tdb_lock_type *find_nestlock(struct tdb_context *tdb,
{ {
unsigned int i; unsigned int i;
for (i=0; i<tdb->num_lockrecs; i++) { for (i=0; i<tdb->file->num_lockrecs; i++) {
if (tdb->lockrecs[i].off == offset) { if (tdb->file->lockrecs[i].off == offset) {
return &tdb->lockrecs[i]; return &tdb->file->lockrecs[i];
} }
} }
return NULL; return NULL;
...@@ -303,7 +303,7 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, ...@@ -303,7 +303,7 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
return TDB_SUCCESS; return TDB_SUCCESS;
} }
if (tdb->num_lockrecs if (tdb->file->num_lockrecs
&& offset >= TDB_HASH_LOCK_START && offset >= TDB_HASH_LOCK_START
&& offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) { && offset < TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
...@@ -311,15 +311,15 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, ...@@ -311,15 +311,15 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
} }
new_lck = (struct tdb_lock_type *)realloc( new_lck = (struct tdb_lock_type *)realloc(
tdb->lockrecs, tdb->file->lockrecs,
sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1)); sizeof(*tdb->file->lockrecs) * (tdb->file->num_lockrecs+1));
if (new_lck == NULL) { if (new_lck == NULL) {
return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
"tdb_nest_lock:" "tdb_nest_lock:"
" unable to allocate %zu lock struct", " unable to allocate %zu lock struct",
tdb->num_lockrecs + 1); tdb->file->num_lockrecs + 1);
} }
tdb->lockrecs = new_lck; tdb->file->lockrecs = new_lck;
/* Since fcntl locks don't nest, we do a lock for the first one, /* Since fcntl locks don't nest, we do a lock for the first one,
and simply bump the count for future ones */ and simply bump the count for future ones */
...@@ -330,7 +330,7 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, ...@@ -330,7 +330,7 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
/* First time we grab a lock, perhaps someone died in commit? */ /* First time we grab a lock, perhaps someone died in commit? */
if (!(flags & TDB_LOCK_NOCHECK) if (!(flags & TDB_LOCK_NOCHECK)
&& tdb->num_lockrecs == 0) { && tdb->file->num_lockrecs == 0) {
tdb_bool_err berr = tdb_needs_recovery(tdb); tdb_bool_err berr = tdb_needs_recovery(tdb);
if (berr != false) { if (berr != false) {
tdb_brunlock(tdb, ltype, offset, 1); tdb_brunlock(tdb, ltype, offset, 1);
...@@ -348,10 +348,10 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, ...@@ -348,10 +348,10 @@ static enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
} }
} }
tdb->lockrecs[tdb->num_lockrecs].off = offset; tdb->file->lockrecs[tdb->file->num_lockrecs].off = offset;
tdb->lockrecs[tdb->num_lockrecs].count = 1; tdb->file->lockrecs[tdb->file->num_lockrecs].count = 1;
tdb->lockrecs[tdb->num_lockrecs].ltype = ltype; tdb->file->lockrecs[tdb->file->num_lockrecs].ltype = ltype;
tdb->num_lockrecs++; tdb->file->num_lockrecs++;
return TDB_SUCCESS; return TDB_SUCCESS;
} }
...@@ -389,7 +389,7 @@ static enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb, ...@@ -389,7 +389,7 @@ static enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
* Shrink the array by overwriting the element just unlocked with the * Shrink the array by overwriting the element just unlocked with the
* last array element. * last array element.
*/ */
*lck = tdb->lockrecs[--tdb->num_lockrecs]; *lck = tdb->file->lockrecs[--tdb->file->num_lockrecs];
return ecode; return ecode;
} }
...@@ -452,17 +452,18 @@ enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype, ...@@ -452,17 +452,18 @@ enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
enum TDB_ERROR ecode; enum TDB_ERROR ecode;
tdb_bool_err berr; tdb_bool_err berr;
if (tdb->allrecord_lock.count if (tdb->file->allrecord_lock.count
&& (ltype == F_RDLCK || tdb->allrecord_lock.ltype == F_WRLCK)) { && (ltype == F_RDLCK
tdb->allrecord_lock.count++; || tdb->file->allrecord_lock.ltype == F_WRLCK)) {
tdb->file->allrecord_lock.count++;
return TDB_SUCCESS; return TDB_SUCCESS;
} }
if (tdb->allrecord_lock.count) { if (tdb->file->allrecord_lock.count) {
/* a global lock of a different type exists */ /* a global lock of a different type exists */
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_allrecord_lock: already have %s lock", "tdb_allrecord_lock: already have %s lock",
tdb->allrecord_lock.ltype == F_RDLCK tdb->file->allrecord_lock.ltype == F_RDLCK
? "read" : "write"); ? "read" : "write");
} }
...@@ -507,11 +508,11 @@ again: ...@@ -507,11 +508,11 @@ again:
return ecode; return ecode;
} }
tdb->allrecord_lock.count = 1; tdb->file->allrecord_lock.count = 1;
/* If it's upgradable, it's actually exclusive so we can treat /* If it's upgradable, it's actually exclusive so we can treat
* it as a write lock. */ * it as a write lock. */
tdb->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype; tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
tdb->allrecord_lock.off = upgradable; tdb->file->allrecord_lock.off = upgradable;
/* Now check for needing recovery. */ /* Now check for needing recovery. */
if (flags & TDB_LOCK_NOCHECK) if (flags & TDB_LOCK_NOCHECK)
...@@ -543,7 +544,8 @@ void tdb_unlock_open(struct tdb_context *tdb) ...@@ -543,7 +544,8 @@ void tdb_unlock_open(struct tdb_context *tdb)
bool tdb_has_open_lock(struct tdb_context *tdb) bool tdb_has_open_lock(struct tdb_context *tdb)
{ {
return find_nestlock(tdb, TDB_OPEN_LOCK) != NULL; return !(tdb->flags & TDB_NOLOCK)
&& find_nestlock(tdb, TDB_OPEN_LOCK) != NULL;
} }
enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype) enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype)
...@@ -561,15 +563,15 @@ void tdb_unlock_expand(struct tdb_context *tdb, int ltype) ...@@ -561,15 +563,15 @@ void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
/* unlock entire db */ /* unlock entire db */
void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype) void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
{ {
if (tdb->allrecord_lock.count == 0) { if (tdb->file->allrecord_lock.count == 0) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_allrecord_unlock: not locked!"); "tdb_allrecord_unlock: not locked!");
return; return;
} }
/* Upgradable locks are marked as write locks. */ /* Upgradable locks are marked as write locks. */
if (tdb->allrecord_lock.ltype != ltype if (tdb->file->allrecord_lock.ltype != ltype
&& (!tdb->allrecord_lock.off || ltype != F_RDLCK)) { && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_allrecord_unlock: have %s lock", "tdb_allrecord_unlock: have %s lock",
tdb->allrecord_lock.ltype == F_RDLCK tdb->allrecord_lock.ltype == F_RDLCK
...@@ -577,13 +579,13 @@ void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype) ...@@ -577,13 +579,13 @@ void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
return; return;
} }
if (tdb->allrecord_lock.count > 1) { if (tdb->file->allrecord_lock.count > 1) {
tdb->allrecord_lock.count--; tdb->file->allrecord_lock.count--;
return; return;
} }
tdb->allrecord_lock.count = 0; tdb->file->allrecord_lock.count = 0;
tdb->allrecord_lock.ltype = 0; tdb->file->allrecord_lock.ltype = 0;
tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0); tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, 0);
} }
...@@ -597,10 +599,10 @@ bool tdb_has_hash_locks(struct tdb_context *tdb) ...@@ -597,10 +599,10 @@ bool tdb_has_hash_locks(struct tdb_context *tdb)
{ {
unsigned int i; unsigned int i;
for (i=0; i<tdb->num_lockrecs; i++) { for (i=0; i<tdb->file->num_lockrecs; i++) {
if (tdb->lockrecs[i].off >= TDB_HASH_LOCK_START if (tdb->file->lockrecs[i].off >= TDB_HASH_LOCK_START
&& tdb->lockrecs[i].off < (TDB_HASH_LOCK_START && tdb->file->lockrecs[i].off < (TDB_HASH_LOCK_START
+ TDB_HASH_LOCK_RANGE)) + TDB_HASH_LOCK_RANGE))
return true; return true;
} }
return false; return false;
...@@ -610,8 +612,11 @@ static bool tdb_has_free_lock(struct tdb_context *tdb) ...@@ -610,8 +612,11 @@ static bool tdb_has_free_lock(struct tdb_context *tdb)
{ {
unsigned int i; unsigned int i;
for (i=0; i<tdb->num_lockrecs; i++) { if (tdb->flags & TDB_NOLOCK)
if (tdb->lockrecs[i].off return false;
for (i=0; i<tdb->file->num_lockrecs; i++) {
if (tdb->file->lockrecs[i].off
> TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE) > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE)
return true; return true;
} }
...@@ -628,16 +633,16 @@ enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb, ...@@ -628,16 +633,16 @@ enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
+ (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS)); + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
/* a allrecord lock allows us to avoid per chain locks */ /* a allrecord lock allows us to avoid per chain locks */
if (tdb->allrecord_lock.count && if (tdb->file->allrecord_lock.count &&
(ltype == tdb->allrecord_lock.ltype || ltype == F_RDLCK)) { (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
return TDB_SUCCESS; return TDB_SUCCESS;
} }
if (tdb->allrecord_lock.count) { if (tdb->file->allrecord_lock.count) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
"tdb_lock_hashes:" "tdb_lock_hashes:"
" already have %s allrecordlock", " already have %s allrecordlock",
tdb->allrecord_lock.ltype == F_RDLCK tdb->file->allrecord_lock.ltype == F_RDLCK
? "read" : "write"); ? "read" : "write");
} }
...@@ -662,9 +667,12 @@ enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb, ...@@ -662,9 +667,12 @@ enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
unsigned lock = TDB_HASH_LOCK_START unsigned lock = TDB_HASH_LOCK_START
+ (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS)); + (hash_lock >> (64 - TDB_HASH_LOCK_RANGE_BITS));
if (tdb->flags & TDB_NOLOCK)
return 0;
/* a allrecord lock allows us to avoid per chain locks */ /* a allrecord lock allows us to avoid per chain locks */
if (tdb->allrecord_lock.count) { if (tdb->file->allrecord_lock.count) {
if (tdb->allrecord_lock.ltype == F_RDLCK if (tdb->file->allrecord_lock.ltype == F_RDLCK
&& ltype == F_WRLCK) { && ltype == F_WRLCK) {
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_unlock_hashes RO allrecord!"); "tdb_unlock_hashes RO allrecord!");
...@@ -691,9 +699,12 @@ enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off, ...@@ -691,9 +699,12 @@ enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
{ {
assert(b_off >= sizeof(struct tdb_header)); assert(b_off >= sizeof(struct tdb_header));
if (tdb->flags & TDB_NOLOCK)
return 0;
/* a allrecord lock allows us to avoid per chain locks */ /* a allrecord lock allows us to avoid per chain locks */
if (tdb->allrecord_lock.count) { if (tdb->file->allrecord_lock.count) {
if (tdb->allrecord_lock.ltype == F_WRLCK) if (tdb->file->allrecord_lock.ltype == F_WRLCK)
return 0; return 0;
return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
"tdb_lock_free_bucket with" "tdb_lock_free_bucket with"
...@@ -713,15 +724,8 @@ enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off, ...@@ -713,15 +724,8 @@ enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off) void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off)
{ {
if (tdb->allrecord_lock.count) if (tdb->file->allrecord_lock.count)
return; return;
tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK); tdb_nest_unlock(tdb, free_lock_off(b_off), F_WRLCK);
} }
void tdb_lock_init(struct tdb_context *tdb)
{
tdb->num_lockrecs = 0;
tdb->lockrecs = NULL;
tdb->allrecord_lock.count = 0;
}
#include "private.h" #include "private.h"
/* all contexts, to ensure no double-opens (fcntl locks don't nest!) */ /* all lock info, to detect double-opens (fcntl file don't nest!) */
static struct tdb_context *tdbs = NULL; 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) { if (i->device == device && i->inode == ino) {
return true; break;
} }
} }
return i;
return false;
} }
static bool read_all(int fd, void *buf, size_t len) 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, ...@@ -143,19 +142,19 @@ static enum TDB_ERROR tdb_new_database(struct tdb_context *tdb,
memcpy(tdb->map_ptr, &newdb, tdb->map_size); memcpy(tdb->map_ptr, &newdb, tdb->map_size);
return TDB_SUCCESS; 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, return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_new_database:" "tdb_new_database:"
" failed to seek: %s", strerror(errno)); " 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, return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_new_database:" "tdb_new_database:"
" failed to truncate: %s", strerror(errno)); " 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 != sizeof(newdb)) {
if (rlen >= 0) if (rlen >= 0)
errno = ENOSPC; errno = ENOSPC;
...@@ -190,16 +189,15 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -190,16 +189,15 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->name = NULL; tdb->name = NULL;
tdb->map_ptr = NULL; tdb->map_ptr = NULL;
tdb->direct_access = 0; tdb->direct_access = 0;
tdb->fd = -1;
tdb->map_size = sizeof(struct tdb_header); tdb->map_size = sizeof(struct tdb_header);
tdb->flags = tdb_flags; tdb->flags = tdb_flags;
tdb->logfn = NULL; tdb->logfn = NULL;
tdb->transaction = NULL; tdb->transaction = NULL;
tdb->stats = NULL; tdb->stats = NULL;
tdb->access = NULL; tdb->access = NULL;
tdb->file = NULL;
tdb_hash_init(tdb); tdb_hash_init(tdb);
tdb_io_init(tdb); tdb_io_init(tdb);
tdb_lock_init(tdb);
while (attr) { while (attr) {
switch (attr->base.attr) { switch (attr->base.attr) {
...@@ -266,18 +264,56 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -266,18 +264,56 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
return tdb; return tdb;
} }
if ((tdb->fd = open(name, open_flags, mode)) == -1) { if (stat(name, &st) != -1)
/* errno set by open(2) */ tdb->file = find_file(st.st_dev, st.st_ino);
saved_errno = errno;
ecode = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, 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", "tdb_open: could not open file %s: %s",
name, strerror(errno)); 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 */ tdb->file->next = files;
v = fcntl(tdb->fd, F_GETFD, 0); tdb->file->num_lockrecs = 0;
fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC); 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 */ /* ensure there is only one process initialising at once */
ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK); 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, ...@@ -286,7 +322,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
} }
/* If they used O_TRUNC, read will return 0. */ /* 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)) { if (rlen == 0 && (open_flags & O_CREAT)) {
ecode = tdb_new_database(tdb, seed, &hdr); ecode = tdb_new_database(tdb, seed, &hdr);
if (ecode != TDB_SUCCESS) { if (ecode != TDB_SUCCESS) {
...@@ -330,24 +366,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -330,24 +366,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
goto fail; 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); tdb->name = strdup(name);
if (!tdb->name) { if (!tdb->name) {
ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, 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, ...@@ -365,8 +383,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
goto fail; goto fail;
} }
tdb->device = st.st_dev;
tdb->inode = st.st_ino;
tdb_unlock_open(tdb); tdb_unlock_open(tdb);
/* This make sure we have current map_size and mmap. */ /* 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, ...@@ -390,8 +406,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
goto fail; goto fail;
} }
tdb->next = tdbs; /* Add to linked list. */
tdbs = tdb; files = tdb->file;
return tdb; return tdb;
fail: fail:
...@@ -426,13 +442,16 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -426,13 +442,16 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
} else } else
tdb_munmap(tdb); tdb_munmap(tdb);
} }
free(tdb->lockrecs);
free((char *)tdb->name); free((char *)tdb->name);
if (tdb->fd != -1) if (tdb->file) {
if (close(tdb->fd) != 0) if (close(tdb->file->fd) != 0)
tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, 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)); " on error: %s", strerror(errno));
free(tdb->file->lockrecs);
free(tdb->file);
}
free(tdb); free(tdb);
errno = saved_errno; errno = saved_errno;
return NULL; return NULL;
...@@ -440,7 +459,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -440,7 +459,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
int tdb_close(struct tdb_context *tdb) int tdb_close(struct tdb_context *tdb)
{ {
struct tdb_context **i;
int ret = 0; int ret = 0;
tdb_trace(tdb, "tdb_close"); tdb_trace(tdb, "tdb_close");
...@@ -456,18 +474,19 @@ int tdb_close(struct tdb_context *tdb) ...@@ -456,18 +474,19 @@ int tdb_close(struct tdb_context *tdb)
tdb_munmap(tdb); tdb_munmap(tdb);
} }
free((char *)tdb->name); free((char *)tdb->name);
if (tdb->fd != -1) { if (tdb->file) {
ret = close(tdb->fd); struct tdb_file **i;
tdb->fd = -1; ret = close(tdb->file->fd);
}
free(tdb->lockrecs); /* Remove from files list */
for (i = &files; *i; i = &(*i)->next) {
/* Remove from contexts list */ if (*i == tdb->file) {
for (i = &tdbs; *i; i = &(*i)->next) { *i = tdb->file->next;
if (*i == tdb) { break;
*i = tdb->next; }
break;
} }
free(tdb->file->lockrecs);
free(tdb->file);
} }
#ifdef TDB_TRACE #ifdef TDB_TRACE
......
...@@ -315,6 +315,23 @@ struct tdb_access_hdr { ...@@ -315,6 +315,23 @@ struct tdb_access_hdr {
bool convert; 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 { struct tdb_context {
/* Filename of the database. */ /* Filename of the database. */
const char *name; const char *name;
...@@ -325,9 +342,6 @@ struct tdb_context { ...@@ -325,9 +342,6 @@ struct tdb_context {
/* Are we accessing directly? (debugging check). */ /* Are we accessing directly? (debugging check). */
int direct_access; int direct_access;
/* Open file descriptor (undefined for TDB_INTERNAL). */
int fd;
/* How much space has been mapped (<= current file size) */ /* How much space has been mapped (<= current file size) */
tdb_len_t map_size; tdb_len_t map_size;
...@@ -365,20 +379,13 @@ struct tdb_context { ...@@ -365,20 +379,13 @@ struct tdb_context {
/* IO methods: changes for transactions. */ /* IO methods: changes for transactions. */
const struct tdb_methods *methods; 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; struct tdb_attribute_stats *stats;
/* Direct access information */ /* Direct access information */
struct tdb_access_hdr *access; struct tdb_access_hdr *access;
/* Single list of all TDBs, to avoid multiple opens. */ /* The actual file information */
struct tdb_context *next; struct tdb_file *file;
dev_t device;
ino_t inode;
}; };
struct tdb_methods { struct tdb_methods {
...@@ -521,8 +528,6 @@ void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val); ...@@ -521,8 +528,6 @@ void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val);
} while (0) } while (0)
/* lock.c: */ /* lock.c: */
void tdb_lock_init(struct tdb_context *tdb);
/* Lock/unlock a range of hashes. */ /* Lock/unlock a range of hashes. */
enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb, enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
tdb_off_t hash_lock, tdb_len_t hash_range, tdb_off_t hash_lock, tdb_len_t hash_range,
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include <stdbool.h> #include <stdbool.h>
/* FIXME: Check these! */ /* 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_OPEN "open.c", 43, FAILTEST_OPEN
#define URANDOM_READ "open.c", 23, FAILTEST_READ #define URANDOM_READ "open.c", 23, FAILTEST_READ
......
...@@ -94,7 +94,7 @@ int main(int argc, char *argv[]) ...@@ -94,7 +94,7 @@ int main(int argc, char *argv[])
/* Lock and coalesce. */ /* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); 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(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) ok1(free_record_length(tdb, layout->elem[1].base.off)
== 1024 + sizeof(struct tdb_used_record) + 2048); == 1024 + sizeof(struct tdb_used_record) + 2048);
ok1(tdb_check(tdb, NULL, NULL) == 0); ok1(tdb_check(tdb, NULL, NULL) == 0);
...@@ -117,7 +117,7 @@ int main(int argc, char *argv[]) ...@@ -117,7 +117,7 @@ int main(int argc, char *argv[])
/* Lock and coalesce. */ /* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); 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(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) ok1(free_record_length(tdb, layout->elem[1].base.off)
== 1024 + sizeof(struct tdb_used_record) + 512); == 1024 + sizeof(struct tdb_used_record) + 512);
ok1(tdb_check(tdb, NULL, NULL) == 0); ok1(tdb_check(tdb, NULL, NULL) == 0);
...@@ -141,7 +141,8 @@ int main(int argc, char *argv[]) ...@@ -141,7 +141,8 @@ int main(int argc, char *argv[])
/* Lock and coalesce. */ /* Lock and coalesce. */
ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); 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(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) ok1(free_record_length(tdb, layout->elem[1].base.off)
== 1024 + sizeof(struct tdb_used_record) + 512 == 1024 + sizeof(struct tdb_used_record) + 512
+ sizeof(struct tdb_used_record) + 256); + sizeof(struct tdb_used_record) + 256);
......
...@@ -60,9 +60,9 @@ int main(int argc, char *argv[]) ...@@ -60,9 +60,9 @@ int main(int argc, char *argv[])
ok1(h.hlock_start == 0); ok1(h.hlock_start == 0);
ok1(h.hlock_range == ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS))); 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) 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 */ /* FIXME: Check lock length */
/* Allocate a new record. */ /* Allocate a new record. */
...@@ -101,9 +101,9 @@ int main(int argc, char *argv[]) ...@@ -101,9 +101,9 @@ int main(int argc, char *argv[])
ok1(h.hlock_start == 0); ok1(h.hlock_start == 0);
ok1(h.hlock_range == ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS))); 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) 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 */ /* FIXME: Check lock length */
ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, ok1(tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
...@@ -127,9 +127,9 @@ int main(int argc, char *argv[]) ...@@ -127,9 +127,9 @@ int main(int argc, char *argv[])
ok1(h.hlock_start == 0); ok1(h.hlock_start == 0);
ok1(h.hlock_range == ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS))); 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) 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 */ /* FIXME: Check lock length */
/* Make it expand 0'th bucket. */ /* Make it expand 0'th bucket. */
...@@ -165,9 +165,9 @@ int main(int argc, char *argv[]) ...@@ -165,9 +165,9 @@ int main(int argc, char *argv[])
ok1(h.hlock_start == 0); ok1(h.hlock_start == 0);
ok1(h.hlock_range == ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS))); 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) 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 */ /* FIXME: Check lock length */
/* Simple delete should work. */ /* Simple delete should work. */
...@@ -196,9 +196,9 @@ int main(int argc, char *argv[]) ...@@ -196,9 +196,9 @@ int main(int argc, char *argv[])
ok1(h.hlock_start == 0); ok1(h.hlock_start == 0);
ok1(h.hlock_range == ok1(h.hlock_range ==
1ULL << (64-(TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS))); 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) 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 */ /* FIXME: Check lock length */
ok1(expand_group(tdb, &h) == 0); ok1(expand_group(tdb, &h) == 0);
......
...@@ -180,7 +180,8 @@ int main(int argc, char *argv[]) ...@@ -180,7 +180,8 @@ int main(int argc, char *argv[])
/* Check mixed bitpattern. */ /* Check mixed bitpattern. */
test_val(tdb, 0x123456789ABCDEF0ULL); 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); tdb_close(tdb);
/* Deleting these entries in the db gave problems. */ /* Deleting these entries in the db gave problems. */
......
...@@ -69,7 +69,8 @@ int main(int argc, char *argv[]) ...@@ -69,7 +69,8 @@ int main(int argc, char *argv[])
moves++; moves++;
oldoff = newoff; 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... */ /* We should increase by 50% each time... */
ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves); ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
tdb_close(tdb); tdb_close(tdb);
...@@ -100,7 +101,8 @@ int main(int argc, char *argv[]) ...@@ -100,7 +101,8 @@ int main(int argc, char *argv[])
moves++; moves++;
oldoff = newoff; 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... */ /* We should increase by 50% each time... */
ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves); ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
tdb_close(tdb); tdb_close(tdb);
...@@ -122,7 +124,8 @@ int main(int argc, char *argv[]) ...@@ -122,7 +124,8 @@ int main(int argc, char *argv[])
ok1(data.dsize == MAX_SIZE); ok1(data.dsize == MAX_SIZE);
ok1(memcmp(data.dptr, buffer, data.dsize) == 0); ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
free(data.dptr); 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); tdb_close(tdb);
} }
......
...@@ -43,7 +43,8 @@ int main(int argc, char *argv[]) ...@@ -43,7 +43,8 @@ int main(int argc, char *argv[])
/* Cancelling a transaction means no store */ /* Cancelling a transaction means no store */
tdb_transaction_cancel(tdb); 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_check(tdb, NULL, NULL) == 0);
ok1(tdb_fetch(tdb, key, &data) == TDB_ERR_NOEXIST); ok1(tdb_fetch(tdb, key, &data) == TDB_ERR_NOEXIST);
...@@ -57,7 +58,8 @@ int main(int argc, char *argv[]) ...@@ -57,7 +58,8 @@ int main(int argc, char *argv[])
ok1(memcmp(data.dptr, buffer, data.dsize) == 0); ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
free(data.dptr); free(data.dptr);
ok1(tdb_transaction_commit(tdb) == 0); 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_check(tdb, NULL, NULL) == 0);
ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS); ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
ok1(data.dsize == 1000); ok1(data.dsize == 1000);
......
...@@ -153,7 +153,7 @@ reset: ...@@ -153,7 +153,7 @@ reset:
if (setjmp(jmpbuf) != 0) { if (setjmp(jmpbuf) != 0) {
/* We're partway through. Simulate our death. */ /* We're partway through. Simulate our death. */
close(tdb->fd); close(tdb->file->fd);
forget_locking(); forget_locking();
in_transaction = false; in_transaction = false;
......
...@@ -51,7 +51,7 @@ int main(int argc, char *argv[]) ...@@ -51,7 +51,7 @@ int main(int argc, char *argv[])
O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr); O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
ok1(external_agent_operation(agent, OPEN, filename) == SUCCESS); 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. */ /* Do a traverse. */
ok1(tdb_traverse(tdb, NULL, NULL) == i); ok1(tdb_traverse(tdb, NULL, NULL) == i);
......
...@@ -430,7 +430,7 @@ static enum TDB_ERROR transaction_sync(struct tdb_context *tdb, ...@@ -430,7 +430,7 @@ static enum TDB_ERROR transaction_sync(struct tdb_context *tdb,
return TDB_SUCCESS; return TDB_SUCCESS;
} }
if (fsync(tdb->fd) != 0) { if (fsync(tdb->file->fd) != 0) {
return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR, return tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_transaction: fsync failed: %s", "tdb_transaction: fsync failed: %s",
strerror(errno)); strerror(errno));
...@@ -495,8 +495,8 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb) ...@@ -495,8 +495,8 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
} }
} }
if (tdb->allrecord_lock.count) if (tdb->file->allrecord_lock.count)
tdb_allrecord_unlock(tdb, tdb->allrecord_lock.ltype); tdb_allrecord_unlock(tdb, tdb->file->allrecord_lock.ltype);
/* restore the normal io methods */ /* restore the normal io methods */
tdb->methods = tdb->transaction->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