]> git.ozlabs.org Git - ccan/blob - junkcode/rusty@rustcorp.com.au-ntdb/check.c
ccan/io: keep always pointers to plans, not a linked list.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / 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(struct ntdb_context *ntdb,
24                    ntdb_off_t **arr, size_t *num, ntdb_off_t off)
25 {
26         ntdb_off_t *new;
27
28         if (*num == 0) {
29                 new = ntdb->alloc_fn(ntdb, sizeof(ntdb_off_t), ntdb->alloc_data);
30         } else {
31                 new = ntdb->expand_fn(*arr, (*num + 1) * sizeof(ntdb_off_t),
32                                   ntdb->alloc_data);
33         }
34         if (!new)
35                 return false;
36         new[(*num)++] = off;
37         *arr = new;
38         return true;
39 }
40
41 static enum NTDB_ERROR check_header(struct ntdb_context *ntdb,
42                                     ntdb_off_t *recovery,
43                                     uint64_t *features,
44                                     size_t *num_capabilities)
45 {
46         uint64_t hash_test;
47         struct ntdb_header hdr;
48         enum NTDB_ERROR ecode;
49         ntdb_off_t off, next;
50
51         ecode = ntdb_read_convert(ntdb, 0, &hdr, sizeof(hdr));
52         if (ecode != NTDB_SUCCESS) {
53                 return ecode;
54         }
55         /* magic food should not be converted, so convert back. */
56         ntdb_convert(ntdb, hdr.magic_food, sizeof(hdr.magic_food));
57
58         hash_test = NTDB_HASH_MAGIC;
59         hash_test = ntdb_hash(ntdb, &hash_test, sizeof(hash_test));
60         if (hdr.hash_test != hash_test) {
61                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
62                                   "check: hash test %llu should be %llu",
63                                   (long long)hdr.hash_test,
64                                   (long long)hash_test);
65         }
66
67         if (strcmp(hdr.magic_food, NTDB_MAGIC_FOOD) != 0) {
68                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
69                                   "check: bad magic '%.*s'",
70                                   (unsigned)sizeof(hdr.magic_food),
71                                   hdr.magic_food);
72         }
73
74         /* Features which are used must be a subset of features offered. */
75         if (hdr.features_used & ~hdr.features_offered) {
76                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
77                                   "check: features used (0x%llx) which"
78                                   " are not offered (0x%llx)",
79                                   (long long)hdr.features_used,
80                                   (long long)hdr.features_offered);
81         }
82
83         *features = hdr.features_offered;
84         *recovery = hdr.recovery;
85         if (*recovery) {
86                 if (*recovery < sizeof(hdr)
87                     || *recovery > ntdb->file->map_size) {
88                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
89                                           "ntdb_check:"
90                                           " invalid recovery offset %zu",
91                                           (size_t)*recovery);
92                 }
93         }
94
95         for (off = hdr.capabilities; off && ecode == NTDB_SUCCESS; off = next) {
96                 const struct ntdb_capability *cap;
97                 enum NTDB_ERROR e;
98
99                 cap = ntdb_access_read(ntdb, off, sizeof(*cap), true);
100                 if (NTDB_PTR_IS_ERR(cap)) {
101                         return NTDB_PTR_ERR(cap);
102                 }
103
104                 /* All capabilities are unknown. */
105                 e = unknown_capability(ntdb, "ntdb_check", cap->type);
106                 next = cap->next;
107                 ntdb_access_release(ntdb, cap);
108                 if (e)
109                         return e;
110                 (*num_capabilities)++;
111         }
112
113         /* Don't check reserved: they *can* be used later. */
114         return NTDB_SUCCESS;
115 }
116
117 static int off_cmp(const ntdb_off_t *a, const ntdb_off_t *b, void *ctx)
118 {
119         /* Can overflow an int. */
120         return *a > *b ? 1
121                 : *a < *b ? -1
122                 : 0;
123 }
124
125 static enum NTDB_ERROR check_entry(struct ntdb_context *ntdb,
126                                    ntdb_off_t off_and_hash,
127                                    ntdb_len_t bucket,
128                                    ntdb_off_t used[],
129                                    size_t num_used,
130                                    size_t *num_found,
131                                    enum NTDB_ERROR (*check)(NTDB_DATA,
132                                                             NTDB_DATA,
133                                                             void *),
134                                    void *data)
135 {
136         enum NTDB_ERROR ecode;
137         const struct ntdb_used_record *r;
138         const unsigned char *kptr;
139         ntdb_len_t klen, dlen;
140         uint32_t hash;
141         ntdb_off_t off = off_and_hash & NTDB_OFF_MASK;
142         ntdb_off_t *p;
143
144         /* Empty bucket is fine. */
145         if (!off_and_hash) {
146                 return NTDB_SUCCESS;
147         }
148
149         /* This can't point to a chain, we handled those at toplevel. */
150         if (off_and_hash & (1ULL << NTDB_OFF_CHAIN_BIT)) {
151                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
152                                    "ntdb_check: Invalid chain bit in offset "
153                                    " %llu", (long long)off_and_hash);
154         }
155
156         p = asearch(&off, used, num_used, off_cmp, NULL);
157         if (!p) {
158                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
159                                    "ntdb_check: Invalid offset"
160                                    " %llu in hash", (long long)off);
161         }
162         /* Mark it invalid. */
163         *p ^= 1;
164         (*num_found)++;
165
166         r = ntdb_access_read(ntdb, off, sizeof(*r), true);
167         if (NTDB_PTR_IS_ERR(r)) {
168                 return NTDB_PTR_ERR(r);
169         }
170         klen = rec_key_length(r);
171         dlen = rec_data_length(r);
172         ntdb_access_release(ntdb, r);
173
174         kptr = ntdb_access_read(ntdb, off + sizeof(*r), klen + dlen, false);
175         if (NTDB_PTR_IS_ERR(kptr)) {
176                 return NTDB_PTR_ERR(kptr);
177         }
178
179         hash = ntdb_hash(ntdb, kptr, klen);
180
181         /* Are we in the right chain? */
182         if (bits_from(hash, 0, ntdb->hash_bits) != bucket) {
183                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
184                                     NTDB_LOG_ERROR,
185                                     "ntdb_check: Bad bucket %u vs %llu",
186                                     bits_from(hash, 0, ntdb->hash_bits),
187                                     (long long)bucket);
188         /* Next 8 bits should be the same as top bits of bucket. */
189         } else if (bits_from(hash, ntdb->hash_bits, NTDB_OFF_UPPER_STEAL)
190                    != bits_from(off_and_hash, 64-NTDB_OFF_UPPER_STEAL,
191                                 NTDB_OFF_UPPER_STEAL)) {
192                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
193                                     NTDB_LOG_ERROR,
194                                     "ntdb_check: Bad hash bits %llu vs %llu",
195                                     (long long)off_and_hash,
196                                     (long long)hash);
197         } else if (check) {
198                 NTDB_DATA k, d;
199
200                 k = ntdb_mkdata(kptr, klen);
201                 d = ntdb_mkdata(kptr + klen, dlen);
202                 ecode = check(k, d, data);
203         } else {
204                 ecode = NTDB_SUCCESS;
205         }
206         ntdb_access_release(ntdb, kptr);
207
208         return ecode;
209 }
210
211 static enum NTDB_ERROR check_hash_chain(struct ntdb_context *ntdb,
212                                         ntdb_off_t off,
213                                         ntdb_len_t bucket,
214                                         ntdb_off_t used[],
215                                         size_t num_used,
216                                         size_t *num_found,
217                                         enum NTDB_ERROR (*check)(NTDB_DATA,
218                                                                  NTDB_DATA,
219                                                                  void *),
220                                         void *data)
221 {
222         struct ntdb_used_record rec;
223         enum NTDB_ERROR ecode;
224         const ntdb_off_t *entries;
225         ntdb_len_t i, num;
226
227         /* This is a used entry. */
228         (*num_found)++;
229
230         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec));
231         if (ecode != NTDB_SUCCESS) {
232                 return ecode;
233         }
234
235         if (rec_magic(&rec) != NTDB_CHAIN_MAGIC) {
236                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
237                                   "ntdb_check: Bad hash chain magic %llu",
238                                   (long long)rec_magic(&rec));
239         }
240
241         if (rec_data_length(&rec) % sizeof(ntdb_off_t)) {
242                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
243                                   "ntdb_check: Bad hash chain data length %llu",
244                                   (long long)rec_data_length(&rec));
245         }
246
247         if (rec_key_length(&rec) != 0) {
248                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
249                                   "ntdb_check: Bad hash chain key length %llu",
250                                   (long long)rec_key_length(&rec));
251         }
252
253         off += sizeof(rec);
254         num = rec_data_length(&rec) / sizeof(ntdb_off_t);
255         entries = ntdb_access_read(ntdb, off, rec_data_length(&rec), true);
256         if (NTDB_PTR_IS_ERR(entries)) {
257                 return NTDB_PTR_ERR(entries);
258         }
259
260         /* Check each non-deleted entry in chain. */
261         for (i = 0; i < num; i++) {
262                 ecode = check_entry(ntdb, entries[i], bucket,
263                                     used, num_used, num_found, check, data);
264                 if (ecode) {
265                         break;
266                 }
267         }
268
269         ntdb_access_release(ntdb, entries);
270         return ecode;
271 }
272
273 static enum NTDB_ERROR check_hash(struct ntdb_context *ntdb,
274                                   ntdb_off_t used[],
275                                   size_t num_used,
276                                   size_t num_other_used,
277                                   enum NTDB_ERROR (*check)(NTDB_DATA,
278                                                            NTDB_DATA,
279                                                            void *),
280                                   void *data)
281 {
282         enum NTDB_ERROR ecode;
283         struct ntdb_used_record rec;
284         const ntdb_off_t *entries;
285         ntdb_len_t i;
286         /* Free tables and capabilities also show up as used, as do we. */
287         size_t num_found = num_other_used + 1;
288
289         ecode = ntdb_read_convert(ntdb, NTDB_HASH_OFFSET, &rec, sizeof(rec));
290         if (ecode != NTDB_SUCCESS) {
291                 return ecode;
292         }
293
294         if (rec_magic(&rec) != NTDB_HTABLE_MAGIC) {
295                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
296                                   "ntdb_check: Bad hash table magic %llu",
297                                   (long long)rec_magic(&rec));
298         }
299
300         if (rec_data_length(&rec) != (sizeof(ntdb_off_t) << ntdb->hash_bits)) {
301                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
302                                   "ntdb_check: Bad hash table data length %llu",
303                                   (long long)rec_data_length(&rec));
304         }
305
306         if (rec_key_length(&rec) != 0) {
307                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
308                                   "ntdb_check: Bad hash table key length %llu",
309                                   (long long)rec_key_length(&rec));
310         }
311
312         entries = ntdb_access_read(ntdb, NTDB_HASH_OFFSET + sizeof(rec),
313                                    rec_data_length(&rec), true);
314         if (NTDB_PTR_IS_ERR(entries)) {
315                 return NTDB_PTR_ERR(entries);
316         }
317
318         for (i = 0; i < (1 << ntdb->hash_bits); i++) {
319                 ntdb_off_t off = entries[i] & NTDB_OFF_MASK;
320                 if (entries[i] & (1ULL << NTDB_OFF_CHAIN_BIT)) {
321                         ecode = check_hash_chain(ntdb, off, i,
322                                                  used, num_used, &num_found,
323                                                  check, data);
324                 } else {
325                         ecode = check_entry(ntdb, entries[i], i,
326                                             used, num_used, &num_found,
327                                             check, data);
328                 }
329                 if (ecode) {
330                         break;
331                 }
332         }
333         ntdb_access_release(ntdb, entries);
334
335         if (ecode == NTDB_SUCCESS && num_found != num_used) {
336                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
337                                     "ntdb_check: Not all entries are in hash");
338         }
339         return ecode;
340 }
341
342 static enum NTDB_ERROR check_free(struct ntdb_context *ntdb,
343                                  ntdb_off_t off,
344                                  const struct ntdb_free_record *frec,
345                                  ntdb_off_t prev, unsigned int ftable,
346                                  unsigned int bucket)
347 {
348         enum NTDB_ERROR ecode;
349
350         if (frec_magic(frec) != NTDB_FREE_MAGIC) {
351                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
352                                   "ntdb_check: offset %llu bad magic 0x%llx",
353                                   (long long)off,
354                                   (long long)frec->magic_and_prev);
355         }
356         if (frec_ftable(frec) != ftable) {
357                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
358                                   "ntdb_check: offset %llu bad freetable %u",
359                                   (long long)off, frec_ftable(frec));
360
361         }
362
363         ecode = ntdb_oob(ntdb, off,
364                          frec_len(frec) + sizeof(struct ntdb_used_record),
365                          false);
366         if (ecode != NTDB_SUCCESS) {
367                 return ecode;
368         }
369         if (size_to_bucket(frec_len(frec)) != bucket) {
370                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
371                                   "ntdb_check: offset %llu in wrong bucket"
372                                   " (%u vs %u)",
373                                   (long long)off,
374                                   bucket, size_to_bucket(frec_len(frec)));
375         }
376         if (prev && prev != frec_prev(frec)) {
377                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
378                                   "ntdb_check: offset %llu bad prev"
379                                   " (%llu vs %llu)",
380                                   (long long)off,
381                                   (long long)prev, (long long)frec_len(frec));
382         }
383         return NTDB_SUCCESS;
384 }
385
386 static enum NTDB_ERROR check_free_table(struct ntdb_context *ntdb,
387                                        ntdb_off_t ftable_off,
388                                        unsigned ftable_num,
389                                        ntdb_off_t fr[],
390                                        size_t num_free,
391                                        size_t *num_found)
392 {
393         struct ntdb_freetable ft;
394         ntdb_off_t h;
395         unsigned int i;
396         enum NTDB_ERROR ecode;
397
398         ecode = ntdb_read_convert(ntdb, ftable_off, &ft, sizeof(ft));
399         if (ecode != NTDB_SUCCESS) {
400                 return ecode;
401         }
402
403         if (rec_magic(&ft.hdr) != NTDB_FTABLE_MAGIC
404             || rec_key_length(&ft.hdr) != 0
405             || rec_data_length(&ft.hdr) != sizeof(ft) - sizeof(ft.hdr)) {
406                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
407                                   "ntdb_check: Invalid header on free table");
408         }
409
410         for (i = 0; i < NTDB_FREE_BUCKETS; i++) {
411                 ntdb_off_t off, prev = 0, *p, first = 0;
412                 struct ntdb_free_record f;
413
414                 h = bucket_off(ftable_off, i);
415                 for (off = ntdb_read_off(ntdb, h); off; off = f.next) {
416                         if (NTDB_OFF_IS_ERR(off)) {
417                                 return NTDB_OFF_TO_ERR(off);
418                         }
419                         if (!first) {
420                                 off &= NTDB_OFF_MASK;
421                                 first = off;
422                         }
423                         ecode = ntdb_read_convert(ntdb, off, &f, sizeof(f));
424                         if (ecode != NTDB_SUCCESS) {
425                                 return ecode;
426                         }
427                         ecode = check_free(ntdb, off, &f, prev, ftable_num, i);
428                         if (ecode != NTDB_SUCCESS) {
429                                 return ecode;
430                         }
431
432                         /* FIXME: Check hash bits */
433                         p = asearch(&off, fr, num_free, off_cmp, NULL);
434                         if (!p) {
435                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
436                                                   NTDB_LOG_ERROR,
437                                                   "ntdb_check: Invalid offset"
438                                                   " %llu in free table",
439                                                   (long long)off);
440                         }
441                         /* Mark it invalid. */
442                         *p ^= 1;
443                         (*num_found)++;
444                         prev = off;
445                 }
446
447                 if (first) {
448                         /* Now we can check first back pointer. */
449                         ecode = ntdb_read_convert(ntdb, first, &f, sizeof(f));
450                         if (ecode != NTDB_SUCCESS) {
451                                 return ecode;
452                         }
453                         ecode = check_free(ntdb, first, &f, prev, ftable_num, i);
454                         if (ecode != NTDB_SUCCESS) {
455                                 return ecode;
456                         }
457                 }
458         }
459         return NTDB_SUCCESS;
460 }
461
462 /* Slow, but should be very rare. */
463 ntdb_off_t dead_space(struct ntdb_context *ntdb, ntdb_off_t off)
464 {
465         size_t len;
466         enum NTDB_ERROR ecode;
467
468         for (len = 0; off + len < ntdb->file->map_size; len++) {
469                 char c;
470                 ecode = ntdb->io->tread(ntdb, off, &c, 1);
471                 if (ecode != NTDB_SUCCESS) {
472                         return NTDB_ERR_TO_OFF(ecode);
473                 }
474                 if (c != 0 && c != 0x43)
475                         break;
476         }
477         return len;
478 }
479
480 static enum NTDB_ERROR check_linear(struct ntdb_context *ntdb,
481                                    ntdb_off_t **used, size_t *num_used,
482                                    ntdb_off_t **fr, size_t *num_free,
483                                    uint64_t features, ntdb_off_t recovery)
484 {
485         ntdb_off_t off;
486         ntdb_len_t len;
487         enum NTDB_ERROR ecode;
488         bool found_recovery = false;
489
490         for (off = sizeof(struct ntdb_header);
491              off < ntdb->file->map_size;
492              off += len) {
493                 union {
494                         struct ntdb_used_record u;
495                         struct ntdb_free_record f;
496                         struct ntdb_recovery_record r;
497                 } rec;
498                 /* r is larger: only get that if we need to. */
499                 ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec.f));
500                 if (ecode != NTDB_SUCCESS) {
501                         return ecode;
502                 }
503
504                 /* If we crash after ftruncate, we can get zeroes or fill. */
505                 if (rec.r.magic == NTDB_RECOVERY_INVALID_MAGIC
506                     || rec.r.magic ==  0x4343434343434343ULL) {
507                         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec.r));
508                         if (ecode != NTDB_SUCCESS) {
509                                 return ecode;
510                         }
511                         if (recovery == off) {
512                                 found_recovery = true;
513                                 len = sizeof(rec.r) + rec.r.max_len;
514                         } else {
515                                 len = dead_space(ntdb, off);
516                                 if (NTDB_OFF_IS_ERR(len)) {
517                                         return NTDB_OFF_TO_ERR(len);
518                                 }
519                                 if (len < sizeof(rec.r)) {
520                                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
521                                                           NTDB_LOG_ERROR,
522                                                           "ntdb_check: invalid"
523                                                           " dead space at %zu",
524                                                           (size_t)off);
525                                 }
526
527                                 ntdb_logerr(ntdb, NTDB_SUCCESS, NTDB_LOG_WARNING,
528                                            "Dead space at %zu-%zu (of %zu)",
529                                            (size_t)off, (size_t)(off + len),
530                                            (size_t)ntdb->file->map_size);
531                         }
532                 } else if (rec.r.magic == NTDB_RECOVERY_MAGIC) {
533                         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec.r));
534                         if (ecode != NTDB_SUCCESS) {
535                                 return ecode;
536                         }
537                         if (recovery != off) {
538                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
539                                                   NTDB_LOG_ERROR,
540                                                   "ntdb_check: unexpected"
541                                                   " recovery record at offset"
542                                                   " %zu",
543                                                   (size_t)off);
544                         }
545                         if (rec.r.len > rec.r.max_len) {
546                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
547                                                   NTDB_LOG_ERROR,
548                                                   "ntdb_check: invalid recovery"
549                                                   " length %zu",
550                                                   (size_t)rec.r.len);
551                         }
552                         if (rec.r.eof > ntdb->file->map_size) {
553                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
554                                                   NTDB_LOG_ERROR,
555                                                   "ntdb_check: invalid old EOF"
556                                                   " %zu", (size_t)rec.r.eof);
557                         }
558                         found_recovery = true;
559                         len = sizeof(rec.r) + rec.r.max_len;
560                 } else if (frec_magic(&rec.f) == NTDB_FREE_MAGIC) {
561                         len = sizeof(rec.u) + frec_len(&rec.f);
562                         if (off + len > ntdb->file->map_size) {
563                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
564                                                   NTDB_LOG_ERROR,
565                                                   "ntdb_check: free overlength"
566                                                   " %llu at offset %llu",
567                                                   (long long)len,
568                                                   (long long)off);
569                         }
570                         /* This record should be in free lists. */
571                         if (frec_ftable(&rec.f) != NTDB_FTABLE_NONE
572                             && !append(ntdb, fr, num_free, off)) {
573                                 return ntdb_logerr(ntdb, NTDB_ERR_OOM,
574                                                   NTDB_LOG_ERROR,
575                                                   "ntdb_check: tracking %zu'th"
576                                                   " free record.", *num_free);
577                         }
578                 } else if (rec_magic(&rec.u) == NTDB_USED_MAGIC
579                            || rec_magic(&rec.u) == NTDB_CHAIN_MAGIC
580                            || rec_magic(&rec.u) == NTDB_HTABLE_MAGIC
581                            || rec_magic(&rec.u) == NTDB_FTABLE_MAGIC
582                            || rec_magic(&rec.u) == NTDB_CAP_MAGIC) {
583                         uint64_t klen, dlen, extra;
584
585                         /* This record is used! */
586                         if (!append(ntdb, used, num_used, off)) {
587                                 return ntdb_logerr(ntdb, NTDB_ERR_OOM,
588                                                   NTDB_LOG_ERROR,
589                                                   "ntdb_check: tracking %zu'th"
590                                                   " used record.", *num_used);
591                         }
592
593                         klen = rec_key_length(&rec.u);
594                         dlen = rec_data_length(&rec.u);
595                         extra = rec_extra_padding(&rec.u);
596
597                         len = sizeof(rec.u) + klen + dlen + extra;
598                         if (off + len > ntdb->file->map_size) {
599                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
600                                                   NTDB_LOG_ERROR,
601                                                   "ntdb_check: used overlength"
602                                                   " %llu at offset %llu",
603                                                   (long long)len,
604                                                   (long long)off);
605                         }
606
607                         if (len < sizeof(rec.f)) {
608                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
609                                                   NTDB_LOG_ERROR,
610                                                   "ntdb_check: too short record"
611                                                   " %llu at %llu",
612                                                   (long long)len,
613                                                   (long long)off);
614                         }
615
616                         /* Check that records have correct 0 at end (but may
617                          * not in future). */
618                         if (extra && !features
619                             && rec_magic(&rec.u) != NTDB_CAP_MAGIC) {
620                                 const char *p;
621                                 char c;
622                                 p = ntdb_access_read(ntdb, off + sizeof(rec.u)
623                                                     + klen + dlen, 1, false);
624                                 if (NTDB_PTR_IS_ERR(p))
625                                         return NTDB_PTR_ERR(p);
626                                 c = *p;
627                                 ntdb_access_release(ntdb, p);
628
629                                 if (c != '\0') {
630                                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
631                                                           NTDB_LOG_ERROR,
632                                                           "ntdb_check:"
633                                                           " non-zero extra"
634                                                           " at %llu",
635                                                           (long long)off);
636                                 }
637                         }
638                 } else {
639                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
640                                           NTDB_LOG_ERROR,
641                                           "ntdb_check: Bad magic 0x%llx"
642                                           " at offset %zu",
643                                           (long long)rec_magic(&rec.u),
644                                           (size_t)off);
645                 }
646         }
647
648         /* We must have found recovery area if there was one. */
649         if (recovery != 0 && !found_recovery) {
650                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
651                                   "ntdb_check: expected a recovery area at %zu",
652                                   (size_t)recovery);
653         }
654
655         return NTDB_SUCCESS;
656 }
657
658 _PUBLIC_ enum NTDB_ERROR ntdb_check_(struct ntdb_context *ntdb,
659                           enum NTDB_ERROR (*check)(NTDB_DATA, NTDB_DATA, void *),
660                           void *data)
661 {
662         ntdb_off_t *fr = NULL, *used = NULL;
663         ntdb_off_t ft = 0, recovery = 0;
664         size_t num_free = 0, num_used = 0, num_found = 0, num_ftables = 0,
665                 num_capabilities = 0;
666         uint64_t features = 0;
667         enum NTDB_ERROR ecode;
668
669         if (ntdb->flags & NTDB_CANT_CHECK) {
670                 return ntdb_logerr(ntdb, NTDB_SUCCESS, NTDB_LOG_WARNING,
671                                   "ntdb_check: database has unknown capability,"
672                                   " cannot check.");
673         }
674
675         ecode = ntdb_allrecord_lock(ntdb, F_RDLCK, NTDB_LOCK_WAIT, false);
676         if (ecode != NTDB_SUCCESS) {
677                 return ecode;
678         }
679
680         ecode = ntdb_lock_expand(ntdb, F_RDLCK);
681         if (ecode != NTDB_SUCCESS) {
682                 ntdb_allrecord_unlock(ntdb, F_RDLCK);
683                 return ecode;
684         }
685
686         ecode = check_header(ntdb, &recovery, &features, &num_capabilities);
687         if (ecode != NTDB_SUCCESS)
688                 goto out;
689
690         /* First we do a linear scan, checking all records. */
691         ecode = check_linear(ntdb, &used, &num_used, &fr, &num_free, features,
692                              recovery);
693         if (ecode != NTDB_SUCCESS)
694                 goto out;
695
696         for (ft = first_ftable(ntdb); ft; ft = next_ftable(ntdb, ft)) {
697                 if (NTDB_OFF_IS_ERR(ft)) {
698                         ecode = NTDB_OFF_TO_ERR(ft);
699                         goto out;
700                 }
701                 ecode = check_free_table(ntdb, ft, num_ftables, fr, num_free,
702                                          &num_found);
703                 if (ecode != NTDB_SUCCESS)
704                         goto out;
705                 num_ftables++;
706         }
707
708         /* FIXME: Check key uniqueness? */
709         ecode = check_hash(ntdb, used, num_used, num_ftables + num_capabilities,
710                            check, data);
711         if (ecode != NTDB_SUCCESS)
712                 goto out;
713
714         if (num_found != num_free) {
715                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
716                                    "ntdb_check: Not all entries are in"
717                                    " free table");
718         }
719
720 out:
721         ntdb_allrecord_unlock(ntdb, F_RDLCK);
722         ntdb_unlock_expand(ntdb, F_RDLCK);
723         ntdb->free_fn(fr, ntdb->alloc_data);
724         ntdb->free_fn(used, ntdb->alloc_data);
725         return ecode;
726 }