private.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. #ifndef NTDB_PRIVATE_H
  2. #define NTDB_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. #ifndef HAVE_CCAN
  19. #error You need ccan to build ntdb!
  20. #endif
  21. #include "ntdb.h"
  22. #include <ccan/compiler/compiler.h>
  23. #include <ccan/likely/likely.h>
  24. #include <ccan/endian/endian.h>
  25. #ifdef HAVE_LIBREPLACE
  26. #include "replace.h"
  27. #include "system/filesys.h"
  28. #include "system/time.h"
  29. #include "system/shmem.h"
  30. #include "system/select.h"
  31. #include "system/wait.h"
  32. #else
  33. #include <stdarg.h>
  34. #include <stdint.h>
  35. #include <stdbool.h>
  36. #include <stdlib.h>
  37. #include <stddef.h>
  38. #include <sys/time.h>
  39. #include <sys/mman.h>
  40. #include <unistd.h>
  41. #include <fcntl.h>
  42. #include <errno.h>
  43. #include <stdio.h>
  44. #include <utime.h>
  45. #include <unistd.h>
  46. #include <ctype.h>
  47. #include <string.h>
  48. #include <sys/wait.h>
  49. #include <time.h>
  50. #endif
  51. #include <assert.h>
  52. #ifndef TEST_IT
  53. #define TEST_IT(cond)
  54. #endif
  55. /* #define NTDB_TRACE 1 */
  56. #ifndef __STRING
  57. #define __STRING(x) #x
  58. #endif
  59. #ifndef __STRINGSTRING
  60. #define __STRINGSTRING(x) __STRING(x)
  61. #endif
  62. #ifndef __location__
  63. #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
  64. #endif
  65. typedef uint64_t ntdb_len_t;
  66. typedef uint64_t ntdb_off_t;
  67. #define NTDB_MAGIC_FOOD "NTDB file\n"
  68. #define NTDB_VERSION ((uint64_t)(0x26011967 + 7))
  69. #define NTDB_USED_MAGIC ((uint64_t)0x1999)
  70. #define NTDB_HTABLE_MAGIC ((uint64_t)0x1888)
  71. #define NTDB_CHAIN_MAGIC ((uint64_t)0x1777)
  72. #define NTDB_FTABLE_MAGIC ((uint64_t)0x1666)
  73. #define NTDB_CAP_MAGIC ((uint64_t)0x1555)
  74. #define NTDB_FREE_MAGIC ((uint64_t)0xFE)
  75. #define NTDB_HASH_MAGIC (0xA1ABE11A01092008ULL)
  76. #define NTDB_RECOVERY_MAGIC (0xf53bc0e7ad124589ULL)
  77. #define NTDB_RECOVERY_INVALID_MAGIC (0x0ULL)
  78. /* Capability bits. */
  79. #define NTDB_CAP_TYPE_MASK 0x1FFFFFFFFFFFFFFFULL
  80. #define NTDB_CAP_NOCHECK 0x8000000000000000ULL
  81. #define NTDB_CAP_NOWRITE 0x4000000000000000ULL
  82. #define NTDB_CAP_NOOPEN 0x2000000000000000ULL
  83. #define NTDB_OFF_IS_ERR(off) unlikely(off >= (ntdb_off_t)(long)NTDB_ERR_LAST)
  84. #define NTDB_OFF_TO_ERR(off) ((enum NTDB_ERROR)(long)(off))
  85. #define NTDB_ERR_TO_OFF(ecode) ((ntdb_off_t)(long)(ecode))
  86. /* Packing errors into pointers and v.v. */
  87. #define NTDB_PTR_IS_ERR(ptr) \
  88. unlikely((unsigned long)(ptr) >= (unsigned long)NTDB_ERR_LAST)
  89. #define NTDB_PTR_ERR(p) ((enum NTDB_ERROR)(long)(p))
  90. #define NTDB_ERR_PTR(err) ((void *)(long)(err))
  91. /* This doesn't really need to be pagesize, but we use it for similar
  92. * reasons. */
  93. #define NTDB_PGSIZE 16384
  94. /* Common case of returning true, false or -ve error. */
  95. typedef int ntdb_bool_err;
  96. /* Prevent others from opening the file. */
  97. #define NTDB_OPEN_LOCK 0
  98. /* Expanding file. */
  99. #define NTDB_EXPANSION_LOCK 2
  100. /* Doing a transaction. */
  101. #define NTDB_TRANSACTION_LOCK 8
  102. /* Hash chain locks. */
  103. #define NTDB_HASH_LOCK_START 64
  104. /* Extend file by least 100 times larger than needed. */
  105. #define NTDB_EXTENSION_FACTOR 100
  106. /* We steal this many upper bits, giving a maximum offset of 64 exabytes. */
  107. #define NTDB_OFF_UPPER_STEAL 8
  108. /* And we use the lower bit, too. */
  109. #define NTDB_OFF_CHAIN_BIT 0
  110. /* Hash table sits just after the header. */
  111. #define NTDB_HASH_OFFSET (sizeof(struct ntdb_header))
  112. /* Additional features we understand. Currently: none. */
  113. #define NTDB_FEATURE_MASK ((uint64_t)0)
  114. /* The bit number where we store the extra hash bits. */
  115. /* Convenience mask to get actual offset. */
  116. #define NTDB_OFF_MASK \
  117. (((1ULL << (64 - NTDB_OFF_UPPER_STEAL)) - 1) - (1<<NTDB_OFF_CHAIN_BIT))
  118. /* How many buckets in a free list: see size_to_bucket(). */
  119. #define NTDB_FREE_BUCKETS (64 - NTDB_OFF_UPPER_STEAL)
  120. /* We have to be able to fit a free record here. */
  121. #define NTDB_MIN_DATA_LEN \
  122. (sizeof(struct ntdb_free_record) - sizeof(struct ntdb_used_record))
  123. /* Indicates this entry is not on an flist (can happen during coalescing) */
  124. #define NTDB_FTABLE_NONE ((1ULL << NTDB_OFF_UPPER_STEAL) - 1)
  125. /* By default, hash is 64k bytes */
  126. #define NTDB_DEFAULT_HBITS 13
  127. struct ntdb_used_record {
  128. /* For on-disk compatibility, we avoid bitfields:
  129. magic: 16, (highest)
  130. key_len_bits: 5,
  131. extra_padding: 32
  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 ntdb_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 ntdb_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 ntdb_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 ntdb_used_record *r)
  150. {
  151. return (r->magic_and_meta >> 11) & 0xFFFFFFFF;
  152. }
  153. static inline uint16_t rec_magic(const struct ntdb_used_record *r)
  154. {
  155. return (r->magic_and_meta >> 48);
  156. }
  157. struct ntdb_free_record {
  158. uint64_t magic_and_prev; /* NTDB_OFF_UPPER_STEAL bits magic, then prev */
  159. uint64_t ftable_and_len; /* Len not counting these two fields. */
  160. /* This is why the minimum record size is 8 bytes. */
  161. uint64_t next;
  162. };
  163. static inline uint64_t frec_prev(const struct ntdb_free_record *f)
  164. {
  165. return f->magic_and_prev & ((1ULL << (64 - NTDB_OFF_UPPER_STEAL)) - 1);
  166. }
  167. static inline uint64_t frec_magic(const struct ntdb_free_record *f)
  168. {
  169. return f->magic_and_prev >> (64 - NTDB_OFF_UPPER_STEAL);
  170. }
  171. static inline uint64_t frec_len(const struct ntdb_free_record *f)
  172. {
  173. return f->ftable_and_len & ((1ULL << (64 - NTDB_OFF_UPPER_STEAL))-1);
  174. }
  175. static inline unsigned frec_ftable(const struct ntdb_free_record *f)
  176. {
  177. return f->ftable_and_len >> (64 - NTDB_OFF_UPPER_STEAL);
  178. }
  179. struct ntdb_recovery_record {
  180. uint64_t magic;
  181. /* Length of record (add this header to get total length). */
  182. uint64_t max_len;
  183. /* Length used. */
  184. uint64_t len;
  185. /* Old length of file before transaction. */
  186. uint64_t eof;
  187. };
  188. /* this is stored at the front of every database */
  189. struct ntdb_header {
  190. char magic_food[64]; /* for /etc/magic */
  191. /* FIXME: Make me 32 bit? */
  192. uint64_t version; /* version of the code */
  193. uint64_t hash_bits; /* bits for toplevel hash table. */
  194. uint64_t hash_test; /* result of hashing HASH_MAGIC. */
  195. uint64_t hash_seed; /* "random" seed written at creation time. */
  196. ntdb_off_t free_table; /* (First) free table. */
  197. ntdb_off_t recovery; /* Transaction recovery area. */
  198. uint64_t features_used; /* Features all writers understand */
  199. uint64_t features_offered; /* Features offered */
  200. uint64_t seqnum; /* Sequence number for NTDB_SEQNUM */
  201. ntdb_off_t capabilities; /* Optional linked list of capabilities. */
  202. ntdb_off_t reserved[22];
  203. /*
  204. * Hash table is next:
  205. *
  206. * struct ntdb_used_record htable_hdr;
  207. * ntdb_off_t htable[1 << hash_bits];
  208. */
  209. };
  210. struct ntdb_freetable {
  211. struct ntdb_used_record hdr;
  212. ntdb_off_t next;
  213. ntdb_off_t buckets[NTDB_FREE_BUCKETS];
  214. };
  215. struct ntdb_capability {
  216. struct ntdb_used_record hdr;
  217. ntdb_off_t type;
  218. ntdb_off_t next;
  219. /* ... */
  220. };
  221. /* Information about a particular (locked) hash entry. */
  222. struct hash_info {
  223. /* Full hash value of entry. */
  224. uint32_t h;
  225. /* Start of hash table / chain. */
  226. ntdb_off_t table;
  227. /* Number of entries in this table/chain. */
  228. ntdb_off_t table_size;
  229. /* Bucket we (or an empty space) were found in. */
  230. ntdb_off_t bucket;
  231. /* Old value that was in that entry (if not found) */
  232. ntdb_off_t old_val;
  233. };
  234. enum ntdb_lock_flags {
  235. /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
  236. NTDB_LOCK_NOWAIT = 0,
  237. NTDB_LOCK_WAIT = 1,
  238. /* If set, don't log an error on failure. */
  239. NTDB_LOCK_PROBE = 2,
  240. /* If set, don't check for recovery (used by recovery code). */
  241. NTDB_LOCK_NOCHECK = 4,
  242. };
  243. struct ntdb_lock {
  244. struct ntdb_context *owner;
  245. off_t off;
  246. uint32_t count;
  247. uint32_t ltype;
  248. };
  249. /* This is only needed for ntdb_access_commit, but used everywhere to
  250. * simplify. */
  251. struct ntdb_access_hdr {
  252. struct ntdb_access_hdr *next;
  253. ntdb_off_t off;
  254. ntdb_len_t len;
  255. bool convert;
  256. };
  257. /* mmaps we are keeping around because they are still direct accessed */
  258. struct ntdb_old_mmap {
  259. struct ntdb_old_mmap *next;
  260. void *map_ptr;
  261. ntdb_len_t map_size;
  262. };
  263. struct ntdb_file {
  264. /* How many are sharing us? */
  265. unsigned int refcnt;
  266. /* Mmap (if any), or malloc (for NTDB_INTERNAL). */
  267. void *map_ptr;
  268. /* How much space has been mapped (<= current file size) */
  269. ntdb_len_t map_size;
  270. /* The file descriptor (-1 for NTDB_INTERNAL). */
  271. int fd;
  272. /* How many are accessing directly? */
  273. unsigned int direct_count;
  274. /* Old maps, still direct accessed. */
  275. struct ntdb_old_mmap *old_mmaps;
  276. /* Lock information */
  277. pid_t locker;
  278. struct ntdb_lock allrecord_lock;
  279. size_t num_lockrecs;
  280. struct ntdb_lock *lockrecs;
  281. /* Identity of this file. */
  282. dev_t device;
  283. ino_t inode;
  284. };
  285. struct ntdb_methods {
  286. enum NTDB_ERROR (*tread)(struct ntdb_context *, ntdb_off_t, void *,
  287. ntdb_len_t);
  288. enum NTDB_ERROR (*twrite)(struct ntdb_context *, ntdb_off_t, const void *,
  289. ntdb_len_t);
  290. enum NTDB_ERROR (*oob)(struct ntdb_context *, ntdb_off_t, ntdb_len_t, bool);
  291. enum NTDB_ERROR (*expand_file)(struct ntdb_context *, ntdb_len_t);
  292. void *(*direct)(struct ntdb_context *, ntdb_off_t, size_t, bool);
  293. ntdb_off_t (*read_off)(struct ntdb_context *ntdb, ntdb_off_t off);
  294. enum NTDB_ERROR (*write_off)(struct ntdb_context *ntdb, ntdb_off_t off,
  295. ntdb_off_t val);
  296. };
  297. /*
  298. internal prototypes
  299. */
  300. /* Get bits from a value. */
  301. static inline uint32_t bits_from(uint64_t val, unsigned start, unsigned num)
  302. {
  303. assert(num <= 32);
  304. return (val >> start) & ((1U << num) - 1);
  305. }
  306. /* hash.c: */
  307. uint32_t ntdb_jenkins_hash(const void *key, size_t length, uint32_t seed,
  308. void *unused);
  309. enum NTDB_ERROR first_in_hash(struct ntdb_context *ntdb,
  310. struct hash_info *h,
  311. NTDB_DATA *kbuf, size_t *dlen);
  312. enum NTDB_ERROR next_in_hash(struct ntdb_context *ntdb,
  313. struct hash_info *h,
  314. NTDB_DATA *kbuf, size_t *dlen);
  315. /* Hash random memory. */
  316. uint32_t ntdb_hash(struct ntdb_context *ntdb, const void *ptr, size_t len);
  317. /* Find and lock a hash entry (or where it would be). */
  318. ntdb_off_t find_and_lock(struct ntdb_context *ntdb,
  319. NTDB_DATA key,
  320. int ltype,
  321. struct hash_info *h,
  322. struct ntdb_used_record *rec,
  323. const char **rkey);
  324. enum NTDB_ERROR replace_in_hash(struct ntdb_context *ntdb,
  325. const struct hash_info *h,
  326. ntdb_off_t new_off);
  327. enum NTDB_ERROR add_to_hash(struct ntdb_context *ntdb,
  328. const struct hash_info *h,
  329. ntdb_off_t new_off);
  330. enum NTDB_ERROR delete_from_hash(struct ntdb_context *ntdb,
  331. const struct hash_info *h);
  332. /* For ntdb_check */
  333. bool is_subhash(ntdb_off_t val);
  334. enum NTDB_ERROR unknown_capability(struct ntdb_context *ntdb, const char *caller,
  335. ntdb_off_t type);
  336. /* free.c: */
  337. enum NTDB_ERROR ntdb_ftable_init(struct ntdb_context *ntdb);
  338. /* check.c needs these to iterate through free lists. */
  339. ntdb_off_t first_ftable(struct ntdb_context *ntdb);
  340. ntdb_off_t next_ftable(struct ntdb_context *ntdb, ntdb_off_t ftable);
  341. /* This returns space or -ve error number. */
  342. ntdb_off_t alloc(struct ntdb_context *ntdb, size_t keylen, size_t datalen,
  343. unsigned magic, bool growing);
  344. /* Put this record in a free list. */
  345. enum NTDB_ERROR add_free_record(struct ntdb_context *ntdb,
  346. ntdb_off_t off, ntdb_len_t len_with_header,
  347. enum ntdb_lock_flags waitflag,
  348. bool coalesce_ok);
  349. /* Set up header for a used/ftable/htable/chain/capability record. */
  350. enum NTDB_ERROR set_header(struct ntdb_context *ntdb,
  351. struct ntdb_used_record *rec,
  352. unsigned magic, uint64_t keylen, uint64_t datalen,
  353. uint64_t actuallen);
  354. /* Used by ntdb_check to verify. */
  355. unsigned int size_to_bucket(ntdb_len_t data_len);
  356. ntdb_off_t bucket_off(ntdb_off_t ftable_off, unsigned bucket);
  357. /* Used by ntdb_summary */
  358. ntdb_off_t dead_space(struct ntdb_context *ntdb, ntdb_off_t off);
  359. /* Adjust expansion, used by create_recovery_area */
  360. ntdb_off_t ntdb_expand_adjust(ntdb_off_t map_size, ntdb_off_t size);
  361. /* io.c: */
  362. /* Initialize ntdb->methods. */
  363. void ntdb_io_init(struct ntdb_context *ntdb);
  364. /* Convert endian of the buffer if required. */
  365. void *ntdb_convert(const struct ntdb_context *ntdb, void *buf, ntdb_len_t size);
  366. /* Unmap and try to map the ntdb. */
  367. enum NTDB_ERROR ntdb_munmap(struct ntdb_context *ntdb);
  368. enum NTDB_ERROR ntdb_mmap(struct ntdb_context *ntdb);
  369. /* Either alloc a copy, or give direct access. Release frees or noop. */
  370. const void *ntdb_access_read(struct ntdb_context *ntdb,
  371. ntdb_off_t off, ntdb_len_t len, bool convert);
  372. void *ntdb_access_write(struct ntdb_context *ntdb,
  373. ntdb_off_t off, ntdb_len_t len, bool convert);
  374. /* Release result of ntdb_access_read/write. */
  375. void ntdb_access_release(struct ntdb_context *ntdb, const void *p);
  376. /* Commit result of ntdb_acces_write. */
  377. enum NTDB_ERROR ntdb_access_commit(struct ntdb_context *ntdb, void *p);
  378. /* Clear an ondisk area. */
  379. enum NTDB_ERROR zero_out(struct ntdb_context *ntdb, ntdb_off_t off, ntdb_len_t len);
  380. /* Return a non-zero offset between >= start < end in this array (or end). */
  381. ntdb_off_t ntdb_find_nonzero_off(struct ntdb_context *ntdb,
  382. ntdb_off_t base,
  383. uint64_t start,
  384. uint64_t end);
  385. /* Return a zero offset in this array, or num. */
  386. ntdb_off_t ntdb_find_zero_off(struct ntdb_context *ntdb, ntdb_off_t off,
  387. uint64_t num);
  388. /* Allocate and make a copy of some offset. */
  389. void *ntdb_alloc_read(struct ntdb_context *ntdb, ntdb_off_t offset, ntdb_len_t len);
  390. /* Writes a converted copy of a record. */
  391. enum NTDB_ERROR ntdb_write_convert(struct ntdb_context *ntdb, ntdb_off_t off,
  392. const void *rec, size_t len);
  393. /* Reads record and converts it */
  394. enum NTDB_ERROR ntdb_read_convert(struct ntdb_context *ntdb, ntdb_off_t off,
  395. void *rec, size_t len);
  396. /* Bump the seqnum (caller checks for ntdb->flags & NTDB_SEQNUM) */
  397. void ntdb_inc_seqnum(struct ntdb_context *ntdb);
  398. /* lock.c: */
  399. /* Print message because another ntdb owns a lock we want. */
  400. enum NTDB_ERROR owner_conflict(struct ntdb_context *ntdb, const char *call);
  401. /* If we fork, we no longer really own locks. */
  402. bool check_lock_pid(struct ntdb_context *ntdb, const char *call, bool log);
  403. /* Lock/unlock a hash bucket. */
  404. enum NTDB_ERROR ntdb_lock_hash(struct ntdb_context *ntdb,
  405. unsigned int hbucket,
  406. int ltype);
  407. enum NTDB_ERROR ntdb_unlock_hash(struct ntdb_context *ntdb,
  408. unsigned int hash, int ltype);
  409. /* For closing the file. */
  410. void ntdb_lock_cleanup(struct ntdb_context *ntdb);
  411. /* Lock/unlock a particular free bucket. */
  412. enum NTDB_ERROR ntdb_lock_free_bucket(struct ntdb_context *ntdb, ntdb_off_t b_off,
  413. enum ntdb_lock_flags waitflag);
  414. void ntdb_unlock_free_bucket(struct ntdb_context *ntdb, ntdb_off_t b_off);
  415. /* Serialize transaction start. */
  416. enum NTDB_ERROR ntdb_transaction_lock(struct ntdb_context *ntdb, int ltype);
  417. void ntdb_transaction_unlock(struct ntdb_context *ntdb, int ltype);
  418. /* Do we have any hash locks (ie. via ntdb_chainlock) ? */
  419. bool ntdb_has_hash_locks(struct ntdb_context *ntdb);
  420. /* Lock entire database. */
  421. enum NTDB_ERROR ntdb_allrecord_lock(struct ntdb_context *ntdb, int ltype,
  422. enum ntdb_lock_flags flags, bool upgradable);
  423. void ntdb_allrecord_unlock(struct ntdb_context *ntdb, int ltype);
  424. enum NTDB_ERROR ntdb_allrecord_upgrade(struct ntdb_context *ntdb, off_t start);
  425. /* Serialize db open. */
  426. enum NTDB_ERROR ntdb_lock_open(struct ntdb_context *ntdb,
  427. int ltype, enum ntdb_lock_flags flags);
  428. void ntdb_unlock_open(struct ntdb_context *ntdb, int ltype);
  429. bool ntdb_has_open_lock(struct ntdb_context *ntdb);
  430. /* Serialize db expand. */
  431. enum NTDB_ERROR ntdb_lock_expand(struct ntdb_context *ntdb, int ltype);
  432. void ntdb_unlock_expand(struct ntdb_context *ntdb, int ltype);
  433. bool ntdb_has_expansion_lock(struct ntdb_context *ntdb);
  434. /* If it needs recovery, grab all the locks and do it. */
  435. enum NTDB_ERROR ntdb_lock_and_recover(struct ntdb_context *ntdb);
  436. /* Default lock and unlock functions. */
  437. int ntdb_fcntl_lock(int fd, int rw, off_t off, off_t len, bool waitflag, void *);
  438. int ntdb_fcntl_unlock(int fd, int rw, off_t off, off_t len, void *);
  439. /* transaction.c: */
  440. enum NTDB_ERROR ntdb_transaction_recover(struct ntdb_context *ntdb);
  441. ntdb_bool_err ntdb_needs_recovery(struct ntdb_context *ntdb);
  442. struct ntdb_context {
  443. /* Single list of all TDBs, to detect multiple opens. */
  444. struct ntdb_context *next;
  445. /* Filename of the database. */
  446. const char *name;
  447. /* Logging function */
  448. void (*log_fn)(struct ntdb_context *ntdb,
  449. enum ntdb_log_level level,
  450. enum NTDB_ERROR ecode,
  451. const char *message,
  452. void *data);
  453. void *log_data;
  454. /* Open flags passed to ntdb_open. */
  455. int open_flags;
  456. /* low level (fnctl) lock functions. */
  457. int (*lock_fn)(int fd, int rw, off_t off, off_t len, bool w, void *);
  458. int (*unlock_fn)(int fd, int rw, off_t off, off_t len, void *);
  459. void *lock_data;
  460. /* the ntdb flags passed to ntdb_open. */
  461. uint32_t flags;
  462. /* Our statistics. */
  463. struct ntdb_attribute_stats stats;
  464. /* The actual file information */
  465. struct ntdb_file *file;
  466. /* Hash function. */
  467. uint32_t (*hash_fn)(const void *key, size_t len, uint32_t seed, void *);
  468. void *hash_data;
  469. uint32_t hash_seed;
  470. /* Bits in toplevel hash table. */
  471. unsigned int hash_bits;
  472. /* Allocate and free functions. */
  473. void *(*alloc_fn)(const void *owner, size_t len, void *priv_data);
  474. void *(*expand_fn)(void *old, size_t newlen, void *priv_data);
  475. void (*free_fn)(void *old, void *priv_data);
  476. void *alloc_data;
  477. /* Our open hook, if any. */
  478. enum NTDB_ERROR (*openhook)(int fd, void *data);
  479. void *openhook_data;
  480. /* Set if we are in a transaction. */
  481. struct ntdb_transaction *transaction;
  482. /* What free table are we using? */
  483. ntdb_off_t ftable_off;
  484. unsigned int ftable;
  485. /* IO methods: changes for transactions. */
  486. const struct ntdb_methods *io;
  487. /* Direct access information */
  488. struct ntdb_access_hdr *access;
  489. };
  490. /* ntdb.c: */
  491. enum NTDB_ERROR COLD PRINTF_FMT(4, 5)
  492. ntdb_logerr(struct ntdb_context *ntdb,
  493. enum NTDB_ERROR ecode,
  494. enum ntdb_log_level level,
  495. const char *fmt, ...);
  496. static inline enum NTDB_ERROR ntdb_oob(struct ntdb_context *ntdb,
  497. ntdb_off_t off, ntdb_len_t len,
  498. bool probe)
  499. {
  500. if (likely(off + len >= off)
  501. && likely(off + len <= ntdb->file->map_size)
  502. && likely(!probe)) {
  503. return NTDB_SUCCESS;
  504. }
  505. return ntdb->io->oob(ntdb, off, len, probe);
  506. }
  507. /* Convenience routine to get an offset. */
  508. static inline ntdb_off_t ntdb_read_off(struct ntdb_context *ntdb,
  509. ntdb_off_t off)
  510. {
  511. return ntdb->io->read_off(ntdb, off);
  512. }
  513. /* Write an offset at an offset. */
  514. static inline enum NTDB_ERROR ntdb_write_off(struct ntdb_context *ntdb,
  515. ntdb_off_t off,
  516. ntdb_off_t val)
  517. {
  518. return ntdb->io->write_off(ntdb, off, val);
  519. }
  520. #ifdef NTDB_TRACE
  521. void ntdb_trace(struct ntdb_context *ntdb, const char *op);
  522. void ntdb_trace_seqnum(struct ntdb_context *ntdb, uint32_t seqnum, const char *op);
  523. void ntdb_trace_open(struct ntdb_context *ntdb, const char *op,
  524. unsigned hash_size, unsigned ntdb_flags, unsigned open_flags);
  525. void ntdb_trace_ret(struct ntdb_context *ntdb, const char *op, int ret);
  526. void ntdb_trace_retrec(struct ntdb_context *ntdb, const char *op, NTDB_DATA ret);
  527. void ntdb_trace_1rec(struct ntdb_context *ntdb, const char *op,
  528. NTDB_DATA rec);
  529. void ntdb_trace_1rec_ret(struct ntdb_context *ntdb, const char *op,
  530. NTDB_DATA rec, int ret);
  531. void ntdb_trace_1rec_retrec(struct ntdb_context *ntdb, const char *op,
  532. NTDB_DATA rec, NTDB_DATA ret);
  533. void ntdb_trace_2rec_flag_ret(struct ntdb_context *ntdb, const char *op,
  534. NTDB_DATA rec1, NTDB_DATA rec2, unsigned flag,
  535. int ret);
  536. void ntdb_trace_2rec_retrec(struct ntdb_context *ntdb, const char *op,
  537. NTDB_DATA rec1, NTDB_DATA rec2, NTDB_DATA ret);
  538. #else
  539. #define ntdb_trace(ntdb, op)
  540. #define ntdb_trace_seqnum(ntdb, seqnum, op)
  541. #define ntdb_trace_open(ntdb, op, hash_size, ntdb_flags, open_flags)
  542. #define ntdb_trace_ret(ntdb, op, ret)
  543. #define ntdb_trace_retrec(ntdb, op, ret)
  544. #define ntdb_trace_1rec(ntdb, op, rec)
  545. #define ntdb_trace_1rec_ret(ntdb, op, rec, ret)
  546. #define ntdb_trace_1rec_retrec(ntdb, op, rec, ret)
  547. #define ntdb_trace_2rec_flag_ret(ntdb, op, rec1, rec2, flag, ret)
  548. #define ntdb_trace_2rec_retrec(ntdb, op, rec1, rec2, ret)
  549. #endif /* !NTDB_TRACE */
  550. #endif