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