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/>.
34 #if HAVE_FILE_OFFSET_BITS
35 #define _FILE_OFFSET_BITS 64
38 #include <sys/types.h>
41 /* For sig_atomic_t. */
50 #include <ccan/compiler/compiler.h>
51 #include <ccan/typesafe_cb/typesafe_cb.h>
52 #include <ccan/cast/cast.h>
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.
65 * This call opens (and potentially creates) a database file.
66 * Multiple processes can have the TDB file open at once.
68 * On failure it will return NULL, and set errno: it may also call
69 * any log attribute found in @attributes.
74 struct tdb_context *tdb_open(const char *name, int tdb_flags,
75 int open_flags, mode_t mode,
76 union tdb_attribute *attributes);
79 /* flags for tdb_open() */
80 #define TDB_DEFAULT 0 /* just a readability place holder */
81 #define TDB_INTERNAL 2 /* don't store on disk */
82 #define TDB_NOLOCK 4 /* don't do any locking */
83 #define TDB_NOMMAP 8 /* don't use mmap */
84 #define TDB_CONVERT 16 /* convert endian */
85 #define TDB_NOSYNC 64 /* don't use synchronous transactions */
86 #define TDB_SEQNUM 128 /* maintain a sequence number */
87 #define TDB_ALLOW_NESTING 256 /* fake nested transactions */
88 #define TDB_RDONLY 512 /* implied by O_RDONLY */
89 #define TDB_VERSION1 1024 /* create/open an old style TDB */
92 * tdb_close - close and free a tdb.
93 * @tdb: the tdb context returned from tdb_open()
95 * This always succeeds, in that @tdb is unusable after this call. But if
96 * some unexpected error occurred while closing, it will return non-zero
97 * (the only clue as to cause will be via the log attribute).
99 int tdb_close(struct tdb_context *tdb);
102 * struct tdb_data - representation of keys or values.
103 * @dptr: the data pointer
104 * @dsize: the size of the data pointed to by dptr.
106 * This is the "blob" representation of keys and data used by TDB.
108 typedef struct tdb_data {
114 * enum TDB_ERROR - error returns for TDB
120 TDB_SUCCESS = 0, /* No error. */
121 TDB_ERR_CORRUPT = -1, /* We read the db, and it was bogus. */
122 TDB_ERR_IO = -2, /* We couldn't read/write the db. */
123 TDB_ERR_LOCK = -3, /* Locking failed. */
124 TDB_ERR_OOM = -4, /* Out of Memory. */
125 TDB_ERR_EXISTS = -5, /* The key already exists. */
126 TDB_ERR_NOEXIST = -6, /* The key does not exist. */
127 TDB_ERR_EINVAL = -7, /* You're using it wrong. */
128 TDB_ERR_RDONLY = -8, /* The database is read-only. */
129 TDB_ERR_LAST = TDB_ERR_RDONLY
133 * tdb_store - store a key/value pair in a tdb.
134 * @tdb: the tdb context returned from tdb_open()
136 * @dbuf: the data to associate with the key.
137 * @flag: TDB_REPLACE, TDB_INSERT or TDB_MODIFY.
139 * This inserts (or overwrites) a key/value pair in the TDB. If flag
140 * is TDB_REPLACE, it doesn't matter whether the key exists or not;
141 * TDB_INSERT means it must not exist (returns TDB_ERR_EXISTS otherwise),
142 * and TDB_MODIFY means it must exist (returns TDB_ERR_NOEXIST otherwise).
144 * On success, this returns TDB_SUCCESS.
147 * tdb_fetch, tdb_transaction_start, tdb_append, tdb_delete.
149 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
151 struct tdb_data dbuf,
154 /* flags to tdb_store() */
155 #define TDB_REPLACE 1 /* A readability place holder */
156 #define TDB_INSERT 2 /* Don't overwrite an existing entry */
157 #define TDB_MODIFY 3 /* Don't create an existing entry */
160 * tdb_fetch - fetch a value from a tdb.
161 * @tdb: the tdb context returned from tdb_open()
163 * @data: pointer to data.
165 * This looks up a key in the database and sets it in @data.
167 * If it returns TDB_SUCCESS, the key was found: it is your
168 * responsibility to call free() on @data->dptr.
170 * Otherwise, it returns an error (usually, TDB_ERR_NOEXIST) and @data is
173 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
174 struct tdb_data *data);
177 * tdb_errorstr - map the tdb error onto a constant readable string
178 * @ecode: the enum TDB_ERROR to map.
180 * This is useful for displaying errors to users.
182 const char *tdb_errorstr(enum TDB_ERROR ecode);
185 * tdb_append - append a value to a key/value pair in a tdb.
186 * @tdb: the tdb context returned from tdb_open()
188 * @dbuf: the data to append.
190 * This is equivalent to fetching a record, reallocating .dptr to add the
191 * data, and writing it back, only it's much more efficient. If the key
192 * doesn't exist, it's equivalent to tdb_store (with an additional hint that
193 * you expect to expand the record in future).
196 * tdb_fetch(), tdb_store()
198 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
199 struct tdb_data key, struct tdb_data dbuf);
202 * tdb_delete - delete a key from a tdb.
203 * @tdb: the tdb context returned from tdb_open()
204 * @key: the key to delete.
206 * Returns TDB_SUCCESS on success, or an error (usually TDB_ERR_NOEXIST).
209 * tdb_fetch(), tdb_store()
211 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key);
214 * tdb_exists - does a key exist in the database?
215 * @tdb: the tdb context returned from tdb_open()
216 * @key: the key to search for.
218 * Returns true if it exists, or false if it doesn't or any other error.
220 bool tdb_exists(struct tdb_context *tdb, TDB_DATA key);
223 * tdb_deq - are struct tdb_data equal?
224 * @a: one struct tdb_data
225 * @b: another struct tdb_data
227 static inline bool tdb_deq(struct tdb_data a, struct tdb_data b)
229 return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
233 * tdb_mkdata - make a struct tdb_data from const data
234 * @p: the constant pointer
237 * As the dptr member of struct tdb_data is not constant, you need to
238 * cast it. This function keeps thost casts in one place, as well as
239 * suppressing the warning some compilers give when casting away a
240 * qualifier (eg. gcc with -Wcast-qual)
242 static inline struct tdb_data tdb_mkdata(const void *p, size_t len)
245 d.dptr = cast_const(void *, p);
251 * tdb_transaction_start - start a transaction
252 * @tdb: the tdb context returned from tdb_open()
254 * This begins a series of atomic operations. Other processes will be able
255 * to read the tdb, but not alter it (they will block), nor will they see
256 * any changes until tdb_transaction_commit() is called.
258 * Note that if the TDB_ALLOW_NESTING flag is set, a tdb_transaction_start()
259 * within a transaction will succeed, but it's not a real transaction:
260 * (1) An inner transaction which is committed is not actually committed until
261 * the outer transaction is; if the outer transaction is cancelled, the
262 * inner ones are discarded.
263 * (2) tdb_transaction_cancel() marks the outer transaction as having an error,
264 * so the final tdb_transaction_commit() will fail.
265 * (3) the outer transaction will see the results of the inner transaction.
268 * tdb_transaction_cancel, tdb_transaction_commit.
270 enum TDB_ERROR tdb_transaction_start(struct tdb_context *tdb);
273 * tdb_transaction_cancel - abandon a transaction
274 * @tdb: the tdb context returned from tdb_open()
276 * This aborts a transaction, discarding any changes which were made.
277 * tdb_close() does this implicitly.
279 void tdb_transaction_cancel(struct tdb_context *tdb);
282 * tdb_transaction_commit - commit a transaction
283 * @tdb: the tdb context returned from tdb_open()
285 * This completes a transaction, writing any changes which were made.
287 * fsync() is used to commit the transaction (unless TDB_NOSYNC is set),
288 * making it robust against machine crashes, but very slow compared to
289 * other TDB operations.
291 * A failure can only be caused by unexpected errors (eg. I/O or
292 * memory); this is no point looping on transaction failure.
295 * tdb_transaction_prepare_commit()
297 enum TDB_ERROR tdb_transaction_commit(struct tdb_context *tdb);
300 * tdb_transaction_prepare_commit - prepare to commit a transaction
301 * @tdb: the tdb context returned from tdb_open()
303 * This ensures we have the resources to commit a transaction (using
304 * tdb_transaction_commit): if this succeeds then a transaction will only
305 * fail if the write() or fsync() calls fail.
307 * If this fails you must still call tdb_transaction_cancel() to cancel
311 * tdb_transaction_commit()
313 enum TDB_ERROR tdb_transaction_prepare_commit(struct tdb_context *tdb);
316 * tdb_traverse - traverse a TDB
317 * @tdb: the tdb context returned from tdb_open()
318 * @fn: the function to call for every key/value pair (or NULL)
319 * @p: the pointer to hand to @f
321 * This walks the TDB until all they keys have been traversed, or @fn
322 * returns non-zero. If the traverse function or other processes are
323 * changing data or adding or deleting keys, the traverse may be
324 * unreliable: keys may be skipped or (rarely) visited twice.
326 * There is one specific exception: the special case of deleting the
327 * current key does not undermine the reliability of the traversal.
329 * On success, returns the number of keys iterated. On error returns
330 * a negative enum TDB_ERROR value.
332 #define tdb_traverse(tdb, fn, p) \
333 tdb_traverse_(tdb, typesafe_cb_preargs(int, void *, (fn), (p), \
334 struct tdb_context *, \
335 TDB_DATA, TDB_DATA), (p))
337 int64_t tdb_traverse_(struct tdb_context *tdb,
338 int (*fn)(struct tdb_context *,
339 TDB_DATA, TDB_DATA, void *), void *p);
342 * tdb_parse_record - operate directly on data in the database.
343 * @tdb: the tdb context returned from tdb_open()
344 * @key: the key whose record we should hand to @parse
345 * @parse: the function to call for the data
346 * @data: the private pointer to hand to @parse (types must match).
348 * This avoids a copy for many cases, by handing you a pointer into
349 * the memory-mapped database. It also locks the record to prevent
350 * other accesses at the same time.
352 * Do not alter the data handed to parse()!
354 #define tdb_parse_record(tdb, key, parse, data) \
355 tdb_parse_record_((tdb), (key), \
356 typesafe_cb_preargs(enum TDB_ERROR, void *, \
358 TDB_DATA, TDB_DATA), (data))
360 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
362 enum TDB_ERROR (*parse)(TDB_DATA k,
368 * tdb_get_seqnum - get a database sequence number
369 * @tdb: the tdb context returned from tdb_open()
371 * This returns a sequence number: any change to the database from a
372 * tdb context opened with the TDB_SEQNUM flag will cause that number
373 * to increment. Note that the incrementing is unreliable (it is done
374 * without locking), so this is only useful as an optimization.
376 * For example, you may have a regular database backup routine which
377 * does not operate if the sequence number is unchanged. In the
378 * unlikely event of a failed increment, it will be backed up next
381 * Returns an enum TDB_ERROR (ie. negative) on error.
383 int64_t tdb_get_seqnum(struct tdb_context *tdb);
386 * tdb_firstkey - get the "first" key in a TDB
387 * @tdb: the tdb context returned from tdb_open()
388 * @key: pointer to key.
390 * This returns an arbitrary key in the database; with tdb_nextkey() it allows
391 * open-coded traversal of the database, though it is slightly less efficient
394 * It is your responsibility to free @key->dptr on success.
396 * Returns TDB_ERR_NOEXIST if the database is empty.
398 enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key);
401 * tdb_nextkey - get the "next" key in a TDB
402 * @tdb: the tdb context returned from tdb_open()
403 * @key: a key returned by tdb_firstkey() or tdb_nextkey().
405 * This returns another key in the database; it will free @key.dptr for
408 * Returns TDB_ERR_NOEXIST if there are no more keys.
410 enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key);
413 * tdb_chainlock - lock a record in the TDB
414 * @tdb: the tdb context returned from tdb_open()
415 * @key: the key to lock.
417 * This prevents any access occurring to a group of keys including @key,
418 * even if @key does not exist. This allows primitive atomic updates of
419 * records without using transactions.
421 * You cannot begin a transaction while holding a tdb_chainlock(), nor can
422 * you do any operations on any other keys in the database. This also means
423 * that you cannot hold more than one tdb_chainlock() at a time.
428 enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
431 * tdb_chainunlock - unlock a record in the TDB
432 * @tdb: the tdb context returned from tdb_open()
433 * @key: the key to unlock.
435 * The key must have previously been locked by tdb_chainlock().
437 void tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
440 * tdb_chainlock_read - lock a record in the TDB, for reading
441 * @tdb: the tdb context returned from tdb_open()
442 * @key: the key to lock.
444 * This prevents any changes from occurring to a group of keys including @key,
445 * even if @key does not exist. This allows primitive atomic updates of
446 * records without using transactions.
448 * You cannot begin a transaction while holding a tdb_chainlock_read(), nor can
449 * you do any operations on any other keys in the database. This also means
450 * that you cannot hold more than one tdb_chainlock()/read() at a time.
455 enum TDB_ERROR tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
458 * tdb_chainunlock_read - unlock a record in the TDB for reading
459 * @tdb: the tdb context returned from tdb_open()
460 * @key: the key to unlock.
462 * The key must have previously been locked by tdb_chainlock_read().
464 void tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
467 * tdb_lockall - lock the entire TDB
468 * @tdb: the tdb context returned from tdb_open()
470 * You cannot hold a tdb_chainlock while calling this. It nests, so you
471 * must call tdb_unlockall as many times as you call tdb_lockall.
473 enum TDB_ERROR tdb_lockall(struct tdb_context *tdb);
476 * tdb_unlockall - unlock the entire TDB
477 * @tdb: the tdb context returned from tdb_open()
479 void tdb_unlockall(struct tdb_context *tdb);
482 * tdb_lockall_read - lock the entire TDB for reading
483 * @tdb: the tdb context returned from tdb_open()
485 * This prevents others writing to the database, eg. tdb_delete, tdb_store,
486 * tdb_append, but not tdb_fetch.
488 * You cannot hold a tdb_chainlock while calling this. It nests, so you
489 * must call tdb_unlockall_read as many times as you call tdb_lockall_read.
491 enum TDB_ERROR tdb_lockall_read(struct tdb_context *tdb);
494 * tdb_unlockall_read - unlock the entire TDB for reading
495 * @tdb: the tdb context returned from tdb_open()
497 void tdb_unlockall_read(struct tdb_context *tdb);
500 * tdb_wipe_all - wipe the database clean
501 * @tdb: the tdb context returned from tdb_open()
503 * Completely erase the database. This is faster than iterating through
504 * each key and doing tdb_delete.
506 enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb);
509 * tdb_check - check a TDB for consistency
510 * @tdb: the tdb context returned from tdb_open()
511 * @check: function to check each key/data pair (or NULL)
512 * @data: argument for @check, must match type.
514 * This performs a consistency check of the open database, optionally calling
515 * a check() function on each record so you can do your own data consistency
516 * checks as well. If check() returns an error, that is returned from
519 * Returns TDB_SUCCESS or an error.
521 #define tdb_check(tdb, check, data) \
522 tdb_check_((tdb), typesafe_cb_preargs(enum TDB_ERROR, void *, \
528 enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
529 enum TDB_ERROR (*check)(struct tdb_data k,
535 * tdb_error - get the last error (not threadsafe)
536 * @tdb: the tdb context returned from tdb_open()
538 * Returns the last error returned by a TDB function.
540 * This makes porting from TDB1 easier, but note that the last error is not
541 * reliable in threaded programs.
543 enum TDB_ERROR tdb_error(struct tdb_context *tdb);
546 * enum tdb_summary_flags - flags for tdb_summary.
548 enum tdb_summary_flags {
549 TDB_SUMMARY_HISTOGRAMS = 1 /* Draw graphs in the summary. */
553 * tdb_summary - return a string describing the TDB state
554 * @tdb: the tdb context returned from tdb_open()
555 * @flags: flags to control the summary output.
556 * @summary: pointer to string to allocate.
558 * This returns a developer-readable string describing the overall
559 * state of the tdb, such as the percentage used and sizes of records.
560 * It is designed to provide information about the tdb at a glance
561 * without displaying any keys or data in the database.
563 * On success, sets @summary to point to a malloc()'ed nul-terminated
564 * multi-line string. It is your responsibility to free() it.
566 enum TDB_ERROR tdb_summary(struct tdb_context *tdb,
567 enum tdb_summary_flags flags,
572 * tdb_get_flags - return the flags for a tdb
573 * @tdb: the tdb context returned from tdb_open()
575 * This returns the flags on the current tdb. Some of these are caused by
576 * the flags argument to tdb_open(), others (such as TDB_CONVERT) are
579 unsigned int tdb_get_flags(struct tdb_context *tdb);
582 * tdb_add_flag - set a flag for a tdb
583 * @tdb: the tdb context returned from tdb_open()
584 * @flag: one of TDB_NOLOCK, TDB_NOMMAP, TDB_NOSYNC or TDB_ALLOW_NESTING.
586 * You can use this to set a flag on the TDB. You cannot set these flags
587 * on a TDB_INTERNAL tdb.
589 void tdb_add_flag(struct tdb_context *tdb, unsigned flag);
592 * tdb_remove_flag - unset a flag for a tdb
593 * @tdb: the tdb context returned from tdb_open()
594 * @flag: one of TDB_NOLOCK, TDB_NOMMAP, TDB_NOSYNC or TDB_ALLOW_NESTING.
596 * You can use this to clear a flag on the TDB. You cannot clear flags
597 * on a TDB_INTERNAL tdb.
599 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag);
602 * enum tdb_attribute_type - descriminator for union tdb_attribute.
604 enum tdb_attribute_type {
605 TDB_ATTRIBUTE_LOG = 0,
606 TDB_ATTRIBUTE_HASH = 1,
607 TDB_ATTRIBUTE_SEED = 2,
608 TDB_ATTRIBUTE_STATS = 3,
609 TDB_ATTRIBUTE_OPENHOOK = 4,
610 TDB_ATTRIBUTE_FLOCK = 5,
611 TDB_ATTRIBUTE_TDB1_HASHSIZE = 128,
615 * tdb_get_attribute - get an attribute for an existing tdb
616 * @tdb: the tdb context returned from tdb_open()
617 * @attr: the union tdb_attribute to set.
619 * This gets an attribute from a TDB which has previously been set (or
620 * may return the default values). Set @attr.base.attr to the
621 * attribute type you want get.
623 enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
624 union tdb_attribute *attr);
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.
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.
635 * Note that TDB_ATTRIBUTE_HASH, TDB_ATTRIBUTE_SEED,
636 * TDB_ATTRIBUTE_OPENHOOK and TDB_ATTRIBUTE_TDB1_HASHSIZE cannot
637 * currently be set after tdb_open.
639 enum TDB_ERROR tdb_set_attribute(struct tdb_context *tdb,
640 const union tdb_attribute *attr);
643 * tdb_unset_attribute - reset an attribute for an existing tdb
644 * @tdb: the tdb context returned from tdb_open()
645 * @type: the attribute type to unset.
647 * This unsets an attribute on a TDB, returning it to the defaults
648 * (where applicable).
650 * Note that it only makes sense for TDB_ATTRIBUTE_LOG and TDB_ATTRIBUTE_FLOCK
653 void tdb_unset_attribute(struct tdb_context *tdb,
654 enum tdb_attribute_type type);
657 * tdb_name - get the name of a tdb
658 * @tdb: the tdb context returned from tdb_open()
660 * This returns a copy of the name string, made at tdb_open() time. If that
661 * argument was NULL (possible for a TDB_INTERNAL db) this will return NULL.
663 * This is mostly useful for logging.
665 const char *tdb_name(const struct tdb_context *tdb);
668 * tdb_fd - get the file descriptor of a tdb
669 * @tdb: the tdb context returned from tdb_open()
671 * This returns the file descriptor for the underlying database file, or -1
674 int tdb_fd(const struct tdb_context *tdb);
677 * tdb_foreach - iterate through every open TDB.
678 * @fn: the function to call for every TDB
679 * @p: the pointer to hand to @fn
681 * TDB internally keeps track of all open TDBs; this function allows you to
682 * iterate through them. If @fn returns non-zero, traversal stops.
684 #define tdb_foreach(fn, p) \
685 tdb_foreach_(typesafe_cb_preargs(int, void *, (fn), (p), \
686 struct tdb_context *), (p))
688 void tdb_foreach_(int (*fn)(struct tdb_context *, void *), void *p);
691 * struct tdb_attribute_base - common fields for all tdb attributes.
693 struct tdb_attribute_base {
694 enum tdb_attribute_type attr;
695 union tdb_attribute *next;
699 * enum tdb_log_level - log levels for tdb_attribute_log
700 * @TDB_LOG_ERROR: used to log unrecoverable errors such as I/O errors
701 * or internal consistency failures.
702 * @TDB_LOG_USE_ERROR: used to log usage errors such as invalid parameters
703 * or writing to a read-only database.
704 * @TDB_LOG_WARNING: used for informational messages on issues which
705 * are unusual but handled by TDB internally, such
706 * as a failure to mmap or failure to open /dev/urandom.
715 * struct tdb_attribute_log - log function attribute
717 * This attribute provides a hook for you to log errors.
719 struct tdb_attribute_log {
720 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
721 void (*fn)(struct tdb_context *tdb,
722 enum tdb_log_level level,
723 enum TDB_ERROR ecode,
730 * struct tdb_attribute_hash - hash function attribute
732 * This attribute allows you to provide an alternative hash function.
733 * This hash function will be handed keys from the database; it will also
734 * be handed the 8-byte TDB_HASH_MAGIC value for checking the header (the
735 * tdb_open() will fail if the hash value doesn't match the header).
737 * Note that if your hash function gives different results on
738 * different machine endians, your tdb will no longer work across
739 * different architectures!
741 struct tdb_attribute_hash {
742 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
743 uint64_t (*fn)(const void *key, size_t len, uint64_t seed,
749 * struct tdb_attribute_seed - hash function seed attribute
751 * The hash function seed is normally taken from /dev/urandom (or equivalent)
752 * but can be set manually here. This is mainly for testing purposes.
754 struct tdb_attribute_seed {
755 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_SEED */
760 * struct tdb_attribute_stats - tdb operational statistics
762 * This attribute records statistics of various low-level TDB operations.
763 * This can be used to assist performance evaluation. This is only
764 * useful for tdb_get_attribute().
766 * New fields will be added at the end, hence the "size" argument which
767 * indicates how large your structure is: it must be filled in before
768 * calling tdb_get_attribute(), which will overwrite it with the size
771 struct tdb_attribute_stats {
772 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_STATS */
773 size_t size; /* = sizeof(struct tdb_attribute_stats) */
775 uint64_t alloc_subhash;
776 uint64_t alloc_chain;
777 uint64_t alloc_bucket_exact;
778 uint64_t alloc_bucket_max;
779 uint64_t alloc_leftover;
780 uint64_t alloc_coalesce_tried;
781 uint64_t alloc_coalesce_iterate_clash;
782 uint64_t alloc_coalesce_lockfail;
783 uint64_t alloc_coalesce_race;
784 uint64_t alloc_coalesce_succeeded;
785 uint64_t alloc_coalesce_num_merged;
787 uint64_t compare_wrong_bucket;
788 uint64_t compare_wrong_offsetbits;
789 uint64_t compare_wrong_keylen;
790 uint64_t compare_wrong_rechash;
791 uint64_t compare_wrong_keycmp;
792 uint64_t transactions;
793 uint64_t transaction_cancel;
794 uint64_t transaction_nest;
795 uint64_t transaction_expand_file;
796 uint64_t transaction_read_direct;
797 uint64_t transaction_read_direct_fail;
798 uint64_t transaction_write_direct;
799 uint64_t transaction_write_direct_fail;
803 uint64_t lock_lowlevel;
804 uint64_t lock_nonblock;
805 uint64_t lock_nonblock_fail;
809 * struct tdb_attribute_openhook - tdb special effects hook for open
811 * This attribute contains a function to call once we have the OPEN_LOCK
812 * for the tdb, but before we've examined its contents. If this succeeds,
813 * the tdb will be populated if it's then zero-length.
815 * This is a hack to allow support for TDB1-style TDB_CLEAR_IF_FIRST
818 struct tdb_attribute_openhook {
819 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_OPENHOOK */
820 enum TDB_ERROR (*fn)(int fd, void *data);
825 * struct tdb_attribute_flock - tdb special effects hook for file locking
827 * This attribute contains function to call to place locks on a file; it can
828 * be used to support non-blocking operations or lock proxying.
830 * They should return 0 on success, -1 on failure and set errno.
832 * An error will be logged on error if errno is neither EAGAIN nor EINTR
833 * (normally it would only return EAGAIN if waitflag is false, and
834 * loop internally on EINTR).
836 struct tdb_attribute_flock {
837 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_FLOCK */
838 int (*lock)(int fd,int rw, off_t off, off_t len, bool waitflag, void *);
839 int (*unlock)(int fd, int rw, off_t off, off_t len, void *);
844 * struct tdb_attribute_tdb1_hashsize - tdb1 hashsize
846 * This attribute allows setting the TDB1 hashsize; it only makes sense with
847 * O_CREAT and TDB_VERSION1.
849 * Hashsize should generally be a prime, such as 10007.
851 struct tdb_attribute_tdb1_hashsize {
852 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_TDB1_HASHSIZE */
857 * union tdb_attribute - tdb attributes.
859 * This represents all the known attributes.
862 * struct tdb_attribute_log, struct tdb_attribute_hash,
863 * struct tdb_attribute_seed, struct tdb_attribute_stats,
864 * struct tdb_attribute_openhook, struct tdb_attribute_flock.
866 union tdb_attribute {
867 struct tdb_attribute_base base;
868 struct tdb_attribute_log log;
869 struct tdb_attribute_hash hash;
870 struct tdb_attribute_seed seed;
871 struct tdb_attribute_stats stats;
872 struct tdb_attribute_openhook openhook;
873 struct tdb_attribute_flock flock;
874 struct tdb_attribute_tdb1_hashsize tdb1_hashsize;