2 Trivial Database 2: free list/block handling
3 Copyright (C) Rusty Russell 2010
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include <ccan/likely/likely.h>
20 #include <ccan/asearch/asearch.h>
22 /* We keep an ordered array of offsets. */
23 static bool append(tdb_off_t **arr, size_t *num, tdb_off_t off)
25 tdb_off_t *new = realloc(*arr, (*num + 1) * sizeof(tdb_off_t));
33 static bool check_header(struct tdb_context *tdb)
36 struct tdb_header hdr;
38 if (tdb_read_convert(tdb, 0, &hdr, sizeof(hdr)) == -1)
40 /* magic food should not be converted, so convert back. */
41 tdb_convert(tdb, hdr.magic_food, sizeof(hdr.magic_food));
43 hash_test = TDB_HASH_MAGIC;
44 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
45 if (hdr.hash_test != hash_test) {
46 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
47 "check: hash test %llu should be %llu\n",
48 (long long)hdr.hash_test,
49 (long long)hash_test);
53 if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
54 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
55 "check: bad magic '%.*s'\n",
56 (unsigned)sizeof(hdr.magic_food), hdr.magic_food);
60 /* Don't check reserved: they *can* be used later. */
64 static bool check_hash_tree(struct tdb_context *tdb,
65 tdb_off_t off, unsigned int group_bits,
67 unsigned hprefix_bits,
72 static bool check_hash_record(struct tdb_context *tdb,
75 unsigned hprefix_bits,
80 struct tdb_used_record rec;
82 if (tdb_read_convert(tdb, off, &rec, sizeof(rec)) == -1)
85 if (rec_data_length(&rec)
86 != sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) {
87 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
88 "tdb_check: Bad hash table length %llu vs %llu\n",
89 (long long)rec_data_length(&rec),
90 (long long)sizeof(tdb_off_t)<<TDB_SUBLEVEL_HASH_BITS);
93 if (rec_key_length(&rec) != 0) {
94 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
95 "tdb_check: Bad hash table key length %llu\n",
96 (long long)rec_key_length(&rec));
99 if (rec_hash(&rec) != 0) {
100 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
101 "tdb_check: Bad hash table hash value %llu\n",
102 (long long)rec_hash(&rec));
107 return check_hash_tree(tdb, off,
108 TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS,
109 hprefix, hprefix_bits,
110 used, num_used, num_found);
113 static int off_cmp(const tdb_off_t *a, const tdb_off_t *b)
115 /* Can overflow an int. */
121 static uint64_t get_bits(uint64_t h, unsigned num, unsigned *used)
125 return (h >> (64 - *used)) & ((1U << num) - 1);
128 static bool check_hash_tree(struct tdb_context *tdb,
129 tdb_off_t off, unsigned int group_bits,
131 unsigned hprefix_bits,
137 const tdb_off_t *hash;
138 struct tdb_used_record rec;
140 hash = tdb_access_read(tdb, off,
142 << (group_bits + TDB_HASH_GROUP_BITS),
147 for (g = 0; g < (1 << group_bits); g++) {
148 const tdb_off_t *group = hash + (g << TDB_HASH_GROUP_BITS);
149 for (b = 0; b < (1 << TDB_HASH_GROUP_BITS); b++) {
150 unsigned int bucket, i, used_bits;
156 off = group[b] & TDB_OFF_MASK;
157 p = asearch(&off, used, num_used, off_cmp);
159 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
160 "tdb_check: Invalid offset %llu "
165 /* Mark it invalid. */
169 if (is_subhash(group[b])) {
172 << (group_bits + TDB_HASH_GROUP_BITS))
173 + g * (1 << TDB_HASH_GROUP_BITS) + b;
175 if (!check_hash_record(tdb,
176 group[b] & TDB_OFF_MASK,
180 + TDB_HASH_GROUP_BITS,
181 used, num_used, num_found))
187 /* Does it belong here at all? */
188 h = hash_record(tdb, off);
190 if (get_bits(h, hprefix_bits, &used_bits) != hprefix
192 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
193 "check: bad hash placement"
194 " 0x%llx vs 0x%llx\n",
195 (long long)h, (long long)hprefix);
199 /* Does it belong in this group? */
200 if (get_bits(h, group_bits, &used_bits) != g) {
201 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
202 "check: bad group %llu vs %u\n",
207 /* Are bucket bits correct? */
208 bucket = group[b] & TDB_OFF_HASH_GROUP_MASK;
209 if (get_bits(h, TDB_HASH_GROUP_BITS, &used_bits)
211 used_bits -= TDB_HASH_GROUP_BITS;
212 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
213 "check: bad bucket %u vs %u\n",
214 (unsigned)get_bits(h,
221 /* There must not be any zero entries between
222 * the bucket it belongs in and this one! */
225 i = (i + 1) % (1 << TDB_HASH_GROUP_BITS)) {
227 tdb->log(tdb, TDB_DEBUG_ERROR,
229 "check: bad group placement"
236 if (tdb_read_convert(tdb, off, &rec, sizeof(rec)) == -1)
239 /* Bottom bits must match header. */
240 if ((h & ((1 << 11)-1)) != rec_hash(&rec)) {
241 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
242 "tdb_check: Bad hash magic at"
243 " offset %llu (0x%llx vs 0x%llx)\n",
246 (long long)rec_hash(&rec));
251 tdb_access_release(tdb, hash);
255 tdb_access_release(tdb, hash);
259 static bool check_hash(struct tdb_context *tdb,
263 size_t num_found = 0;
265 if (!check_hash_tree(tdb, offsetof(struct tdb_header, hashtable),
266 TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS,
267 0, 0, used, num_used, &num_found))
270 /* 1 is for the free list. */
271 if (num_found != num_used - 1) {
272 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
273 "tdb_check: Not all entries are in hash\n");
279 static bool check_free(struct tdb_context *tdb,
281 const struct tdb_free_record *frec,
282 tdb_off_t prev, unsigned int bucket)
284 if (frec_magic(frec) != TDB_FREE_MAGIC) {
285 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
286 "tdb_check: offset %llu bad magic 0x%llx\n",
287 (long long)off, (long long)frec->magic_and_meta);
290 if (tdb->methods->oob(tdb, off
291 + frec->data_len+sizeof(struct tdb_used_record),
294 if (size_to_bucket(frec->data_len) != bucket) {
295 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
296 "tdb_check: offset %llu in wrong bucket %u vs %u\n",
298 bucket, size_to_bucket(frec->data_len));
301 if (prev != frec->prev) {
302 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
303 "tdb_check: offset %llu bad prev %llu vs %llu\n",
305 (long long)prev, (long long)frec->prev);
311 static bool check_free_list(struct tdb_context *tdb,
317 struct tdb_freelist flist;
321 if (tdb_read_convert(tdb, flist_off, &flist, sizeof(flist)) == -1)
324 if (rec_magic(&flist.hdr) != TDB_MAGIC
325 || rec_key_length(&flist.hdr) != 0
326 || rec_data_length(&flist.hdr) != sizeof(flist) - sizeof(flist.hdr)
327 || rec_hash(&flist.hdr) != 1) {
328 tdb->log(tdb, TDB_DEBUG_ERROR,
330 "tdb_check: Invalid header on free list\n");
334 for (i = 0; i < TDB_FREE_BUCKETS; i++) {
335 tdb_off_t off, prev = 0, *p;
336 struct tdb_free_record f;
338 h = bucket_off(flist_off, i);
339 for (off = tdb_read_off(tdb, h); off; off = f.next) {
340 if (off == TDB_OFF_ERR)
342 if (tdb_read_convert(tdb, off, &f, sizeof(f)))
344 if (!check_free(tdb, off, &f, prev, i))
347 /* FIXME: Check hash bits */
348 p = asearch(&off, free, num_free, off_cmp);
350 tdb->log(tdb, TDB_DEBUG_ERROR,
352 "tdb_check: Invalid offset"
353 " %llu in free table\n",
357 /* Mark it invalid. */
366 static bool check_linear(struct tdb_context *tdb,
367 tdb_off_t **used, size_t *num_used,
368 tdb_off_t **free, size_t *num_free)
373 for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) {
375 struct tdb_used_record u;
376 struct tdb_free_record f;
378 p = tdb_get(tdb, off, &pad, sizeof(pad));
381 if (frec_magic(&p->f) == TDB_FREE_MAGIC
382 || frec_magic(&p->f) == TDB_COALESCING_MAGIC) {
383 len = sizeof(p->u) + p->f.data_len;
384 if (off + len > tdb->map_size) {
385 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
386 "tdb_check: free overlength %llu"
388 (long long)len, (long long)off);
391 /* This record is free! */
392 if (frec_magic(&p->f) == TDB_FREE_MAGIC
393 && !append(free, num_free, off))
396 uint64_t klen, dlen, extra;
398 /* This record is used! */
399 if (rec_magic(&p->u) != TDB_MAGIC) {
400 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
401 "tdb_check: Bad magic 0x%llx"
403 (long long)rec_magic(&p->u),
408 if (!append(used, num_used, off))
411 klen = rec_key_length(&p->u);
412 dlen = rec_data_length(&p->u);
413 extra = rec_extra_padding(&p->u);
415 len = sizeof(p->u) + klen + dlen + extra;
416 if (off + len > tdb->map_size) {
417 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
418 "tdb_check: used overlength %llu"
420 (long long)len, (long long)off);
424 if (len < sizeof(p->f)) {
425 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
426 "tdb_check: too short record %llu at"
428 (long long)len, (long long)off);
436 /* FIXME: call check() function. */
437 int tdb_check(struct tdb_context *tdb,
438 int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
441 tdb_off_t *free = NULL, *used = NULL;
442 size_t num_free = 0, num_used = 0, num_found = 0;
444 if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
447 if (tdb_lock_expand(tdb, F_RDLCK) != 0) {
448 tdb_allrecord_unlock(tdb, F_RDLCK);
452 if (!check_header(tdb))
455 /* First we do a linear scan, checking all records. */
456 if (!check_linear(tdb, &used, &num_used, &free, &num_free))
459 /* FIXME: Check key uniqueness? */
460 if (!check_hash(tdb, used, num_used))
463 if (!check_free_list(tdb, tdb->flist_off, free, num_free, &num_found))
466 if (num_found != num_free) {
467 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
468 "tdb_check: Not all entries are in free table\n");
472 tdb_allrecord_unlock(tdb, F_RDLCK);
473 tdb_unlock_expand(tdb, F_RDLCK);
477 tdb_allrecord_unlock(tdb, F_RDLCK);
478 tdb_unlock_expand(tdb, F_RDLCK);