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