tdb2: move mmap into struct tdb_file
[ccan] / ccan / tdb2 / check.c
1  /*
2    Trivial Database 2: free list/block handling
3    Copyright (C) Rusty Russell 2010
4
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.
9
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.
14
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/>.
17 */
18 #include "private.h"
19 #include <ccan/likely/likely.h>
20 #include <ccan/asearch/asearch.h>
21
22 /* We keep an ordered array of offsets. */
23 static bool append(tdb_off_t **arr, size_t *num, tdb_off_t off)
24 {
25         tdb_off_t *new = realloc(*arr, (*num + 1) * sizeof(tdb_off_t));
26         if (!new)
27                 return false;
28         new[(*num)++] = off;
29         *arr = new;
30         return true;
31 }
32
33 static enum TDB_ERROR check_header(struct tdb_context *tdb, tdb_off_t *recovery,
34                                    uint64_t *features)
35 {
36         uint64_t hash_test;
37         struct tdb_header hdr;
38         enum TDB_ERROR ecode;
39
40         ecode = tdb_read_convert(tdb, 0, &hdr, sizeof(hdr));
41         if (ecode != TDB_SUCCESS) {
42                 return ecode;
43         }
44         /* magic food should not be converted, so convert back. */
45         tdb_convert(tdb, hdr.magic_food, sizeof(hdr.magic_food));
46
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);
54         }
55
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),
60                                   hdr.magic_food);
61         }
62
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);
70         }
71
72         *features = hdr.features_offered;
73         *recovery = hdr.recovery;
74         if (*recovery) {
75                 if (*recovery < sizeof(hdr)
76                     || *recovery > tdb->file->map_size) {
77                         return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
78                                           "tdb_check:"
79                                           " invalid recovery offset %zu",
80                                           (size_t)*recovery);
81                 }
82         }
83
84         /* Don't check reserved: they *can* be used later. */
85         return TDB_SUCCESS;
86 }
87
88 static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
89                                       tdb_off_t off, unsigned int group_bits,
90                                       uint64_t hprefix,
91                                       unsigned hprefix_bits,
92                                       tdb_off_t used[],
93                                       size_t num_used,
94                                       size_t *num_found,
95                                       enum TDB_ERROR (*check)(TDB_DATA,
96                                                               TDB_DATA, void *),
97                                       void *private_data);
98
99 static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb,
100                                        tdb_off_t off,
101                                        uint64_t hash,
102                                        tdb_off_t used[],
103                                        size_t num_used,
104                                        size_t *num_found,
105                                        enum TDB_ERROR (*check)(TDB_DATA,
106                                                                TDB_DATA,
107                                                                void *),
108                                        void *private_data)
109 {
110         struct tdb_used_record rec;
111         enum TDB_ERROR ecode;
112
113         ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
114         if (ecode != TDB_SUCCESS) {
115                 return ecode;
116         }
117
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));
122         }
123
124         if (rec_data_length(&rec) != sizeof(struct tdb_chain)) {
125                 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
126                                   "tdb_check:"
127                                   " Bad hash chain length %llu vs %zu",
128                                   (long long)rec_data_length(&rec),
129                                   sizeof(struct tdb_chain));
130         }
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));
135         }
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));
140         }
141
142         off += sizeof(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) {
146                 return ecode;
147         }
148
149         off = tdb_read_off(tdb, off + offsetof(struct tdb_chain, next));
150         if (TDB_OFF_IS_ERR(off)) {
151                 return off;
152         }
153         if (off == 0)
154                 return TDB_SUCCESS;
155         (*num_found)++;
156         return check_hash_chain(tdb, off, hash, used, num_used, num_found,
157                                 check, private_data);
158 }
159
160 static enum TDB_ERROR check_hash_record(struct tdb_context *tdb,
161                                         tdb_off_t off,
162                                         uint64_t hprefix,
163                                         unsigned hprefix_bits,
164                                         tdb_off_t used[],
165                                         size_t num_used,
166                                         size_t *num_found,
167                                         enum TDB_ERROR (*check)(TDB_DATA,
168                                                                 TDB_DATA,
169                                                                 void *),
170                                         void *private_data)
171 {
172         struct tdb_used_record rec;
173         enum TDB_ERROR ecode;
174
175         if (hprefix_bits >= 64)
176                 return check_hash_chain(tdb, off, hprefix, used, num_used,
177                                         num_found, check, private_data);
178
179         ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
180         if (ecode != TDB_SUCCESS) {
181                 return ecode;
182         }
183
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));
188         }
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,
192                                   "tdb_check:"
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);
197         }
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));
202         }
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));
207         }
208
209         off += sizeof(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);
214 }
215
216 static int off_cmp(const tdb_off_t *a, const tdb_off_t *b)
217 {
218         /* Can overflow an int. */
219         return *a > *b ? 1
220                 : *a < *b ? -1
221                 : 0;
222 }
223
224 static uint64_t get_bits(uint64_t h, unsigned num, unsigned *used)
225 {
226         *used += num;
227
228         return (h >> (64 - *used)) & ((1U << num) - 1);
229 }
230
231 static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
232                                       tdb_off_t off, unsigned int group_bits,
233                                       uint64_t hprefix,
234                                       unsigned hprefix_bits,
235                                       tdb_off_t used[],
236                                       size_t num_used,
237                                       size_t *num_found,
238                                       enum TDB_ERROR (*check)(TDB_DATA,
239                                                               TDB_DATA, void *),
240                                       void *private_data)
241 {
242         unsigned int g, b;
243         const tdb_off_t *hash;
244         struct tdb_used_record rec;
245         enum TDB_ERROR ecode;
246
247         hash = tdb_access_read(tdb, off,
248                                sizeof(tdb_off_t)
249                                << (group_bits + TDB_HASH_GROUP_BITS),
250                                true);
251         if (TDB_PTR_IS_ERR(hash)) {
252                 return TDB_PTR_ERR(hash);
253         }
254
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;
259                         uint64_t h;
260                         tdb_off_t *p;
261                         if (group[b] == 0)
262                                 continue;
263
264                         off = group[b] & TDB_OFF_MASK;
265                         p = asearch(&off, used, num_used, off_cmp);
266                         if (!p) {
267                                 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
268                                                    TDB_LOG_ERROR,
269                                                    "tdb_check: Invalid offset"
270                                                    " %llu in hash",
271                                                    (long long)off);
272                                 goto fail;
273                         }
274                         /* Mark it invalid. */
275                         *p ^= 1;
276                         (*num_found)++;
277
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,
283                                                    TDB_LOG_ERROR,
284                                                    "tdb_check: Invalid chain"
285                                                    " entry subhash");
286                                         goto fail;
287                                 }
288                                 h = hash_record(tdb, off);
289                                 if (h != hprefix) {
290                                         ecode = TDB_ERR_CORRUPT;
291                                         tdb_logerr(tdb, ecode,
292                                                    TDB_LOG_ERROR,
293                                                    "check: bad hash chain"
294                                                    " placement"
295                                                    " 0x%llx vs 0x%llx",
296                                                    (long long)h,
297                                                    (long long)hprefix);
298                                         goto fail;
299                                 }
300                                 ecode = tdb_read_convert(tdb, off, &rec,
301                                                          sizeof(rec));
302                                 if (ecode != TDB_SUCCESS) {
303                                         goto fail;
304                                 }
305                                 goto check;
306                         }
307
308                         if (is_subhash(group[b])) {
309                                 uint64_t subprefix;
310                                 subprefix = (hprefix
311                                      << (group_bits + TDB_HASH_GROUP_BITS))
312                                         + g * (1 << TDB_HASH_GROUP_BITS) + b;
313
314                                 ecode = check_hash_record(tdb,
315                                                group[b] & TDB_OFF_MASK,
316                                                subprefix,
317                                                hprefix_bits
318                                                        + group_bits
319                                                        + TDB_HASH_GROUP_BITS,
320                                                used, num_used, num_found,
321                                                check, private_data);
322                                 if (ecode != TDB_SUCCESS) {
323                                         goto fail;
324                                 }
325                                 continue;
326                         }
327                         /* A normal entry */
328
329                         /* Does it belong here at all? */
330                         h = hash_record(tdb, off);
331                         used_bits = 0;
332                         if (get_bits(h, hprefix_bits, &used_bits) != hprefix
333                             && hprefix_bits) {
334                                 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
335                                                    TDB_LOG_ERROR,
336                                                    "check: bad hash placement"
337                                                    " 0x%llx vs 0x%llx",
338                                                    (long long)h,
339                                                    (long long)hprefix);
340                                 goto fail;
341                         }
342
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,
346                                                    TDB_LOG_ERROR,
347                                                    "check: bad group %llu"
348                                                    " vs %u",
349                                                    (long long)h, g);
350                                 goto fail;
351                         }
352
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)
356                             != bucket) {
357                                 used_bits -= TDB_HASH_GROUP_BITS;
358                                 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
359                                                    TDB_LOG_ERROR,
360                                                    "check: bad bucket %u vs %u",
361                                                    (unsigned)get_bits(h,
362                                                         TDB_HASH_GROUP_BITS,
363                                                         &used_bits),
364                                                    bucket);
365                                 goto fail;
366                         }
367
368                         /* There must not be any zero entries between
369                          * the bucket it belongs in and this one! */
370                         for (i = bucket;
371                              i != b;
372                              i = (i + 1) % (1 << TDB_HASH_GROUP_BITS)) {
373                                 if (group[i] == 0) {
374                                         ecode = TDB_ERR_CORRUPT;
375                                         tdb_logerr(tdb, ecode,
376                                                    TDB_LOG_ERROR,
377                                                    "check: bad group placement"
378                                                    " %u vs %u",
379                                                    b, bucket);
380                                         goto fail;
381                                 }
382                         }
383
384                         ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
385                         if (ecode != TDB_SUCCESS) {
386                                 goto fail;
387                         }
388
389                         /* Bottom bits must match header. */
390                         if ((h & ((1 << 11)-1)) != rec_hash(&rec)) {
391                                 ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
392                                                    TDB_LOG_ERROR,
393                                                    "tdb_check: Bad hash magic"
394                                                    " at offset %llu"
395                                                    " (0x%llx vs 0x%llx)",
396                                                    (long long)off,
397                                                    (long long)h,
398                                                    (long long)rec_hash(&rec));
399                                 goto fail;
400                         }
401
402                 check:
403                         if (check) {
404                                 TDB_DATA key, data;
405                                 key.dsize = rec_key_length(&rec);
406                                 data.dsize = rec_data_length(&rec);
407                                 key.dptr = (void *)tdb_access_read(tdb,
408                                                    off + sizeof(rec),
409                                                    key.dsize + data.dsize,
410                                                    false);
411                                 if (TDB_PTR_IS_ERR(key.dptr)) {
412                                         ecode = TDB_PTR_ERR(key.dptr);
413                                         goto fail;
414                                 }
415                                 data.dptr = key.dptr + key.dsize;
416                                 ecode = check(key, data, private_data);
417                                 if (ecode != TDB_SUCCESS) {
418                                         goto fail;
419                                 }
420                                 tdb_access_release(tdb, key.dptr);
421                         }
422                 }
423         }
424         tdb_access_release(tdb, hash);
425         return TDB_SUCCESS;
426
427 fail:
428         tdb_access_release(tdb, hash);
429         return ecode;
430 }
431
432 static enum TDB_ERROR check_hash(struct tdb_context *tdb,
433                                  tdb_off_t used[],
434                                  size_t num_used, size_t num_ftables,
435                                  int (*check)(TDB_DATA, TDB_DATA, void *),
436                                  void *private_data)
437 {
438         /* Free tables also show up as used. */
439         size_t num_found = num_ftables;
440         enum TDB_ERROR ecode;
441
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"
450                                            " are in hash");
451                 }
452         }
453         return ecode;
454 }
455
456 static enum TDB_ERROR check_free(struct tdb_context *tdb,
457                                  tdb_off_t off,
458                                  const struct tdb_free_record *frec,
459                                  tdb_off_t prev, unsigned int ftable,
460                                  unsigned int bucket)
461 {
462         enum TDB_ERROR ecode;
463
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",
467                                   (long long)off,
468                                   (long long)frec->magic_and_prev);
469         }
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));
474
475         }
476
477         ecode = tdb->methods->oob(tdb, off
478                                   + frec_len(frec)
479                                   + sizeof(struct tdb_used_record),
480                                   false);
481         if (ecode != TDB_SUCCESS) {
482                 return ecode;
483         }
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"
487                                   " (%u vs %u)",
488                                   (long long)off,
489                                   bucket, size_to_bucket(frec_len(frec)));
490         }
491         if (prev != frec_prev(frec)) {
492                 return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
493                                   "tdb_check: offset %llu bad prev"
494                                   " (%llu vs %llu)",
495                                   (long long)off,
496                                   (long long)prev, (long long)frec_len(frec));
497         }
498         return TDB_SUCCESS;
499 }
500
501 static enum TDB_ERROR check_free_table(struct tdb_context *tdb,
502                                        tdb_off_t ftable_off,
503                                        unsigned ftable_num,
504                                        tdb_off_t fr[],
505                                        size_t num_free,
506                                        size_t *num_found)
507 {
508         struct tdb_freetable ft;
509         tdb_off_t h;
510         unsigned int i;
511         enum TDB_ERROR ecode;
512
513         ecode = tdb_read_convert(tdb, ftable_off, &ft, sizeof(ft));
514         if (ecode != TDB_SUCCESS) {
515                 return ecode;
516         }
517
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");
524         }
525
526         for (i = 0; i < TDB_FREE_BUCKETS; i++) {
527                 tdb_off_t off, prev = 0, *p;
528                 struct tdb_free_record f;
529
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)) {
533                                 return off;
534                         }
535                         ecode = tdb_read_convert(tdb, off, &f, sizeof(f));
536                         if (ecode != TDB_SUCCESS) {
537                                 return ecode;
538                         }
539                         ecode = check_free(tdb, off, &f, prev, ftable_num, i);
540                         if (ecode != TDB_SUCCESS) {
541                                 return ecode;
542                         }
543
544                         /* FIXME: Check hash bits */
545                         p = asearch(&off, fr, num_free, off_cmp);
546                         if (!p) {
547                                 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
548                                                   TDB_LOG_ERROR,
549                                                   "tdb_check: Invalid offset"
550                                                   " %llu in free table",
551                                                   (long long)off);
552                         }
553                         /* Mark it invalid. */
554                         *p ^= 1;
555                         (*num_found)++;
556                         prev = off;
557                 }
558         }
559         return TDB_SUCCESS;
560 }
561
562 /* Slow, but should be very rare. */
563 tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off)
564 {
565         size_t len;
566         enum TDB_ERROR ecode;
567
568         for (len = 0; off + len < tdb->file->map_size; len++) {
569                 char c;
570                 ecode = tdb->methods->tread(tdb, off, &c, 1);
571                 if (ecode != TDB_SUCCESS) {
572                         return ecode;
573                 }
574                 if (c != 0 && c != 0x43)
575                         break;
576         }
577         return len;
578 }
579
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)
584 {
585         tdb_off_t off;
586         tdb_len_t len;
587         enum TDB_ERROR ecode;
588         bool found_recovery = false;
589
590         for (off = sizeof(struct tdb_header);
591              off < tdb->file->map_size;
592              off += len) {
593                 union {
594                         struct tdb_used_record u;
595                         struct tdb_free_record f;
596                         struct tdb_recovery_record r;
597                 } rec;
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) {
601                         return ecode;
602                 }
603
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) {
609                                 return ecode;
610                         }
611                         if (recovery == off) {
612                                 found_recovery = true;
613                                 len = sizeof(rec.r) + rec.r.max_len;
614                         } else {
615                                 len = dead_space(tdb, off);
616                                 if (TDB_OFF_IS_ERR(len)) {
617                                         return len;
618                                 }
619                                 if (len < sizeof(rec.r)) {
620                                         return tdb_logerr(tdb, TDB_ERR_CORRUPT,
621                                                           TDB_LOG_ERROR,
622                                                           "tdb_check: invalid"
623                                                           " dead space at %zu",
624                                                           (size_t)off);
625                                 }
626
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);
631                         }
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) {
635                                 return ecode;
636                         }
637                         if (recovery != off) {
638                                 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
639                                                   TDB_LOG_ERROR,
640                                                   "tdb_check: unexpected"
641                                                   " recovery record at offset"
642                                                   " %zu",
643                                                   (size_t)off);
644                         }
645                         if (rec.r.len > rec.r.max_len) {
646                                 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
647                                                   TDB_LOG_ERROR,
648                                                   "tdb_check: invalid recovery"
649                                                   " length %zu",
650                                                   (size_t)rec.r.len);
651                         }
652                         if (rec.r.eof > tdb->file->map_size) {
653                                 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
654                                                   TDB_LOG_ERROR,
655                                                   "tdb_check: invalid old EOF"
656                                                   " %zu", (size_t)rec.r.eof);
657                         }
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,
664                                                   TDB_LOG_ERROR,
665                                                   "tdb_check: free overlength"
666                                                   " %llu at offset %llu",
667                                                   (long long)len,
668                                                   (long long)off);
669                         }
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,
674                                                   TDB_LOG_ERROR,
675                                                   "tdb_check: tracking %zu'th"
676                                                   " free record.", *num_free);
677                         }
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;
683
684                         /* This record is used! */
685                         if (!append(used, num_used, off)) {
686                                 return tdb_logerr(tdb, TDB_ERR_OOM,
687                                                   TDB_LOG_ERROR,
688                                                   "tdb_check: tracking %zu'th"
689                                                   " used record.", *num_used);
690                         }
691
692                         klen = rec_key_length(&rec.u);
693                         dlen = rec_data_length(&rec.u);
694                         extra = rec_extra_padding(&rec.u);
695
696                         len = sizeof(rec.u) + klen + dlen + extra;
697                         if (off + len > tdb->file->map_size) {
698                                 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
699                                                   TDB_LOG_ERROR,
700                                                   "tdb_check: used overlength"
701                                                   " %llu at offset %llu",
702                                                   (long long)len,
703                                                   (long long)off);
704                         }
705
706                         if (len < sizeof(rec.f)) {
707                                 return tdb_logerr(tdb, TDB_ERR_CORRUPT,
708                                                   TDB_LOG_ERROR,
709                                                   "tdb_check: too short record"
710                                                   " %llu at %llu",
711                                                   (long long)len,
712                                                   (long long)off);
713                         }
714
715                         /* Check that records have correct 0 at end (but may
716                          * not in future). */
717                         if (extra && !features) {
718                                 const char *p;
719                                 char c;
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);
724                                 c = *p;
725                                 tdb_access_release(tdb, p);
726
727                                 if (c != '\0') {
728                                         return tdb_logerr(tdb, TDB_ERR_CORRUPT,
729                                                           TDB_LOG_ERROR,
730                                                           "tdb_check:"
731                                                           " non-zero extra"
732                                                           " at %llu",
733                                                           (long long)off);
734                                 }
735                         }
736                 } else {
737                         return tdb_logerr(tdb, TDB_ERR_CORRUPT,
738                                           TDB_LOG_ERROR,
739                                           "tdb_check: Bad magic 0x%llx"
740                                           " at offset %zu",
741                                           (long long)rec_magic(&rec.u),
742                                           (size_t)off);
743                 }
744         }
745
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",
750                                   (size_t)recovery);
751         }
752
753         return TDB_SUCCESS;
754 }
755
756 enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
757                           enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data,
758                                                   void *private),
759                           void *private)
760 {
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;
763         uint64_t features;
764         enum TDB_ERROR ecode;
765
766         ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
767         if (ecode != TDB_SUCCESS) {
768                 return ecode;
769         }
770
771         ecode = tdb_lock_expand(tdb, F_RDLCK);
772         if (ecode != TDB_SUCCESS) {
773                 tdb_allrecord_unlock(tdb, F_RDLCK);
774                 return ecode;
775         }
776
777         ecode = check_header(tdb, &recovery, &features);
778         if (ecode != TDB_SUCCESS)
779                 goto out;
780
781         /* First we do a linear scan, checking all records. */
782         ecode = check_linear(tdb, &used, &num_used, &fr, &num_free, features,
783                              recovery);
784         if (ecode != TDB_SUCCESS)
785                 goto out;
786
787         for (ft = first_ftable(tdb); ft; ft = next_ftable(tdb, ft)) {
788                 if (TDB_OFF_IS_ERR(ft)) {
789                         ecode = ft;
790                         goto out;
791                 }
792                 ecode = check_free_table(tdb, ft, num_ftables, fr, num_free,
793                                          &num_found);
794                 if (ecode != TDB_SUCCESS)
795                         goto out;
796                 num_ftables++;
797         }
798
799         /* FIXME: Check key uniqueness? */
800         ecode = check_hash(tdb, used, num_used, num_ftables, check, private);
801         if (ecode != TDB_SUCCESS)
802                 goto out;
803
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"
807                                    " free table");
808         }
809
810 out:
811         tdb_allrecord_unlock(tdb, F_RDLCK);
812         tdb_unlock_expand(tdb, F_RDLCK);
813         free(fr);
814         free(used);
815         return ecode;
816 }