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