tdb.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. Unix SMB/CIFS implementation.
  3. trivial database library
  4. Copyright (C) Andrew Tridgell 1999-2005
  5. Copyright (C) Paul `Rusty' Russell 2000
  6. Copyright (C) Jeremy Allison 2000-2003
  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. #include "tdb_private.h"
  22. TDB_DATA tdb_null;
  23. /*
  24. non-blocking increment of the tdb sequence number if the tdb has been opened using
  25. the TDB_SEQNUM flag
  26. */
  27. void tdb_increment_seqnum_nonblock(struct tdb_context *tdb)
  28. {
  29. tdb_off_t seqnum=0;
  30. if (!(tdb->flags & TDB_SEQNUM)) {
  31. return;
  32. }
  33. /* we ignore errors from this, as we have no sane way of
  34. dealing with them.
  35. */
  36. tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
  37. seqnum++;
  38. tdb_ofs_write(tdb, TDB_SEQNUM_OFS, &seqnum);
  39. }
  40. /*
  41. increment the tdb sequence number if the tdb has been opened using
  42. the TDB_SEQNUM flag
  43. */
  44. static void tdb_increment_seqnum(struct tdb_context *tdb)
  45. {
  46. if (!(tdb->flags & TDB_SEQNUM)) {
  47. return;
  48. }
  49. if (tdb_nest_lock(tdb, TDB_SEQNUM_OFS, F_WRLCK,
  50. TDB_LOCK_WAIT|TDB_LOCK_PROBE) != 0) {
  51. return;
  52. }
  53. tdb_increment_seqnum_nonblock(tdb);
  54. tdb_nest_unlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, false);
  55. }
  56. static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
  57. {
  58. return memcmp(data.dptr, key.dptr, data.dsize);
  59. }
  60. /* Returns 0 on fail. On success, return offset of record, and fills
  61. in rec */
  62. static tdb_off_t tdb_find(struct tdb_context *tdb, TDB_DATA key, uint32_t hash,
  63. struct tdb_record *r)
  64. {
  65. tdb_off_t rec_ptr;
  66. /* read in the hash top */
  67. if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
  68. return 0;
  69. /* keep looking until we find the right record */
  70. while (rec_ptr) {
  71. if (tdb_rec_read(tdb, rec_ptr, r) == -1)
  72. return 0;
  73. if (!TDB_DEAD(r) && hash==r->full_hash
  74. && key.dsize==r->key_len
  75. && tdb_parse_data(tdb, key, rec_ptr + sizeof(*r),
  76. r->key_len, tdb_key_compare,
  77. NULL) == 0) {
  78. return rec_ptr;
  79. }
  80. /* detect tight infinite loop */
  81. if (rec_ptr == r->next) {
  82. tdb->ecode = TDB_ERR_CORRUPT;
  83. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_find: loop detected.\n"));
  84. return 0;
  85. }
  86. rec_ptr = r->next;
  87. }
  88. tdb->ecode = TDB_ERR_NOEXIST;
  89. return 0;
  90. }
  91. /* As tdb_find, but if you succeed, keep the lock */
  92. tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
  93. struct tdb_record *rec)
  94. {
  95. uint32_t rec_ptr;
  96. if (tdb_lock(tdb, BUCKET(hash), locktype) == -1)
  97. return 0;
  98. if (!(rec_ptr = tdb_find(tdb, key, hash, rec)))
  99. tdb_unlock(tdb, BUCKET(hash), locktype);
  100. return rec_ptr;
  101. }
  102. static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
  103. /* update an entry in place - this only works if the new data size
  104. is <= the old data size and the key exists.
  105. on failure return -1.
  106. */
  107. static int tdb_update_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, TDB_DATA dbuf)
  108. {
  109. struct tdb_record rec;
  110. tdb_off_t rec_ptr;
  111. /* find entry */
  112. if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
  113. return -1;
  114. /* it could be an exact duplicate of what is there - this is
  115. * surprisingly common (eg. with a ldb re-index). */
  116. if (rec.key_len == key.dsize &&
  117. rec.data_len == dbuf.dsize &&
  118. rec.full_hash == hash) {
  119. TDB_DATA data = _tdb_fetch(tdb, key);
  120. if (data.dsize == dbuf.dsize &&
  121. memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
  122. if (data.dptr) {
  123. free(data.dptr);
  124. }
  125. return 0;
  126. }
  127. if (data.dptr) {
  128. free(data.dptr);
  129. }
  130. }
  131. /* must be long enough key, data and tailer */
  132. if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
  133. tdb->ecode = TDB_SUCCESS; /* Not really an error */
  134. return -1;
  135. }
  136. if (tdb->methods->tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len,
  137. dbuf.dptr, dbuf.dsize) == -1)
  138. return -1;
  139. if (dbuf.dsize != rec.data_len) {
  140. /* update size */
  141. rec.data_len = dbuf.dsize;
  142. return tdb_rec_write(tdb, rec_ptr, &rec);
  143. }
  144. return 0;
  145. }
  146. /* find an entry in the database given a key */
  147. /* If an entry doesn't exist tdb_err will be set to
  148. * TDB_ERR_NOEXIST. If a key has no data attached
  149. * then the TDB_DATA will have zero length but
  150. * a non-zero pointer
  151. */
  152. static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
  153. {
  154. tdb_off_t rec_ptr;
  155. struct tdb_record rec;
  156. TDB_DATA ret;
  157. uint32_t hash;
  158. /* find which hash bucket it is in */
  159. hash = tdb->hash_fn(&key);
  160. if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
  161. return tdb_null;
  162. ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
  163. rec.data_len);
  164. ret.dsize = rec.data_len;
  165. tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
  166. return ret;
  167. }
  168. TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key)
  169. {
  170. TDB_DATA ret = _tdb_fetch(tdb, key);
  171. tdb_trace_1rec_retrec(tdb, "tdb_fetch", key, ret);
  172. return ret;
  173. }
  174. /*
  175. * Find an entry in the database and hand the record's data to a parsing
  176. * function. The parsing function is executed under the chain read lock, so it
  177. * should be fast and should not block on other syscalls.
  178. *
  179. * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
  180. *
  181. * For mmapped tdb's that do not have a transaction open it points the parsing
  182. * function directly at the mmap area, it avoids the malloc/memcpy in this
  183. * case. If a transaction is open or no mmap is available, it has to do
  184. * malloc/read/parse/free.
  185. *
  186. * This is interesting for all readers of potentially large data structures in
  187. * the tdb records, ldb indexes being one example.
  188. */
  189. int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
  190. int (*parser)(TDB_DATA key, TDB_DATA data,
  191. void *private_data),
  192. void *private_data)
  193. {
  194. tdb_off_t rec_ptr;
  195. struct tdb_record rec;
  196. int ret;
  197. uint32_t hash;
  198. /* find which hash bucket it is in */
  199. hash = tdb->hash_fn(&key);
  200. if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
  201. tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, -1);
  202. tdb->ecode = TDB_ERR_NOEXIST;
  203. return 0;
  204. }
  205. tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, 0);
  206. ret = tdb_parse_data(tdb, key, rec_ptr + sizeof(rec) + rec.key_len,
  207. rec.data_len, parser, private_data);
  208. tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
  209. return ret;
  210. }
  211. /* check if an entry in the database exists
  212. note that 1 is returned if the key is found and 0 is returned if not found
  213. this doesn't match the conventions in the rest of this module, but is
  214. compatible with gdbm
  215. */
  216. static int tdb_exists_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
  217. {
  218. struct tdb_record rec;
  219. if (tdb_find_lock_hash(tdb, key, hash, F_RDLCK, &rec) == 0)
  220. return 0;
  221. tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
  222. return 1;
  223. }
  224. int tdb_exists(struct tdb_context *tdb, TDB_DATA key)
  225. {
  226. uint32_t hash = tdb->hash_fn(&key);
  227. int ret;
  228. ret = tdb_exists_hash(tdb, key, hash);
  229. tdb_trace_1rec_ret(tdb, "tdb_exists", key, ret);
  230. return ret;
  231. }
  232. /* actually delete an entry in the database given the offset */
  233. int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct tdb_record *rec)
  234. {
  235. tdb_off_t last_ptr, i;
  236. struct tdb_record lastrec;
  237. if (tdb->read_only || tdb->traverse_read) return -1;
  238. if (((tdb->traverse_write != 0) && (!TDB_DEAD(rec))) ||
  239. tdb_write_lock_record(tdb, rec_ptr) == -1) {
  240. /* Someone traversing here: mark it as dead */
  241. rec->magic = TDB_DEAD_MAGIC;
  242. return tdb_rec_write(tdb, rec_ptr, rec);
  243. }
  244. if (tdb_write_unlock_record(tdb, rec_ptr) != 0)
  245. return -1;
  246. /* find previous record in hash chain */
  247. if (tdb_ofs_read(tdb, TDB_HASH_TOP(rec->full_hash), &i) == -1)
  248. return -1;
  249. for (last_ptr = 0; i != rec_ptr; last_ptr = i, i = lastrec.next)
  250. if (tdb_rec_read(tdb, i, &lastrec) == -1)
  251. return -1;
  252. /* unlink it: next ptr is at start of record. */
  253. if (last_ptr == 0)
  254. last_ptr = TDB_HASH_TOP(rec->full_hash);
  255. if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1)
  256. return -1;
  257. /* recover the space */
  258. if (tdb_free(tdb, rec_ptr, rec) == -1)
  259. return -1;
  260. return 0;
  261. }
  262. static int tdb_count_dead(struct tdb_context *tdb, uint32_t hash)
  263. {
  264. int res = 0;
  265. tdb_off_t rec_ptr;
  266. struct tdb_record rec;
  267. /* read in the hash top */
  268. if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
  269. return 0;
  270. while (rec_ptr) {
  271. if (tdb_rec_read(tdb, rec_ptr, &rec) == -1)
  272. return 0;
  273. if (rec.magic == TDB_DEAD_MAGIC) {
  274. res += 1;
  275. }
  276. rec_ptr = rec.next;
  277. }
  278. return res;
  279. }
  280. /*
  281. * Purge all DEAD records from a hash chain
  282. */
  283. static int tdb_purge_dead(struct tdb_context *tdb, uint32_t hash)
  284. {
  285. int res = -1;
  286. struct tdb_record rec;
  287. tdb_off_t rec_ptr;
  288. if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
  289. return -1;
  290. }
  291. /* read in the hash top */
  292. if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
  293. goto fail;
  294. while (rec_ptr) {
  295. tdb_off_t next;
  296. if (tdb_rec_read(tdb, rec_ptr, &rec) == -1) {
  297. goto fail;
  298. }
  299. next = rec.next;
  300. if (rec.magic == TDB_DEAD_MAGIC
  301. && tdb_do_delete(tdb, rec_ptr, &rec) == -1) {
  302. goto fail;
  303. }
  304. rec_ptr = next;
  305. }
  306. res = 0;
  307. fail:
  308. tdb_unlock(tdb, -1, F_WRLCK);
  309. return res;
  310. }
  311. /* delete an entry in the database given a key */
  312. static int tdb_delete_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash)
  313. {
  314. tdb_off_t rec_ptr;
  315. struct tdb_record rec;
  316. int ret;
  317. if (tdb->max_dead_records != 0) {
  318. /*
  319. * Allow for some dead records per hash chain, mainly for
  320. * tdb's with a very high create/delete rate like locking.tdb.
  321. */
  322. if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
  323. return -1;
  324. if (tdb_count_dead(tdb, hash) >= tdb->max_dead_records) {
  325. /*
  326. * Don't let the per-chain freelist grow too large,
  327. * delete all existing dead records
  328. */
  329. tdb_purge_dead(tdb, hash);
  330. }
  331. if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) {
  332. tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
  333. return -1;
  334. }
  335. /*
  336. * Just mark the record as dead.
  337. */
  338. rec.magic = TDB_DEAD_MAGIC;
  339. ret = tdb_rec_write(tdb, rec_ptr, &rec);
  340. }
  341. else {
  342. if (!(rec_ptr = tdb_find_lock_hash(tdb, key, hash, F_WRLCK,
  343. &rec)))
  344. return -1;
  345. ret = tdb_do_delete(tdb, rec_ptr, &rec);
  346. }
  347. if (ret == 0) {
  348. tdb_increment_seqnum(tdb);
  349. }
  350. if (tdb_unlock(tdb, BUCKET(rec.full_hash), F_WRLCK) != 0)
  351. TDB_LOG((tdb, TDB_DEBUG_WARNING, "tdb_delete: WARNING tdb_unlock failed!\n"));
  352. return ret;
  353. }
  354. int tdb_delete(struct tdb_context *tdb, TDB_DATA key)
  355. {
  356. uint32_t hash = tdb->hash_fn(&key);
  357. int ret;
  358. ret = tdb_delete_hash(tdb, key, hash);
  359. tdb_trace_1rec_ret(tdb, "tdb_delete", key, ret);
  360. return ret;
  361. }
  362. /*
  363. * See if we have a dead record around with enough space
  364. */
  365. static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash,
  366. struct tdb_record *r, tdb_len_t length)
  367. {
  368. tdb_off_t rec_ptr;
  369. /* read in the hash top */
  370. if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1)
  371. return 0;
  372. /* keep looking until we find the right record */
  373. while (rec_ptr) {
  374. if (tdb_rec_read(tdb, rec_ptr, r) == -1)
  375. return 0;
  376. if (TDB_DEAD(r) && r->rec_len >= length) {
  377. /*
  378. * First fit for simple coding, TODO: change to best
  379. * fit
  380. */
  381. return rec_ptr;
  382. }
  383. rec_ptr = r->next;
  384. }
  385. return 0;
  386. }
  387. static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
  388. TDB_DATA dbuf, int flag, uint32_t hash)
  389. {
  390. struct tdb_record rec;
  391. tdb_off_t rec_ptr;
  392. char *p = NULL;
  393. int ret = -1;
  394. /* check for it existing, on insert. */
  395. if (flag == TDB_INSERT) {
  396. if (tdb_exists_hash(tdb, key, hash)) {
  397. tdb->ecode = TDB_ERR_EXISTS;
  398. goto fail;
  399. }
  400. } else {
  401. /* first try in-place update, on modify or replace. */
  402. if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {
  403. goto done;
  404. }
  405. if (tdb->ecode == TDB_ERR_NOEXIST &&
  406. flag == TDB_MODIFY) {
  407. /* if the record doesn't exist and we are in TDB_MODIFY mode then
  408. we should fail the store */
  409. goto fail;
  410. }
  411. }
  412. /* reset the error code potentially set by the tdb_update() */
  413. tdb->ecode = TDB_SUCCESS;
  414. /* delete any existing record - if it doesn't exist we don't
  415. care. Doing this first reduces fragmentation, and avoids
  416. coalescing with `allocated' block before it's updated. */
  417. if (flag != TDB_INSERT)
  418. tdb_delete_hash(tdb, key, hash);
  419. /* Copy key+value *before* allocating free space in case malloc
  420. fails and we are left with a dead spot in the tdb. */
  421. if (!(p = (char *)malloc(key.dsize + dbuf.dsize))) {
  422. tdb->ecode = TDB_ERR_OOM;
  423. goto fail;
  424. }
  425. memcpy(p, key.dptr, key.dsize);
  426. if (dbuf.dsize)
  427. memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);
  428. if (tdb->max_dead_records != 0) {
  429. /*
  430. * Allow for some dead records per hash chain, look if we can
  431. * find one that can hold the new record. We need enough space
  432. * for key, data and tailer. If we find one, we don't have to
  433. * consult the central freelist.
  434. */
  435. rec_ptr = tdb_find_dead(
  436. tdb, hash, &rec,
  437. key.dsize + dbuf.dsize + sizeof(tdb_off_t));
  438. if (rec_ptr != 0) {
  439. rec.key_len = key.dsize;
  440. rec.data_len = dbuf.dsize;
  441. rec.full_hash = hash;
  442. rec.magic = TDB_MAGIC;
  443. if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
  444. || tdb->methods->tdb_write(
  445. tdb, rec_ptr + sizeof(rec),
  446. p, key.dsize + dbuf.dsize) == -1) {
  447. goto fail;
  448. }
  449. goto done;
  450. }
  451. }
  452. /*
  453. * We have to allocate some space from the freelist, so this means we
  454. * have to lock it. Use the chance to purge all the DEAD records from
  455. * the hash chain under the freelist lock.
  456. */
  457. if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
  458. goto fail;
  459. }
  460. if ((tdb->max_dead_records != 0)
  461. && (tdb_purge_dead(tdb, hash) == -1)) {
  462. tdb_unlock(tdb, -1, F_WRLCK);
  463. goto fail;
  464. }
  465. /* we have to allocate some space */
  466. rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec);
  467. tdb_unlock(tdb, -1, F_WRLCK);
  468. if (rec_ptr == 0) {
  469. goto fail;
  470. }
  471. /* Read hash top into next ptr */
  472. if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)
  473. goto fail;
  474. rec.key_len = key.dsize;
  475. rec.data_len = dbuf.dsize;
  476. rec.full_hash = hash;
  477. rec.magic = TDB_MAGIC;
  478. /* write out and point the top of the hash chain at it */
  479. if (tdb_rec_write(tdb, rec_ptr, &rec) == -1
  480. || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1
  481. || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {
  482. /* Need to tdb_unallocate() here */
  483. goto fail;
  484. }
  485. done:
  486. ret = 0;
  487. fail:
  488. if (ret == 0) {
  489. tdb_increment_seqnum(tdb);
  490. }
  491. SAFE_FREE(p);
  492. return ret;
  493. }
  494. /* store an element in the database, replacing any existing element
  495. with the same key
  496. return 0 on success, -1 on failure
  497. */
  498. int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag)
  499. {
  500. uint32_t hash;
  501. int ret;
  502. if (tdb->read_only || tdb->traverse_read) {
  503. tdb->ecode = TDB_ERR_RDONLY;
  504. tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, -1);
  505. return -1;
  506. }
  507. /* find which hash bucket it is in */
  508. hash = tdb->hash_fn(&key);
  509. if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
  510. return -1;
  511. ret = _tdb_store(tdb, key, dbuf, flag, hash);
  512. tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, ret);
  513. tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
  514. return ret;
  515. }
  516. /* Append to an entry. Create if not exist. */
  517. int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
  518. {
  519. uint32_t hash;
  520. TDB_DATA dbuf;
  521. int ret = -1;
  522. /* find which hash bucket it is in */
  523. hash = tdb->hash_fn(&key);
  524. if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
  525. return -1;
  526. dbuf = _tdb_fetch(tdb, key);
  527. if (dbuf.dptr == NULL) {
  528. dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);
  529. } else {
  530. unsigned int new_len = dbuf.dsize + new_dbuf.dsize;
  531. unsigned char *new_dptr;
  532. /* realloc '0' is special: don't do that. */
  533. if (new_len == 0)
  534. new_len = 1;
  535. new_dptr = (unsigned char *)realloc(dbuf.dptr, new_len);
  536. if (new_dptr == NULL) {
  537. free(dbuf.dptr);
  538. }
  539. dbuf.dptr = new_dptr;
  540. }
  541. if (dbuf.dptr == NULL) {
  542. tdb->ecode = TDB_ERR_OOM;
  543. goto failed;
  544. }
  545. memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
  546. dbuf.dsize += new_dbuf.dsize;
  547. ret = _tdb_store(tdb, key, dbuf, 0, hash);
  548. tdb_trace_2rec_retrec(tdb, "tdb_append", key, new_dbuf, dbuf);
  549. failed:
  550. tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
  551. SAFE_FREE(dbuf.dptr);
  552. return ret;
  553. }
  554. /*
  555. return the name of the current tdb file
  556. useful for external logging functions
  557. */
  558. const char *tdb_name(struct tdb_context *tdb)
  559. {
  560. return tdb->name;
  561. }
  562. /*
  563. return the underlying file descriptor being used by tdb, or -1
  564. useful for external routines that want to check the device/inode
  565. of the fd
  566. */
  567. int tdb_fd(struct tdb_context *tdb)
  568. {
  569. return tdb->fd;
  570. }
  571. /*
  572. return the current logging function
  573. useful for external tdb routines that wish to log tdb errors
  574. */
  575. tdb_log_func tdb_log_fn(struct tdb_context *tdb)
  576. {
  577. return tdb->log.log_fn;
  578. }
  579. /*
  580. get the tdb sequence number. Only makes sense if the writers opened
  581. with TDB_SEQNUM set. Note that this sequence number will wrap quite
  582. quickly, so it should only be used for a 'has something changed'
  583. test, not for code that relies on the count of the number of changes
  584. made. If you want a counter then use a tdb record.
  585. The aim of this sequence number is to allow for a very lightweight
  586. test of a possible tdb change.
  587. */
  588. int tdb_get_seqnum(struct tdb_context *tdb)
  589. {
  590. tdb_off_t seqnum=0;
  591. tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
  592. return seqnum;
  593. }
  594. int tdb_hash_size(struct tdb_context *tdb)
  595. {
  596. return tdb->header.hash_size;
  597. }
  598. size_t tdb_map_size(struct tdb_context *tdb)
  599. {
  600. return tdb->map_size;
  601. }
  602. int tdb_get_flags(struct tdb_context *tdb)
  603. {
  604. return tdb->flags;
  605. }
  606. void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
  607. {
  608. if ((flags & TDB_ALLOW_NESTING) &&
  609. (flags & TDB_DISALLOW_NESTING)) {
  610. tdb->ecode = TDB_ERR_NESTING;
  611. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_add_flags: "
  612. "allow_nesting and disallow_nesting are not allowed together!"));
  613. return;
  614. }
  615. if (flags & TDB_ALLOW_NESTING) {
  616. tdb->flags &= ~TDB_DISALLOW_NESTING;
  617. }
  618. if (flags & TDB_DISALLOW_NESTING) {
  619. tdb->flags &= ~TDB_ALLOW_NESTING;
  620. }
  621. tdb->flags |= flags;
  622. }
  623. void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
  624. {
  625. if ((flags & TDB_ALLOW_NESTING) &&
  626. (flags & TDB_DISALLOW_NESTING)) {
  627. tdb->ecode = TDB_ERR_NESTING;
  628. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_remove_flags: "
  629. "allow_nesting and disallow_nesting are not allowed together!"));
  630. return;
  631. }
  632. if (flags & TDB_ALLOW_NESTING) {
  633. tdb->flags |= TDB_DISALLOW_NESTING;
  634. }
  635. if (flags & TDB_DISALLOW_NESTING) {
  636. tdb->flags |= TDB_ALLOW_NESTING;
  637. }
  638. tdb->flags &= ~flags;
  639. }
  640. /*
  641. enable sequence number handling on an open tdb
  642. */
  643. void tdb_enable_seqnum(struct tdb_context *tdb)
  644. {
  645. tdb->flags |= TDB_SEQNUM;
  646. }
  647. /*
  648. add a region of the file to the freelist. Length is the size of the region in bytes,
  649. which includes the free list header that needs to be added
  650. */
  651. static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length)
  652. {
  653. struct tdb_record rec;
  654. if (length <= sizeof(rec)) {
  655. /* the region is not worth adding */
  656. return 0;
  657. }
  658. if (length + offset > tdb->map_size) {
  659. TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: adding region beyond end of file\n"));
  660. return -1;
  661. }
  662. memset(&rec,'\0',sizeof(rec));
  663. rec.rec_len = length - sizeof(rec);
  664. if (tdb_free(tdb, offset, &rec) == -1) {
  665. TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: failed to add free record\n"));
  666. return -1;
  667. }
  668. return 0;
  669. }
  670. /*
  671. wipe the entire database, deleting all records. This can be done
  672. very fast by using a allrecord lock. The entire data portion of the
  673. file becomes a single entry in the freelist.
  674. This code carefully steps around the recovery area, leaving it alone
  675. */
  676. int tdb_wipe_all(struct tdb_context *tdb)
  677. {
  678. int i;
  679. tdb_off_t offset = 0;
  680. ssize_t data_len;
  681. tdb_off_t recovery_head;
  682. tdb_len_t recovery_size = 0;
  683. if (tdb_lockall(tdb) != 0) {
  684. return -1;
  685. }
  686. tdb_trace(tdb, "tdb_wipe_all");
  687. /* see if the tdb has a recovery area, and remember its size
  688. if so. We don't want to lose this as otherwise each
  689. tdb_wipe_all() in a transaction will increase the size of
  690. the tdb by the size of the recovery area */
  691. if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
  692. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery head\n"));
  693. goto failed;
  694. }
  695. if (recovery_head != 0) {
  696. struct tdb_record rec;
  697. if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
  698. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));
  699. return -1;
  700. }
  701. recovery_size = rec.rec_len + sizeof(rec);
  702. }
  703. /* wipe the hashes */
  704. for (i=0;i<tdb->header.hash_size;i++) {
  705. if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {
  706. TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));
  707. goto failed;
  708. }
  709. }
  710. /* wipe the freelist */
  711. if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
  712. TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));
  713. goto failed;
  714. }
  715. /* add all the rest of the file to the freelist, possibly leaving a gap
  716. for the recovery area */
  717. if (recovery_size == 0) {
  718. /* the simple case - the whole file can be used as a freelist */
  719. data_len = (tdb->map_size - TDB_DATA_START(tdb->header.hash_size));
  720. if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
  721. goto failed;
  722. }
  723. } else {
  724. /* we need to add two freelist entries - one on either
  725. side of the recovery area
  726. Note that we cannot shift the recovery area during
  727. this operation. Only the transaction.c code may
  728. move the recovery area or we risk subtle data
  729. corruption
  730. */
  731. data_len = (recovery_head - TDB_DATA_START(tdb->header.hash_size));
  732. if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {
  733. goto failed;
  734. }
  735. /* and the 2nd free list entry after the recovery area - if any */
  736. data_len = tdb->map_size - (recovery_head+recovery_size);
  737. if (tdb_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {
  738. goto failed;
  739. }
  740. }
  741. if (tdb_unlockall(tdb) != 0) {
  742. TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));
  743. goto failed;
  744. }
  745. return 0;
  746. failed:
  747. tdb_unlockall(tdb);
  748. return -1;
  749. }
  750. struct traverse_state {
  751. bool error;
  752. struct tdb_context *dest_db;
  753. };
  754. /*
  755. traverse function for repacking
  756. */
  757. static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
  758. {
  759. struct traverse_state *state = (struct traverse_state *)private_data;
  760. if (tdb_store(state->dest_db, key, data, TDB_INSERT) != 0) {
  761. state->error = true;
  762. return -1;
  763. }
  764. return 0;
  765. }
  766. /*
  767. repack a tdb
  768. */
  769. int tdb_repack(struct tdb_context *tdb)
  770. {
  771. struct tdb_context *tmp_db;
  772. struct traverse_state state;
  773. tdb_trace(tdb, "tdb_repack");
  774. if (tdb_transaction_start(tdb) != 0) {
  775. TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
  776. return -1;
  777. }
  778. tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0);
  779. if (tmp_db == NULL) {
  780. TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to create tmp_db\n"));
  781. tdb_transaction_cancel(tdb);
  782. return -1;
  783. }
  784. state.error = false;
  785. state.dest_db = tmp_db;
  786. if (tdb_traverse_read(tdb, repack_traverse, &state) == -1) {
  787. TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying out\n"));
  788. tdb_transaction_cancel(tdb);
  789. tdb_close(tmp_db);
  790. return -1;
  791. }
  792. if (state.error) {
  793. TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during traversal\n"));
  794. tdb_transaction_cancel(tdb);
  795. tdb_close(tmp_db);
  796. return -1;
  797. }
  798. if (tdb_wipe_all(tdb) != 0) {
  799. TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to wipe database\n"));
  800. tdb_transaction_cancel(tdb);
  801. tdb_close(tmp_db);
  802. return -1;
  803. }
  804. state.error = false;
  805. state.dest_db = tdb;
  806. if (tdb_traverse_read(tmp_db, repack_traverse, &state) == -1) {
  807. TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying back\n"));
  808. tdb_transaction_cancel(tdb);
  809. tdb_close(tmp_db);
  810. return -1;
  811. }
  812. if (state.error) {
  813. TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during second traversal\n"));
  814. tdb_transaction_cancel(tdb);
  815. tdb_close(tmp_db);
  816. return -1;
  817. }
  818. tdb_close(tmp_db);
  819. if (tdb_transaction_commit(tdb) != 0) {
  820. TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to commit\n"));
  821. return -1;
  822. }
  823. return 0;
  824. }
  825. #ifdef TDB_TRACE
  826. static void tdb_trace_write(struct tdb_context *tdb, const char *str)
  827. {
  828. if (write(tdb->tracefd, str, strlen(str)) != strlen(str)) {
  829. close(tdb->tracefd);
  830. tdb->tracefd = -1;
  831. }
  832. }
  833. static void tdb_trace_start(struct tdb_context *tdb)
  834. {
  835. tdb_off_t seqnum=0;
  836. char msg[sizeof(tdb_off_t) * 4 + 1];
  837. tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
  838. snprintf(msg, sizeof(msg), "%u ", seqnum);
  839. tdb_trace_write(tdb, msg);
  840. }
  841. static void tdb_trace_end(struct tdb_context *tdb)
  842. {
  843. tdb_trace_write(tdb, "\n");
  844. }
  845. static void tdb_trace_end_ret(struct tdb_context *tdb, int ret)
  846. {
  847. char msg[sizeof(ret) * 4 + 4];
  848. snprintf(msg, sizeof(msg), " = %i\n", ret);
  849. tdb_trace_write(tdb, msg);
  850. }
  851. static void tdb_trace_record(struct tdb_context *tdb, TDB_DATA rec)
  852. {
  853. char msg[20 + rec.dsize*2], *p;
  854. unsigned int i;
  855. /* We differentiate zero-length records from non-existent ones. */
  856. if (rec.dptr == NULL) {
  857. tdb_trace_write(tdb, " NULL");
  858. return;
  859. }
  860. /* snprintf here is purely cargo-cult programming. */
  861. p = msg;
  862. p += snprintf(p, sizeof(msg), " %zu:", rec.dsize);
  863. for (i = 0; i < rec.dsize; i++)
  864. p += snprintf(p, 2, "%02x", rec.dptr[i]);
  865. tdb_trace_write(tdb, msg);
  866. }
  867. void tdb_trace(struct tdb_context *tdb, const char *op)
  868. {
  869. tdb_trace_start(tdb);
  870. tdb_trace_write(tdb, op);
  871. tdb_trace_end(tdb);
  872. }
  873. void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op)
  874. {
  875. char msg[sizeof(tdb_off_t) * 4 + 1];
  876. snprintf(msg, sizeof(msg), "%u ", seqnum);
  877. tdb_trace_write(tdb, msg);
  878. tdb_trace_write(tdb, op);
  879. tdb_trace_end(tdb);
  880. }
  881. void tdb_trace_open(struct tdb_context *tdb, const char *op,
  882. unsigned hash_size, unsigned tdb_flags, unsigned open_flags)
  883. {
  884. char msg[128];
  885. snprintf(msg, sizeof(msg),
  886. "%s %u 0x%x 0x%x", op, hash_size, tdb_flags, open_flags);
  887. tdb_trace_start(tdb);
  888. tdb_trace_write(tdb, msg);
  889. tdb_trace_end(tdb);
  890. }
  891. void tdb_trace_ret(struct tdb_context *tdb, const char *op, int ret)
  892. {
  893. tdb_trace_start(tdb);
  894. tdb_trace_write(tdb, op);
  895. tdb_trace_end_ret(tdb, ret);
  896. }
  897. void tdb_trace_retrec(struct tdb_context *tdb, const char *op, TDB_DATA ret)
  898. {
  899. tdb_trace_start(tdb);
  900. tdb_trace_write(tdb, op);
  901. tdb_trace_write(tdb, " =");
  902. tdb_trace_record(tdb, ret);
  903. tdb_trace_end(tdb);
  904. }
  905. void tdb_trace_1rec(struct tdb_context *tdb, const char *op,
  906. TDB_DATA rec)
  907. {
  908. tdb_trace_start(tdb);
  909. tdb_trace_write(tdb, op);
  910. tdb_trace_record(tdb, rec);
  911. tdb_trace_end(tdb);
  912. }
  913. void tdb_trace_1rec_ret(struct tdb_context *tdb, const char *op,
  914. TDB_DATA rec, int ret)
  915. {
  916. tdb_trace_start(tdb);
  917. tdb_trace_write(tdb, op);
  918. tdb_trace_record(tdb, rec);
  919. tdb_trace_end_ret(tdb, ret);
  920. }
  921. void tdb_trace_1rec_retrec(struct tdb_context *tdb, const char *op,
  922. TDB_DATA rec, TDB_DATA ret)
  923. {
  924. tdb_trace_start(tdb);
  925. tdb_trace_write(tdb, op);
  926. tdb_trace_record(tdb, rec);
  927. tdb_trace_write(tdb, " =");
  928. tdb_trace_record(tdb, ret);
  929. tdb_trace_end(tdb);
  930. }
  931. void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op,
  932. TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
  933. int ret)
  934. {
  935. char msg[1 + sizeof(ret) * 4];
  936. snprintf(msg, sizeof(msg), " %#x", flag);
  937. tdb_trace_start(tdb);
  938. tdb_trace_write(tdb, op);
  939. tdb_trace_record(tdb, rec1);
  940. tdb_trace_record(tdb, rec2);
  941. tdb_trace_write(tdb, msg);
  942. tdb_trace_end_ret(tdb, ret);
  943. }
  944. void tdb_trace_2rec_retrec(struct tdb_context *tdb, const char *op,
  945. TDB_DATA rec1, TDB_DATA rec2, TDB_DATA ret)
  946. {
  947. tdb_trace_start(tdb);
  948. tdb_trace_write(tdb, op);
  949. tdb_trace_record(tdb, rec1);
  950. tdb_trace_record(tdb, rec2);
  951. tdb_trace_write(tdb, " =");
  952. tdb_trace_record(tdb, ret);
  953. tdb_trace_end(tdb);
  954. }
  955. #endif