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 enum TDB_ERROR check_header(struct tdb_context *tdb, tdb_off_t *recovery,
37 struct tdb_header hdr;
40 ecode = tdb_read_convert(tdb, 0, &hdr, sizeof(hdr));
41 if (ecode != TDB_SUCCESS) {
44 /* magic food should not be converted, so convert back. */
45 tdb_convert(tdb, hdr.magic_food, sizeof(hdr.magic_food));
47 hash_test = TDB_HASH_MAGIC;
48 hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
49 if (hdr.hash_test != hash_test) {
50 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
51 "check: hash test %llu should be %llu",
52 (long long)hdr.hash_test,
53 (long long)hash_test);
56 if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
57 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
58 "check: bad magic '%.*s'",
59 (unsigned)sizeof(hdr.magic_food),
63 /* Features which are used must be a subset of features offered. */
64 if (hdr.features_used & ~hdr.features_offered) {
65 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
66 "check: features used (0x%llx) which"
67 " are not offered (0x%llx)",
68 (long long)hdr.features_used,
69 (long long)hdr.features_offered);
72 *features = hdr.features_offered;
73 *recovery = hdr.recovery;
75 if (*recovery < sizeof(hdr)
76 || *recovery > tdb->file->map_size) {
77 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
79 " invalid recovery offset %zu",
84 /* Don't check reserved: they *can* be used later. */
88 static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
89 tdb_off_t off, unsigned int group_bits,
91 unsigned hprefix_bits,
95 enum TDB_ERROR (*check)(TDB_DATA,
99 static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb,
105 enum TDB_ERROR (*check)(TDB_DATA,
110 struct tdb_used_record rec;
111 enum TDB_ERROR ecode;
113 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
114 if (ecode != TDB_SUCCESS) {
118 if (rec_magic(&rec) != TDB_CHAIN_MAGIC) {
119 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
120 "tdb_check: Bad hash chain magic %llu",
121 (long long)rec_magic(&rec));
124 if (rec_data_length(&rec) != sizeof(struct tdb_chain)) {
125 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
127 " Bad hash chain length %llu vs %zu",
128 (long long)rec_data_length(&rec),
129 sizeof(struct tdb_chain));
131 if (rec_key_length(&rec) != 0) {
132 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
133 "tdb_check: Bad hash chain key length %llu",
134 (long long)rec_key_length(&rec));
136 if (rec_hash(&rec) != 0) {
137 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
138 "tdb_check: Bad hash chain hash value %llu",
139 (long long)rec_hash(&rec));
143 ecode = check_hash_tree(tdb, off, 0, hash, 64,
144 used, num_used, num_found, check, private_data);
145 if (ecode != TDB_SUCCESS) {
149 off = tdb_read_off(tdb, off + offsetof(struct tdb_chain, next));
150 if (TDB_OFF_IS_ERR(off)) {
156 return check_hash_chain(tdb, off, hash, used, num_used, num_found,
157 check, private_data);
160 static enum TDB_ERROR check_hash_record(struct tdb_context *tdb,
163 unsigned hprefix_bits,
167 enum TDB_ERROR (*check)(TDB_DATA,
172 struct tdb_used_record rec;
173 enum TDB_ERROR ecode;
175 if (hprefix_bits >= 64)
176 return check_hash_chain(tdb, off, hprefix, used, num_used,
177 num_found, check, private_data);
179 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
180 if (ecode != TDB_SUCCESS) {
184 if (rec_magic(&rec) != TDB_HTABLE_MAGIC) {
185 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
186 "tdb_check: Bad hash table magic %llu",
187 (long long)rec_magic(&rec));
189 if (rec_data_length(&rec)
190 != sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) {
191 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
193 " Bad hash table length %llu vs %llu",
194 (long long)rec_data_length(&rec),
195 (long long)sizeof(tdb_off_t)
196 << TDB_SUBLEVEL_HASH_BITS);
198 if (rec_key_length(&rec) != 0) {
199 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
200 "tdb_check: Bad hash table key length %llu",
201 (long long)rec_key_length(&rec));
203 if (rec_hash(&rec) != 0) {
204 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
205 "tdb_check: Bad hash table hash value %llu",
206 (long long)rec_hash(&rec));
210 return check_hash_tree(tdb, off,
211 TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS,
212 hprefix, hprefix_bits,
213 used, num_used, num_found, check, private_data);
216 static int off_cmp(const tdb_off_t *a, const tdb_off_t *b)
218 /* Can overflow an int. */
224 static uint64_t get_bits(uint64_t h, unsigned num, unsigned *used)
228 return (h >> (64 - *used)) & ((1U << num) - 1);
231 static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
232 tdb_off_t off, unsigned int group_bits,
234 unsigned hprefix_bits,
238 enum TDB_ERROR (*check)(TDB_DATA,
243 const tdb_off_t *hash;
244 struct tdb_used_record rec;
245 enum TDB_ERROR ecode;
247 hash = tdb_access_read(tdb, off,
249 << (group_bits + TDB_HASH_GROUP_BITS),
251 if (TDB_PTR_IS_ERR(hash)) {
252 return TDB_PTR_ERR(hash);
255 for (g = 0; g < (1 << group_bits); g++) {
256 const tdb_off_t *group = hash + (g << TDB_HASH_GROUP_BITS);
257 for (b = 0; b < (1 << TDB_HASH_GROUP_BITS); b++) {
258 unsigned int bucket, i, used_bits;
264 off = group[b] & TDB_OFF_MASK;
265 p = asearch(&off, used, num_used, off_cmp);
267 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
269 "tdb_check: Invalid offset"
274 /* Mark it invalid. */
278 if (hprefix_bits == 64) {
279 /* Chained entries are unordered. */
280 if (is_subhash(group[b])) {
281 ecode = TDB_ERR_CORRUPT;
282 tdb_logerr(tdb, ecode,
284 "tdb_check: Invalid chain"
288 h = hash_record(tdb, off);
290 ecode = TDB_ERR_CORRUPT;
291 tdb_logerr(tdb, ecode,
293 "check: bad hash chain"
300 ecode = tdb_read_convert(tdb, off, &rec,
302 if (ecode != TDB_SUCCESS) {
308 if (is_subhash(group[b])) {
311 << (group_bits + TDB_HASH_GROUP_BITS))
312 + g * (1 << TDB_HASH_GROUP_BITS) + b;
314 ecode = check_hash_record(tdb,
315 group[b] & TDB_OFF_MASK,
319 + TDB_HASH_GROUP_BITS,
320 used, num_used, num_found,
321 check, private_data);
322 if (ecode != TDB_SUCCESS) {
329 /* Does it belong here at all? */
330 h = hash_record(tdb, off);
332 if (get_bits(h, hprefix_bits, &used_bits) != hprefix
334 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
336 "check: bad hash placement"
343 /* Does it belong in this group? */
344 if (get_bits(h, group_bits, &used_bits) != g) {
345 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
347 "check: bad group %llu"
353 /* Are bucket bits correct? */
354 bucket = group[b] & TDB_OFF_HASH_GROUP_MASK;
355 if (get_bits(h, TDB_HASH_GROUP_BITS, &used_bits)
357 used_bits -= TDB_HASH_GROUP_BITS;
358 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
360 "check: bad bucket %u vs %u",
361 (unsigned)get_bits(h,
368 /* There must not be any zero entries between
369 * the bucket it belongs in and this one! */
372 i = (i + 1) % (1 << TDB_HASH_GROUP_BITS)) {
374 ecode = TDB_ERR_CORRUPT;
375 tdb_logerr(tdb, ecode,
377 "check: bad group placement"
384 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
385 if (ecode != TDB_SUCCESS) {
389 /* Bottom bits must match header. */
390 if ((h & ((1 << 11)-1)) != rec_hash(&rec)) {
391 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
393 "tdb_check: Bad hash magic"
395 " (0x%llx vs 0x%llx)",
398 (long long)rec_hash(&rec));
405 key.dsize = rec_key_length(&rec);
406 data.dsize = rec_data_length(&rec);
407 key.dptr = (void *)tdb_access_read(tdb,
409 key.dsize + data.dsize,
411 if (TDB_PTR_IS_ERR(key.dptr)) {
412 ecode = TDB_PTR_ERR(key.dptr);
415 data.dptr = key.dptr + key.dsize;
416 ecode = check(key, data, private_data);
417 if (ecode != TDB_SUCCESS) {
420 tdb_access_release(tdb, key.dptr);
424 tdb_access_release(tdb, hash);
428 tdb_access_release(tdb, hash);
432 static enum TDB_ERROR check_hash(struct tdb_context *tdb,
434 size_t num_used, size_t num_ftables,
435 int (*check)(TDB_DATA, TDB_DATA, void *),
438 /* Free tables also show up as used. */
439 size_t num_found = num_ftables;
440 enum TDB_ERROR ecode;
442 ecode = check_hash_tree(tdb, offsetof(struct tdb_header, hashtable),
443 TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS,
444 0, 0, used, num_used, &num_found,
445 check, private_data);
446 if (ecode == TDB_SUCCESS) {
447 if (num_found != num_used) {
448 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
449 "tdb_check: Not all entries"
456 static enum TDB_ERROR check_free(struct tdb_context *tdb,
458 const struct tdb_free_record *frec,
459 tdb_off_t prev, unsigned int ftable,
462 enum TDB_ERROR ecode;
464 if (frec_magic(frec) != TDB_FREE_MAGIC) {
465 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
466 "tdb_check: offset %llu bad magic 0x%llx",
468 (long long)frec->magic_and_prev);
470 if (frec_ftable(frec) != ftable) {
471 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
472 "tdb_check: offset %llu bad freetable %u",
473 (long long)off, frec_ftable(frec));
477 ecode = tdb->methods->oob(tdb, off
479 + sizeof(struct tdb_used_record),
481 if (ecode != TDB_SUCCESS) {
484 if (size_to_bucket(frec_len(frec)) != bucket) {
485 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
486 "tdb_check: offset %llu in wrong bucket"
489 bucket, size_to_bucket(frec_len(frec)));
491 if (prev != frec_prev(frec)) {
492 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
493 "tdb_check: offset %llu bad prev"
496 (long long)prev, (long long)frec_len(frec));
501 static enum TDB_ERROR check_free_table(struct tdb_context *tdb,
502 tdb_off_t ftable_off,
508 struct tdb_freetable ft;
511 enum TDB_ERROR ecode;
513 ecode = tdb_read_convert(tdb, ftable_off, &ft, sizeof(ft));
514 if (ecode != TDB_SUCCESS) {
518 if (rec_magic(&ft.hdr) != TDB_FTABLE_MAGIC
519 || rec_key_length(&ft.hdr) != 0
520 || rec_data_length(&ft.hdr) != sizeof(ft) - sizeof(ft.hdr)
521 || rec_hash(&ft.hdr) != 0) {
522 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
523 "tdb_check: Invalid header on free table");
526 for (i = 0; i < TDB_FREE_BUCKETS; i++) {
527 tdb_off_t off, prev = 0, *p;
528 struct tdb_free_record f;
530 h = bucket_off(ftable_off, i);
531 for (off = tdb_read_off(tdb, h); off; off = f.next) {
532 if (TDB_OFF_IS_ERR(off)) {
535 ecode = tdb_read_convert(tdb, off, &f, sizeof(f));
536 if (ecode != TDB_SUCCESS) {
539 ecode = check_free(tdb, off, &f, prev, ftable_num, i);
540 if (ecode != TDB_SUCCESS) {
544 /* FIXME: Check hash bits */
545 p = asearch(&off, fr, num_free, off_cmp);
547 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
549 "tdb_check: Invalid offset"
550 " %llu in free table",
553 /* Mark it invalid. */
562 /* Slow, but should be very rare. */
563 tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off)
566 enum TDB_ERROR ecode;
568 for (len = 0; off + len < tdb->file->map_size; len++) {
570 ecode = tdb->methods->tread(tdb, off, &c, 1);
571 if (ecode != TDB_SUCCESS) {
574 if (c != 0 && c != 0x43)
580 static enum TDB_ERROR check_linear(struct tdb_context *tdb,
581 tdb_off_t **used, size_t *num_used,
582 tdb_off_t **fr, size_t *num_free,
583 uint64_t features, tdb_off_t recovery)
587 enum TDB_ERROR ecode;
588 bool found_recovery = false;
590 for (off = sizeof(struct tdb_header);
591 off < tdb->file->map_size;
594 struct tdb_used_record u;
595 struct tdb_free_record f;
596 struct tdb_recovery_record r;
598 /* r is larger: only get that if we need to. */
599 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec.f));
600 if (ecode != TDB_SUCCESS) {
604 /* If we crash after ftruncate, we can get zeroes or fill. */
605 if (rec.r.magic == TDB_RECOVERY_INVALID_MAGIC
606 || rec.r.magic == 0x4343434343434343ULL) {
607 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec.r));
608 if (ecode != TDB_SUCCESS) {
611 if (recovery == off) {
612 found_recovery = true;
613 len = sizeof(rec.r) + rec.r.max_len;
615 len = dead_space(tdb, off);
616 if (TDB_OFF_IS_ERR(len)) {
619 if (len < sizeof(rec.r)) {
620 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
623 " dead space at %zu",
627 tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
628 "Dead space at %zu-%zu (of %zu)",
629 (size_t)off, (size_t)(off + len),
630 (size_t)tdb->file->map_size);
632 } else if (rec.r.magic == TDB_RECOVERY_MAGIC) {
633 ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec.r));
634 if (ecode != TDB_SUCCESS) {
637 if (recovery != off) {
638 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
640 "tdb_check: unexpected"
641 " recovery record at offset"
645 if (rec.r.len > rec.r.max_len) {
646 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
648 "tdb_check: invalid recovery"
652 if (rec.r.eof > tdb->file->map_size) {
653 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
655 "tdb_check: invalid old EOF"
656 " %zu", (size_t)rec.r.eof);
658 found_recovery = true;
659 len = sizeof(rec.r) + rec.r.max_len;
660 } else if (frec_magic(&rec.f) == TDB_FREE_MAGIC) {
661 len = sizeof(rec.u) + frec_len(&rec.f);
662 if (off + len > tdb->file->map_size) {
663 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
665 "tdb_check: free overlength"
666 " %llu at offset %llu",
670 /* This record should be in free lists. */
671 if (frec_ftable(&rec.f) != TDB_FTABLE_NONE
672 && !append(fr, num_free, off)) {
673 return tdb_logerr(tdb, TDB_ERR_OOM,
675 "tdb_check: tracking %zu'th"
676 " free record.", *num_free);
678 } else if (rec_magic(&rec.u) == TDB_USED_MAGIC
679 || rec_magic(&rec.u) == TDB_CHAIN_MAGIC
680 || rec_magic(&rec.u) == TDB_HTABLE_MAGIC
681 || rec_magic(&rec.u) == TDB_FTABLE_MAGIC) {
682 uint64_t klen, dlen, extra;
684 /* This record is used! */
685 if (!append(used, num_used, off)) {
686 return tdb_logerr(tdb, TDB_ERR_OOM,
688 "tdb_check: tracking %zu'th"
689 " used record.", *num_used);
692 klen = rec_key_length(&rec.u);
693 dlen = rec_data_length(&rec.u);
694 extra = rec_extra_padding(&rec.u);
696 len = sizeof(rec.u) + klen + dlen + extra;
697 if (off + len > tdb->file->map_size) {
698 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
700 "tdb_check: used overlength"
701 " %llu at offset %llu",
706 if (len < sizeof(rec.f)) {
707 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
709 "tdb_check: too short record"
715 /* Check that records have correct 0 at end (but may
717 if (extra && !features) {
720 p = tdb_access_read(tdb, off + sizeof(rec.u)
721 + klen + dlen, 1, false);
722 if (TDB_PTR_IS_ERR(p))
723 return TDB_PTR_ERR(p);
725 tdb_access_release(tdb, p);
728 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
737 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
739 "tdb_check: Bad magic 0x%llx"
741 (long long)rec_magic(&rec.u),
746 /* We must have found recovery area if there was one. */
747 if (recovery != 0 && !found_recovery) {
748 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
749 "tdb_check: expected a recovery area at %zu",
756 enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
757 enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data,
761 tdb_off_t *fr = NULL, *used = NULL, ft, recovery;
762 size_t num_free = 0, num_used = 0, num_found = 0, num_ftables = 0;
764 enum TDB_ERROR ecode;
766 ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
767 if (ecode != TDB_SUCCESS) {
768 return tdb->last_error = ecode;
771 ecode = tdb_lock_expand(tdb, F_RDLCK);
772 if (ecode != TDB_SUCCESS) {
773 tdb_allrecord_unlock(tdb, F_RDLCK);
774 return tdb->last_error = ecode;
777 ecode = check_header(tdb, &recovery, &features);
778 if (ecode != TDB_SUCCESS)
781 /* First we do a linear scan, checking all records. */
782 ecode = check_linear(tdb, &used, &num_used, &fr, &num_free, features,
784 if (ecode != TDB_SUCCESS)
787 for (ft = first_ftable(tdb); ft; ft = next_ftable(tdb, ft)) {
788 if (TDB_OFF_IS_ERR(ft)) {
792 ecode = check_free_table(tdb, ft, num_ftables, fr, num_free,
794 if (ecode != TDB_SUCCESS)
799 /* FIXME: Check key uniqueness? */
800 ecode = check_hash(tdb, used, num_used, num_ftables, check, private);
801 if (ecode != TDB_SUCCESS)
804 if (num_found != num_free) {
805 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
806 "tdb_check: Not all entries are in"
811 tdb_allrecord_unlock(tdb, F_RDLCK);
812 tdb_unlock_expand(tdb, F_RDLCK);
815 return tdb->last_error = ecode;