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