wintest: add option to select the dns backend
[Samba/gebeck_regimport.git] / lib / ntdb / ntdb.h
blob8e8458e4d17d811ae9d89eed19ed540a875a6a71
1 #ifndef CCAN_NTDB_H
2 #define CCAN_NTDB_H
4 /*
5 NTDB: trivial database library version 2
7 Copyright (C) Andrew Tridgell 1999-2004
8 Copyright (C) Rusty Russell 2010-2012
10 ** NOTE! The following LGPL license applies to the ntdb
11 ** library. This does NOT imply that all of Samba is released
12 ** under the LGPL
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/>.
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
32 #ifdef HAVE_LIBREPLACE
33 #include <replace.h>
34 #else
35 #if HAVE_FILE_OFFSET_BITS
36 #define _FILE_OFFSET_BITS 64
37 #endif
38 /* For mode_t */
39 #include <sys/types.h>
40 /* For O_* flags. */
41 #include <sys/stat.h>
42 /* For sig_atomic_t. */
43 #include <signal.h>
44 /* For uint64_t */
45 #include <stdint.h>
46 /* For bool */
47 #include <stdbool.h>
48 /* For memcmp */
49 #include <string.h>
50 #endif
52 #if HAVE_CCAN
53 #include <ccan/compiler/compiler.h>
54 #include <ccan/typesafe_cb/typesafe_cb.h>
55 #include <ccan/cast/cast.h>
56 #else
57 #ifndef typesafe_cb_preargs
58 /* Failing to have CCAN just mean less typesafe protection, etc. */
59 #define typesafe_cb_preargs(rtype, atype, fn, arg, ...) \
60 ((rtype (*)(__VA_ARGS__, atype))(fn))
61 #endif
62 #ifndef cast_const
63 #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
64 #define cast_const(type, expr) ((type)((intptr_t)(expr)))
65 #else
66 #define cast_const(type, expr) ((type *)(expr))
67 #endif
68 #endif
69 #endif /* !HAVE_CCAN */
71 union ntdb_attribute;
72 struct ntdb_context;
74 /**
75 * struct TDB_DATA - (n)tdb data blob
77 * To ease compatibility, we use 'struct TDB_DATA' from tdb.h, so if
78 * you want to include both tdb.h and ntdb.h, you need to #include
79 * tdb.h first.
81 #ifndef __TDB_H__
82 struct TDB_DATA {
83 unsigned char *dptr;
84 size_t dsize;
86 #endif
88 typedef struct TDB_DATA NTDB_DATA;
90 /**
91 * ntdb_open - open a database file
92 * @name: the file name (or database name if flags contains NTDB_INTERNAL)
93 * @ntdb_flags: options for this database
94 * @open_flags: flags argument for ntdb's open() call.
95 * @mode: mode argument for ntdb's open() call.
96 * @attributes: linked list of extra attributes for this ntdb.
98 * This call opens (and potentially creates) a database file.
99 * Multiple processes can have the NTDB file open at once.
101 * On failure it will return NULL, and set errno: it may also call
102 * any log attribute found in @attributes.
104 * See also:
105 * union ntdb_attribute
107 struct ntdb_context *ntdb_open(const char *name, int ntdb_flags,
108 int open_flags, mode_t mode,
109 union ntdb_attribute *attributes);
112 /* flags for ntdb_open() */
113 #define NTDB_DEFAULT 0 /* just a readability place holder */
114 #define NTDB_INTERNAL 2 /* don't store on disk */
115 #define NTDB_NOLOCK 4 /* don't do any locking */
116 #define NTDB_NOMMAP 8 /* don't use mmap */
117 #define NTDB_CONVERT 16 /* convert endian */
118 #define NTDB_NOSYNC 64 /* don't use synchronous transactions */
119 #define NTDB_SEQNUM 128 /* maintain a sequence number */
120 #define NTDB_ALLOW_NESTING 256 /* fake nested transactions */
121 #define NTDB_RDONLY 512 /* implied by O_RDONLY */
122 #define NTDB_CANT_CHECK 2048 /* has a feature which we don't understand */
125 * ntdb_close - close and free a ntdb.
126 * @ntdb: the ntdb context returned from ntdb_open()
128 * This always succeeds, in that @ntdb is unusable after this call. But if
129 * some unexpected error occurred while closing, it will return non-zero
130 * (the only clue as to cause will be via the log attribute).
132 int ntdb_close(struct ntdb_context *ntdb);
135 * enum NTDB_ERROR - error returns for NTDB
137 * See Also:
138 * ntdb_errorstr()
140 enum NTDB_ERROR {
141 NTDB_SUCCESS = 0, /* No error. */
142 NTDB_ERR_CORRUPT = -1, /* We read the db, and it was bogus. */
143 NTDB_ERR_IO = -2, /* We couldn't read/write the db. */
144 NTDB_ERR_LOCK = -3, /* Locking failed. */
145 NTDB_ERR_OOM = -4, /* Out of Memory. */
146 NTDB_ERR_EXISTS = -5, /* The key already exists. */
147 NTDB_ERR_NOEXIST = -6, /* The key does not exist. */
148 NTDB_ERR_EINVAL = -7, /* You're using it wrong. */
149 NTDB_ERR_RDONLY = -8, /* The database is read-only. */
150 NTDB_ERR_LAST = NTDB_ERR_RDONLY
154 * ntdb_store - store a key/value pair in a ntdb.
155 * @ntdb: the ntdb context returned from ntdb_open()
156 * @key: the key
157 * @dbuf: the data to associate with the key.
158 * @flag: NTDB_REPLACE, NTDB_INSERT or NTDB_MODIFY.
160 * This inserts (or overwrites) a key/value pair in the NTDB. If flag
161 * is NTDB_REPLACE, it doesn't matter whether the key exists or not;
162 * NTDB_INSERT means it must not exist (returns NTDB_ERR_EXISTS otherwise),
163 * and NTDB_MODIFY means it must exist (returns NTDB_ERR_NOEXIST otherwise).
165 * On success, this returns NTDB_SUCCESS.
167 * See also:
168 * ntdb_fetch, ntdb_transaction_start, ntdb_append, ntdb_delete.
170 enum NTDB_ERROR ntdb_store(struct ntdb_context *ntdb,
171 NTDB_DATA key,
172 NTDB_DATA dbuf,
173 int flag);
175 /* flags to ntdb_store() */
176 #define NTDB_REPLACE 1 /* A readability place holder */
177 #define NTDB_INSERT 2 /* Don't overwrite an existing entry */
178 #define NTDB_MODIFY 3 /* Don't create an existing entry */
181 * ntdb_fetch - fetch a value from a ntdb.
182 * @ntdb: the ntdb context returned from ntdb_open()
183 * @key: the key
184 * @data: pointer to data.
186 * This looks up a key in the database and sets it in @data.
188 * If it returns NTDB_SUCCESS, the key was found: it is your
189 * responsibility to call free() on @data->dptr.
191 * Otherwise, it returns an error (usually, NTDB_ERR_NOEXIST) and @data is
192 * undefined.
194 enum NTDB_ERROR ntdb_fetch(struct ntdb_context *ntdb, NTDB_DATA key,
195 NTDB_DATA *data);
198 * ntdb_errorstr - map the ntdb error onto a constant readable string
199 * @ecode: the enum NTDB_ERROR to map.
201 * This is useful for displaying errors to users.
203 const char *ntdb_errorstr(enum NTDB_ERROR ecode);
206 * ntdb_append - append a value to a key/value pair in a ntdb.
207 * @ntdb: the ntdb context returned from ntdb_open()
208 * @key: the key
209 * @dbuf: the data to append.
211 * This is equivalent to fetching a record, reallocating .dptr to add the
212 * data, and writing it back, only it's much more efficient. If the key
213 * doesn't exist, it's equivalent to ntdb_store (with an additional hint that
214 * you expect to expand the record in future).
216 * See Also:
217 * ntdb_fetch(), ntdb_store()
219 enum NTDB_ERROR ntdb_append(struct ntdb_context *ntdb,
220 NTDB_DATA key, NTDB_DATA dbuf);
223 * ntdb_delete - delete a key from a ntdb.
224 * @ntdb: the ntdb context returned from ntdb_open()
225 * @key: the key to delete.
227 * Returns NTDB_SUCCESS on success, or an error (usually NTDB_ERR_NOEXIST).
229 * See Also:
230 * ntdb_fetch(), ntdb_store()
232 enum NTDB_ERROR ntdb_delete(struct ntdb_context *ntdb, NTDB_DATA key);
235 * ntdb_exists - does a key exist in the database?
236 * @ntdb: the ntdb context returned from ntdb_open()
237 * @key: the key to search for.
239 * Returns true if it exists, or false if it doesn't or any other error.
241 bool ntdb_exists(struct ntdb_context *ntdb, NTDB_DATA key);
244 * ntdb_deq - are NTDB_DATA equal?
245 * @a: one NTDB_DATA
246 * @b: another NTDB_DATA
248 static inline bool ntdb_deq(NTDB_DATA a, NTDB_DATA b)
250 return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
254 * ntdb_mkdata - make a NTDB_DATA from const data
255 * @p: the constant pointer
256 * @len: the length
258 * As the dptr member of NTDB_DATA is not constant, you need to
259 * cast it. This function keeps thost casts in one place, as well as
260 * suppressing the warning some compilers give when casting away a
261 * qualifier (eg. gcc with -Wcast-qual)
263 static inline NTDB_DATA ntdb_mkdata(const void *p, size_t len)
265 NTDB_DATA d;
266 d.dptr = cast_const(void *, p);
267 d.dsize = len;
268 return d;
272 * ntdb_transaction_start - start a transaction
273 * @ntdb: the ntdb context returned from ntdb_open()
275 * This begins a series of atomic operations. Other processes will be able
276 * to read the ntdb, but not alter it (they will block), nor will they see
277 * any changes until ntdb_transaction_commit() is called.
279 * Note that if the NTDB_ALLOW_NESTING flag is set, a ntdb_transaction_start()
280 * within a transaction will succeed, but it's not a real transaction:
281 * (1) An inner transaction which is committed is not actually committed until
282 * the outer transaction is; if the outer transaction is cancelled, the
283 * inner ones are discarded.
284 * (2) ntdb_transaction_cancel() marks the outer transaction as having an error,
285 * so the final ntdb_transaction_commit() will fail.
286 * (3) the outer transaction will see the results of the inner transaction.
288 * See Also:
289 * ntdb_transaction_cancel, ntdb_transaction_commit.
291 enum NTDB_ERROR ntdb_transaction_start(struct ntdb_context *ntdb);
294 * ntdb_transaction_cancel - abandon a transaction
295 * @ntdb: the ntdb context returned from ntdb_open()
297 * This aborts a transaction, discarding any changes which were made.
298 * ntdb_close() does this implicitly.
300 void ntdb_transaction_cancel(struct ntdb_context *ntdb);
303 * ntdb_transaction_commit - commit a transaction
304 * @ntdb: the ntdb context returned from ntdb_open()
306 * This completes a transaction, writing any changes which were made.
308 * fsync() is used to commit the transaction (unless NTDB_NOSYNC is set),
309 * making it robust against machine crashes, but very slow compared to
310 * other NTDB operations.
312 * A failure can only be caused by unexpected errors (eg. I/O or
313 * memory); this is no point looping on transaction failure.
315 * See Also:
316 * ntdb_transaction_prepare_commit()
318 enum NTDB_ERROR ntdb_transaction_commit(struct ntdb_context *ntdb);
321 * ntdb_transaction_prepare_commit - prepare to commit a transaction
322 * @ntdb: the ntdb context returned from ntdb_open()
324 * This ensures we have the resources to commit a transaction (using
325 * ntdb_transaction_commit): if this succeeds then a transaction will only
326 * fail if the write() or fsync() calls fail.
328 * If this fails you must still call ntdb_transaction_cancel() to cancel
329 * the transaction.
331 * See Also:
332 * ntdb_transaction_commit()
334 enum NTDB_ERROR ntdb_transaction_prepare_commit(struct ntdb_context *ntdb);
337 * ntdb_traverse - traverse a NTDB
338 * @ntdb: the ntdb context returned from ntdb_open()
339 * @fn: the function to call for every key/value pair (or NULL)
340 * @p: the pointer to hand to @f
342 * This walks the NTDB until all they keys have been traversed, or @fn
343 * returns non-zero. If the traverse function or other processes are
344 * changing data or adding or deleting keys, the traverse may be
345 * unreliable: keys may be skipped or (rarely) visited twice.
347 * There is one specific exception: the special case of deleting the
348 * current key does not undermine the reliability of the traversal.
350 * On success, returns the number of keys iterated. On error returns
351 * a negative enum NTDB_ERROR value.
353 #define ntdb_traverse(ntdb, fn, p) \
354 ntdb_traverse_(ntdb, typesafe_cb_preargs(int, void *, (fn), (p), \
355 struct ntdb_context *, \
356 NTDB_DATA, NTDB_DATA), (p))
358 int64_t ntdb_traverse_(struct ntdb_context *ntdb,
359 int (*fn)(struct ntdb_context *,
360 NTDB_DATA, NTDB_DATA, void *), void *p);
363 * ntdb_parse_record - operate directly on data in the database.
364 * @ntdb: the ntdb context returned from ntdb_open()
365 * @key: the key whose record we should hand to @parse
366 * @parse: the function to call for the data
367 * @data: the private pointer to hand to @parse (types must match).
369 * This avoids a copy for many cases, by handing you a pointer into
370 * the memory-mapped database. It also locks the record to prevent
371 * other accesses at the same time.
373 * Do not alter the data handed to parse()!
375 #define ntdb_parse_record(ntdb, key, parse, data) \
376 ntdb_parse_record_((ntdb), (key), \
377 typesafe_cb_preargs(enum NTDB_ERROR, void *, \
378 (parse), (data), \
379 NTDB_DATA, NTDB_DATA), (data))
381 enum NTDB_ERROR ntdb_parse_record_(struct ntdb_context *ntdb,
382 NTDB_DATA key,
383 enum NTDB_ERROR (*parse)(NTDB_DATA k,
384 NTDB_DATA d,
385 void *data),
386 void *data);
389 * ntdb_get_seqnum - get a database sequence number
390 * @ntdb: the ntdb context returned from ntdb_open()
392 * This returns a sequence number: any change to the database from a
393 * ntdb context opened with the NTDB_SEQNUM flag will cause that number
394 * to increment. Note that the incrementing is unreliable (it is done
395 * without locking), so this is only useful as an optimization.
397 * For example, you may have a regular database backup routine which
398 * does not operate if the sequence number is unchanged. In the
399 * unlikely event of a failed increment, it will be backed up next
400 * time any way.
402 * Returns an enum NTDB_ERROR (ie. negative) on error.
404 int64_t ntdb_get_seqnum(struct ntdb_context *ntdb);
407 * ntdb_firstkey - get the "first" key in a NTDB
408 * @ntdb: the ntdb context returned from ntdb_open()
409 * @key: pointer to key.
411 * This returns an arbitrary key in the database; with ntdb_nextkey() it allows
412 * open-coded traversal of the database, though it is slightly less efficient
413 * than ntdb_traverse.
415 * It is your responsibility to free @key->dptr on success.
417 * Returns NTDB_ERR_NOEXIST if the database is empty.
419 enum NTDB_ERROR ntdb_firstkey(struct ntdb_context *ntdb, NTDB_DATA *key);
422 * ntdb_nextkey - get the "next" key in a NTDB
423 * @ntdb: the ntdb context returned from ntdb_open()
424 * @key: a key returned by ntdb_firstkey() or ntdb_nextkey().
426 * This returns another key in the database; it will free @key.dptr for
427 * your convenience.
429 * Returns NTDB_ERR_NOEXIST if there are no more keys.
431 enum NTDB_ERROR ntdb_nextkey(struct ntdb_context *ntdb, NTDB_DATA *key);
434 * ntdb_chainlock - lock a record in the NTDB
435 * @ntdb: the ntdb context returned from ntdb_open()
436 * @key: the key to lock.
438 * This prevents any access occurring to a group of keys including @key,
439 * even if @key does not exist. This allows primitive atomic updates of
440 * records without using transactions.
442 * You cannot begin a transaction while holding a ntdb_chainlock(), nor can
443 * you do any operations on any other keys in the database. This also means
444 * that you cannot hold more than one ntdb_chainlock() at a time.
446 * See Also:
447 * ntdb_chainunlock()
449 enum NTDB_ERROR ntdb_chainlock(struct ntdb_context *ntdb, NTDB_DATA key);
452 * ntdb_chainunlock - unlock a record in the NTDB
453 * @ntdb: the ntdb context returned from ntdb_open()
454 * @key: the key to unlock.
456 * The key must have previously been locked by ntdb_chainlock().
458 void ntdb_chainunlock(struct ntdb_context *ntdb, NTDB_DATA key);
461 * ntdb_chainlock_read - lock a record in the NTDB, for reading
462 * @ntdb: the ntdb context returned from ntdb_open()
463 * @key: the key to lock.
465 * This prevents any changes from occurring to a group of keys including @key,
466 * even if @key does not exist. This allows primitive atomic updates of
467 * records without using transactions.
469 * You cannot begin a transaction while holding a ntdb_chainlock_read(), nor can
470 * you do any operations on any other keys in the database. This also means
471 * that you cannot hold more than one ntdb_chainlock()/read() at a time.
473 * See Also:
474 * ntdb_chainlock()
476 enum NTDB_ERROR ntdb_chainlock_read(struct ntdb_context *ntdb, NTDB_DATA key);
479 * ntdb_chainunlock_read - unlock a record in the NTDB for reading
480 * @ntdb: the ntdb context returned from ntdb_open()
481 * @key: the key to unlock.
483 * The key must have previously been locked by ntdb_chainlock_read().
485 void ntdb_chainunlock_read(struct ntdb_context *ntdb, NTDB_DATA key);
488 * ntdb_lockall - lock the entire NTDB
489 * @ntdb: the ntdb context returned from ntdb_open()
491 * You cannot hold a ntdb_chainlock while calling this. It nests, so you
492 * must call ntdb_unlockall as many times as you call ntdb_lockall.
494 enum NTDB_ERROR ntdb_lockall(struct ntdb_context *ntdb);
497 * ntdb_unlockall - unlock the entire NTDB
498 * @ntdb: the ntdb context returned from ntdb_open()
500 void ntdb_unlockall(struct ntdb_context *ntdb);
503 * ntdb_lockall_read - lock the entire NTDB for reading
504 * @ntdb: the ntdb context returned from ntdb_open()
506 * This prevents others writing to the database, eg. ntdb_delete, ntdb_store,
507 * ntdb_append, but not ntdb_fetch.
509 * You cannot hold a ntdb_chainlock while calling this. It nests, so you
510 * must call ntdb_unlockall_read as many times as you call ntdb_lockall_read.
512 enum NTDB_ERROR ntdb_lockall_read(struct ntdb_context *ntdb);
515 * ntdb_unlockall_read - unlock the entire NTDB for reading
516 * @ntdb: the ntdb context returned from ntdb_open()
518 void ntdb_unlockall_read(struct ntdb_context *ntdb);
521 * ntdb_wipe_all - wipe the database clean
522 * @ntdb: the ntdb context returned from ntdb_open()
524 * Completely erase the database. This is faster than iterating through
525 * each key and doing ntdb_delete.
527 enum NTDB_ERROR ntdb_wipe_all(struct ntdb_context *ntdb);
530 * ntdb_repack - repack the database
531 * @ntdb: the ntdb context returned from ntdb_open()
533 * This repacks the database; if it is suffering from a great deal of
534 * fragmentation this might help. However, it can take twice the
535 * memory of the existing NTDB.
537 enum NTDB_ERROR ntdb_repack(struct ntdb_context *ntdb);
540 * ntdb_check - check a NTDB for consistency
541 * @ntdb: the ntdb context returned from ntdb_open()
542 * @check: function to check each key/data pair (or NULL)
543 * @data: argument for @check, must match type.
545 * This performs a consistency check of the open database, optionally calling
546 * a check() function on each record so you can do your own data consistency
547 * checks as well. If check() returns an error, that is returned from
548 * ntdb_check().
550 * Note that the NTDB uses a feature which we don't understand which
551 * indicates we can't run ntdb_check(), this will log a warning to that
552 * effect and return NTDB_SUCCESS. You can detect this condition by
553 * looking for NTDB_CANT_CHECK in ntdb_get_flags().
555 * Returns NTDB_SUCCESS or an error.
557 #define ntdb_check(ntdb, check, data) \
558 ntdb_check_((ntdb), typesafe_cb_preargs(enum NTDB_ERROR, void *, \
559 (check), (data), \
560 NTDB_DATA, \
561 NTDB_DATA), \
562 (data))
564 enum NTDB_ERROR ntdb_check_(struct ntdb_context *ntdb,
565 enum NTDB_ERROR (*check)(NTDB_DATA k,
566 NTDB_DATA d,
567 void *data),
568 void *data);
571 * enum ntdb_summary_flags - flags for ntdb_summary.
573 enum ntdb_summary_flags {
574 NTDB_SUMMARY_HISTOGRAMS = 1 /* Draw graphs in the summary. */
578 * ntdb_summary - return a string describing the NTDB state
579 * @ntdb: the ntdb context returned from ntdb_open()
580 * @flags: flags to control the summary output.
581 * @summary: pointer to string to allocate.
583 * This returns a developer-readable string describing the overall
584 * state of the ntdb, such as the percentage used and sizes of records.
585 * It is designed to provide information about the ntdb at a glance
586 * without displaying any keys or data in the database.
588 * On success, sets @summary to point to a malloc()'ed nul-terminated
589 * multi-line string. It is your responsibility to free() it.
591 enum NTDB_ERROR ntdb_summary(struct ntdb_context *ntdb,
592 enum ntdb_summary_flags flags,
593 char **summary);
597 * ntdb_get_flags - return the flags for a ntdb
598 * @ntdb: the ntdb context returned from ntdb_open()
600 * This returns the flags on the current ntdb. Some of these are caused by
601 * the flags argument to ntdb_open(), others (such as NTDB_CONVERT) are
602 * intuited.
604 unsigned int ntdb_get_flags(struct ntdb_context *ntdb);
607 * ntdb_add_flag - set a flag for a ntdb
608 * @ntdb: the ntdb context returned from ntdb_open()
609 * @flag: one of NTDB_NOLOCK, NTDB_NOMMAP, NTDB_NOSYNC or NTDB_ALLOW_NESTING.
611 * You can use this to set a flag on the NTDB. You cannot set these flags
612 * on a NTDB_INTERNAL ntdb.
614 void ntdb_add_flag(struct ntdb_context *ntdb, unsigned flag);
617 * ntdb_remove_flag - unset a flag for a ntdb
618 * @ntdb: the ntdb context returned from ntdb_open()
619 * @flag: one of NTDB_NOLOCK, NTDB_NOMMAP, NTDB_NOSYNC or NTDB_ALLOW_NESTING.
621 * You can use this to clear a flag on the NTDB. You cannot clear flags
622 * on a NTDB_INTERNAL ntdb.
624 void ntdb_remove_flag(struct ntdb_context *ntdb, unsigned flag);
627 * enum ntdb_attribute_type - descriminator for union ntdb_attribute.
629 enum ntdb_attribute_type {
630 NTDB_ATTRIBUTE_LOG = 0,
631 NTDB_ATTRIBUTE_HASH = 1,
632 NTDB_ATTRIBUTE_SEED = 2,
633 NTDB_ATTRIBUTE_STATS = 3,
634 NTDB_ATTRIBUTE_OPENHOOK = 4,
635 NTDB_ATTRIBUTE_FLOCK = 5,
636 NTDB_ATTRIBUTE_ALLOCATOR = 6,
637 NTDB_ATTRIBUTE_HASHSIZE = 7
641 * ntdb_get_attribute - get an attribute for an existing ntdb
642 * @ntdb: the ntdb context returned from ntdb_open()
643 * @attr: the union ntdb_attribute to set.
645 * This gets an attribute from a NTDB which has previously been set (or
646 * may return the default values). Set @attr.base.attr to the
647 * attribute type you want get.
649 enum NTDB_ERROR ntdb_get_attribute(struct ntdb_context *ntdb,
650 union ntdb_attribute *attr);
653 * ntdb_set_attribute - set an attribute for an existing ntdb
654 * @ntdb: the ntdb context returned from ntdb_open()
655 * @attr: the union ntdb_attribute to set.
657 * This sets an attribute on a NTDB, overriding any previous attribute
658 * of the same type. It returns NTDB_ERR_EINVAL if the attribute is
659 * unknown or invalid.
661 * Note that NTDB_ATTRIBUTE_HASH, NTDB_ATTRIBUTE_SEED, and
662 * NTDB_ATTRIBUTE_OPENHOOK cannot currently be set after ntdb_open.
664 enum NTDB_ERROR ntdb_set_attribute(struct ntdb_context *ntdb,
665 const union ntdb_attribute *attr);
668 * ntdb_unset_attribute - reset an attribute for an existing ntdb
669 * @ntdb: the ntdb context returned from ntdb_open()
670 * @type: the attribute type to unset.
672 * This unsets an attribute on a NTDB, returning it to the defaults
673 * (where applicable).
675 * Note that it only makes sense for NTDB_ATTRIBUTE_LOG and NTDB_ATTRIBUTE_FLOCK
676 * to be unset.
678 void ntdb_unset_attribute(struct ntdb_context *ntdb,
679 enum ntdb_attribute_type type);
682 * ntdb_name - get the name of a ntdb
683 * @ntdb: the ntdb context returned from ntdb_open()
685 * This returns a copy of the name string, made at ntdb_open() time.
687 * This is mostly useful for logging.
689 const char *ntdb_name(const struct ntdb_context *ntdb);
692 * ntdb_fd - get the file descriptor of a ntdb
693 * @ntdb: the ntdb context returned from ntdb_open()
695 * This returns the file descriptor for the underlying database file, or -1
696 * for NTDB_INTERNAL.
698 int ntdb_fd(const struct ntdb_context *ntdb);
701 * ntdb_foreach - iterate through every open NTDB.
702 * @fn: the function to call for every NTDB
703 * @p: the pointer to hand to @fn
705 * NTDB internally keeps track of all open TDBs; this function allows you to
706 * iterate through them. If @fn returns non-zero, traversal stops.
708 #define ntdb_foreach(fn, p) \
709 ntdb_foreach_(typesafe_cb_preargs(int, void *, (fn), (p), \
710 struct ntdb_context *), (p))
712 void ntdb_foreach_(int (*fn)(struct ntdb_context *, void *), void *p);
715 * struct ntdb_attribute_base - common fields for all ntdb attributes.
717 struct ntdb_attribute_base {
718 enum ntdb_attribute_type attr;
719 union ntdb_attribute *next;
723 * enum ntdb_log_level - log levels for ntdb_attribute_log
724 * @NTDB_LOG_ERROR: used to log unrecoverable errors such as I/O errors
725 * or internal consistency failures.
726 * @NTDB_LOG_USE_ERROR: used to log usage errors such as invalid parameters
727 * or writing to a read-only database.
728 * @NTDB_LOG_WARNING: used for informational messages on issues which
729 * are unusual but handled by NTDB internally, such
730 * as a failure to mmap or failure to open /dev/urandom.
731 * It's also used when ntdb_open() fails without O_CREAT
732 * because a file does not exist.
734 enum ntdb_log_level {
735 NTDB_LOG_ERROR,
736 NTDB_LOG_USE_ERROR,
737 NTDB_LOG_WARNING
741 * struct ntdb_attribute_log - log function attribute
743 * This attribute provides a hook for you to log errors.
745 struct ntdb_attribute_log {
746 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_LOG */
747 void (*fn)(struct ntdb_context *ntdb,
748 enum ntdb_log_level level,
749 enum NTDB_ERROR ecode,
750 const char *message,
751 void *data);
752 void *data;
756 * struct ntdb_attribute_hash - hash function attribute
758 * This attribute allows you to provide an alternative hash function.
759 * This hash function will be handed keys from the database; it will also
760 * be handed the 8-byte NTDB_HASH_MAGIC value for checking the header (the
761 * ntdb_open() will fail if the hash value doesn't match the header).
763 * Note that if your hash function gives different results on
764 * different machine endians, your ntdb will no longer work across
765 * different architectures!
767 struct ntdb_attribute_hash {
768 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_HASH */
769 uint32_t (*fn)(const void *key, size_t len, uint32_t seed,
770 void *data);
771 void *data;
775 * struct ntdb_attribute_seed - hash function seed attribute
777 * The hash function seed is normally taken from /dev/urandom (or equivalent)
778 * but can be set manually here. This is mainly for testing purposes.
780 struct ntdb_attribute_seed {
781 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_SEED */
782 uint64_t seed;
786 * struct ntdb_attribute_stats - ntdb operational statistics
788 * This attribute records statistics of various low-level NTDB operations.
789 * This can be used to assist performance evaluation. This is only
790 * useful for ntdb_get_attribute().
792 * New fields will be added at the end, hence the "size" argument which
793 * indicates how large your structure is: it must be filled in before
794 * calling ntdb_get_attribute(), which will overwrite it with the size
795 * ntdb knows about.
797 struct ntdb_attribute_stats {
798 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_STATS */
799 size_t size; /* = sizeof(struct ntdb_attribute_stats) */
800 uint64_t allocs;
801 uint64_t alloc_subhash;
802 uint64_t alloc_chain;
803 uint64_t alloc_bucket_exact;
804 uint64_t alloc_bucket_max;
805 uint64_t alloc_leftover;
806 uint64_t alloc_coalesce_tried;
807 uint64_t alloc_coalesce_iterate_clash;
808 uint64_t alloc_coalesce_lockfail;
809 uint64_t alloc_coalesce_race;
810 uint64_t alloc_coalesce_succeeded;
811 uint64_t alloc_coalesce_num_merged;
812 uint64_t compares;
813 uint64_t compare_wrong_offsetbits;
814 uint64_t compare_wrong_keylen;
815 uint64_t compare_wrong_rechash;
816 uint64_t compare_wrong_keycmp;
817 uint64_t transactions;
818 uint64_t transaction_cancel;
819 uint64_t transaction_nest;
820 uint64_t transaction_expand_file;
821 uint64_t transaction_read_direct;
822 uint64_t transaction_read_direct_fail;
823 uint64_t transaction_write_direct;
824 uint64_t transaction_write_direct_fail;
825 uint64_t traverses;
826 uint64_t traverse_val_vanished;
827 uint64_t expands;
828 uint64_t frees;
829 uint64_t locks;
830 uint64_t lock_lowlevel;
831 uint64_t lock_nonblock;
832 uint64_t lock_nonblock_fail;
836 * struct ntdb_attribute_openhook - ntdb special effects hook for open
838 * This attribute contains a function to call once we have the OPEN_LOCK
839 * for the ntdb, but before we've examined its contents. If this succeeds,
840 * the ntdb will be populated if it's then zero-length.
842 * This is a hack to allow support for TDB-style TDB_CLEAR_IF_FIRST
843 * behaviour.
845 struct ntdb_attribute_openhook {
846 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_OPENHOOK */
847 enum NTDB_ERROR (*fn)(int fd, void *data);
848 void *data;
852 * struct ntdb_attribute_flock - ntdb special effects hook for file locking
854 * This attribute contains function to call to place locks on a file; it can
855 * be used to support non-blocking operations or lock proxying.
857 * They should return 0 on success, -1 on failure and set errno.
859 * An error will be logged on error if errno is neither EAGAIN nor EINTR
860 * (normally it would only return EAGAIN if waitflag is false, and
861 * loop internally on EINTR).
863 struct ntdb_attribute_flock {
864 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_FLOCK */
865 int (*lock)(int fd,int rw, off_t off, off_t len, bool waitflag, void *);
866 int (*unlock)(int fd, int rw, off_t off, off_t len, void *);
867 void *data;
871 * struct ntdb_attribute_hashsize - ntdb hashsize setting.
873 * This attribute is only settable on ntdb_open; it indicates that we create
874 * a hashtable of the given size, rather than the default.
876 struct ntdb_attribute_hashsize {
877 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_HASHSIZE */
878 uint32_t size;
882 * struct ntdb_attribute_allocator - allocator for ntdb to use.
884 * You can replace malloc/free with your own allocation functions.
885 * The allocator takes an "owner" pointer, which is either NULL (for
886 * the initial struct ntdb_context and struct ntdb_file), or a
887 * previously allocated pointer. This is useful for relationship
888 * tracking, such as the talloc library.
890 * The expand function is realloc, but only ever used to expand an
891 * existing allocation.
893 * Be careful mixing allocators: two ntdb_contexts which have the same file
894 * open will share the same struct ntdb_file. This may be allocated by one
895 * ntdb's allocator, and freed by the other.
897 struct ntdb_attribute_allocator {
898 struct ntdb_attribute_base base; /* .attr = NTDB_ATTRIBUTE_ALLOCATOR */
899 void *(*alloc)(const void *owner, size_t len, void *priv_data);
900 void *(*expand)(void *old, size_t newlen, void *priv_data);
901 void (*free)(void *old, void *priv_data);
902 void *priv_data;
906 * union ntdb_attribute - ntdb attributes.
908 * This represents all the known attributes.
910 * See also:
911 * struct ntdb_attribute_log, struct ntdb_attribute_hash,
912 * struct ntdb_attribute_seed, struct ntdb_attribute_stats,
913 * struct ntdb_attribute_openhook, struct ntdb_attribute_flock,
914 * struct ntdb_attribute_allocator alloc.
916 union ntdb_attribute {
917 struct ntdb_attribute_base base;
918 struct ntdb_attribute_log log;
919 struct ntdb_attribute_hash hash;
920 struct ntdb_attribute_seed seed;
921 struct ntdb_attribute_stats stats;
922 struct ntdb_attribute_openhook openhook;
923 struct ntdb_attribute_flock flock;
924 struct ntdb_attribute_allocator alloc;
925 struct ntdb_attribute_hashsize hashsize;
928 #ifdef __cplusplus
930 #endif
932 #endif /* ntdb.h */