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