transaction.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. Unix SMB/CIFS implementation.
  3. trivial database library
  4. Copyright (C) Andrew Tridgell 2005
  5. ** NOTE! The following LGPL license applies to the tdb
  6. ** library. This does NOT imply that all of Samba is released
  7. ** under the LGPL
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 3 of the License, or (at your option) any later version.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "tdb_private.h"
  20. /*
  21. transaction design:
  22. - only allow a single transaction at a time per database. This makes
  23. using the transaction API simpler, as otherwise the caller would
  24. have to cope with temporary failures in transactions that conflict
  25. with other current transactions
  26. - keep the transaction recovery information in the same file as the
  27. database, using a special 'transaction recovery' record pointed at
  28. by the header. This removes the need for extra journal files as
  29. used by some other databases
  30. - dynamically allocated the transaction recover record, re-using it
  31. for subsequent transactions. If a larger record is needed then
  32. tdb_free() the old record to place it on the normal tdb freelist
  33. before allocating the new record
  34. - during transactions, keep a linked list of writes all that have
  35. been performed by intercepting all tdb_write() calls. The hooked
  36. transaction versions of tdb_read() and tdb_write() check this
  37. linked list and try to use the elements of the list in preference
  38. to the real database.
  39. - don't allow any locks to be held when a transaction starts,
  40. otherwise we can end up with deadlock (plus lack of lock nesting
  41. in posix locks would mean the lock is lost)
  42. - if the caller gains a lock during the transaction but doesn't
  43. release it then fail the commit
  44. - allow for nested calls to tdb_transaction_start(), re-using the
  45. existing transaction record. If the inner transaction is cancelled
  46. then a subsequent commit will fail
  47. - keep a mirrored copy of the tdb hash chain heads to allow for the
  48. fast hash heads scan on traverse, updating the mirrored copy in
  49. the transaction version of tdb_write
  50. - allow callers to mix transaction and non-transaction use of tdb,
  51. although once a transaction is started then an exclusive lock is
  52. gained until the transaction is committed or cancelled
  53. - the commit stategy involves first saving away all modified data
  54. into a linearised buffer in the transaction recovery area, then
  55. marking the transaction recovery area with a magic value to
  56. indicate a valid recovery record. In total 4 fsync/msync calls are
  57. needed per commit to prevent race conditions. It might be possible
  58. to reduce this to 3 or even 2 with some more work.
  59. - check for a valid recovery record on open of the tdb, while the
  60. open lock is held. Automatically recover from the transaction
  61. recovery area if needed, then continue with the open as
  62. usual. This allows for smooth crash recovery with no administrator
  63. intervention.
  64. - if TDB_NOSYNC is passed to flags in tdb_open then transactions are
  65. still available, but no transaction recovery area is used and no
  66. fsync/msync calls are made.
  67. - if TDB_ALLOW_NESTING is passed to flags in tdb open, or added using
  68. tdb_add_flags() transaction nesting is enabled.
  69. It resets the TDB_DISALLOW_NESTING flag, as both cannot be used together.
  70. The default is that transaction nesting is allowed.
  71. Note: this default may change in future versions of tdb.
  72. Beware. when transactions are nested a transaction successfully
  73. completed with tdb_transaction_commit() can be silently unrolled later.
  74. - if TDB_DISALLOW_NESTING is passed to flags in tdb open, or added using
  75. tdb_add_flags() transaction nesting is disabled.
  76. It resets the TDB_ALLOW_NESTING flag, as both cannot be used together.
  77. An attempt create a nested transaction will fail with TDB_ERR_NESTING.
  78. The default is that transaction nesting is allowed.
  79. Note: this default may change in future versions of tdb.
  80. */
  81. /*
  82. hold the context of any current transaction
  83. */
  84. struct tdb_transaction {
  85. /* we keep a mirrored copy of the tdb hash heads here so
  86. tdb_next_hash_chain() can operate efficiently */
  87. uint32_t *hash_heads;
  88. /* the original io methods - used to do IOs to the real db */
  89. const struct tdb_methods *io_methods;
  90. /* the list of transaction blocks. When a block is first
  91. written to, it gets created in this list */
  92. uint8_t **blocks;
  93. uint32_t num_blocks;
  94. uint32_t block_size; /* bytes in each block */
  95. uint32_t last_block_size; /* number of valid bytes in the last block */
  96. /* non-zero when an internal transaction error has
  97. occurred. All write operations will then fail until the
  98. transaction is ended */
  99. int transaction_error;
  100. /* when inside a transaction we need to keep track of any
  101. nested tdb_transaction_start() calls, as these are allowed,
  102. but don't create a new transaction */
  103. int nesting;
  104. /* set when a prepare has already occurred */
  105. bool prepared;
  106. tdb_off_t magic_offset;
  107. /* set when the OPEN_LOCK has been taken */
  108. bool open_lock_taken;
  109. /* old file size before transaction */
  110. tdb_len_t old_map_size;
  111. /* we should re-pack on commit */
  112. bool need_repack;
  113. };
  114. /*
  115. read while in a transaction. We need to check first if the data is in our list
  116. of transaction elements, then if not do a real read
  117. */
  118. static int transaction_read(struct tdb_context *tdb, tdb_off_t off, void *buf,
  119. tdb_len_t len, int cv)
  120. {
  121. uint32_t blk;
  122. /* break it down into block sized ops */
  123. while (len + (off % tdb->transaction->block_size) > tdb->transaction->block_size) {
  124. tdb_len_t len2 = tdb->transaction->block_size - (off % tdb->transaction->block_size);
  125. if (transaction_read(tdb, off, buf, len2, cv) != 0) {
  126. return -1;
  127. }
  128. len -= len2;
  129. off += len2;
  130. buf = (void *)(len2 + (char *)buf);
  131. }
  132. if (len == 0) {
  133. return 0;
  134. }
  135. blk = off / tdb->transaction->block_size;
  136. /* see if we have it in the block list */
  137. if (tdb->transaction->num_blocks <= blk ||
  138. tdb->transaction->blocks[blk] == NULL) {
  139. /* nope, do a real read */
  140. if (tdb->transaction->io_methods->tdb_read(tdb, off, buf, len, cv) != 0) {
  141. goto fail;
  142. }
  143. return 0;
  144. }
  145. /* it is in the block list. Now check for the last block */
  146. if (blk == tdb->transaction->num_blocks-1) {
  147. if (len > tdb->transaction->last_block_size) {
  148. goto fail;
  149. }
  150. }
  151. /* now copy it out of this block */
  152. memcpy(buf, tdb->transaction->blocks[blk] + (off % tdb->transaction->block_size), len);
  153. if (cv) {
  154. tdb_convert(buf, len);
  155. }
  156. return 0;
  157. fail:
  158. TDB_LOG((tdb, TDB_DEBUG_FATAL, "transaction_read: failed at off=%d len=%d\n", off, len));
  159. tdb->ecode = TDB_ERR_IO;
  160. tdb->transaction->transaction_error = 1;
  161. return -1;
  162. }
  163. /*
  164. write while in a transaction
  165. */
  166. static int transaction_write(struct tdb_context *tdb, tdb_off_t off,
  167. const void *buf, tdb_len_t len)
  168. {
  169. uint32_t blk;
  170. /* Only a commit is allowed on a prepared transaction */
  171. if (tdb->transaction->prepared) {
  172. tdb->ecode = TDB_ERR_EINVAL;
  173. TDB_LOG((tdb, TDB_DEBUG_FATAL, "transaction_write: transaction already prepared, write not allowed\n"));
  174. tdb->transaction->transaction_error = 1;
  175. return -1;
  176. }
  177. /* if the write is to a hash head, then update the transaction
  178. hash heads */
  179. if (len == sizeof(tdb_off_t) && off >= FREELIST_TOP &&
  180. off < FREELIST_TOP+TDB_HASHTABLE_SIZE(tdb)) {
  181. uint32_t chain = (off-FREELIST_TOP) / sizeof(tdb_off_t);
  182. memcpy(&tdb->transaction->hash_heads[chain], buf, len);
  183. }
  184. /* break it up into block sized chunks */
  185. while (len + (off % tdb->transaction->block_size) > tdb->transaction->block_size) {
  186. tdb_len_t len2 = tdb->transaction->block_size - (off % tdb->transaction->block_size);
  187. if (transaction_write(tdb, off, buf, len2) != 0) {
  188. return -1;
  189. }
  190. len -= len2;
  191. off += len2;
  192. if (buf != NULL) {
  193. buf = (const void *)(len2 + (const char *)buf);
  194. }
  195. }
  196. if (len == 0) {
  197. return 0;
  198. }
  199. blk = off / tdb->transaction->block_size;
  200. off = off % tdb->transaction->block_size;
  201. if (tdb->transaction->num_blocks <= blk) {
  202. uint8_t **new_blocks;
  203. /* expand the blocks array */
  204. if (tdb->transaction->blocks == NULL) {
  205. new_blocks = (uint8_t **)malloc(
  206. (blk+1)*sizeof(uint8_t *));
  207. } else {
  208. new_blocks = (uint8_t **)realloc(
  209. tdb->transaction->blocks,
  210. (blk+1)*sizeof(uint8_t *));
  211. }
  212. if (new_blocks == NULL) {
  213. tdb->ecode = TDB_ERR_OOM;
  214. goto fail;
  215. }
  216. memset(&new_blocks[tdb->transaction->num_blocks], 0,
  217. (1+(blk - tdb->transaction->num_blocks))*sizeof(uint8_t *));
  218. tdb->transaction->blocks = new_blocks;
  219. tdb->transaction->num_blocks = blk+1;
  220. tdb->transaction->last_block_size = 0;
  221. }
  222. /* allocate and fill a block? */
  223. if (tdb->transaction->blocks[blk] == NULL) {
  224. tdb->transaction->blocks[blk] = (uint8_t *)calloc(tdb->transaction->block_size, 1);
  225. if (tdb->transaction->blocks[blk] == NULL) {
  226. tdb->ecode = TDB_ERR_OOM;
  227. tdb->transaction->transaction_error = 1;
  228. return -1;
  229. }
  230. if (tdb->transaction->old_map_size > blk * tdb->transaction->block_size) {
  231. tdb_len_t len2 = tdb->transaction->block_size;
  232. if (len2 + (blk * tdb->transaction->block_size) > tdb->transaction->old_map_size) {
  233. len2 = tdb->transaction->old_map_size - (blk * tdb->transaction->block_size);
  234. }
  235. if (tdb->transaction->io_methods->tdb_read(tdb, blk * tdb->transaction->block_size,
  236. tdb->transaction->blocks[blk],
  237. len2, 0) != 0) {
  238. SAFE_FREE(tdb->transaction->blocks[blk]);
  239. tdb->ecode = TDB_ERR_IO;
  240. goto fail;
  241. }
  242. if (blk == tdb->transaction->num_blocks-1) {
  243. tdb->transaction->last_block_size = len2;
  244. }
  245. }
  246. }
  247. /* overwrite part of an existing block */
  248. if (buf == NULL) {
  249. memset(tdb->transaction->blocks[blk] + off, 0, len);
  250. } else {
  251. memcpy(tdb->transaction->blocks[blk] + off, buf, len);
  252. }
  253. if (blk == tdb->transaction->num_blocks-1) {
  254. if (len + off > tdb->transaction->last_block_size) {
  255. tdb->transaction->last_block_size = len + off;
  256. }
  257. }
  258. return 0;
  259. fail:
  260. TDB_LOG((tdb, TDB_DEBUG_FATAL, "transaction_write: failed at off=%d len=%d\n",
  261. (blk*tdb->transaction->block_size) + off, len));
  262. tdb->transaction->transaction_error = 1;
  263. return -1;
  264. }
  265. /*
  266. write while in a transaction - this varient never expands the transaction blocks, it only
  267. updates existing blocks. This means it cannot change the recovery size
  268. */
  269. static int transaction_write_existing(struct tdb_context *tdb, tdb_off_t off,
  270. const void *buf, tdb_len_t len)
  271. {
  272. uint32_t blk;
  273. /* break it up into block sized chunks */
  274. while (len + (off % tdb->transaction->block_size) > tdb->transaction->block_size) {
  275. tdb_len_t len2 = tdb->transaction->block_size - (off % tdb->transaction->block_size);
  276. if (transaction_write_existing(tdb, off, buf, len2) != 0) {
  277. return -1;
  278. }
  279. len -= len2;
  280. off += len2;
  281. if (buf != NULL) {
  282. buf = (const void *)(len2 + (const char *)buf);
  283. }
  284. }
  285. if (len == 0) {
  286. return 0;
  287. }
  288. blk = off / tdb->transaction->block_size;
  289. off = off % tdb->transaction->block_size;
  290. if (tdb->transaction->num_blocks <= blk ||
  291. tdb->transaction->blocks[blk] == NULL) {
  292. return 0;
  293. }
  294. if (blk == tdb->transaction->num_blocks-1 &&
  295. off + len > tdb->transaction->last_block_size) {
  296. if (off >= tdb->transaction->last_block_size) {
  297. return 0;
  298. }
  299. len = tdb->transaction->last_block_size - off;
  300. }
  301. /* overwrite part of an existing block */
  302. memcpy(tdb->transaction->blocks[blk] + off, buf, len);
  303. return 0;
  304. }
  305. /*
  306. accelerated hash chain head search, using the cached hash heads
  307. */
  308. static void transaction_next_hash_chain(struct tdb_context *tdb, uint32_t *chain)
  309. {
  310. uint32_t h = *chain;
  311. for (;h < tdb->header.hash_size;h++) {
  312. /* the +1 takes account of the freelist */
  313. if (0 != tdb->transaction->hash_heads[h+1]) {
  314. break;
  315. }
  316. }
  317. (*chain) = h;
  318. }
  319. /*
  320. out of bounds check during a transaction
  321. */
  322. static int transaction_oob(struct tdb_context *tdb, tdb_off_t len, int probe)
  323. {
  324. if (len <= tdb->map_size) {
  325. return 0;
  326. }
  327. tdb->ecode = TDB_ERR_IO;
  328. return -1;
  329. }
  330. /*
  331. transaction version of tdb_expand().
  332. */
  333. static int transaction_expand_file(struct tdb_context *tdb, tdb_off_t size,
  334. tdb_off_t addition)
  335. {
  336. /* add a write to the transaction elements, so subsequent
  337. reads see the zero data */
  338. if (transaction_write(tdb, size, NULL, addition) != 0) {
  339. return -1;
  340. }
  341. tdb->transaction->need_repack = true;
  342. return 0;
  343. }
  344. /*
  345. brlock during a transaction - ignore them
  346. */
  347. static int transaction_brlock(struct tdb_context *tdb,
  348. int rw_type, tdb_off_t offset, size_t len,
  349. enum tdb_lock_flags flags)
  350. {
  351. return 0;
  352. }
  353. static int transaction_brunlock(struct tdb_context *tdb,
  354. int rw_type, tdb_off_t offset, size_t len)
  355. {
  356. return 0;
  357. }
  358. static const struct tdb_methods transaction_methods = {
  359. transaction_read,
  360. transaction_write,
  361. transaction_next_hash_chain,
  362. transaction_oob,
  363. transaction_expand_file,
  364. transaction_brlock,
  365. transaction_brunlock
  366. };
  367. /*
  368. sync to disk
  369. */
  370. static int transaction_sync(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t length)
  371. {
  372. if (tdb->flags & TDB_NOSYNC) {
  373. return 0;
  374. }
  375. if (fsync(tdb->fd) != 0) {
  376. tdb->ecode = TDB_ERR_IO;
  377. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction: fsync failed\n"));
  378. return -1;
  379. }
  380. #ifdef MS_SYNC
  381. if (tdb->map_ptr) {
  382. tdb_off_t moffset = offset & ~(tdb->page_size-1);
  383. if (msync(moffset + (char *)tdb->map_ptr,
  384. length + (offset - moffset), MS_SYNC) != 0) {
  385. tdb->ecode = TDB_ERR_IO;
  386. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction: msync failed - %s\n",
  387. strerror(errno)));
  388. return -1;
  389. }
  390. }
  391. #endif
  392. return 0;
  393. }
  394. /* ltype is F_WRLCK after prepare. */
  395. int _tdb_transaction_cancel(struct tdb_context *tdb, int ltype)
  396. {
  397. int i, ret = 0;
  398. if (tdb->transaction == NULL) {
  399. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_cancel: no transaction\n"));
  400. return -1;
  401. }
  402. if (tdb->transaction->nesting != 0) {
  403. tdb->transaction->transaction_error = 1;
  404. tdb->transaction->nesting--;
  405. return 0;
  406. }
  407. tdb->map_size = tdb->transaction->old_map_size;
  408. /* free all the transaction blocks */
  409. for (i=0;i<tdb->transaction->num_blocks;i++) {
  410. if (tdb->transaction->blocks[i] != NULL) {
  411. free(tdb->transaction->blocks[i]);
  412. }
  413. }
  414. SAFE_FREE(tdb->transaction->blocks);
  415. if (tdb->transaction->magic_offset) {
  416. const struct tdb_methods *methods = tdb->transaction->io_methods;
  417. uint32_t invalid = TDB_RECOVERY_INVALID_MAGIC;
  418. /* remove the recovery marker */
  419. if (methods->tdb_write(tdb, tdb->transaction->magic_offset, &invalid, 4) == -1 ||
  420. transaction_sync(tdb, tdb->transaction->magic_offset, 4) == -1) {
  421. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_cancel: failed to remove recovery magic\n"));
  422. ret = -1;
  423. }
  424. }
  425. if (tdb->transaction->open_lock_taken) {
  426. tdb_brunlock(tdb, F_WRLCK, OPEN_LOCK, 1);
  427. tdb->transaction->open_lock_taken = false;
  428. }
  429. /* remove any global lock created during the transaction */
  430. if (tdb->allrecord_lock.count != 0) {
  431. tdb_brunlock(tdb, tdb->allrecord_lock.ltype,
  432. FREELIST_TOP, 4*tdb->header.hash_size);
  433. tdb->allrecord_lock.count = 0;
  434. }
  435. /* remove any locks created during the transaction */
  436. if (tdb->num_locks != 0) {
  437. for (i=0;i<tdb->num_lockrecs;i++) {
  438. tdb_brunlock(tdb, tdb->lockrecs[i].ltype,
  439. tdb->lockrecs[i].off, 1);
  440. }
  441. tdb->num_locks = 0;
  442. tdb->num_lockrecs = 0;
  443. SAFE_FREE(tdb->lockrecs);
  444. }
  445. /* restore the normal io methods */
  446. tdb->methods = tdb->transaction->io_methods;
  447. tdb_brunlock(tdb, ltype, FREELIST_TOP, 0);
  448. tdb_transaction_unlock(tdb, F_WRLCK);
  449. SAFE_FREE(tdb->transaction->hash_heads);
  450. SAFE_FREE(tdb->transaction);
  451. return ret;
  452. }
  453. /*
  454. start a tdb transaction. No token is returned, as only a single
  455. transaction is allowed to be pending per tdb_context
  456. */
  457. int tdb_transaction_start(struct tdb_context *tdb)
  458. {
  459. /* some sanity checks */
  460. if (tdb->read_only || (tdb->flags & TDB_INTERNAL) || tdb->traverse_read) {
  461. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction on a read-only or internal db\n"));
  462. tdb->ecode = TDB_ERR_EINVAL;
  463. return -1;
  464. }
  465. /* cope with nested tdb_transaction_start() calls */
  466. if (tdb->transaction != NULL) {
  467. if (!(tdb->flags & TDB_ALLOW_NESTING)) {
  468. tdb->ecode = TDB_ERR_NESTING;
  469. return -1;
  470. }
  471. tdb_trace(tdb, "tdb_transaction_start");
  472. tdb->transaction->nesting++;
  473. TDB_LOG((tdb, TDB_DEBUG_TRACE, "tdb_transaction_start: nesting %d\n",
  474. tdb->transaction->nesting));
  475. return 0;
  476. }
  477. if (tdb->num_locks != 0 || tdb->allrecord_lock.count) {
  478. /* the caller must not have any locks when starting a
  479. transaction as otherwise we'll be screwed by lack
  480. of nested locks in posix */
  481. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction with locks held\n"));
  482. tdb->ecode = TDB_ERR_LOCK;
  483. return -1;
  484. }
  485. if (tdb->travlocks.next != NULL) {
  486. /* you cannot use transactions inside a traverse (although you can use
  487. traverse inside a transaction) as otherwise you can end up with
  488. deadlock */
  489. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: cannot start a transaction within a traverse\n"));
  490. tdb->ecode = TDB_ERR_LOCK;
  491. return -1;
  492. }
  493. tdb->transaction = (struct tdb_transaction *)
  494. calloc(sizeof(struct tdb_transaction), 1);
  495. if (tdb->transaction == NULL) {
  496. tdb->ecode = TDB_ERR_OOM;
  497. return -1;
  498. }
  499. /* a page at a time seems like a reasonable compromise between compactness and efficiency */
  500. tdb->transaction->block_size = tdb->page_size;
  501. /* get the transaction write lock. This is a blocking lock. As
  502. discussed with Volker, there are a number of ways we could
  503. make this async, which we will probably do in the future */
  504. if (tdb_transaction_lock(tdb, F_WRLCK) == -1) {
  505. SAFE_FREE(tdb->transaction->blocks);
  506. SAFE_FREE(tdb->transaction);
  507. return -1;
  508. }
  509. /* get a read lock from the freelist to the end of file. This
  510. is upgraded to a write lock during the commit */
  511. if (tdb_brlock(tdb, F_RDLCK, FREELIST_TOP, 0, TDB_LOCK_WAIT) == -1) {
  512. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: failed to get hash locks\n"));
  513. tdb->ecode = TDB_ERR_LOCK;
  514. goto fail;
  515. }
  516. /* setup a copy of the hash table heads so the hash scan in
  517. traverse can be fast */
  518. tdb->transaction->hash_heads = (uint32_t *)
  519. calloc(tdb->header.hash_size+1, sizeof(uint32_t));
  520. if (tdb->transaction->hash_heads == NULL) {
  521. tdb->ecode = TDB_ERR_OOM;
  522. goto fail;
  523. }
  524. if (tdb->methods->tdb_read(tdb, FREELIST_TOP, tdb->transaction->hash_heads,
  525. TDB_HASHTABLE_SIZE(tdb), 0) != 0) {
  526. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_start: failed to read hash heads\n"));
  527. tdb->ecode = TDB_ERR_IO;
  528. goto fail;
  529. }
  530. /* make sure we know about any file expansions already done by
  531. anyone else */
  532. tdb->methods->tdb_oob(tdb, tdb->map_size + 1, 1);
  533. tdb->transaction->old_map_size = tdb->map_size;
  534. /* finally hook the io methods, replacing them with
  535. transaction specific methods */
  536. tdb->transaction->io_methods = tdb->methods;
  537. tdb->methods = &transaction_methods;
  538. /* Trace at the end, so we get sequence number correct. */
  539. tdb_trace(tdb, "tdb_transaction_start");
  540. return 0;
  541. fail:
  542. tdb_brunlock(tdb, F_RDLCK, FREELIST_TOP, 0);
  543. tdb_transaction_unlock(tdb, F_WRLCK);
  544. SAFE_FREE(tdb->transaction->blocks);
  545. SAFE_FREE(tdb->transaction->hash_heads);
  546. SAFE_FREE(tdb->transaction);
  547. return -1;
  548. }
  549. /*
  550. cancel the current transaction
  551. */
  552. int tdb_transaction_cancel(struct tdb_context *tdb)
  553. {
  554. int ltype = F_RDLCK;
  555. tdb_trace(tdb, "tdb_transaction_cancel");
  556. if (tdb->transaction && tdb->transaction->prepared)
  557. ltype = F_WRLCK;
  558. return _tdb_transaction_cancel(tdb, ltype);
  559. }
  560. /*
  561. work out how much space the linearised recovery data will consume
  562. */
  563. static tdb_len_t tdb_recovery_size(struct tdb_context *tdb)
  564. {
  565. tdb_len_t recovery_size = 0;
  566. int i;
  567. recovery_size = sizeof(uint32_t);
  568. for (i=0;i<tdb->transaction->num_blocks;i++) {
  569. if (i * tdb->transaction->block_size >= tdb->transaction->old_map_size) {
  570. break;
  571. }
  572. if (tdb->transaction->blocks[i] == NULL) {
  573. continue;
  574. }
  575. recovery_size += 2*sizeof(tdb_off_t);
  576. if (i == tdb->transaction->num_blocks-1) {
  577. recovery_size += tdb->transaction->last_block_size;
  578. } else {
  579. recovery_size += tdb->transaction->block_size;
  580. }
  581. }
  582. return recovery_size;
  583. }
  584. /*
  585. allocate the recovery area, or use an existing recovery area if it is
  586. large enough
  587. */
  588. static int tdb_recovery_allocate(struct tdb_context *tdb,
  589. tdb_len_t *recovery_size,
  590. tdb_off_t *recovery_offset,
  591. tdb_len_t *recovery_max_size)
  592. {
  593. struct tdb_record rec;
  594. const struct tdb_methods *methods = tdb->transaction->io_methods;
  595. tdb_off_t recovery_head;
  596. if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
  597. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery head\n"));
  598. return -1;
  599. }
  600. rec.rec_len = 0;
  601. if (recovery_head != 0 &&
  602. methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {
  603. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to read recovery record\n"));
  604. return -1;
  605. }
  606. *recovery_size = tdb_recovery_size(tdb);
  607. if (recovery_head != 0 && *recovery_size <= rec.rec_len) {
  608. /* it fits in the existing area */
  609. *recovery_max_size = rec.rec_len;
  610. *recovery_offset = recovery_head;
  611. return 0;
  612. }
  613. /* we need to free up the old recovery area, then allocate a
  614. new one at the end of the file. Note that we cannot use
  615. tdb_allocate() to allocate the new one as that might return
  616. us an area that is being currently used (as of the start of
  617. the transaction) */
  618. if (recovery_head != 0) {
  619. if (tdb_free(tdb, recovery_head, &rec) == -1) {
  620. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to free previous recovery area\n"));
  621. return -1;
  622. }
  623. }
  624. /* the tdb_free() call might have increased the recovery size */
  625. *recovery_size = tdb_recovery_size(tdb);
  626. /* round up to a multiple of page size */
  627. *recovery_max_size = TDB_ALIGN(sizeof(rec) + *recovery_size, tdb->page_size) - sizeof(rec);
  628. *recovery_offset = tdb->map_size;
  629. recovery_head = *recovery_offset;
  630. if (methods->tdb_expand_file(tdb, tdb->transaction->old_map_size,
  631. (tdb->map_size - tdb->transaction->old_map_size) +
  632. sizeof(rec) + *recovery_max_size) == -1) {
  633. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to create recovery area\n"));
  634. return -1;
  635. }
  636. /* remap the file (if using mmap) */
  637. methods->tdb_oob(tdb, tdb->map_size + 1, 1);
  638. /* we have to reset the old map size so that we don't try to expand the file
  639. again in the transaction commit, which would destroy the recovery area */
  640. tdb->transaction->old_map_size = tdb->map_size;
  641. /* write the recovery header offset and sync - we can sync without a race here
  642. as the magic ptr in the recovery record has not been set */
  643. CONVERT(recovery_head);
  644. if (methods->tdb_write(tdb, TDB_RECOVERY_HEAD,
  645. &recovery_head, sizeof(tdb_off_t)) == -1) {
  646. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to write recovery head\n"));
  647. return -1;
  648. }
  649. if (transaction_write_existing(tdb, TDB_RECOVERY_HEAD, &recovery_head, sizeof(tdb_off_t)) == -1) {
  650. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_recovery_allocate: failed to write recovery head\n"));
  651. return -1;
  652. }
  653. return 0;
  654. }
  655. /*
  656. setup the recovery data that will be used on a crash during commit
  657. */
  658. static int transaction_setup_recovery(struct tdb_context *tdb,
  659. tdb_off_t *magic_offset)
  660. {
  661. tdb_len_t recovery_size;
  662. unsigned char *data, *p;
  663. const struct tdb_methods *methods = tdb->transaction->io_methods;
  664. struct tdb_record *rec;
  665. tdb_off_t recovery_offset, recovery_max_size;
  666. tdb_off_t old_map_size = tdb->transaction->old_map_size;
  667. uint32_t magic, tailer;
  668. int i;
  669. /*
  670. check that the recovery area has enough space
  671. */
  672. if (tdb_recovery_allocate(tdb, &recovery_size,
  673. &recovery_offset, &recovery_max_size) == -1) {
  674. return -1;
  675. }
  676. data = (unsigned char *)malloc(recovery_size + sizeof(*rec));
  677. if (data == NULL) {
  678. tdb->ecode = TDB_ERR_OOM;
  679. return -1;
  680. }
  681. rec = (struct tdb_record *)data;
  682. memset(rec, 0, sizeof(*rec));
  683. rec->magic = TDB_RECOVERY_INVALID_MAGIC;
  684. rec->data_len = recovery_size;
  685. rec->rec_len = recovery_max_size;
  686. rec->key_len = old_map_size;
  687. CONVERT(rec);
  688. /* build the recovery data into a single blob to allow us to do a single
  689. large write, which should be more efficient */
  690. p = data + sizeof(*rec);
  691. for (i=0;i<tdb->transaction->num_blocks;i++) {
  692. tdb_off_t offset;
  693. tdb_len_t length;
  694. if (tdb->transaction->blocks[i] == NULL) {
  695. continue;
  696. }
  697. offset = i * tdb->transaction->block_size;
  698. length = tdb->transaction->block_size;
  699. if (i == tdb->transaction->num_blocks-1) {
  700. length = tdb->transaction->last_block_size;
  701. }
  702. if (offset >= old_map_size) {
  703. continue;
  704. }
  705. if (offset + length > tdb->transaction->old_map_size) {
  706. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: transaction data over new region boundary\n"));
  707. free(data);
  708. tdb->ecode = TDB_ERR_CORRUPT;
  709. return -1;
  710. }
  711. memcpy(p, &offset, 4);
  712. memcpy(p+4, &length, 4);
  713. if (DOCONV()) {
  714. tdb_convert(p, 8);
  715. }
  716. /* the recovery area contains the old data, not the
  717. new data, so we have to call the original tdb_read
  718. method to get it */
  719. if (methods->tdb_read(tdb, offset, p + 8, length, 0) != 0) {
  720. free(data);
  721. tdb->ecode = TDB_ERR_IO;
  722. return -1;
  723. }
  724. p += 8 + length;
  725. }
  726. /* and the tailer */
  727. tailer = sizeof(*rec) + recovery_max_size;
  728. memcpy(p, &tailer, 4);
  729. CONVERT(p);
  730. /* write the recovery data to the recovery area */
  731. if (methods->tdb_write(tdb, recovery_offset, data, sizeof(*rec) + recovery_size) == -1) {
  732. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write recovery data\n"));
  733. free(data);
  734. tdb->ecode = TDB_ERR_IO;
  735. return -1;
  736. }
  737. if (transaction_write_existing(tdb, recovery_offset, data, sizeof(*rec) + recovery_size) == -1) {
  738. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write secondary recovery data\n"));
  739. free(data);
  740. tdb->ecode = TDB_ERR_IO;
  741. return -1;
  742. }
  743. /* as we don't have ordered writes, we have to sync the recovery
  744. data before we update the magic to indicate that the recovery
  745. data is present */
  746. if (transaction_sync(tdb, recovery_offset, sizeof(*rec) + recovery_size) == -1) {
  747. free(data);
  748. return -1;
  749. }
  750. free(data);
  751. magic = TDB_RECOVERY_MAGIC;
  752. CONVERT(magic);
  753. *magic_offset = recovery_offset + offsetof(struct tdb_record, magic);
  754. if (methods->tdb_write(tdb, *magic_offset, &magic, sizeof(magic)) == -1) {
  755. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write recovery magic\n"));
  756. tdb->ecode = TDB_ERR_IO;
  757. return -1;
  758. }
  759. if (transaction_write_existing(tdb, *magic_offset, &magic, sizeof(magic)) == -1) {
  760. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_setup_recovery: failed to write secondary recovery magic\n"));
  761. tdb->ecode = TDB_ERR_IO;
  762. return -1;
  763. }
  764. /* ensure the recovery magic marker is on disk */
  765. if (transaction_sync(tdb, *magic_offset, sizeof(magic)) == -1) {
  766. return -1;
  767. }
  768. return 0;
  769. }
  770. static int _tdb_transaction_prepare_commit(struct tdb_context *tdb)
  771. {
  772. const struct tdb_methods *methods;
  773. if (tdb->transaction == NULL) {
  774. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: no transaction\n"));
  775. return -1;
  776. }
  777. if (tdb->transaction->prepared) {
  778. tdb->ecode = TDB_ERR_EINVAL;
  779. _tdb_transaction_cancel(tdb, F_WRLCK);
  780. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: transaction already prepared\n"));
  781. return -1;
  782. }
  783. if (tdb->transaction->transaction_error) {
  784. tdb->ecode = TDB_ERR_IO;
  785. _tdb_transaction_cancel(tdb, F_RDLCK);
  786. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: transaction error pending\n"));
  787. return -1;
  788. }
  789. if (tdb->transaction->nesting != 0) {
  790. tdb->transaction->nesting--;
  791. return 0;
  792. }
  793. /* check for a null transaction */
  794. if (tdb->transaction->blocks == NULL) {
  795. return 0;
  796. }
  797. methods = tdb->transaction->io_methods;
  798. /* if there are any locks pending then the caller has not
  799. nested their locks properly, so fail the transaction */
  800. if (tdb->num_locks || tdb->allrecord_lock.count) {
  801. tdb->ecode = TDB_ERR_LOCK;
  802. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: locks pending on commit\n"));
  803. _tdb_transaction_cancel(tdb, F_RDLCK);
  804. return -1;
  805. }
  806. /* upgrade the main transaction lock region to a write lock */
  807. if (tdb_brlock_upgrade(tdb, FREELIST_TOP, 0) == -1) {
  808. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: failed to upgrade hash locks\n"));
  809. tdb->ecode = TDB_ERR_LOCK;
  810. _tdb_transaction_cancel(tdb, F_RDLCK);
  811. return -1;
  812. }
  813. /* get the open lock - this prevents new users attaching to the database
  814. during the commit */
  815. if (tdb_brlock(tdb, F_WRLCK, OPEN_LOCK, 1, TDB_LOCK_WAIT) == -1) {
  816. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: failed to get open lock\n"));
  817. tdb->ecode = TDB_ERR_LOCK;
  818. _tdb_transaction_cancel(tdb, F_WRLCK);
  819. return -1;
  820. }
  821. tdb->transaction->open_lock_taken = true;
  822. if (!(tdb->flags & TDB_NOSYNC)) {
  823. /* write the recovery data to the end of the file */
  824. if (transaction_setup_recovery(tdb, &tdb->transaction->magic_offset) == -1) {
  825. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_prepare_commit: failed to setup recovery data\n"));
  826. _tdb_transaction_cancel(tdb, F_WRLCK);
  827. return -1;
  828. }
  829. }
  830. tdb->transaction->prepared = true;
  831. /* expand the file to the new size if needed */
  832. if (tdb->map_size != tdb->transaction->old_map_size) {
  833. if (methods->tdb_expand_file(tdb, tdb->transaction->old_map_size,
  834. tdb->map_size -
  835. tdb->transaction->old_map_size) == -1) {
  836. tdb->ecode = TDB_ERR_IO;
  837. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_prepare_commit: expansion failed\n"));
  838. _tdb_transaction_cancel(tdb, F_WRLCK);
  839. return -1;
  840. }
  841. tdb->map_size = tdb->transaction->old_map_size;
  842. methods->tdb_oob(tdb, tdb->map_size + 1, 1);
  843. }
  844. /* Keep the open lock until the actual commit */
  845. return 0;
  846. }
  847. /*
  848. prepare to commit the current transaction
  849. */
  850. int tdb_transaction_prepare_commit(struct tdb_context *tdb)
  851. {
  852. tdb_trace(tdb, "tdb_transaction_prepare_commit");
  853. return _tdb_transaction_prepare_commit(tdb);
  854. }
  855. /*
  856. commit the current transaction
  857. */
  858. int tdb_transaction_commit(struct tdb_context *tdb)
  859. {
  860. const struct tdb_methods *methods;
  861. int i;
  862. bool need_repack;
  863. if (tdb->transaction == NULL) {
  864. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: no transaction\n"));
  865. return -1;
  866. }
  867. tdb_trace(tdb, "tdb_transaction_commit");
  868. if (tdb->transaction->transaction_error) {
  869. tdb->ecode = TDB_ERR_IO;
  870. tdb_transaction_cancel(tdb);
  871. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_commit: transaction error pending\n"));
  872. return -1;
  873. }
  874. if (tdb->transaction->nesting != 0) {
  875. tdb->transaction->nesting--;
  876. return 0;
  877. }
  878. /* check for a null transaction */
  879. if (tdb->transaction->blocks == NULL) {
  880. _tdb_transaction_cancel(tdb, F_RDLCK);
  881. return 0;
  882. }
  883. if (!tdb->transaction->prepared) {
  884. int ret = _tdb_transaction_prepare_commit(tdb);
  885. if (ret)
  886. return ret;
  887. }
  888. methods = tdb->transaction->io_methods;
  889. /* perform all the writes */
  890. for (i=0;i<tdb->transaction->num_blocks;i++) {
  891. tdb_off_t offset;
  892. tdb_len_t length;
  893. if (tdb->transaction->blocks[i] == NULL) {
  894. continue;
  895. }
  896. offset = i * tdb->transaction->block_size;
  897. length = tdb->transaction->block_size;
  898. if (i == tdb->transaction->num_blocks-1) {
  899. length = tdb->transaction->last_block_size;
  900. }
  901. if (methods->tdb_write(tdb, offset, tdb->transaction->blocks[i], length) == -1) {
  902. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: write failed during commit\n"));
  903. /* we've overwritten part of the data and
  904. possibly expanded the file, so we need to
  905. run the crash recovery code */
  906. tdb->methods = methods;
  907. tdb_transaction_recover(tdb);
  908. _tdb_transaction_cancel(tdb, F_WRLCK);
  909. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_commit: write failed\n"));
  910. return -1;
  911. }
  912. SAFE_FREE(tdb->transaction->blocks[i]);
  913. }
  914. SAFE_FREE(tdb->transaction->blocks);
  915. tdb->transaction->num_blocks = 0;
  916. /* ensure the new data is on disk */
  917. if (transaction_sync(tdb, 0, tdb->map_size) == -1) {
  918. return -1;
  919. }
  920. /*
  921. TODO: maybe write to some dummy hdr field, or write to magic
  922. offset without mmap, before the last sync, instead of the
  923. utime() call
  924. */
  925. /* on some systems (like Linux 2.6.x) changes via mmap/msync
  926. don't change the mtime of the file, this means the file may
  927. not be backed up (as tdb rounding to block sizes means that
  928. file size changes are quite rare too). The following forces
  929. mtime changes when a transaction completes */
  930. #if HAVE_UTIME
  931. utime(tdb->name, NULL);
  932. #endif
  933. need_repack = tdb->transaction->need_repack;
  934. /* use a transaction cancel to free memory and remove the
  935. transaction locks */
  936. _tdb_transaction_cancel(tdb, F_WRLCK);
  937. if (need_repack) {
  938. return tdb_repack(tdb);
  939. }
  940. return 0;
  941. }
  942. /*
  943. recover from an aborted transaction. Must be called with exclusive
  944. database write access already established (including the open
  945. lock to prevent new processes attaching)
  946. */
  947. int tdb_transaction_recover(struct tdb_context *tdb)
  948. {
  949. tdb_off_t recovery_head, recovery_eof;
  950. unsigned char *data, *p;
  951. uint32_t zero = 0;
  952. struct tdb_record rec;
  953. /* find the recovery area */
  954. if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {
  955. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery head\n"));
  956. tdb->ecode = TDB_ERR_IO;
  957. return -1;
  958. }
  959. if (recovery_head == 0) {
  960. /* we have never allocated a recovery record */
  961. return 0;
  962. }
  963. /* read the recovery record */
  964. if (tdb->methods->tdb_read(tdb, recovery_head, &rec,
  965. sizeof(rec), DOCONV()) == -1) {
  966. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery record\n"));
  967. tdb->ecode = TDB_ERR_IO;
  968. return -1;
  969. }
  970. if (rec.magic != TDB_RECOVERY_MAGIC) {
  971. /* there is no valid recovery data */
  972. return 0;
  973. }
  974. if (tdb->read_only) {
  975. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: attempt to recover read only database\n"));
  976. tdb->ecode = TDB_ERR_CORRUPT;
  977. return -1;
  978. }
  979. recovery_eof = rec.key_len;
  980. data = (unsigned char *)malloc(rec.data_len);
  981. if (data == NULL) {
  982. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to allocate recovery data\n"));
  983. tdb->ecode = TDB_ERR_OOM;
  984. return -1;
  985. }
  986. /* read the full recovery data */
  987. if (tdb->methods->tdb_read(tdb, recovery_head + sizeof(rec), data,
  988. rec.data_len, 0) == -1) {
  989. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to read recovery data\n"));
  990. tdb->ecode = TDB_ERR_IO;
  991. return -1;
  992. }
  993. /* recover the file data */
  994. p = data;
  995. while (p+8 < data + rec.data_len) {
  996. uint32_t ofs, len;
  997. if (DOCONV()) {
  998. tdb_convert(p, 8);
  999. }
  1000. memcpy(&ofs, p, 4);
  1001. memcpy(&len, p+4, 4);
  1002. if (tdb->methods->tdb_write(tdb, ofs, p+8, len) == -1) {
  1003. free(data);
  1004. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to recover %d bytes at offset %d\n", len, ofs));
  1005. tdb->ecode = TDB_ERR_IO;
  1006. return -1;
  1007. }
  1008. p += 8 + len;
  1009. }
  1010. free(data);
  1011. if (transaction_sync(tdb, 0, tdb->map_size) == -1) {
  1012. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to sync recovery\n"));
  1013. tdb->ecode = TDB_ERR_IO;
  1014. return -1;
  1015. }
  1016. /* if the recovery area is after the recovered eof then remove it */
  1017. if (recovery_eof <= recovery_head) {
  1018. if (tdb_ofs_write(tdb, TDB_RECOVERY_HEAD, &zero) == -1) {
  1019. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to remove recovery head\n"));
  1020. tdb->ecode = TDB_ERR_IO;
  1021. return -1;
  1022. }
  1023. }
  1024. /* remove the recovery magic */
  1025. if (tdb_ofs_write(tdb, recovery_head + offsetof(struct tdb_record, magic),
  1026. &zero) == -1) {
  1027. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to remove recovery magic\n"));
  1028. tdb->ecode = TDB_ERR_IO;
  1029. return -1;
  1030. }
  1031. /* reduce the file size to the old size */
  1032. tdb_munmap(tdb);
  1033. if (ftruncate(tdb->fd, recovery_eof) != 0) {
  1034. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to reduce to recovery size\n"));
  1035. tdb->ecode = TDB_ERR_IO;
  1036. return -1;
  1037. }
  1038. tdb->map_size = recovery_eof;
  1039. tdb_mmap(tdb);
  1040. if (transaction_sync(tdb, 0, recovery_eof) == -1) {
  1041. TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_transaction_recover: failed to sync2 recovery\n"));
  1042. tdb->ecode = TDB_ERR_IO;
  1043. return -1;
  1044. }
  1045. TDB_LOG((tdb, TDB_DEBUG_TRACE, "tdb_transaction_recover: recovered %d byte database\n",
  1046. recovery_eof));
  1047. /* all done */
  1048. return 0;
  1049. }