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