check.c 20 KB

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