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