tdb2: unify tdb1_fetch into tdb_fetch
[Samba/gebeck_regimport.git] / lib / tdb2 / private.h
blob1c794753f932d0085dd915c7130d04284429ee69
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
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.
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.
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/>.
21 #include "tdb2.h"
22 #include <ccan/likely/likely.h>
23 #include <ccan/endian/endian.h>
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
47 #ifndef TEST_IT
48 #define TEST_IT(cond)
49 #endif
51 /* #define TDB_TRACE 1 */
53 #ifndef __STRING
54 #define __STRING(x) #x
55 #endif
57 #ifndef __STRINGSTRING
58 #define __STRINGSTRING(x) __STRING(x)
59 #endif
61 #ifndef __location__
62 #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
63 #endif
65 typedef uint64_t tdb_len_t;
66 typedef uint64_t tdb_off_t;
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)
80 #define TDB_OFF_IS_ERR(off) unlikely(off >= (tdb_off_t)TDB_ERR_LAST)
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))
88 /* Common case of returning true, false or -ve error. */
89 typedef int tdb_bool_err;
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
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)
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)
113 /* Extend file by least 100 times larger than needed. */
114 #define TDB_EXTENSION_FACTOR 100
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
125 /* Additional features we understand. Currently: none. */
126 #define TDB_FEATURE_MASK ((uint64_t)0)
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)
133 /* How many buckets in a free list: see size_to_bucket(). */
134 #define TDB_FREE_BUCKETS (64 - TDB_OFF_UPPER_STEAL)
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))
140 /* Indicates this entry is not on an flist (can happen during coalescing) */
141 #define TDB_FTABLE_NONE ((1ULL << TDB_OFF_UPPER_STEAL) - 1)
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
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;
155 static inline unsigned rec_key_bits(const struct tdb_used_record *r)
157 return ((r->magic_and_meta >> 43) & ((1 << 5)-1)) * 2;
160 static inline uint64_t rec_key_length(const struct tdb_used_record *r)
162 return r->key_and_data_len & ((1ULL << rec_key_bits(r)) - 1);
165 static inline uint64_t rec_data_length(const struct tdb_used_record *r)
167 return r->key_and_data_len >> rec_key_bits(r);
170 static inline uint64_t rec_extra_padding(const struct tdb_used_record *r)
172 return (r->magic_and_meta >> 11) & 0xFFFFFFFF;
175 static inline uint32_t rec_hash(const struct tdb_used_record *r)
177 return r->magic_and_meta & ((1 << 11) - 1);
180 static inline uint16_t rec_magic(const struct tdb_used_record *r)
182 return (r->magic_and_meta >> 48);
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;
192 static inline uint64_t frec_prev(const struct tdb_free_record *f)
194 return f->magic_and_prev & ((1ULL << (64 - TDB_OFF_UPPER_STEAL)) - 1);
197 static inline uint64_t frec_magic(const struct tdb_free_record *f)
199 return f->magic_and_prev >> (64 - TDB_OFF_UPPER_STEAL);
202 static inline uint64_t frec_len(const struct tdb_free_record *f)
204 return f->ftable_and_len & ((1ULL << (64 - TDB_OFF_UPPER_STEAL))-1);
207 static inline unsigned frec_ftable(const struct tdb_free_record *f)
209 return f->ftable_and_len >> (64 - TDB_OFF_UPPER_STEAL);
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;
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;
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. */
238 uint64_t features_used; /* Features all writers understand */
239 uint64_t features_offered; /* Features offered */
241 uint64_t seqnum; /* Sequence number for TDB_SEQNUM */
243 tdb_off_t reserved[23];
245 /* Top level hash table. */
246 tdb_off_t hashtable[1ULL << TDB_TOPLEVEL_HASH_BITS];
249 struct tdb_freetable {
250 struct tdb_used_record hdr;
251 tdb_off_t next;
252 tdb_off_t buckets[TDB_FREE_BUCKETS];
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];
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;
287 typedef uint32_t tdb1_len_t;
288 typedef uint32_t tdb1_off_t;
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,
300 struct tdb_lock {
301 struct tdb_context *owner;
302 off_t off;
303 uint32_t count;
304 uint32_t ltype;
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;
316 struct tdb_file {
317 /* How many are sharing us? */
318 unsigned int refcnt;
320 /* Mmap (if any), or malloc (for TDB_INTERNAL). */
321 void *map_ptr;
323 /* How much space has been mapped (<= current file size) */
324 tdb_len_t map_size;
326 /* The file descriptor (-1 for TDB_INTERNAL). */
327 int fd;
329 /* Lock information */
330 pid_t locker;
331 struct tdb_lock allrecord_lock;
332 size_t num_lockrecs;
333 struct tdb_lock *lockrecs;
335 /* Identity of this file. */
336 dev_t device;
337 ino_t inode;
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);
351 internal prototypes
353 /* hash.c: */
354 uint64_t tdb_jenkins_hash(const void *key, size_t length, uint64_t seed,
355 void *unused);
357 tdb_bool_err first_in_hash(struct tdb_context *tdb,
358 struct traverse_info *tinfo,
359 TDB_DATA *kbuf, size_t *dlen);
361 tdb_bool_err next_in_hash(struct tdb_context *tdb,
362 struct traverse_info *tinfo,
363 TDB_DATA *kbuf, size_t *dlen);
365 /* Hash random memory. */
366 uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len);
368 /* Hash on disk. */
369 uint64_t hash_record(struct tdb_context *tdb, tdb_off_t off);
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);
379 enum TDB_ERROR replace_in_hash(struct tdb_context *tdb,
380 struct hash_info *h,
381 tdb_off_t new_off);
383 enum TDB_ERROR add_to_hash(struct tdb_context *tdb, struct hash_info *h,
384 tdb_off_t new_off);
386 enum TDB_ERROR delete_from_hash(struct tdb_context *tdb, struct hash_info *h);
388 /* For tdb_check */
389 bool is_subhash(tdb_off_t val);
391 /* free.c: */
392 enum TDB_ERROR tdb_ftable_init(struct tdb_context *tdb);
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);
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);
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);
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);
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);
418 /* Used by tdb_summary */
419 tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off);
421 /* io.c: */
422 /* Initialize tdb->methods. */
423 void tdb_io_init(struct tdb_context *tdb);
425 /* Convert endian of the buffer if required. */
426 void *tdb_convert(const struct tdb_context *tdb, void *buf, tdb_len_t size);
428 /* Unmap and try to map the tdb. */
429 void tdb_munmap(struct tdb_file *file);
430 void tdb_mmap(struct tdb_context *tdb);
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);
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);
443 /* Convenience routine to get an offset. */
444 tdb_off_t tdb_read_off(struct tdb_context *tdb, tdb_off_t off);
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);
450 /* Clear an ondisk area. */
451 enum TDB_ERROR zero_out(struct tdb_context *tdb, tdb_off_t off, tdb_len_t len);
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);
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);
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);
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);
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);
474 /* Bump the seqnum (caller checks for tdb->flags & TDB_SEQNUM) */
475 void tdb_inc_seqnum(struct tdb_context *tdb);
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);
486 /* For closing the file. */
487 void tdb_lock_cleanup(struct tdb_context *tdb);
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);
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);
498 /* Do we have any hash locks (ie. via tdb_chainlock) ? */
499 bool tdb_has_hash_locks(struct tdb_context *tdb);
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);
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);
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);
518 /* If it needs recovery, grab all the locks and do it. */
519 enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb);
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);
526 enum TDB_ERROR tdb_brunlock(struct tdb_context *tdb,
527 int rw_type, tdb_off_t offset, size_t len);
529 enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb,
530 tdb_off_t offset, int ltype,
531 enum tdb_lock_flags flags);
533 enum TDB_ERROR tdb_nest_unlock(struct tdb_context *tdb,
534 tdb_off_t off, int ltype);
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);
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 *);
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);
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];
561 struct tdb1_traverse_lock {
562 struct tdb1_traverse_lock *next;
563 uint32_t off;
564 uint32_t hash;
565 int lock_rw;
568 struct tdb_context {
569 /* Single list of all TDBs, to detect multiple opens. */
570 struct tdb_context *next;
572 /* Filename of the database. */
573 const char *name;
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;
583 /* Open flags passed to tdb_open. */
584 int open_flags;
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;
591 /* the tdb flags passed to tdb_open. */
592 uint32_t flags;
594 /* Our statistics. */
595 struct tdb_attribute_stats stats;
597 /* The actual file information */
598 struct tdb_file *file;
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;
605 /* Our open hook, if any. */
606 enum TDB_ERROR (*openhook)(int fd, void *data);
607 void *openhook_data;
609 /* Last error we returned. */
610 enum TDB_ERROR last_error;
612 struct {
614 /* Are we accessing directly? (debugging check). */
615 int direct_access;
617 /* Set if we are in a transaction. */
618 struct tdb_transaction *transaction;
620 /* What free table are we using? */
621 tdb_off_t ftable_off;
622 unsigned int ftable;
624 /* IO methods: changes for transactions. */
625 const struct tdb_methods *io;
627 /* Direct access information */
628 struct tdb_access_hdr *access;
629 } tdb2;
631 struct {
632 int traverse_read; /* read-only traversal */
633 int traverse_write; /* read-write traversal */
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;
644 #define TDB1_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
646 /* tdb1_open.c: */
647 int tdb1_new_database(struct tdb_context *tdb,
648 struct tdb_attribute_tdb1_hashsize *hashsize);
649 enum TDB_ERROR tdb1_open(struct tdb_context *tdb);
651 /* tdb1_io.c: */
652 enum TDB_ERROR tdb1_probe_length(struct tdb_context *tdb);
654 /* tdb1_lock.c: */
655 int tdb1_allrecord_unlock(struct tdb_context *tdb, int ltype);
657 /* tdb1_transaction.c: */
658 int tdb1_transaction_recover(struct tdb_context *tdb);
659 int tdb1_transaction_cancel(struct tdb_context *tdb);
661 /* tdb1_tdb.c: */
662 int tdb1_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
663 enum TDB_ERROR tdb1_fetch(struct tdb_context *tdb, TDB_DATA key,
664 TDB_DATA *data);
666 /* tdb.c: */
667 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
668 enum TDB_ERROR ecode,
669 enum tdb_log_level level,
670 const char *fmt, ...);
672 #ifdef TDB_TRACE
673 void tdb_trace(struct tdb_context *tdb, const char *op);
674 void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op);
675 void tdb_trace_open(struct tdb_context *tdb, const char *op,
676 unsigned hash_size, unsigned tdb_flags, unsigned open_flags);
677 void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret);
678 void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret);
679 void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
680 TDB_DATA rec);
681 void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
682 TDB_DATA rec, int ret);
683 void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
684 TDB_DATA rec, TDB_DATA ret);
685 void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
686 TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
687 int ret);
688 void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
689 TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret);
690 #else
691 #define tdb_trace(tdb, op)
692 #define tdb_trace_seqnum(tdb, seqnum, op)
693 #define tdb_trace_open(tdb, op, hash_size, tdb_flags, open_flags)
694 #define tdb_trace_ret(tdb, op, ret)
695 #define tdb_trace_retrec(tdb, op, ret)
696 #define tdb_trace_1rec(tdb, op, rec)
697 #define tdb_trace_1rec_ret(tdb, op, rec, ret)
698 #define tdb_trace_1rec_retrec(tdb, op, rec, ret)
699 #define tdb_trace_2rec_flag_ret(tdb, op, rec1, rec2, flag, ret)
700 #define tdb_trace_2rec_retrec(tdb, op, rec1, rec2, ret)
701 #endif /* !TDB_TRACE */
703 #endif