return true;
}
-static enum TDB_ERROR check_header(struct tdb_context *tdb, tdb_off_t *recovery)
+static enum TDB_ERROR check_header(struct tdb_context *tdb, tdb_off_t *recovery,
+ uint64_t *features)
{
uint64_t hash_test;
struct tdb_header hdr;
hdr.magic_food);
}
+ /* Features which are used must be a subset of features offered. */
+ if (hdr.features_used & ~hdr.features_offered) {
+ return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+ "check: features used (0x%llx) which"
+ " are not offered (0x%llx)",
+ (long long)hdr.features_used,
+ (long long)hdr.features_offered);
+ }
+
+ *features = hdr.features_offered;
*recovery = hdr.recovery;
if (*recovery) {
- if (*recovery < sizeof(hdr) || *recovery > tdb->map_size) {
+ if (*recovery < sizeof(hdr)
+ || *recovery > tdb->file->map_size) {
return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"tdb_check:"
" invalid recovery offset %zu",
size_t *num_found,
enum TDB_ERROR (*check)(TDB_DATA,
TDB_DATA, void *),
- void *private_data);
+ void *data);
static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb,
tdb_off_t off,
enum TDB_ERROR (*check)(TDB_DATA,
TDB_DATA,
void *),
- void *private_data)
+ void *data)
{
struct tdb_used_record rec;
enum TDB_ERROR ecode;
off += sizeof(rec);
ecode = check_hash_tree(tdb, off, 0, hash, 64,
- used, num_used, num_found, check, private_data);
+ used, num_used, num_found, check, data);
if (ecode != TDB_SUCCESS) {
return ecode;
}
return TDB_SUCCESS;
(*num_found)++;
return check_hash_chain(tdb, off, hash, used, num_used, num_found,
- check, private_data);
+ check, data);
}
static enum TDB_ERROR check_hash_record(struct tdb_context *tdb,
enum TDB_ERROR (*check)(TDB_DATA,
TDB_DATA,
void *),
- void *private_data)
+ void *data)
{
struct tdb_used_record rec;
enum TDB_ERROR ecode;
if (hprefix_bits >= 64)
return check_hash_chain(tdb, off, hprefix, used, num_used,
- num_found, check, private_data);
+ num_found, check, data);
ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
if (ecode != TDB_SUCCESS) {
return check_hash_tree(tdb, off,
TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS,
hprefix, hprefix_bits,
- used, num_used, num_found, check, private_data);
+ used, num_used, num_found, check, data);
}
static int off_cmp(const tdb_off_t *a, const tdb_off_t *b)
size_t *num_found,
enum TDB_ERROR (*check)(TDB_DATA,
TDB_DATA, void *),
- void *private_data)
+ void *data)
{
unsigned int g, b;
const tdb_off_t *hash;
+ group_bits
+ TDB_HASH_GROUP_BITS,
used, num_used, num_found,
- check, private_data);
+ check, data);
if (ecode != TDB_SUCCESS) {
goto fail;
}
check:
if (check) {
- TDB_DATA key, data;
- key.dsize = rec_key_length(&rec);
- data.dsize = rec_data_length(&rec);
- key.dptr = (void *)tdb_access_read(tdb,
- off + sizeof(rec),
- key.dsize + data.dsize,
- false);
- if (TDB_PTR_IS_ERR(key.dptr)) {
- ecode = TDB_PTR_ERR(key.dptr);
+ TDB_DATA k, d;
+ const unsigned char *kptr;
+
+ kptr = tdb_access_read(tdb,
+ off + sizeof(rec),
+ rec_key_length(&rec)
+ + rec_data_length(&rec),
+ false);
+ if (TDB_PTR_IS_ERR(kptr)) {
+ ecode = TDB_PTR_ERR(kptr);
goto fail;
}
- data.dptr = key.dptr + key.dsize;
- ecode = check(key, data, private_data);
+
+ k = tdb_mkdata(kptr, rec_key_length(&rec));
+ d = tdb_mkdata(kptr + k.dsize,
+ rec_data_length(&rec));
+ ecode = check(k, d, data);
+ tdb_access_release(tdb, kptr);
if (ecode != TDB_SUCCESS) {
goto fail;
}
- tdb_access_release(tdb, key.dptr);
}
}
}
tdb_off_t used[],
size_t num_used, size_t num_ftables,
int (*check)(TDB_DATA, TDB_DATA, void *),
- void *private_data)
+ void *data)
{
/* Free tables also show up as used. */
size_t num_found = num_ftables;
ecode = check_hash_tree(tdb, offsetof(struct tdb_header, hashtable),
TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS,
0, 0, used, num_used, &num_found,
- check, private_data);
+ check, data);
if (ecode == TDB_SUCCESS) {
if (num_found != num_used) {
ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
(long long)off,
bucket, size_to_bucket(frec_len(frec)));
}
- if (prev != frec_prev(frec)) {
+ if (prev && prev != frec_prev(frec)) {
return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
"tdb_check: offset %llu bad prev"
" (%llu vs %llu)",
}
for (i = 0; i < TDB_FREE_BUCKETS; i++) {
- tdb_off_t off, prev = 0, *p;
+ tdb_off_t off, prev = 0, *p, first = 0;
struct tdb_free_record f;
h = bucket_off(ftable_off, i);
if (TDB_OFF_IS_ERR(off)) {
return off;
}
+ if (!first) {
+ off &= TDB_OFF_MASK;
+ first = off;
+ }
ecode = tdb_read_convert(tdb, off, &f, sizeof(f));
if (ecode != TDB_SUCCESS) {
return ecode;
(*num_found)++;
prev = off;
}
+
+ if (first) {
+ /* Now we can check first back pointer. */
+ ecode = tdb_read_convert(tdb, first, &f, sizeof(f));
+ if (ecode != TDB_SUCCESS) {
+ return ecode;
+ }
+ ecode = check_free(tdb, first, &f, prev, ftable_num, i);
+ if (ecode != TDB_SUCCESS) {
+ return ecode;
+ }
+ }
}
return TDB_SUCCESS;
}
size_t len;
enum TDB_ERROR ecode;
- for (len = 0; off + len < tdb->map_size; len++) {
+ for (len = 0; off + len < tdb->file->map_size; len++) {
char c;
ecode = tdb->methods->tread(tdb, off, &c, 1);
if (ecode != TDB_SUCCESS) {
static enum TDB_ERROR check_linear(struct tdb_context *tdb,
tdb_off_t **used, size_t *num_used,
tdb_off_t **fr, size_t *num_free,
- tdb_off_t recovery)
+ uint64_t features, tdb_off_t recovery)
{
tdb_off_t off;
tdb_len_t len;
enum TDB_ERROR ecode;
bool found_recovery = false;
- for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) {
+ for (off = sizeof(struct tdb_header);
+ off < tdb->file->map_size;
+ off += len) {
union {
struct tdb_used_record u;
struct tdb_free_record f;
tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
"Dead space at %zu-%zu (of %zu)",
(size_t)off, (size_t)(off + len),
- (size_t)tdb->map_size);
+ (size_t)tdb->file->map_size);
}
} else if (rec.r.magic == TDB_RECOVERY_MAGIC) {
ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec.r));
" length %zu",
(size_t)rec.r.len);
}
- if (rec.r.eof > tdb->map_size) {
+ if (rec.r.eof > tdb->file->map_size) {
return tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_LOG_ERROR,
"tdb_check: invalid old EOF"
len = sizeof(rec.r) + rec.r.max_len;
} else if (frec_magic(&rec.f) == TDB_FREE_MAGIC) {
len = sizeof(rec.u) + frec_len(&rec.f);
- if (off + len > tdb->map_size) {
+ if (off + len > tdb->file->map_size) {
return tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_LOG_ERROR,
"tdb_check: free overlength"
extra = rec_extra_padding(&rec.u);
len = sizeof(rec.u) + klen + dlen + extra;
- if (off + len > tdb->map_size) {
+ if (off + len > tdb->file->map_size) {
return tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_LOG_ERROR,
"tdb_check: used overlength"
(long long)len,
(long long)off);
}
+
+ /* Check that records have correct 0 at end (but may
+ * not in future). */
+ if (extra && !features) {
+ const char *p;
+ char c;
+ p = tdb_access_read(tdb, off + sizeof(rec.u)
+ + klen + dlen, 1, false);
+ if (TDB_PTR_IS_ERR(p))
+ return TDB_PTR_ERR(p);
+ c = *p;
+ tdb_access_release(tdb, p);
+
+ if (c != '\0') {
+ return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+ TDB_LOG_ERROR,
+ "tdb_check:"
+ " non-zero extra"
+ " at %llu",
+ (long long)off);
+ }
+ }
} else {
return tdb_logerr(tdb, TDB_ERR_CORRUPT,
TDB_LOG_ERROR,
return TDB_SUCCESS;
}
-enum TDB_ERROR tdb_check(struct tdb_context *tdb,
- enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data,
- void *private_data),
- void *private_data)
+enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
+ enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA, void *),
+ void *data)
{
tdb_off_t *fr = NULL, *used = NULL, ft, recovery;
size_t num_free = 0, num_used = 0, num_found = 0, num_ftables = 0;
+ uint64_t features;
enum TDB_ERROR ecode;
ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
if (ecode != TDB_SUCCESS) {
- return ecode;
+ return tdb->last_error = ecode;
}
ecode = tdb_lock_expand(tdb, F_RDLCK);
if (ecode != TDB_SUCCESS) {
tdb_allrecord_unlock(tdb, F_RDLCK);
- return ecode;
+ return tdb->last_error = ecode;
}
- ecode = check_header(tdb, &recovery);
+ ecode = check_header(tdb, &recovery, &features);
if (ecode != TDB_SUCCESS)
goto out;
/* First we do a linear scan, checking all records. */
- ecode = check_linear(tdb, &used, &num_used, &fr, &num_free, recovery);
+ ecode = check_linear(tdb, &used, &num_used, &fr, &num_free, features,
+ recovery);
if (ecode != TDB_SUCCESS)
goto out;
}
/* FIXME: Check key uniqueness? */
- ecode = check_hash(tdb, used, num_used, num_ftables, check,
- private_data);
+ ecode = check_hash(tdb, used, num_used, num_ftables, check, data);
if (ecode != TDB_SUCCESS)
goto out;
tdb_unlock_expand(tdb, F_RDLCK);
free(fr);
free(used);
- return ecode;
+ return tdb->last_error = ecode;
}