5 TDB version 2: trivial database library
7 Copyright (C) Andrew Tridgell 1999-2004
8 Copyright (C) Rusty Russell 2010-2011
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
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.
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.
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/>.
32 #ifdef HAVE_LIBREPLACE
35 #if HAVE_FILE_OFFSET_BITS
36 #define _FILE_OFFSET_BITS 64
39 #include <sys/types.h>
42 /* For sig_atomic_t. */
53 #include <ccan/compiler/compiler.h>
54 #include <ccan/typesafe_cb/typesafe_cb.h>
55 #include <ccan/cast/cast.h>
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))
63 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
64 #define cast_const(type, expr) ((type)((intptr_t)(expr)))
66 #define cast_const(type, expr) ((type *)(expr))
69 #endif /* !HAVE_CCAN */
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.
82 * This call opens (and potentially creates) a database file.
83 * Multiple processes can have the TDB file open at once.
85 * On failure it will return NULL, and set errno: it may also call
86 * any log attribute found in @attributes.
91 struct tdb_context
*tdb_open(const char *name
, int tdb_flags
,
92 int open_flags
, mode_t mode
,
93 union tdb_attribute
*attributes
);
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_VERSION1 1024 /* create/open an old style TDB */
107 #define TDB_CANT_CHECK 2048 /* has a feature which we don't understand */
110 * tdb1_incompatible_hash - better (Jenkins) hash for tdb1
112 * This is better than the default hash for tdb1; but older versions of the
113 * tdb library (prior to version 1.2.6) won't be able to open them.
115 * It only makes sense to specify this (using tdb_attribute_hash) when
116 * creating (with O_CREAT) an old tdb version using TDB_VERSION1. It's
117 * equivalent to the TDB_INCOMPATIBLE_HASH flag for tdb1.
119 uint64_t tdb1_incompatible_hash(const void *, size_t, uint64_t, void *);
122 * tdb_close - close and free a tdb.
123 * @tdb: the tdb context returned from tdb_open()
125 * This always succeeds, in that @tdb is unusable after this call. But if
126 * some unexpected error occurred while closing, it will return non-zero
127 * (the only clue as to cause will be via the log attribute).
129 int tdb_close(struct tdb_context
*tdb
);
132 * struct tdb_data - representation of keys or values.
133 * @dptr: the data pointer
134 * @dsize: the size of the data pointed to by dptr.
136 * This is the "blob" representation of keys and data used by TDB.
138 typedef struct tdb_data
{
144 * enum TDB_ERROR - error returns for TDB
150 TDB_SUCCESS
= 0, /* No error. */
151 TDB_ERR_CORRUPT
= -1, /* We read the db, and it was bogus. */
152 TDB_ERR_IO
= -2, /* We couldn't read/write the db. */
153 TDB_ERR_LOCK
= -3, /* Locking failed. */
154 TDB_ERR_OOM
= -4, /* Out of Memory. */
155 TDB_ERR_EXISTS
= -5, /* The key already exists. */
156 TDB_ERR_NOEXIST
= -6, /* The key does not exist. */
157 TDB_ERR_EINVAL
= -7, /* You're using it wrong. */
158 TDB_ERR_RDONLY
= -8, /* The database is read-only. */
159 TDB_ERR_LAST
= TDB_ERR_RDONLY
163 * tdb_store - store a key/value pair in a tdb.
164 * @tdb: the tdb context returned from tdb_open()
166 * @dbuf: the data to associate with the key.
167 * @flag: TDB_REPLACE, TDB_INSERT or TDB_MODIFY.
169 * This inserts (or overwrites) a key/value pair in the TDB. If flag
170 * is TDB_REPLACE, it doesn't matter whether the key exists or not;
171 * TDB_INSERT means it must not exist (returns TDB_ERR_EXISTS otherwise),
172 * and TDB_MODIFY means it must exist (returns TDB_ERR_NOEXIST otherwise).
174 * On success, this returns TDB_SUCCESS.
177 * tdb_fetch, tdb_transaction_start, tdb_append, tdb_delete.
179 enum TDB_ERROR
tdb_store(struct tdb_context
*tdb
,
181 struct tdb_data dbuf
,
184 /* flags to tdb_store() */
185 #define TDB_REPLACE 1 /* A readability place holder */
186 #define TDB_INSERT 2 /* Don't overwrite an existing entry */
187 #define TDB_MODIFY 3 /* Don't create an existing entry */
190 * tdb_fetch - fetch a value from a tdb.
191 * @tdb: the tdb context returned from tdb_open()
193 * @data: pointer to data.
195 * This looks up a key in the database and sets it in @data.
197 * If it returns TDB_SUCCESS, the key was found: it is your
198 * responsibility to call free() on @data->dptr.
200 * Otherwise, it returns an error (usually, TDB_ERR_NOEXIST) and @data is
203 enum TDB_ERROR
tdb_fetch(struct tdb_context
*tdb
, struct tdb_data key
,
204 struct tdb_data
*data
);
207 * tdb_errorstr - map the tdb error onto a constant readable string
208 * @ecode: the enum TDB_ERROR to map.
210 * This is useful for displaying errors to users.
212 const char *tdb_errorstr(enum TDB_ERROR ecode
);
215 * tdb_append - append a value to a key/value pair in a tdb.
216 * @tdb: the tdb context returned from tdb_open()
218 * @dbuf: the data to append.
220 * This is equivalent to fetching a record, reallocating .dptr to add the
221 * data, and writing it back, only it's much more efficient. If the key
222 * doesn't exist, it's equivalent to tdb_store (with an additional hint that
223 * you expect to expand the record in future).
226 * tdb_fetch(), tdb_store()
228 enum TDB_ERROR
tdb_append(struct tdb_context
*tdb
,
229 struct tdb_data key
, struct tdb_data dbuf
);
232 * tdb_delete - delete a key from a tdb.
233 * @tdb: the tdb context returned from tdb_open()
234 * @key: the key to delete.
236 * Returns TDB_SUCCESS on success, or an error (usually TDB_ERR_NOEXIST).
239 * tdb_fetch(), tdb_store()
241 enum TDB_ERROR
tdb_delete(struct tdb_context
*tdb
, struct tdb_data key
);
244 * tdb_exists - does a key exist in the database?
245 * @tdb: the tdb context returned from tdb_open()
246 * @key: the key to search for.
248 * Returns true if it exists, or false if it doesn't or any other error.
250 bool tdb_exists(struct tdb_context
*tdb
, TDB_DATA key
);
253 * tdb_deq - are struct tdb_data equal?
254 * @a: one struct tdb_data
255 * @b: another struct tdb_data
257 static inline bool tdb_deq(struct tdb_data a
, struct tdb_data b
)
259 return a
.dsize
== b
.dsize
&& memcmp(a
.dptr
, b
.dptr
, a
.dsize
) == 0;
263 * tdb_mkdata - make a struct tdb_data from const data
264 * @p: the constant pointer
267 * As the dptr member of struct tdb_data is not constant, you need to
268 * cast it. This function keeps thost casts in one place, as well as
269 * suppressing the warning some compilers give when casting away a
270 * qualifier (eg. gcc with -Wcast-qual)
272 static inline struct tdb_data
tdb_mkdata(const void *p
, size_t len
)
275 d
.dptr
= cast_const(void *, p
);
281 * tdb_transaction_start - start a transaction
282 * @tdb: the tdb context returned from tdb_open()
284 * This begins a series of atomic operations. Other processes will be able
285 * to read the tdb, but not alter it (they will block), nor will they see
286 * any changes until tdb_transaction_commit() is called.
288 * Note that if the TDB_ALLOW_NESTING flag is set, a tdb_transaction_start()
289 * within a transaction will succeed, but it's not a real transaction:
290 * (1) An inner transaction which is committed is not actually committed until
291 * the outer transaction is; if the outer transaction is cancelled, the
292 * inner ones are discarded.
293 * (2) tdb_transaction_cancel() marks the outer transaction as having an error,
294 * so the final tdb_transaction_commit() will fail.
295 * (3) the outer transaction will see the results of the inner transaction.
298 * tdb_transaction_cancel, tdb_transaction_commit.
300 enum TDB_ERROR
tdb_transaction_start(struct tdb_context
*tdb
);
303 * tdb_transaction_cancel - abandon a transaction
304 * @tdb: the tdb context returned from tdb_open()
306 * This aborts a transaction, discarding any changes which were made.
307 * tdb_close() does this implicitly.
309 void tdb_transaction_cancel(struct tdb_context
*tdb
);
312 * tdb_transaction_commit - commit a transaction
313 * @tdb: the tdb context returned from tdb_open()
315 * This completes a transaction, writing any changes which were made.
317 * fsync() is used to commit the transaction (unless TDB_NOSYNC is set),
318 * making it robust against machine crashes, but very slow compared to
319 * other TDB operations.
321 * A failure can only be caused by unexpected errors (eg. I/O or
322 * memory); this is no point looping on transaction failure.
325 * tdb_transaction_prepare_commit()
327 enum TDB_ERROR
tdb_transaction_commit(struct tdb_context
*tdb
);
330 * tdb_transaction_prepare_commit - prepare to commit a transaction
331 * @tdb: the tdb context returned from tdb_open()
333 * This ensures we have the resources to commit a transaction (using
334 * tdb_transaction_commit): if this succeeds then a transaction will only
335 * fail if the write() or fsync() calls fail.
337 * If this fails you must still call tdb_transaction_cancel() to cancel
341 * tdb_transaction_commit()
343 enum TDB_ERROR
tdb_transaction_prepare_commit(struct tdb_context
*tdb
);
346 * tdb_traverse - traverse a TDB
347 * @tdb: the tdb context returned from tdb_open()
348 * @fn: the function to call for every key/value pair (or NULL)
349 * @p: the pointer to hand to @f
351 * This walks the TDB until all they keys have been traversed, or @fn
352 * returns non-zero. If the traverse function or other processes are
353 * changing data or adding or deleting keys, the traverse may be
354 * unreliable: keys may be skipped or (rarely) visited twice.
356 * There is one specific exception: the special case of deleting the
357 * current key does not undermine the reliability of the traversal.
359 * On success, returns the number of keys iterated. On error returns
360 * a negative enum TDB_ERROR value.
362 #define tdb_traverse(tdb, fn, p) \
363 tdb_traverse_(tdb, typesafe_cb_preargs(int, void *, (fn), (p), \
364 struct tdb_context *, \
365 TDB_DATA, TDB_DATA), (p))
367 int64_t tdb_traverse_(struct tdb_context
*tdb
,
368 int (*fn
)(struct tdb_context
*,
369 TDB_DATA
, TDB_DATA
, void *), void *p
);
372 * tdb_parse_record - operate directly on data in the database.
373 * @tdb: the tdb context returned from tdb_open()
374 * @key: the key whose record we should hand to @parse
375 * @parse: the function to call for the data
376 * @data: the private pointer to hand to @parse (types must match).
378 * This avoids a copy for many cases, by handing you a pointer into
379 * the memory-mapped database. It also locks the record to prevent
380 * other accesses at the same time.
382 * Do not alter the data handed to parse()!
384 #define tdb_parse_record(tdb, key, parse, data) \
385 tdb_parse_record_((tdb), (key), \
386 typesafe_cb_preargs(enum TDB_ERROR, void *, \
388 TDB_DATA, TDB_DATA), (data))
390 enum TDB_ERROR
tdb_parse_record_(struct tdb_context
*tdb
,
392 enum TDB_ERROR (*parse
)(TDB_DATA k
,
398 * tdb_get_seqnum - get a database sequence number
399 * @tdb: the tdb context returned from tdb_open()
401 * This returns a sequence number: any change to the database from a
402 * tdb context opened with the TDB_SEQNUM flag will cause that number
403 * to increment. Note that the incrementing is unreliable (it is done
404 * without locking), so this is only useful as an optimization.
406 * For example, you may have a regular database backup routine which
407 * does not operate if the sequence number is unchanged. In the
408 * unlikely event of a failed increment, it will be backed up next
411 * Returns an enum TDB_ERROR (ie. negative) on error.
413 int64_t tdb_get_seqnum(struct tdb_context
*tdb
);
416 * tdb_firstkey - get the "first" key in a TDB
417 * @tdb: the tdb context returned from tdb_open()
418 * @key: pointer to key.
420 * This returns an arbitrary key in the database; with tdb_nextkey() it allows
421 * open-coded traversal of the database, though it is slightly less efficient
424 * It is your responsibility to free @key->dptr on success.
426 * Returns TDB_ERR_NOEXIST if the database is empty.
428 enum TDB_ERROR
tdb_firstkey(struct tdb_context
*tdb
, struct tdb_data
*key
);
431 * tdb_nextkey - get the "next" key in a TDB
432 * @tdb: the tdb context returned from tdb_open()
433 * @key: a key returned by tdb_firstkey() or tdb_nextkey().
435 * This returns another key in the database; it will free @key.dptr for
438 * Returns TDB_ERR_NOEXIST if there are no more keys.
440 enum TDB_ERROR
tdb_nextkey(struct tdb_context
*tdb
, struct tdb_data
*key
);
443 * tdb_chainlock - lock a record in the TDB
444 * @tdb: the tdb context returned from tdb_open()
445 * @key: the key to lock.
447 * This prevents any access occurring to a group of keys including @key,
448 * even if @key does not exist. This allows primitive atomic updates of
449 * records without using transactions.
451 * You cannot begin a transaction while holding a tdb_chainlock(), nor can
452 * you do any operations on any other keys in the database. This also means
453 * that you cannot hold more than one tdb_chainlock() at a time.
458 enum TDB_ERROR
tdb_chainlock(struct tdb_context
*tdb
, TDB_DATA key
);
461 * tdb_chainunlock - unlock a record in the TDB
462 * @tdb: the tdb context returned from tdb_open()
463 * @key: the key to unlock.
465 * The key must have previously been locked by tdb_chainlock().
467 void tdb_chainunlock(struct tdb_context
*tdb
, TDB_DATA key
);
470 * tdb_chainlock_read - lock a record in the TDB, for reading
471 * @tdb: the tdb context returned from tdb_open()
472 * @key: the key to lock.
474 * This prevents any changes from occurring to a group of keys including @key,
475 * even if @key does not exist. This allows primitive atomic updates of
476 * records without using transactions.
478 * You cannot begin a transaction while holding a tdb_chainlock_read(), nor can
479 * you do any operations on any other keys in the database. This also means
480 * that you cannot hold more than one tdb_chainlock()/read() at a time.
485 enum TDB_ERROR
tdb_chainlock_read(struct tdb_context
*tdb
, TDB_DATA key
);
488 * tdb_chainunlock_read - unlock a record in the TDB for reading
489 * @tdb: the tdb context returned from tdb_open()
490 * @key: the key to unlock.
492 * The key must have previously been locked by tdb_chainlock_read().
494 void tdb_chainunlock_read(struct tdb_context
*tdb
, TDB_DATA key
);
497 * tdb_lockall - lock the entire TDB
498 * @tdb: the tdb context returned from tdb_open()
500 * You cannot hold a tdb_chainlock while calling this. It nests, so you
501 * must call tdb_unlockall as many times as you call tdb_lockall.
503 enum TDB_ERROR
tdb_lockall(struct tdb_context
*tdb
);
506 * tdb_unlockall - unlock the entire TDB
507 * @tdb: the tdb context returned from tdb_open()
509 void tdb_unlockall(struct tdb_context
*tdb
);
512 * tdb_lockall_read - lock the entire TDB for reading
513 * @tdb: the tdb context returned from tdb_open()
515 * This prevents others writing to the database, eg. tdb_delete, tdb_store,
516 * tdb_append, but not tdb_fetch.
518 * You cannot hold a tdb_chainlock while calling this. It nests, so you
519 * must call tdb_unlockall_read as many times as you call tdb_lockall_read.
521 enum TDB_ERROR
tdb_lockall_read(struct tdb_context
*tdb
);
524 * tdb_unlockall_read - unlock the entire TDB for reading
525 * @tdb: the tdb context returned from tdb_open()
527 void tdb_unlockall_read(struct tdb_context
*tdb
);
530 * tdb_wipe_all - wipe the database clean
531 * @tdb: the tdb context returned from tdb_open()
533 * Completely erase the database. This is faster than iterating through
534 * each key and doing tdb_delete.
536 enum TDB_ERROR
tdb_wipe_all(struct tdb_context
*tdb
);
539 * tdb_repack - repack the database
540 * @tdb: the tdb context returned from tdb_open()
542 * This repacks the database; if it is suffering from a great deal of
543 * fragmentation this might help. However, it can take twice the
544 * memory of the existing TDB.
546 enum TDB_ERROR
tdb_repack(struct tdb_context
*tdb
);
549 * tdb_check - check a TDB for consistency
550 * @tdb: the tdb context returned from tdb_open()
551 * @check: function to check each key/data pair (or NULL)
552 * @data: argument for @check, must match type.
554 * This performs a consistency check of the open database, optionally calling
555 * a check() function on each record so you can do your own data consistency
556 * checks as well. If check() returns an error, that is returned from
559 * Note that the TDB uses a feature which we don't understand which
560 * indicates we can't run tdb_check(), this will log a warning to that
561 * effect and return TDB_SUCCESS. You can detect this condition by
562 * looking for TDB_CANT_CHECK in tdb_get_flags().
564 * Returns TDB_SUCCESS or an error.
566 #define tdb_check(tdb, check, data) \
567 tdb_check_((tdb), typesafe_cb_preargs(enum TDB_ERROR, void *, \
573 enum TDB_ERROR
tdb_check_(struct tdb_context
*tdb
,
574 enum TDB_ERROR (*check
)(struct tdb_data k
,
580 * tdb_error - get the last error (not threadsafe)
581 * @tdb: the tdb context returned from tdb_open()
583 * Returns the last error returned by a TDB function.
585 * This makes porting from TDB1 easier, but note that the last error is not
586 * reliable in threaded programs.
588 enum TDB_ERROR
tdb_error(struct tdb_context
*tdb
);
591 * enum tdb_summary_flags - flags for tdb_summary.
593 enum tdb_summary_flags
{
594 TDB_SUMMARY_HISTOGRAMS
= 1 /* Draw graphs in the summary. */
598 * tdb_summary - return a string describing the TDB state
599 * @tdb: the tdb context returned from tdb_open()
600 * @flags: flags to control the summary output.
601 * @summary: pointer to string to allocate.
603 * This returns a developer-readable string describing the overall
604 * state of the tdb, such as the percentage used and sizes of records.
605 * It is designed to provide information about the tdb at a glance
606 * without displaying any keys or data in the database.
608 * On success, sets @summary to point to a malloc()'ed nul-terminated
609 * multi-line string. It is your responsibility to free() it.
611 enum TDB_ERROR
tdb_summary(struct tdb_context
*tdb
,
612 enum tdb_summary_flags flags
,
617 * tdb_get_flags - return the flags for a tdb
618 * @tdb: the tdb context returned from tdb_open()
620 * This returns the flags on the current tdb. Some of these are caused by
621 * the flags argument to tdb_open(), others (such as TDB_CONVERT) are
624 unsigned int tdb_get_flags(struct tdb_context
*tdb
);
627 * tdb_add_flag - set a flag for a tdb
628 * @tdb: the tdb context returned from tdb_open()
629 * @flag: one of TDB_NOLOCK, TDB_NOMMAP, TDB_NOSYNC or TDB_ALLOW_NESTING.
631 * You can use this to set a flag on the TDB. You cannot set these flags
632 * on a TDB_INTERNAL tdb.
634 void tdb_add_flag(struct tdb_context
*tdb
, unsigned flag
);
637 * tdb_remove_flag - unset a flag for a tdb
638 * @tdb: the tdb context returned from tdb_open()
639 * @flag: one of TDB_NOLOCK, TDB_NOMMAP, TDB_NOSYNC or TDB_ALLOW_NESTING.
641 * You can use this to clear a flag on the TDB. You cannot clear flags
642 * on a TDB_INTERNAL tdb.
644 void tdb_remove_flag(struct tdb_context
*tdb
, unsigned flag
);
647 * enum tdb_attribute_type - descriminator for union tdb_attribute.
649 enum tdb_attribute_type
{
650 TDB_ATTRIBUTE_LOG
= 0,
651 TDB_ATTRIBUTE_HASH
= 1,
652 TDB_ATTRIBUTE_SEED
= 2,
653 TDB_ATTRIBUTE_STATS
= 3,
654 TDB_ATTRIBUTE_OPENHOOK
= 4,
655 TDB_ATTRIBUTE_FLOCK
= 5,
656 TDB_ATTRIBUTE_TDB1_HASHSIZE
= 128,
657 TDB_ATTRIBUTE_TDB1_MAX_DEAD
= 129,
661 * tdb_get_attribute - get an attribute for an existing tdb
662 * @tdb: the tdb context returned from tdb_open()
663 * @attr: the union tdb_attribute to set.
665 * This gets an attribute from a TDB which has previously been set (or
666 * may return the default values). Set @attr.base.attr to the
667 * attribute type you want get.
669 enum TDB_ERROR
tdb_get_attribute(struct tdb_context
*tdb
,
670 union tdb_attribute
*attr
);
673 * tdb_set_attribute - set an attribute for an existing tdb
674 * @tdb: the tdb context returned from tdb_open()
675 * @attr: the union tdb_attribute to set.
677 * This sets an attribute on a TDB, overriding any previous attribute
678 * of the same type. It returns TDB_ERR_EINVAL if the attribute is
679 * unknown or invalid.
681 * Note that TDB_ATTRIBUTE_HASH, TDB_ATTRIBUTE_SEED,
682 * TDB_ATTRIBUTE_OPENHOOK and TDB_ATTRIBUTE_TDB1_HASHSIZE cannot
683 * currently be set after tdb_open.
685 enum TDB_ERROR
tdb_set_attribute(struct tdb_context
*tdb
,
686 const union tdb_attribute
*attr
);
689 * tdb_unset_attribute - reset an attribute for an existing tdb
690 * @tdb: the tdb context returned from tdb_open()
691 * @type: the attribute type to unset.
693 * This unsets an attribute on a TDB, returning it to the defaults
694 * (where applicable).
696 * Note that it only makes sense for TDB_ATTRIBUTE_LOG and TDB_ATTRIBUTE_FLOCK
699 void tdb_unset_attribute(struct tdb_context
*tdb
,
700 enum tdb_attribute_type type
);
703 * tdb_name - get the name of a tdb
704 * @tdb: the tdb context returned from tdb_open()
706 * This returns a copy of the name string, made at tdb_open() time. If that
707 * argument was NULL (possible for a TDB_INTERNAL db) this will return NULL.
709 * This is mostly useful for logging.
711 const char *tdb_name(const struct tdb_context
*tdb
);
714 * tdb_fd - get the file descriptor of a tdb
715 * @tdb: the tdb context returned from tdb_open()
717 * This returns the file descriptor for the underlying database file, or -1
720 int tdb_fd(const struct tdb_context
*tdb
);
723 * tdb_foreach - iterate through every open TDB.
724 * @fn: the function to call for every TDB
725 * @p: the pointer to hand to @fn
727 * TDB internally keeps track of all open TDBs; this function allows you to
728 * iterate through them. If @fn returns non-zero, traversal stops.
730 #define tdb_foreach(fn, p) \
731 tdb_foreach_(typesafe_cb_preargs(int, void *, (fn), (p), \
732 struct tdb_context *), (p))
734 void tdb_foreach_(int (*fn
)(struct tdb_context
*, void *), void *p
);
737 * struct tdb_attribute_base - common fields for all tdb attributes.
739 struct tdb_attribute_base
{
740 enum tdb_attribute_type attr
;
741 union tdb_attribute
*next
;
745 * enum tdb_log_level - log levels for tdb_attribute_log
746 * @TDB_LOG_ERROR: used to log unrecoverable errors such as I/O errors
747 * or internal consistency failures.
748 * @TDB_LOG_USE_ERROR: used to log usage errors such as invalid parameters
749 * or writing to a read-only database.
750 * @TDB_LOG_WARNING: used for informational messages on issues which
751 * are unusual but handled by TDB internally, such
752 * as a failure to mmap or failure to open /dev/urandom.
761 * struct tdb_attribute_log - log function attribute
763 * This attribute provides a hook for you to log errors.
765 struct tdb_attribute_log
{
766 struct tdb_attribute_base base
; /* .attr = TDB_ATTRIBUTE_LOG */
767 void (*fn
)(struct tdb_context
*tdb
,
768 enum tdb_log_level level
,
769 enum TDB_ERROR ecode
,
776 * struct tdb_attribute_hash - hash function attribute
778 * This attribute allows you to provide an alternative hash function.
779 * This hash function will be handed keys from the database; it will also
780 * be handed the 8-byte TDB_HASH_MAGIC value for checking the header (the
781 * tdb_open() will fail if the hash value doesn't match the header).
783 * Note that if your hash function gives different results on
784 * different machine endians, your tdb will no longer work across
785 * different architectures!
787 struct tdb_attribute_hash
{
788 struct tdb_attribute_base base
; /* .attr = TDB_ATTRIBUTE_HASH */
789 uint64_t (*fn
)(const void *key
, size_t len
, uint64_t seed
,
795 * struct tdb_attribute_seed - hash function seed attribute
797 * The hash function seed is normally taken from /dev/urandom (or equivalent)
798 * but can be set manually here. This is mainly for testing purposes.
800 struct tdb_attribute_seed
{
801 struct tdb_attribute_base base
; /* .attr = TDB_ATTRIBUTE_SEED */
806 * struct tdb_attribute_stats - tdb operational statistics
808 * This attribute records statistics of various low-level TDB operations.
809 * This can be used to assist performance evaluation. This is only
810 * useful for tdb_get_attribute().
812 * New fields will be added at the end, hence the "size" argument which
813 * indicates how large your structure is: it must be filled in before
814 * calling tdb_get_attribute(), which will overwrite it with the size
817 struct tdb_attribute_stats
{
818 struct tdb_attribute_base base
; /* .attr = TDB_ATTRIBUTE_STATS */
819 size_t size
; /* = sizeof(struct tdb_attribute_stats) */
821 uint64_t alloc_subhash
;
822 uint64_t alloc_chain
;
823 uint64_t alloc_bucket_exact
;
824 uint64_t alloc_bucket_max
;
825 uint64_t alloc_leftover
;
826 uint64_t alloc_coalesce_tried
;
827 uint64_t alloc_coalesce_iterate_clash
;
828 uint64_t alloc_coalesce_lockfail
;
829 uint64_t alloc_coalesce_race
;
830 uint64_t alloc_coalesce_succeeded
;
831 uint64_t alloc_coalesce_num_merged
;
833 uint64_t compare_wrong_bucket
;
834 uint64_t compare_wrong_offsetbits
;
835 uint64_t compare_wrong_keylen
;
836 uint64_t compare_wrong_rechash
;
837 uint64_t compare_wrong_keycmp
;
838 uint64_t transactions
;
839 uint64_t transaction_cancel
;
840 uint64_t transaction_nest
;
841 uint64_t transaction_expand_file
;
842 uint64_t transaction_read_direct
;
843 uint64_t transaction_read_direct_fail
;
844 uint64_t transaction_write_direct
;
845 uint64_t transaction_write_direct_fail
;
849 uint64_t lock_lowlevel
;
850 uint64_t lock_nonblock
;
851 uint64_t lock_nonblock_fail
;
855 * struct tdb_attribute_openhook - tdb special effects hook for open
857 * This attribute contains a function to call once we have the OPEN_LOCK
858 * for the tdb, but before we've examined its contents. If this succeeds,
859 * the tdb will be populated if it's then zero-length.
861 * This is a hack to allow support for TDB1-style TDB_CLEAR_IF_FIRST
864 struct tdb_attribute_openhook
{
865 struct tdb_attribute_base base
; /* .attr = TDB_ATTRIBUTE_OPENHOOK */
866 enum TDB_ERROR (*fn
)(int fd
, void *data
);
871 * struct tdb_attribute_flock - tdb special effects hook for file locking
873 * This attribute contains function to call to place locks on a file; it can
874 * be used to support non-blocking operations or lock proxying.
876 * They should return 0 on success, -1 on failure and set errno.
878 * An error will be logged on error if errno is neither EAGAIN nor EINTR
879 * (normally it would only return EAGAIN if waitflag is false, and
880 * loop internally on EINTR).
882 struct tdb_attribute_flock
{
883 struct tdb_attribute_base base
; /* .attr = TDB_ATTRIBUTE_FLOCK */
884 int (*lock
)(int fd
,int rw
, off_t off
, off_t len
, bool waitflag
, void *);
885 int (*unlock
)(int fd
, int rw
, off_t off
, off_t len
, void *);
890 * struct tdb_attribute_tdb1_hashsize - tdb1 hashsize
892 * This attribute allows setting the TDB1 hashsize; it only makes sense with
893 * O_CREAT and TDB_VERSION1.
895 * Hashsize should generally be a prime, such as 10007.
897 struct tdb_attribute_tdb1_hashsize
{
898 struct tdb_attribute_base base
; /* .attr = TDB_ATTRIBUTE_TDB1_HASHSIZE */
903 * struct tdb_attribute_tdb1_max_dead - tdb1 number of maximum dead records.
905 * TDB1 has a method to speed up its slow free list: it lets a certain
906 * number of "dead" records build up before freeing them. This is
907 * particularly useful for volatile TDBs; setting it to 5 is
908 * equivalent to tdb1's TDB_VOLATILE flag.
910 struct tdb_attribute_tdb1_max_dead
{
911 struct tdb_attribute_base base
; /* .attr = TDB_ATTRIBUTE_TDB1_MAX_DEAD */
912 unsigned int max_dead
;
916 * union tdb_attribute - tdb attributes.
918 * This represents all the known attributes.
921 * struct tdb_attribute_log, struct tdb_attribute_hash,
922 * struct tdb_attribute_seed, struct tdb_attribute_stats,
923 * struct tdb_attribute_openhook, struct tdb_attribute_flock.
925 union tdb_attribute
{
926 struct tdb_attribute_base base
;
927 struct tdb_attribute_log log
;
928 struct tdb_attribute_hash hash
;
929 struct tdb_attribute_seed seed
;
930 struct tdb_attribute_stats stats
;
931 struct tdb_attribute_openhook openhook
;
932 struct tdb_attribute_flock flock
;
933 struct tdb_attribute_tdb1_hashsize tdb1_hashsize
;
934 struct tdb_attribute_tdb1_max_dead tdb1_max_dead
;