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