private.h 19 KB

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