tdb2.h 27 KB

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