tdb2: unify tdb1_wipe into tdb_wipe.
[ira/wip.git] / lib / tdb2 / private.h
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
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 "tdb2.h"
22 #include <ccan/likely/likely.h>
23 #include <ccan/endian/endian.h>
24
25 #ifdef _SAMBA_BUILD_
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 <stdint.h>
34 #include <stdbool.h>
35 #include <stdlib.h>
36 #include <stddef.h>
37 #include <sys/time.h>
38 #include <sys/mman.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <utime.h>
44 #include <unistd.h>
45 #endif
46
47 #ifndef TEST_IT
48 #define TEST_IT(cond)
49 #endif
50
51 /* #define TDB_TRACE 1 */
52
53 #ifndef __STRING
54 #define __STRING(x)    #x
55 #endif
56
57 #ifndef __STRINGSTRING
58 #define __STRINGSTRING(x) __STRING(x)
59 #endif
60
61 #ifndef __location__
62 #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
63 #endif
64
65 typedef uint64_t tdb_len_t;
66 typedef uint64_t tdb_off_t;
67
68 #define TDB_MAGIC_FOOD "TDB file\n"
69 #define TDB_VERSION ((uint64_t)(0x26011967 + 7))
70 #define TDB1_VERSION (0x26011967 + 6)
71 #define TDB_USED_MAGIC ((uint64_t)0x1999)
72 #define TDB_HTABLE_MAGIC ((uint64_t)0x1888)
73 #define TDB_CHAIN_MAGIC ((uint64_t)0x1777)
74 #define TDB_FTABLE_MAGIC ((uint64_t)0x1666)
75 #define TDB_FREE_MAGIC ((uint64_t)0xFE)
76 #define TDB_HASH_MAGIC (0xA1ABE11A01092008ULL)
77 #define TDB_RECOVERY_MAGIC (0xf53bc0e7ad124589ULL)
78 #define TDB_RECOVERY_INVALID_MAGIC (0x0ULL)
79
80 #define TDB_OFF_IS_ERR(off) unlikely(off >= (tdb_off_t)TDB_ERR_LAST)
81
82 /* Packing errors into pointers and v.v. */
83 #define TDB_PTR_IS_ERR(ptr) \
84         unlikely((unsigned long)(ptr) >= (unsigned long)TDB_ERR_LAST)
85 #define TDB_PTR_ERR(p) ((enum TDB_ERROR)(long)(p))
86 #define TDB_ERR_PTR(err) ((void *)(long)(err))
87
88 /* Common case of returning true, false or -ve error. */
89 typedef int tdb_bool_err;
90
91 /* Prevent others from opening the file. */
92 #define TDB_OPEN_LOCK 0
93 /* Expanding file. */
94 #define TDB_EXPANSION_LOCK 2
95 /* Doing a transaction. */
96 #define TDB_TRANSACTION_LOCK 8
97 /* Hash chain locks. */
98 #define TDB_HASH_LOCK_START 64
99
100 /* Range for hash locks. */
101 #define TDB_HASH_LOCK_RANGE_BITS 30
102 #define TDB_HASH_LOCK_RANGE (1 << TDB_HASH_LOCK_RANGE_BITS)
103
104 /* We have 1024 entries in the top level. */
105 #define TDB_TOPLEVEL_HASH_BITS 10
106 /* And 64 entries in each sub-level: thus 64 bits exactly after 9 levels. */
107 #define TDB_SUBLEVEL_HASH_BITS 6
108 /* And 8 entries in each group, ie 8 groups per sublevel. */
109 #define TDB_HASH_GROUP_BITS 3
110 /* This is currently 10: beyond this we chain. */
111 #define TDB_MAX_LEVELS (1+(64-TDB_TOPLEVEL_HASH_BITS) / TDB_SUBLEVEL_HASH_BITS)
112
113 /* Extend file by least 100 times larger than needed. */
114 #define TDB_EXTENSION_FACTOR 100
115
116 /* We steal bits from the offsets to store hash info. */
117 #define TDB_OFF_HASH_GROUP_MASK ((1ULL << TDB_HASH_GROUP_BITS) - 1)
118 /* We steal this many upper bits, giving a maximum offset of 64 exabytes. */
119 #define TDB_OFF_UPPER_STEAL 8
120 #define   TDB_OFF_UPPER_STEAL_EXTRA 7
121 /* The bit number where we store extra hash bits. */
122 #define TDB_OFF_HASH_EXTRA_BIT 57
123 #define TDB_OFF_UPPER_STEAL_SUBHASH_BIT 56
124
125 /* Additional features we understand.  Currently: none. */
126 #define TDB_FEATURE_MASK ((uint64_t)0)
127
128 /* The bit number where we store the extra hash bits. */
129 /* Convenience mask to get actual offset. */
130 #define TDB_OFF_MASK \
131         (((1ULL << (64 - TDB_OFF_UPPER_STEAL)) - 1) - TDB_OFF_HASH_GROUP_MASK)
132
133 /* How many buckets in a free list: see size_to_bucket(). */
134 #define TDB_FREE_BUCKETS (64 - TDB_OFF_UPPER_STEAL)
135
136 /* We have to be able to fit a free record here. */
137 #define TDB_MIN_DATA_LEN        \
138         (sizeof(struct tdb_free_record) - sizeof(struct tdb_used_record))
139
140 /* Indicates this entry is not on an flist (can happen during coalescing) */
141 #define TDB_FTABLE_NONE ((1ULL << TDB_OFF_UPPER_STEAL) - 1)
142
143 struct tdb_used_record {
144         /* For on-disk compatibility, we avoid bitfields:
145            magic: 16,        (highest)
146            key_len_bits: 5,
147            extra_padding: 32
148            hash_bits: 11
149         */
150         uint64_t magic_and_meta;
151         /* The bottom key_len_bits*2 are key length, rest is data length. */
152         uint64_t key_and_data_len;
153 };
154
155 static inline unsigned rec_key_bits(const struct tdb_used_record *r)
156 {
157         return ((r->magic_and_meta >> 43) & ((1 << 5)-1)) * 2;
158 }
159
160 static inline uint64_t rec_key_length(const struct tdb_used_record *r)
161 {
162         return r->key_and_data_len & ((1ULL << rec_key_bits(r)) - 1);
163 }
164
165 static inline uint64_t rec_data_length(const struct tdb_used_record *r)
166 {
167         return r->key_and_data_len >> rec_key_bits(r);
168 }
169
170 static inline uint64_t rec_extra_padding(const struct tdb_used_record *r)
171 {
172         return (r->magic_and_meta >> 11) & 0xFFFFFFFF;
173 }
174
175 static inline uint32_t rec_hash(const struct tdb_used_record *r)
176 {
177         return r->magic_and_meta & ((1 << 11) - 1);
178 }
179
180 static inline uint16_t rec_magic(const struct tdb_used_record *r)
181 {
182         return (r->magic_and_meta >> 48);
183 }
184
185 struct tdb_free_record {
186         uint64_t magic_and_prev; /* TDB_OFF_UPPER_STEAL bits magic, then prev */
187         uint64_t ftable_and_len; /* Len not counting these two fields. */
188         /* This is why the minimum record size is 8 bytes.  */
189         uint64_t next;
190 };
191
192 static inline uint64_t frec_prev(const struct tdb_free_record *f)
193 {
194         return f->magic_and_prev & ((1ULL << (64 - TDB_OFF_UPPER_STEAL)) - 1);
195 }
196
197 static inline uint64_t frec_magic(const struct tdb_free_record *f)
198 {
199         return f->magic_and_prev >> (64 - TDB_OFF_UPPER_STEAL);
200 }
201
202 static inline uint64_t frec_len(const struct tdb_free_record *f)
203 {
204         return f->ftable_and_len & ((1ULL << (64 - TDB_OFF_UPPER_STEAL))-1);
205 }
206
207 static inline unsigned frec_ftable(const struct tdb_free_record *f)
208 {
209         return f->ftable_and_len >> (64 - TDB_OFF_UPPER_STEAL);
210 }
211
212 struct tdb_recovery_record {
213         uint64_t magic;
214         /* Length of record (add this header to get total length). */
215         uint64_t max_len;
216         /* Length used. */
217         uint64_t len;
218         /* Old length of file before transaction. */
219         uint64_t eof;
220 };
221
222 /* If we bottom out of the subhashes, we chain. */
223 struct tdb_chain {
224         tdb_off_t rec[1 << TDB_HASH_GROUP_BITS];
225         tdb_off_t next;
226 };
227
228 /* this is stored at the front of every database */
229 struct tdb_header {
230         char magic_food[64]; /* for /etc/magic */
231         /* FIXME: Make me 32 bit? */
232         uint64_t version; /* version of the code */
233         uint64_t hash_test; /* result of hashing HASH_MAGIC. */
234         uint64_t hash_seed; /* "random" seed written at creation time. */
235         tdb_off_t free_table; /* (First) free table. */
236         tdb_off_t recovery; /* Transaction recovery area. */
237
238         uint64_t features_used; /* Features all writers understand */
239         uint64_t features_offered; /* Features offered */
240
241         uint64_t seqnum; /* Sequence number for TDB_SEQNUM */
242
243         tdb_off_t reserved[23];
244
245         /* Top level hash table. */
246         tdb_off_t hashtable[1ULL << TDB_TOPLEVEL_HASH_BITS];
247 };
248
249 struct tdb_freetable {
250         struct tdb_used_record hdr;
251         tdb_off_t next;
252         tdb_off_t buckets[TDB_FREE_BUCKETS];
253 };
254
255 /* Information about a particular (locked) hash entry. */
256 struct hash_info {
257         /* Full hash value of entry. */
258         uint64_t h;
259         /* Start and length of lock acquired. */
260         tdb_off_t hlock_start;
261         tdb_len_t hlock_range;
262         /* Start of hash group. */
263         tdb_off_t group_start;
264         /* Bucket we belong in. */
265         unsigned int home_bucket;
266         /* Bucket we (or an empty space) were found in. */
267         unsigned int found_bucket;
268         /* How many bits of the hash are already used. */
269         unsigned int hash_used;
270         /* Current working group. */
271         tdb_off_t group[1 << TDB_HASH_GROUP_BITS];
272 };
273
274 struct traverse_info {
275         struct traverse_level {
276                 tdb_off_t hashtable;
277                 /* We ignore groups here, and treat it as a big array. */
278                 unsigned entry;
279                 unsigned int total_buckets;
280         } levels[TDB_MAX_LEVELS + 1];
281         unsigned int num_levels;
282         unsigned int toplevel_group;
283         /* This makes delete-everything-inside-traverse work as expected. */
284         tdb_off_t prev;
285 };
286
287 typedef uint32_t tdb1_len_t;
288 typedef uint32_t tdb1_off_t;
289
290 enum tdb_lock_flags {
291         /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
292         TDB_LOCK_NOWAIT = 0,
293         TDB_LOCK_WAIT = 1,
294         /* If set, don't log an error on failure. */
295         TDB_LOCK_PROBE = 2,
296         /* If set, don't check for recovery (used by recovery code). */
297         TDB_LOCK_NOCHECK = 4,
298 };
299
300 struct tdb_lock {
301         struct tdb_context *owner;
302         off_t off;
303         uint32_t count;
304         uint32_t ltype;
305 };
306
307 /* This is only needed for tdb_access_commit, but used everywhere to
308  * simplify. */
309 struct tdb_access_hdr {
310         struct tdb_access_hdr *next;
311         tdb_off_t off;
312         tdb_len_t len;
313         bool convert;
314 };
315
316 struct tdb_file {
317         /* How many are sharing us? */
318         unsigned int refcnt;
319
320         /* Mmap (if any), or malloc (for TDB_INTERNAL). */
321         void *map_ptr;
322
323         /* How much space has been mapped (<= current file size) */
324         tdb_len_t map_size;
325
326         /* The file descriptor (-1 for TDB_INTERNAL). */
327         int fd;
328
329         /* Lock information */
330         pid_t locker;
331         struct tdb_lock allrecord_lock;
332         size_t num_lockrecs;
333         struct tdb_lock *lockrecs;
334
335         /* Identity of this file. */
336         dev_t device;
337         ino_t inode;
338 };
339
340 struct tdb_methods {
341         enum TDB_ERROR (*tread)(struct tdb_context *, tdb_off_t, void *,
342                                 tdb_len_t);
343         enum TDB_ERROR (*twrite)(struct tdb_context *, tdb_off_t, const void *,
344                                  tdb_len_t);
345         enum TDB_ERROR (*oob)(struct tdb_context *, tdb_off_t, bool);
346         enum TDB_ERROR (*expand_file)(struct tdb_context *, tdb_len_t);
347         void *(*direct)(struct tdb_context *, tdb_off_t, size_t, bool);
348 };
349
350 /*
351   internal prototypes
352 */
353 /* hash.c: */
354 uint64_t tdb_jenkins_hash(const void *key, size_t length, uint64_t seed,
355                           void *unused);
356
357 tdb_bool_err first_in_hash(struct tdb_context *tdb,
358                            struct traverse_info *tinfo,
359                            TDB_DATA *kbuf, size_t *dlen);
360
361 tdb_bool_err next_in_hash(struct tdb_context *tdb,
362                           struct traverse_info *tinfo,
363                           TDB_DATA *kbuf, size_t *dlen);
364
365 /* Hash random memory. */
366 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len);
367
368 /* Hash on disk. */
369 uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off);
370
371 /* Find and lock a hash entry (or where it would be). */
372 tdb_off_t find_and_lock(struct tdb_context *tdb,
373                         struct tdb_data key,
374                         int ltype,
375                         struct hash_info *h,
376                         struct tdb_used_record *rec,
377                         struct traverse_info *tinfo);
378
379 enum TDB_ERROR replace_in_hash(struct tdb_context *tdb,
380                                struct hash_info *h,
381                                tdb_off_t new_off);
382
383 enum TDB_ERROR add_to_hash(struct tdb_context *tdb, struct hash_info *h,
384                            tdb_off_t new_off);
385
386 enum TDB_ERROR delete_from_hash(struct tdb_context *tdb, struct hash_info *h);
387
388 /* For tdb_check */
389 bool is_subhash(tdb_off_t val);
390
391 /* free.c: */
392 enum TDB_ERROR tdb_ftable_init(struct tdb_context *tdb);
393
394 /* check.c needs these to iterate through free lists. */
395 tdb_off_t first_ftable(struct tdb_context *tdb);
396 tdb_off_t next_ftable(struct tdb_context *tdb, tdb_off_t ftable);
397
398 /* This returns space or -ve error number. */
399 tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
400                 uint64_t hash, unsigned magic, bool growing);
401
402 /* Put this record in a free list. */
403 enum TDB_ERROR add_free_record(struct tdb_context *tdb,
404                                tdb_off_t off, tdb_len_t len_with_header,
405                                enum tdb_lock_flags waitflag,
406                                bool coalesce_ok);
407
408 /* Set up header for a used/ftable/htable/chain record. */
409 enum TDB_ERROR set_header(struct tdb_context *tdb,
410                           struct tdb_used_record *rec,
411                           unsigned magic, uint64_t keylen, uint64_t datalen,
412                           uint64_t actuallen, unsigned hashlow);
413
414 /* Used by tdb_check to verify. */
415 unsigned int size_to_bucket(tdb_len_t data_len);
416 tdb_off_t bucket_off(tdb_off_t ftable_off, unsigned bucket);
417
418 /* Used by tdb_summary */
419 tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off);
420
421 /* io.c: */
422 /* Initialize tdb->methods. */
423 void tdb_io_init(struct tdb_context *tdb);
424
425 /* Convert endian of the buffer if required. */
426 void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size);
427
428 /* Unmap and try to map the tdb. */
429 void tdb_munmap(struct tdb_file *file);
430 void tdb_mmap(struct tdb_context *tdb);
431
432 /* Either alloc a copy, or give direct access.  Release frees or noop. */
433 const void *tdb_access_read(struct tdb_context *tdb,
434                             tdb_off_t off, tdb_len_t len, bool convert);
435 void *tdb_access_write(struct tdb_context *tdb,
436                        tdb_off_t off, tdb_len_t len, bool convert);
437
438 /* Release result of tdb_access_read/write. */
439 void tdb_access_release(struct tdb_context *tdb, const void *p);
440 /* Commit result of tdb_acces_write. */
441 enum TDB_ERROR tdb_access_commit(struct tdb_context *tdb, void *p);
442
443 /* Convenience routine to get an offset. */
444 tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off);
445
446 /* Write an offset at an offset. */
447 enum TDB_ERROR tdb_write_off(struct tdb_context *tdb, tdb_off_t off,
448                              tdb_off_t val);
449
450 /* Clear an ondisk area. */
451 enum TDB_ERROR zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len);
452
453 /* Return a non-zero offset between >= start < end in this array (or end). */
454 tdb_off_t tdb_find_nonzero_off(struct tdb_context *tdb,
455                                tdb_off_t base,
456                                uint64_t start,
457                                uint64_t end);
458
459 /* Return a zero offset in this array, or num. */
460 tdb_off_t tdb_find_zero_off(struct tdb_context *tdb, tdb_off_t off,
461                             uint64_t num);
462
463 /* Allocate and make a copy of some offset. */
464 void *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
465
466 /* Writes a converted copy of a record. */
467 enum TDB_ERROR tdb_write_convert(struct tdb_context *tdb, tdb_off_t off,
468                                  const void *rec, size_t len);
469
470 /* Reads record and converts it */
471 enum TDB_ERROR tdb_read_convert(struct tdb_context *tdb, tdb_off_t off,
472                                 void *rec, size_t len);
473
474 /* Bump the seqnum (caller checks for tdb->flags & TDB_SEQNUM) */
475 void tdb_inc_seqnum(struct tdb_context *tdb);
476
477 /* lock.c: */
478 /* Lock/unlock a range of hashes. */
479 enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb,
480                                tdb_off_t hash_lock, tdb_len_t hash_range,
481                                int ltype, enum tdb_lock_flags waitflag);
482 enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
483                                  tdb_off_t hash_lock,
484                                  tdb_len_t hash_range, int ltype);
485
486 /* For closing the file. */
487 void tdb_lock_cleanup(struct tdb_context *tdb);
488
489 /* Lock/unlock a particular free bucket. */
490 enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
491                                     enum tdb_lock_flags waitflag);
492 void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off);
493
494 /* Serialize transaction start. */
495 enum TDB_ERROR tdb_transaction_lock(struct tdb_context *tdb, int ltype);
496 void tdb_transaction_unlock(struct tdb_context *tdb, int ltype);
497
498 /* Do we have any hash locks (ie. via tdb_chainlock) ? */
499 bool tdb_has_hash_locks(struct tdb_context *tdb);
500
501 /* Lock entire database. */
502 enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype,
503                                   enum tdb_lock_flags flags, bool upgradable);
504 void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype);
505 enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb, off_t start);
506
507 /* Serialize db open. */
508 enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb,
509                              int ltype, enum tdb_lock_flags flags);
510 void tdb_unlock_open(struct tdb_context *tdb, int ltype);
511 bool tdb_has_open_lock(struct tdb_context *tdb);
512
513 /* Serialize db expand. */
514 enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype);
515 void tdb_unlock_expand(struct tdb_context *tdb, int ltype);
516 bool tdb_has_expansion_lock(struct tdb_context *tdb);
517
518 /* If it needs recovery, grab all the locks and do it. */
519 enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb);
520
521 /* Byte-range lock wrappers for TDB1 to access. */
522 enum TDB_ERROR tdb_brlock(struct tdb_context *tdb,
523                           int rw_type, tdb_off_t offset, tdb_off_t len,
524                           enum tdb_lock_flags flags);
525
526 enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
527                             int rw_type, tdb_off_t offset, size_t len);
528
529 enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
530                              tdb_off_t offset, int ltype,
531                              enum tdb_lock_flags flags);
532
533 enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
534                                tdb_off_t off, int ltype);
535
536 enum TDB_ERROR tdb_lock_gradual(struct tdb_context *tdb,
537                                 int ltype, enum tdb_lock_flags flags,
538                                 tdb_off_t off, tdb_off_t len);
539
540 /* Default lock and unlock functions. */
541 int tdb_fcntl_lock(int fd, int rw, off_t off, off_t len, bool waitflag, void *);
542 int tdb_fcntl_unlock(int fd, int rw, off_t off, off_t len, void *);
543
544 /* transaction.c: */
545 enum TDB_ERROR tdb_transaction_recover(struct tdb_context *tdb);
546 tdb_bool_err tdb_needs_recovery(struct tdb_context *tdb);
547
548 /* this is stored at the front of every database */
549 struct tdb1_header {
550         char magic_food[32]; /* for /etc/magic */
551         uint32_t version; /* version of the code */
552         uint32_t hash_size; /* number of hash entries */
553         tdb1_off_t rwlocks; /* obsolete - kept to detect old formats */
554         tdb1_off_t recovery_start; /* offset of transaction recovery region */
555         tdb1_off_t sequence_number; /* used when TDB1_SEQNUM is set */
556         uint32_t magic1_hash; /* hash of TDB_MAGIC_FOOD. */
557         uint32_t magic2_hash; /* hash of TDB1_MAGIC. */
558         tdb1_off_t reserved[27];
559 };
560
561 struct tdb1_traverse_lock {
562         struct tdb1_traverse_lock *next;
563         uint32_t off;
564         uint32_t hash;
565         int lock_rw;
566 };
567
568 struct tdb_context {
569         /* Single list of all TDBs, to detect multiple opens. */
570         struct tdb_context *next;
571
572         /* Filename of the database. */
573         const char *name;
574
575         /* Logging function */
576         void (*log_fn)(struct tdb_context *tdb,
577                        enum tdb_log_level level,
578                        enum TDB_ERROR ecode,
579                        const char *message,
580                        void *data);
581         void *log_data;
582
583         /* Open flags passed to tdb_open. */
584         int open_flags;
585
586         /* low level (fnctl) lock functions. */
587         int (*lock_fn)(int fd, int rw, off_t off, off_t len, bool w, void *);
588         int (*unlock_fn)(int fd, int rw, off_t off, off_t len, void *);
589         void *lock_data;
590
591         /* the tdb flags passed to tdb_open. */
592         uint32_t flags;
593
594         /* Our statistics. */
595         struct tdb_attribute_stats stats;
596
597         /* The actual file information */
598         struct tdb_file *file;
599
600         /* Hash function. */
601         uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed, void *);
602         void *hash_data;
603         uint64_t hash_seed;
604
605         /* Our open hook, if any. */
606         enum TDB_ERROR (*openhook)(int fd, void *data);
607         void *openhook_data;
608
609         /* Last error we returned. */
610         enum TDB_ERROR last_error;
611
612         struct {
613
614                 /* Are we accessing directly? (debugging check). */
615                 int direct_access;
616
617                 /* Set if we are in a transaction. */
618                 struct tdb_transaction *transaction;
619
620                 /* What free table are we using? */
621                 tdb_off_t ftable_off;
622                 unsigned int ftable;
623
624                 /* IO methods: changes for transactions. */
625                 const struct tdb_methods *io;
626
627                 /* Direct access information */
628                 struct tdb_access_hdr *access;
629         } tdb2;
630
631         struct {
632                 int traverse_read; /* read-only traversal */
633                 int traverse_write; /* read-write traversal */
634
635                 struct tdb1_header header; /* a cached copy of the header */
636                 struct tdb1_traverse_lock travlocks; /* current traversal locks */
637                 const struct tdb1_methods *io;
638                 struct tdb1_transaction *transaction;
639                 int page_size;
640                 int max_dead_records;
641         } tdb1;
642 };
643
644 #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
645
646 /* tdb1_check.c: */
647 int tdb1_check(struct tdb_context *tdb,
648                enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data, void *),
649                void *private_data);
650
651
652 /* tdb1_open.c: */
653 int tdb1_new_database(struct tdb_context *tdb,
654                       struct tdb_attribute_tdb1_hashsize *hashsize);
655 enum TDB_ERROR tdb1_open(struct tdb_context *tdb);
656
657 /* tdb1_io.c: */
658 enum TDB_ERROR tdb1_probe_length(struct tdb_context *tdb);
659
660 /* tdb1_lock.c: */
661 int tdb1_allrecord_lock(struct tdb_context *tdb, int ltype,
662                         enum tdb_lock_flags flags, bool upgradable);
663 int tdb1_allrecord_unlock(struct tdb_context *tdb, int ltype);
664
665 int tdb1_chainlock(struct tdb_context *tdb, TDB_DATA key);
666 int tdb1_chainunlock(struct tdb_context *tdb, TDB_DATA key);
667 int tdb1_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
668 int tdb1_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
669
670 /* tdb1_transaction.c: */
671 int tdb1_transaction_recover(struct tdb_context *tdb);
672 int tdb1_transaction_cancel(struct tdb_context *tdb);
673
674 /* tdb1_traverse.c: */
675 int tdb1_traverse(struct tdb_context *tdb,
676                   int (*)(struct tdb_context *, TDB_DATA, TDB_DATA, void *),
677                   void *private_data);
678
679 /* tdb1_summary.c: */
680 char *tdb1_summary(struct tdb_context *tdb);
681
682 /* tdb1_tdb.c: */
683 int tdb1_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
684 enum TDB_ERROR tdb1_fetch(struct tdb_context *tdb, TDB_DATA key,
685                           TDB_DATA *data);
686 int tdb1_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
687 int tdb1_delete(struct tdb_context *tdb, TDB_DATA key);
688 int tdb1_exists(struct tdb_context *tdb, TDB_DATA key);
689 enum TDB_ERROR tdb1_parse_record(struct tdb_context *tdb, TDB_DATA key,
690                                  enum TDB_ERROR (*parser)(TDB_DATA key,
691                                                           TDB_DATA data,
692                                                           void *private_data),
693                                  void *private_data);
694 void tdb1_increment_seqnum_nonblock(struct tdb_context *tdb);
695 int tdb1_get_seqnum(struct tdb_context *tdb);
696 int tdb1_wipe_all(struct tdb_context *tdb);
697
698 /* tdb1_transaction.c: */
699 int tdb1_transaction_start(struct tdb_context *tdb);
700 int tdb1_transaction_prepare_commit(struct tdb_context *tdb);
701 int tdb1_transaction_commit(struct tdb_context *tdb);
702
703 /* tdb1_traverse.c: */
704 TDB_DATA tdb1_firstkey(struct tdb_context *tdb);
705 TDB_DATA tdb1_nextkey(struct tdb_context *tdb, TDB_DATA key);
706
707 /* tdb.c: */
708 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
709                                enum TDB_ERROR ecode,
710                                enum tdb_log_level level,
711                                const char *fmt, ...);
712
713 #ifdef TDB_TRACE
714 void tdb_trace(struct tdb_context *tdb, const char *op);
715 void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op);
716 void tdb_trace_open(struct tdb_context *tdb, const char *op,
717                     unsigned hash_size, unsigned tdb_flags, unsigned open_flags);
718 void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret);
719 void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret);
720 void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
721                     TDB_DATA rec);
722 void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
723                         TDB_DATA rec, int ret);
724 void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
725                            TDB_DATA rec, TDB_DATA ret);
726 void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
727                              TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
728                              int ret);
729 void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
730                            TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret);
731 #else
732 #define tdb_trace(tdb, op)
733 #define tdb_trace_seqnum(tdb, seqnum, op)
734 #define tdb_trace_open(tdb, op, hash_size, tdb_flags, open_flags)
735 #define tdb_trace_ret(tdb, op, ret)
736 #define tdb_trace_retrec(tdb, op, ret)
737 #define tdb_trace_1rec(tdb, op, rec)
738 #define tdb_trace_1rec_ret(tdb, op, rec, ret)
739 #define tdb_trace_1rec_retrec(tdb, op, rec, ret)
740 #define tdb_trace_2rec_flag_ret(tdb, op, rec1, rec2, flag, ret)
741 #define tdb_trace_2rec_retrec(tdb, op, rec1, rec2, ret)
742 #endif /* !TDB_TRACE */
743
744 #endif