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