private.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #ifndef TDB_PRIVATE_H
  2. #define TDB_PRIVATE_H
  3. /*
  4. Trivial Database 2: private types and prototypes
  5. Copyright (C) Rusty Russell 2010
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. 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.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define _XOPEN_SOURCE 500
  18. #define _FILE_OFFSET_BITS 64
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include <stdlib.h>
  22. #include <stddef.h>
  23. #include <sys/time.h>
  24. #include <sys/mman.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <utime.h>
  31. #include <unistd.h>
  32. #include "config.h"
  33. #include <ccan/tdb2/tdb2.h>
  34. #include <ccan/likely/likely.h>
  35. #include <ccan/compiler/compiler.h>
  36. #if HAVE_BYTESWAP_H
  37. #include <byteswap.h>
  38. #endif
  39. #ifndef TEST_IT
  40. #define TEST_IT(cond)
  41. #endif
  42. /* #define TDB_TRACE 1 */
  43. #ifndef __STRING
  44. #define __STRING(x) #x
  45. #endif
  46. #ifndef __STRINGSTRING
  47. #define __STRINGSTRING(x) __STRING(x)
  48. #endif
  49. #ifndef __location__
  50. #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
  51. #endif
  52. typedef uint64_t tdb_len_t;
  53. typedef uint64_t tdb_off_t;
  54. #define TDB_MAGIC_FOOD "TDB file\n"
  55. #define TDB_VERSION ((uint64_t)(0x26011967 + 7))
  56. #define TDB_USED_MAGIC ((uint64_t)0x1999)
  57. #define TDB_HTABLE_MAGIC ((uint64_t)0x1888)
  58. #define TDB_CHAIN_MAGIC ((uint64_t)0x1777)
  59. #define TDB_FTABLE_MAGIC ((uint64_t)0x1666)
  60. #define TDB_FREE_MAGIC ((uint64_t)0xFE)
  61. #define TDB_HASH_MAGIC (0xA1ABE11A01092008ULL)
  62. #define TDB_RECOVERY_MAGIC (0xf53bc0e7ad124589ULL)
  63. #define TDB_RECOVERY_INVALID_MAGIC (0x0ULL)
  64. #define TDB_OFF_ERR ((tdb_off_t)-1)
  65. #define TDB_OFF_IS_ERR(off) unlikely(off >= (tdb_off_t)TDB_ERR_LAST)
  66. /* Packing errors into pointers and v.v. */
  67. #define TDB_PTR_IS_ERR(ptr) \
  68. unlikely((void *)(ptr) >= (void *)(long)TDB_ERR_LAST)
  69. #define TDB_PTR_ERR(p) ((enum TDB_ERROR)(long)(p))
  70. #define TDB_ERR_PTR(err) ((void *)(long)(err))
  71. /* Common case of returning true, false or -ve error. */
  72. typedef int tdb_bool_err;
  73. /* Prevent others from opening the file. */
  74. #define TDB_OPEN_LOCK 0
  75. /* Doing a transaction. */
  76. #define TDB_TRANSACTION_LOCK 1
  77. /* Expanding file. */
  78. #define TDB_EXPANSION_LOCK 2
  79. /* Hash chain locks. */
  80. #define TDB_HASH_LOCK_START 3
  81. /* Range for hash locks. */
  82. #define TDB_HASH_LOCK_RANGE_BITS 30
  83. #define TDB_HASH_LOCK_RANGE (1 << TDB_HASH_LOCK_RANGE_BITS)
  84. /* We have 1024 entries in the top level. */
  85. #define TDB_TOPLEVEL_HASH_BITS 10
  86. /* And 64 entries in each sub-level: thus 64 bits exactly after 9 levels. */
  87. #define TDB_SUBLEVEL_HASH_BITS 6
  88. /* And 8 entries in each group, ie 8 groups per sublevel. */
  89. #define TDB_HASH_GROUP_BITS 3
  90. /* This is currently 10: beyond this we chain. */
  91. #define TDB_MAX_LEVELS (1+(64-TDB_TOPLEVEL_HASH_BITS) / TDB_SUBLEVEL_HASH_BITS)
  92. /* Extend file by least 100 times larger than needed. */
  93. #define TDB_EXTENSION_FACTOR 100
  94. /* We steal bits from the offsets to store hash info. */
  95. #define TDB_OFF_HASH_GROUP_MASK ((1ULL << TDB_HASH_GROUP_BITS) - 1)
  96. /* We steal this many upper bits, giving a maximum offset of 64 exabytes. */
  97. #define TDB_OFF_UPPER_STEAL 8
  98. #define TDB_OFF_UPPER_STEAL_EXTRA 7
  99. /* The bit number where we store extra hash bits. */
  100. #define TDB_OFF_HASH_EXTRA_BIT 57
  101. #define TDB_OFF_UPPER_STEAL_SUBHASH_BIT 56
  102. /* The bit number where we store the extra hash bits. */
  103. /* Convenience mask to get actual offset. */
  104. #define TDB_OFF_MASK \
  105. (((1ULL << (64 - TDB_OFF_UPPER_STEAL)) - 1) - TDB_OFF_HASH_GROUP_MASK)
  106. /* How many buckets in a free list: see size_to_bucket(). */
  107. #define TDB_FREE_BUCKETS (64 - TDB_OFF_UPPER_STEAL)
  108. /* We have to be able to fit a free record here. */
  109. #define TDB_MIN_DATA_LEN \
  110. (sizeof(struct tdb_free_record) - sizeof(struct tdb_used_record))
  111. /* Indicates this entry is not on an flist (can happen during coalescing) */
  112. #define TDB_FTABLE_NONE ((1ULL << TDB_OFF_UPPER_STEAL) - 1)
  113. #if !HAVE_BSWAP_64
  114. static inline uint64_t bswap_64(uint64_t x)
  115. {
  116. return (((x&0x000000FFULL)<<56)
  117. | ((x&0x0000FF00ULL)<<48)
  118. | ((x&0x00FF0000ULL)<<40)
  119. | ((x&0xFF000000ULL)<<32)
  120. | ((x>>8)&0xFF000000ULL)
  121. | ((x>>16)&0x00FF0000ULL)
  122. | ((x>>24)&0x0000FF00ULL)
  123. | ((x>>32)&0x000000FFULL));
  124. }
  125. #endif
  126. struct tdb_used_record {
  127. /* For on-disk compatibility, we avoid bitfields:
  128. magic: 16, (highest)
  129. key_len_bits: 5,
  130. extra_padding: 32
  131. hash_bits: 11
  132. */
  133. uint64_t magic_and_meta;
  134. /* The bottom key_len_bits*2 are key length, rest is data length. */
  135. uint64_t key_and_data_len;
  136. };
  137. static inline unsigned rec_key_bits(const struct tdb_used_record *r)
  138. {
  139. return ((r->magic_and_meta >> 43) & ((1 << 5)-1)) * 2;
  140. }
  141. static inline uint64_t rec_key_length(const struct tdb_used_record *r)
  142. {
  143. return r->key_and_data_len & ((1ULL << rec_key_bits(r)) - 1);
  144. }
  145. static inline uint64_t rec_data_length(const struct tdb_used_record *r)
  146. {
  147. return r->key_and_data_len >> rec_key_bits(r);
  148. }
  149. static inline uint64_t rec_extra_padding(const struct tdb_used_record *r)
  150. {
  151. return (r->magic_and_meta >> 11) & 0xFFFFFFFF;
  152. }
  153. static inline uint32_t rec_hash(const struct tdb_used_record *r)
  154. {
  155. return r->magic_and_meta & ((1 << 11) - 1);
  156. }
  157. static inline uint16_t rec_magic(const struct tdb_used_record *r)
  158. {
  159. return (r->magic_and_meta >> 48);
  160. }
  161. struct tdb_free_record {
  162. uint64_t magic_and_prev; /* TDB_OFF_UPPER_STEAL bits magic, then prev */
  163. uint64_t ftable_and_len; /* Len not counting these two fields. */
  164. /* This is why the minimum record size is 8 bytes. */
  165. uint64_t next;
  166. };
  167. static inline uint64_t frec_prev(const struct tdb_free_record *f)
  168. {
  169. return f->magic_and_prev & ((1ULL << (64 - TDB_OFF_UPPER_STEAL)) - 1);
  170. }
  171. static inline uint64_t frec_magic(const struct tdb_free_record *f)
  172. {
  173. return f->magic_and_prev >> (64 - TDB_OFF_UPPER_STEAL);
  174. }
  175. static inline uint64_t frec_len(const struct tdb_free_record *f)
  176. {
  177. return f->ftable_and_len & ((1ULL << (64 - TDB_OFF_UPPER_STEAL))-1);
  178. }
  179. static inline unsigned frec_ftable(const struct tdb_free_record *f)
  180. {
  181. return f->ftable_and_len >> (64 - TDB_OFF_UPPER_STEAL);
  182. }
  183. struct tdb_recovery_record {
  184. uint64_t magic;
  185. /* Length of record. */
  186. uint64_t max_len;
  187. /* Length used. */
  188. uint64_t len;
  189. /* Old length of file before transaction. */
  190. uint64_t eof;
  191. };
  192. /* If we bottom out of the subhashes, we chain. */
  193. struct tdb_chain {
  194. tdb_off_t rec[1 << TDB_HASH_GROUP_BITS];
  195. tdb_off_t next;
  196. };
  197. /* this is stored at the front of every database */
  198. struct tdb_header {
  199. char magic_food[64]; /* for /etc/magic */
  200. /* FIXME: Make me 32 bit? */
  201. uint64_t version; /* version of the code */
  202. uint64_t hash_test; /* result of hashing HASH_MAGIC. */
  203. uint64_t hash_seed; /* "random" seed written at creation time. */
  204. tdb_off_t free_table; /* (First) free table. */
  205. tdb_off_t recovery; /* Transaction recovery area. */
  206. tdb_off_t reserved[26];
  207. /* Top level hash table. */
  208. tdb_off_t hashtable[1ULL << TDB_TOPLEVEL_HASH_BITS];
  209. };
  210. struct tdb_freetable {
  211. struct tdb_used_record hdr;
  212. tdb_off_t next;
  213. tdb_off_t buckets[TDB_FREE_BUCKETS];
  214. };
  215. /* Information about a particular (locked) hash entry. */
  216. struct hash_info {
  217. /* Full hash value of entry. */
  218. uint64_t h;
  219. /* Start and length of lock acquired. */
  220. tdb_off_t hlock_start;
  221. tdb_len_t hlock_range;
  222. /* Start of hash group. */
  223. tdb_off_t group_start;
  224. /* Bucket we belong in. */
  225. unsigned int home_bucket;
  226. /* Bucket we (or an empty space) were found in. */
  227. unsigned int found_bucket;
  228. /* How many bits of the hash are already used. */
  229. unsigned int hash_used;
  230. /* Current working group. */
  231. tdb_off_t group[1 << TDB_HASH_GROUP_BITS];
  232. };
  233. struct traverse_info {
  234. struct traverse_level {
  235. tdb_off_t hashtable;
  236. /* We ignore groups here, and treat it as a big array. */
  237. unsigned entry;
  238. unsigned int total_buckets;
  239. } levels[TDB_MAX_LEVELS + 1];
  240. unsigned int num_levels;
  241. unsigned int toplevel_group;
  242. /* This makes delete-everything-inside-traverse work as expected. */
  243. tdb_off_t prev;
  244. };
  245. enum tdb_lock_flags {
  246. /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
  247. TDB_LOCK_NOWAIT = 0,
  248. TDB_LOCK_WAIT = 1,
  249. /* If set, don't log an error on failure. */
  250. TDB_LOCK_PROBE = 2,
  251. /* If set, don't check for recovery (used by recovery code). */
  252. TDB_LOCK_NOCHECK = 4,
  253. };
  254. struct tdb_lock_type {
  255. uint32_t off;
  256. uint32_t count;
  257. uint32_t ltype;
  258. };
  259. /* This is only needed for tdb_access_commit, but used everywhere to
  260. * simplify. */
  261. struct tdb_access_hdr {
  262. struct tdb_access_hdr *next;
  263. tdb_off_t off;
  264. tdb_len_t len;
  265. bool convert;
  266. };
  267. struct tdb_context {
  268. /* Filename of the database. */
  269. const char *name;
  270. /* Mmap (if any), or malloc (for TDB_INTERNAL). */
  271. void *map_ptr;
  272. /* Are we accessing directly? (debugging check). */
  273. int direct_access;
  274. /* Open file descriptor (undefined for TDB_INTERNAL). */
  275. int fd;
  276. /* How much space has been mapped (<= current file size) */
  277. tdb_len_t map_size;
  278. /* Operating read-only? (Opened O_RDONLY, or in traverse_read) */
  279. bool read_only;
  280. /* mmap read only? */
  281. int mmap_flags;
  282. /* Error code for last tdb error. */
  283. enum TDB_ERROR ecode;
  284. /* the flags passed to tdb_open, for tdb_reopen. */
  285. uint32_t flags;
  286. /* Logging function */
  287. void (*logfn)(struct tdb_context *tdb,
  288. enum tdb_log_level level,
  289. void *log_private,
  290. const char *message);
  291. void *log_private;
  292. /* Hash function. */
  293. uint64_t (*khash)(const void *key, size_t len, uint64_t seed, void *);
  294. void *hash_priv;
  295. uint64_t hash_seed;
  296. /* Set if we are in a transaction. */
  297. struct tdb_transaction *transaction;
  298. /* What free table are we using? */
  299. tdb_off_t ftable_off;
  300. unsigned int ftable;
  301. /* IO methods: changes for transactions. */
  302. const struct tdb_methods *methods;
  303. /* Lock information */
  304. struct tdb_lock_type allrecord_lock;
  305. size_t num_lockrecs;
  306. struct tdb_lock_type *lockrecs;
  307. struct tdb_attribute_stats *stats;
  308. /* Direct access information */
  309. struct tdb_access_hdr *access;
  310. /* Single list of all TDBs, to avoid multiple opens. */
  311. struct tdb_context *next;
  312. dev_t device;
  313. ino_t inode;
  314. };
  315. struct tdb_methods {
  316. enum TDB_ERROR (*tread)(struct tdb_context *, tdb_off_t, void *,
  317. tdb_len_t);
  318. enum TDB_ERROR (*twrite)(struct tdb_context *, tdb_off_t, const void *,
  319. tdb_len_t);
  320. enum TDB_ERROR (*oob)(struct tdb_context *, tdb_off_t, bool);
  321. enum TDB_ERROR (*expand_file)(struct tdb_context *, tdb_len_t);
  322. void *(*direct)(struct tdb_context *, tdb_off_t, size_t, bool);
  323. };
  324. /*
  325. internal prototypes
  326. */
  327. /* hash.c: */
  328. void tdb_hash_init(struct tdb_context *tdb);
  329. tdb_bool_err first_in_hash(struct tdb_context *tdb,
  330. struct traverse_info *tinfo,
  331. TDB_DATA *kbuf, size_t *dlen);
  332. tdb_bool_err next_in_hash(struct tdb_context *tdb,
  333. struct traverse_info *tinfo,
  334. TDB_DATA *kbuf, size_t *dlen);
  335. /* Hash random memory. */
  336. uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len);
  337. /* Hash on disk. */
  338. uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off);
  339. /* Find and lock a hash entry (or where it would be). */
  340. tdb_off_t find_and_lock(struct tdb_context *tdb,
  341. struct tdb_data key,
  342. int ltype,
  343. struct hash_info *h,
  344. struct tdb_used_record *rec,
  345. struct traverse_info *tinfo);
  346. enum TDB_ERROR replace_in_hash(struct tdb_context *tdb,
  347. struct hash_info *h,
  348. tdb_off_t new_off);
  349. enum TDB_ERROR add_to_hash(struct tdb_context *tdb, struct hash_info *h,
  350. tdb_off_t new_off);
  351. enum TDB_ERROR delete_from_hash(struct tdb_context *tdb, struct hash_info *h);
  352. /* For tdb_check */
  353. bool is_subhash(tdb_off_t val);
  354. /* free.c: */
  355. int tdb_ftable_init(struct tdb_context *tdb);
  356. /* check.c needs these to iterate through free lists. */
  357. tdb_off_t first_ftable(struct tdb_context *tdb);
  358. tdb_off_t next_ftable(struct tdb_context *tdb, tdb_off_t ftable);
  359. /* This returns space or TDB_OFF_ERR. */
  360. tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
  361. uint64_t hash, unsigned magic, bool growing);
  362. /* Put this record in a free list. */
  363. int add_free_record(struct tdb_context *tdb,
  364. tdb_off_t off, tdb_len_t len_with_header);
  365. /* Set up header for a used/ftable/htable/chain record. */
  366. int set_header(struct tdb_context *tdb,
  367. struct tdb_used_record *rec,
  368. unsigned magic, uint64_t keylen, uint64_t datalen,
  369. uint64_t actuallen, unsigned hashlow);
  370. /* Used by tdb_check to verify. */
  371. unsigned int size_to_bucket(tdb_len_t data_len);
  372. tdb_off_t bucket_off(tdb_off_t ftable_off, unsigned bucket);
  373. /* Used by tdb_summary */
  374. size_t dead_space(struct tdb_context *tdb, tdb_off_t off);
  375. /* io.c: */
  376. /* Initialize tdb->methods. */
  377. void tdb_io_init(struct tdb_context *tdb);
  378. /* Convert endian of the buffer if required. */
  379. void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size);
  380. /* Unmap and try to map the tdb. */
  381. void tdb_munmap(struct tdb_context *tdb);
  382. void tdb_mmap(struct tdb_context *tdb);
  383. /* Either alloc a copy, or give direct access. Release frees or noop. */
  384. const void *tdb_access_read(struct tdb_context *tdb,
  385. tdb_off_t off, tdb_len_t len, bool convert);
  386. void *tdb_access_write(struct tdb_context *tdb,
  387. tdb_off_t off, tdb_len_t len, bool convert);
  388. /* Release result of tdb_access_read/write. */
  389. void tdb_access_release(struct tdb_context *tdb, const void *p);
  390. /* Commit result of tdb_acces_write. */
  391. enum TDB_ERROR tdb_access_commit(struct tdb_context *tdb, void *p);
  392. /* Convenience routine to get an offset. */
  393. tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off);
  394. /* Write an offset at an offset. */
  395. enum TDB_ERROR tdb_write_off(struct tdb_context *tdb, tdb_off_t off,
  396. tdb_off_t val);
  397. /* Clear an ondisk area. */
  398. enum TDB_ERROR zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len);
  399. /* Return a non-zero offset between >= start < end in this array (or end). */
  400. tdb_off_t tdb_find_nonzero_off(struct tdb_context *tdb,
  401. tdb_off_t base,
  402. uint64_t start,
  403. uint64_t end);
  404. /* Return a zero offset in this array, or num. */
  405. tdb_off_t tdb_find_zero_off(struct tdb_context *tdb, tdb_off_t off,
  406. uint64_t num);
  407. /* Allocate and make a copy of some offset. */
  408. void *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
  409. /* Writes a converted copy of a record. */
  410. enum TDB_ERROR tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
  411. const void *rec, size_t len);
  412. /* Reads record and converts it */
  413. enum TDB_ERROR tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
  414. void *rec, size_t len);
  415. /* Adds a stat, if it's in range. */
  416. void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val);
  417. #define add_stat(tdb, statname, val) \
  418. do { \
  419. if (unlikely((tdb)->stats)) \
  420. add_stat_((tdb), &(tdb)->stats->statname, (val)); \
  421. } while (0)
  422. /* lock.c: */
  423. void tdb_lock_init(struct tdb_context *tdb);
  424. /* Lock/unlock a range of hashes. */
  425. enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
  426. tdb_off_t hash_lock, tdb_len_t hash_range,
  427. int ltype, enum tdb_lock_flags waitflag);
  428. enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
  429. tdb_off_t hash_lock,
  430. tdb_len_t hash_range, int ltype);
  431. /* Lock/unlock a particular free bucket. */
  432. enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
  433. enum tdb_lock_flags waitflag);
  434. void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off);
  435. /* Serialize transaction start. */
  436. enum TDB_ERROR tdb_transaction_lock(struct tdb_context *tdb, int ltype);
  437. void tdb_transaction_unlock(struct tdb_context *tdb, int ltype);
  438. /* Do we have any hash locks (ie. via tdb_chainlock) ? */
  439. bool tdb_has_hash_locks(struct tdb_context *tdb);
  440. /* Lock entire database. */
  441. enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
  442. enum tdb_lock_flags flags, bool upgradable);
  443. void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype);
  444. enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb);
  445. /* Serialize db open. */
  446. enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb,
  447. enum tdb_lock_flags flags);
  448. void tdb_unlock_open(struct tdb_context *tdb);
  449. bool tdb_has_open_lock(struct tdb_context *tdb);
  450. /* Serialize db expand. */
  451. enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype);
  452. void tdb_unlock_expand(struct tdb_context *tdb, int ltype);
  453. bool tdb_has_expansion_lock(struct tdb_context *tdb);
  454. /* If it needs recovery, grab all the locks and do it. */
  455. enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb);
  456. /* transaction.c: */
  457. int tdb_transaction_recover(struct tdb_context *tdb);
  458. bool tdb_needs_recovery(struct tdb_context *tdb);
  459. /* tdb.c: */
  460. enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
  461. enum TDB_ERROR ecode,
  462. enum tdb_log_level level,
  463. const char *fmt, ...);
  464. #ifdef TDB_TRACE
  465. void tdb_trace(struct tdb_context *tdb, const char *op);
  466. void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op);
  467. void tdb_trace_open(struct tdb_context *tdb, const char *op,
  468. unsigned hash_size, unsigned tdb_flags, unsigned open_flags);
  469. void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret);
  470. void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret);
  471. void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
  472. TDB_DATA rec);
  473. void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
  474. TDB_DATA rec, int ret);
  475. void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
  476. TDB_DATA rec, TDB_DATA ret);
  477. void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
  478. TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
  479. int ret);
  480. void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
  481. TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret);
  482. #else
  483. #define tdb_trace(tdb, op)
  484. #define tdb_trace_seqnum(tdb, seqnum, op)
  485. #define tdb_trace_open(tdb, op, hash_size, tdb_flags, open_flags)
  486. #define tdb_trace_ret(tdb, op, ret)
  487. #define tdb_trace_retrec(tdb, op, ret)
  488. #define tdb_trace_1rec(tdb, op, rec)
  489. #define tdb_trace_1rec_ret(tdb, op, rec, ret)
  490. #define tdb_trace_1rec_retrec(tdb, op, rec, ret)
  491. #define tdb_trace_2rec_flag_ret(tdb, op, rec1, rec2, flag, ret)
  492. #define tdb_trace_2rec_retrec(tdb, op, rec1, rec2, ret)
  493. #endif /* !TDB_TRACE */
  494. #endif