s3:auth small optimization in create_token_from_sid
[Samba/id10ts.git] / lib / ntdb / check.c
blob2790c68eafdca98a474b3c7a2f14ec5341caaf63
1 /*
2 Trivial Database 2: free list/block handling
3 Copyright (C) Rusty Russell 2010
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 #include "private.h"
19 #include <ccan/likely/likely.h>
20 #include <ccan/asearch/asearch.h>
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)
26 ntdb_off_t *new;
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);
34 if (!new)
35 return false;
36 new[(*num)++] = off;
37 *arr = new;
38 return true;
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)
46 uint64_t hash_test;
47 struct ntdb_header hdr;
48 enum NTDB_ERROR ecode;
49 ntdb_off_t off, next;
51 ecode = ntdb_read_convert(ntdb, 0, &hdr, sizeof(hdr));
52 if (ecode != NTDB_SUCCESS) {
53 return ecode;
55 /* magic food should not be converted, so convert back. */
56 ntdb_convert(ntdb, hdr.magic_food, sizeof(hdr.magic_food));
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);
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);
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);
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);
95 for (off = hdr.capabilities; off && ecode == NTDB_SUCCESS; off = next) {
96 const struct ntdb_capability *cap;
97 enum NTDB_ERROR e;
99 cap = ntdb_access_read(ntdb, off, sizeof(*cap), true);
100 if (NTDB_PTR_IS_ERR(cap)) {
101 return NTDB_PTR_ERR(cap);
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)++;
113 /* Don't check reserved: they *can* be used later. */
114 return NTDB_SUCCESS;
117 static int off_cmp(const ntdb_off_t *a, const ntdb_off_t *b)
119 /* Can overflow an int. */
120 return *a > *b ? 1
121 : *a < *b ? -1
122 : 0;
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)
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;
144 /* Empty bucket is fine. */
145 if (!off_and_hash) {
146 return NTDB_SUCCESS;
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);
156 p = asearch(&off, used, num_used, off_cmp);
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);
162 /* Mark it invalid. */
163 *p ^= 1;
164 (*num_found)++;
166 r = ntdb_access_read(ntdb, off, sizeof(*r), true);
167 if (NTDB_PTR_IS_ERR(r)) {
168 return NTDB_PTR_ERR(r);
170 klen = rec_key_length(r);
171 dlen = rec_data_length(r);
172 ntdb_access_release(ntdb, r);
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);
179 hash = ntdb_hash(ntdb, kptr, klen);
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;
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;
206 ntdb_access_release(ntdb, kptr);
208 return ecode;
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)
222 struct ntdb_used_record rec;
223 enum NTDB_ERROR ecode;
224 const ntdb_off_t *entries;
225 ntdb_len_t i, num;
227 /* This is a used entry. */
228 (*num_found)++;
230 ecode = ntdb_read_convert(ntdb, off, &rec, sizeof(rec));
231 if (ecode != NTDB_SUCCESS) {
232 return ecode;
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));
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));
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));
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);
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;
269 ntdb_access_release(ntdb, entries);
270 return ecode;
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)
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;
289 ecode = ntdb_read_convert(ntdb, NTDB_HASH_OFFSET, &rec, sizeof(rec));
290 if (ecode != NTDB_SUCCESS) {
291 return ecode;
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));
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));
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));
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);
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);
329 if (ecode) {
330 break;
333 ntdb_access_release(ntdb, entries);
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");
339 return ecode;
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)
348 enum NTDB_ERROR ecode;
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);
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));
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;
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)));
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));
383 return NTDB_SUCCESS;
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)
393 struct ntdb_freetable ft;
394 ntdb_off_t h;
395 unsigned int i;
396 enum NTDB_ERROR ecode;
398 ecode = ntdb_read_convert(ntdb, ftable_off, &ft, sizeof(ft));
399 if (ecode != NTDB_SUCCESS) {
400 return ecode;
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");
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;
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);
419 if (!first) {
420 off &= NTDB_OFF_MASK;
421 first = off;
423 ecode = ntdb_read_convert(ntdb, off, &f, sizeof(f));
424 if (ecode != NTDB_SUCCESS) {
425 return ecode;
427 ecode = check_free(ntdb, off, &f, prev, ftable_num, i);
428 if (ecode != NTDB_SUCCESS) {
429 return ecode;
432 /* FIXME: Check hash bits */
433 p = asearch(&off, fr, num_free, off_cmp);
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);
441 /* Mark it invalid. */
442 *p ^= 1;
443 (*num_found)++;
444 prev = off;
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;
453 ecode = check_free(ntdb, first, &f, prev, ftable_num, i);
454 if (ecode != NTDB_SUCCESS) {
455 return ecode;
459 return NTDB_SUCCESS;
462 /* Slow, but should be very rare. */
463 ntdb_off_t dead_space(struct ntdb_context *ntdb, ntdb_off_t off)
465 size_t len;
466 enum NTDB_ERROR ecode;
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);
474 if (c != 0 && c != 0x43)
475 break;
477 return len;
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)
485 ntdb_off_t off;
486 ntdb_len_t len;
487 enum NTDB_ERROR ecode;
488 bool found_recovery = false;
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;
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;
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);
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);
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);
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;
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);
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);
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);
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);
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);
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;
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);
593 klen = rec_key_length(&rec.u);
594 dlen = rec_data_length(&rec.u);
595 extra = rec_extra_padding(&rec.u);
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);
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);
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);
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);
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);
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);
655 return NTDB_SUCCESS;
658 _PUBLIC_ enum NTDB_ERROR ntdb_check_(struct ntdb_context *ntdb,
659 enum NTDB_ERROR (*check)(NTDB_DATA, NTDB_DATA, void *),
660 void *data)
662 ntdb_off_t *fr = NULL, *used = NULL, ft, recovery;
663 size_t num_free = 0, num_used = 0, num_found = 0, num_ftables = 0,
664 num_capabilities = 0;
665 uint64_t features;
666 enum NTDB_ERROR ecode;
668 if (ntdb->flags & NTDB_CANT_CHECK) {
669 return ntdb_logerr(ntdb, NTDB_SUCCESS, NTDB_LOG_WARNING,
670 "ntdb_check: database has unknown capability,"
671 " cannot check.");
674 ecode = ntdb_allrecord_lock(ntdb, F_RDLCK, NTDB_LOCK_WAIT, false);
675 if (ecode != NTDB_SUCCESS) {
676 return ecode;
679 ecode = ntdb_lock_expand(ntdb, F_RDLCK);
680 if (ecode != NTDB_SUCCESS) {
681 ntdb_allrecord_unlock(ntdb, F_RDLCK);
682 return ecode;
685 ecode = check_header(ntdb, &recovery, &features, &num_capabilities);
686 if (ecode != NTDB_SUCCESS)
687 goto out;
689 /* First we do a linear scan, checking all records. */
690 ecode = check_linear(ntdb, &used, &num_used, &fr, &num_free, features,
691 recovery);
692 if (ecode != NTDB_SUCCESS)
693 goto out;
695 for (ft = first_ftable(ntdb); ft; ft = next_ftable(ntdb, ft)) {
696 if (NTDB_OFF_IS_ERR(ft)) {
697 ecode = NTDB_OFF_TO_ERR(ft);
698 goto out;
700 ecode = check_free_table(ntdb, ft, num_ftables, fr, num_free,
701 &num_found);
702 if (ecode != NTDB_SUCCESS)
703 goto out;
704 num_ftables++;
707 /* FIXME: Check key uniqueness? */
708 ecode = check_hash(ntdb, used, num_used, num_ftables + num_capabilities,
709 check, data);
710 if (ecode != NTDB_SUCCESS)
711 goto out;
713 if (num_found != num_free) {
714 ecode = ntdb_logerr(ntdb, NTDB_ERR_CORRUPT, NTDB_LOG_ERROR,
715 "ntdb_check: Not all entries are in"
716 " free table");
719 out:
720 ntdb_allrecord_unlock(ntdb, F_RDLCK);
721 ntdb_unlock_expand(ntdb, F_RDLCK);
722 ntdb->free_fn(fr, ntdb->alloc_data);
723 ntdb->free_fn(used, ntdb->alloc_data);
724 return ecode;