ntdb: optimize ntdb_fetch.
[kai/samba-autobuild/.git] / lib / ntdb / private.h
1 #ifndef NTDB_PRIVATE_H
2 #define NTDB_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 "config.h"
22 #ifndef HAVE_CCAN
23 #error You need ccan to build ntdb!
24 #endif
25 #include "ntdb.h"
26 #include <ccan/compiler/compiler.h>
27 #include <ccan/likely/likely.h>
28 #include <ccan/endian/endian.h>
29
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
51 #include <assert.h>
52
53 #ifndef TEST_IT
54 #define TEST_IT(cond)
55 #endif
56
57 /* #define NTDB_TRACE 1 */
58
59 #ifndef __STRING
60 #define __STRING(x)    #x
61 #endif
62
63 #ifndef __STRINGSTRING
64 #define __STRINGSTRING(x) __STRING(x)
65 #endif
66
67 #ifndef __location__
68 #define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
69 #endif
70
71 typedef uint64_t ntdb_len_t;
72 typedef uint64_t ntdb_off_t;
73
74 #define NTDB_MAGIC_FOOD "NTDB file\n"
75 #define NTDB_VERSION ((uint64_t)(0x26011967 + 7))
76 #define NTDB_USED_MAGIC ((uint64_t)0x1999)
77 #define NTDB_HTABLE_MAGIC ((uint64_t)0x1888)
78 #define NTDB_CHAIN_MAGIC ((uint64_t)0x1777)
79 #define NTDB_FTABLE_MAGIC ((uint64_t)0x1666)
80 #define NTDB_CAP_MAGIC ((uint64_t)0x1555)
81 #define NTDB_FREE_MAGIC ((uint64_t)0xFE)
82 #define NTDB_HASH_MAGIC (0xA1ABE11A01092008ULL)
83 #define NTDB_RECOVERY_MAGIC (0xf53bc0e7ad124589ULL)
84 #define NTDB_RECOVERY_INVALID_MAGIC (0x0ULL)
85
86 /* Capability bits. */
87 #define NTDB_CAP_TYPE_MASK      0x1FFFFFFFFFFFFFFFULL
88 #define NTDB_CAP_NOCHECK                0x8000000000000000ULL
89 #define NTDB_CAP_NOWRITE                0x4000000000000000ULL
90 #define NTDB_CAP_NOOPEN         0x2000000000000000ULL
91
92 #define NTDB_OFF_IS_ERR(off) unlikely(off >= (ntdb_off_t)(long)NTDB_ERR_LAST)
93 #define NTDB_OFF_TO_ERR(off) ((enum NTDB_ERROR)(long)(off))
94 #define NTDB_ERR_TO_OFF(ecode) ((ntdb_off_t)(long)(ecode))
95
96 /* Packing errors into pointers and v.v. */
97 #define NTDB_PTR_IS_ERR(ptr)                                            \
98         unlikely((unsigned long)(ptr) >= (unsigned long)NTDB_ERR_LAST)
99 #define NTDB_PTR_ERR(p) ((enum NTDB_ERROR)(long)(p))
100 #define NTDB_ERR_PTR(err) ((void *)(long)(err))
101
102 /* This doesn't really need to be pagesize, but we use it for similar
103  * reasons. */
104 #define NTDB_PGSIZE 16384
105
106 /* Common case of returning true, false or -ve error. */
107 typedef int ntdb_bool_err;
108
109 /* Prevent others from opening the file. */
110 #define NTDB_OPEN_LOCK 0
111 /* Expanding file. */
112 #define NTDB_EXPANSION_LOCK 2
113 /* Doing a transaction. */
114 #define NTDB_TRANSACTION_LOCK 8
115 /* Hash chain locks. */
116 #define NTDB_HASH_LOCK_START 64
117
118 /* Extend file by least 100 times larger than needed. */
119 #define NTDB_EXTENSION_FACTOR 100
120
121 /* We steal this many upper bits, giving a maximum offset of 64 exabytes. */
122 #define NTDB_OFF_UPPER_STEAL 8
123
124 /* And we use the lower bit, too. */
125 #define NTDB_OFF_CHAIN_BIT      0
126
127 /* Hash table sits just after the header. */
128 #define NTDB_HASH_OFFSET (sizeof(struct ntdb_header))
129
130 /* Additional features we understand.  Currently: none. */
131 #define NTDB_FEATURE_MASK ((uint64_t)0)
132
133 /* The bit number where we store the extra hash bits. */
134 /* Convenience mask to get actual offset. */
135 #define NTDB_OFF_MASK                                                   \
136         (((1ULL << (64 - NTDB_OFF_UPPER_STEAL)) - 1) - (1<<NTDB_OFF_CHAIN_BIT))
137
138 /* How many buckets in a free list: see size_to_bucket(). */
139 #define NTDB_FREE_BUCKETS (64 - NTDB_OFF_UPPER_STEAL)
140
141 /* We have to be able to fit a free record here. */
142 #define NTDB_MIN_DATA_LEN                                               \
143         (sizeof(struct ntdb_free_record) - sizeof(struct ntdb_used_record))
144
145 /* Indicates this entry is not on an flist (can happen during coalescing) */
146 #define NTDB_FTABLE_NONE ((1ULL << NTDB_OFF_UPPER_STEAL) - 1)
147
148 /* By default, hash is 64k bytes */
149 #define NTDB_DEFAULT_HBITS 13
150
151 struct ntdb_used_record {
152         /* For on-disk compatibility, we avoid bitfields:
153            magic: 16,        (highest)
154            key_len_bits: 5,
155            extra_padding: 32
156         */
157         uint64_t magic_and_meta;
158         /* The bottom key_len_bits*2 are key length, rest is data length. */
159         uint64_t key_and_data_len;
160 };
161
162 static inline unsigned rec_key_bits(const struct ntdb_used_record *r)
163 {
164         return ((r->magic_and_meta >> 43) & ((1 << 5)-1)) * 2;
165 }
166
167 static inline uint64_t rec_key_length(const struct ntdb_used_record *r)
168 {
169         return r->key_and_data_len & ((1ULL << rec_key_bits(r)) - 1);
170 }
171
172 static inline uint64_t rec_data_length(const struct ntdb_used_record *r)
173 {
174         return r->key_and_data_len >> rec_key_bits(r);
175 }
176
177 static inline uint64_t rec_extra_padding(const struct ntdb_used_record *r)
178 {
179         return (r->magic_and_meta >> 11) & 0xFFFFFFFF;
180 }
181
182 static inline uint16_t rec_magic(const struct ntdb_used_record *r)
183 {
184         return (r->magic_and_meta >> 48);
185 }
186
187 struct ntdb_free_record {
188         uint64_t magic_and_prev; /* NTDB_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;
192 };
193
194 static inline uint64_t frec_prev(const struct ntdb_free_record *f)
195 {
196         return f->magic_and_prev & ((1ULL << (64 - NTDB_OFF_UPPER_STEAL)) - 1);
197 }
198
199 static inline uint64_t frec_magic(const struct ntdb_free_record *f)
200 {
201         return f->magic_and_prev >> (64 - NTDB_OFF_UPPER_STEAL);
202 }
203
204 static inline uint64_t frec_len(const struct ntdb_free_record *f)
205 {
206         return f->ftable_and_len & ((1ULL << (64 - NTDB_OFF_UPPER_STEAL))-1);
207 }
208
209 static inline unsigned frec_ftable(const struct ntdb_free_record *f)
210 {
211         return f->ftable_and_len >> (64 - NTDB_OFF_UPPER_STEAL);
212 }
213
214 struct ntdb_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;
222 };
223
224 /* this is stored at the front of every database */
225 struct ntdb_header {
226         char magic_food[64]; /* for /etc/magic */
227         /* FIXME: Make me 32 bit? */
228         uint64_t version; /* version of the code */
229         uint64_t hash_bits; /* bits for toplevel hash table. */
230         uint64_t hash_test; /* result of hashing HASH_MAGIC. */
231         uint64_t hash_seed; /* "random" seed written at creation time. */
232         ntdb_off_t free_table; /* (First) free table. */
233         ntdb_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 NTDB_SEQNUM */
239
240         ntdb_off_t capabilities; /* Optional linked list of capabilities. */
241         ntdb_off_t reserved[22];
242
243         /*
244          * Hash table is next:
245          *
246          * struct ntdb_used_record htable_hdr;
247          * ntdb_off_t htable[1 << hash_bits];
248          */
249 };
250
251 struct ntdb_freetable {
252         struct ntdb_used_record hdr;
253         ntdb_off_t next;
254         ntdb_off_t buckets[NTDB_FREE_BUCKETS];
255 };
256
257 struct ntdb_capability {
258         struct ntdb_used_record hdr;
259         ntdb_off_t type;
260         ntdb_off_t next;
261         /* ... */
262 };
263
264 /* Information about a particular (locked) hash entry. */
265 struct hash_info {
266         /* Full hash value of entry. */
267         uint32_t h;
268         /* Start of hash table / chain. */
269         ntdb_off_t table;
270         /* Number of entries in this table/chain. */
271         ntdb_off_t table_size;
272         /* Bucket we (or an empty space) were found in. */
273         ntdb_off_t bucket;
274         /* Old value that was in that entry (if not found) */
275         ntdb_off_t old_val;
276 };
277
278 enum ntdb_lock_flags {
279         /* WAIT == F_SETLKW, NOWAIT == F_SETLK */
280         NTDB_LOCK_NOWAIT = 0,
281         NTDB_LOCK_WAIT = 1,
282         /* If set, don't log an error on failure. */
283         NTDB_LOCK_PROBE = 2,
284         /* If set, don't check for recovery (used by recovery code). */
285         NTDB_LOCK_NOCHECK = 4,
286 };
287
288 struct ntdb_lock {
289         struct ntdb_context *owner;
290         off_t off;
291         uint32_t count;
292         uint32_t ltype;
293 };
294
295 /* This is only needed for ntdb_access_commit, but used everywhere to
296  * simplify. */
297 struct ntdb_access_hdr {
298         struct ntdb_access_hdr *next;
299         ntdb_off_t off;
300         ntdb_len_t len;
301         bool convert;
302 };
303
304 struct ntdb_file {
305         /* How many are sharing us? */
306         unsigned int refcnt;
307
308         /* Mmap (if any), or malloc (for NTDB_INTERNAL). */
309         void *map_ptr;
310
311         /* How much space has been mapped (<= current file size) */
312         ntdb_len_t map_size;
313
314         /* The file descriptor (-1 for NTDB_INTERNAL). */
315         int fd;
316
317         /* Lock information */
318         pid_t locker;
319         struct ntdb_lock allrecord_lock;
320         size_t num_lockrecs;
321         struct ntdb_lock *lockrecs;
322
323         /* Identity of this file. */
324         dev_t device;
325         ino_t inode;
326 };
327
328 struct ntdb_methods {
329         enum NTDB_ERROR (*tread)(struct ntdb_context *, ntdb_off_t, void *,
330                                  ntdb_len_t);
331         enum NTDB_ERROR (*twrite)(struct ntdb_context *, ntdb_off_t, const void *,
332                                   ntdb_len_t);
333         enum NTDB_ERROR (*oob)(struct ntdb_context *, ntdb_off_t, ntdb_len_t, bool);
334         enum NTDB_ERROR (*expand_file)(struct ntdb_context *, ntdb_len_t);
335         void *(*direct)(struct ntdb_context *, ntdb_off_t, size_t, bool);
336         ntdb_off_t (*read_off)(struct ntdb_context *ntdb, ntdb_off_t off);
337         enum NTDB_ERROR (*write_off)(struct ntdb_context *ntdb, ntdb_off_t off,
338                                      ntdb_off_t val);
339 };
340
341 /*
342   internal prototypes
343 */
344 /* Get bits from a value. */
345 static inline uint32_t bits_from(uint64_t val, unsigned start, unsigned num)
346 {
347         assert(num <= 32);
348         return (val >> start) & ((1U << num) - 1);
349 }
350
351
352 /* hash.c: */
353 uint32_t ntdb_jenkins_hash(const void *key, size_t length, uint32_t seed,
354                            void *unused);
355
356 enum NTDB_ERROR first_in_hash(struct ntdb_context *ntdb,
357                               struct hash_info *h,
358                               NTDB_DATA *kbuf, size_t *dlen);
359
360 enum NTDB_ERROR next_in_hash(struct ntdb_context *ntdb,
361                              struct hash_info *h,
362                              NTDB_DATA *kbuf, size_t *dlen);
363
364 /* Hash random memory. */
365 uint32_t ntdb_hash(struct ntdb_context *ntdb, const void *ptr, size_t len);
366
367 /* Find and lock a hash entry (or where it would be). */
368 ntdb_off_t find_and_lock(struct ntdb_context *ntdb,
369                          NTDB_DATA key,
370                          int ltype,
371                          struct hash_info *h,
372                          struct ntdb_used_record *rec,
373                          const char **rkey);
374
375 enum NTDB_ERROR replace_in_hash(struct ntdb_context *ntdb,
376                                 const struct hash_info *h,
377                                 ntdb_off_t new_off);
378
379 enum NTDB_ERROR add_to_hash(struct ntdb_context *ntdb,
380                             const struct hash_info *h,
381                             ntdb_off_t new_off);
382
383 enum NTDB_ERROR delete_from_hash(struct ntdb_context *ntdb,
384                                  const struct hash_info *h);
385
386 /* For ntdb_check */
387 bool is_subhash(ntdb_off_t val);
388 enum NTDB_ERROR unknown_capability(struct ntdb_context *ntdb, const char *caller,
389                                    ntdb_off_t type);
390
391 /* free.c: */
392 enum NTDB_ERROR ntdb_ftable_init(struct ntdb_context *ntdb);
393
394 /* check.c needs these to iterate through free lists. */
395 ntdb_off_t first_ftable(struct ntdb_context *ntdb);
396 ntdb_off_t next_ftable(struct ntdb_context *ntdb, ntdb_off_t ftable);
397
398 /* This returns space or -ve error number. */
399 ntdb_off_t alloc(struct ntdb_context *ntdb, size_t keylen, size_t datalen,
400                  unsigned magic, bool growing);
401
402 /* Put this record in a free list. */
403 enum NTDB_ERROR add_free_record(struct ntdb_context *ntdb,
404                                 ntdb_off_t off, ntdb_len_t len_with_header,
405                                 enum ntdb_lock_flags waitflag,
406                                 bool coalesce_ok);
407
408 /* Set up header for a used/ftable/htable/chain/capability record. */
409 enum NTDB_ERROR set_header(struct ntdb_context *ntdb,
410                            struct ntdb_used_record *rec,
411                            unsigned magic, uint64_t keylen, uint64_t datalen,
412                            uint64_t actuallen);
413
414 /* Used by ntdb_check to verify. */
415 unsigned int size_to_bucket(ntdb_len_t data_len);
416 ntdb_off_t bucket_off(ntdb_off_t ftable_off, unsigned bucket);
417
418 /* Used by ntdb_summary */
419 ntdb_off_t dead_space(struct ntdb_context *ntdb, ntdb_off_t off);
420
421 /* Adjust expansion, used by create_recovery_area */
422 ntdb_off_t ntdb_expand_adjust(ntdb_off_t map_size, ntdb_off_t size);
423
424 /* io.c: */
425 /* Initialize ntdb->methods. */
426 void ntdb_io_init(struct ntdb_context *ntdb);
427
428 /* Convert endian of the buffer if required. */
429 void *ntdb_convert(const struct ntdb_context *ntdb, void *buf, ntdb_len_t size);
430
431 /* Unmap and try to map the ntdb. */
432 void ntdb_munmap(struct ntdb_file *file);
433 enum NTDB_ERROR ntdb_mmap(struct ntdb_context *ntdb);
434
435 /* Either alloc a copy, or give direct access.  Release frees or noop. */
436 const void *ntdb_access_read(struct ntdb_context *ntdb,
437                              ntdb_off_t off, ntdb_len_t len, bool convert);
438 void *ntdb_access_write(struct ntdb_context *ntdb,
439                         ntdb_off_t off, ntdb_len_t len, bool convert);
440
441 /* Release result of ntdb_access_read/write. */
442 void ntdb_access_release(struct ntdb_context *ntdb, const void *p);
443 /* Commit result of ntdb_acces_write. */
444 enum NTDB_ERROR ntdb_access_commit(struct ntdb_context *ntdb, void *p);
445
446 /* Clear an ondisk area. */
447 enum NTDB_ERROR zero_out(struct ntdb_context *ntdb, ntdb_off_t off, ntdb_len_t len);
448
449 /* Return a non-zero offset between >= start < end in this array (or end). */
450 ntdb_off_t ntdb_find_nonzero_off(struct ntdb_context *ntdb,
451                                  ntdb_off_t base,
452                                  uint64_t start,
453                                  uint64_t end);
454
455 /* Return a zero offset in this array, or num. */
456 ntdb_off_t ntdb_find_zero_off(struct ntdb_context *ntdb, ntdb_off_t off,
457                               uint64_t num);
458
459 /* Allocate and make a copy of some offset. */
460 void *ntdb_alloc_read(struct ntdb_context *ntdb, ntdb_off_t offset, ntdb_len_t len);
461
462 /* Writes a converted copy of a record. */
463 enum NTDB_ERROR ntdb_write_convert(struct ntdb_context *ntdb, ntdb_off_t off,
464                                    const void *rec, size_t len);
465
466 /* Reads record and converts it */
467 enum NTDB_ERROR ntdb_read_convert(struct ntdb_context *ntdb, ntdb_off_t off,
468                                   void *rec, size_t len);
469
470 /* Bump the seqnum (caller checks for ntdb->flags & NTDB_SEQNUM) */
471 void ntdb_inc_seqnum(struct ntdb_context *ntdb);
472
473 /* lock.c: */
474 /* Print message because another ntdb owns a lock we want. */
475 enum NTDB_ERROR owner_conflict(struct ntdb_context *ntdb, const char *call);
476
477 /* If we fork, we no longer really own locks. */
478 bool check_lock_pid(struct ntdb_context *ntdb, const char *call, bool log);
479
480 /* Lock/unlock a hash bucket. */
481 enum NTDB_ERROR ntdb_lock_hash(struct ntdb_context *ntdb,
482                                unsigned int hbucket,
483                                int ltype);
484 enum NTDB_ERROR ntdb_unlock_hash(struct ntdb_context *ntdb,
485                                  unsigned int hash, int ltype);
486
487 /* For closing the file. */
488 void ntdb_lock_cleanup(struct ntdb_context *ntdb);
489
490 /* Lock/unlock a particular free bucket. */
491 enum NTDB_ERROR ntdb_lock_free_bucket(struct ntdb_context *ntdb, ntdb_off_t b_off,
492                                       enum ntdb_lock_flags waitflag);
493 void ntdb_unlock_free_bucket(struct ntdb_context *ntdb, ntdb_off_t b_off);
494
495 /* Serialize transaction start. */
496 enum NTDB_ERROR ntdb_transaction_lock(struct ntdb_context *ntdb, int ltype);
497 void ntdb_transaction_unlock(struct ntdb_context *ntdb, int ltype);
498
499 /* Do we have any hash locks (ie. via ntdb_chainlock) ? */
500 bool ntdb_has_hash_locks(struct ntdb_context *ntdb);
501
502 /* Lock entire database. */
503 enum NTDB_ERROR ntdb_allrecord_lock(struct ntdb_context *ntdb, int ltype,
504                                     enum ntdb_lock_flags flags, bool upgradable);
505 void ntdb_allrecord_unlock(struct ntdb_context *ntdb, int ltype);
506 enum NTDB_ERROR ntdb_allrecord_upgrade(struct ntdb_context *ntdb, off_t start);
507
508 /* Serialize db open. */
509 enum NTDB_ERROR ntdb_lock_open(struct ntdb_context *ntdb,
510                                int ltype, enum ntdb_lock_flags flags);
511 void ntdb_unlock_open(struct ntdb_context *ntdb, int ltype);
512 bool ntdb_has_open_lock(struct ntdb_context *ntdb);
513
514 /* Serialize db expand. */
515 enum NTDB_ERROR ntdb_lock_expand(struct ntdb_context *ntdb, int ltype);
516 void ntdb_unlock_expand(struct ntdb_context *ntdb, int ltype);
517 bool ntdb_has_expansion_lock(struct ntdb_context *ntdb);
518
519 /* If it needs recovery, grab all the locks and do it. */
520 enum NTDB_ERROR ntdb_lock_and_recover(struct ntdb_context *ntdb);
521
522 /* Default lock and unlock functions. */
523 int ntdb_fcntl_lock(int fd, int rw, off_t off, off_t len, bool waitflag, void *);
524 int ntdb_fcntl_unlock(int fd, int rw, off_t off, off_t len, void *);
525
526 /* transaction.c: */
527 enum NTDB_ERROR ntdb_transaction_recover(struct ntdb_context *ntdb);
528 ntdb_bool_err ntdb_needs_recovery(struct ntdb_context *ntdb);
529
530 struct ntdb_context {
531         /* Single list of all TDBs, to detect multiple opens. */
532         struct ntdb_context *next;
533
534         /* Filename of the database. */
535         const char *name;
536
537         /* Logging function */
538         void (*log_fn)(struct ntdb_context *ntdb,
539                        enum ntdb_log_level level,
540                        enum NTDB_ERROR ecode,
541                        const char *message,
542                        void *data);
543         void *log_data;
544
545         /* Open flags passed to ntdb_open. */
546         int open_flags;
547
548         /* low level (fnctl) lock functions. */
549         int (*lock_fn)(int fd, int rw, off_t off, off_t len, bool w, void *);
550         int (*unlock_fn)(int fd, int rw, off_t off, off_t len, void *);
551         void *lock_data;
552
553         /* the ntdb flags passed to ntdb_open. */
554         uint32_t flags;
555
556         /* Our statistics. */
557         struct ntdb_attribute_stats stats;
558
559         /* The actual file information */
560         struct ntdb_file *file;
561
562         /* Hash function. */
563         uint32_t (*hash_fn)(const void *key, size_t len, uint32_t seed, void *);
564         void *hash_data;
565         uint32_t hash_seed;
566         /* Bits in toplevel hash table. */
567         unsigned int hash_bits;
568
569         /* Allocate and free functions. */
570         void *(*alloc_fn)(const void *owner, size_t len, void *priv_data);
571         void *(*expand_fn)(void *old, size_t newlen, void *priv_data);
572         void (*free_fn)(void *old, void *priv_data);
573         void *alloc_data;
574
575         /* Our open hook, if any. */
576         enum NTDB_ERROR (*openhook)(int fd, void *data);
577         void *openhook_data;
578
579         /* Are we accessing directly? (debugging check). */
580         int direct_access;
581
582         /* Set if we are in a transaction. */
583         struct ntdb_transaction *transaction;
584
585         /* What free table are we using? */
586         ntdb_off_t ftable_off;
587         unsigned int ftable;
588
589         /* IO methods: changes for transactions. */
590         const struct ntdb_methods *io;
591
592         /* Direct access information */
593         struct ntdb_access_hdr *access;
594 };
595
596 /* ntdb.c: */
597 enum NTDB_ERROR COLD PRINTF_FMT(4, 5)
598         ntdb_logerr(struct ntdb_context *ntdb,
599                     enum NTDB_ERROR ecode,
600                     enum ntdb_log_level level,
601                     const char *fmt, ...);
602
603 static inline enum NTDB_ERROR ntdb_oob(struct ntdb_context *ntdb,
604                                        ntdb_off_t off, ntdb_len_t len,
605                                        bool probe)
606 {
607         if (likely(off + len >= off)
608             && likely(off + len <= ntdb->file->map_size)
609             && likely(!probe)) {
610                     return NTDB_SUCCESS;
611         }
612         return ntdb->io->oob(ntdb, off, len, probe);
613 }
614
615 /* Convenience routine to get an offset. */
616 static inline ntdb_off_t ntdb_read_off(struct ntdb_context *ntdb,
617                                        ntdb_off_t off)
618 {
619         return ntdb->io->read_off(ntdb, off);
620 }
621
622 /* Write an offset at an offset. */
623 static inline enum NTDB_ERROR ntdb_write_off(struct ntdb_context *ntdb,
624                                              ntdb_off_t off,
625                                ntdb_off_t val)
626 {
627         return ntdb->io->write_off(ntdb, off, val);
628 }
629
630 #ifdef NTDB_TRACE
631 void ntdb_trace(struct ntdb_context *ntdb, const char *op);
632 void ntdb_trace_seqnum(struct ntdb_context *ntdb, uint32_t seqnum, const char *op);
633 void ntdb_trace_open(struct ntdb_context *ntdb, const char *op,
634                      unsigned hash_size, unsigned ntdb_flags, unsigned open_flags);
635 void ntdb_trace_ret(struct ntdb_context *ntdb, const char *op, int ret);
636 void ntdb_trace_retrec(struct ntdb_context *ntdb, const char *op, NTDB_DATA ret);
637 void ntdb_trace_1rec(struct ntdb_context *ntdb, const char *op,
638                      NTDB_DATA rec);
639 void ntdb_trace_1rec_ret(struct ntdb_context *ntdb, const char *op,
640                          NTDB_DATA rec, int ret);
641 void ntdb_trace_1rec_retrec(struct ntdb_context *ntdb, const char *op,
642                             NTDB_DATA rec, NTDB_DATA ret);
643 void ntdb_trace_2rec_flag_ret(struct ntdb_context *ntdb, const char *op,
644                               NTDB_DATA rec1, NTDB_DATA rec2, unsigned flag,
645                               int ret);
646 void ntdb_trace_2rec_retrec(struct ntdb_context *ntdb, const char *op,
647                             NTDB_DATA rec1, NTDB_DATA rec2, NTDB_DATA ret);
648 #else
649 #define ntdb_trace(ntdb, op)
650 #define ntdb_trace_seqnum(ntdb, seqnum, op)
651 #define ntdb_trace_open(ntdb, op, hash_size, ntdb_flags, open_flags)
652 #define ntdb_trace_ret(ntdb, op, ret)
653 #define ntdb_trace_retrec(ntdb, op, ret)
654 #define ntdb_trace_1rec(ntdb, op, rec)
655 #define ntdb_trace_1rec_ret(ntdb, op, rec, ret)
656 #define ntdb_trace_1rec_retrec(ntdb, op, rec, ret)
657 #define ntdb_trace_2rec_flag_ret(ntdb, op, rec1, rec2, flag, ret)
658 #define ntdb_trace_2rec_retrec(ntdb, op, rec1, rec2, ret)
659 #endif /* !NTDB_TRACE */
660
661 #endif