tdb2: use HAVE_LIBREPLACE instead of _SAMBA_BUILD_.
[kai/samba.git] / lib / tdb2 / tdb2.h
1 #ifndef CCAN_TDB2_H
2 #define CCAN_TDB2_H
3
4 /*
5    TDB version 2: trivial database library
6
7    Copyright (C) Andrew Tridgell 1999-2004
8    Copyright (C) Rusty Russell 2010-2011
9
10      ** NOTE! The following LGPL license applies to the tdb
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #ifdef  __cplusplus
29 extern "C" {
30 #endif
31
32 #include "config.h"
33 #ifdef HAVE_LIBREPLACE
34 #include "replace.h"
35 #else
36 #if HAVE_FILE_OFFSET_BITS
37 #define _FILE_OFFSET_BITS 64
38 #endif
39 /* For mode_t */
40 #include <sys/types.h>
41 /* For O_* flags. */
42 #include <sys/stat.h>
43 /* For sig_atomic_t. */
44 #include <signal.h>
45 /* For uint64_t */
46 #include <stdint.h>
47 /* For bool */
48 #include <stdbool.h>
49 /* For memcmp */
50 #include <string.h>
51 #endif
52 #include <ccan/compiler/compiler.h>
53 #include <ccan/typesafe_cb/typesafe_cb.h>
54 #include <ccan/cast/cast.h>
55
56 union tdb_attribute;
57 struct tdb_context;
58
59 /**
60  * tdb_open - open a database file
61  * @name: the file name (can be NULL if flags contains TDB_INTERNAL)
62  * @tdb_flags: options for this database
63  * @open_flags: flags argument for tdb's open() call.
64  * @mode: mode argument for tdb's open() call.
65  * @attributes: linked list of extra attributes for this tdb.
66  *
67  * This call opens (and potentially creates) a database file.
68  * Multiple processes can have the TDB file open at once.
69  *
70  * On failure it will return NULL, and set errno: it may also call
71  * any log attribute found in @attributes.
72  *
73  * See also:
74  *      union tdb_attribute
75  */
76 struct tdb_context *tdb_open(const char *name, int tdb_flags,
77                              int open_flags, mode_t mode,
78                              union tdb_attribute *attributes);
79
80
81 /* flags for tdb_open() */
82 #define TDB_DEFAULT 0 /* just a readability place holder */
83 #define TDB_INTERNAL 2 /* don't store on disk */
84 #define TDB_NOLOCK   4 /* don't do any locking */
85 #define TDB_NOMMAP   8 /* don't use mmap */
86 #define TDB_CONVERT 16 /* convert endian */
87 #define TDB_NOSYNC   64 /* don't use synchronous transactions */
88 #define TDB_SEQNUM   128 /* maintain a sequence number */
89 #define TDB_ALLOW_NESTING   256 /* fake nested transactions */
90 #define TDB_RDONLY   512 /* implied by O_RDONLY */
91 #define TDB_VERSION1  1024 /* create/open an old style TDB */
92
93 /**
94  * tdb1_incompatible_hash - better (Jenkins) hash for tdb1
95  *
96  * This is better than the default hash for tdb1; but older versions of the
97  * tdb library (prior to version 1.2.6) won't be able to open them.
98  *
99  * It only makes sense to specify this (using tdb_attribute_hash) when
100  * creating (with O_CREAT) an old tdb version using TDB_VERSION1.  It's
101  * equivalent to the TDB_INCOMPATIBLE_HASH flag for tdb1.
102  */
103 uint64_t tdb1_incompatible_hash(const void *, size_t, uint64_t, void *);
104
105 /**
106  * tdb_close - close and free a tdb.
107  * @tdb: the tdb context returned from tdb_open()
108  *
109  * This always succeeds, in that @tdb is unusable after this call.  But if
110  * some unexpected error occurred while closing, it will return non-zero
111  * (the only clue as to cause will be via the log attribute).
112  */
113 int tdb_close(struct tdb_context *tdb);
114
115 /**
116  * struct tdb_data - representation of keys or values.
117  * @dptr: the data pointer
118  * @dsize: the size of the data pointed to by dptr.
119  *
120  * This is the "blob" representation of keys and data used by TDB.
121  */
122 typedef struct tdb_data {
123         unsigned char *dptr;
124         size_t dsize;
125 } TDB_DATA;
126
127 /**
128  * enum TDB_ERROR - error returns for TDB
129  *
130  * See Also:
131  *      tdb_errorstr()
132  */
133 enum TDB_ERROR {
134         TDB_SUCCESS     = 0,    /* No error. */
135         TDB_ERR_CORRUPT = -1,   /* We read the db, and it was bogus. */
136         TDB_ERR_IO      = -2,   /* We couldn't read/write the db. */
137         TDB_ERR_LOCK    = -3,   /* Locking failed. */
138         TDB_ERR_OOM     = -4,   /* Out of Memory. */
139         TDB_ERR_EXISTS  = -5,   /* The key already exists. */
140         TDB_ERR_NOEXIST = -6,   /* The key does not exist. */
141         TDB_ERR_EINVAL  = -7,   /* You're using it wrong. */
142         TDB_ERR_RDONLY  = -8,   /* The database is read-only. */
143         TDB_ERR_LAST = TDB_ERR_RDONLY
144 };
145
146 /**
147  * tdb_store - store a key/value pair in a tdb.
148  * @tdb: the tdb context returned from tdb_open()
149  * @key: the key
150  * @dbuf: the data to associate with the key.
151  * @flag: TDB_REPLACE, TDB_INSERT or TDB_MODIFY.
152  *
153  * This inserts (or overwrites) a key/value pair in the TDB.  If flag
154  * is TDB_REPLACE, it doesn't matter whether the key exists or not;
155  * TDB_INSERT means it must not exist (returns TDB_ERR_EXISTS otherwise),
156  * and TDB_MODIFY means it must exist (returns TDB_ERR_NOEXIST otherwise).
157  *
158  * On success, this returns TDB_SUCCESS.
159  *
160  * See also:
161  *      tdb_fetch, tdb_transaction_start, tdb_append, tdb_delete.
162  */
163 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
164                          struct tdb_data key,
165                          struct tdb_data dbuf,
166                          int flag);
167
168 /* flags to tdb_store() */
169 #define TDB_REPLACE 1           /* A readability place holder */
170 #define TDB_INSERT 2            /* Don't overwrite an existing entry */
171 #define TDB_MODIFY 3            /* Don't create an existing entry    */
172
173 /**
174  * tdb_fetch - fetch a value from a tdb.
175  * @tdb: the tdb context returned from tdb_open()
176  * @key: the key
177  * @data: pointer to data.
178  *
179  * This looks up a key in the database and sets it in @data.
180  *
181  * If it returns TDB_SUCCESS, the key was found: it is your
182  * responsibility to call free() on @data->dptr.
183  *
184  * Otherwise, it returns an error (usually, TDB_ERR_NOEXIST) and @data is
185  * undefined.
186  */
187 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
188                          struct tdb_data *data);
189
190 /**
191  * tdb_errorstr - map the tdb error onto a constant readable string
192  * @ecode: the enum TDB_ERROR to map.
193  *
194  * This is useful for displaying errors to users.
195  */
196 const char *tdb_errorstr(enum TDB_ERROR ecode);
197
198 /**
199  * tdb_append - append a value to a key/value pair in a tdb.
200  * @tdb: the tdb context returned from tdb_open()
201  * @key: the key
202  * @dbuf: the data to append.
203  *
204  * This is equivalent to fetching a record, reallocating .dptr to add the
205  * data, and writing it back, only it's much more efficient.  If the key
206  * doesn't exist, it's equivalent to tdb_store (with an additional hint that
207  * you expect to expand the record in future).
208  *
209  * See Also:
210  *      tdb_fetch(), tdb_store()
211  */
212 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
213                           struct tdb_data key, struct tdb_data dbuf);
214
215 /**
216  * tdb_delete - delete a key from a tdb.
217  * @tdb: the tdb context returned from tdb_open()
218  * @key: the key to delete.
219  *
220  * Returns TDB_SUCCESS on success, or an error (usually TDB_ERR_NOEXIST).
221  *
222  * See Also:
223  *      tdb_fetch(), tdb_store()
224  */
225 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key);
226
227 /**
228  * tdb_exists - does a key exist in the database?
229  * @tdb: the tdb context returned from tdb_open()
230  * @key: the key to search for.
231  *
232  * Returns true if it exists, or false if it doesn't or any other error.
233  */
234 bool tdb_exists(struct tdb_context *tdb, TDB_DATA key);
235
236 /**
237  * tdb_deq - are struct tdb_data equal?
238  * @a: one struct tdb_data
239  * @b: another struct tdb_data
240  */
241 static inline bool tdb_deq(struct tdb_data a, struct tdb_data b)
242 {
243         return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
244 }
245
246 /**
247  * tdb_mkdata - make a struct tdb_data from const data
248  * @p: the constant pointer
249  * @len: the length
250  *
251  * As the dptr member of struct tdb_data is not constant, you need to
252  * cast it.  This function keeps thost casts in one place, as well as
253  * suppressing the warning some compilers give when casting away a
254  * qualifier (eg. gcc with -Wcast-qual)
255  */
256 static inline struct tdb_data tdb_mkdata(const void *p, size_t len)
257 {
258         struct tdb_data d;
259         d.dptr = cast_const(void *, p);
260         d.dsize = len;
261         return d;
262 }
263
264 /**
265  * tdb_transaction_start - start a transaction
266  * @tdb: the tdb context returned from tdb_open()
267  *
268  * This begins a series of atomic operations.  Other processes will be able
269  * to read the tdb, but not alter it (they will block), nor will they see
270  * any changes until tdb_transaction_commit() is called.
271  *
272  * Note that if the TDB_ALLOW_NESTING flag is set, a tdb_transaction_start()
273  * within a transaction will succeed, but it's not a real transaction:
274  * (1) An inner transaction which is committed is not actually committed until
275  *     the outer transaction is; if the outer transaction is cancelled, the
276  *     inner ones are discarded.
277  * (2) tdb_transaction_cancel() marks the outer transaction as having an error,
278  *     so the final tdb_transaction_commit() will fail.
279  * (3) the outer transaction will see the results of the inner transaction.
280  *
281  * See Also:
282  *      tdb_transaction_cancel, tdb_transaction_commit.
283  */
284 enum TDB_ERROR tdb_transaction_start(struct tdb_context *tdb);
285
286 /**
287  * tdb_transaction_cancel - abandon a transaction
288  * @tdb: the tdb context returned from tdb_open()
289  *
290  * This aborts a transaction, discarding any changes which were made.
291  * tdb_close() does this implicitly.
292  */
293 void tdb_transaction_cancel(struct tdb_context *tdb);
294
295 /**
296  * tdb_transaction_commit - commit a transaction
297  * @tdb: the tdb context returned from tdb_open()
298  *
299  * This completes a transaction, writing any changes which were made.
300  *
301  * fsync() is used to commit the transaction (unless TDB_NOSYNC is set),
302  * making it robust against machine crashes, but very slow compared to
303  * other TDB operations.
304  *
305  * A failure can only be caused by unexpected errors (eg. I/O or
306  * memory); this is no point looping on transaction failure.
307  *
308  * See Also:
309  *      tdb_transaction_prepare_commit()
310  */
311 enum TDB_ERROR tdb_transaction_commit(struct tdb_context *tdb);
312
313 /**
314  * tdb_transaction_prepare_commit - prepare to commit a transaction
315  * @tdb: the tdb context returned from tdb_open()
316  *
317  * This ensures we have the resources to commit a transaction (using
318  * tdb_transaction_commit): if this succeeds then a transaction will only
319  * fail if the write() or fsync() calls fail.
320  *
321  * If this fails you must still call tdb_transaction_cancel() to cancel
322  * the transaction.
323  *
324  * See Also:
325  *      tdb_transaction_commit()
326  */
327 enum TDB_ERROR tdb_transaction_prepare_commit(struct tdb_context *tdb);
328
329 /**
330  * tdb_traverse - traverse a TDB
331  * @tdb: the tdb context returned from tdb_open()
332  * @fn: the function to call for every key/value pair (or NULL)
333  * @p: the pointer to hand to @f
334  *
335  * This walks the TDB until all they keys have been traversed, or @fn
336  * returns non-zero.  If the traverse function or other processes are
337  * changing data or adding or deleting keys, the traverse may be
338  * unreliable: keys may be skipped or (rarely) visited twice.
339  *
340  * There is one specific exception: the special case of deleting the
341  * current key does not undermine the reliability of the traversal.
342  *
343  * On success, returns the number of keys iterated.  On error returns
344  * a negative enum TDB_ERROR value.
345  */
346 #define tdb_traverse(tdb, fn, p)                                        \
347         tdb_traverse_(tdb, typesafe_cb_preargs(int, void *, (fn), (p),  \
348                                                struct tdb_context *,    \
349                                                TDB_DATA, TDB_DATA), (p))
350
351 int64_t tdb_traverse_(struct tdb_context *tdb,
352                       int (*fn)(struct tdb_context *,
353                                 TDB_DATA, TDB_DATA, void *), void *p);
354
355 /**
356  * tdb_parse_record - operate directly on data in the database.
357  * @tdb: the tdb context returned from tdb_open()
358  * @key: the key whose record we should hand to @parse
359  * @parse: the function to call for the data
360  * @data: the private pointer to hand to @parse (types must match).
361  *
362  * This avoids a copy for many cases, by handing you a pointer into
363  * the memory-mapped database.  It also locks the record to prevent
364  * other accesses at the same time.
365  *
366  * Do not alter the data handed to parse()!
367  */
368 #define tdb_parse_record(tdb, key, parse, data)                         \
369         tdb_parse_record_((tdb), (key),                                 \
370                           typesafe_cb_preargs(enum TDB_ERROR, void *,   \
371                                               (parse), (data),          \
372                                               TDB_DATA, TDB_DATA), (data))
373
374 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
375                                  TDB_DATA key,
376                                  enum TDB_ERROR (*parse)(TDB_DATA k,
377                                                          TDB_DATA d,
378                                                          void *data),
379                                  void *data);
380
381 /**
382  * tdb_get_seqnum - get a database sequence number
383  * @tdb: the tdb context returned from tdb_open()
384  *
385  * This returns a sequence number: any change to the database from a
386  * tdb context opened with the TDB_SEQNUM flag will cause that number
387  * to increment.  Note that the incrementing is unreliable (it is done
388  * without locking), so this is only useful as an optimization.
389  *
390  * For example, you may have a regular database backup routine which
391  * does not operate if the sequence number is unchanged.  In the
392  * unlikely event of a failed increment, it will be backed up next
393  * time any way.
394  *
395  * Returns an enum TDB_ERROR (ie. negative) on error.
396  */
397 int64_t tdb_get_seqnum(struct tdb_context *tdb);
398
399 /**
400  * tdb_firstkey - get the "first" key in a TDB
401  * @tdb: the tdb context returned from tdb_open()
402  * @key: pointer to key.
403  *
404  * This returns an arbitrary key in the database; with tdb_nextkey() it allows
405  * open-coded traversal of the database, though it is slightly less efficient
406  * than tdb_traverse.
407  *
408  * It is your responsibility to free @key->dptr on success.
409  *
410  * Returns TDB_ERR_NOEXIST if the database is empty.
411  */
412 enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key);
413
414 /**
415  * tdb_nextkey - get the "next" key in a TDB
416  * @tdb: the tdb context returned from tdb_open()
417  * @key: a key returned by tdb_firstkey() or tdb_nextkey().
418  *
419  * This returns another key in the database; it will free @key.dptr for
420  * your convenience.
421  *
422  * Returns TDB_ERR_NOEXIST if there are no more keys.
423  */
424 enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key);
425
426 /**
427  * tdb_chainlock - lock a record in the TDB
428  * @tdb: the tdb context returned from tdb_open()
429  * @key: the key to lock.
430  *
431  * This prevents any access occurring to a group of keys including @key,
432  * even if @key does not exist.  This allows primitive atomic updates of
433  * records without using transactions.
434  *
435  * You cannot begin a transaction while holding a tdb_chainlock(), nor can
436  * you do any operations on any other keys in the database.  This also means
437  * that you cannot hold more than one tdb_chainlock() at a time.
438  *
439  * See Also:
440  *      tdb_chainunlock()
441  */
442 enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
443
444 /**
445  * tdb_chainunlock - unlock a record in the TDB
446  * @tdb: the tdb context returned from tdb_open()
447  * @key: the key to unlock.
448  *
449  * The key must have previously been locked by tdb_chainlock().
450  */
451 void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
452
453 /**
454  * tdb_chainlock_read - lock a record in the TDB, for reading
455  * @tdb: the tdb context returned from tdb_open()
456  * @key: the key to lock.
457  *
458  * This prevents any changes from occurring to a group of keys including @key,
459  * even if @key does not exist.  This allows primitive atomic updates of
460  * records without using transactions.
461  *
462  * You cannot begin a transaction while holding a tdb_chainlock_read(), nor can
463  * you do any operations on any other keys in the database.  This also means
464  * that you cannot hold more than one tdb_chainlock()/read() at a time.
465  *
466  * See Also:
467  *      tdb_chainlock()
468  */
469 enum TDB_ERROR tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
470
471 /**
472  * tdb_chainunlock_read - unlock a record in the TDB for reading
473  * @tdb: the tdb context returned from tdb_open()
474  * @key: the key to unlock.
475  *
476  * The key must have previously been locked by tdb_chainlock_read().
477  */
478 void tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
479
480 /**
481  * tdb_lockall - lock the entire TDB
482  * @tdb: the tdb context returned from tdb_open()
483  *
484  * You cannot hold a tdb_chainlock while calling this.  It nests, so you
485  * must call tdb_unlockall as many times as you call tdb_lockall.
486  */
487 enum TDB_ERROR tdb_lockall(struct tdb_context *tdb);
488
489 /**
490  * tdb_unlockall - unlock the entire TDB
491  * @tdb: the tdb context returned from tdb_open()
492  */
493 void tdb_unlockall(struct tdb_context *tdb);
494
495 /**
496  * tdb_lockall_read - lock the entire TDB for reading
497  * @tdb: the tdb context returned from tdb_open()
498  *
499  * This prevents others writing to the database, eg. tdb_delete, tdb_store,
500  * tdb_append, but not tdb_fetch.
501  *
502  * You cannot hold a tdb_chainlock while calling this.  It nests, so you
503  * must call tdb_unlockall_read as many times as you call tdb_lockall_read.
504  */
505 enum TDB_ERROR tdb_lockall_read(struct tdb_context *tdb);
506
507 /**
508  * tdb_unlockall_read - unlock the entire TDB for reading
509  * @tdb: the tdb context returned from tdb_open()
510  */
511 void tdb_unlockall_read(struct tdb_context *tdb);
512
513 /**
514  * tdb_wipe_all - wipe the database clean
515  * @tdb: the tdb context returned from tdb_open()
516  *
517  * Completely erase the database.  This is faster than iterating through
518  * each key and doing tdb_delete.
519  */
520 enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb);
521
522 /**
523  * tdb_repack - repack the database
524  * @tdb: the tdb context returned from tdb_open()
525  *
526  * This repacks the database; if it is suffering from a great deal of
527  * fragmentation this might help.  However, it can take twice the
528  * memory of the existing TDB.
529  */
530 enum TDB_ERROR tdb_repack(struct tdb_context *tdb);
531
532 /**
533  * tdb_check - check a TDB for consistency
534  * @tdb: the tdb context returned from tdb_open()
535  * @check: function to check each key/data pair (or NULL)
536  * @data: argument for @check, must match type.
537  *
538  * This performs a consistency check of the open database, optionally calling
539  * a check() function on each record so you can do your own data consistency
540  * checks as well.  If check() returns an error, that is returned from
541  * tdb_check().
542  *
543  * Returns TDB_SUCCESS or an error.
544  */
545 #define tdb_check(tdb, check, data)                                     \
546         tdb_check_((tdb), typesafe_cb_preargs(enum TDB_ERROR, void *,   \
547                                               (check), (data),          \
548                                               struct tdb_data,          \
549                                               struct tdb_data),         \
550                    (data))
551
552 enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
553                           enum TDB_ERROR (*check)(struct tdb_data k,
554                                                   struct tdb_data d,
555                                                   void *data),
556                           void *data);
557
558 /**
559  * tdb_error - get the last error (not threadsafe)
560  * @tdb: the tdb context returned from tdb_open()
561  *
562  * Returns the last error returned by a TDB function.
563  *
564  * This makes porting from TDB1 easier, but note that the last error is not
565  * reliable in threaded programs.
566  */
567 enum TDB_ERROR tdb_error(struct tdb_context *tdb);
568
569 /**
570  * enum tdb_summary_flags - flags for tdb_summary.
571  */
572 enum tdb_summary_flags {
573         TDB_SUMMARY_HISTOGRAMS = 1 /* Draw graphs in the summary. */
574 };
575
576 /**
577  * tdb_summary - return a string describing the TDB state
578  * @tdb: the tdb context returned from tdb_open()
579  * @flags: flags to control the summary output.
580  * @summary: pointer to string to allocate.
581  *
582  * This returns a developer-readable string describing the overall
583  * state of the tdb, such as the percentage used and sizes of records.
584  * It is designed to provide information about the tdb at a glance
585  * without displaying any keys or data in the database.
586  *
587  * On success, sets @summary to point to a malloc()'ed nul-terminated
588  * multi-line string.  It is your responsibility to free() it.
589  */
590 enum TDB_ERROR tdb_summary(struct tdb_context *tdb,
591                            enum tdb_summary_flags flags,
592                            char **summary);
593
594
595 /**
596  * tdb_get_flags - return the flags for a tdb
597  * @tdb: the tdb context returned from tdb_open()
598  *
599  * This returns the flags on the current tdb.  Some of these are caused by
600  * the flags argument to tdb_open(), others (such as TDB_CONVERT) are
601  * intuited.
602  */
603 unsigned int tdb_get_flags(struct tdb_context *tdb);
604
605 /**
606  * tdb_add_flag - set a flag for a tdb
607  * @tdb: the tdb context returned from tdb_open()
608  * @flag: one of TDB_NOLOCK, TDB_NOMMAP, TDB_NOSYNC or TDB_ALLOW_NESTING.
609  *
610  * You can use this to set a flag on the TDB.  You cannot set these flags
611  * on a TDB_INTERNAL tdb.
612  */
613 void tdb_add_flag(struct tdb_context *tdb, unsigned flag);
614
615 /**
616  * tdb_remove_flag - unset a flag for a tdb
617  * @tdb: the tdb context returned from tdb_open()
618  * @flag: one of TDB_NOLOCK, TDB_NOMMAP, TDB_NOSYNC or TDB_ALLOW_NESTING.
619  *
620  * You can use this to clear a flag on the TDB.  You cannot clear flags
621  * on a TDB_INTERNAL tdb.
622  */
623 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag);
624
625 /**
626  * enum tdb_attribute_type - descriminator for union tdb_attribute.
627  */
628 enum tdb_attribute_type {
629         TDB_ATTRIBUTE_LOG = 0,
630         TDB_ATTRIBUTE_HASH = 1,
631         TDB_ATTRIBUTE_SEED = 2,
632         TDB_ATTRIBUTE_STATS = 3,
633         TDB_ATTRIBUTE_OPENHOOK = 4,
634         TDB_ATTRIBUTE_FLOCK = 5,
635         TDB_ATTRIBUTE_TDB1_HASHSIZE = 128,
636         TDB_ATTRIBUTE_TDB1_MAX_DEAD = 129,
637 };
638
639 /**
640  * tdb_get_attribute - get an attribute for an existing tdb
641  * @tdb: the tdb context returned from tdb_open()
642  * @attr: the union tdb_attribute to set.
643  *
644  * This gets an attribute from a TDB which has previously been set (or
645  * may return the default values).  Set @attr.base.attr to the
646  * attribute type you want get.
647  */
648 enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
649                                  union tdb_attribute *attr);
650
651 /**
652  * tdb_set_attribute - set an attribute for an existing tdb
653  * @tdb: the tdb context returned from tdb_open()
654  * @attr: the union tdb_attribute to set.
655  *
656  * This sets an attribute on a TDB, overriding any previous attribute
657  * of the same type.  It returns TDB_ERR_EINVAL if the attribute is
658  * unknown or invalid.
659  *
660  * Note that TDB_ATTRIBUTE_HASH, TDB_ATTRIBUTE_SEED,
661  * TDB_ATTRIBUTE_OPENHOOK and TDB_ATTRIBUTE_TDB1_HASHSIZE cannot
662  * currently be set after tdb_open.
663  */
664 enum TDB_ERROR tdb_set_attribute(struct tdb_context *tdb,
665                                  const union tdb_attribute *attr);
666
667 /**
668  * tdb_unset_attribute - reset an attribute for an existing tdb
669  * @tdb: the tdb context returned from tdb_open()
670  * @type: the attribute type to unset.
671  *
672  * This unsets an attribute on a TDB, returning it to the defaults
673  * (where applicable).
674  *
675  * Note that it only makes sense for TDB_ATTRIBUTE_LOG and TDB_ATTRIBUTE_FLOCK
676  * to be unset.
677  */
678 void tdb_unset_attribute(struct tdb_context *tdb,
679                          enum tdb_attribute_type type);
680
681 /**
682  * tdb_name - get the name of a tdb
683  * @tdb: the tdb context returned from tdb_open()
684  *
685  * This returns a copy of the name string, made at tdb_open() time.  If that
686  * argument was NULL (possible for a TDB_INTERNAL db) this will return NULL.
687  *
688  * This is mostly useful for logging.
689  */
690 const char *tdb_name(const struct tdb_context *tdb);
691
692 /**
693  * tdb_fd - get the file descriptor of a tdb
694  * @tdb: the tdb context returned from tdb_open()
695  *
696  * This returns the file descriptor for the underlying database file, or -1
697  * for TDB_INTERNAL.
698  */
699 int tdb_fd(const struct tdb_context *tdb);
700
701 /**
702  * tdb_foreach - iterate through every open TDB.
703  * @fn: the function to call for every TDB
704  * @p: the pointer to hand to @fn
705  *
706  * TDB internally keeps track of all open TDBs; this function allows you to
707  * iterate through them.  If @fn returns non-zero, traversal stops.
708  */
709 #define tdb_foreach(fn, p)                                              \
710         tdb_foreach_(typesafe_cb_preargs(int, void *, (fn), (p),        \
711                                          struct tdb_context *), (p))
712
713 void tdb_foreach_(int (*fn)(struct tdb_context *, void *), void *p);
714
715 /**
716  * struct tdb_attribute_base - common fields for all tdb attributes.
717  */
718 struct tdb_attribute_base {
719         enum tdb_attribute_type attr;
720         union tdb_attribute *next;
721 };
722
723 /**
724  * enum tdb_log_level - log levels for tdb_attribute_log
725  * @TDB_LOG_ERROR: used to log unrecoverable errors such as I/O errors
726  *                 or internal consistency failures.
727  * @TDB_LOG_USE_ERROR: used to log usage errors such as invalid parameters
728  *                 or writing to a read-only database.
729  * @TDB_LOG_WARNING: used for informational messages on issues which
730  *                   are unusual but handled by TDB internally, such
731  *                   as a failure to mmap or failure to open /dev/urandom.
732  */
733 enum tdb_log_level {
734         TDB_LOG_ERROR,
735         TDB_LOG_USE_ERROR,
736         TDB_LOG_WARNING
737 };
738
739 /**
740  * struct tdb_attribute_log - log function attribute
741  *
742  * This attribute provides a hook for you to log errors.
743  */
744 struct tdb_attribute_log {
745         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
746         void (*fn)(struct tdb_context *tdb,
747                    enum tdb_log_level level,
748                    enum TDB_ERROR ecode,
749                    const char *message,
750                    void *data);
751         void *data;
752 };
753
754 /**
755  * struct tdb_attribute_hash - hash function attribute
756  *
757  * This attribute allows you to provide an alternative hash function.
758  * This hash function will be handed keys from the database; it will also
759  * be handed the 8-byte TDB_HASH_MAGIC value for checking the header (the
760  * tdb_open() will fail if the hash value doesn't match the header).
761  *
762  * Note that if your hash function gives different results on
763  * different machine endians, your tdb will no longer work across
764  * different architectures!
765  */
766 struct tdb_attribute_hash {
767         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
768         uint64_t (*fn)(const void *key, size_t len, uint64_t seed,
769                        void *data);
770         void *data;
771 };
772
773 /**
774  * struct tdb_attribute_seed - hash function seed attribute
775  *
776  * The hash function seed is normally taken from /dev/urandom (or equivalent)
777  * but can be set manually here.  This is mainly for testing purposes.
778  */
779 struct tdb_attribute_seed {
780         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_SEED */
781         uint64_t seed;
782 };
783
784 /**
785  * struct tdb_attribute_stats - tdb operational statistics
786  *
787  * This attribute records statistics of various low-level TDB operations.
788  * This can be used to assist performance evaluation.  This is only
789  * useful for tdb_get_attribute().
790  *
791  * New fields will be added at the end, hence the "size" argument which
792  * indicates how large your structure is: it must be filled in before
793  * calling tdb_get_attribute(), which will overwrite it with the size
794  * tdb knows about.
795  */
796 struct tdb_attribute_stats {
797         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_STATS */
798         size_t size; /* = sizeof(struct tdb_attribute_stats) */
799         uint64_t allocs;
800         uint64_t   alloc_subhash;
801         uint64_t   alloc_chain;
802         uint64_t   alloc_bucket_exact;
803         uint64_t   alloc_bucket_max;
804         uint64_t   alloc_leftover;
805         uint64_t   alloc_coalesce_tried;
806         uint64_t     alloc_coalesce_iterate_clash;
807         uint64_t     alloc_coalesce_lockfail;
808         uint64_t     alloc_coalesce_race;
809         uint64_t     alloc_coalesce_succeeded;
810         uint64_t       alloc_coalesce_num_merged;
811         uint64_t compares;
812         uint64_t   compare_wrong_bucket;
813         uint64_t   compare_wrong_offsetbits;
814         uint64_t   compare_wrong_keylen;
815         uint64_t   compare_wrong_rechash;
816         uint64_t   compare_wrong_keycmp;
817         uint64_t transactions;
818         uint64_t   transaction_cancel;
819         uint64_t   transaction_nest;
820         uint64_t   transaction_expand_file;
821         uint64_t   transaction_read_direct;
822         uint64_t      transaction_read_direct_fail;
823         uint64_t   transaction_write_direct;
824         uint64_t      transaction_write_direct_fail;
825         uint64_t expands;
826         uint64_t frees;
827         uint64_t locks;
828         uint64_t   lock_lowlevel;
829         uint64_t   lock_nonblock;
830         uint64_t     lock_nonblock_fail;
831 };
832
833 /**
834  * struct tdb_attribute_openhook - tdb special effects hook for open
835  *
836  * This attribute contains a function to call once we have the OPEN_LOCK
837  * for the tdb, but before we've examined its contents.  If this succeeds,
838  * the tdb will be populated if it's then zero-length.
839  *
840  * This is a hack to allow support for TDB1-style TDB_CLEAR_IF_FIRST
841  * behaviour.
842  */
843 struct tdb_attribute_openhook {
844         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_OPENHOOK */
845         enum TDB_ERROR (*fn)(int fd, void *data);
846         void *data;
847 };
848
849 /**
850  * struct tdb_attribute_flock - tdb special effects hook for file locking
851  *
852  * This attribute contains function to call to place locks on a file; it can
853  * be used to support non-blocking operations or lock proxying.
854  *
855  * They should return 0 on success, -1 on failure and set errno.
856  *
857  * An error will be logged on error if errno is neither EAGAIN nor EINTR
858  * (normally it would only return EAGAIN if waitflag is false, and
859  * loop internally on EINTR).
860  */
861 struct tdb_attribute_flock {
862         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_FLOCK */
863         int (*lock)(int fd,int rw, off_t off, off_t len, bool waitflag, void *);
864         int (*unlock)(int fd, int rw, off_t off, off_t len, void *);
865         void *data;
866 };
867
868 /**
869  * struct tdb_attribute_tdb1_hashsize - tdb1 hashsize
870  *
871  * This attribute allows setting the TDB1 hashsize; it only makes sense with
872  * O_CREAT and TDB_VERSION1.
873  *
874  * Hashsize should generally be a prime, such as 10007.
875  */
876 struct tdb_attribute_tdb1_hashsize {
877         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_TDB1_HASHSIZE */
878         unsigned int hsize;
879 };
880
881 /**
882  * struct tdb_attribute_tdb1_max_dead - tdb1 number of maximum dead records.
883  *
884  * TDB1 has a method to speed up its slow free list: it lets a certain
885  * number of "dead" records build up before freeing them.  This is
886  * particularly useful for volatile TDBs; setting it to 5 is
887  * equivalent to tdb1's TDB_VOLATILE flag.
888  */
889 struct tdb_attribute_tdb1_max_dead {
890         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_TDB1_MAX_DEAD */
891         unsigned int max_dead;
892 };
893
894 /**
895  * union tdb_attribute - tdb attributes.
896  *
897  * This represents all the known attributes.
898  *
899  * See also:
900  *      struct tdb_attribute_log, struct tdb_attribute_hash,
901  *      struct tdb_attribute_seed, struct tdb_attribute_stats,
902  *      struct tdb_attribute_openhook, struct tdb_attribute_flock.
903  */
904 union tdb_attribute {
905         struct tdb_attribute_base base;
906         struct tdb_attribute_log log;
907         struct tdb_attribute_hash hash;
908         struct tdb_attribute_seed seed;
909         struct tdb_attribute_stats stats;
910         struct tdb_attribute_openhook openhook;
911         struct tdb_attribute_flock flock;
912         struct tdb_attribute_tdb1_hashsize tdb1_hashsize;
913         struct tdb_attribute_tdb1_max_dead tdb1_max_dead;
914 };
915
916 #ifdef  __cplusplus
917 }
918 #endif
919
920 #endif /* tdb2.h */