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