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 #include <sys/types.h>
37 /* For sig_atomic_t. */
46 #include <ccan/compiler/compiler.h>
47 #include <ccan/typesafe_cb/typesafe_cb.h>
48 #include <ccan/cast/cast.h>
54 * tdb_open - open a database file
55 * @name: the file name (can be NULL if flags contains TDB_INTERNAL)
56 * @tdb_flags: options for this database
57 * @open_flags: flags argument for tdb's open() call.
58 * @mode: mode argument for tdb's open() call.
59 * @attributes: linked list of extra attributes for this tdb.
61 * This call opens (and potentially creates) a database file.
62 * Multiple processes can have the TDB file open at once.
64 * On failure it will return NULL, and set errno: it may also call
65 * any log attribute found in @attributes.
70 struct tdb_context *tdb_open(const char *name, int tdb_flags,
71 int open_flags, mode_t mode,
72 union tdb_attribute *attributes);
75 /* flags for tdb_open() */
76 #define TDB_DEFAULT 0 /* just a readability place holder */
77 #define TDB_INTERNAL 2 /* don't store on disk */
78 #define TDB_NOLOCK 4 /* don't do any locking */
79 #define TDB_NOMMAP 8 /* don't use mmap */
80 #define TDB_CONVERT 16 /* convert endian */
81 #define TDB_NOSYNC 64 /* don't use synchronous transactions */
82 #define TDB_SEQNUM 128 /* maintain a sequence number */
85 * tdb_close - close and free a tdb.
86 * @tdb: the tdb context returned from tdb_open()
88 * This always succeeds, in that @tdb is unusable after this call. But if
89 * some unexpected error occurred while closing, it will return non-zero
90 * (the only clue as to cause will be via the log attribute).
92 int tdb_close(struct tdb_context *tdb);
95 * struct tdb_data - representation of keys or values.
96 * @dptr: the data pointer
97 * @dsize: the size of the data pointed to by dptr.
99 * This is the "blob" representation of keys and data used by TDB.
101 typedef struct tdb_data {
107 * enum TDB_ERROR - error returns for TDB
113 TDB_SUCCESS = 0, /* No error. */
114 TDB_ERR_CORRUPT = -1, /* We read the db, and it was bogus. */
115 TDB_ERR_IO = -2, /* We couldn't read/write the db. */
116 TDB_ERR_LOCK = -3, /* Locking failed. */
117 TDB_ERR_OOM = -4, /* Out of Memory. */
118 TDB_ERR_EXISTS = -5, /* The key already exists. */
119 TDB_ERR_NOEXIST = -6, /* The key does not exist. */
120 TDB_ERR_EINVAL = -7, /* You're using it wrong. */
121 TDB_ERR_RDONLY = -8, /* The database is read-only. */
122 TDB_ERR_LAST = TDB_ERR_RDONLY
126 * tdb_store - store a key/value pair in a tdb.
127 * @tdb: the tdb context returned from tdb_open()
129 * @dbuf: the data to associate with the key.
130 * @flag: TDB_REPLACE, TDB_INSERT or TDB_MODIFY.
132 * This inserts (or overwrites) a key/value pair in the TDB. If flag
133 * is TDB_REPLACE, it doesn't matter whether the key exists or not;
134 * TDB_INSERT means it must not exist (returns TDB_ERR_EXISTS otherwise),
135 * and TDB_MODIFY means it must exist (returns TDB_ERR_NOEXIST otherwise).
137 * On success, this returns TDB_SUCCESS.
140 * tdb_fetch, tdb_transaction_start, tdb_append, tdb_delete.
142 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
144 struct tdb_data dbuf,
147 /* flags to tdb_store() */
148 #define TDB_REPLACE 1 /* A readability place holder */
149 #define TDB_INSERT 2 /* Don't overwrite an existing entry */
150 #define TDB_MODIFY 3 /* Don't create an existing entry */
153 * tdb_fetch - fetch a value from a tdb.
154 * @tdb: the tdb context returned from tdb_open()
156 * @data: pointer to data.
158 * This looks up a key in the database and sets it in @data.
160 * If it returns TDB_SUCCESS, the key was found: it is your
161 * responsibility to call free() on @data->dptr.
163 * Otherwise, it returns an error (usually, TDB_ERR_NOEXIST) and @data is
166 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
167 struct tdb_data *data);
170 * tdb_errorstr - map the tdb error onto a constant readable string
171 * @ecode: the enum TDB_ERROR to map.
173 * This is useful for displaying errors to users.
175 const char *tdb_errorstr(enum TDB_ERROR ecode);
178 * tdb_append - append a value to a key/value pair in a tdb.
179 * @tdb: the tdb context returned from tdb_open()
181 * @dbuf: the data to append.
183 * This is equivalent to fetching a record, reallocating .dptr to add the
184 * data, and writing it back, only it's much more efficient. If the key
185 * doesn't exist, it's equivalent to tdb_store (with an additional hint that
186 * you expect to expand the record in future).
189 * tdb_fetch(), tdb_store()
191 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
192 struct tdb_data key, struct tdb_data dbuf);
195 * tdb_delete - delete a key from a tdb.
196 * @tdb: the tdb context returned from tdb_open()
197 * @key: the key to delete.
199 * Returns TDB_SUCCESS on success, or an error (usually TDB_ERR_NOEXIST).
202 * tdb_fetch(), tdb_store()
204 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key);
207 * tdb_exists - does a key exist in the database?
208 * @tdb: the tdb context returned from tdb_open()
209 * @key: the key to search for.
211 * Returns true if it exists, or false if it doesn't or any other error.
213 bool tdb_exists(struct tdb_context *tdb, TDB_DATA key);
216 * tdb_deq - are struct tdb_data equal?
217 * @a: one struct tdb_data
218 * @b: another struct tdb_data
220 static inline bool tdb_deq(struct tdb_data a, struct tdb_data b)
222 return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
226 * tdb_mkdata - make a struct tdb_data from const data
227 * @p: the constant pointer
230 * As the dptr member of struct tdb_data is not constant, you need to
231 * cast it. This function keeps thost casts in one place, as well as
232 * suppressing the warning some compilers give when casting away a
233 * qualifier (eg. gcc with -Wcast-qual)
235 static inline struct tdb_data tdb_mkdata(const void *p, size_t len)
238 d.dptr = cast_const(void *, p);
244 * tdb_transaction_start - start a transaction
245 * @tdb: the tdb context returned from tdb_open()
247 * This begins a series of atomic operations. Other processes will be able
248 * to read the tdb, but not alter it (they will block), nor will they see
249 * any changes until tdb_transaction_commit() is called.
252 * tdb_transaction_cancel, tdb_transaction_commit.
254 enum TDB_ERROR tdb_transaction_start(struct tdb_context *tdb);
257 * tdb_transaction_cancel - abandon a transaction
258 * @tdb: the tdb context returned from tdb_open()
260 * This aborts a transaction, discarding any changes which were made.
261 * tdb_close() does this implicitly.
263 void tdb_transaction_cancel(struct tdb_context *tdb);
266 * tdb_transaction_commit - commit a transaction
267 * @tdb: the tdb context returned from tdb_open()
269 * This completes a transaction, writing any changes which were made.
271 * fsync() is used to commit the transaction (unless TDB_NOSYNC is set),
272 * making it robust against machine crashes, but very slow compared to
273 * other TDB operations.
275 * A failure can only be caused by unexpected errors (eg. I/O or
276 * memory); this is no point looping on transaction failure.
279 * tdb_transaction_prepare_commit()
281 enum TDB_ERROR tdb_transaction_commit(struct tdb_context *tdb);
284 * tdb_transaction_prepare_commit - prepare to commit a transaction
285 * @tdb: the tdb context returned from tdb_open()
287 * This ensures we have the resources to commit a transaction (using
288 * tdb_transaction_commit): if this succeeds then a transaction will only
289 * fail if the write() or fsync() calls fail.
292 * tdb_transaction_commit()
294 enum TDB_ERROR tdb_transaction_prepare_commit(struct tdb_context *tdb);
297 * tdb_traverse - traverse a TDB
298 * @tdb: the tdb context returned from tdb_open()
299 * @fn: the function to call for every key/value pair (or NULL)
300 * @p: the pointer to hand to @f
302 * This walks the TDB until all they keys have been traversed, or @fn
303 * returns non-zero. If the traverse function or other processes are
304 * changing data or adding or deleting keys, the traverse may be
305 * unreliable: keys may be skipped or (rarely) visited twice.
307 * There is one specific exception: the special case of deleting the
308 * current key does not undermine the reliability of the traversal.
310 * On success, returns the number of keys iterated. On error returns
311 * a negative enum TDB_ERROR value.
313 #define tdb_traverse(tdb, fn, p) \
314 tdb_traverse_(tdb, typesafe_cb_preargs(int, (fn), (p), \
315 struct tdb_context *, \
316 TDB_DATA, TDB_DATA), (p))
318 int64_t tdb_traverse_(struct tdb_context *tdb,
319 int (*fn)(struct tdb_context *,
320 TDB_DATA, TDB_DATA, void *), void *p);
323 * tdb_parse_record - operate directly on data in the database.
324 * @tdb: the tdb context returned from tdb_open()
325 * @key: the key whose record we should hand to @parse
326 * @parse: the function to call for the data
327 * @p: the private pointer to hand to @parse (types must match).
329 * This avoids a copy for many cases, by handing you a pointer into
330 * the memory-mapped database. It also locks the record to prevent
331 * other accesses at the same time.
333 * Do not alter the data handed to parse()!
335 #define tdb_parse_record(tdb, key, parse, p) \
336 tdb_parse_record_((tdb), (key), \
337 typesafe_cb_preargs(enum TDB_ERROR, (parse), (p), \
338 TDB_DATA, TDB_DATA), (p))
340 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
342 enum TDB_ERROR (*parse)(TDB_DATA key,
348 * tdb_get_seqnum - get a database sequence number
349 * @tdb: the tdb context returned from tdb_open()
351 * This returns a sequence number: any change to the database from a
352 * tdb context opened with the TDB_SEQNUM flag will cause that number
353 * to increment. Note that the incrementing is unreliable (it is done
354 * without locking), so this is only useful as an optimization.
356 * For example, you may have a regular database backup routine which
357 * does not operate if the sequence number is unchanged. In the
358 * unlikely event of a failed increment, it will be backed up next
361 * Returns an enum TDB_ERROR (ie. negative) on error.
363 int64_t tdb_get_seqnum(struct tdb_context *tdb);
366 * tdb_firstkey - get the "first" key in a TDB
367 * @tdb: the tdb context returned from tdb_open()
368 * @key: pointer to key.
370 * This returns an arbitrary key in the database; with tdb_nextkey() it allows
371 * open-coded traversal of the database, though it is slightly less efficient
374 * It is your responsibility to free @key->dptr on success.
376 * Returns TDB_ERR_NOEXIST if the database is empty.
378 enum TDB_ERROR tdb_firstkey(struct tdb_context *tdb, struct tdb_data *key);
381 * tdb_nextkey - get the "next" key in a TDB
382 * @tdb: the tdb context returned from tdb_open()
383 * @key: a key returned by tdb_firstkey() or tdb_nextkey().
385 * This returns another key in the database; it will free @key.dptr for
388 * Returns TDB_ERR_NOEXIST if there are no more keys.
390 enum TDB_ERROR tdb_nextkey(struct tdb_context *tdb, struct tdb_data *key);
393 * tdb_chainlock - lock a record in the TDB
394 * @tdb: the tdb context returned from tdb_open()
395 * @key: the key to lock.
397 * This prevents any changes from occurring to a group of keys including @key,
398 * even if @key does not exist. This allows primitive atomic updates of
399 * records without using transactions.
401 * You cannot begin a transaction while holding a tdb_chainlock(), nor can
402 * you do any operations on any other keys in the database. This also means
403 * that you cannot hold more than one tdb_chainlock() at a time.
408 enum TDB_ERROR tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
411 * tdb_chainunlock - unlock a record in the TDB
412 * @tdb: the tdb context returned from tdb_open()
413 * @key: the key to unlock.
415 enum TDB_ERROR tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
418 * tdb_wipe_all - wipe the database clean
419 * @tdb: the tdb context returned from tdb_open()
421 * Completely erase the database. This is faster than iterating through
422 * each key and doing tdb_delete.
424 enum TDB_ERROR tdb_wipe_all(struct tdb_context *tdb);
427 * tdb_check - check a TDB for consistency
428 * @tdb: the tdb context returned from tdb_open()
429 * @check: function to check each key/data pair (or NULL)
430 * @private: argument for @check, must match type.
432 * This performs a consistency check of the open database, optionally calling
433 * a check() function on each record so you can do your own data consistency
434 * checks as well. If check() returns an error, that is returned from
437 * Returns TDB_SUCCESS or an error.
439 #define tdb_check(tdb, check, private) \
440 tdb_check_((tdb), typesafe_cb_preargs(enum TDB_ERROR, \
441 (check), (private), \
446 enum TDB_ERROR tdb_check_(struct tdb_context *tdb,
447 enum TDB_ERROR (*check)(struct tdb_data key,
448 struct tdb_data data,
453 * tdb_error - get the last error (not threadsafe)
454 * @tdb: the tdb context returned from tdb_open()
456 * Returns the last error returned by a TDB function.
458 * This makes porting from TDB1 easier, but note that the last error is not
459 * reliable in threaded programs.
461 enum TDB_ERROR tdb_error(struct tdb_context *tdb);
464 * enum tdb_summary_flags - flags for tdb_summary.
466 enum tdb_summary_flags {
467 TDB_SUMMARY_HISTOGRAMS = 1 /* Draw graphs in the summary. */
471 * tdb_summary - return a string describing the TDB state
472 * @tdb: the tdb context returned from tdb_open()
473 * @flags: flags to control the summary output.
474 * @summary: pointer to string to allocate.
476 * This returns a developer-readable string describing the overall
477 * state of the tdb, such as the percentage used and sizes of records.
478 * It is designed to provide information about the tdb at a glance
479 * without displaying any keys or data in the database.
481 * On success, sets @summary to point to a malloc()'ed nul-terminated
482 * multi-line string. It is your responsibility to free() it.
484 enum TDB_ERROR tdb_summary(struct tdb_context *tdb,
485 enum tdb_summary_flags flags,
490 * tdb_get_flags - return the flags for a tdb
491 * @tdb: the tdb context returned from tdb_open()
493 * This returns the flags on the current tdb. Some of these are caused by
494 * the flags argument to tdb_open(), others (such as TDB_CONVERT) are
497 unsigned int tdb_get_flags(struct tdb_context *tdb);
500 * tdb_add_flag - set a flag for a tdb
501 * @tdb: the tdb context returned from tdb_open()
502 * @flag: one of TDB_NOLOCK, TDB_NOMMAP or TDB_NOSYNC.
504 * You can use this to set a flag on the TDB. You cannot set these flags
505 * on a TDB_INTERNAL tdb.
507 void tdb_add_flag(struct tdb_context *tdb, unsigned flag);
510 * tdb_remove_flag - unset a flag for a tdb
511 * @tdb: the tdb context returned from tdb_open()
512 * @flag: one of TDB_NOLOCK, TDB_NOMMAP or TDB_NOSYNC.
514 * You can use this to clear a flag on the TDB. You cannot clear flags
515 * on a TDB_INTERNAL tdb.
517 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag);
520 * tdb_name - get the name of a tdb
521 * @tdb: the tdb context returned from tdb_open()
523 * This returns a copy of the name string, made at tdb_open() time. If that
524 * argument was NULL (possible for a TDB_INTERNAL db) this will return NULL.
526 * This is mostly useful for logging.
528 const char *tdb_name(const struct tdb_context *tdb);
531 * tdb_fd - get the file descriptor of a tdb
532 * @tdb: the tdb context returned from tdb_open()
534 * This returns the file descriptor for the underlying database file, or -1
537 int tdb_fd(const struct tdb_context *tdb);
540 * enum tdb_attribute_type - descriminator for union tdb_attribute.
542 enum tdb_attribute_type {
543 TDB_ATTRIBUTE_LOG = 0,
544 TDB_ATTRIBUTE_HASH = 1,
545 TDB_ATTRIBUTE_SEED = 2,
546 TDB_ATTRIBUTE_STATS = 3
550 * struct tdb_attribute_base - common fields for all tdb attributes.
552 struct tdb_attribute_base {
553 enum tdb_attribute_type attr;
554 union tdb_attribute *next;
558 * enum tdb_log_level - log levels for tdb_attribute_log
559 * @TDB_LOG_ERROR: used to log unrecoverable errors such as I/O errors
560 * or internal consistency failures.
561 * @TDB_LOG_USE_ERROR: used to log usage errors such as invalid parameters
562 * or writing to a read-only database.
563 * @TDB_LOG_WARNING: used for informational messages on issues which
564 * are unusual but handled by TDB internally, such
565 * as a failure to mmap or failure to open /dev/urandom.
574 * struct tdb_attribute_log - log function attribute
576 * This attribute provides a hook for you to log errors.
578 struct tdb_attribute_log {
579 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
580 void (*log_fn)(struct tdb_context *tdb,
581 enum tdb_log_level level,
583 const char *message);
588 * struct tdb_attribute_hash - hash function attribute
590 * This attribute allows you to provide an alternative hash function.
591 * This hash function will be handed keys from the database; it will also
592 * be handed the 8-byte TDB_HASH_MAGIC value for checking the header (the
593 * tdb_open() will fail if the hash value doesn't match the header).
595 * Note that if your hash function gives different results on
596 * different machine endians, your tdb will no longer work across
597 * different architectures!
599 struct tdb_attribute_hash {
600 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
601 uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed,
607 * struct tdb_attribute_seed - hash function seed attribute
609 * The hash function seed is normally taken from /dev/urandom (or equivalent)
610 * but can be set manually here. This is mainly for testing purposes.
612 struct tdb_attribute_seed {
613 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_SEED */
618 * struct tdb_attribute_stats - tdb operational statistics
620 * This attribute records statistics of various low-level TDB operations.
621 * This can be used to assist performance evaluation.
623 * New fields will be added at the end, hence the "size" argument which
624 * indicates how large your structure is. If your size is larger than
625 * that known about by this version of tdb, the size will be reduced to
626 * the known structure size. Thus you can detect older versions, and
627 * thus know that newer stats will not be updated.
629 struct tdb_attribute_stats {
630 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_STATS */
631 size_t size; /* = sizeof(struct tdb_attribute_stats) */
633 uint64_t alloc_subhash;
634 uint64_t alloc_chain;
635 uint64_t alloc_bucket_exact;
636 uint64_t alloc_bucket_max;
637 uint64_t alloc_leftover;
638 uint64_t alloc_coalesce_tried;
639 uint64_t alloc_coalesce_lockfail;
640 uint64_t alloc_coalesce_race;
641 uint64_t alloc_coalesce_succeeded;
642 uint64_t alloc_coalesce_num_merged;
644 uint64_t compare_wrong_bucket;
645 uint64_t compare_wrong_offsetbits;
646 uint64_t compare_wrong_keylen;
647 uint64_t compare_wrong_rechash;
648 uint64_t compare_wrong_keycmp;
652 uint64_t lock_lowlevel;
653 uint64_t lock_nonblock;
657 * union tdb_attribute - tdb attributes.
659 * This represents all the known attributes.
662 * struct tdb_attribute_log, struct tdb_attribute_hash,
663 * struct tdb_attribute_seed, struct tdb_attribute_stats.
665 union tdb_attribute {
666 struct tdb_attribute_base base;
667 struct tdb_attribute_log log;
668 struct tdb_attribute_hash hash;
669 struct tdb_attribute_seed seed;
670 struct tdb_attribute_stats stats;