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