X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Fcheck.c;h=54f96bb0ea8283583faf91693decd8d467970423;hp=5500883ff2b04cad383953877f4c8f415b06b0b2;hb=ec88af5dd11a003beb8cd3fbe420f9d5e5dcf5d9;hpb=1a24a8708494668c07e5c02284bfc2ef3b09603b diff --git a/ccan/tdb2/check.c b/ccan/tdb2/check.c index 5500883f..54f96bb0 100644 --- a/ccan/tdb2/check.c +++ b/ccan/tdb2/check.c @@ -30,66 +30,74 @@ static bool append(tdb_off_t **arr, size_t *num, tdb_off_t off) return true; } -static bool check_header(struct tdb_context *tdb) +static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery) { uint64_t hash_test; + struct tdb_header hdr; + + if (tdb_read_convert(tdb, 0, &hdr, sizeof(hdr)) == -1) + return false; + /* magic food should not be converted, so convert back. */ + tdb_convert(tdb, hdr.magic_food, sizeof(hdr.magic_food)); hash_test = TDB_HASH_MAGIC; hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test)); - if (tdb->header.hash_test != hash_test) { + if (hdr.hash_test != hash_test) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "check: hash test %llu should be %llu\n", - tdb->header.hash_test, hash_test); + (long long)hdr.hash_test, + (long long)hash_test); return false; } - if (strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0) { + + if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "check: bad magic '%.*s'\n", - sizeof(tdb->header.magic_food), - tdb->header.magic_food); - return false; - } - if (tdb->header.v.hash_bits < INITIAL_HASH_BITS) { - tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "check: bad hash bits %llu\n", - (long long)tdb->header.v.hash_bits); + (unsigned)sizeof(hdr.magic_food), hdr.magic_food); return false; } - /* We check hash_off later. */ + *recovery = hdr.recovery; + if (*recovery) { + if (*recovery < sizeof(hdr) || *recovery > tdb->map_size) { + tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, + "tdb_check: invalid recovery offset %zu\n", + (size_t)*recovery); + return false; + } + } /* Don't check reserved: they *can* be used later. */ return true; } -static int off_cmp(const tdb_off_t *a, const tdb_off_t *b) -{ - /* Can overflow an int. */ - return *a > *b ? 1 - : *a < *b ? -1 - : 0; -} - -static bool check_hash_list(struct tdb_context *tdb, +static bool check_hash_tree(struct tdb_context *tdb, + tdb_off_t off, unsigned int group_bits, + uint64_t hprefix, + unsigned hprefix_bits, tdb_off_t used[], - size_t num_used) + size_t num_used, + size_t *num_found); + +static bool check_hash_record(struct tdb_context *tdb, + tdb_off_t off, + uint64_t hprefix, + unsigned hprefix_bits, + tdb_off_t used[], + size_t num_used, + size_t *num_found) { struct tdb_used_record rec; - tdb_len_t hashlen, i, num_nonzero; - tdb_off_t h; - size_t num_found; - - hashlen = sizeof(tdb_off_t) << tdb->header.v.hash_bits; - if (tdb_read_convert(tdb, tdb->header.v.hash_off - sizeof(rec), - &rec, sizeof(rec)) == -1) + if (tdb_read_convert(tdb, off, &rec, sizeof(rec)) == -1) return false; - if (rec_data_length(&rec) != hashlen) { + if (rec_data_length(&rec) + != sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_check: Bad hash table length %llu vs %llu\n", (long long)rec_data_length(&rec), - (long long)hashlen); + (long long)sizeof(tdb_off_t)<header.v.hash_off; - i < (1ULL << tdb->header.v.hash_bits); - i++, h += sizeof(tdb_off_t)) { - tdb_off_t off, *p, pos; - struct tdb_used_record rec; - uint64_t hash; - - off = tdb_read_off(tdb, h); - if (off == TDB_OFF_ERR) - return false; - if (!off) { - num_nonzero = 0; - continue; - } - /* FIXME: Check hash bits */ - p = asearch(&off, used, num_used, off_cmp); - if (!p) { - tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "tdb_check: Invalid offset %llu in hash\n", - (long long)off); - return false; - } - /* Mark it invalid. */ - *p ^= 1; - num_found++; + off += sizeof(rec); + return check_hash_tree(tdb, off, + TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS, + hprefix, hprefix_bits, + used, num_used, num_found); +} - if (tdb_read_convert(tdb, off, &rec, sizeof(rec)) == -1) - return false; +static int off_cmp(const tdb_off_t *a, const tdb_off_t *b) +{ + /* Can overflow an int. */ + return *a > *b ? 1 + : *a < *b ? -1 + : 0; +} - /* Check it is hashed correctly. */ - hash = hash_record(tdb, off); +static uint64_t get_bits(uint64_t h, unsigned num, unsigned *used) +{ + *used += num; - /* Top bits must match header. */ - if (hash >> (64 - 5) != rec_hash(&rec)) { - tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "tdb_check: Bad hash magic at offset %llu" - " (0x%llx vs 0x%llx)\n", - (long long)off, - (long long)hash, (long long)rec_hash(&rec)); - return false; - } + return (h >> (64 - *used)) & ((1U << num) - 1); +} + +static bool check_hash_tree(struct tdb_context *tdb, + tdb_off_t off, unsigned int group_bits, + uint64_t hprefix, + unsigned hprefix_bits, + tdb_off_t used[], + size_t num_used, + size_t *num_found) +{ + unsigned int g, b; + const tdb_off_t *hash; + struct tdb_used_record rec; + + hash = tdb_access_read(tdb, off, + sizeof(tdb_off_t) + << (group_bits + TDB_HASH_GROUP_BITS), + true); + if (!hash) + return false; + + for (g = 0; g < (1 << group_bits); g++) { + const tdb_off_t *group = hash + (g << TDB_HASH_GROUP_BITS); + for (b = 0; b < (1 << TDB_HASH_GROUP_BITS); b++) { + unsigned int bucket, i, used_bits; + uint64_t h; + tdb_off_t *p; + if (group[b] == 0) + continue; + + off = group[b] & TDB_OFF_MASK; + p = asearch(&off, used, num_used, off_cmp); + if (!p) { + tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, + "tdb_check: Invalid offset %llu " + "in hash\n", + (long long)off); + goto fail; + } + /* Mark it invalid. */ + *p ^= 1; + (*num_found)++; + + if (is_subhash(group[b])) { + uint64_t subprefix; + subprefix = (hprefix + << (group_bits + TDB_HASH_GROUP_BITS)) + + g * (1 << TDB_HASH_GROUP_BITS) + b; + + if (!check_hash_record(tdb, + group[b] & TDB_OFF_MASK, + subprefix, + hprefix_bits + + group_bits + + TDB_HASH_GROUP_BITS, + used, num_used, num_found)) + goto fail; + continue; + } + /* A normal entry */ + + /* Does it belong here at all? */ + h = hash_record(tdb, off); + used_bits = 0; + if (get_bits(h, hprefix_bits, &used_bits) != hprefix + && hprefix_bits) { + tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, + "check: bad hash placement" + " 0x%llx vs 0x%llx\n", + (long long)h, (long long)hprefix); + goto fail; + } + + /* Does it belong in this group? */ + if (get_bits(h, group_bits, &used_bits) != g) { + tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, + "check: bad group %llu vs %u\n", + (long long)h, g); + goto fail; + } - /* It must be in the right place in hash array. */ - pos = hash & ((1ULL << tdb->header.v.hash_bits)-1); - if (pos < i - num_nonzero || pos > i) { - /* Could be wrap from end of array? FIXME: check? */ - if (i != num_nonzero) { + /* Are bucket bits correct? */ + bucket = group[b] & TDB_OFF_HASH_GROUP_MASK; + if (get_bits(h, TDB_HASH_GROUP_BITS, &used_bits) + != bucket) { + used_bits -= TDB_HASH_GROUP_BITS; tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "tdb_check: Bad hash position %llu at" - " offset %llu hash 0x%llx\n", - (long long)i, + "check: bad bucket %u vs %u\n", + (unsigned)get_bits(h, + TDB_HASH_GROUP_BITS, + &used_bits), + bucket); + goto fail; + } + + /* There must not be any zero entries between + * the bucket it belongs in and this one! */ + for (i = bucket; + i != b; + i = (i + 1) % (1 << TDB_HASH_GROUP_BITS)) { + if (group[i] == 0) { + tdb->log(tdb, TDB_DEBUG_ERROR, + tdb->log_priv, + "check: bad group placement" + " %u vs %u\n", + b, bucket); + goto fail; + } + } + + if (tdb_read_convert(tdb, off, &rec, sizeof(rec)) == -1) + goto fail; + + /* Bottom bits must match header. */ + if ((h & ((1 << 11)-1)) != rec_hash(&rec)) { + tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, + "tdb_check: Bad hash magic at" + " offset %llu (0x%llx vs 0x%llx)\n", (long long)off, - (long long)hash); - return false; + (long long)h, + (long long)rec_hash(&rec)); + goto fail; } } - num_nonzero++; } + tdb_access_release(tdb, hash); + return true; - /* hash table is one of the used blocks. */ - if (num_found != num_used - 1) { +fail: + tdb_access_release(tdb, hash); + return false; +} + +static bool check_hash(struct tdb_context *tdb, + tdb_off_t used[], + size_t num_used, size_t num_flists) +{ + /* Free lists also show up as used. */ + size_t num_found = num_flists; + + if (!check_hash_tree(tdb, offsetof(struct tdb_header, hashtable), + TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS, + 0, 0, used, num_used, &num_found)) + return false; + + if (num_found != num_used) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_check: Not all entries are in hash\n"); return false; @@ -178,8 +289,7 @@ static bool check_hash_list(struct tdb_context *tdb, static bool check_free(struct tdb_context *tdb, tdb_off_t off, const struct tdb_free_record *frec, - tdb_off_t prev, - tdb_off_t zone_off, unsigned int bucket) + tdb_off_t prev, tdb_off_t flist_off, unsigned int bucket) { if (frec_magic(frec) != TDB_FREE_MAGIC) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, @@ -187,24 +297,22 @@ static bool check_free(struct tdb_context *tdb, (long long)off, (long long)frec->magic_and_meta); return false; } - if (tdb->methods->oob(tdb, off - + frec->data_len-sizeof(struct tdb_used_record), - true)) - return false; - if (off < zone_off || off >= zone_off + (1ULL<log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "tdb_check: offset %llu outside zone %llu-%llu\n", - (long long)off, - (long long)zone_off, - (long long)zone_off + (1ULL<data_len) != bucket) { + + if (tdb->methods->oob(tdb, off + + frec->data_len+sizeof(struct tdb_used_record), + false)) + return false; + if (size_to_bucket(frec->data_len) != bucket) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_check: offset %llu in wrong bucket %u vs %u\n", (long long)off, - bucket, - size_to_bucket(frec_zone_bits(frec), frec->data_len)); + bucket, size_to_bucket(frec->data_len)); return false; } if (prev != frec->prev) { @@ -217,30 +325,40 @@ static bool check_free(struct tdb_context *tdb, return true; } -static tdb_len_t check_free_list(struct tdb_context *tdb, - tdb_off_t zone_off, - tdb_off_t free[], - size_t num_free, - size_t *num_found) +static bool check_free_list(struct tdb_context *tdb, + tdb_off_t flist_off, + tdb_off_t free[], + size_t num_free, + size_t *num_found) { - struct free_zone_header zhdr; + struct tdb_freelist flist; tdb_off_t h; unsigned int i; - if (tdb_read_convert(tdb, zone_off, &zhdr, sizeof(zhdr)) == -1) - return TDB_OFF_ERR; + if (tdb_read_convert(tdb, flist_off, &flist, sizeof(flist)) == -1) + return false; - for (i = 0; i <= BUCKETS_FOR_ZONE(zhdr.zone_bits); i++) { + if (rec_magic(&flist.hdr) != TDB_MAGIC + || rec_key_length(&flist.hdr) != 0 + || rec_data_length(&flist.hdr) != sizeof(flist) - sizeof(flist.hdr) + || rec_hash(&flist.hdr) != 1) { + tdb->log(tdb, TDB_DEBUG_ERROR, + tdb->log_priv, + "tdb_check: Invalid header on free list\n"); + return false; + } + + for (i = 0; i < TDB_FREE_BUCKETS; i++) { tdb_off_t off, prev = 0, *p; struct tdb_free_record f; - h = bucket_off(zone_off, i); + h = bucket_off(flist_off, i); for (off = tdb_read_off(tdb, h); off; off = f.next) { if (off == TDB_OFF_ERR) return false; if (tdb_read_convert(tdb, off, &f, sizeof(f))) return false; - if (!check_free(tdb, off, &f, prev, zone_off, i)) + if (!check_free(tdb, off, &f, prev, flist_off, i)) return false; /* FIXME: Check hash bits */ @@ -259,74 +377,88 @@ static tdb_len_t check_free_list(struct tdb_context *tdb, prev = off; } } - return 1ULL << zhdr.zone_bits; + return true; } -static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off, - tdb_off_t **used, size_t *num_used, - tdb_off_t **free, size_t *num_free, - bool *hash_found, unsigned int *max_zone_bits) +/* Slow, but should be very rare. */ +size_t dead_space(struct tdb_context *tdb, tdb_off_t off) { - struct free_zone_header zhdr; - tdb_off_t off, hdrlen; - tdb_len_t len; - - if (tdb_read_convert(tdb, zone_off, &zhdr, sizeof(zhdr)) == -1) - return TDB_OFF_ERR; - - if (zhdr.zone_bits < INITIAL_ZONE_BITS) { - tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "check: bad zone_bits %llu at zone %llu\n", - (long long)zhdr.zone_bits, (long long)zone_off); - return TDB_OFF_ERR; - } - - /* Zone bits can only increase... */ - if (zhdr.zone_bits > *max_zone_bits) - *max_zone_bits = zhdr.zone_bits; - else if (zhdr.zone_bits < *max_zone_bits) { - tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "check: small zone_bits %llu at zone %llu\n", - (long long)zhdr.zone_bits, (long long)zone_off); - return TDB_OFF_ERR; + size_t len; + + for (len = 0; off + len < tdb->map_size; len++) { + char c; + if (tdb->methods->read(tdb, off, &c, 1)) + return 0; + if (c != 0 && c != 0x43) + break; } + return len; +} - /* Zone must be within file! */ - if (tdb->methods->oob(tdb, zone_off + (1ULL << zhdr.zone_bits), false)) - return TDB_OFF_ERR; +static bool check_linear(struct tdb_context *tdb, + tdb_off_t **used, size_t *num_used, + tdb_off_t **free, size_t *num_free, + tdb_off_t recovery) +{ + tdb_off_t off; + tdb_len_t len; + bool found_recovery = false; - hdrlen = sizeof(zhdr) - + (BUCKETS_FOR_ZONE(zhdr.zone_bits) + 1) * sizeof(tdb_off_t); - for (off = zone_off + hdrlen; - off < zone_off + (1ULL << zhdr.zone_bits); - off += len) { + for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) { union { struct tdb_used_record u; struct tdb_free_record f; + struct tdb_recovery_record r; } pad, *p; p = tdb_get(tdb, off, &pad, sizeof(pad)); if (!p) - return TDB_OFF_ERR; - if (frec_magic(&p->f) == TDB_FREE_MAGIC) { - if (frec_zone_bits(&p->f) != zhdr.zone_bits) { + return false; + + /* If we crash after ftruncate, we can get zeroes or fill. */ + if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC + || p->r.magic == 0x4343434343434343ULL) { + if (recovery == off) { + found_recovery = true; + len = sizeof(p->r) + p->r.max_len; + } else { + len = dead_space(tdb, off); + if (len < sizeof(p->r)) { + tdb->log(tdb, TDB_DEBUG_ERROR, + tdb->log_priv, + "tdb_check: invalid dead space" + " at %zu\n", (size_t)off); + return false; + } + + tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv, + "Dead space at %zu-%zu (of %zu)\n", + (size_t)off, (size_t)(off + len), + (size_t)tdb->map_size); + } + } else if (p->r.magic == TDB_RECOVERY_MAGIC) { + if (recovery != off) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "tdb_check: Bad free zone bits %u" - " at offset %llu\n", - frec_zone_bits(&p->f), - (long long)off); - return TDB_OFF_ERR; + "tdb_check: unexpected recovery" + " record at offset %zu\n", + (size_t)off); + return false; } - /* This record is free! */ - if (!append(free, num_free, off)) - return TDB_OFF_ERR; + found_recovery = true; + len = sizeof(p->r) + p->r.max_len; + } else if (frec_magic(&p->f) == TDB_FREE_MAGIC + || frec_magic(&p->f) == TDB_COALESCING_MAGIC) { len = sizeof(p->u) + p->f.data_len; - if (off + len > zone_off + (1ULL << zhdr.zone_bits)) { + if (off + len > tdb->map_size) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_check: free overlength %llu" " at offset %llu\n", (long long)len, (long long)off); - return TDB_OFF_ERR; + return false; } + /* This record is free! */ + if (frec_magic(&p->f) == TDB_FREE_MAGIC + && !append(free, num_free, off)) + return false; } else { uint64_t klen, dlen, extra; @@ -337,32 +469,23 @@ static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off, " at offset %llu\n", (long long)rec_magic(&p->u), (long long)off); - return TDB_OFF_ERR; + return false; } - if (rec_zone_bits(&p->u) != zhdr.zone_bits) { - tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "tdb_check: Bad zone bits %u" - " at offset %llu\n", - rec_zone_bits(&p->u), - (long long)off); - return TDB_OFF_ERR; - } - if (!append(used, num_used, off)) - return TDB_OFF_ERR; + return false; klen = rec_key_length(&p->u); dlen = rec_data_length(&p->u); extra = rec_extra_padding(&p->u); len = sizeof(p->u) + klen + dlen + extra; - if (off + len > zone_off + (1ULL << zhdr.zone_bits)) { + if (off + len > tdb->map_size) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_check: used overlength %llu" " at offset %llu\n", (long long)len, (long long)off); - return TDB_OFF_ERR; + return false; } if (len < sizeof(p->f)) { @@ -370,14 +493,20 @@ static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off, "tdb_check: too short record %llu at" " %llu\n", (long long)len, (long long)off); - return TDB_OFF_ERR; + return false; } - - if (off + sizeof(p->u) == tdb->header.v.hash_off) - *hash_found = true; } } - return 1ULL << zhdr.zone_bits; + + /* We must have found recovery area if there was one. */ + if (recovery != 0 && !found_recovery) { + tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, + "tdb_check: expected a recovery area at %zu\n", + (size_t)recovery); + return false; + } + + return true; } /* FIXME: call check() function. */ @@ -385,60 +514,36 @@ int tdb_check(struct tdb_context *tdb, int (*check)(TDB_DATA key, TDB_DATA data, void *private_data), void *private_data) { - tdb_off_t *free = NULL, *used = NULL, off; - tdb_len_t len; - size_t num_free = 0, num_used = 0, num_found = 0; - bool hash_found = false; - unsigned max_zone_bits = INITIAL_ZONE_BITS; - uint8_t tailer; + tdb_off_t *free = NULL, *used = NULL, flist, recovery; + size_t num_free = 0, num_used = 0, num_found = 0, num_flists = 0; - /* FIXME: need more locking? against expansion? */ - /* This always ensures the header is uptodate. */ if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0) return -1; - if (!check_header(tdb)) - goto fail; - - /* First we do a linear scan, checking all records. */ - for (off = sizeof(struct tdb_header); - off < tdb->map_size - 1; - off += len) { - len = check_zone(tdb, off, &used, &num_used, &free, &num_free, - &hash_found, &max_zone_bits); - if (len == TDB_OFF_ERR) - goto fail; + if (tdb_lock_expand(tdb, F_RDLCK) != 0) { + tdb_allrecord_unlock(tdb, F_RDLCK); + return -1; } - /* Check tailer. */ - if (tdb->methods->read(tdb, tdb->map_size - 1, &tailer, 1) == -1) - goto fail; - if (tailer != max_zone_bits) { - tdb->ecode = TDB_ERR_CORRUPT; - tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "tdb_check: Bad tailer value %u vs %u\n", tailer, - max_zone_bits); + if (!check_header(tdb, &recovery)) goto fail; - } - if (!hash_found) { - tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, - "tdb_check: hash table not found at %llu\n", - (long long)tdb->header.v.hash_off); + /* First we do a linear scan, checking all records. */ + if (!check_linear(tdb, &used, &num_used, &free, &num_free, recovery)) goto fail; + + for (flist = first_flist(tdb); flist; flist = next_flist(tdb, flist)) { + if (flist == TDB_OFF_ERR) + goto fail; + if (!check_free_list(tdb, flist, free, num_free, &num_found)) + goto fail; + num_flists++; } /* FIXME: Check key uniqueness? */ - if (!check_hash_list(tdb, used, num_used)) + if (!check_hash(tdb, used, num_used, num_flists)) goto fail; - for (off = sizeof(struct tdb_header); - off < tdb->map_size - 1; - off += len) { - len = check_free_list(tdb, off, free, num_free, &num_found); - if (len == TDB_OFF_ERR) - goto fail; - } if (num_found != num_free) { tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv, "tdb_check: Not all entries are in free table\n"); @@ -446,9 +551,11 @@ int tdb_check(struct tdb_context *tdb, } tdb_allrecord_unlock(tdb, F_RDLCK); + tdb_unlock_expand(tdb, F_RDLCK); return 0; fail: tdb_allrecord_unlock(tdb, F_RDLCK); + tdb_unlock_expand(tdb, F_RDLCK); return -1; }