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