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