transaction.c 37 KB

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