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