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