PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/bind9/lib/dns/journal.c

https://bitbucket.org/freebsd/freebsd-head/
C | 2250 lines | 1498 code | 289 blank | 463 comment | 349 complexity | 4532cfc27fb3334af1b34567dc5b1ad5 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * Copyright (C) 2004, 2005, 2007-2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2002 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. #include <config.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <errno.h>
  22. #include <isc/file.h>
  23. #include <isc/mem.h>
  24. #include <isc/stdio.h>
  25. #include <isc/string.h>
  26. #include <isc/util.h>
  27. #include <dns/compress.h>
  28. #include <dns/db.h>
  29. #include <dns/dbiterator.h>
  30. #include <dns/diff.h>
  31. #include <dns/fixedname.h>
  32. #include <dns/journal.h>
  33. #include <dns/log.h>
  34. #include <dns/rdataset.h>
  35. #include <dns/rdatasetiter.h>
  36. #include <dns/result.h>
  37. #include <dns/soa.h>
  38. /*! \file
  39. * \brief Journaling.
  40. *
  41. * A journal file consists of
  42. *
  43. * \li A fixed-size header of type journal_rawheader_t.
  44. *
  45. * \li The index. This is an unordered array of index entries
  46. * of type journal_rawpos_t giving the locations
  47. * of some arbitrary subset of the journal's addressable
  48. * transactions. The index entries are used as hints to
  49. * speed up the process of locating a transaction with a given
  50. * serial number. Unused index entries have an "offset"
  51. * field of zero. The size of the index can vary between
  52. * journal files, but does not change during the lifetime
  53. * of a file. The size can be zero.
  54. *
  55. * \li The journal data. This consists of one or more transactions.
  56. * Each transaction begins with a transaction header of type
  57. * journal_rawxhdr_t. The transaction header is followed by a
  58. * sequence of RRs, similar in structure to an IXFR difference
  59. * sequence (RFC1995). That is, the pre-transaction SOA,
  60. * zero or more other deleted RRs, the post-transaction SOA,
  61. * and zero or more other added RRs. Unlike in IXFR, each RR
  62. * is prefixed with a 32-bit length.
  63. *
  64. * The journal data part grows as new transactions are
  65. * appended to the file. Only those transactions
  66. * whose serial number is current-(2^31-1) to current
  67. * are considered "addressable" and may be pointed
  68. * to from the header or index. They may be preceded
  69. * by old transactions that are no longer addressable,
  70. * and they may be followed by transactions that were
  71. * appended to the journal but never committed by updating
  72. * the "end" position in the header. The latter will
  73. * be overwritten when new transactions are added.
  74. */
  75. /*%
  76. * When true, accept IXFR difference sequences where the
  77. * SOA serial number does not change (BIND 8 sends such
  78. * sequences).
  79. */
  80. static isc_boolean_t bind8_compat = ISC_TRUE; /* XXX config */
  81. /**************************************************************************/
  82. /*
  83. * Miscellaneous utilities.
  84. */
  85. #define JOURNAL_COMMON_LOGARGS \
  86. dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_JOURNAL
  87. #define JOURNAL_DEBUG_LOGARGS(n) \
  88. JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(n)
  89. /*%
  90. * It would be non-sensical (or at least obtuse) to use FAIL() with an
  91. * ISC_R_SUCCESS code, but the test is there to keep the Solaris compiler
  92. * from complaining about "end-of-loop code not reached".
  93. */
  94. #define FAIL(code) \
  95. do { result = (code); \
  96. if (result != ISC_R_SUCCESS) goto failure; \
  97. } while (0)
  98. #define CHECK(op) \
  99. do { result = (op); \
  100. if (result != ISC_R_SUCCESS) goto failure; \
  101. } while (0)
  102. static isc_result_t index_to_disk(dns_journal_t *);
  103. static inline isc_uint32_t
  104. decode_uint32(unsigned char *p) {
  105. return ((p[0] << 24) +
  106. (p[1] << 16) +
  107. (p[2] << 8) +
  108. (p[3] << 0));
  109. }
  110. static inline void
  111. encode_uint32(isc_uint32_t val, unsigned char *p) {
  112. p[0] = (isc_uint8_t)(val >> 24);
  113. p[1] = (isc_uint8_t)(val >> 16);
  114. p[2] = (isc_uint8_t)(val >> 8);
  115. p[3] = (isc_uint8_t)(val >> 0);
  116. }
  117. isc_result_t
  118. dns_db_createsoatuple(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
  119. dns_diffop_t op, dns_difftuple_t **tp)
  120. {
  121. isc_result_t result;
  122. dns_dbnode_t *node;
  123. dns_rdataset_t rdataset;
  124. dns_rdata_t rdata = DNS_RDATA_INIT;
  125. dns_name_t *zonename;
  126. zonename = dns_db_origin(db);
  127. node = NULL;
  128. result = dns_db_findnode(db, zonename, ISC_FALSE, &node);
  129. if (result != ISC_R_SUCCESS)
  130. goto nonode;
  131. dns_rdataset_init(&rdataset);
  132. result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
  133. (isc_stdtime_t)0, &rdataset, NULL);
  134. if (result != ISC_R_SUCCESS)
  135. goto freenode;
  136. result = dns_rdataset_first(&rdataset);
  137. if (result != ISC_R_SUCCESS)
  138. goto freenode;
  139. dns_rdataset_current(&rdataset, &rdata);
  140. result = dns_difftuple_create(mctx, op, zonename, rdataset.ttl,
  141. &rdata, tp);
  142. dns_rdataset_disassociate(&rdataset);
  143. dns_db_detachnode(db, &node);
  144. return (result);
  145. freenode:
  146. dns_db_detachnode(db, &node);
  147. nonode:
  148. UNEXPECTED_ERROR(__FILE__, __LINE__, "missing SOA");
  149. return (result);
  150. }
  151. /* Journaling */
  152. /*%
  153. * On-disk representation of a "pointer" to a journal entry.
  154. * These are used in the journal header to locate the beginning
  155. * and end of the journal, and in the journal index to locate
  156. * other transactions.
  157. */
  158. typedef struct {
  159. unsigned char serial[4]; /*%< SOA serial before update. */
  160. /*
  161. * XXXRTH Should offset be 8 bytes?
  162. * XXXDCL ... probably, since isc_offset_t is 8 bytes on many OSs.
  163. * XXXAG ... but we will not be able to seek >2G anyway on many
  164. * platforms as long as we are using fseek() rather
  165. * than lseek().
  166. */
  167. unsigned char offset[4]; /*%< Offset from beginning of file. */
  168. } journal_rawpos_t;
  169. /*%
  170. * The header is of a fixed size, with some spare room for future
  171. * extensions.
  172. */
  173. #define JOURNAL_HEADER_SIZE 64 /* Bytes. */
  174. /*%
  175. * The on-disk representation of the journal header.
  176. * All numbers are stored in big-endian order.
  177. */
  178. typedef union {
  179. struct {
  180. /*% File format version ID. */
  181. unsigned char format[16];
  182. /*% Position of the first addressable transaction */
  183. journal_rawpos_t begin;
  184. /*% Position of the next (yet nonexistent) transaction. */
  185. journal_rawpos_t end;
  186. /*% Number of index entries following the header. */
  187. unsigned char index_size[4];
  188. } h;
  189. /* Pad the header to a fixed size. */
  190. unsigned char pad[JOURNAL_HEADER_SIZE];
  191. } journal_rawheader_t;
  192. /*%
  193. * The on-disk representation of the transaction header.
  194. * There is one of these at the beginning of each transaction.
  195. */
  196. typedef struct {
  197. unsigned char size[4]; /*%< In bytes, excluding header. */
  198. unsigned char serial0[4]; /*%< SOA serial before update. */
  199. unsigned char serial1[4]; /*%< SOA serial after update. */
  200. } journal_rawxhdr_t;
  201. /*%
  202. * The on-disk representation of the RR header.
  203. * There is one of these at the beginning of each RR.
  204. */
  205. typedef struct {
  206. unsigned char size[4]; /*%< In bytes, excluding header. */
  207. } journal_rawrrhdr_t;
  208. /*%
  209. * The in-core representation of the journal header.
  210. */
  211. typedef struct {
  212. isc_uint32_t serial;
  213. isc_offset_t offset;
  214. } journal_pos_t;
  215. #define POS_VALID(pos) ((pos).offset != 0)
  216. #define POS_INVALIDATE(pos) ((pos).offset = 0, (pos).serial = 0)
  217. typedef struct {
  218. unsigned char format[16];
  219. journal_pos_t begin;
  220. journal_pos_t end;
  221. isc_uint32_t index_size;
  222. } journal_header_t;
  223. /*%
  224. * The in-core representation of the transaction header.
  225. */
  226. typedef struct {
  227. isc_uint32_t size;
  228. isc_uint32_t serial0;
  229. isc_uint32_t serial1;
  230. } journal_xhdr_t;
  231. /*%
  232. * The in-core representation of the RR header.
  233. */
  234. typedef struct {
  235. isc_uint32_t size;
  236. } journal_rrhdr_t;
  237. /*%
  238. * Initial contents to store in the header of a newly created
  239. * journal file.
  240. *
  241. * The header starts with the magic string ";BIND LOG V9\n"
  242. * to identify the file as a BIND 9 journal file. An ASCII
  243. * identification string is used rather than a binary magic
  244. * number to be consistent with BIND 8 (BIND 8 journal files
  245. * are ASCII text files).
  246. */
  247. static journal_header_t
  248. initial_journal_header = { ";BIND LOG V9\n", { 0, 0 }, { 0, 0 }, 0 };
  249. #define JOURNAL_EMPTY(h) ((h)->begin.offset == (h)->end.offset)
  250. typedef enum {
  251. JOURNAL_STATE_INVALID,
  252. JOURNAL_STATE_READ,
  253. JOURNAL_STATE_WRITE,
  254. JOURNAL_STATE_TRANSACTION
  255. } journal_state_t;
  256. struct dns_journal {
  257. unsigned int magic; /*%< JOUR */
  258. isc_mem_t *mctx; /*%< Memory context */
  259. journal_state_t state;
  260. const char *filename; /*%< Journal file name */
  261. FILE * fp; /*%< File handle */
  262. isc_offset_t offset; /*%< Current file offset */
  263. journal_header_t header; /*%< In-core journal header */
  264. unsigned char *rawindex; /*%< In-core buffer for journal index in on-disk format */
  265. journal_pos_t *index; /*%< In-core journal index */
  266. /*% Current transaction state (when writing). */
  267. struct {
  268. unsigned int n_soa; /*%< Number of SOAs seen */
  269. journal_pos_t pos[2]; /*%< Begin/end position */
  270. } x;
  271. /*% Iteration state (when reading). */
  272. struct {
  273. /* These define the part of the journal we iterate over. */
  274. journal_pos_t bpos; /*%< Position before first, */
  275. journal_pos_t epos; /*%< and after last transaction */
  276. /* The rest is iterator state. */
  277. isc_uint32_t current_serial; /*%< Current SOA serial */
  278. isc_buffer_t source; /*%< Data from disk */
  279. isc_buffer_t target; /*%< Data from _fromwire check */
  280. dns_decompress_t dctx; /*%< Dummy decompression ctx */
  281. dns_name_t name; /*%< Current domain name */
  282. dns_rdata_t rdata; /*%< Current rdata */
  283. isc_uint32_t ttl; /*%< Current TTL */
  284. unsigned int xsize; /*%< Size of transaction data */
  285. unsigned int xpos; /*%< Current position in it */
  286. isc_result_t result; /*%< Result of last call */
  287. } it;
  288. };
  289. #define DNS_JOURNAL_MAGIC ISC_MAGIC('J', 'O', 'U', 'R')
  290. #define DNS_JOURNAL_VALID(t) ISC_MAGIC_VALID(t, DNS_JOURNAL_MAGIC)
  291. static void
  292. journal_pos_decode(journal_rawpos_t *raw, journal_pos_t *cooked) {
  293. cooked->serial = decode_uint32(raw->serial);
  294. cooked->offset = decode_uint32(raw->offset);
  295. }
  296. static void
  297. journal_pos_encode(journal_rawpos_t *raw, journal_pos_t *cooked) {
  298. encode_uint32(cooked->serial, raw->serial);
  299. encode_uint32(cooked->offset, raw->offset);
  300. }
  301. static void
  302. journal_header_decode(journal_rawheader_t *raw, journal_header_t *cooked) {
  303. INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
  304. memcpy(cooked->format, raw->h.format, sizeof(cooked->format));
  305. journal_pos_decode(&raw->h.begin, &cooked->begin);
  306. journal_pos_decode(&raw->h.end, &cooked->end);
  307. cooked->index_size = decode_uint32(raw->h.index_size);
  308. }
  309. static void
  310. journal_header_encode(journal_header_t *cooked, journal_rawheader_t *raw) {
  311. INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
  312. memset(raw->pad, 0, sizeof(raw->pad));
  313. memcpy(raw->h.format, cooked->format, sizeof(raw->h.format));
  314. journal_pos_encode(&raw->h.begin, &cooked->begin);
  315. journal_pos_encode(&raw->h.end, &cooked->end);
  316. encode_uint32(cooked->index_size, raw->h.index_size);
  317. }
  318. /*
  319. * Journal file I/O subroutines, with error checking and reporting.
  320. */
  321. static isc_result_t
  322. journal_seek(dns_journal_t *j, isc_uint32_t offset) {
  323. isc_result_t result;
  324. result = isc_stdio_seek(j->fp, (long)offset, SEEK_SET);
  325. if (result != ISC_R_SUCCESS) {
  326. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  327. "%s: seek: %s", j->filename,
  328. isc_result_totext(result));
  329. return (ISC_R_UNEXPECTED);
  330. }
  331. j->offset = offset;
  332. return (ISC_R_SUCCESS);
  333. }
  334. static isc_result_t
  335. journal_read(dns_journal_t *j, void *mem, size_t nbytes) {
  336. isc_result_t result;
  337. result = isc_stdio_read(mem, 1, nbytes, j->fp, NULL);
  338. if (result != ISC_R_SUCCESS) {
  339. if (result == ISC_R_EOF)
  340. return (ISC_R_NOMORE);
  341. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  342. "%s: read: %s",
  343. j->filename, isc_result_totext(result));
  344. return (ISC_R_UNEXPECTED);
  345. }
  346. j->offset += nbytes;
  347. return (ISC_R_SUCCESS);
  348. }
  349. static isc_result_t
  350. journal_write(dns_journal_t *j, void *mem, size_t nbytes) {
  351. isc_result_t result;
  352. result = isc_stdio_write(mem, 1, nbytes, j->fp, NULL);
  353. if (result != ISC_R_SUCCESS) {
  354. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  355. "%s: write: %s",
  356. j->filename, isc_result_totext(result));
  357. return (ISC_R_UNEXPECTED);
  358. }
  359. j->offset += nbytes;
  360. return (ISC_R_SUCCESS);
  361. }
  362. static isc_result_t
  363. journal_fsync(dns_journal_t *j) {
  364. isc_result_t result;
  365. result = isc_stdio_flush(j->fp);
  366. if (result != ISC_R_SUCCESS) {
  367. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  368. "%s: flush: %s",
  369. j->filename, isc_result_totext(result));
  370. return (ISC_R_UNEXPECTED);
  371. }
  372. result = isc_stdio_sync(j->fp);
  373. if (result != ISC_R_SUCCESS) {
  374. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  375. "%s: fsync: %s",
  376. j->filename, isc_result_totext(result));
  377. return (ISC_R_UNEXPECTED);
  378. }
  379. return (ISC_R_SUCCESS);
  380. }
  381. /*
  382. * Read/write a transaction header at the current file position.
  383. */
  384. static isc_result_t
  385. journal_read_xhdr(dns_journal_t *j, journal_xhdr_t *xhdr) {
  386. journal_rawxhdr_t raw;
  387. isc_result_t result;
  388. result = journal_read(j, &raw, sizeof(raw));
  389. if (result != ISC_R_SUCCESS)
  390. return (result);
  391. xhdr->size = decode_uint32(raw.size);
  392. xhdr->serial0 = decode_uint32(raw.serial0);
  393. xhdr->serial1 = decode_uint32(raw.serial1);
  394. return (ISC_R_SUCCESS);
  395. }
  396. static isc_result_t
  397. journal_write_xhdr(dns_journal_t *j, isc_uint32_t size,
  398. isc_uint32_t serial0, isc_uint32_t serial1)
  399. {
  400. journal_rawxhdr_t raw;
  401. encode_uint32(size, raw.size);
  402. encode_uint32(serial0, raw.serial0);
  403. encode_uint32(serial1, raw.serial1);
  404. return (journal_write(j, &raw, sizeof(raw)));
  405. }
  406. /*
  407. * Read an RR header at the current file position.
  408. */
  409. static isc_result_t
  410. journal_read_rrhdr(dns_journal_t *j, journal_rrhdr_t *rrhdr) {
  411. journal_rawrrhdr_t raw;
  412. isc_result_t result;
  413. result = journal_read(j, &raw, sizeof(raw));
  414. if (result != ISC_R_SUCCESS)
  415. return (result);
  416. rrhdr->size = decode_uint32(raw.size);
  417. return (ISC_R_SUCCESS);
  418. }
  419. static isc_result_t
  420. journal_file_create(isc_mem_t *mctx, const char *filename) {
  421. FILE *fp = NULL;
  422. isc_result_t result;
  423. journal_header_t header;
  424. journal_rawheader_t rawheader;
  425. int index_size = 56; /* XXX configurable */
  426. int size;
  427. void *mem; /* Memory for temporary index image. */
  428. INSIST(sizeof(journal_rawheader_t) == JOURNAL_HEADER_SIZE);
  429. result = isc_stdio_open(filename, "wb", &fp);
  430. if (result != ISC_R_SUCCESS) {
  431. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  432. "%s: create: %s",
  433. filename, isc_result_totext(result));
  434. return (ISC_R_UNEXPECTED);
  435. }
  436. header = initial_journal_header;
  437. header.index_size = index_size;
  438. journal_header_encode(&header, &rawheader);
  439. size = sizeof(journal_rawheader_t) +
  440. index_size * sizeof(journal_rawpos_t);
  441. mem = isc_mem_get(mctx, size);
  442. if (mem == NULL) {
  443. (void)isc_stdio_close(fp);
  444. (void)isc_file_remove(filename);
  445. return (ISC_R_NOMEMORY);
  446. }
  447. memset(mem, 0, size);
  448. memcpy(mem, &rawheader, sizeof(rawheader));
  449. result = isc_stdio_write(mem, 1, (size_t) size, fp, NULL);
  450. if (result != ISC_R_SUCCESS) {
  451. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  452. "%s: write: %s",
  453. filename, isc_result_totext(result));
  454. (void)isc_stdio_close(fp);
  455. (void)isc_file_remove(filename);
  456. isc_mem_put(mctx, mem, size);
  457. return (ISC_R_UNEXPECTED);
  458. }
  459. isc_mem_put(mctx, mem, size);
  460. result = isc_stdio_close(fp);
  461. if (result != ISC_R_SUCCESS) {
  462. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  463. "%s: close: %s",
  464. filename, isc_result_totext(result));
  465. (void)isc_file_remove(filename);
  466. return (ISC_R_UNEXPECTED);
  467. }
  468. return (ISC_R_SUCCESS);
  469. }
  470. static isc_result_t
  471. journal_open(isc_mem_t *mctx, const char *filename, isc_boolean_t write,
  472. isc_boolean_t create, dns_journal_t **journalp) {
  473. FILE *fp = NULL;
  474. isc_result_t result;
  475. journal_rawheader_t rawheader;
  476. dns_journal_t *j;
  477. INSIST(journalp != NULL && *journalp == NULL);
  478. j = isc_mem_get(mctx, sizeof(*j));
  479. if (j == NULL)
  480. return (ISC_R_NOMEMORY);
  481. j->mctx = mctx;
  482. j->state = JOURNAL_STATE_INVALID;
  483. j->fp = NULL;
  484. j->filename = filename;
  485. j->index = NULL;
  486. j->rawindex = NULL;
  487. result = isc_stdio_open(j->filename, write ? "rb+" : "rb", &fp);
  488. if (result == ISC_R_FILENOTFOUND) {
  489. if (create) {
  490. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(1),
  491. "journal file %s does not exist, "
  492. "creating it", j->filename);
  493. CHECK(journal_file_create(mctx, filename));
  494. /*
  495. * Retry.
  496. */
  497. result = isc_stdio_open(j->filename, "rb+", &fp);
  498. } else {
  499. FAIL(ISC_R_NOTFOUND);
  500. }
  501. }
  502. if (result != ISC_R_SUCCESS) {
  503. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  504. "%s: open: %s",
  505. j->filename, isc_result_totext(result));
  506. FAIL(ISC_R_UNEXPECTED);
  507. }
  508. j->fp = fp;
  509. /*
  510. * Set magic early so that seek/read can succeed.
  511. */
  512. j->magic = DNS_JOURNAL_MAGIC;
  513. CHECK(journal_seek(j, 0));
  514. CHECK(journal_read(j, &rawheader, sizeof(rawheader)));
  515. if (memcmp(rawheader.h.format, initial_journal_header.format,
  516. sizeof(initial_journal_header.format)) != 0) {
  517. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  518. "%s: journal format not recognized",
  519. j->filename);
  520. FAIL(ISC_R_UNEXPECTED);
  521. }
  522. journal_header_decode(&rawheader, &j->header);
  523. /*
  524. * If there is an index, read the raw index into a dynamically
  525. * allocated buffer and then convert it into a cooked index.
  526. */
  527. if (j->header.index_size != 0) {
  528. unsigned int i;
  529. unsigned int rawbytes;
  530. unsigned char *p;
  531. rawbytes = j->header.index_size * sizeof(journal_rawpos_t);
  532. j->rawindex = isc_mem_get(mctx, rawbytes);
  533. if (j->rawindex == NULL)
  534. FAIL(ISC_R_NOMEMORY);
  535. CHECK(journal_read(j, j->rawindex, rawbytes));
  536. j->index = isc_mem_get(mctx, j->header.index_size *
  537. sizeof(journal_pos_t));
  538. if (j->index == NULL)
  539. FAIL(ISC_R_NOMEMORY);
  540. p = j->rawindex;
  541. for (i = 0; i < j->header.index_size; i++) {
  542. j->index[i].serial = decode_uint32(p);
  543. p += 4;
  544. j->index[i].offset = decode_uint32(p);
  545. p += 4;
  546. }
  547. INSIST(p == j->rawindex + rawbytes);
  548. }
  549. j->offset = -1; /* Invalid, must seek explicitly. */
  550. /*
  551. * Initialize the iterator.
  552. */
  553. dns_name_init(&j->it.name, NULL);
  554. dns_rdata_init(&j->it.rdata);
  555. /*
  556. * Set up empty initial buffers for unchecked and checked
  557. * wire format RR data. They will be reallocated
  558. * later.
  559. */
  560. isc_buffer_init(&j->it.source, NULL, 0);
  561. isc_buffer_init(&j->it.target, NULL, 0);
  562. dns_decompress_init(&j->it.dctx, -1, DNS_DECOMPRESS_NONE);
  563. j->state =
  564. write ? JOURNAL_STATE_WRITE : JOURNAL_STATE_READ;
  565. *journalp = j;
  566. return (ISC_R_SUCCESS);
  567. failure:
  568. j->magic = 0;
  569. if (j->index != NULL) {
  570. isc_mem_put(j->mctx, j->index, j->header.index_size *
  571. sizeof(journal_rawpos_t));
  572. j->index = NULL;
  573. }
  574. if (j->fp != NULL)
  575. (void)isc_stdio_close(j->fp);
  576. isc_mem_put(j->mctx, j, sizeof(*j));
  577. return (result);
  578. }
  579. isc_result_t
  580. dns_journal_open(isc_mem_t *mctx, const char *filename, isc_boolean_t write,
  581. dns_journal_t **journalp) {
  582. isc_result_t result;
  583. int namelen;
  584. char backup[1024];
  585. result = journal_open(mctx, filename, write, write, journalp);
  586. if (result == ISC_R_NOTFOUND) {
  587. namelen = strlen(filename);
  588. if (namelen > 4 && strcmp(filename + namelen - 4, ".jnl") == 0)
  589. namelen -= 4;
  590. result = isc_string_printf(backup, sizeof(backup), "%.*s.jbk",
  591. namelen, filename);
  592. if (result != ISC_R_SUCCESS)
  593. return (result);
  594. result = journal_open(mctx, backup, write, write, journalp);
  595. }
  596. return (result);
  597. }
  598. /*
  599. * A comparison function defining the sorting order for
  600. * entries in the IXFR-style journal file.
  601. *
  602. * The IXFR format requires that deletions are sorted before
  603. * additions, and within either one, SOA records are sorted
  604. * before others.
  605. *
  606. * Also sort the non-SOA records by type as a courtesy to the
  607. * server receiving the IXFR - it may help reduce the amount of
  608. * rdataset merging it has to do.
  609. */
  610. static int
  611. ixfr_order(const void *av, const void *bv) {
  612. dns_difftuple_t const * const *ap = av;
  613. dns_difftuple_t const * const *bp = bv;
  614. dns_difftuple_t const *a = *ap;
  615. dns_difftuple_t const *b = *bp;
  616. int r;
  617. int bop = 0, aop = 0;
  618. switch (a->op) {
  619. case DNS_DIFFOP_DEL:
  620. case DNS_DIFFOP_DELRESIGN:
  621. aop = 1;
  622. break;
  623. case DNS_DIFFOP_ADD:
  624. case DNS_DIFFOP_ADDRESIGN:
  625. aop = 0;
  626. break;
  627. default:
  628. INSIST(0);
  629. }
  630. switch (b->op) {
  631. case DNS_DIFFOP_DEL:
  632. case DNS_DIFFOP_DELRESIGN:
  633. bop = 1;
  634. break;
  635. case DNS_DIFFOP_ADD:
  636. case DNS_DIFFOP_ADDRESIGN:
  637. bop = 0;
  638. break;
  639. default:
  640. INSIST(0);
  641. }
  642. r = bop - aop;
  643. if (r != 0)
  644. return (r);
  645. r = (b->rdata.type == dns_rdatatype_soa) -
  646. (a->rdata.type == dns_rdatatype_soa);
  647. if (r != 0)
  648. return (r);
  649. r = (a->rdata.type - b->rdata.type);
  650. return (r);
  651. }
  652. /*
  653. * Advance '*pos' to the next journal transaction.
  654. *
  655. * Requires:
  656. * *pos refers to a valid journal transaction.
  657. *
  658. * Ensures:
  659. * When ISC_R_SUCCESS is returned,
  660. * *pos refers to the next journal transaction.
  661. *
  662. * Returns one of:
  663. *
  664. * ISC_R_SUCCESS
  665. * ISC_R_NOMORE *pos pointed at the last transaction
  666. * Other results due to file errors are possible.
  667. */
  668. static isc_result_t
  669. journal_next(dns_journal_t *j, journal_pos_t *pos) {
  670. isc_result_t result;
  671. journal_xhdr_t xhdr;
  672. REQUIRE(DNS_JOURNAL_VALID(j));
  673. result = journal_seek(j, pos->offset);
  674. if (result != ISC_R_SUCCESS)
  675. return (result);
  676. if (pos->serial == j->header.end.serial)
  677. return (ISC_R_NOMORE);
  678. /*
  679. * Read the header of the current transaction.
  680. * This will return ISC_R_NOMORE if we are at EOF.
  681. */
  682. result = journal_read_xhdr(j, &xhdr);
  683. if (result != ISC_R_SUCCESS)
  684. return (result);
  685. /*
  686. * Check serial number consistency.
  687. */
  688. if (xhdr.serial0 != pos->serial) {
  689. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  690. "%s: journal file corrupt: "
  691. "expected serial %u, got %u",
  692. j->filename, pos->serial, xhdr.serial0);
  693. return (ISC_R_UNEXPECTED);
  694. }
  695. /*
  696. * Check for offset wraparound.
  697. */
  698. if ((isc_offset_t)(pos->offset + sizeof(journal_rawxhdr_t) + xhdr.size)
  699. < pos->offset) {
  700. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  701. "%s: offset too large", j->filename);
  702. return (ISC_R_UNEXPECTED);
  703. }
  704. pos->offset += sizeof(journal_rawxhdr_t) + xhdr.size;
  705. pos->serial = xhdr.serial1;
  706. return (ISC_R_SUCCESS);
  707. }
  708. /*
  709. * If the index of the journal 'j' contains an entry "better"
  710. * than '*best_guess', replace '*best_guess' with it.
  711. *
  712. * "Better" means having a serial number closer to 'serial'
  713. * but not greater than 'serial'.
  714. */
  715. static void
  716. index_find(dns_journal_t *j, isc_uint32_t serial, journal_pos_t *best_guess) {
  717. unsigned int i;
  718. if (j->index == NULL)
  719. return;
  720. for (i = 0; i < j->header.index_size; i++) {
  721. if (POS_VALID(j->index[i]) &&
  722. DNS_SERIAL_GE(serial, j->index[i].serial) &&
  723. DNS_SERIAL_GT(j->index[i].serial, best_guess->serial))
  724. *best_guess = j->index[i];
  725. }
  726. }
  727. /*
  728. * Add a new index entry. If there is no room, make room by removing
  729. * the odd-numbered entries and compacting the others into the first
  730. * half of the index. This decimates old index entries exponentially
  731. * over time, so that the index always contains a much larger fraction
  732. * of recent serial numbers than of old ones. This is deliberate -
  733. * most index searches are for outgoing IXFR, and IXFR tends to request
  734. * recent versions more often than old ones.
  735. */
  736. static void
  737. index_add(dns_journal_t *j, journal_pos_t *pos) {
  738. unsigned int i;
  739. if (j->index == NULL)
  740. return;
  741. /*
  742. * Search for a vacant position.
  743. */
  744. for (i = 0; i < j->header.index_size; i++) {
  745. if (! POS_VALID(j->index[i]))
  746. break;
  747. }
  748. if (i == j->header.index_size) {
  749. unsigned int k = 0;
  750. /*
  751. * Found no vacant position. Make some room.
  752. */
  753. for (i = 0; i < j->header.index_size; i += 2) {
  754. j->index[k++] = j->index[i];
  755. }
  756. i = k; /* 'i' identifies the first vacant position. */
  757. while (k < j->header.index_size) {
  758. POS_INVALIDATE(j->index[k]);
  759. k++;
  760. }
  761. }
  762. INSIST(i < j->header.index_size);
  763. INSIST(! POS_VALID(j->index[i]));
  764. /*
  765. * Store the new index entry.
  766. */
  767. j->index[i] = *pos;
  768. }
  769. /*
  770. * Invalidate any existing index entries that could become
  771. * ambiguous when a new transaction with number 'serial' is added.
  772. */
  773. static void
  774. index_invalidate(dns_journal_t *j, isc_uint32_t serial) {
  775. unsigned int i;
  776. if (j->index == NULL)
  777. return;
  778. for (i = 0; i < j->header.index_size; i++) {
  779. if (! DNS_SERIAL_GT(serial, j->index[i].serial))
  780. POS_INVALIDATE(j->index[i]);
  781. }
  782. }
  783. /*
  784. * Try to find a transaction with initial serial number 'serial'
  785. * in the journal 'j'.
  786. *
  787. * If found, store its position at '*pos' and return ISC_R_SUCCESS.
  788. *
  789. * If 'serial' is current (= the ending serial number of the
  790. * last transaction in the journal), set '*pos' to
  791. * the position immediately following the last transaction and
  792. * return ISC_R_SUCCESS.
  793. *
  794. * If 'serial' is within the range of addressable serial numbers
  795. * covered by the journal but that particular serial number is missing
  796. * (from the journal, not just from the index), return ISC_R_NOTFOUND.
  797. *
  798. * If 'serial' is outside the range of addressable serial numbers
  799. * covered by the journal, return ISC_R_RANGE.
  800. *
  801. */
  802. static isc_result_t
  803. journal_find(dns_journal_t *j, isc_uint32_t serial, journal_pos_t *pos) {
  804. isc_result_t result;
  805. journal_pos_t current_pos;
  806. REQUIRE(DNS_JOURNAL_VALID(j));
  807. if (DNS_SERIAL_GT(j->header.begin.serial, serial))
  808. return (ISC_R_RANGE);
  809. if (DNS_SERIAL_GT(serial, j->header.end.serial))
  810. return (ISC_R_RANGE);
  811. if (serial == j->header.end.serial) {
  812. *pos = j->header.end;
  813. return (ISC_R_SUCCESS);
  814. }
  815. current_pos = j->header.begin;
  816. index_find(j, serial, &current_pos);
  817. while (current_pos.serial != serial) {
  818. if (DNS_SERIAL_GT(current_pos.serial, serial))
  819. return (ISC_R_NOTFOUND);
  820. result = journal_next(j, &current_pos);
  821. if (result != ISC_R_SUCCESS)
  822. return (result);
  823. }
  824. *pos = current_pos;
  825. return (ISC_R_SUCCESS);
  826. }
  827. isc_result_t
  828. dns_journal_begin_transaction(dns_journal_t *j) {
  829. isc_uint32_t offset;
  830. isc_result_t result;
  831. journal_rawxhdr_t hdr;
  832. REQUIRE(DNS_JOURNAL_VALID(j));
  833. REQUIRE(j->state == JOURNAL_STATE_WRITE);
  834. /*
  835. * Find the file offset where the new transaction should
  836. * be written, and seek there.
  837. */
  838. if (JOURNAL_EMPTY(&j->header)) {
  839. offset = sizeof(journal_rawheader_t) +
  840. j->header.index_size * sizeof(journal_rawpos_t);
  841. } else {
  842. offset = j->header.end.offset;
  843. }
  844. j->x.pos[0].offset = offset;
  845. j->x.pos[1].offset = offset; /* Initial value, will be incremented. */
  846. j->x.n_soa = 0;
  847. CHECK(journal_seek(j, offset));
  848. /*
  849. * Write a dummy transaction header of all zeroes to reserve
  850. * space. It will be filled in when the transaction is
  851. * finished.
  852. */
  853. memset(&hdr, 0, sizeof(hdr));
  854. CHECK(journal_write(j, &hdr, sizeof(hdr)));
  855. j->x.pos[1].offset = j->offset;
  856. j->state = JOURNAL_STATE_TRANSACTION;
  857. result = ISC_R_SUCCESS;
  858. failure:
  859. return (result);
  860. }
  861. isc_result_t
  862. dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff) {
  863. dns_difftuple_t *t;
  864. isc_buffer_t buffer;
  865. void *mem = NULL;
  866. unsigned int size;
  867. isc_result_t result;
  868. isc_region_t used;
  869. REQUIRE(DNS_DIFF_VALID(diff));
  870. REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
  871. isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "writing to journal");
  872. (void)dns_diff_print(diff, NULL);
  873. /*
  874. * Pass 1: determine the buffer size needed, and
  875. * keep track of SOA serial numbers.
  876. */
  877. size = 0;
  878. for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
  879. t = ISC_LIST_NEXT(t, link))
  880. {
  881. if (t->rdata.type == dns_rdatatype_soa) {
  882. if (j->x.n_soa < 2)
  883. j->x.pos[j->x.n_soa].serial =
  884. dns_soa_getserial(&t->rdata);
  885. j->x.n_soa++;
  886. }
  887. size += sizeof(journal_rawrrhdr_t);
  888. size += t->name.length; /* XXX should have access macro? */
  889. size += 10;
  890. size += t->rdata.length;
  891. }
  892. mem = isc_mem_get(j->mctx, size);
  893. if (mem == NULL)
  894. return (ISC_R_NOMEMORY);
  895. isc_buffer_init(&buffer, mem, size);
  896. /*
  897. * Pass 2. Write RRs to buffer.
  898. */
  899. for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
  900. t = ISC_LIST_NEXT(t, link))
  901. {
  902. /*
  903. * Write the RR header.
  904. */
  905. isc_buffer_putuint32(&buffer, t->name.length + 10 +
  906. t->rdata.length);
  907. /*
  908. * Write the owner name, RR header, and RR data.
  909. */
  910. isc_buffer_putmem(&buffer, t->name.ndata, t->name.length);
  911. isc_buffer_putuint16(&buffer, t->rdata.type);
  912. isc_buffer_putuint16(&buffer, t->rdata.rdclass);
  913. isc_buffer_putuint32(&buffer, t->ttl);
  914. INSIST(t->rdata.length < 65536);
  915. isc_buffer_putuint16(&buffer, (isc_uint16_t)t->rdata.length);
  916. INSIST(isc_buffer_availablelength(&buffer) >= t->rdata.length);
  917. isc_buffer_putmem(&buffer, t->rdata.data, t->rdata.length);
  918. }
  919. isc_buffer_usedregion(&buffer, &used);
  920. INSIST(used.length == size);
  921. j->x.pos[1].offset += used.length;
  922. /*
  923. * Write the buffer contents to the journal file.
  924. */
  925. CHECK(journal_write(j, used.base, used.length));
  926. result = ISC_R_SUCCESS;
  927. failure:
  928. if (mem != NULL)
  929. isc_mem_put(j->mctx, mem, size);
  930. return (result);
  931. }
  932. isc_result_t
  933. dns_journal_commit(dns_journal_t *j) {
  934. isc_result_t result;
  935. journal_rawheader_t rawheader;
  936. REQUIRE(DNS_JOURNAL_VALID(j));
  937. REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
  938. /*
  939. * Perform some basic consistency checks.
  940. */
  941. if (j->x.n_soa != 2) {
  942. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  943. "%s: malformed transaction: %d SOAs",
  944. j->filename, j->x.n_soa);
  945. return (ISC_R_UNEXPECTED);
  946. }
  947. if (! (DNS_SERIAL_GT(j->x.pos[1].serial, j->x.pos[0].serial) ||
  948. (bind8_compat &&
  949. j->x.pos[1].serial == j->x.pos[0].serial)))
  950. {
  951. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  952. "%s: malformed transaction: serial number "
  953. "would decrease", j->filename);
  954. return (ISC_R_UNEXPECTED);
  955. }
  956. if (! JOURNAL_EMPTY(&j->header)) {
  957. if (j->x.pos[0].serial != j->header.end.serial) {
  958. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  959. "malformed transaction: "
  960. "%s last serial %u != "
  961. "transaction first serial %u",
  962. j->filename,
  963. j->header.end.serial,
  964. j->x.pos[0].serial);
  965. return (ISC_R_UNEXPECTED);
  966. }
  967. }
  968. /*
  969. * Some old journal entries may become non-addressable
  970. * when we increment the current serial number. Purge them
  971. * by stepping header.begin forward to the first addressable
  972. * transaction. Also purge them from the index.
  973. */
  974. if (! JOURNAL_EMPTY(&j->header)) {
  975. while (! DNS_SERIAL_GT(j->x.pos[1].serial,
  976. j->header.begin.serial)) {
  977. CHECK(journal_next(j, &j->header.begin));
  978. }
  979. index_invalidate(j, j->x.pos[1].serial);
  980. }
  981. #ifdef notyet
  982. if (DNS_SERIAL_GT(last_dumped_serial, j->x.pos[1].serial)) {
  983. force_dump(...);
  984. }
  985. #endif
  986. /*
  987. * Commit the transaction data to stable storage.
  988. */
  989. CHECK(journal_fsync(j));
  990. /*
  991. * Update the transaction header.
  992. */
  993. CHECK(journal_seek(j, j->x.pos[0].offset));
  994. CHECK(journal_write_xhdr(j, (j->x.pos[1].offset - j->x.pos[0].offset) -
  995. sizeof(journal_rawxhdr_t),
  996. j->x.pos[0].serial, j->x.pos[1].serial));
  997. /*
  998. * Update the journal header.
  999. */
  1000. if (JOURNAL_EMPTY(&j->header)) {
  1001. j->header.begin = j->x.pos[0];
  1002. }
  1003. j->header.end = j->x.pos[1];
  1004. journal_header_encode(&j->header, &rawheader);
  1005. CHECK(journal_seek(j, 0));
  1006. CHECK(journal_write(j, &rawheader, sizeof(rawheader)));
  1007. /*
  1008. * Update the index.
  1009. */
  1010. index_add(j, &j->x.pos[0]);
  1011. /*
  1012. * Convert the index into on-disk format and write
  1013. * it to disk.
  1014. */
  1015. CHECK(index_to_disk(j));
  1016. /*
  1017. * Commit the header to stable storage.
  1018. */
  1019. CHECK(journal_fsync(j));
  1020. /*
  1021. * We no longer have a transaction open.
  1022. */
  1023. j->state = JOURNAL_STATE_WRITE;
  1024. result = ISC_R_SUCCESS;
  1025. failure:
  1026. return (result);
  1027. }
  1028. isc_result_t
  1029. dns_journal_write_transaction(dns_journal_t *j, dns_diff_t *diff) {
  1030. isc_result_t result;
  1031. CHECK(dns_diff_sort(diff, ixfr_order));
  1032. CHECK(dns_journal_begin_transaction(j));
  1033. CHECK(dns_journal_writediff(j, diff));
  1034. CHECK(dns_journal_commit(j));
  1035. result = ISC_R_SUCCESS;
  1036. failure:
  1037. return (result);
  1038. }
  1039. void
  1040. dns_journal_destroy(dns_journal_t **journalp) {
  1041. dns_journal_t *j = *journalp;
  1042. REQUIRE(DNS_JOURNAL_VALID(j));
  1043. j->it.result = ISC_R_FAILURE;
  1044. dns_name_invalidate(&j->it.name);
  1045. dns_decompress_invalidate(&j->it.dctx);
  1046. if (j->rawindex != NULL)
  1047. isc_mem_put(j->mctx, j->rawindex, j->header.index_size *
  1048. sizeof(journal_rawpos_t));
  1049. if (j->index != NULL)
  1050. isc_mem_put(j->mctx, j->index, j->header.index_size *
  1051. sizeof(journal_pos_t));
  1052. if (j->it.target.base != NULL)
  1053. isc_mem_put(j->mctx, j->it.target.base, j->it.target.length);
  1054. if (j->it.source.base != NULL)
  1055. isc_mem_put(j->mctx, j->it.source.base, j->it.source.length);
  1056. if (j->fp != NULL)
  1057. (void)isc_stdio_close(j->fp);
  1058. j->magic = 0;
  1059. isc_mem_put(j->mctx, j, sizeof(*j));
  1060. *journalp = NULL;
  1061. }
  1062. /*
  1063. * Roll the open journal 'j' into the database 'db'.
  1064. * A new database version will be created.
  1065. */
  1066. /* XXX Share code with incoming IXFR? */
  1067. static isc_result_t
  1068. roll_forward(dns_journal_t *j, dns_db_t *db, unsigned int options,
  1069. isc_uint32_t resign)
  1070. {
  1071. isc_buffer_t source; /* Transaction data from disk */
  1072. isc_buffer_t target; /* Ditto after _fromwire check */
  1073. isc_uint32_t db_serial; /* Database SOA serial */
  1074. isc_uint32_t end_serial; /* Last journal SOA serial */
  1075. isc_result_t result;
  1076. dns_dbversion_t *ver = NULL;
  1077. journal_pos_t pos;
  1078. dns_diff_t diff;
  1079. unsigned int n_soa = 0;
  1080. unsigned int n_put = 0;
  1081. dns_diffop_t op;
  1082. REQUIRE(DNS_JOURNAL_VALID(j));
  1083. REQUIRE(DNS_DB_VALID(db));
  1084. dns_diff_init(j->mctx, &diff);
  1085. diff.resign = resign;
  1086. /*
  1087. * Set up empty initial buffers for unchecked and checked
  1088. * wire format transaction data. They will be reallocated
  1089. * later.
  1090. */
  1091. isc_buffer_init(&source, NULL, 0);
  1092. isc_buffer_init(&target, NULL, 0);
  1093. /*
  1094. * Create the new database version.
  1095. */
  1096. CHECK(dns_db_newversion(db, &ver));
  1097. /*
  1098. * Get the current database SOA serial number.
  1099. */
  1100. CHECK(dns_db_getsoaserial(db, ver, &db_serial));
  1101. /*
  1102. * Locate a journal entry for the current database serial.
  1103. */
  1104. CHECK(journal_find(j, db_serial, &pos));
  1105. /*
  1106. * XXX do more drastic things, like marking zone stale,
  1107. * if this fails?
  1108. */
  1109. /*
  1110. * XXXRTH The zone code should probably mark the zone as bad and
  1111. * scream loudly into the log if this is a dynamic update
  1112. * log reply that failed.
  1113. */
  1114. end_serial = dns_journal_last_serial(j);
  1115. if (db_serial == end_serial)
  1116. CHECK(DNS_R_UPTODATE);
  1117. CHECK(dns_journal_iter_init(j, db_serial, end_serial));
  1118. for (result = dns_journal_first_rr(j);
  1119. result == ISC_R_SUCCESS;
  1120. result = dns_journal_next_rr(j))
  1121. {
  1122. dns_name_t *name;
  1123. isc_uint32_t ttl;
  1124. dns_rdata_t *rdata;
  1125. dns_difftuple_t *tuple = NULL;
  1126. name = NULL;
  1127. rdata = NULL;
  1128. dns_journal_current_rr(j, &name, &ttl, &rdata);
  1129. if (rdata->type == dns_rdatatype_soa) {
  1130. n_soa++;
  1131. if (n_soa == 2)
  1132. db_serial = j->it.current_serial;
  1133. }
  1134. if (n_soa == 3)
  1135. n_soa = 1;
  1136. if (n_soa == 0) {
  1137. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  1138. "%s: journal file corrupt: missing "
  1139. "initial SOA", j->filename);
  1140. FAIL(ISC_R_UNEXPECTED);
  1141. }
  1142. if ((options & DNS_JOURNALOPT_RESIGN) != 0)
  1143. op = (n_soa == 1) ? DNS_DIFFOP_DELRESIGN :
  1144. DNS_DIFFOP_ADDRESIGN;
  1145. else
  1146. op = (n_soa == 1) ? DNS_DIFFOP_DEL : DNS_DIFFOP_ADD;
  1147. CHECK(dns_difftuple_create(diff.mctx, op, name, ttl, rdata,
  1148. &tuple));
  1149. dns_diff_append(&diff, &tuple);
  1150. if (++n_put > 100) {
  1151. isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
  1152. "%s: applying diff to database (%u)",
  1153. j->filename, db_serial);
  1154. (void)dns_diff_print(&diff, NULL);
  1155. CHECK(dns_diff_apply(&diff, db, ver));
  1156. dns_diff_clear(&diff);
  1157. n_put = 0;
  1158. }
  1159. }
  1160. if (result == ISC_R_NOMORE)
  1161. result = ISC_R_SUCCESS;
  1162. CHECK(result);
  1163. if (n_put != 0) {
  1164. isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
  1165. "%s: applying final diff to database (%u)",
  1166. j->filename, db_serial);
  1167. (void)dns_diff_print(&diff, NULL);
  1168. CHECK(dns_diff_apply(&diff, db, ver));
  1169. dns_diff_clear(&diff);
  1170. }
  1171. failure:
  1172. if (ver != NULL)
  1173. dns_db_closeversion(db, &ver, result == ISC_R_SUCCESS ?
  1174. ISC_TRUE : ISC_FALSE);
  1175. if (source.base != NULL)
  1176. isc_mem_put(j->mctx, source.base, source.length);
  1177. if (target.base != NULL)
  1178. isc_mem_put(j->mctx, target.base, target.length);
  1179. dns_diff_clear(&diff);
  1180. return (result);
  1181. }
  1182. isc_result_t
  1183. dns_journal_rollforward(isc_mem_t *mctx, dns_db_t *db,
  1184. unsigned int options, const char *filename)
  1185. {
  1186. REQUIRE((options & DNS_JOURNALOPT_RESIGN) == 0);
  1187. return (dns_journal_rollforward2(mctx, db, options, 0, filename));
  1188. }
  1189. isc_result_t
  1190. dns_journal_rollforward2(isc_mem_t *mctx, dns_db_t *db, unsigned int options,
  1191. isc_uint32_t resign, const char *filename)
  1192. {
  1193. dns_journal_t *j;
  1194. isc_result_t result;
  1195. REQUIRE(DNS_DB_VALID(db));
  1196. REQUIRE(filename != NULL);
  1197. j = NULL;
  1198. result = dns_journal_open(mctx, filename, ISC_FALSE, &j);
  1199. if (result == ISC_R_NOTFOUND) {
  1200. isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
  1201. "no journal file, but that's OK");
  1202. return (DNS_R_NOJOURNAL);
  1203. }
  1204. if (result != ISC_R_SUCCESS)
  1205. return (result);
  1206. if (JOURNAL_EMPTY(&j->header))
  1207. result = DNS_R_UPTODATE;
  1208. else
  1209. result = roll_forward(j, db, options, resign);
  1210. dns_journal_destroy(&j);
  1211. return (result);
  1212. }
  1213. isc_result_t
  1214. dns_journal_print(isc_mem_t *mctx, const char *filename, FILE *file) {
  1215. dns_journal_t *j;
  1216. isc_buffer_t source; /* Transaction data from disk */
  1217. isc_buffer_t target; /* Ditto after _fromwire check */
  1218. isc_uint32_t start_serial; /* Database SOA serial */
  1219. isc_uint32_t end_serial; /* Last journal SOA serial */
  1220. isc_result_t result;
  1221. dns_diff_t diff;
  1222. unsigned int n_soa = 0;
  1223. unsigned int n_put = 0;
  1224. REQUIRE(filename != NULL);
  1225. j = NULL;
  1226. result = dns_journal_open(mctx, filename, ISC_FALSE, &j);
  1227. if (result == ISC_R_NOTFOUND) {
  1228. isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no journal file");
  1229. return (DNS_R_NOJOURNAL);
  1230. }
  1231. if (result != ISC_R_SUCCESS) {
  1232. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  1233. "journal open failure: %s: %s",
  1234. isc_result_totext(result), filename);
  1235. return (result);
  1236. }
  1237. dns_diff_init(j->mctx, &diff);
  1238. /*
  1239. * Set up empty initial buffers for unchecked and checked
  1240. * wire format transaction data. They will be reallocated
  1241. * later.
  1242. */
  1243. isc_buffer_init(&source, NULL, 0);
  1244. isc_buffer_init(&target, NULL, 0);
  1245. start_serial = dns_journal_first_serial(j);
  1246. end_serial = dns_journal_last_serial(j);
  1247. CHECK(dns_journal_iter_init(j, start_serial, end_serial));
  1248. for (result = dns_journal_first_rr(j);
  1249. result == ISC_R_SUCCESS;
  1250. result = dns_journal_next_rr(j))
  1251. {
  1252. dns_name_t *name;
  1253. isc_uint32_t ttl;
  1254. dns_rdata_t *rdata;
  1255. dns_difftuple_t *tuple = NULL;
  1256. name = NULL;
  1257. rdata = NULL;
  1258. dns_journal_current_rr(j, &name, &ttl, &rdata);
  1259. if (rdata->type == dns_rdatatype_soa)
  1260. n_soa++;
  1261. if (n_soa == 3)
  1262. n_soa = 1;
  1263. if (n_soa == 0) {
  1264. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  1265. "%s: journal file corrupt: missing "
  1266. "initial SOA", j->filename);
  1267. FAIL(ISC_R_UNEXPECTED);
  1268. }
  1269. CHECK(dns_difftuple_create(diff.mctx, n_soa == 1 ?
  1270. DNS_DIFFOP_DEL : DNS_DIFFOP_ADD,
  1271. name, ttl, rdata, &tuple));
  1272. dns_diff_append(&diff, &tuple);
  1273. if (++n_put > 100) {
  1274. result = dns_diff_print(&diff, file);
  1275. dns_diff_clear(&diff);
  1276. n_put = 0;
  1277. if (result != ISC_R_SUCCESS)
  1278. break;
  1279. }
  1280. }
  1281. if (result == ISC_R_NOMORE)
  1282. result = ISC_R_SUCCESS;
  1283. CHECK(result);
  1284. if (n_put != 0) {
  1285. result = dns_diff_print(&diff, file);
  1286. dns_diff_clear(&diff);
  1287. }
  1288. goto cleanup;
  1289. failure:
  1290. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  1291. "%s: cannot print: journal file corrupt", j->filename);
  1292. cleanup:
  1293. if (source.base != NULL)
  1294. isc_mem_put(j->mctx, source.base, source.length);
  1295. if (target.base != NULL)
  1296. isc_mem_put(j->mctx, target.base, target.length);
  1297. dns_diff_clear(&diff);
  1298. dns_journal_destroy(&j);
  1299. return (result);
  1300. }
  1301. /**************************************************************************/
  1302. /*
  1303. * Miscellaneous accessors.
  1304. */
  1305. isc_uint32_t dns_journal_first_serial(dns_journal_t *j) {
  1306. return (j->header.begin.serial);
  1307. }
  1308. isc_uint32_t dns_journal_last_serial(dns_journal_t *j) {
  1309. return (j->header.end.serial);
  1310. }
  1311. /**************************************************************************/
  1312. /*
  1313. * Iteration support.
  1314. *
  1315. * When serving an outgoing IXFR, we transmit a part the journal starting
  1316. * at the serial number in the IXFR request and ending at the serial
  1317. * number that is current when the IXFR request arrives. The ending
  1318. * serial number is not necessarily at the end of the journal:
  1319. * the journal may grow while the IXFR is in progress, but we stop
  1320. * when we reach the serial number that was current when the IXFR started.
  1321. */
  1322. static isc_result_t read_one_rr(dns_journal_t *j);
  1323. /*
  1324. * Make sure the buffer 'b' is has at least 'size' bytes
  1325. * allocated, and clear it.
  1326. *
  1327. * Requires:
  1328. * Either b->base is NULL, or it points to b->length bytes of memory
  1329. * previously allocated by isc_mem_get().
  1330. */
  1331. static isc_result_t
  1332. size_buffer(isc_mem_t *mctx, isc_buffer_t *b, unsigned size) {
  1333. if (b->length < size) {
  1334. void *mem = isc_mem_get(mctx, size);
  1335. if (mem == NULL)
  1336. return (ISC_R_NOMEMORY);
  1337. if (b->base != NULL)
  1338. isc_mem_put(mctx, b->base, b->length);
  1339. b->base = mem;
  1340. b->length = size;
  1341. }
  1342. isc_buffer_clear(b);
  1343. return (ISC_R_SUCCESS);
  1344. }
  1345. isc_result_t
  1346. dns_journal_iter_init(dns_journal_t *j,
  1347. isc_uint32_t begin_serial, isc_uint32_t end_serial)
  1348. {
  1349. isc_result_t result;
  1350. CHECK(journal_find(j, begin_serial, &j->it.bpos));
  1351. INSIST(j->it.bpos.serial == begin_serial);
  1352. CHECK(journal_find(j, end_serial, &j->it.epos));
  1353. INSIST(j->it.epos.serial == end_serial);
  1354. result = ISC_R_SUCCESS;
  1355. failure:
  1356. j->it.result = result;
  1357. return (j->it.result);
  1358. }
  1359. isc_result_t
  1360. dns_journal_first_rr(dns_journal_t *j) {
  1361. isc_result_t result;
  1362. /*
  1363. * Seek to the beginning of the first transaction we are
  1364. * interested in.
  1365. */
  1366. CHECK(journal_seek(j, j->it.bpos.offset));
  1367. j->it.current_serial = j->it.bpos.serial;
  1368. j->it.xsize = 0; /* We have no transaction data yet... */
  1369. j->it.xpos = 0; /* ...and haven't used any of it. */
  1370. return (read_one_rr(j));
  1371. failure:
  1372. return (result);
  1373. }
  1374. static isc_result_t
  1375. read_one_rr(dns_journal_t *j) {
  1376. isc_result_t result;
  1377. dns_rdatatype_t rdtype;
  1378. dns_rdataclass_t rdclass;
  1379. unsigned int rdlen;
  1380. isc_uint32_t ttl;
  1381. journal_xhdr_t xhdr;
  1382. journal_rrhdr_t rrhdr;
  1383. INSIST(j->offset <= j->it.epos.offset);
  1384. if (j->offset == j->it.epos.offset)
  1385. return (ISC_R_NOMORE);
  1386. if (j->it.xpos == j->it.xsize) {
  1387. /*
  1388. * We are at a transaction boundary.
  1389. * Read another transaction header.
  1390. */
  1391. CHECK(journal_read_xhdr(j, &xhdr));
  1392. if (xhdr.size == 0) {
  1393. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  1394. "%s: journal corrupt: empty transaction",
  1395. j->filename);
  1396. FAIL(ISC_R_UNEXPECTED);
  1397. }
  1398. if (xhdr.serial0 != j->it.current_serial) {
  1399. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  1400. "%s: journal file corrupt: "
  1401. "expected serial %u, got %u",
  1402. j->filename,
  1403. j->it.current_serial, xhdr.serial0);
  1404. FAIL(ISC_R_UNEXPECTED);
  1405. }
  1406. j->it.xsize = xhdr.size;
  1407. j->it.xpos = 0;
  1408. }
  1409. /*
  1410. * Read an RR.
  1411. */
  1412. CHECK(journal_read_rrhdr(j, &rrhdr));
  1413. /*
  1414. * Perform a sanity check on the journal RR size.
  1415. * The smallest possible RR has a 1-byte owner name
  1416. * and a 10-byte header. The largest possible
  1417. * RR has 65535 bytes of data, a header, and a maximum-
  1418. * size owner name, well below 70 k total.
  1419. */
  1420. if (rrhdr.size < 1+10 || rrhdr.size > 70000) {
  1421. isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
  1422. "%s: journal corrupt: impossible RR size "
  1423. "(%d bytes)", j->filename, rrhdr.size);
  1424. FAIL(ISC_R_UNEXPECTED);
  1425. }
  1426. CHECK(size_buffer(j->mctx, &j->it.source, rrhdr.size));
  1427. CHECK(journal_read(j, j->it.source.base, rrhdr.size));
  1428. isc_buffer_add(&j->it.source, rrhdr.size);
  1429. /*
  1430. * The target buffer is made the same size
  1431. * as the source buffer, with the assumption that when
  1432. * no compression in present, the output of dns_*_fromwire()
  1433. * is no larger than the input.
  1434. */
  1435. CHECK(size_buffer(j->mctx, &j->it.target, rrhdr.size));
  1436. /*
  1437. * Parse the owner name. We don't know where it
  1438. * ends yet, so we make the entire "remaining"
  1439. * part of the buffer "active".
  1440. */
  1441. isc_buffer_setactive(&j->it.source,
  1442. j->it.source.used - j->it.source.current);
  1443. CHECK(dns_name_fromwire(&j->it.name, &j->it.source,
  1444. &j->it.dctx, 0, &j->it.target));
  1445. /*
  1446. * Check that the RR header is there, and parse it.
  1447. */
  1448. if (isc_buffer_remaininglength(&j->it.source) < 10)
  1449. FAIL(DNS_R_FORMERR);
  1450. rdtype = isc_buffer_getuint16(&j->it.source);
  1451. rdclass = isc_buffer_getuint16(&j->it.source);
  1452. ttl = isc_buffer_getuint32(&j->it.source);
  1453. rdlen = isc_buffer_getuint16(&j->it.source);
  1454. /*
  1455. * Parse the rdata.
  1456. */
  1457. if (isc_buffer_remaininglength(&j->it.source) != rdlen)
  1458. FAIL(DNS_R_FORMERR);
  1459. isc_buffer_setactive(&j->it.source, rdlen);
  1460. dns_rdata_reset(&j->it.rdata);
  1461. CHECK(dns_rdata_fromwire(&j->it.rdata, rdclass,
  1462. rdtype, &j->it.source, &j->it.dctx,
  1463. 0, &j->it.target));
  1464. j->it.ttl = ttl;
  1465. j->it.xpos += sizeof(journal_rawrrhdr_t) + rrhdr.size;
  1466. if (rdtype == dns_rdatatype_soa) {
  1467. /* XXX could do additional consistency checks here */
  1468. j->it.current_serial = dns_soa_getserial(&j->it.rdata);
  1469. }
  1470. result = ISC_R_SUCCESS;
  1471. failure:
  1472. j->it.result = result;
  1473. return (result);
  1474. }
  1475. isc_result_t
  1476. dns_journal_next_rr(dns_journal_t *j) {
  1477. j->it.result = read_one_rr(j);
  1478. return (j->it.result);
  1479. }
  1480. void
  1481. dns_journal_current_rr(dns_journal_t *j, dns_name_t **name, isc_uint32_t *ttl,
  1482. dns_rdata_t **rdata)
  1483. {
  1484. REQUIRE(j->it.result == ISC_R_SUCCESS);
  1485. *name = &j->it.name;
  1486. *ttl = j->it.ttl;
  1487. *rdata = &j->it.rdata;
  1488. }
  1489. /**************************************************************************/
  1490. /*
  1491. * Generating diffs from databases
  1492. */
  1493. /*
  1494. * Construct a diff containing all the RRs at the current name of the
  1495. * database iterator 'dbit' in database 'db', version 'ver'.
  1496. * Set '*name' to the current name, and append the diff to 'diff'.
  1497. * All new tuples will have the operation 'op'.
  1498. *
  1499. * Requires: 'name' must have buffer large enough to hold the name.
  1500. * Typically, a dns_fixedname_t would be used.
  1501. */
  1502. static isc_result_t
  1503. get_name_diff(dns_db_t *db, dns_dbversion_t *ver, isc_stdtime_t now,
  1504. dns_dbiterator_t *dbit, dns_name_t *name, dns_diffop_t op,
  1505. dns_diff_t *diff)
  1506. {
  1507. isc_result_t result;
  1508. dns_dbnode_t *node = NULL;
  1509. dns_rdatasetiter_t *rdsiter = NULL;
  1510. dns_difftuple_t *tuple = NULL;
  1511. result = dns_dbiterator_current(dbit, &node, name);
  1512. if (result != ISC_R_SUCCESS)
  1513. return (result);
  1514. result = dns_db_allrdatasets(db, node, ver, now, &rdsiter);
  1515. if (result != ISC_R_SUCCESS)
  1516. goto cleanup_node;
  1517. for (result = dns_rdatasetiter_first(rdsiter);
  1518. result == ISC_R_SUCCESS;
  1519. result = dns_rdatasetiter_next(rdsiter))
  1520. {
  1521. dns_rdataset_t rdataset;
  1522. dns_rdataset_init(&rdataset);
  1523. dns_rdatasetiter_current(rdsiter, &rdataset);
  1524. for (result = dns_rdataset_first(&rdataset);
  1525. result == ISC_R_SUCCESS;
  1526. result = dns_rdataset_next(&rdataset))
  1527. {
  1528. dns_rdata_t rdata = DNS_RDATA_INIT;
  1529. dns_rdataset_current(&rdataset, &rdata);
  1530. result = dns_difftuple_create(diff->mctx, op, name,
  1531. rdataset.ttl, &rdata,
  1532. &tuple);
  1533. if (result != ISC_R_SUCCESS) {
  1534. dns_rdataset_disassociate(&rdataset);
  1535. goto cleanup_iterator;
  1536. }
  1537. dns_diff_append(diff, &tuple);
  1538. }
  1539. dns_rdataset_disassociate(&rdataset);
  1540. if (result != ISC_R_NOMORE)
  1541. goto cleanup_iterator;
  1542. }
  1543. if (result != ISC_R_NOMORE)
  1544. goto cleanup_iterator;
  1545. result = ISC_R_SUCCESS;
  1546. cleanup_iterator:
  1547. dns_rdatasetiter_destroy(&rdsiter);
  1548. cleanup_node:
  1549. dns_db_detachnode(db, &node);
  1550. return (result);
  1551. }
  1552. /*
  1553. * Comparison function for use by dns_diff_subtract when sorting
  1554. * the diffs to be subtracted. The sort keys are the rdata type
  1555. * and the rdata itself. The owner name is ignored, because
  1556. * it is known to be the same for all tuples.
  1557. */
  1558. static int
  1559. rdata_order(const void *av, const void *bv) {
  1560. dns_difftuple_t const * const *ap = av;
  1561. dns_difftuple_t const * const *bp = bv;
  1562. dns_difftuple_t const *a = *ap;
  1563. dns_difftuple_t const *b = *bp;
  1564. int r;
  1565. r = (b->rdata.type - a->rdata.type);
  1566. if (r != 0)
  1567. return (r);
  1568. r = dns_rdata_compare(&a->rdata, &b->rdata);
  1569. return (r);
  1570. }
  1571. static isc_result_t
  1572. dns_diff_subtract(dns_diff_t diff[2], dns_diff_t *r) {
  1573. isc_result_t result;
  1574. dns_difftuple_t *p[2];
  1575. int i, t;
  1576. isc_boolean_t append;
  1577. CHECK(dns_diff_sort(&diff[0], rdata_order));
  1578. CHECK(dns_diff_sort(&diff[1], rdata_order));
  1579. for (;;) {
  1580. p[0] = ISC_LIST_HEAD(diff[0].tuples);
  1581. p[1] = ISC_LIST_HEAD(diff[1].tuples);
  1582. if (p[0] == NULL && p[1] == NULL)
  1583. break;
  1584. for (i = 0; i < 2; i++)
  1585. if (p[!i] == NULL) {
  1586. ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
  1587. ISC_LIST_APPEND(r->tuples, p[i], link);
  1588. goto next;
  1589. }
  1590. t = rdata_order(&p[0], &p[1]);
  1591. if (t < 0) {
  1592. ISC_LIST_UNLINK(diff[0].tuples, p[0], link);
  1593. ISC_LIST_APPEND(r->tuples, p[0], link);
  1594. goto next;
  1595. }
  1596. if (t > 0) {
  1597. ISC_LIST_UNLINK(diff[1].tuples, p[1], link);
  1598. ISC_LIST_APPEND(r->tuples, p[1], link);
  1599. goto next;
  1600. }
  1601. INSIST(t == 0);
  1602. /*
  1603. * Identical RRs in both databases; skip them both
  1604. * if the ttl differs.
  1605. */
  1606. append = ISC_TF(p[0]->ttl != p[1]->ttl);
  1607. for (i = 0; i < 2; i++) {
  1608. ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
  1609. if (append) {
  1610. ISC_LIST_APPEND(r->tuples, p[i], link);
  1611. } else {
  1612. dns_difftuple_free(&p[i]);
  1613. }
  1614. }
  1615. next: ;
  1616. }
  1617. result = ISC_R_SUCCESS;
  1618. failure:
  1619. return (result);
  1620. }
  1621. static isc_result_t
  1622. diff_namespace(isc_mem_t *mctx,
  1623. dns_db_t *dba, dns_dbversion_t *dbvera,
  1624. dns_db_t *dbb, dns_dbversion_t *dbverb,
  1625. unsigned int options, dns_diff_t *resultdiff)
  1626. {
  1627. dns_db_t *db[2];
  1628. dns_dbversion_t *ver[2];
  1629. dns_dbiterator_t *dbit[2] = { NULL, NULL };
  1630. isc_boolean_t have[2] = { ISC_FALSE, ISC_FALSE };
  1631. dns_fixedname_t fixname[2];
  1632. isc_result_t result, itresult[2];
  1633. dns_diff_t diff[2];
  1634. int i, t;
  1635. db[0] = dba, db[1] = dbb;
  1636. ver[0] = dbvera, ver[1] = dbverb;
  1637. dns_diff_init(mctx, &diff[0]);
  1638. dns_diff_init(mctx, &diff[1]);
  1639. dns_fixedname_init(&fixname[0]);
  1640. dns_fixedname_init(&fixname[1]);
  1641. result = dns_db_createiterator(db[0], options, &dbit[0]);
  1642. if (result != ISC_R_SUCCESS)
  1643. return (result);
  1644. result = dns_db_createiterator(db[1], options, &dbit[1]);
  1645. if (result != ISC_R_SUCCESS)
  1646. goto cleanup_iterator;
  1647. itresult[0] = dns_dbiterator_first(dbit[0]);
  1648. itresult[1] = dns_dbiterator_first(dbit[1]);
  1649. for (;;) {
  1650. for (i = 0; i < 2; i++) {
  1651. if (! have[i] && itresult[i] == ISC_R_SUCCESS) {
  1652. CHECK(get_name_diff(db[i], ver[i], 0, dbit[i],
  1653. dns_fixedname_name(&fixname[i]),
  1654. i == 0 ?
  1655. DNS_DIFFOP_ADD :
  1656. DNS_DIFFOP_DEL,
  1657. &diff[i]));
  1658. itresult[i] = dns_dbiterator_next(dbit[i]);
  1659. have[i] = ISC_TRUE;
  1660. }
  1661. }
  1662. if (! have[0] && ! have[1]) {
  1663. INSIST(ISC_LIST_EMPTY(diff[0].tuples));
  1664. INSIST(ISC_LIST_EMPTY(diff[1].tuples));
  1665. break;
  1666. }
  1667. for (i = 0; i < 2; i++) {
  1668. if (! have[!i]) {
  1669. ISC_LIST_APPENDLIST(resultdiff->tuples,
  1670. diff[i].tuples, link);
  1671. INSIST(ISC_LIST_EMPTY(diff[i].tuples));
  1672. have[i] = ISC_FALSE;
  1673. goto next;
  1674. }
  1675. }
  1676. t = dns_name_compare(dns_fixedname_name(&fixname[0]),
  1677. dns_fixedname_name(&fixname[1]));
  1678. if (t < 0) {
  1679. ISC_LIST_APPENDLIST(resultdiff->tuples,
  1680. diff[0].tuples, link);
  1681. INSIST(ISC_LIST_EMPTY(diff[0].tuples));
  1682. have[0] = ISC_FALSE;
  1683. continue;
  1684. }
  1685. if (t > 0) {
  1686. ISC_LIST_APPENDLIST(resultdiff->tuples,
  1687. diff[1].tuples, link);
  1688. INSIST(ISC_LIST_EMPTY(diff[1].tuples));
  1689. have[1] = ISC_FALSE;
  1690. continue;
  1691. }
  1692. INSIST(t == 0);
  1693. CHECK(dns_diff_subtract(diff, resultdiff));
  1694. INSIST(ISC_LIST_EMPTY(diff[0].tuples));
  1695. INSIST(ISC_LIST_EMPTY(diff[1].tuples));
  1696. have[0] = have[1] = ISC_FALSE;
  1697. next: ;
  1698. }
  1699. if (itresult[0] != ISC_R_NOMORE)
  1700. FAIL(itresult[0]);
  1701. if (itresult[1] != ISC_R_NOMORE)
  1702. FAIL(itresult[1]);
  1703. INSIST(ISC_LIST_EMPTY(diff[0].tuples));
  1704. INSIST(ISC_LIST_EMPTY(diff[1].tuples));
  1705. failure:
  1706. dns_dbiterator_destroy(&dbit[1]);
  1707. cleanup_iterator:
  1708. dns_dbiterator_destroy(&dbit[0]);
  1709. return (result);
  1710. }
  1711. /*
  1712. * Compare the databases 'dba' and 'dbb' and generate a journal
  1713. * entry containing the changes to make 'dba' from 'dbb' (note
  1714. * the order). This journal entry will consist of a single,
  1715. * possibly very large transaction.
  1716. */
  1717. isc_result_t
  1718. dns_db_diff(isc_mem_t *mctx,
  1719. dns_db_t *dba, dns_dbversion_t *dbvera,
  1720. dns_db_t *dbb, dns_dbversion_t *dbverb,
  1721. const char *journal_filename)
  1722. {
  1723. isc_result_t result;
  1724. dns_journal_t *journal = NULL;
  1725. dns_diff_t resultdiff;
  1726. result = dns_journal_open(mctx, journal_filename, ISC_TRUE, &journal);
  1727. if (result != ISC_R_SUCCESS)
  1728. return (result);
  1729. dns_diff_init(mctx, &resultdiff);
  1730. CHECK(diff_namespace(mctx, dba, dbvera, dbb, dbverb,
  1731. DNS_DB_NONSEC3, &resultdiff));
  1732. CHECK(diff_namespace(mctx, dba, dbvera, dbb, dbverb,
  1733. DNS_DB_NSEC3ONLY, &resultdiff));
  1734. if (ISC_LIST_EMPTY(resultdiff.tuples)) {
  1735. isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no changes");
  1736. } else {
  1737. CHECK(dns_journal_write_transaction(journal, &resultdiff));
  1738. }
  1739. failure:
  1740. dns_diff_clear(&resultdiff);
  1741. dns_journal_destroy(&journal);
  1742. return (result);
  1743. }
  1744. isc_result_t
  1745. dns_journal_compact(isc_mem_t *mctx, char *filename, isc_uint32_t serial,
  1746. isc_uint32_t target_size)
  1747. {
  1748. unsigned int i;
  1749. journal_pos_t best_guess;
  1750. journal_pos_t current_pos;
  1751. dns_journal_t *j = NULL;
  1752. dns_journal_t *new = NULL;
  1753. journal_rawheader_t rawheader;
  1754. unsigned int copy_length;
  1755. int namelen;
  1756. char *buf = NULL;
  1757. unsigned int size = 0;
  1758. isc_result_t result;
  1759. unsigned int indexend;
  1760. char newname[1024];
  1761. char backup[1024];
  1762. isc_boolean_t is_backup = ISC_FALSE;
  1763. namelen = strlen(filename);
  1764. if (namelen > 4 && strcmp(filename + namelen - 4, ".jnl") == 0)
  1765. namelen -= 4;
  1766. result = isc_string_printf(newname, sizeof(newname), "%.*s.jnw",
  1767. namelen, filename);
  1768. if (result != ISC_R_SUCCESS)
  1769. return (result);
  1770. result = isc_string_printf(backup, sizeof(backup), "%.*s.jbk",
  1771. namelen, filename);
  1772. if (result != ISC_R_SUCCESS)
  1773. return (result);
  1774. result = journal_open(mctx, filename, ISC_FALSE, ISC_FALSE, &j);
  1775. if (result == ISC_R_NOTFOUND) {
  1776. is_backup = ISC_TRUE;
  1777. result = journal_open(mctx, backup, ISC_FALSE, ISC_FALSE, &j);
  1778. }
  1779. if (result != ISC_R_SUCCESS)
  1780. return (result);
  1781. if (JOURNAL_EMPTY(&j->header)) {
  1782. dns_journal_destroy(&j);
  1783. return (ISC_R_SUCCESS);
  1784. }
  1785. if (DNS_SERIAL_GT(j->header.begin.serial, serial) ||
  1786. DNS_SERIAL_GT(serial, j->header.end.serial)) {
  1787. dns_journal_destroy(&j);
  1788. return (ISC_R_RANGE);
  1789. }
  1790. /*
  1791. * Cope with very small target sizes.
  1792. */
  1793. indexend = sizeof(journal_rawheader_t) +
  1794. j->header.index_size * sizeof(journal_rawpos_t);
  1795. if (target_size < indexend * 2)
  1796. target_size = target_size/2 + indexend;
  1797. /*
  1798. * See if there is any work to do.
  1799. */
  1800. if ((isc_uint32_t) j->header.end.offset < target_size) {
  1801. dns_journal_destroy(&j);
  1802. return (ISC_R_SUCCESS);
  1803. }
  1804. CHECK(journal_open(mctx, newname, ISC_TRUE, ISC_TRUE, &new));
  1805. /*
  1806. * Remove overhead so space test below can succeed.
  1807. */
  1808. if (target_size >= indexend)
  1809. target_size -= indexend;
  1810. /*
  1811. * Find if we can create enough free space.
  1812. */
  1813. best_guess = j->header.begin;
  1814. for (i = 0; i < j->header.index_size; i++) {
  1815. if (POS_VALID(j->index[i]) &&
  1816. DNS_SERIAL_GE(serial, j->index[i].serial) &&
  1817. ((isc_uint32_t)(j->header.end.offset - j->index[i].offset)
  1818. >= target_size / 2) &&
  1819. j->index[i].offset > best_guess.offset)
  1820. best_guess = j->index[i];
  1821. }
  1822. current_pos = best_guess;
  1823. while (current_pos.serial != serial) {
  1824. CHECK(journal_next(j, &current_pos));
  1825. if (current_pos.serial == j->header.end.serial)
  1826. break;
  1827. if (DNS_SERIAL_GE(serial, current_pos.serial) &&
  1828. ((isc_uint32_t)(j->header.end.offset - current_pos.offset)
  1829. >= (target_size / 2)) &&
  1830. current_pos.offset > best_guess.offset)
  1831. best_guess = current_pos;
  1832. else
  1833. break;
  1834. }
  1835. INSIST(best_guess.serial != j->header.end.serial);
  1836. if (best_guess.serial != serial)
  1837. CHECK(journal_next(j, &best_guess));
  1838. /*
  1839. * We should now be roughly half target_size provided
  1840. * we did not reach 'serial'. If not we will just copy
  1841. * all uncommitted deltas regardless of the size.
  1842. */
  1843. copy_length = j->header.end.offset - best_guess.offset;
  1844. if (copy_length != 0) {
  1845. /*
  1846. * Copy best_guess to end into space just freed.
  1847. */
  1848. size = 64*1024;
  1849. if (copy_length < size)
  1850. size = copy_length;
  1851. buf = isc_mem_get(mctx, size);
  1852. if (buf == NULL) {
  1853. result = ISC_R_NOMEMORY;
  1854. goto failure;
  1855. }
  1856. CHECK(journal_seek(j, best_guess.offset));
  1857. CHECK(journal_seek(new, indexend));
  1858. for (i = 0; i < copy_length; i += size) {
  1859. unsigned int len = (copy_length - i) > size ? size :
  1860. (copy_length - i);
  1861. CHECK(journal_read(j, buf, len));
  1862. CHECK(journal_write(new, buf, len));
  1863. }
  1864. CHECK(journal_fsync(new));
  1865. /*
  1866. * Compute new header.
  1867. */
  1868. new->header.begin.serial = best_guess.serial;
  1869. new->header.begin.offset = indexend;
  1870. new->header.end.serial = j->header.end.serial;
  1871. new->header.end.offset = indexend + copy_length;
  1872. /*
  1873. * Update the journal header.
  1874. */
  1875. journal_header_encode(&new->header, &rawheader);
  1876. CHECK(journal_seek(new, 0));
  1877. CHECK(journal_write(new, &rawheader, sizeof(rawheader)));
  1878. CHECK(journal_fsync(new));
  1879. /*
  1880. * Build new index.
  1881. */
  1882. current_pos = new->header.begin;
  1883. while (current_pos.serial != new->header.end.serial) {
  1884. index_add(new, &current_pos);
  1885. CHECK(journal_next(new, &current_pos));
  1886. }
  1887. /*
  1888. * Write index.
  1889. */
  1890. CHECK(index_to_disk(new));
  1891. CHECK(journal_fsync(new));
  1892. indexend = new->header.end.offset;
  1893. POST(indexend);
  1894. }
  1895. /*
  1896. * Close both journals before trying to rename files (this is
  1897. * necessary on WIN32).
  1898. */
  1899. dns_journal_destroy(&j);
  1900. dns_journal_destroy(&new);
  1901. /*
  1902. * With a UFS file system this should just succeed and be atomic.
  1903. * Any IXFR outs will just continue and the old journal will be
  1904. * removed on final close.
  1905. *
  1906. * With MSDOS / NTFS we need to do a two stage rename, triggered
  1907. * by EEXIST. (If any IXFR's are running in other threads, however,
  1908. * this will fail, and the journal will not be compacted. But
  1909. * if so, hopefully they'll be finished by the next time we
  1910. * compact.)
  1911. */
  1912. if (rename(newname, filename) == -1) {
  1913. if (errno == EEXIST && !is_backup) {
  1914. result = isc_file_remove(backup);
  1915. if (result != ISC_R_SUCCESS &&
  1916. result != ISC_R_FILENOTFOUND)
  1917. goto failure;
  1918. if (rename(filename, backup) == -1)
  1919. goto maperrno;
  1920. if (rename(newname, filename) == -1)
  1921. goto maperrno;
  1922. (void)isc_file_remove(backup);
  1923. } else {
  1924. maperrno:
  1925. result = ISC_R_FAILURE;
  1926. goto failure;
  1927. }
  1928. }
  1929. result = ISC_R_SUCCESS;
  1930. failure:
  1931. (void)isc_file_remove(newname);
  1932. if (buf != NULL)
  1933. isc_mem_put(mctx, buf, size);
  1934. if (j != NULL)
  1935. dns_journal_destroy(&j);
  1936. if (new != NULL)
  1937. dns_journal_destroy(&new);
  1938. return (result);
  1939. }
  1940. static isc_result_t
  1941. index_to_disk(dns_journal_t *j) {
  1942. isc_result_t result = ISC_R_SUCCESS;
  1943. if (j->header.index_size != 0) {
  1944. unsigned int i;
  1945. unsigned char *p;
  1946. unsigned int rawbytes;
  1947. rawbytes = j->header.index_size * sizeof(journal_rawpos_t);
  1948. p = j->rawindex;
  1949. for (i = 0; i < j->header.index_size; i++) {
  1950. encode_uint32(j->index[i].serial, p);
  1951. p += 4;
  1952. encode_uint32(j->index[i].offset, p);
  1953. p += 4;
  1954. }
  1955. INSIST(p == j->rawindex + rawbytes);
  1956. CHECK(journal_seek(j, sizeof(journal_rawheader_t)));
  1957. CHECK(journal_write(j, j->rawindex, rawbytes));
  1958. }
  1959. failure:
  1960. return (result);
  1961. }