| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
- /*
- Trivial Database 2: free list/block handling
- Copyright (C) Rusty Russell 2010
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 3 of the License, or (at your option) any later version.
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, see <http://www.gnu.org/licenses/>.
- */
- #include "private.h"
- #include <ccan/likely/likely.h>
- #include <ccan/asearch/asearch.h>
- /* We keep an ordered array of offsets. */
- static bool append(tdb_off_t **arr, size_t *num, tdb_off_t off)
- {
- tdb_off_t *new = realloc(*arr, (*num + 1) * sizeof(tdb_off_t));
- if (!new)
- return false;
- new[(*num)++] = off;
- *arr = new;
- return true;
- }
- 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 (hdr.hash_test != hash_test) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "check: hash test %llu should be %llu",
- (long long)hdr.hash_test,
- (long long)hash_test);
- return false;
- }
- if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "check: bad magic '%.*s'",
- (unsigned)sizeof(hdr.magic_food), hdr.magic_food);
- return false;
- }
- *recovery = hdr.recovery;
- if (*recovery) {
- if (*recovery < sizeof(hdr) || *recovery > tdb->map_size) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: invalid recovery offset %zu",
- (size_t)*recovery);
- return false;
- }
- }
- /* Don't check reserved: they *can* be used later. */
- return true;
- }
- 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,
- int (*check)(TDB_DATA, TDB_DATA, void *),
- void *private_data);
- 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,
- int (*check)(TDB_DATA, TDB_DATA, void *),
- void *private_data)
- {
- struct tdb_used_record rec;
- if (tdb_read_convert(tdb, off, &rec, sizeof(rec)) == -1)
- return false;
- if (rec_data_length(&rec)
- != sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: Bad hash table length %llu vs %llu",
- (long long)rec_data_length(&rec),
- (long long)sizeof(tdb_off_t)
- << TDB_SUBLEVEL_HASH_BITS);
- return false;
- }
- if (rec_key_length(&rec) != 0) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: Bad hash table key length %llu",
- (long long)rec_key_length(&rec));
- return false;
- }
- if (rec_hash(&rec) != 0) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: Bad hash table hash value %llu",
- (long long)rec_hash(&rec));
- return false;
- }
- 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, check, private_data);
- }
- 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 uint64_t get_bits(uint64_t h, unsigned num, unsigned *used)
- {
- *used += num;
- 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,
- int (*check)(TDB_DATA, TDB_DATA, void *),
- void *private_data)
- {
- 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_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: Invalid offset %llu "
- "in hash", (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,
- check, private_data))
- 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_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "check: bad hash placement"
- " 0x%llx vs 0x%llx",
- (long long)h, (long long)hprefix);
- goto fail;
- }
- /* Does it belong in this group? */
- if (get_bits(h, group_bits, &used_bits) != g) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "check: bad group %llu vs %u",
- (long long)h, g);
- goto fail;
- }
- /* 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_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "check: bad bucket %u vs %u",
- (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_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "check: bad group placement"
- " %u vs %u",
- b, bucket);
- goto fail;
- }
- }
- if (tdb_read_convert(tdb, off, &rec, sizeof(rec)))
- goto fail;
- /* Bottom bits must match header. */
- if ((h & ((1 << 11)-1)) != rec_hash(&rec)) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: Bad hash magic at"
- " offset %llu (0x%llx vs 0x%llx)",
- (long long)off,
- (long long)h,
- (long long)rec_hash(&rec));
- goto fail;
- }
- 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 (!key.dptr)
- goto fail;
- data.dptr = key.dptr + key.dsize;
- if (check(key, data, private_data) != 0)
- goto fail;
- tdb_access_release(tdb, key.dptr);
- }
- }
- }
- tdb_access_release(tdb, hash);
- return true;
- 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,
- int (*check)(TDB_DATA, TDB_DATA, void *),
- void *private_data)
- {
- /* 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,
- check, private_data))
- return false;
- if (num_found != num_used) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: Not all entries are in hash");
- return false;
- }
- return true;
- }
- static bool check_free(struct tdb_context *tdb,
- tdb_off_t off,
- const struct tdb_free_record *frec,
- tdb_off_t prev, unsigned int flist, unsigned int bucket)
- {
- if (frec_magic(frec) != TDB_FREE_MAGIC) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: offset %llu bad magic 0x%llx",
- (long long)off, (long long)frec->magic_and_prev);
- return false;
- }
- if (frec_flist(frec) != flist) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: offset %llu bad freelist %u",
- (long long)off, frec_flist(frec));
- return false;
- }
- if (tdb->methods->oob(tdb, off
- + frec_len(frec) + sizeof(struct tdb_used_record),
- false))
- return false;
- if (size_to_bucket(frec_len(frec)) != bucket) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: offset %llu in wrong bucket %u vs %u",
- (long long)off,
- bucket, size_to_bucket(frec_len(frec)));
- return false;
- }
- if (prev != frec_prev(frec)) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: offset %llu bad prev %llu vs %llu",
- (long long)off,
- (long long)prev, (long long)frec_len(frec));
- return false;
- }
- return true;
- }
-
- static bool check_free_list(struct tdb_context *tdb,
- tdb_off_t flist_off,
- unsigned flist_num,
- tdb_off_t free[],
- size_t num_free,
- size_t *num_found)
- {
- struct tdb_freelist flist;
- tdb_off_t h;
- unsigned int i;
- if (tdb_read_convert(tdb, flist_off, &flist, sizeof(flist)) == -1)
- return false;
- 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_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: Invalid header on free list");
- 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(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, flist_num, i))
- return false;
- /* FIXME: Check hash bits */
- p = asearch(&off, free, num_free, off_cmp);
- if (!p) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: Invalid offset"
- " %llu in free table",
- (long long)off);
- return false;
- }
- /* Mark it invalid. */
- *p ^= 1;
- (*num_found)++;
- prev = off;
- }
- }
- return true;
- }
- /* Slow, but should be very rare. */
- size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
- {
- 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;
- }
- 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;
- 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;
- } rec;
- /* r is larger: only get that if we need to. */
- if (tdb_read_convert(tdb, off, &rec, sizeof(rec.f)) == -1)
- return false;
- /* If we crash after ftruncate, we can get zeroes or fill. */
- if (rec.r.magic == TDB_RECOVERY_INVALID_MAGIC
- || rec.r.magic == 0x4343434343434343ULL) {
- if (tdb_read_convert(tdb, off, &rec, sizeof(rec.r)))
- return false;
- if (recovery == off) {
- found_recovery = true;
- len = sizeof(rec.r) + rec.r.max_len;
- } else {
- len = dead_space(tdb, off);
- if (len < sizeof(rec.r)) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: invalid dead"
- " space at %zu",
- (size_t)off);
- return false;
- }
- tdb_logerr(tdb, TDB_SUCCESS, TDB_DEBUG_WARNING,
- "Dead space at %zu-%zu (of %zu)",
- (size_t)off, (size_t)(off + len),
- (size_t)tdb->map_size);
- }
- } else if (rec.r.magic == TDB_RECOVERY_MAGIC) {
- if (tdb_read_convert(tdb, off, &rec, sizeof(rec.r)))
- return false;
- if (recovery != off) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: unexpected recovery"
- " record at offset %zu",
- (size_t)off);
- return false;
- }
- if (rec.r.len > rec.r.max_len) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: invalid recovery length"
- " %zu", (size_t)rec.r.len);
- return false;
- }
- if (rec.r.eof > tdb->map_size) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: invalid old EOF"
- " %zu", (size_t)rec.r.eof);
- return false;
- }
- found_recovery = true;
- len = sizeof(rec.r) + rec.r.max_len;
- } else if (frec_magic(&rec.f) == TDB_FREE_MAGIC
- || frec_magic(&rec.f) == TDB_COALESCING_MAGIC) {
- len = sizeof(rec.u) + frec_len(&rec.f);
- if (off + len > tdb->map_size) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: free overlength %llu"
- " at offset %llu",
- (long long)len, (long long)off);
- return false;
- }
- /* This record is free! */
- if (frec_magic(&rec.f) == TDB_FREE_MAGIC
- && !append(free, num_free, off))
- return false;
- } else {
- uint64_t klen, dlen, extra;
- /* This record is used! */
- if (rec_magic(&rec.u) != TDB_MAGIC) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: Bad magic 0x%llx"
- " at offset %zu",
- (long long)rec_magic(&rec.u),
- (size_t)off);
- return false;
- }
- if (!append(used, num_used, off))
- return false;
- klen = rec_key_length(&rec.u);
- dlen = rec_data_length(&rec.u);
- extra = rec_extra_padding(&rec.u);
- len = sizeof(rec.u) + klen + dlen + extra;
- if (off + len > tdb->map_size) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: used overlength %llu"
- " at offset %llu",
- (long long)len, (long long)off);
- return false;
- }
- if (len < sizeof(rec.f)) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT,
- TDB_DEBUG_ERROR,
- "tdb_check: too short record %llu"
- " at %llu",
- (long long)len, (long long)off);
- return false;
- }
- }
- }
- /* We must have found recovery area if there was one. */
- if (recovery != 0 && !found_recovery) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: expected a recovery area at %zu",
- (size_t)recovery);
- return false;
- }
- return true;
- }
- 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, flist, recovery;
- size_t num_free = 0, num_used = 0, num_found = 0, num_flists = 0;
- if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
- return -1;
- if (tdb_lock_expand(tdb, F_RDLCK) != 0) {
- tdb_allrecord_unlock(tdb, F_RDLCK);
- return -1;
- }
- if (!check_header(tdb, &recovery))
- goto fail;
- /* 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, num_flists, free, num_free,
- &num_found))
- goto fail;
- num_flists++;
- }
- /* FIXME: Check key uniqueness? */
- if (!check_hash(tdb, used, num_used, num_flists, check, private_data))
- goto fail;
- if (num_found != num_free) {
- tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_DEBUG_ERROR,
- "tdb_check: Not all entries are in free table");
- return -1;
- }
- 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;
- }
|