tdb2.h 28 KB

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