Commit 4e185ad8 authored by Rusty Russell's avatar Rusty Russell

tdb2: clean up logging

Logged errors should always set tdb->ecode before they are called, and
there's little reason to have a sprintf-style logging function since
we can do the formatting internally.

Change the tdb_log attribute to just take a "const char *", and create
a new tdb_logerr() helper which sets ecode and calls it.  As a bonus,
mark it COLD so the compiler can optimize appropriately knowing that
it's unlikely to be invoked.
parent 57680260
...@@ -43,16 +43,16 @@ static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery) ...@@ -43,16 +43,16 @@ static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery)
hash_test = TDB_HASH_MAGIC; hash_test = TDB_HASH_MAGIC;
hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test)); hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
if (hdr.hash_test != hash_test) { if (hdr.hash_test != hash_test) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"check: hash test %llu should be %llu\n", "check: hash test %llu should be %llu",
(long long)hdr.hash_test, (long long)hdr.hash_test,
(long long)hash_test); (long long)hash_test);
return false; return false;
} }
if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) { if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"check: bad magic '%.*s'\n", "check: bad magic '%.*s'",
(unsigned)sizeof(hdr.magic_food), hdr.magic_food); (unsigned)sizeof(hdr.magic_food), hdr.magic_food);
return false; return false;
} }
...@@ -60,8 +60,8 @@ static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery) ...@@ -60,8 +60,8 @@ static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery)
*recovery = hdr.recovery; *recovery = hdr.recovery;
if (*recovery) { if (*recovery) {
if (*recovery < sizeof(hdr) || *recovery > tdb->map_size) { if (*recovery < sizeof(hdr) || *recovery > tdb->map_size) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: invalid recovery offset %zu\n", "tdb_check: invalid recovery offset %zu",
(size_t)*recovery); (size_t)*recovery);
return false; return false;
} }
...@@ -98,21 +98,22 @@ static bool check_hash_record(struct tdb_context *tdb, ...@@ -98,21 +98,22 @@ static bool check_hash_record(struct tdb_context *tdb,
if (rec_data_length(&rec) if (rec_data_length(&rec)
!= sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) { != sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: Bad hash table length %llu vs %llu\n", "tdb_check: Bad hash table length %llu vs %llu",
(long long)rec_data_length(&rec), (long long)rec_data_length(&rec),
(long long)sizeof(tdb_off_t)<<TDB_SUBLEVEL_HASH_BITS); (long long)sizeof(tdb_off_t)
<< TDB_SUBLEVEL_HASH_BITS);
return false; return false;
} }
if (rec_key_length(&rec) != 0) { if (rec_key_length(&rec) != 0) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: Bad hash table key length %llu\n", "tdb_check: Bad hash table key length %llu",
(long long)rec_key_length(&rec)); (long long)rec_key_length(&rec));
return false; return false;
} }
if (rec_hash(&rec) != 0) { if (rec_hash(&rec) != 0) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: Bad hash table hash value %llu\n", "tdb_check: Bad hash table hash value %llu",
(long long)rec_hash(&rec)); (long long)rec_hash(&rec));
return false; return false;
} }
...@@ -172,10 +173,10 @@ static bool check_hash_tree(struct tdb_context *tdb, ...@@ -172,10 +173,10 @@ static bool check_hash_tree(struct tdb_context *tdb,
off = group[b] & TDB_OFF_MASK; off = group[b] & TDB_OFF_MASK;
p = asearch(&off, used, num_used, off_cmp); p = asearch(&off, used, num_used, off_cmp);
if (!p) { if (!p) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"tdb_check: Invalid offset %llu " "tdb_check: Invalid offset %llu "
"in hash\n", "in hash", (long long)off);
(long long)off);
goto fail; goto fail;
} }
/* Mark it invalid. */ /* Mark it invalid. */
...@@ -206,17 +207,19 @@ static bool check_hash_tree(struct tdb_context *tdb, ...@@ -206,17 +207,19 @@ static bool check_hash_tree(struct tdb_context *tdb,
used_bits = 0; used_bits = 0;
if (get_bits(h, hprefix_bits, &used_bits) != hprefix if (get_bits(h, hprefix_bits, &used_bits) != hprefix
&& hprefix_bits) { && hprefix_bits) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"check: bad hash placement" "check: bad hash placement"
" 0x%llx vs 0x%llx\n", " 0x%llx vs 0x%llx",
(long long)h, (long long)hprefix); (long long)h, (long long)hprefix);
goto fail; goto fail;
} }
/* Does it belong in this group? */ /* Does it belong in this group? */
if (get_bits(h, group_bits, &used_bits) != g) { if (get_bits(h, group_bits, &used_bits) != g) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
"check: bad group %llu vs %u\n", TDB_DEBUG_ERROR,
"check: bad group %llu vs %u",
(long long)h, g); (long long)h, g);
goto fail; goto fail;
} }
...@@ -226,8 +229,9 @@ static bool check_hash_tree(struct tdb_context *tdb, ...@@ -226,8 +229,9 @@ static bool check_hash_tree(struct tdb_context *tdb,
if (get_bits(h, TDB_HASH_GROUP_BITS, &used_bits) if (get_bits(h, TDB_HASH_GROUP_BITS, &used_bits)
!= bucket) { != bucket) {
used_bits -= TDB_HASH_GROUP_BITS; used_bits -= TDB_HASH_GROUP_BITS;
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
"check: bad bucket %u vs %u\n", TDB_DEBUG_ERROR,
"check: bad bucket %u vs %u",
(unsigned)get_bits(h, (unsigned)get_bits(h,
TDB_HASH_GROUP_BITS, TDB_HASH_GROUP_BITS,
&used_bits), &used_bits),
...@@ -241,23 +245,24 @@ static bool check_hash_tree(struct tdb_context *tdb, ...@@ -241,23 +245,24 @@ static bool check_hash_tree(struct tdb_context *tdb,
i != b; i != b;
i = (i + 1) % (1 << TDB_HASH_GROUP_BITS)) { i = (i + 1) % (1 << TDB_HASH_GROUP_BITS)) {
if (group[i] == 0) { if (group[i] == 0) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_CORRUPT,
tdb->log_priv, TDB_DEBUG_ERROR,
"check: bad group placement" "check: bad group placement"
" %u vs %u\n", " %u vs %u",
b, bucket); b, bucket);
goto fail; goto fail;
} }
} }
if (tdb_read_convert(tdb, off, &rec, sizeof(rec)) == -1) if (tdb_read_convert(tdb, off, &rec, sizeof(rec)))
goto fail; goto fail;
/* Bottom bits must match header. */ /* Bottom bits must match header. */
if ((h & ((1 << 11)-1)) != rec_hash(&rec)) { if ((h & ((1 << 11)-1)) != rec_hash(&rec)) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"tdb_check: Bad hash magic at" "tdb_check: Bad hash magic at"
" offset %llu (0x%llx vs 0x%llx)\n", " offset %llu (0x%llx vs 0x%llx)",
(long long)off, (long long)off,
(long long)h, (long long)h,
(long long)rec_hash(&rec)); (long long)rec_hash(&rec));
...@@ -305,8 +310,8 @@ static bool check_hash(struct tdb_context *tdb, ...@@ -305,8 +310,8 @@ static bool check_hash(struct tdb_context *tdb,
return false; return false;
if (num_found != num_used) { if (num_found != num_used) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: Not all entries are in hash\n"); "tdb_check: Not all entries are in hash");
return false; return false;
} }
return true; return true;
...@@ -318,14 +323,14 @@ static bool check_free(struct tdb_context *tdb, ...@@ -318,14 +323,14 @@ static bool check_free(struct tdb_context *tdb,
tdb_off_t prev, unsigned int flist, unsigned int bucket) tdb_off_t prev, unsigned int flist, unsigned int bucket)
{ {
if (frec_magic(frec) != TDB_FREE_MAGIC) { if (frec_magic(frec) != TDB_FREE_MAGIC) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: offset %llu bad magic 0x%llx\n", "tdb_check: offset %llu bad magic 0x%llx",
(long long)off, (long long)frec->magic_and_prev); (long long)off, (long long)frec->magic_and_prev);
return false; return false;
} }
if (frec_flist(frec) != flist) { if (frec_flist(frec) != flist) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: offset %llu bad freelist %u\n", "tdb_check: offset %llu bad freelist %u",
(long long)off, frec_flist(frec)); (long long)off, frec_flist(frec));
return false; return false;
} }
...@@ -335,15 +340,15 @@ static bool check_free(struct tdb_context *tdb, ...@@ -335,15 +340,15 @@ static bool check_free(struct tdb_context *tdb,
false)) false))
return false; return false;
if (size_to_bucket(frec_len(frec)) != bucket) { if (size_to_bucket(frec_len(frec)) != bucket) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: offset %llu in wrong bucket %u vs %u\n", "tdb_check: offset %llu in wrong bucket %u vs %u",
(long long)off, (long long)off,
bucket, size_to_bucket(frec_len(frec))); bucket, size_to_bucket(frec_len(frec)));
return false; return false;
} }
if (prev != frec_prev(frec)) { if (prev != frec_prev(frec)) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: offset %llu bad prev %llu vs %llu\n", "tdb_check: offset %llu bad prev %llu vs %llu",
(long long)off, (long long)off,
(long long)prev, (long long)frec_len(frec)); (long long)prev, (long long)frec_len(frec));
return false; return false;
...@@ -369,9 +374,8 @@ static bool check_free_list(struct tdb_context *tdb, ...@@ -369,9 +374,8 @@ static bool check_free_list(struct tdb_context *tdb,
|| rec_key_length(&flist.hdr) != 0 || rec_key_length(&flist.hdr) != 0
|| rec_data_length(&flist.hdr) != sizeof(flist) - sizeof(flist.hdr) || rec_data_length(&flist.hdr) != sizeof(flist) - sizeof(flist.hdr)
|| rec_hash(&flist.hdr) != 1) { || rec_hash(&flist.hdr) != 1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
tdb->log_priv, "tdb_check: Invalid header on free list");
"tdb_check: Invalid header on free list\n");
return false; return false;
} }
...@@ -391,10 +395,10 @@ static bool check_free_list(struct tdb_context *tdb, ...@@ -391,10 +395,10 @@ static bool check_free_list(struct tdb_context *tdb,
/* FIXME: Check hash bits */ /* FIXME: Check hash bits */
p = asearch(&off, free, num_free, off_cmp); p = asearch(&off, free, num_free, off_cmp);
if (!p) { if (!p) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_CORRUPT,
tdb->log_priv, TDB_DEBUG_ERROR,
"tdb_check: Invalid offset" "tdb_check: Invalid offset"
" %llu in free table\n", " %llu in free table",
(long long)off); (long long)off);
return false; return false;
} }
...@@ -453,15 +457,16 @@ static bool check_linear(struct tdb_context *tdb, ...@@ -453,15 +457,16 @@ static bool check_linear(struct tdb_context *tdb,
} else { } else {
len = dead_space(tdb, off); len = dead_space(tdb, off);
if (len < sizeof(rec.r)) { if (len < sizeof(rec.r)) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb_logerr(tdb, TDB_ERR_CORRUPT,
tdb->log_priv, TDB_DEBUG_ERROR,
"tdb_check: invalid dead space" "tdb_check: invalid dead"
" at %zu\n", (size_t)off); " space at %zu",
(size_t)off);
return false; return false;
} }
tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv, tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_WARNING,
"Dead space at %zu-%zu (of %zu)\n", "Dead space at %zu-%zu (of %zu)",
(size_t)off, (size_t)(off + len), (size_t)off, (size_t)(off + len),
(size_t)tdb->map_size); (size_t)tdb->map_size);
} }
...@@ -469,22 +474,25 @@ static bool check_linear(struct tdb_context *tdb, ...@@ -469,22 +474,25 @@ static bool check_linear(struct tdb_context *tdb,
if (tdb_read_convert(tdb, off, &rec, sizeof(rec.r))) if (tdb_read_convert(tdb, off, &rec, sizeof(rec.r)))
return false; return false;
if (recovery != off) { if (recovery != off) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"tdb_check: unexpected recovery" "tdb_check: unexpected recovery"
" record at offset %zu\n", " record at offset %zu",
(size_t)off); (size_t)off);
return false; return false;
} }
if (rec.r.len > rec.r.max_len) { if (rec.r.len > rec.r.max_len) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"tdb_check: invalid recovery length" "tdb_check: invalid recovery length"
" %zu\n", (size_t)rec.r.len); " %zu", (size_t)rec.r.len);
return false; return false;
} }
if (rec.r.eof > tdb->map_size) { if (rec.r.eof > tdb->map_size) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"tdb_check: invalid old EOF" "tdb_check: invalid old EOF"
" %zu\n", (size_t)rec.r.eof); " %zu", (size_t)rec.r.eof);
return false; return false;
} }
found_recovery = true; found_recovery = true;
...@@ -493,9 +501,10 @@ static bool check_linear(struct tdb_context *tdb, ...@@ -493,9 +501,10 @@ static bool check_linear(struct tdb_context *tdb,
|| frec_magic(&rec.f) == TDB_COALESCING_MAGIC) { || frec_magic(&rec.f) == TDB_COALESCING_MAGIC) {
len = sizeof(rec.u) + frec_len(&rec.f); len = sizeof(rec.u) + frec_len(&rec.f);
if (off + len > tdb->map_size) { if (off + len > tdb->map_size) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"tdb_check: free overlength %llu" "tdb_check: free overlength %llu"
" at offset %llu\n", " at offset %llu",
(long long)len, (long long)off); (long long)len, (long long)off);
return false; return false;
} }
...@@ -508,11 +517,12 @@ static bool check_linear(struct tdb_context *tdb, ...@@ -508,11 +517,12 @@ static bool check_linear(struct tdb_context *tdb,
/* This record is used! */ /* This record is used! */
if (rec_magic(&rec.u) != TDB_MAGIC) { if (rec_magic(&rec.u) != TDB_MAGIC) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"tdb_check: Bad magic 0x%llx" "tdb_check: Bad magic 0x%llx"
" at offset %llu\n", " at offset %zu",
(long long)rec_magic(&rec.u), (long long)rec_magic(&rec.u),
(long long)off); (size_t)off);
return false; return false;
} }
...@@ -525,17 +535,19 @@ static bool check_linear(struct tdb_context *tdb, ...@@ -525,17 +535,19 @@ static bool check_linear(struct tdb_context *tdb,
len = sizeof(rec.u) + klen + dlen + extra; len = sizeof(rec.u) + klen + dlen + extra;
if (off + len > tdb->map_size) { if (off + len > tdb->map_size) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_ERROR,
"tdb_check: used overlength %llu" "tdb_check: used overlength %llu"
" at offset %llu\n", " at offset %llu",
(long long)len, (long long)off); (long long)len, (long long)off);
return false; return false;
} }
if (len < sizeof(rec.f)) { if (len < sizeof(rec.f)) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
"tdb_check: too short record %llu at" TDB_DEBUG_ERROR,
" %llu\n", "tdb_check: too short record %llu"
" at %llu",
(long long)len, (long long)off); (long long)len, (long long)off);
return false; return false;
} }
...@@ -544,8 +556,8 @@ static bool check_linear(struct tdb_context *tdb, ...@@ -544,8 +556,8 @@ static bool check_linear(struct tdb_context *tdb,
/* We must have found recovery area if there was one. */ /* We must have found recovery area if there was one. */
if (recovery != 0 && !found_recovery) { if (recovery != 0 && !found_recovery) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: expected a recovery area at %zu\n", "tdb_check: expected a recovery area at %zu",
(size_t)recovery); (size_t)recovery);
return false; return false;
} }
...@@ -589,8 +601,8 @@ int tdb_check(struct tdb_context *tdb, ...@@ -589,8 +601,8 @@ int tdb_check(struct tdb_context *tdb,
goto fail; goto fail;
if (num_found != num_free) { if (num_found != num_free) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
"tdb_check: Not all entries are in free table\n"); "tdb_check: Not all entries are in free table");
return -1; return -1;
} }
......
...@@ -118,8 +118,8 @@ static int remove_from_list(struct tdb_context *tdb, ...@@ -118,8 +118,8 @@ static int remove_from_list(struct tdb_context *tdb,
#ifdef DEBUG #ifdef DEBUG
if (tdb_read_off(tdb, off) != r_off) { if (tdb_read_off(tdb, off) != r_off) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
"remove_from_list: %llu bad prev in list %llu\n", "remove_from_list: %llu bad prev in list %llu",
(long long)r_off, (long long)b_off); (long long)r_off, (long long)b_off);
return -1; return -1;
} }
...@@ -136,8 +136,8 @@ static int remove_from_list(struct tdb_context *tdb, ...@@ -136,8 +136,8 @@ static int remove_from_list(struct tdb_context *tdb,
#ifdef DEBUG #ifdef DEBUG
if (tdb_read_off(tdb, off) & TDB_OFF_MASK != r_off) { if (tdb_read_off(tdb, off) & TDB_OFF_MASK != r_off) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
"remove_from_list: %llu bad list %llu\n", "remove_from_list: %llu bad list %llu",
(long long)r_off, (long long)b_off); (long long)r_off, (long long)b_off);
return -1; return -1;
} }
...@@ -176,8 +176,9 @@ static int enqueue_in_free(struct tdb_context *tdb, ...@@ -176,8 +176,9 @@ static int enqueue_in_free(struct tdb_context *tdb,
new.next + offsetof(struct tdb_free_record, new.next + offsetof(struct tdb_free_record,
magic_and_prev)) magic_and_prev))
!= magic) { != magic) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
"enqueue_in_free: %llu bad head prev %llu\n", "enqueue_in_free: %llu bad head"
" prev %llu",
(long long)new.next, (long long)b_off); (long long)new.next, (long long)b_off);
return -1; return -1;
} }
...@@ -331,10 +332,9 @@ static int coalesce(struct tdb_context *tdb, ...@@ -331,10 +332,9 @@ static int coalesce(struct tdb_context *tdb,
goto err; goto err;
if (frec_len(&rec) != data_len) { if (frec_len(&rec) != data_len) {
tdb->ecode = TDB_ERR_CORRUPT; tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "coalesce: expected data len %zu not %zu",
"coalesce: expected data len %llu not %llu\n", (size_t)data_len, (size_t)frec_len(&rec));
(long long)data_len, (long long)frec_len(&rec));
goto err; goto err;
} }
...@@ -412,8 +412,8 @@ again: ...@@ -412,8 +412,8 @@ again:
if (frec_magic(r) != TDB_FREE_MAGIC) { if (frec_magic(r) != TDB_FREE_MAGIC) {
tdb_access_release(tdb, r); tdb_access_release(tdb, r);
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
"lock_and_alloc: %llu non-free 0x%llx\n", "lock_and_alloc: %llu non-free 0x%llx",
(long long)off, (long long)r->magic_and_prev); (long long)off, (long long)r->magic_and_prev);
goto unlock_err; goto unlock_err;
} }
...@@ -566,9 +566,8 @@ int set_used_header(struct tdb_context *tdb, ...@@ -566,9 +566,8 @@ int set_used_header(struct tdb_context *tdb,
if (rec_key_length(rec) != keylen if (rec_key_length(rec) != keylen
|| rec_data_length(rec) != datalen || rec_data_length(rec) != datalen
|| rec_extra_padding(rec) != actuallen - (keylen + datalen)) { || rec_extra_padding(rec) != actuallen - (keylen + datalen)) {
tdb->ecode = TDB_ERR_IO; tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "Could not encode k=%llu,d=%llu,a=%llu",
"Could not encode k=%llu,d=%llu,a=%llu\n",
(long long)keylen, (long long)datalen, (long long)keylen, (long long)datalen,
(long long)actuallen); (long long)actuallen);
return -1; return -1;
...@@ -588,8 +587,8 @@ static int tdb_expand(struct tdb_context *tdb, tdb_len_t size) ...@@ -588,8 +587,8 @@ static int 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->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
"tdb_expand: must hold lock during expand\n"); "tdb_expand: must hold lock during expand");
return -1; return -1;
} }
......
...@@ -561,9 +561,10 @@ int next_in_hash(struct tdb_context *tdb, int ltype, ...@@ -561,9 +561,10 @@ int next_in_hash(struct tdb_context *tdb, int ltype,
return -1; return -1;
} }
if (rec_magic(&rec) != TDB_MAGIC) { if (rec_magic(&rec) != TDB_MAGIC) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_DEBUG_FATAL,
"next_in_hash:" "next_in_hash:"
" corrupt record at %llu\n", " corrupt record at %llu",
(long long)off); (long long)off);
return -1; return -1;
} }
......
...@@ -56,8 +56,8 @@ void tdb_mmap(struct tdb_context *tdb) ...@@ -56,8 +56,8 @@ void tdb_mmap(struct tdb_context *tdb)
*/ */
if (tdb->map_ptr == MAP_FAILED) { if (tdb->map_ptr == MAP_FAILED) {
tdb->map_ptr = NULL; tdb->map_ptr = NULL;
tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv, tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_WARNING,
"tdb_mmap failed for size %lld (%s)\n", "tdb_mmap failed for size %lld (%s)",
(long long)tdb->map_size, strerror(errno)); (long long)tdb->map_size, strerror(errno));
} }
} }
...@@ -70,7 +70,6 @@ void tdb_mmap(struct tdb_context *tdb) ...@@ -70,7 +70,6 @@ void tdb_mmap(struct tdb_context *tdb)
static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
{ {
struct stat st; struct stat st;
int ret;
/* We can't hold pointers during this: we could unmap! */ /* We can't hold pointers during this: we could unmap! */
assert(!tdb->direct_access assert(!tdb->direct_access
...@@ -81,11 +80,9 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) ...@@ -81,11 +80,9 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
return 0; return 0;
if (tdb->flags & TDB_INTERNAL) { if (tdb->flags & TDB_INTERNAL) {
if (!probe) { if (!probe) {
/* Ensure ecode is set for log fn. */ tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
tdb->ecode = TDB_ERR_IO;
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
"tdb_oob len %lld beyond internal" "tdb_oob len %lld beyond internal"
" malloc size %lld\n", " malloc size %lld",
(long long)len, (long long)len,
(long long)tdb->map_size); (long long)tdb->map_size);
} }
...@@ -95,22 +92,20 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) ...@@ -95,22 +92,20 @@ static int tdb_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
if (tdb_lock_expand(tdb, F_RDLCK) != 0) if (tdb_lock_expand(tdb, F_RDLCK) != 0)
return -1; return -1;
ret = fstat(tdb->fd, &st); if (fstat(tdb->fd, &st) != 0) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
"Failed to fstat file: %s", strerror(errno));
tdb_unlock_expand(tdb, F_RDLCK); tdb_unlock_expand(tdb, F_RDLCK);
if (ret == -1) {
tdb->ecode = TDB_ERR_IO;
return -1; return -1;
} }
tdb_unlock_expand(tdb, F_RDLCK);
if (st.st_size < (size_t)len) { if (st.st_size < (size_t)len) {
if (!probe) { if (!probe) {
/* Ensure ecode is set for log fn. */ tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
tdb->ecode = TDB_ERR_IO; "tdb_oob len %zu beyond eof at %zu",
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, (size_t)len, st.st_size);
"tdb_oob len %lld beyond eof at %lld\n",
(long long)len, (long long)st.st_size);
} }
return -1; return -1;
} }
...@@ -180,11 +175,7 @@ int zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len) ...@@ -180,11 +175,7 @@ int zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len)
char buf[8192] = { 0 }; char buf[8192] = { 0 };
void *p = tdb->methods->direct(tdb, off, len); void *p = tdb->methods->direct(tdb, off, len);
if (tdb->read_only) { assert(!tdb->read_only);
tdb->ecode = TDB_ERR_RDONLY;
return -1;
}
if (p) { if (p) {
memset(p, 0, len); memset(p, 0, len);
return 0; return 0;
...@@ -275,15 +266,17 @@ bool tdb_read_all(int fd, void *buf, size_t len) ...@@ -275,15 +266,17 @@ bool tdb_read_all(int fd, void *buf, size_t len)
static int tdb_write(struct tdb_context *tdb, tdb_off_t off, static int tdb_write(struct tdb_context *tdb, tdb_off_t off,
const void *buf, tdb_len_t len) const void *buf, tdb_len_t len)
{ {
if (len == 0) {
return 0;
}
if (tdb->read_only) { if (tdb->read_only) {
tdb->ecode = TDB_ERR_RDONLY; tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
"Write to read-only database");
return -1; return -1;
} }
/* FIXME: Bogus optimization? */
if (len == 0) {
return 0;
}
if (tdb->methods->oob(tdb, off + len, 0) != 0) if (tdb->methods->oob(tdb, off + len, 0) != 0)
return -1; return -1;
...@@ -291,11 +284,9 @@ static int tdb_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -291,11 +284,9 @@ static int 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 {
if (!tdb_pwrite_all(tdb->fd, buf, len, off)) { if (!tdb_pwrite_all(tdb->fd, buf, len, off)) {
tdb->ecode = TDB_ERR_IO; tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_write failed at %zu len=%zu (%s)",
"tdb_write failed at %llu len=%llu (%s)\n", (size_t)off, (size_t)len, strerror(errno));
(long long)off, (long long)len,
strerror(errno));
return -1; return -1;
} }
} }
...@@ -314,14 +305,12 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf, ...@@ -314,14 +305,12 @@ static int tdb_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
memcpy(buf, off + (char *)tdb->map_ptr, len); memcpy(buf, off + (char *)tdb->map_ptr, len);
} else { } else {
if (!tdb_pread_all(tdb->fd, buf, len, off)) { if (!tdb_pread_all(tdb->fd, buf, len, off)) {
/* Ensure ecode is set for log fn. */ tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
tdb->ecode = TDB_ERR_IO; "tdb_read failed at %zu "
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "len=%zu (%s) map_size=%zu",
"tdb_read failed at %lld " (size_t)off, (size_t)len,
"len=%lld (%s) map_size=%lld\n",
(long long)off, (long long)len,
strerror(errno), strerror(errno),
(long long)tdb->map_size); (size_t)tdb->map_size);
return -1; return -1;
} }
} }
...@@ -335,10 +324,9 @@ int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off, ...@@ -335,10 +324,9 @@ int tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
if (unlikely((tdb->flags & TDB_CONVERT))) { if (unlikely((tdb->flags & TDB_CONVERT))) {
void *conv = malloc(len); void *conv = malloc(len);
if (!conv) { if (!conv) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_write: no memory converting"
"tdb_write: no memory converting %zu bytes\n", " %zu bytes", len);
len);
return -1; return -1;
} }
memcpy(conv, rec, len); memcpy(conv, rec, len);
...@@ -362,7 +350,8 @@ int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off, ...@@ -362,7 +350,8 @@ int tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
int tdb_write_off(struct tdb_context *tdb, tdb_off_t off, tdb_off_t val) int tdb_write_off(struct tdb_context *tdb, tdb_off_t off, tdb_off_t val)
{ {
if (tdb->read_only) { if (tdb->read_only) {
tdb->ecode = TDB_ERR_RDONLY; tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
"Write to read-only database");
return -1; return -1;
} }
...@@ -383,12 +372,12 @@ static void *_tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, ...@@ -383,12 +372,12 @@ static void *_tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset,
/* some systems don't like zero length malloc */ /* some systems don't like zero length malloc */
buf = malloc(prefix + len ? prefix + len : 1); buf = malloc(prefix + len ? prefix + len : 1);
if (unlikely(!buf)) { if (!buf) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_alloc_read malloc failed len=%zu",
"tdb_alloc_read malloc failed len=%lld\n", (size_t)(prefix + len));
(long long)prefix + len); } else if (unlikely(tdb->methods->read(tdb, offset, buf+prefix,
} else if (unlikely(tdb->methods->read(tdb, offset, buf+prefix, len))) { len) == -1)) {
free(buf); free(buf);
buf = NULL; buf = NULL;
} }
...@@ -409,9 +398,8 @@ static int fill(struct tdb_context *tdb, ...@@ -409,9 +398,8 @@ static int fill(struct tdb_context *tdb,
size_t n = len > size ? size : len; size_t n = len > size ? size : len;
if (!tdb_pwrite_all(tdb->fd, buf, n, off)) { if (!tdb_pwrite_all(tdb->fd, buf, n, off)) {
tdb->ecode = TDB_ERR_IO; tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "fill write failed: giving up!");
"fill write failed: giving up!\n");
return -1; return -1;
} }
len -= n; len -= n;
...@@ -427,14 +415,16 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition) ...@@ -427,14 +415,16 @@ static int tdb_expand_file(struct tdb_context *tdb, tdb_len_t addition)
char buf[8192]; char buf[8192];
if (tdb->read_only) { if (tdb->read_only) {
tdb->ecode = TDB_ERR_RDONLY; tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
"Expand on read-only database");
return -1; return -1;
} }
if (tdb->flags & TDB_INTERNAL) { if (tdb->flags & TDB_INTERNAL) {
char *new = realloc(tdb->map_ptr, tdb->map_size + addition); char *new = realloc(tdb->map_ptr, tdb->map_size + addition);
if (!new) { if (!new) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
"No memory to expand database");
return -1; return -1;
} }
tdb->map_ptr = new; tdb->map_ptr = new;
...@@ -495,7 +485,8 @@ void *tdb_access_write(struct tdb_context *tdb, ...@@ -495,7 +485,8 @@ void *tdb_access_write(struct tdb_context *tdb,
void *ret = NULL; void *ret = NULL;
if (tdb->read_only) { if (tdb->read_only) {
tdb->ecode = TDB_ERR_RDONLY; tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
"Write to read-only database");
return NULL; return NULL;
} }
......
...@@ -102,7 +102,7 @@ static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len) ...@@ -102,7 +102,7 @@ static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
} }
if (!found) { if (!found) {
fprintf(stderr, "Unlock on %u@%u not found!\n", fprintf(stderr, "Unlock on %u@%u not found!",
(int)off, (int)len); (int)off, (int)len);
abort(); abort();
} }
...@@ -135,16 +135,16 @@ static int tdb_brlock(struct tdb_context *tdb, ...@@ -135,16 +135,16 @@ static int tdb_brlock(struct tdb_context *tdb,
} }
if (rw_type == F_WRLCK && tdb->read_only) { if (rw_type == F_WRLCK && tdb->read_only) {
tdb->ecode = TDB_ERR_RDONLY; tdb_logerr(tdb, TDB_ERR_RDONLY, TDB_DEBUG_WARNING,
"Write lock attempted on read-only database");
return -1; return -1;
} }
/* A 32 bit system cannot open a 64-bit file, but it could have /* A 32 bit system cannot open a 64-bit file, but it could have
* expanded since then: check here. */ * expanded since then: check here. */
if ((size_t)(offset + len) != offset + len) { if ((size_t)(offset + len) != offset + len) {
tdb->ecode = TDB_ERR_IO; tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_brlock: lock on giant offset %llu",
"tdb_brlock: lock on giant offset %llu\n",
(long long)(offset + len)); (long long)(offset + len));
return -1; return -1;
} }
...@@ -160,11 +160,12 @@ static int tdb_brlock(struct tdb_context *tdb, ...@@ -160,11 +160,12 @@ static int tdb_brlock(struct tdb_context *tdb,
* EAGAIN is an expected return from non-blocking * EAGAIN is an expected return from non-blocking
* locks. */ * locks. */
if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) { if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
"tdb_brlock failed (fd=%d) at" "tdb_brlock failed (fd=%d) at"
" offset %llu rw_type=%d flags=%d len=%llu\n", " offset %zu rw_type=%d flags=%d len=%zu:"
tdb->fd, (long long)offset, rw_type, " %s",
flags, (long long)len); tdb->fd, (size_t)offset, rw_type,
flags, (size_t)len, strerror(errno));
} }
return -1; return -1;
} }
...@@ -185,10 +186,10 @@ static int tdb_brunlock(struct tdb_context *tdb, ...@@ -185,10 +186,10 @@ static int tdb_brunlock(struct tdb_context *tdb,
} while (ret == -1 && errno == EINTR); } while (ret == -1 && errno == EINTR);
if (ret == -1) { if (ret == -1) {
tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_TRACE,
"tdb_brunlock failed (fd=%d) at offset %llu" "tdb_brunlock failed (fd=%d) at offset %zu"
" rw_type=%d len=%llu\n", " rw_type=%d len=%zu",
tdb->fd, (long long)offset, rw_type, (long long)len); tdb->fd, (size_t)offset, rw_type, (size_t)len);
} }
return ret; return ret;
} }
...@@ -204,15 +205,15 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb) ...@@ -204,15 +205,15 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb)
int count = 1000; int count = 1000;
if (tdb->allrecord_lock.count != 1) { if (tdb->allrecord_lock.count != 1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
"tdb_allrecord_upgrade failed: count %u too high\n", "tdb_allrecord_upgrade failed: count %u too high",
tdb->allrecord_lock.count); tdb->allrecord_lock.count);
return -1; return -1;
} }
if (tdb->allrecord_lock.off != 1) { if (tdb->allrecord_lock.off != 1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
"tdb_allrecord_upgrade failed: already upgraded?\n"); "tdb_allrecord_upgrade failed: already upgraded?");
return -1; return -1;
} }
...@@ -233,8 +234,8 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb) ...@@ -233,8 +234,8 @@ int tdb_allrecord_upgrade(struct tdb_context *tdb)
tv.tv_usec = 1; tv.tv_usec = 1;
select(0, NULL, NULL, NULL, &tv); select(0, NULL, NULL, NULL, &tv);
} }
tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_WARNING,
"tdb_allrecord_upgrade failed\n"); "tdb_allrecord_upgrade failed");
return -1; return -1;
} }
...@@ -279,10 +280,9 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype, ...@@ -279,10 +280,9 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
struct tdb_lock_type *new_lck; struct tdb_lock_type *new_lck;
if (offset > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) { if (offset > TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE + tdb->map_size / 8) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_nest_lock: invalid offset %zu ltype=%d",
"tdb_nest_lock: invalid offset %llu ltype=%d\n", (size_t)offset, ltype);
(long long)offset, ltype);
return -1; return -1;
} }
...@@ -294,10 +294,9 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype, ...@@ -294,10 +294,9 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
new_lck = find_nestlock(tdb, offset); new_lck = find_nestlock(tdb, offset);
if (new_lck) { if (new_lck) {
if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) { if (new_lck->ltype == F_RDLCK && ltype == F_WRLCK) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_nest_lock: offset %zu has read lock",
"tdb_nest_lock: offset %llu has read lock\n", (size_t)offset);
(long long)offset);
return -1; return -1;
} }
/* Just increment the struct, posix locks don't stack. */ /* Just increment the struct, posix locks don't stack. */
...@@ -308,9 +307,8 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype, ...@@ -308,9 +307,8 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
if (tdb->num_lockrecs if (tdb->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) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_nest_lock: already have a hash lock?");
"tdb_nest_lock: already have a hash lock?\n");
return -1; return -1;
} }
...@@ -318,10 +316,9 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype, ...@@ -318,10 +316,9 @@ static int tdb_nest_lock(struct tdb_context *tdb, tdb_off_t offset, int ltype,
tdb->lockrecs, tdb->lockrecs,
sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1)); sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
if (new_lck == NULL) { if (new_lck == NULL) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_nest_lock: unable to allocate %zu lock struct",
"tdb_nest_lock: unable to allocate %llu lock struct", tdb->num_lockrecs + 1);
(long long)(tdb->num_lockrecs + 1));
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
...@@ -366,9 +363,8 @@ static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype) ...@@ -366,9 +363,8 @@ static int tdb_nest_unlock(struct tdb_context *tdb, tdb_off_t off, int ltype)
lck = find_nestlock(tdb, off); lck = find_nestlock(tdb, off);
if ((lck == NULL) || (lck->count == 0)) { if ((lck == NULL) || (lck->count == 0)) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_nest_unlock: no lock for %zu", (size_t)off);
"tdb_nest_unlock: no lock for %llu\n", (long long)off);
return -1; return -1;
} }
...@@ -453,9 +449,8 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype, ...@@ -453,9 +449,8 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
{ {
/* FIXME: There are no locks on read-only dbs */ /* FIXME: There are no locks on read-only dbs */
if (tdb->read_only) { if (tdb->read_only) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_allrecord_lock: read-only");
"tdb_allrecord_lock: read-only\n");
return -1; return -1;
} }
...@@ -467,9 +462,8 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype, ...@@ -467,9 +462,8 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.count) {
/* a global lock of a different type exists */ /* a global lock of a different type exists */
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_allrecord_lock: already have %s lock",
"tdb_allrecord_lock: already have %s lock\n",
tdb->allrecord_lock.ltype == F_RDLCK tdb->allrecord_lock.ltype == F_RDLCK
? "read" : "write"); ? "read" : "write");
return -1; return -1;
...@@ -477,17 +471,15 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype, ...@@ -477,17 +471,15 @@ int tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
if (tdb_has_hash_locks(tdb)) { if (tdb_has_hash_locks(tdb)) {
/* can't combine global and chain locks */ /* can't combine global and chain locks */
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_allrecord_lock: already have chain lock");
"tdb_allrecord_lock: already have chain lock\n");
return -1; return -1;
} }
if (upgradable && ltype != F_RDLCK) { if (upgradable && ltype != F_RDLCK) {
/* tdb error: you can't upgrade a write lock! */ /* tdb error: you can't upgrade a write lock! */
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_allrecord_lock: can't upgrade a write lock");
"tdb_allrecord_lock: can't upgrade a write lock\n");
return -1; return -1;
} }
...@@ -497,9 +489,8 @@ again: ...@@ -497,9 +489,8 @@ again:
if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START, if (tdb_lock_gradual(tdb, ltype, flags, TDB_HASH_LOCK_START,
TDB_HASH_LOCK_RANGE)) { TDB_HASH_LOCK_RANGE)) {
if (!(flags & TDB_LOCK_PROBE)) { if (!(flags & TDB_LOCK_PROBE)) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
"tdb_allrecord_lock hashes failed (%s)\n", "tdb_allrecord_lock hashes failed");
strerror(errno));
} }
return -1; return -1;
} }
...@@ -508,9 +499,8 @@ again: ...@@ -508,9 +499,8 @@ again:
if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE, if (tdb_brlock(tdb, ltype, TDB_HASH_LOCK_START + TDB_HASH_LOCK_RANGE,
0, flags)) { 0, flags)) {
if (!(flags & TDB_LOCK_PROBE)) { if (!(flags & TDB_LOCK_PROBE)) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
"tdb_allrecord_lock freelist failed (%s)\n", "tdb_allrecord_lock freelist failed");
strerror(errno));
} }
tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START, tdb_brunlock(tdb, ltype, TDB_HASH_LOCK_START,
TDB_HASH_LOCK_RANGE); TDB_HASH_LOCK_RANGE);
...@@ -565,27 +555,17 @@ void tdb_unlock_expand(struct tdb_context *tdb, int ltype) ...@@ -565,27 +555,17 @@ void tdb_unlock_expand(struct tdb_context *tdb, int ltype)
/* unlock entire db */ /* unlock entire db */
int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype) int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype)
{ {
/* FIXME: There are no locks on read-only dbs */
if (tdb->read_only) {
tdb->ecode = TDB_ERR_LOCK;
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
"tdb_allrecord_unlock: read-only\n");
return -1;
}
if (tdb->allrecord_lock.count == 0) { if (tdb->allrecord_lock.count == 0) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_allrecord_unlock: not locked!");
"tdb_allrecord_unlock: not locked!\n");
return -1; return -1;
} }
/* Upgradable locks are marked as write locks. */ /* Upgradable locks are marked as write locks. */
if (tdb->allrecord_lock.ltype != ltype if (tdb->allrecord_lock.ltype != ltype
&& (!tdb->allrecord_lock.off || ltype != F_RDLCK)) { && (!tdb->allrecord_lock.off || ltype != F_RDLCK)) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_allrecord_unlock: have %s lock",
"tdb_allrecord_unlock: have %s lock\n",
tdb->allrecord_lock.ltype == F_RDLCK tdb->allrecord_lock.ltype == F_RDLCK
? "read" : "write"); ? "read" : "write");
return -1; return -1;
...@@ -648,25 +628,22 @@ int tdb_lock_hashes(struct tdb_context *tdb, ...@@ -648,25 +628,22 @@ int tdb_lock_hashes(struct tdb_context *tdb,
} }
if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.count) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_lock_hashes: already have %s allrecordlock",
"tdb_lock_hashes: have %s allrecordlock\n",
tdb->allrecord_lock.ltype == F_RDLCK tdb->allrecord_lock.ltype == F_RDLCK
? "read" : "write"); ? "read" : "write");
return -1; return -1;
} }
if (tdb_has_free_lock(tdb)) { if (tdb_has_free_lock(tdb)) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_lock_hashes: already have free lock");
"tdb_lock_hashes: have free lock already\n");
return -1; return -1;
} }
if (tdb_has_expansion_lock(tdb)) { if (tdb_has_expansion_lock(tdb)) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_lock_hashes: already have expansion lock");
"tdb_lock_hashes: have expansion lock already\n");
return -1; return -1;
} }
...@@ -684,9 +661,8 @@ int tdb_unlock_hashes(struct tdb_context *tdb, ...@@ -684,9 +661,8 @@ int tdb_unlock_hashes(struct tdb_context *tdb,
if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.count) {
if (tdb->allrecord_lock.ltype == F_RDLCK if (tdb->allrecord_lock.ltype == F_RDLCK
&& ltype == F_WRLCK) { && ltype == F_WRLCK) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_unlock_hashes RO allrecord!");
"tdb_unlock_hashes RO allrecord!\n");
return -1; return -1;
} }
return 0; return 0;
...@@ -715,17 +691,15 @@ int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off, ...@@ -715,17 +691,15 @@ int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
if (tdb->allrecord_lock.count) { if (tdb->allrecord_lock.count) {
if (tdb->allrecord_lock.ltype == F_WRLCK) if (tdb->allrecord_lock.ltype == F_WRLCK)
return 0; return 0;
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_lock_free_bucket with RO allrecordlock!");
"tdb_lock_free_bucket with RO allrecordlock!\n");
return -1; return -1;
} }
#if 0 /* FIXME */ #if 0 /* FIXME */
if (tdb_has_expansion_lock(tdb)) { if (tdb_has_expansion_lock(tdb)) {
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_lock_free_bucket: already have expansion lock");
"tdb_lock_free_bucket: have expansion lock already\n");
return -1; return -1;
} }
#endif #endif
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "config.h" #include "config.h"
#include <ccan/tdb2/tdb2.h> #include <ccan/tdb2/tdb2.h>
#include <ccan/likely/likely.h> #include <ccan/likely/likely.h>
#include <ccan/compiler/compiler.h>
#ifdef HAVE_BYTESWAP_H #ifdef HAVE_BYTESWAP_H
#include <byteswap.h> #include <byteswap.h>
#endif #endif
...@@ -308,8 +309,8 @@ struct tdb_context { ...@@ -308,8 +309,8 @@ struct tdb_context {
uint32_t flags; uint32_t flags;
/* Logging function */ /* Logging function */
tdb_logfn_t log; tdb_logfn_t logfn;
void *log_priv; void *log_private;
/* Hash function. */ /* Hash function. */
tdb_hashfn_t khash; tdb_hashfn_t khash;
...@@ -328,7 +329,7 @@ struct tdb_context { ...@@ -328,7 +329,7 @@ struct tdb_context {
/* Lock information */ /* Lock information */
struct tdb_lock_type allrecord_lock; struct tdb_lock_type allrecord_lock;
uint64_t num_lockrecs; size_t num_lockrecs;
struct tdb_lock_type *lockrecs; struct tdb_lock_type *lockrecs;
struct tdb_attribute_stats *stats; struct tdb_attribute_stats *stats;
...@@ -529,6 +530,12 @@ int next_in_hash(struct tdb_context *tdb, int ltype, ...@@ -529,6 +530,12 @@ int next_in_hash(struct tdb_context *tdb, int ltype,
int tdb_transaction_recover(struct tdb_context *tdb); int tdb_transaction_recover(struct tdb_context *tdb);
bool tdb_needs_recovery(struct tdb_context *tdb); bool tdb_needs_recovery(struct tdb_context *tdb);
/* tdb.c: */
void COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR ecode,
enum tdb_debug_level level,
const char *fmt, ...);
#ifdef TDB_TRACE #ifdef TDB_TRACE
void tdb_trace(struct tdb_context *tdb, const char *op); void tdb_trace(struct tdb_context *tdb, const char *op);
void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op); void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op);
......
...@@ -164,7 +164,8 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) ...@@ -164,7 +164,8 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags)
buckets = tally_new(HISTO_HEIGHT); buckets = tally_new(HISTO_HEIGHT);
if (!flists || !hashes || !freet || !keys || !data || !extra if (!flists || !hashes || !freet || !keys || !data || !extra
|| !uncoal || !buckets) { || !uncoal || !buckets) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
"tdb_summary: failed to allocate tally structures");
goto unlock; goto unlock;
} }
......
#include "private.h" #include "private.h"
#include <ccan/tdb2/tdb2.h> #include <ccan/tdb2/tdb2.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/likely/likely.h>
#include <assert.h> #include <assert.h>
#include <stdarg.h>
/* The null return. */ /* The null return. */
struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 }; struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
...@@ -10,13 +9,6 @@ struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 }; ...@@ -10,13 +9,6 @@ struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
/* all contexts, to ensure no double-opens (fcntl locks don't nest!) */ /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
static struct tdb_context *tdbs = NULL; static struct tdb_context *tdbs = NULL;
PRINTF_FMT(4, 5) static void
null_log_fn(struct tdb_context *tdb,
enum tdb_debug_level level, void *priv,
const char *fmt, ...)
{
}
static bool tdb_already_open(dev_t device, ino_t ino) static bool tdb_already_open(dev_t device, ino_t ino)
{ {
struct tdb_context *i; struct tdb_context *i;
...@@ -39,8 +31,8 @@ static uint64_t random_number(struct tdb_context *tdb) ...@@ -39,8 +31,8 @@ static uint64_t random_number(struct tdb_context *tdb)
fd = open("/dev/urandom", O_RDONLY); fd = open("/dev/urandom", O_RDONLY);
if (fd >= 0) { if (fd >= 0) {
if (tdb_read_all(fd, &ret, sizeof(ret))) { if (tdb_read_all(fd, &ret, sizeof(ret))) {
tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv, tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
"tdb_open: random from /dev/urandom\n"); "tdb_open: random from /dev/urandom");
close(fd); close(fd);
return ret; return ret;
} }
...@@ -55,9 +47,9 @@ static uint64_t random_number(struct tdb_context *tdb) ...@@ -55,9 +47,9 @@ static uint64_t random_number(struct tdb_context *tdb)
char reply[1 + sizeof(uint64_t)]; char reply[1 + sizeof(uint64_t)];
int r = read(fd, reply, sizeof(reply)); int r = read(fd, reply, sizeof(reply));
if (r > 1) { if (r > 1) {
tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv, tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
"tdb_open: %u random bytes from" "tdb_open: %u random bytes from"
" /dev/egd-pool\n", r-1); " /dev/egd-pool", r-1);
/* Copy at least some bytes. */ /* Copy at least some bytes. */
memcpy(&ret, reply+1, r - 1); memcpy(&ret, reply+1, r - 1);
if (reply[0] == sizeof(uint64_t) if (reply[0] == sizeof(uint64_t)
...@@ -73,8 +65,8 @@ static uint64_t random_number(struct tdb_context *tdb) ...@@ -73,8 +65,8 @@ static uint64_t random_number(struct tdb_context *tdb)
/* Fallback: pid and time. */ /* Fallback: pid and time. */
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec; ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv, tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
"tdb_open: random from getpid and time\n"); "tdb_open: random from getpid and time");
return ret; return ret;
} }
...@@ -130,7 +122,8 @@ static int tdb_new_database(struct tdb_context *tdb, ...@@ -130,7 +122,8 @@ static int tdb_new_database(struct tdb_context *tdb,
tdb->map_size = sizeof(newdb); tdb->map_size = sizeof(newdb);
tdb->map_ptr = malloc(tdb->map_size); tdb->map_ptr = malloc(tdb->map_size);
if (!tdb->map_ptr) { if (!tdb->map_ptr) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
"tdb_new_database: failed to allocate");
return -1; return -1;
} }
memcpy(tdb->map_ptr, &newdb, tdb->map_size); memcpy(tdb->map_ptr, &newdb, tdb->map_size);
...@@ -143,7 +136,9 @@ static int tdb_new_database(struct tdb_context *tdb, ...@@ -143,7 +136,9 @@ static int tdb_new_database(struct tdb_context *tdb,
return -1; return -1;
if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) { if (!tdb_pwrite_all(tdb->fd, &newdb, sizeof(newdb), 0)) {
tdb->ecode = TDB_ERR_IO; tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
"tdb_new_database: failed to write: %s",
strerror(errno));
return -1; return -1;
} }
return 0; return 0;
...@@ -155,7 +150,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -155,7 +150,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
{ {
struct tdb_context *tdb; struct tdb_context *tdb;
struct stat st; struct stat st;
int save_errno; int saved_errno = 0;
uint64_t hash_test; uint64_t hash_test;
unsigned v; unsigned v;
struct tdb_header hdr; struct tdb_header hdr;
...@@ -165,7 +160,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -165,7 +160,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
if (!tdb) { if (!tdb) {
/* Can't log this */ /* Can't log this */
errno = ENOMEM; errno = ENOMEM;
goto fail; return NULL;
} }
tdb->name = NULL; tdb->name = NULL;
tdb->map_ptr = NULL; tdb->map_ptr = NULL;
...@@ -174,8 +169,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -174,8 +169,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->map_size = sizeof(struct tdb_header); tdb->map_size = sizeof(struct tdb_header);
tdb->ecode = TDB_SUCCESS; tdb->ecode = TDB_SUCCESS;
tdb->flags = tdb_flags; tdb->flags = tdb_flags;
tdb->log = null_log_fn; tdb->logfn = NULL;
tdb->log_priv = NULL;
tdb->transaction = NULL; tdb->transaction = NULL;
tdb->stats = NULL; tdb->stats = NULL;
tdb_hash_init(tdb); tdb_hash_init(tdb);
...@@ -185,8 +179,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -185,8 +179,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
while (attr) { while (attr) {
switch (attr->base.attr) { switch (attr->base.attr) {
case TDB_ATTRIBUTE_LOG: case TDB_ATTRIBUTE_LOG:
tdb->log = attr->log.log_fn; tdb->logfn = attr->log.log_fn;
tdb->log_priv = attr->log.log_private; tdb->log_private = attr->log.log_private;
break; break;
case TDB_ATTRIBUTE_HASH: case TDB_ATTRIBUTE_HASH:
tdb->khash = attr->hash.hash_fn; tdb->khash = attr->hash.hash_fn;
...@@ -202,19 +196,17 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -202,19 +196,17 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->stats->size = sizeof(attr->stats); tdb->stats->size = sizeof(attr->stats);
break; break;
default: default:
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
"tdb_open: unknown attribute type %u\n", "tdb_open: unknown attribute type %u",
attr->base.attr); attr->base.attr);
errno = EINVAL;
goto fail; goto fail;
} }
attr = attr->base.next; attr = attr->base.next;
} }
if ((open_flags & O_ACCMODE) == O_WRONLY) { if ((open_flags & O_ACCMODE) == O_WRONLY) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
"tdb_open: can't open tdb %s write-only\n", name); "tdb_open: can't open tdb %s write-only", name);
errno = EINVAL;
goto fail; goto fail;
} }
...@@ -232,8 +224,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -232,8 +224,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
if (tdb->flags & TDB_INTERNAL) { if (tdb->flags & TDB_INTERNAL) {
tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP); tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
if (tdb_new_database(tdb, seed, &hdr) != 0) { if (tdb_new_database(tdb, seed, &hdr) != 0) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
"tdb_open: tdb_new_database failed!");
goto fail; goto fail;
} }
tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed)); tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
...@@ -243,10 +233,12 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -243,10 +233,12 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
} }
if ((tdb->fd = open(name, open_flags, mode)) == -1) { if ((tdb->fd = open(name, open_flags, mode)) == -1) {
tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv, /* errno set by open(2) */
"tdb_open: could not open file %s: %s\n", saved_errno = errno;
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
"tdb_open: could not open file %s: %s",
name, strerror(errno)); name, strerror(errno));
goto fail; /* errno set by open(2) */ goto fail;
} }
/* on exec, don't inherit the fd */ /* on exec, don't inherit the fd */
...@@ -255,19 +247,19 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -255,19 +247,19 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
/* ensure there is only one process initialising at once */ /* ensure there is only one process initialising at once */
if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) { if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, /* errno set by tdb_brlock */
"tdb_open: failed to get open lock on %s: %s\n", saved_errno = errno;
name, strerror(errno)); goto fail;
goto fail; /* errno set by tdb_brlock */
} }
if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0) if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
|| strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) { || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
if (!(open_flags & O_CREAT) if (!(open_flags & O_CREAT)) {
|| tdb_new_database(tdb, seed, &hdr) == -1) { tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
if (errno == 0) { "tdb_open: %s is not a tdb file", name);
errno = EIO; /* ie bad format or something */ goto fail;
} }
if (tdb_new_database(tdb, seed, &hdr) == -1) {
goto fail; goto fail;
} }
} else if (hdr.version != TDB_VERSION) { } else if (hdr.version != TDB_VERSION) {
...@@ -275,10 +267,9 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -275,10 +267,9 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->flags |= TDB_CONVERT; tdb->flags |= TDB_CONVERT;
else { else {
/* wrong version */ /* wrong version */
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
"tdb_open: %s is unknown version 0x%llx\n", "tdb_open: %s is unknown version 0x%llx",
name, (long long)hdr.version); name, (long long)hdr.version);
errno = EIO;
goto fail; goto fail;
} }
} }
...@@ -289,29 +280,34 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -289,29 +280,34 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test)); hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
if (hdr.hash_test != hash_test) { if (hdr.hash_test != hash_test) {
/* wrong hash variant */ /* wrong hash variant */
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
"tdb_open: %s uses a different hash function\n", "tdb_open: %s uses a different hash function",
name); name);
errno = EIO;
goto fail; goto fail;
} }
if (fstat(tdb->fd, &st) == -1) if (fstat(tdb->fd, &st) == -1) {
saved_errno = errno;
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
"tdb_open: could not stat open %s: %s",
name, strerror(errno));
goto fail; goto fail;
}
/* Is it already in the open list? If so, fail. */ /* Is it already in the open list? If so, fail. */
if (tdb_already_open(st.st_dev, st.st_ino)) { if (tdb_already_open(st.st_dev, st.st_ino)) {
/* FIXME */ /* FIXME */
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_NESTING, TDB_DEBUG_ERROR,
"tdb_open: %s (%d,%d) is already open in this process\n", "tdb_open: %s (%d,%d) is already open in this"
" process",
name, (int)st.st_dev, (int)st.st_ino); name, (int)st.st_dev, (int)st.st_ino);
errno = EBUSY;
goto fail; goto fail;
} }
tdb->name = strdup(name); tdb->name = strdup(name);
if (!tdb->name) { if (!tdb->name) {
errno = ENOMEM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
"tdb_open: failed to allocate name");
goto fail; goto fail;
} }
...@@ -324,7 +320,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -324,7 +320,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
/* Now it's fully formed, recover if necessary. */ /* Now it's fully formed, recover if necessary. */
if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) { if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) {
errno = EIO;
goto fail; goto fail;
} }
...@@ -336,10 +331,30 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -336,10 +331,30 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
return tdb; return tdb;
fail: fail:
save_errno = errno; /* Map ecode to some logical errno. */
if (!saved_errno) {
if (!tdb) switch (tdb->ecode) {
return NULL; case TDB_ERR_CORRUPT:
case TDB_ERR_IO:
saved_errno = EIO;
break;
case TDB_ERR_LOCK:
saved_errno = EWOULDBLOCK;
break;
case TDB_ERR_OOM:
saved_errno = ENOMEM;
break;
case TDB_ERR_EINVAL:
saved_errno = EINVAL;
break;
case TDB_ERR_NESTING:
saved_errno = EBUSY;
break;
default:
saved_errno = EINVAL;
break;
}
}
#ifdef TDB_TRACE #ifdef TDB_TRACE
close(tdb->tracefd); close(tdb->tracefd);
...@@ -353,11 +368,11 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -353,11 +368,11 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
free((char *)tdb->name); free((char *)tdb->name);
if (tdb->fd != -1) if (tdb->fd != -1)
if (close(tdb->fd) != 0) if (close(tdb->fd) != 0)
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
"tdb_open: failed to close tdb->fd" "tdb_open: failed to close tdb->fd"
" on error!\n"); " on error!");
free(tdb); free(tdb);
errno = save_errno; errno = saved_errno;
return NULL; return NULL;
} }
...@@ -515,10 +530,9 @@ int tdb_append(struct tdb_context *tdb, ...@@ -515,10 +530,9 @@ int tdb_append(struct tdb_context *tdb,
/* Slow path. */ /* Slow path. */
newdata = malloc(key.dsize + old_dlen + dbuf.dsize); newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
if (!newdata) { if (!newdata) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_append: failed to allocate %zu bytes",
"tdb_append: cannot allocate %llu bytes!\n", (size_t)(key.dsize+old_dlen+dbuf.dsize));
(long long)key.dsize + old_dlen + dbuf.dsize);
goto fail; goto fail;
} }
if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize, if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
...@@ -646,12 +660,12 @@ int tdb_close(struct tdb_context *tdb) ...@@ -646,12 +660,12 @@ int tdb_close(struct tdb_context *tdb)
return ret; return ret;
} }
enum TDB_ERROR tdb_error(struct tdb_context *tdb) enum TDB_ERROR tdb_error(const struct tdb_context *tdb)
{ {
return tdb->ecode; return tdb->ecode;
} }
const char *tdb_errorstr(struct tdb_context *tdb) const char *tdb_errorstr(const struct tdb_context *tdb)
{ {
/* Gcc warns if you miss a case in the switch, so use that. */ /* Gcc warns if you miss a case in the switch, so use that. */
switch (tdb->ecode) { switch (tdb->ecode) {
...@@ -668,3 +682,38 @@ const char *tdb_errorstr(struct tdb_context *tdb) ...@@ -668,3 +682,38 @@ const char *tdb_errorstr(struct tdb_context *tdb)
} }
return "Invalid error code"; return "Invalid error code";
} }
void COLD tdb_logerr(struct tdb_context *tdb,
enum TDB_ERROR ecode,
enum tdb_debug_level level,
const char *fmt, ...)
{
char *message;
va_list ap;
size_t len;
/* tdb_open paths care about errno, so save it. */
int saved_errno = errno;
tdb->ecode = ecode;
if (!tdb->logfn)
return;
/* FIXME: Doesn't assume asprintf. */
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
message = malloc(len + 1);
if (!message) {
tdb->logfn(tdb, level, tdb->log_private,
"out of memory formatting message");
return;
}
va_start(ap, fmt);
len = vsprintf(message, fmt, ap);
va_end(ap);
tdb->logfn(tdb, level, tdb->log_private, message);
free(message);
errno = saved_errno;
}
...@@ -67,7 +67,7 @@ enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, ...@@ -67,7 +67,7 @@ enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
/* flags for tdb_summary. Logical or to combine. */ /* flags for tdb_summary. Logical or to combine. */
enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 }; enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 };
/* debugging uses one of the following levels */ /* logging uses one of the following levels */
enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR, enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
TDB_DEBUG_WARNING, TDB_DEBUG_TRACE}; TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
...@@ -80,7 +80,7 @@ struct tdb_context; ...@@ -80,7 +80,7 @@ struct tdb_context;
/* FIXME: Make typesafe */ /* FIXME: Make typesafe */
typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *); typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
typedef void (*tdb_logfn_t)(struct tdb_context *, enum tdb_debug_level, void *priv, const char *, ...) PRINTF_FMT(4, 5); typedef void (*tdb_logfn_t)(struct tdb_context *, enum tdb_debug_level, void *, const char *);
typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed, typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed,
void *priv); void *priv);
...@@ -161,8 +161,8 @@ int tdb_check(struct tdb_context *tdb, ...@@ -161,8 +161,8 @@ int tdb_check(struct tdb_context *tdb,
int (*check)(TDB_DATA key, TDB_DATA data, void *private_data), int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
void *private_data); void *private_data);
enum TDB_ERROR tdb_error(struct tdb_context *tdb); enum TDB_ERROR tdb_error(const struct tdb_context *tdb);
const char *tdb_errorstr(struct tdb_context *tdb); const char *tdb_errorstr(const struct tdb_context *tdb);
int tdb_transaction_start(struct tdb_context *tdb); int tdb_transaction_start(struct tdb_context *tdb);
void tdb_transaction_cancel(struct tdb_context *tdb); void tdb_transaction_cancel(struct tdb_context *tdb);
......
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <ccan/tap/tap.h> #include <ccan/tap/tap.h>
#include "logging.h" #include "logging.h"
...@@ -16,24 +14,13 @@ union tdb_attribute tap_log_attr = { ...@@ -16,24 +14,13 @@ union tdb_attribute tap_log_attr = {
void tap_log_fn(struct tdb_context *tdb, void tap_log_fn(struct tdb_context *tdb,
enum tdb_debug_level level, void *priv, enum tdb_debug_level level, void *priv,
const char *fmt, ...) const char *message)
{ {
va_list ap;
char *p;
if (suppress_logging) if (suppress_logging)
return; return;
va_start(ap, fmt); diag("tdb log level %u: %s%s", level, log_prefix, message);
if (vasprintf(&p, fmt, ap) == -1)
abort();
/* Strip trailing \n: diag adds it. */
if (p[strlen(p)-1] == '\n')
p[strlen(p)-1] = '\0';
diag("tdb log level %u: %s%s", level, log_prefix, p);
free(p);
if (level != TDB_DEBUG_TRACE) if (level != TDB_DEBUG_TRACE)
tap_log_messages++; tap_log_messages++;
va_end(ap);
} }
...@@ -11,7 +11,7 @@ extern union tdb_attribute tap_log_attr; ...@@ -11,7 +11,7 @@ extern union tdb_attribute tap_log_attr;
void tap_log_fn(struct tdb_context *tdb, void tap_log_fn(struct tdb_context *tdb,
enum tdb_debug_level level, void *priv, enum tdb_debug_level level, void *priv,
const char *fmt, ...); const char *message);
static inline bool data_equal(struct tdb_data a, struct tdb_data b) static inline bool data_equal(struct tdb_data a, struct tdb_data b)
{ {
......
...@@ -12,7 +12,7 @@ int main(int argc, char *argv[]) ...@@ -12,7 +12,7 @@ int main(int argc, char *argv[])
{ {
unsigned int i; unsigned int i;
struct tdb_used_record rec; struct tdb_used_record rec;
struct tdb_context tdb = { .log = tap_log_fn, .log_priv = NULL }; struct tdb_context tdb = { .logfn = tap_log_fn };
plan_tests(64 + 32 + 48*6 + 1); plan_tests(64 + 32 + 48*6 + 1);
......
...@@ -13,7 +13,7 @@ static int log_count = 0; ...@@ -13,7 +13,7 @@ static int log_count = 0;
/* Normally we get a log when setting random seed. */ /* Normally we get a log when setting random seed. */
static void my_log_fn(struct tdb_context *tdb, static void my_log_fn(struct tdb_context *tdb,
enum tdb_debug_level level, void *priv, enum tdb_debug_level level, void *priv,
const char *fmt, ...) const char *message)
{ {
log_count++; log_count++;
} }
......
...@@ -120,7 +120,7 @@ int main(int argc, char *argv[]) ...@@ -120,7 +120,7 @@ int main(int argc, char *argv[])
hattr.base.next = &tap_log_attr; hattr.base.next = &tap_log_attr;
plan_tests(sizeof(flags) / sizeof(flags[0]) * 50 + 1); plan_tests(sizeof(flags) / sizeof(flags[0]) * 53 + 1);
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) { for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
tdb = tdb_open("run-traverse.tdb", flags[i], tdb = tdb_open("run-traverse.tdb", flags[i],
O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr); O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
...@@ -182,6 +182,7 @@ int main(int argc, char *argv[]) ...@@ -182,6 +182,7 @@ int main(int argc, char *argv[])
ok1(td.low <= NUM_RECORDS / 2); ok1(td.low <= NUM_RECORDS / 2);
ok1(td.high > NUM_RECORDS / 2); ok1(td.high > NUM_RECORDS / 2);
ok1(tdb_check(tdb, NULL, NULL) == 0); ok1(tdb_check(tdb, NULL, NULL) == 0);
ok1(tap_log_messages == 0);
/* Growing traverse. Expect failure on r/o traverse. */ /* Growing traverse. Expect failure on r/o traverse. */
tgd.calls = 0; tgd.calls = 0;
...@@ -193,6 +194,8 @@ int main(int argc, char *argv[]) ...@@ -193,6 +194,8 @@ int main(int argc, char *argv[])
ok1(tgd.error == TDB_ERR_RDONLY); ok1(tgd.error == TDB_ERR_RDONLY);
ok1(tgd.calls == 1); ok1(tgd.calls == 1);
ok1(!tgd.mismatch); ok1(!tgd.mismatch);
ok1(tap_log_messages == 1);
tap_log_messages = 0;
ok1(tdb_check(tdb, NULL, NULL) == 0); ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Deleting traverse. Expect failure on r/o traverse. */ /* Deleting traverse. Expect failure on r/o traverse. */
...@@ -209,6 +212,8 @@ int main(int argc, char *argv[]) ...@@ -209,6 +212,8 @@ int main(int argc, char *argv[])
ok1(!td.mismatch); ok1(!td.mismatch);
ok1(td.calls == 1); ok1(td.calls == 1);
ok1(td.low == td.high); ok1(td.low == td.high);
ok1(tap_log_messages == 1);
tap_log_messages = 0;
ok1(tdb_check(tdb, NULL, NULL) == 0); ok1(tdb_check(tdb, NULL, NULL) == 0);
/* Deleting traverse (delete everything). */ /* Deleting traverse (delete everything). */
......
...@@ -169,10 +169,9 @@ static int transaction_read(struct tdb_context *tdb, tdb_off_t off, void *buf, ...@@ -169,10 +169,9 @@ static int transaction_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
return 0; return 0;
fail: fail:
tdb->ecode = TDB_ERR_IO; tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "transaction_read: failed at off=%zu len=%zu",
"transaction_read: failed at off=%llu len=%llu\n", (size_t)off, (size_t)len);
(long long)off, (long long)len);
tdb->transaction->transaction_error = 1; tdb->transaction->transaction_error = 1;
return -1; return -1;
} }
...@@ -188,12 +187,10 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -188,12 +187,10 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
/* Only a commit is allowed on a prepared transaction */ /* Only a commit is allowed on a prepared transaction */
if (tdb->transaction->prepared) { if (tdb->transaction->prepared) {
tdb->ecode = TDB_ERR_EINVAL; tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
"transaction_write: transaction already prepared," "transaction_write: transaction already prepared,"
" write not allowed\n"); " write not allowed");
tdb->transaction->transaction_error = 1; goto fail;
return -1;
} }
/* break it up into block sized chunks */ /* break it up into block sized chunks */
...@@ -228,7 +225,8 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -228,7 +225,8 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
(blk+1)*sizeof(uint8_t *)); (blk+1)*sizeof(uint8_t *));
} }
if (new_blocks == NULL) { if (new_blocks == NULL) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
"transaction_write: failed to allocate");
goto fail; goto fail;
} }
memset(&new_blocks[tdb->transaction->num_blocks], 0, memset(&new_blocks[tdb->transaction->num_blocks], 0,
...@@ -242,9 +240,9 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -242,9 +240,9 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
if (tdb->transaction->blocks[blk] == NULL) { if (tdb->transaction->blocks[blk] == NULL) {
tdb->transaction->blocks[blk] = (uint8_t *)calloc(getpagesize(), 1); tdb->transaction->blocks[blk] = (uint8_t *)calloc(getpagesize(), 1);
if (tdb->transaction->blocks[blk] == NULL) { if (tdb->transaction->blocks[blk] == NULL) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
tdb->transaction->transaction_error = 1; "transaction_write: failed to allocate");
return -1; goto fail;
} }
if (tdb->transaction->old_map_size > blk * getpagesize()) { if (tdb->transaction->old_map_size > blk * getpagesize()) {
tdb_len_t len2 = getpagesize(); tdb_len_t len2 = getpagesize();
...@@ -254,6 +252,10 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -254,6 +252,10 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
if (tdb->transaction->io_methods->read(tdb, blk * getpagesize(), if (tdb->transaction->io_methods->read(tdb, blk * getpagesize(),
tdb->transaction->blocks[blk], tdb->transaction->blocks[blk],
len2) != 0) { len2) != 0) {
tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
"transaction_write: failed to"
" read old block: %s",
strerror(errno));
SAFE_FREE(tdb->transaction->blocks[blk]); SAFE_FREE(tdb->transaction->blocks[blk]);
goto fail; goto fail;
} }
...@@ -278,10 +280,6 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off, ...@@ -278,10 +280,6 @@ static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
return 0; return 0;
fail: fail:
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
"transaction_write: failed at off=%llu len=%llu\n",
(long long)((blk*getpagesize()) + off),
(long long)len);
tdb->transaction->transaction_error = 1; tdb->transaction->transaction_error = 1;
return -1; return -1;
} }
...@@ -341,6 +339,12 @@ static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, bool probe) ...@@ -341,6 +339,12 @@ static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, bool probe)
return 0; return 0;
} }
tdb->ecode = TDB_ERR_IO; tdb->ecode = TDB_ERR_IO;
if (!probe) {
tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
"tdb_oob len %lld beyond transaction size %lld",
(long long)len,
(long long)tdb->map_size);
}
return -1; return -1;
} }
...@@ -383,9 +387,9 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t ...@@ -383,9 +387,9 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t
} }
if (fsync(tdb->fd) != 0) { if (fsync(tdb->fd) != 0) {
tdb->ecode = TDB_ERR_IO; tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_transaction: fsync failed: %s",
"tdb_transaction: fsync failed\n"); strerror(errno));
return -1; return -1;
} }
#ifdef MS_SYNC #ifdef MS_SYNC
...@@ -393,9 +397,8 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t ...@@ -393,9 +397,8 @@ static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t
tdb_off_t moffset = offset & ~(getpagesize()-1); tdb_off_t moffset = offset & ~(getpagesize()-1);
if (msync(moffset + (char *)tdb->map_ptr, if (msync(moffset + (char *)tdb->map_ptr,
length + (offset - moffset), MS_SYNC) != 0) { length + (offset - moffset), MS_SYNC) != 0) {
tdb->ecode = TDB_ERR_IO; tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, "tdb_transaction: msync failed: %s",
"tdb_transaction: msync failed - %s\n",
strerror(errno)); strerror(errno));
return -1; return -1;
} }
...@@ -410,9 +413,8 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb) ...@@ -410,9 +413,8 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
int i; int i;
if (tdb->transaction == NULL) { if (tdb->transaction == NULL) {
tdb->ecode = TDB_ERR_EINVAL; tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_transaction_cancel: no transaction");
"tdb_transaction_cancel: no transaction\n");
return; return;
} }
...@@ -441,9 +443,9 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb) ...@@ -441,9 +443,9 @@ static void _tdb_transaction_cancel(struct tdb_context *tdb)
&invalid, sizeof(invalid)) == -1 || &invalid, sizeof(invalid)) == -1 ||
transaction_sync(tdb, tdb->transaction->magic_offset, transaction_sync(tdb, tdb->transaction->magic_offset,
sizeof(invalid)) == -1) { sizeof(invalid)) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_cancel: failed to remove" "tdb_transaction_cancel: failed to remove"
" recovery magic\n"); " recovery magic");
} }
} }
...@@ -469,16 +471,17 @@ int tdb_transaction_start(struct tdb_context *tdb) ...@@ -469,16 +471,17 @@ int tdb_transaction_start(struct tdb_context *tdb)
{ {
/* some sanity checks */ /* some sanity checks */
if (tdb->read_only || (tdb->flags & TDB_INTERNAL)) { if (tdb->read_only || (tdb->flags & TDB_INTERNAL)) {
tdb->ecode = TDB_ERR_EINVAL; tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
"tdb_transaction_start: cannot start a transaction" "tdb_transaction_start: cannot start a transaction"
" on a read-only or internal db\n"); " on a read-only or internal db");
return -1; return -1;
} }
/* cope with nested tdb_transaction_start() calls */ /* cope with nested tdb_transaction_start() calls */
if (tdb->transaction != NULL) { if (tdb->transaction != NULL) {
tdb->ecode = TDB_ERR_NESTING; tdb_logerr(tdb, TDB_ERR_NESTING, TDB_DEBUG_ERROR,
"tdb_transaction_start:"
" already inside transaction");
return -1; return -1;
} }
...@@ -486,17 +489,17 @@ int tdb_transaction_start(struct tdb_context *tdb) ...@@ -486,17 +489,17 @@ int tdb_transaction_start(struct tdb_context *tdb)
/* the caller must not have any locks when starting a /* the caller must not have any locks when starting a
transaction as otherwise we'll be screwed by lack transaction as otherwise we'll be screwed by lack
of nested locks in posix */ of nested locks in posix */
tdb->ecode = TDB_ERR_LOCK; tdb_logerr(tdb, TDB_ERR_LOCK, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
"tdb_transaction_start: cannot start a transaction" "tdb_transaction_start: cannot start a transaction"
" with locks held\n"); " with locks held");
return -1; return -1;
} }
tdb->transaction = (struct tdb_transaction *) tdb->transaction = (struct tdb_transaction *)
calloc(sizeof(struct tdb_transaction), 1); calloc(sizeof(struct tdb_transaction), 1);
if (tdb->transaction == NULL) { if (tdb->transaction == NULL) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_ERROR,
"tdb_transaction_start: cannot allocate");
return -1; return -1;
} }
...@@ -585,17 +588,17 @@ static int tdb_recovery_allocate(struct tdb_context *tdb, ...@@ -585,17 +588,17 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery)); recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery));
if (recovery_head == TDB_OFF_ERR) { if (recovery_head == TDB_OFF_ERR) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_recovery_allocate:" "tdb_recovery_allocate:"
" failed to read recovery head\n"); " failed to read recovery head");
return -1; return -1;
} }
if (recovery_head != 0) { if (recovery_head != 0) {
if (methods->read(tdb, recovery_head, &rec, sizeof(rec))) { if (methods->read(tdb, recovery_head, &rec, sizeof(rec))) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_recovery_allocate:" "tdb_recovery_allocate:"
" failed to read recovery record\n"); " failed to read recovery record");
return -1; return -1;
} }
tdb_convert(tdb, &rec, sizeof(rec)); tdb_convert(tdb, &rec, sizeof(rec));
...@@ -624,9 +627,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb, ...@@ -624,9 +627,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
add_stat(tdb, frees, 1); add_stat(tdb, frees, 1);
if (add_free_record(tdb, recovery_head, if (add_free_record(tdb, recovery_head,
sizeof(rec) + rec.max_len) != 0) { sizeof(rec) + rec.max_len) != 0) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_recovery_allocate:" "tdb_recovery_allocate:"
" failed to free previous recovery area\n"); " failed to free previous recovery area");
return -1; return -1;
} }
} }
...@@ -650,9 +653,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb, ...@@ -650,9 +653,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
sizeof(rec) + *recovery_max_size; sizeof(rec) + *recovery_max_size;
tdb->map_size = tdb->transaction->old_map_size; tdb->map_size = tdb->transaction->old_map_size;
if (methods->expand_file(tdb, addition) == -1) { if (methods->expand_file(tdb, addition) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_recovery_allocate:" "tdb_recovery_allocate:"
" failed to create recovery area\n"); " failed to create recovery area");
return -1; return -1;
} }
...@@ -666,9 +669,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb, ...@@ -666,9 +669,9 @@ static int tdb_recovery_allocate(struct tdb_context *tdb,
tdb_convert(tdb, &recovery_head, sizeof(recovery_head)); tdb_convert(tdb, &recovery_head, sizeof(recovery_head));
if (methods->write(tdb, offsetof(struct tdb_header, recovery), if (methods->write(tdb, offsetof(struct tdb_header, recovery),
&recovery_head, sizeof(tdb_off_t)) == -1) { &recovery_head, sizeof(tdb_off_t)) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_recovery_allocate:" "tdb_recovery_allocate:"
" failed to write recovery head\n"); " failed to write recovery head");
return -1; return -1;
} }
transaction_write_existing(tdb, offsetof(struct tdb_header, recovery), transaction_write_existing(tdb, offsetof(struct tdb_header, recovery),
...@@ -714,7 +717,8 @@ static int transaction_setup_recovery(struct tdb_context *tdb, ...@@ -714,7 +717,8 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
data = (unsigned char *)malloc(recovery_size + sizeof(*rec)); data = (unsigned char *)malloc(recovery_size + sizeof(*rec));
if (data == NULL) { if (data == NULL) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
"transaction_setup_recovery: cannot allocate");
return -1; return -1;
} }
...@@ -744,10 +748,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb, ...@@ -744,10 +748,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
continue; continue;
} }
if (offset + length > tdb->map_size) { if (offset + length > tdb->map_size) {
tdb->ecode = TDB_ERR_CORRUPT; tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
"tdb_transaction_setup_recovery:" "tdb_transaction_setup_recovery:"
" transaction data over new region boundary\n"); " transaction data over new region boundary");
free(data); free(data);
return -1; return -1;
} }
...@@ -775,9 +778,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb, ...@@ -775,9 +778,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
/* write the recovery data to the recovery area */ /* write the recovery data to the recovery area */
if (methods->write(tdb, recovery_offset, data, if (methods->write(tdb, recovery_offset, data,
sizeof(*rec) + recovery_size) == -1) { sizeof(*rec) + recovery_size) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_setup_recovery:" "tdb_transaction_setup_recovery:"
" failed to write recovery data\n"); " failed to write recovery data");
free(data); free(data);
return -1; return -1;
} }
...@@ -802,9 +805,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb, ...@@ -802,9 +805,9 @@ static int transaction_setup_recovery(struct tdb_context *tdb,
magic); magic);
if (methods->write(tdb, *magic_offset, &magic, sizeof(magic)) == -1) { if (methods->write(tdb, *magic_offset, &magic, sizeof(magic)) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_setup_recovery:" "tdb_transaction_setup_recovery:"
" failed to write recovery magic\n"); " failed to write recovery magic");
return -1; return -1;
} }
transaction_write_existing(tdb, *magic_offset, &magic, sizeof(magic)); transaction_write_existing(tdb, *magic_offset, &magic, sizeof(magic));
...@@ -822,27 +825,24 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb) ...@@ -822,27 +825,24 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
const struct tdb_methods *methods; const struct tdb_methods *methods;
if (tdb->transaction == NULL) { if (tdb->transaction == NULL) {
tdb->ecode = TDB_ERR_EINVAL; tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_transaction_prepare_commit: no transaction");
"tdb_transaction_prepare_commit: no transaction\n");
return -1; return -1;
} }
if (tdb->transaction->prepared) { if (tdb->transaction->prepared) {
tdb->ecode = TDB_ERR_EINVAL;
_tdb_transaction_cancel(tdb); _tdb_transaction_cancel(tdb);
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
"tdb_transaction_prepare_commit:" "tdb_transaction_prepare_commit:"
" transaction already prepared\n"); " transaction already prepared");
return -1; return -1;
} }
if (tdb->transaction->transaction_error) { if (tdb->transaction->transaction_error) {
tdb->ecode = TDB_ERR_IO;
_tdb_transaction_cancel(tdb); _tdb_transaction_cancel(tdb);
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
"tdb_transaction_prepare_commit:" "tdb_transaction_prepare_commit:"
" transaction error pending\n"); " transaction error pending");
return -1; return -1;
} }
...@@ -861,9 +861,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb) ...@@ -861,9 +861,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
/* upgrade the main transaction lock region to a write lock */ /* upgrade the main transaction lock region to a write lock */
if (tdb_allrecord_upgrade(tdb) == -1) { if (tdb_allrecord_upgrade(tdb) == -1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
"tdb_transaction_prepare_commit:" "tdb_transaction_prepare_commit:"
" failed to upgrade hash locks\n"); " failed to upgrade hash locks");
_tdb_transaction_cancel(tdb); _tdb_transaction_cancel(tdb);
return -1; return -1;
} }
...@@ -871,9 +871,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb) ...@@ -871,9 +871,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
/* get the open lock - this prevents new users attaching to the database /* get the open lock - this prevents new users attaching to the database
during the commit */ during the commit */
if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) { if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
"tdb_transaction_prepare_commit:" "tdb_transaction_prepare_commit:"
" failed to get open lock\n"); " failed to get open lock");
_tdb_transaction_cancel(tdb); _tdb_transaction_cancel(tdb);
return -1; return -1;
} }
...@@ -882,9 +882,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb) ...@@ -882,9 +882,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
if (!(tdb->flags & TDB_NOSYNC)) { if (!(tdb->flags & TDB_NOSYNC)) {
/* write the recovery data to the end of the file */ /* write the recovery data to the end of the file */
if (transaction_setup_recovery(tdb, &tdb->transaction->magic_offset) == -1) { if (transaction_setup_recovery(tdb, &tdb->transaction->magic_offset) == -1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_prepare_commit:" "tdb_transaction_prepare_commit:"
" failed to setup recovery data\n"); " failed to setup recovery data");
_tdb_transaction_cancel(tdb); _tdb_transaction_cancel(tdb);
return -1; return -1;
} }
...@@ -898,9 +898,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb) ...@@ -898,9 +898,9 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
/* Restore original map size for tdb_expand_file */ /* Restore original map size for tdb_expand_file */
tdb->map_size = tdb->transaction->old_map_size; tdb->map_size = tdb->transaction->old_map_size;
if (methods->expand_file(tdb, add) == -1) { if (methods->expand_file(tdb, add) == -1) {
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_ERROR,
"tdb_transaction_prepare_commit:" "tdb_transaction_prepare_commit:"
" expansion failed\n"); " expansion failed");
_tdb_transaction_cancel(tdb); _tdb_transaction_cancel(tdb);
return -1; return -1;
} }
...@@ -928,19 +928,18 @@ int tdb_transaction_commit(struct tdb_context *tdb) ...@@ -928,19 +928,18 @@ int tdb_transaction_commit(struct tdb_context *tdb)
int i; int i;
if (tdb->transaction == NULL) { if (tdb->transaction == NULL) {
tdb->ecode = TDB_ERR_EINVAL; tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_DEBUG_ERROR,
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_transaction_commit: no transaction");
"tdb_transaction_commit: no transaction\n");
return -1; return -1;
} }
tdb_trace(tdb, "tdb_transaction_commit"); tdb_trace(tdb, "tdb_transaction_commit");
if (tdb->transaction->transaction_error) { if (tdb->transaction->transaction_error) {
tdb->ecode = TDB_ERR_IO;
tdb_transaction_cancel(tdb); tdb_transaction_cancel(tdb);
tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_IO, TDB_DEBUG_ERROR,
"tdb_transaction_commit: transaction error pending\n"); "tdb_transaction_commit:"
" transaction error pending");
return -1; return -1;
} }
...@@ -981,9 +980,9 @@ int tdb_transaction_commit(struct tdb_context *tdb) ...@@ -981,9 +980,9 @@ int tdb_transaction_commit(struct tdb_context *tdb)
if (methods->write(tdb, offset, tdb->transaction->blocks[i], if (methods->write(tdb, offset, tdb->transaction->blocks[i],
length) == -1) { length) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_commit:" "tdb_transaction_commit:"
" write failed during commit\n"); " write failed during commit");
/* we've overwritten part of the data and /* we've overwritten part of the data and
possibly expanded the file, so we need to possibly expanded the file, so we need to
...@@ -1043,9 +1042,9 @@ int tdb_transaction_recover(struct tdb_context *tdb) ...@@ -1043,9 +1042,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
/* find the recovery area */ /* find the recovery area */
recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery)); recovery_head = tdb_read_off(tdb, offsetof(struct tdb_header,recovery));
if (recovery_head == TDB_OFF_ERR) { if (recovery_head == TDB_OFF_ERR) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_recover:" "tdb_transaction_recover:"
" failed to read recovery head\n"); " failed to read recovery head");
return -1; return -1;
} }
...@@ -1056,9 +1055,9 @@ int tdb_transaction_recover(struct tdb_context *tdb) ...@@ -1056,9 +1055,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
/* read the recovery record */ /* read the recovery record */
if (tdb_read_convert(tdb, recovery_head, &rec, sizeof(rec)) == -1) { if (tdb_read_convert(tdb, recovery_head, &rec, sizeof(rec)) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_recover:" "tdb_transaction_recover:"
" failed to read recovery record\n"); " failed to read recovery record");
return -1; return -1;
} }
...@@ -1068,10 +1067,9 @@ int tdb_transaction_recover(struct tdb_context *tdb) ...@@ -1068,10 +1067,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
} }
if (tdb->read_only) { if (tdb->read_only) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_FATAL,
"tdb_transaction_recover:" "tdb_transaction_recover:"
" attempt to recover read only database\n"); " attempt to recover read only database");
tdb->ecode = TDB_ERR_CORRUPT;
return -1; return -1;
} }
...@@ -1079,19 +1077,18 @@ int tdb_transaction_recover(struct tdb_context *tdb) ...@@ -1079,19 +1077,18 @@ int tdb_transaction_recover(struct tdb_context *tdb)
data = (unsigned char *)malloc(rec.len); data = (unsigned char *)malloc(rec.len);
if (data == NULL) { if (data == NULL) {
tdb->ecode = TDB_ERR_OOM; tdb_logerr(tdb, TDB_ERR_OOM, TDB_DEBUG_FATAL,
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
"tdb_transaction_recover:" "tdb_transaction_recover:"
" failed to allocate recovery data\n"); " failed to allocate recovery data");
return -1; return -1;
} }
/* read the full recovery data */ /* read the full recovery data */
if (tdb->methods->read(tdb, recovery_head + sizeof(rec), data, if (tdb->methods->read(tdb, recovery_head + sizeof(rec), data,
rec.len) == -1) { rec.len) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_recover:" "tdb_transaction_recover:"
" failed to read recovery data\n"); " failed to read recovery data");
return -1; return -1;
} }
...@@ -1107,9 +1104,9 @@ int tdb_transaction_recover(struct tdb_context *tdb) ...@@ -1107,9 +1104,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
if (tdb->methods->write(tdb, ofs, p, len) == -1) { if (tdb->methods->write(tdb, ofs, p, len) == -1) {
free(data); free(data);
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_recover:" "tdb_transaction_recover:"
" failed to recover %zu bytes at offset %zu\n", " failed to recover %zu bytes at offset %zu",
(size_t)len, (size_t)ofs); (size_t)len, (size_t)ofs);
return -1; return -1;
} }
...@@ -1119,8 +1116,8 @@ int tdb_transaction_recover(struct tdb_context *tdb) ...@@ -1119,8 +1116,8 @@ int tdb_transaction_recover(struct tdb_context *tdb)
free(data); free(data);
if (transaction_sync(tdb, 0, tdb->map_size) == -1) { if (transaction_sync(tdb, 0, tdb->map_size) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_recover: failed to sync recovery\n"); "tdb_transaction_recover: failed to sync recovery");
return -1; return -1;
} }
...@@ -1128,9 +1125,9 @@ int tdb_transaction_recover(struct tdb_context *tdb) ...@@ -1128,9 +1125,9 @@ int tdb_transaction_recover(struct tdb_context *tdb)
if (recovery_eof <= recovery_head) { if (recovery_eof <= recovery_head) {
if (tdb_write_off(tdb, offsetof(struct tdb_header,recovery), 0) if (tdb_write_off(tdb, offsetof(struct tdb_header,recovery), 0)
== -1) { == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_recover:" "tdb_transaction_recover:"
" failed to remove recovery head\n"); " failed to remove recovery head");
return -1; return -1;
} }
} }
...@@ -1140,20 +1137,20 @@ int tdb_transaction_recover(struct tdb_context *tdb) ...@@ -1140,20 +1137,20 @@ int tdb_transaction_recover(struct tdb_context *tdb)
recovery_head recovery_head
+ offsetof(struct tdb_recovery_record, magic), + offsetof(struct tdb_recovery_record, magic),
TDB_RECOVERY_INVALID_MAGIC) == -1) { TDB_RECOVERY_INVALID_MAGIC) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_recover:" "tdb_transaction_recover:"
" failed to remove recovery magic\n"); " failed to remove recovery magic");
return -1; return -1;
} }
if (transaction_sync(tdb, 0, recovery_eof) == -1) { if (transaction_sync(tdb, 0, recovery_eof) == -1) {
tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv, tdb_logerr(tdb, tdb->ecode, TDB_DEBUG_FATAL,
"tdb_transaction_recover: failed to sync2 recovery\n"); "tdb_transaction_recover: failed to sync2 recovery");
return -1; return -1;
} }
tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv, tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_TRACE,
"tdb_transaction_recover: recovered %zu byte database\n", "tdb_transaction_recover: recovered %zu byte database",
(size_t)recovery_eof); (size_t)recovery_eof);
/* all done */ /* all done */
......
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