Commit bcf7916c authored by Rusty Russell's avatar Rusty Russell

Wean off TDB_ERRCODE.

It was a regrettable hack to reduce line count in tdb; in fact it caused confusion as can be seen in this patch.
In particular, ecode now needs to be set before TDB_LOG.
Also, we should never set errno, as io.c was doing.
parent f9757c15
...@@ -54,7 +54,7 @@ int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct ...@@ -54,7 +54,7 @@ int tdb_rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct
tdb->ecode = TDB_ERR_CORRUPT; tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_rec_free_read bad magic 0x%x at offset=%d\n", TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_rec_free_read bad magic 0x%x at offset=%d\n",
rec->magic, off)); rec->magic, off));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1); return -1;
} }
if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0) if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
return -1; return -1;
...@@ -78,8 +78,9 @@ static int remove_from_freelist(struct tdb_context *tdb, tdb_off_t off, tdb_off_ ...@@ -78,8 +78,9 @@ static int remove_from_freelist(struct tdb_context *tdb, tdb_off_t off, tdb_off_
/* Follow chain (next offset is at start of record) */ /* Follow chain (next offset is at start of record) */
last_ptr = i; last_ptr = i;
} }
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"remove_from_freelist: not on list at off=%d\n", off)); TDB_LOG((tdb, TDB_DEBUG_FATAL,"remove_from_freelist: not on list at off=%d\n", off));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1); return -1;
} }
#endif #endif
......
...@@ -67,7 +67,8 @@ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries) ...@@ -67,7 +67,8 @@ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
/* Store the FREELIST_TOP record. */ /* Store the FREELIST_TOP record. */
if (seen_insert(mem_tdb, last_ptr) == -1) { if (seen_insert(mem_tdb, last_ptr) == -1) {
ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1); tdb->ecode = TDB_ERR_CORRUPT;
ret = -1;
goto fail; goto fail;
} }
...@@ -83,7 +84,8 @@ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries) ...@@ -83,7 +84,8 @@ int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
be corrupt. */ be corrupt. */
if (seen_insert(mem_tdb, rec_ptr)) { if (seen_insert(mem_tdb, rec_ptr)) {
ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1); tdb->ecode = TDB_ERR_CORRUPT;
ret = -1;
goto fail; goto fail;
} }
......
...@@ -47,11 +47,12 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe) ...@@ -47,11 +47,12 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond internal malloc size %d\n", TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond internal malloc size %d\n",
(int)len, (int)tdb->map_size)); (int)len, (int)tdb->map_size));
} }
return TDB_ERRCODE(TDB_ERR_IO, -1); return -1;
} }
if (fstat(tdb->fd, &st) == -1) { if (fstat(tdb->fd, &st) == -1) {
return TDB_ERRCODE(TDB_ERR_IO, -1); tdb->ecode = TDB_ERR_IO;
return -1;
} }
if (st.st_size < (size_t)len) { if (st.st_size < (size_t)len) {
...@@ -61,12 +62,14 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe) ...@@ -61,12 +62,14 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond eof at %d\n", TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_oob len %d beyond eof at %d\n",
(int)len, (int)st.st_size)); (int)len, (int)st.st_size));
} }
return TDB_ERRCODE(TDB_ERR_IO, -1); return -1;
} }
/* Unmap, update size, remap */ /* Unmap, update size, remap */
if (tdb_munmap(tdb) == -1) if (tdb_munmap(tdb) == -1) {
return TDB_ERRCODE(TDB_ERR_IO, -1); tdb->ecode = TDB_ERR_IO;
return -1;
}
tdb->map_size = st.st_size; tdb->map_size = st.st_size;
tdb_mmap(tdb); tdb_mmap(tdb);
return 0; return 0;
...@@ -94,27 +97,27 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -94,27 +97,27 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
ssize_t written = pwrite(tdb->fd, buf, len, off); ssize_t written = pwrite(tdb->fd, buf, len, off);
if ((written != (ssize_t)len) && (written != -1)) { if ((written != (ssize_t)len) && (written != -1)) {
/* try once more */ /* try once more */
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: wrote only " TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: wrote only "
"%d of %d bytes at %d, trying once more\n", "%d of %d bytes at %d, trying once more\n",
(int)written, len, off)); (int)written, len, off));
errno = ENOSPC; written = pwrite(tdb->fd, (const char *)buf+written,
written = pwrite(tdb->fd, (const void *)((const char *)buf+written),
len-written, len-written,
off+written); off+written);
} }
if (written == -1) { if (written == -1) {
/* Ensure ecode is set for log fn. */ /* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_IO; tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_write failed at %d " TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_write failed at %d "
"len=%d (%s)\n", off, len, strerror(errno))); "len=%d (%s)\n", off, len, strerror(errno)));
return TDB_ERRCODE(TDB_ERR_IO, -1); return -1;
} else if (written != (ssize_t)len) { } else if (written != (ssize_t)len) {
tdb->ecode = TDB_ERR_IO;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: failed to " TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_write: failed to "
"write %d bytes at %d in two attempts\n", "write %d bytes at %d in two attempts\n",
len, off)); len, off));
errno = ENOSPC; return -1;
return TDB_ERRCODE(TDB_ERR_IO, -1); }
}
} }
return 0; return 0;
} }
...@@ -148,7 +151,7 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf, ...@@ -148,7 +151,7 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
"len=%d ret=%d (%s) map_size=%d\n", "len=%d ret=%d (%s) map_size=%d\n",
(int)off, (int)len, (int)ret, strerror(errno), (int)off, (int)len, (int)ret, strerror(errno),
(int)tdb->map_size)); (int)tdb->map_size));
return TDB_ERRCODE(TDB_ERR_IO, -1); return -1;
} }
} }
if (cv) { if (cv) {
...@@ -388,7 +391,7 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len ...@@ -388,7 +391,7 @@ unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len
tdb->ecode = TDB_ERR_OOM; tdb->ecode = TDB_ERR_OOM;
TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_alloc_read malloc failed len=%d (%s)\n", TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_alloc_read malloc failed len=%d (%s)\n",
len, strerror(errno))); len, strerror(errno)));
return TDB_ERRCODE(TDB_ERR_OOM, buf); return NULL;
} }
if (tdb->methods->tdb_read(tdb, offset, buf, len, 0) == -1) { if (tdb->methods->tdb_read(tdb, offset, buf, len, 0) == -1) {
SAFE_FREE(buf); SAFE_FREE(buf);
...@@ -440,7 +443,7 @@ int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct * ...@@ -440,7 +443,7 @@ int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *
/* Ensure ecode is set for log fn. */ /* Ensure ecode is set for log fn. */
tdb->ecode = TDB_ERR_CORRUPT; tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset)); TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec->magic, offset));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1); return -1;
} }
return tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0); return tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0);
} }
......
...@@ -84,7 +84,7 @@ int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset, ...@@ -84,7 +84,7 @@ int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset,
TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n", TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
tdb->fd, offset, rw_type, lck_type, (int)len)); tdb->fd, offset, rw_type, lck_type, (int)len));
} }
return TDB_ERRCODE(TDB_ERR_LOCK, -1); return -1;
} }
return 0; return 0;
} }
...@@ -133,10 +133,12 @@ static int _tdb_lock(struct tdb_context *tdb, int list, int ltype, int op) ...@@ -133,10 +133,12 @@ static int _tdb_lock(struct tdb_context *tdb, int list, int ltype, int op)
} }
if (tdb->global_lock.count) { if (tdb->global_lock.count) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1); tdb->ecode = TDB_ERR_LOCK;
return -1;
} }
if (list < -1 || list >= (int)tdb->header.hash_size) { if (list < -1 || list >= (int)tdb->header.hash_size) {
tdb->ecode = TDB_ERR_LOCK;
TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n", TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
list, ltype)); list, ltype));
return -1; return -1;
...@@ -228,7 +230,8 @@ int tdb_unlock(struct tdb_context *tdb, int list, int ltype) ...@@ -228,7 +230,8 @@ int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
} }
if (tdb->global_lock.count) { if (tdb->global_lock.count) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1); tdb->ecode = TDB_ERR_LOCK;
return -1;
} }
if (tdb->flags & TDB_NOLOCK) if (tdb->flags & TDB_NOLOCK)
...@@ -350,8 +353,10 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op) ...@@ -350,8 +353,10 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
ltype &= ~TDB_MARK_LOCK; ltype &= ~TDB_MARK_LOCK;
/* There are no locks on read-only dbs */ /* There are no locks on read-only dbs */
if (tdb->read_only || tdb->traverse_read) if (tdb->read_only || tdb->traverse_read) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1); tdb->ecode = TDB_ERR_LOCK;
return -1;
}
if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) { if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
tdb->global_lock.count++; tdb->global_lock.count++;
...@@ -360,12 +365,14 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op) ...@@ -360,12 +365,14 @@ static int _tdb_lockall(struct tdb_context *tdb, int ltype, int op)
if (tdb->global_lock.count) { if (tdb->global_lock.count) {
/* a global lock of a different type exists */ /* a global lock of a different type exists */
return TDB_ERRCODE(TDB_ERR_LOCK, -1); tdb->ecode = TDB_ERR_LOCK;
return -1;
} }
if (tdb->num_locks != 0) { if (tdb->num_locks != 0) {
/* can't combine global and chain locks */ /* can't combine global and chain locks */
return TDB_ERRCODE(TDB_ERR_LOCK, -1); tdb->ecode = TDB_ERR_LOCK;
return -1;
} }
if (!mark_lock && if (!mark_lock &&
...@@ -394,11 +401,13 @@ static int _tdb_unlockall(struct tdb_context *tdb, int ltype) ...@@ -394,11 +401,13 @@ static int _tdb_unlockall(struct tdb_context *tdb, int ltype)
/* There are no locks on read-only dbs */ /* There are no locks on read-only dbs */
if (tdb->read_only || tdb->traverse_read) { if (tdb->read_only || tdb->traverse_read) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1); tdb->ecode = TDB_ERR_LOCK;
return -1;
} }
if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) { if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
return TDB_ERRCODE(TDB_ERR_LOCK, -1); tdb->ecode = TDB_ERR_LOCK;
return -1;
} }
if (tdb->global_lock.count > 1) { if (tdb->global_lock.count > 1) {
......
...@@ -55,8 +55,10 @@ static int tdb_new_database(struct tdb_context *tdb, int hash_size) ...@@ -55,8 +55,10 @@ static int tdb_new_database(struct tdb_context *tdb, int hash_size)
/* We make it up in memory, then write it out if not internal */ /* We make it up in memory, then write it out if not internal */
size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t); size = sizeof(struct tdb_header) + (hash_size+1)*sizeof(tdb_off_t);
if (!(newdb = (struct tdb_header *)calloc(size, 1))) if (!(newdb = (struct tdb_header *)calloc(size, 1))) {
return TDB_ERRCODE(TDB_ERR_OOM, -1); tdb->ecode = TDB_ERR_OOM;
return -1;
}
/* Fill in the header */ /* Fill in the header */
newdb->version = TDB_VERSION; newdb->version = TDB_VERSION;
......
...@@ -98,12 +98,14 @@ static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, ...@@ -98,12 +98,14 @@ static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
} }
/* detect tight infinite loop */ /* detect tight infinite loop */
if (rec_ptr == r->next) { if (rec_ptr == r->next) {
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n")); TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n"));
return TDB_ERRCODE(TDB_ERR_CORRUPT, 0); return 0;
} }
rec_ptr = r->next; rec_ptr = r->next;
} }
return TDB_ERRCODE(TDB_ERR_NOEXIST, 0); tdb->ecode = TDB_ERR_NOEXIST;
return 0;
} }
/* As tdb_find, but if you succeed, keep the lock */ /* As tdb_find, but if you succeed, keep the lock */
...@@ -217,7 +219,8 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key, ...@@ -217,7 +219,8 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) { if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, tdb_trace_1rec_ret(tdb, "tdb_parse_record", key,
-TDB_ERR_NOEXIST); -TDB_ERR_NOEXIST);
return TDB_ERRCODE(TDB_ERR_NOEXIST, 0); tdb->ecode = TDB_ERR_NOEXIST;
return 0;
} }
tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0); tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
......
...@@ -57,8 +57,6 @@ extern "C" { ...@@ -57,8 +57,6 @@ extern "C" {
#define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */ #define TDB_VOLATILE 256 /* Activate the per-hashchain freelist, default 5 */
#define TDB_NO_NESTING 512 /* Dont allow transaction nesting */ #define TDB_NO_NESTING 512 /* Dont allow transaction nesting */
#define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
/* error codes */ /* error codes */
enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT, TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
......
...@@ -385,7 +385,8 @@ static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, int probe) ...@@ -385,7 +385,8 @@ static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
if (len <= tdb->map_size) { if (len <= tdb->map_size) {
return 0; return 0;
} }
return TDB_ERRCODE(TDB_ERR_IO, -1); tdb->ecode = TDB_ERR_IO;
return -1;
} }
/* /*
......
...@@ -121,7 +121,8 @@ static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tloc ...@@ -121,7 +121,8 @@ static int tdb_next_lock(struct tdb_context *tdb, struct tdb_traverse_lock *tloc
want_next = 0; want_next = 0;
} }
/* We finished iteration without finding anything */ /* We finished iteration without finding anything */
return TDB_ERRCODE(TDB_SUCCESS, 0); tdb->ecode = TDB_SUCCESS;
return 0;
fail: fail:
tlock->off = 0; tlock->off = 0;
......
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