ntdb: inline oob check
[kai/samba-autobuild/.git] / lib / 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, ntdb_off_t *recovery,
42                                    uint64_t *features, size_t *num_capabilities)
43 {
44         uint64_t hash_test;
45         struct ntdb_header hdr;
46         enum NTDB_ERROR ecode;
47         ntdb_off_t off, next;
48
49         ecode = ntdb_read_convert(ntdb, 0, &hdr, sizeof(hdr));
50         if (ecode != NTDB_SUCCESS) {
51                 return ecode;
52         }
53         /* magic food should not be converted, so convert back. */
54         ntdb_convert(ntdb, hdr.magic_food, sizeof(hdr.magic_food));
55
56         hash_test = NTDB_HASH_MAGIC;
57         hash_test = ntdb_hash(ntdb, &hash_test, sizeof(hash_test));
58         if (hdr.hash_test != hash_test) {
59                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
60                                   "check: hash test %llu should be %llu",
61                                   (long long)hdr.hash_test,
62                                   (long long)hash_test);
63         }
64
65         if (strcmp(hdr.magic_food, NTDB_MAGIC_FOOD) != 0) {
66                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
67                                   "check: bad magic '%.*s'",
68                                   (unsigned)sizeof(hdr.magic_food),
69                                   hdr.magic_food);
70         }
71
72         /* Features which are used must be a subset of features offered. */
73         if (hdr.features_used & ~hdr.features_offered) {
74                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
75                                   "check: features used (0x%llx) which"
76                                   " are not offered (0x%llx)",
77                                   (long long)hdr.features_used,
78                                   (long long)hdr.features_offered);
79         }
80
81         *features = hdr.features_offered;
82         *recovery = hdr.recovery;
83         if (*recovery) {
84                 if (*recovery < sizeof(hdr)
85                     || *recovery > ntdb->file->map_size) {
86                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
87                                           "ntdb_check:"
88                                           " invalid recovery offset %zu",
89                                           (size_t)*recovery);
90                 }
91         }
92
93         for (off = hdr.capabilities; off && ecode == NTDB_SUCCESS; off = next) {
94                 const struct ntdb_capability *cap;
95                 enum NTDB_ERROR e;
96
97                 cap = ntdb_access_read(ntdb, off, sizeof(*cap), true);
98                 if (NTDB_PTR_IS_ERR(cap)) {
99                         return NTDB_PTR_ERR(cap);
100                 }
101
102                 /* All capabilities are unknown. */
103                 e = unknown_capability(ntdb, "ntdb_check", cap->type);
104                 next = cap->next;
105                 ntdb_access_release(ntdb, cap);
106                 if (e)
107                         return e;
108                 (*num_capabilities)++;
109         }
110
111         /* Don't check reserved: they *can* be used later. */
112         return NTDB_SUCCESS;
113 }
114
115 static enum NTDB_ERROR check_hash_tree(struct ntdb_context *ntdb,
116                                       ntdb_off_t off, unsigned int group_bits,
117                                       uint64_t hprefix,
118                                       unsigned hprefix_bits,
119                                       ntdb_off_t used[],
120                                       size_t num_used,
121                                       size_t *num_found,
122                                       enum NTDB_ERROR (*check)(NTDB_DATA,
123                                                               NTDB_DATA, void *),
124                                       void *data);
125
126 static enum NTDB_ERROR check_hash_chain(struct ntdb_context *ntdb,
127                                        ntdb_off_t off,
128                                        uint64_t hash,
129                                        ntdb_off_t used[],
130                                        size_t num_used,
131                                        size_t *num_found,
132                                        enum NTDB_ERROR (*check)(NTDB_DATA,
133                                                                NTDB_DATA,
134                                                                void *),
135                                        void *data)
136 {
137         struct ntdb_used_record rec;
138         enum NTDB_ERROR ecode;
139
140         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec));
141         if (ecode != NTDB_SUCCESS) {
142                 return ecode;
143         }
144
145         if (rec_magic(&rec) != NTDB_CHAIN_MAGIC) {
146                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
147                                   "ntdb_check: Bad hash chain magic %llu",
148                                   (long long)rec_magic(&rec));
149         }
150
151         if (rec_data_length(&rec) != sizeof(struct ntdb_chain)) {
152                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
153                                   "ntdb_check:"
154                                   " Bad hash chain length %llu vs %zu",
155                                   (long long)rec_data_length(&rec),
156                                   sizeof(struct ntdb_chain));
157         }
158         if (rec_key_length(&rec) != 0) {
159                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
160                                   "ntdb_check: Bad hash chain key length %llu",
161                                   (long long)rec_key_length(&rec));
162         }
163         if (rec_hash(&rec) != 0) {
164                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
165                                   "ntdb_check: Bad hash chain hash value %llu",
166                                   (long long)rec_hash(&rec));
167         }
168
169         off += sizeof(rec);
170         ecode = check_hash_tree(ntdb, off, 0, hash, 64,
171                                 used, num_used, num_found, check, data);
172         if (ecode != NTDB_SUCCESS) {
173                 return ecode;
174         }
175
176         off = ntdb_read_off(ntdb, off + offsetof(struct ntdb_chain, next));
177         if (NTDB_OFF_IS_ERR(off)) {
178                 return NTDB_OFF_TO_ERR(off);
179         }
180         if (off == 0)
181                 return NTDB_SUCCESS;
182         (*num_found)++;
183         return check_hash_chain(ntdb, off, hash, used, num_used, num_found,
184                                 check, data);
185 }
186
187 static enum NTDB_ERROR check_hash_record(struct ntdb_context *ntdb,
188                                         ntdb_off_t off,
189                                         uint64_t hprefix,
190                                         unsigned hprefix_bits,
191                                         ntdb_off_t used[],
192                                         size_t num_used,
193                                         size_t *num_found,
194                                         enum NTDB_ERROR (*check)(NTDB_DATA,
195                                                                 NTDB_DATA,
196                                                                 void *),
197                                         void *data)
198 {
199         struct ntdb_used_record rec;
200         enum NTDB_ERROR ecode;
201
202         if (hprefix_bits >= 64)
203                 return check_hash_chain(ntdb, off, hprefix, used, num_used,
204                                         num_found, check, data);
205
206         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec));
207         if (ecode != NTDB_SUCCESS) {
208                 return ecode;
209         }
210
211         if (rec_magic(&rec) != NTDB_HTABLE_MAGIC) {
212                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
213                                   "ntdb_check: Bad hash table magic %llu",
214                                   (long long)rec_magic(&rec));
215         }
216         if (rec_data_length(&rec)
217             != sizeof(ntdb_off_t) << NTDB_SUBLEVEL_HASH_BITS) {
218                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
219                                   "ntdb_check:"
220                                   " Bad hash table length %llu vs %llu",
221                                   (long long)rec_data_length(&rec),
222                                   (long long)sizeof(ntdb_off_t)
223                                   << NTDB_SUBLEVEL_HASH_BITS);
224         }
225         if (rec_key_length(&rec) != 0) {
226                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
227                                   "ntdb_check: Bad hash table key length %llu",
228                                   (long long)rec_key_length(&rec));
229         }
230         if (rec_hash(&rec) != 0) {
231                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
232                                   "ntdb_check: Bad hash table hash value %llu",
233                                   (long long)rec_hash(&rec));
234         }
235
236         off += sizeof(rec);
237         return check_hash_tree(ntdb, off,
238                                NTDB_SUBLEVEL_HASH_BITS-NTDB_HASH_GROUP_BITS,
239                                hprefix, hprefix_bits,
240                                used, num_used, num_found, check, data);
241 }
242
243 static int off_cmp(const ntdb_off_t *a, const ntdb_off_t *b)
244 {
245         /* Can overflow an int. */
246         return *a > *b ? 1
247                 : *a < *b ? -1
248                 : 0;
249 }
250
251 static uint64_t get_bits(uint64_t h, unsigned num, unsigned *used)
252 {
253         *used += num;
254
255         return (h >> (64 - *used)) & ((1U << num) - 1);
256 }
257
258 static enum NTDB_ERROR check_hash_tree(struct ntdb_context *ntdb,
259                                       ntdb_off_t off, unsigned int group_bits,
260                                       uint64_t hprefix,
261                                       unsigned hprefix_bits,
262                                       ntdb_off_t used[],
263                                       size_t num_used,
264                                       size_t *num_found,
265                                       enum NTDB_ERROR (*check)(NTDB_DATA,
266                                                               NTDB_DATA, void *),
267                                       void *data)
268 {
269         unsigned int g, b;
270         const ntdb_off_t *hash;
271         struct ntdb_used_record rec;
272         enum NTDB_ERROR ecode;
273
274         hash = ntdb_access_read(ntdb, off,
275                                sizeof(ntdb_off_t)
276                                << (group_bits + NTDB_HASH_GROUP_BITS),
277                                true);
278         if (NTDB_PTR_IS_ERR(hash)) {
279                 return NTDB_PTR_ERR(hash);
280         }
281
282         for (g = 0; g < (1 << group_bits); g++) {
283                 const ntdb_off_t *group = hash + (g << NTDB_HASH_GROUP_BITS);
284                 for (b = 0; b < (1 << NTDB_HASH_GROUP_BITS); b++) {
285                         unsigned int bucket, i, used_bits;
286                         uint64_t h;
287                         ntdb_off_t *p;
288                         if (group[b] == 0)
289                                 continue;
290
291                         off = group[b] & NTDB_OFF_MASK;
292                         p = asearch(&off, used, num_used, off_cmp);
293                         if (!p) {
294                                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
295                                                    NTDB_LOG_ERROR,
296                                                    "ntdb_check: Invalid offset"
297                                                    " %llu in hash",
298                                                    (long long)off);
299                                 goto fail;
300                         }
301                         /* Mark it invalid. */
302                         *p ^= 1;
303                         (*num_found)++;
304
305                         if (hprefix_bits == 64) {
306                                 /* Chained entries are unordered. */
307                                 if (is_subhash(group[b])) {
308                                         ecode = NTDB_ERR_CORRUPT;
309                                         ntdb_logerr(ntdb, ecode,
310                                                    NTDB_LOG_ERROR,
311                                                    "ntdb_check: Invalid chain"
312                                                    " entry subhash");
313                                         goto fail;
314                                 }
315                                 h = hash_record(ntdb, off);
316                                 if (h != hprefix) {
317                                         ecode = NTDB_ERR_CORRUPT;
318                                         ntdb_logerr(ntdb, ecode,
319                                                    NTDB_LOG_ERROR,
320                                                    "check: bad hash chain"
321                                                    " placement"
322                                                    " 0x%llx vs 0x%llx",
323                                                    (long long)h,
324                                                    (long long)hprefix);
325                                         goto fail;
326                                 }
327                                 ecode = ntdb_read_convert(ntdb, off, &rec,
328                                                          sizeof(rec));
329                                 if (ecode != NTDB_SUCCESS) {
330                                         goto fail;
331                                 }
332                                 goto check;
333                         }
334
335                         if (is_subhash(group[b])) {
336                                 uint64_t subprefix;
337                                 subprefix = (hprefix
338                                      << (group_bits + NTDB_HASH_GROUP_BITS))
339                                         + g * (1 << NTDB_HASH_GROUP_BITS) + b;
340
341                                 ecode = check_hash_record(ntdb,
342                                                group[b] & NTDB_OFF_MASK,
343                                                subprefix,
344                                                hprefix_bits
345                                                        + group_bits
346                                                        + NTDB_HASH_GROUP_BITS,
347                                                used, num_used, num_found,
348                                                check, data);
349                                 if (ecode != NTDB_SUCCESS) {
350                                         goto fail;
351                                 }
352                                 continue;
353                         }
354                         /* A normal entry */
355
356                         /* Does it belong here at all? */
357                         h = hash_record(ntdb, off);
358                         used_bits = 0;
359                         if (get_bits(h, hprefix_bits, &used_bits) != hprefix
360                             && hprefix_bits) {
361                                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
362                                                    NTDB_LOG_ERROR,
363                                                    "check: bad hash placement"
364                                                    " 0x%llx vs 0x%llx",
365                                                    (long long)h,
366                                                    (long long)hprefix);
367                                 goto fail;
368                         }
369
370                         /* Does it belong in this group? */
371                         if (get_bits(h, group_bits, &used_bits) != g) {
372                                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
373                                                    NTDB_LOG_ERROR,
374                                                    "check: bad group %llu"
375                                                    " vs %u",
376                                                    (long long)h, g);
377                                 goto fail;
378                         }
379
380                         /* Are bucket bits correct? */
381                         bucket = group[b] & NTDB_OFF_HASH_GROUP_MASK;
382                         if (get_bits(h, NTDB_HASH_GROUP_BITS, &used_bits)
383                             != bucket) {
384                                 used_bits -= NTDB_HASH_GROUP_BITS;
385                                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
386                                                    NTDB_LOG_ERROR,
387                                                    "check: bad bucket %u vs %u",
388                                                    (unsigned)get_bits(h,
389                                                         NTDB_HASH_GROUP_BITS,
390                                                         &used_bits),
391                                                    bucket);
392                                 goto fail;
393                         }
394
395                         /* There must not be any zero entries between
396                          * the bucket it belongs in and this one! */
397                         for (i = bucket;
398                              i != b;
399                              i = (i + 1) % (1 << NTDB_HASH_GROUP_BITS)) {
400                                 if (group[i] == 0) {
401                                         ecode = NTDB_ERR_CORRUPT;
402                                         ntdb_logerr(ntdb, ecode,
403                                                    NTDB_LOG_ERROR,
404                                                    "check: bad group placement"
405                                                    " %u vs %u",
406                                                    b, bucket);
407                                         goto fail;
408                                 }
409                         }
410
411                         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec));
412                         if (ecode != NTDB_SUCCESS) {
413                                 goto fail;
414                         }
415
416                         /* Bottom bits must match header. */
417                         if ((h & ((1 << 11)-1)) != rec_hash(&rec)) {
418                                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
419                                                    NTDB_LOG_ERROR,
420                                                    "ntdb_check: Bad hash magic"
421                                                    " at offset %llu"
422                                                    " (0x%llx vs 0x%llx)",
423                                                    (long long)off,
424                                                    (long long)h,
425                                                    (long long)rec_hash(&rec));
426                                 goto fail;
427                         }
428
429                 check:
430                         if (check) {
431                                 NTDB_DATA k, d;
432                                 const unsigned char *kptr;
433
434                                 kptr = ntdb_access_read(ntdb,
435                                                        off + sizeof(rec),
436                                                        rec_key_length(&rec)
437                                                        + rec_data_length(&rec),
438                                                        false);
439                                 if (NTDB_PTR_IS_ERR(kptr)) {
440                                         ecode = NTDB_PTR_ERR(kptr);
441                                         goto fail;
442                                 }
443
444                                 k = ntdb_mkdata(kptr, rec_key_length(&rec));
445                                 d = ntdb_mkdata(kptr + k.dsize,
446                                                rec_data_length(&rec));
447                                 ecode = check(k, d, data);
448                                 ntdb_access_release(ntdb, kptr);
449                                 if (ecode != NTDB_SUCCESS) {
450                                         goto fail;
451                                 }
452                         }
453                 }
454         }
455         ntdb_access_release(ntdb, hash);
456         return NTDB_SUCCESS;
457
458 fail:
459         ntdb_access_release(ntdb, hash);
460         return ecode;
461 }
462
463 static enum NTDB_ERROR check_hash(struct ntdb_context *ntdb,
464                                  ntdb_off_t used[],
465                                  size_t num_used, size_t num_other_used,
466                                  enum NTDB_ERROR (*check)(NTDB_DATA, NTDB_DATA, void *),
467                                  void *data)
468 {
469         /* Free tables and capabilities also show up as used. */
470         size_t num_found = num_other_used;
471         enum NTDB_ERROR ecode;
472
473         ecode = check_hash_tree(ntdb, offsetof(struct ntdb_header, hashtable),
474                                 NTDB_TOPLEVEL_HASH_BITS-NTDB_HASH_GROUP_BITS,
475                                 0, 0, used, num_used, &num_found,
476                                 check, data);
477         if (ecode == NTDB_SUCCESS) {
478                 if (num_found != num_used) {
479                         ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
480                                            "ntdb_check: Not all entries"
481                                            " are in hash");
482                 }
483         }
484         return ecode;
485 }
486
487 static enum NTDB_ERROR check_free(struct ntdb_context *ntdb,
488                                  ntdb_off_t off,
489                                  const struct ntdb_free_record *frec,
490                                  ntdb_off_t prev, unsigned int ftable,
491                                  unsigned int bucket)
492 {
493         enum NTDB_ERROR ecode;
494
495         if (frec_magic(frec) != NTDB_FREE_MAGIC) {
496                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
497                                   "ntdb_check: offset %llu bad magic 0x%llx",
498                                   (long long)off,
499                                   (long long)frec->magic_and_prev);
500         }
501         if (frec_ftable(frec) != ftable) {
502                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
503                                   "ntdb_check: offset %llu bad freetable %u",
504                                   (long long)off, frec_ftable(frec));
505
506         }
507
508         ecode = ntdb_oob(ntdb, off,
509                          frec_len(frec) + sizeof(struct ntdb_used_record),
510                          false);
511         if (ecode != NTDB_SUCCESS) {
512                 return ecode;
513         }
514         if (size_to_bucket(frec_len(frec)) != bucket) {
515                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
516                                   "ntdb_check: offset %llu in wrong bucket"
517                                   " (%u vs %u)",
518                                   (long long)off,
519                                   bucket, size_to_bucket(frec_len(frec)));
520         }
521         if (prev && prev != frec_prev(frec)) {
522                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
523                                   "ntdb_check: offset %llu bad prev"
524                                   " (%llu vs %llu)",
525                                   (long long)off,
526                                   (long long)prev, (long long)frec_len(frec));
527         }
528         return NTDB_SUCCESS;
529 }
530
531 static enum NTDB_ERROR check_free_table(struct ntdb_context *ntdb,
532                                        ntdb_off_t ftable_off,
533                                        unsigned ftable_num,
534                                        ntdb_off_t fr[],
535                                        size_t num_free,
536                                        size_t *num_found)
537 {
538         struct ntdb_freetable ft;
539         ntdb_off_t h;
540         unsigned int i;
541         enum NTDB_ERROR ecode;
542
543         ecode = ntdb_read_convert(ntdb, ftable_off, &ft, sizeof(ft));
544         if (ecode != NTDB_SUCCESS) {
545                 return ecode;
546         }
547
548         if (rec_magic(&ft.hdr) != NTDB_FTABLE_MAGIC
549             || rec_key_length(&ft.hdr) != 0
550             || rec_data_length(&ft.hdr) != sizeof(ft) - sizeof(ft.hdr)
551             || rec_hash(&ft.hdr) != 0) {
552                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
553                                   "ntdb_check: Invalid header on free table");
554         }
555
556         for (i = 0; i < NTDB_FREE_BUCKETS; i++) {
557                 ntdb_off_t off, prev = 0, *p, first = 0;
558                 struct ntdb_free_record f;
559
560                 h = bucket_off(ftable_off, i);
561                 for (off = ntdb_read_off(ntdb, h); off; off = f.next) {
562                         if (NTDB_OFF_IS_ERR(off)) {
563                                 return NTDB_OFF_TO_ERR(off);
564                         }
565                         if (!first) {
566                                 off &= NTDB_OFF_MASK;
567                                 first = off;
568                         }
569                         ecode = ntdb_read_convert(ntdb, off, &f, sizeof(f));
570                         if (ecode != NTDB_SUCCESS) {
571                                 return ecode;
572                         }
573                         ecode = check_free(ntdb, off, &f, prev, ftable_num, i);
574                         if (ecode != NTDB_SUCCESS) {
575                                 return ecode;
576                         }
577
578                         /* FIXME: Check hash bits */
579                         p = asearch(&off, fr, num_free, off_cmp);
580                         if (!p) {
581                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
582                                                   NTDB_LOG_ERROR,
583                                                   "ntdb_check: Invalid offset"
584                                                   " %llu in free table",
585                                                   (long long)off);
586                         }
587                         /* Mark it invalid. */
588                         *p ^= 1;
589                         (*num_found)++;
590                         prev = off;
591                 }
592
593                 if (first) {
594                         /* Now we can check first back pointer. */
595                         ecode = ntdb_read_convert(ntdb, first, &f, sizeof(f));
596                         if (ecode != NTDB_SUCCESS) {
597                                 return ecode;
598                         }
599                         ecode = check_free(ntdb, first, &f, prev, ftable_num, i);
600                         if (ecode != NTDB_SUCCESS) {
601                                 return ecode;
602                         }
603                 }
604         }
605         return NTDB_SUCCESS;
606 }
607
608 /* Slow, but should be very rare. */
609 ntdb_off_t dead_space(struct ntdb_context *ntdb, ntdb_off_t off)
610 {
611         size_t len;
612         enum NTDB_ERROR ecode;
613
614         for (len = 0; off + len < ntdb->file->map_size; len++) {
615                 char c;
616                 ecode = ntdb->io->tread(ntdb, off, &c, 1);
617                 if (ecode != NTDB_SUCCESS) {
618                         return NTDB_ERR_TO_OFF(ecode);
619                 }
620                 if (c != 0 && c != 0x43)
621                         break;
622         }
623         return len;
624 }
625
626 static enum NTDB_ERROR check_linear(struct ntdb_context *ntdb,
627                                    ntdb_off_t **used, size_t *num_used,
628                                    ntdb_off_t **fr, size_t *num_free,
629                                    uint64_t features, ntdb_off_t recovery)
630 {
631         ntdb_off_t off;
632         ntdb_len_t len;
633         enum NTDB_ERROR ecode;
634         bool found_recovery = false;
635
636         for (off = sizeof(struct ntdb_header);
637              off < ntdb->file->map_size;
638              off += len) {
639                 union {
640                         struct ntdb_used_record u;
641                         struct ntdb_free_record f;
642                         struct ntdb_recovery_record r;
643                 } rec;
644                 /* r is larger: only get that if we need to. */
645                 ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec.f));
646                 if (ecode != NTDB_SUCCESS) {
647                         return ecode;
648                 }
649
650                 /* If we crash after ftruncate, we can get zeroes or fill. */
651                 if (rec.r.magic == NTDB_RECOVERY_INVALID_MAGIC
652                     || rec.r.magic ==  0x4343434343434343ULL) {
653                         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec.r));
654                         if (ecode != NTDB_SUCCESS) {
655                                 return ecode;
656                         }
657                         if (recovery == off) {
658                                 found_recovery = true;
659                                 len = sizeof(rec.r) + rec.r.max_len;
660                         } else {
661                                 len = dead_space(ntdb, off);
662                                 if (NTDB_OFF_IS_ERR(len)) {
663                                         return NTDB_OFF_TO_ERR(len);
664                                 }
665                                 if (len < sizeof(rec.r)) {
666                                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
667                                                           NTDB_LOG_ERROR,
668                                                           "ntdb_check: invalid"
669                                                           " dead space at %zu",
670                                                           (size_t)off);
671                                 }
672
673                                 ntdb_logerr(ntdb, NTDB_SUCCESS, NTDB_LOG_WARNING,
674                                            "Dead space at %zu-%zu (of %zu)",
675                                            (size_t)off, (size_t)(off + len),
676                                            (size_t)ntdb->file->map_size);
677                         }
678                 } else if (rec.r.magic == NTDB_RECOVERY_MAGIC) {
679                         ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec.r));
680                         if (ecode != NTDB_SUCCESS) {
681                                 return ecode;
682                         }
683                         if (recovery != off) {
684                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
685                                                   NTDB_LOG_ERROR,
686                                                   "ntdb_check: unexpected"
687                                                   " recovery record at offset"
688                                                   " %zu",
689                                                   (size_t)off);
690                         }
691                         if (rec.r.len > rec.r.max_len) {
692                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
693                                                   NTDB_LOG_ERROR,
694                                                   "ntdb_check: invalid recovery"
695                                                   " length %zu",
696                                                   (size_t)rec.r.len);
697                         }
698                         if (rec.r.eof > ntdb->file->map_size) {
699                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
700                                                   NTDB_LOG_ERROR,
701                                                   "ntdb_check: invalid old EOF"
702                                                   " %zu", (size_t)rec.r.eof);
703                         }
704                         found_recovery = true;
705                         len = sizeof(rec.r) + rec.r.max_len;
706                 } else if (frec_magic(&rec.f) == NTDB_FREE_MAGIC) {
707                         len = sizeof(rec.u) + frec_len(&rec.f);
708                         if (off + len > ntdb->file->map_size) {
709                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
710                                                   NTDB_LOG_ERROR,
711                                                   "ntdb_check: free overlength"
712                                                   " %llu at offset %llu",
713                                                   (long long)len,
714                                                   (long long)off);
715                         }
716                         /* This record should be in free lists. */
717                         if (frec_ftable(&rec.f) != NTDB_FTABLE_NONE
718                             && !append(ntdb, fr, num_free, off)) {
719                                 return ntdb_logerr(ntdb, NTDB_ERR_OOM,
720                                                   NTDB_LOG_ERROR,
721                                                   "ntdb_check: tracking %zu'th"
722                                                   " free record.", *num_free);
723                         }
724                 } else if (rec_magic(&rec.u) == NTDB_USED_MAGIC
725                            || rec_magic(&rec.u) == NTDB_CHAIN_MAGIC
726                            || rec_magic(&rec.u) == NTDB_HTABLE_MAGIC
727                            || rec_magic(&rec.u) == NTDB_FTABLE_MAGIC
728                            || rec_magic(&rec.u) == NTDB_CAP_MAGIC) {
729                         uint64_t klen, dlen, extra;
730
731                         /* This record is used! */
732                         if (!append(ntdb, used, num_used, off)) {
733                                 return ntdb_logerr(ntdb, NTDB_ERR_OOM,
734                                                   NTDB_LOG_ERROR,
735                                                   "ntdb_check: tracking %zu'th"
736                                                   " used record.", *num_used);
737                         }
738
739                         klen = rec_key_length(&rec.u);
740                         dlen = rec_data_length(&rec.u);
741                         extra = rec_extra_padding(&rec.u);
742
743                         len = sizeof(rec.u) + klen + dlen + extra;
744                         if (off + len > ntdb->file->map_size) {
745                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
746                                                   NTDB_LOG_ERROR,
747                                                   "ntdb_check: used overlength"
748                                                   " %llu at offset %llu",
749                                                   (long long)len,
750                                                   (long long)off);
751                         }
752
753                         if (len < sizeof(rec.f)) {
754                                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
755                                                   NTDB_LOG_ERROR,
756                                                   "ntdb_check: too short record"
757                                                   " %llu at %llu",
758                                                   (long long)len,
759                                                   (long long)off);
760                         }
761
762                         /* Check that records have correct 0 at end (but may
763                          * not in future). */
764                         if (extra && !features
765                             && rec_magic(&rec.u) != NTDB_CAP_MAGIC) {
766                                 const char *p;
767                                 char c;
768                                 p = ntdb_access_read(ntdb, off + sizeof(rec.u)
769                                                     + klen + dlen, 1, false);
770                                 if (NTDB_PTR_IS_ERR(p))
771                                         return NTDB_PTR_ERR(p);
772                                 c = *p;
773                                 ntdb_access_release(ntdb, p);
774
775                                 if (c != '\0') {
776                                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
777                                                           NTDB_LOG_ERROR,
778                                                           "ntdb_check:"
779                                                           " non-zero extra"
780                                                           " at %llu",
781                                                           (long long)off);
782                                 }
783                         }
784                 } else {
785                         return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT,
786                                           NTDB_LOG_ERROR,
787                                           "ntdb_check: Bad magic 0x%llx"
788                                           " at offset %zu",
789                                           (long long)rec_magic(&rec.u),
790                                           (size_t)off);
791                 }
792         }
793
794         /* We must have found recovery area if there was one. */
795         if (recovery != 0 && !found_recovery) {
796                 return ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
797                                   "ntdb_check: expected a recovery area at %zu",
798                                   (size_t)recovery);
799         }
800
801         return NTDB_SUCCESS;
802 }
803
804 _PUBLIC_ enum NTDB_ERROR ntdb_check_(struct ntdb_context *ntdb,
805                           enum NTDB_ERROR (*check)(NTDB_DATA, NTDB_DATA, void *),
806                           void *data)
807 {
808         ntdb_off_t *fr = NULL, *used = NULL, ft, recovery;
809         size_t num_free = 0, num_used = 0, num_found = 0, num_ftables = 0,
810                 num_capabilities = 0;
811         uint64_t features;
812         enum NTDB_ERROR ecode;
813
814         if (ntdb->flags & NTDB_CANT_CHECK) {
815                 return ntdb_logerr(ntdb, NTDB_SUCCESS, NTDB_LOG_WARNING,
816                                   "ntdb_check: database has unknown capability,"
817                                   " cannot check.");
818         }
819
820         ecode = ntdb_allrecord_lock(ntdb, F_RDLCK, NTDB_LOCK_WAIT, false);
821         if (ecode != NTDB_SUCCESS) {
822                 return ecode;
823         }
824
825         ecode = ntdb_lock_expand(ntdb, F_RDLCK);
826         if (ecode != NTDB_SUCCESS) {
827                 ntdb_allrecord_unlock(ntdb, F_RDLCK);
828                 return ecode;
829         }
830
831         ecode = check_header(ntdb, &recovery, &features, &num_capabilities);
832         if (ecode != NTDB_SUCCESS)
833                 goto out;
834
835         /* First we do a linear scan, checking all records. */
836         ecode = check_linear(ntdb, &used, &num_used, &fr, &num_free, features,
837                              recovery);
838         if (ecode != NTDB_SUCCESS)
839                 goto out;
840
841         for (ft = first_ftable(ntdb); ft; ft = next_ftable(ntdb, ft)) {
842                 if (NTDB_OFF_IS_ERR(ft)) {
843                         ecode = NTDB_OFF_TO_ERR(ft);
844                         goto out;
845                 }
846                 ecode = check_free_table(ntdb, ft, num_ftables, fr, num_free,
847                                          &num_found);
848                 if (ecode != NTDB_SUCCESS)
849                         goto out;
850                 num_ftables++;
851         }
852
853         /* FIXME: Check key uniqueness? */
854         ecode = check_hash(ntdb, used, num_used, num_ftables + num_capabilities,
855                            check, data);
856         if (ecode != NTDB_SUCCESS)
857                 goto out;
858
859         if (num_found != num_free) {
860                 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
861                                    "ntdb_check: Not all entries are in"
862                                    " free table");
863         }
864
865 out:
866         ntdb_allrecord_unlock(ntdb, F_RDLCK);
867         ntdb_unlock_expand(ntdb, F_RDLCK);
868         ntdb->free_fn(fr, ntdb->alloc_data);
869         ntdb->free_fn(used, ntdb->alloc_data);
870         return ecode;
871 }