journal.c revision 1.12.2.1 1 /* $NetBSD: journal.c,v 1.12.2.1 2025/08/02 05:53:26 perseant Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16 #include <errno.h>
17 #include <inttypes.h>
18 #include <stdbool.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include <isc/dir.h>
23 #include <isc/file.h>
24 #include <isc/mem.h>
25 #include <isc/overflow.h>
26 #include <isc/result.h>
27 #include <isc/serial.h>
28 #include <isc/stdio.h>
29 #include <isc/string.h>
30 #include <isc/util.h>
31
32 #include <dns/compress.h>
33 #include <dns/db.h>
34 #include <dns/dbiterator.h>
35 #include <dns/diff.h>
36 #include <dns/fixedname.h>
37 #include <dns/journal.h>
38 #include <dns/log.h>
39 #include <dns/rdataset.h>
40 #include <dns/rdatasetiter.h>
41 #include <dns/soa.h>
42
43 /*! \file
44 * \brief Journaling.
45 *
46 * A journal file consists of
47 *
48 * \li A fixed-size header of type journal_rawheader_t.
49 *
50 * \li The index. This is an unordered array of index entries
51 * of type journal_rawpos_t giving the locations
52 * of some arbitrary subset of the journal's addressable
53 * transactions. The index entries are used as hints to
54 * speed up the process of locating a transaction with a given
55 * serial number. Unused index entries have an "offset"
56 * field of zero. The size of the index can vary between
57 * journal files, but does not change during the lifetime
58 * of a file. The size can be zero.
59 *
60 * \li The journal data. This consists of one or more transactions.
61 * Each transaction begins with a transaction header of type
62 * journal_rawxhdr_t. The transaction header is followed by a
63 * sequence of RRs, similar in structure to an IXFR difference
64 * sequence (RFC1995). That is, the pre-transaction SOA,
65 * zero or more other deleted RRs, the post-transaction SOA,
66 * and zero or more other added RRs. Unlike in IXFR, each RR
67 * is prefixed with a 32-bit length.
68 *
69 * The journal data part grows as new transactions are
70 * appended to the file. Only those transactions
71 * whose serial number is current-(2^31-1) to current
72 * are considered "addressable" and may be pointed
73 * to from the header or index. They may be preceded
74 * by old transactions that are no longer addressable,
75 * and they may be followed by transactions that were
76 * appended to the journal but never committed by updating
77 * the "end" position in the header. The latter will
78 * be overwritten when new transactions are added.
79 */
80
81 /**************************************************************************/
82 /*
83 * Miscellaneous utilities.
84 */
85
86 #define JOURNAL_COMMON_LOGARGS \
87 dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_JOURNAL
88
89 #define JOURNAL_DEBUG_LOGARGS(n) JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(n)
90
91 /*%
92 * It would be non-sensical (or at least obtuse) to use FAIL() with an
93 * ISC_R_SUCCESS code, but the test is there to keep the Solaris compiler
94 * from complaining about "end-of-loop code not reached".
95 */
96 #define FAIL(code) \
97 do { \
98 result = (code); \
99 if (result != ISC_R_SUCCESS) \
100 goto failure; \
101 } while (0)
102
103 #define CHECK(op) \
104 do { \
105 result = (op); \
106 if (result != ISC_R_SUCCESS) \
107 goto failure; \
108 } while (0)
109
110 #define JOURNAL_SERIALSET 0x01U
111
112 static isc_result_t
113 index_to_disk(dns_journal_t *);
114
115 static uint32_t
116 decode_uint32(unsigned char *p) {
117 return ((uint32_t)p[0] << 24) + ((uint32_t)p[1] << 16) +
118 ((uint32_t)p[2] << 8) + ((uint32_t)p[3] << 0);
119 }
120
121 static void
122 encode_uint32(uint32_t val, unsigned char *p) {
123 p[0] = (uint8_t)(val >> 24);
124 p[1] = (uint8_t)(val >> 16);
125 p[2] = (uint8_t)(val >> 8);
126 p[3] = (uint8_t)(val >> 0);
127 }
128
129 isc_result_t
130 dns_db_createsoatuple(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
131 dns_diffop_t op, dns_difftuple_t **tp) {
132 isc_result_t result;
133 dns_dbnode_t *node;
134 dns_rdataset_t rdataset;
135 dns_rdata_t rdata = DNS_RDATA_INIT;
136 dns_fixedname_t fixed;
137 dns_name_t *zonename;
138
139 zonename = dns_fixedname_initname(&fixed);
140 dns_name_copy(dns_db_origin(db), zonename);
141
142 node = NULL;
143 result = dns_db_findnode(db, zonename, false, &node);
144 if (result != ISC_R_SUCCESS) {
145 goto nonode;
146 }
147
148 dns_rdataset_init(&rdataset);
149 result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
150 (isc_stdtime_t)0, &rdataset, NULL);
151 if (result != ISC_R_SUCCESS) {
152 goto freenode;
153 }
154
155 result = dns_rdataset_first(&rdataset);
156 if (result != ISC_R_SUCCESS) {
157 goto freenode;
158 }
159
160 dns_rdataset_current(&rdataset, &rdata);
161 dns_rdataset_getownercase(&rdataset, zonename);
162
163 result = dns_difftuple_create(mctx, op, zonename, rdataset.ttl, &rdata,
164 tp);
165
166 dns_rdataset_disassociate(&rdataset);
167 dns_db_detachnode(db, &node);
168 return result;
169
170 freenode:
171 dns_db_detachnode(db, &node);
172 nonode:
173 UNEXPECTED_ERROR("missing SOA");
174 return result;
175 }
176
177 /* Journaling */
178
179 /*%
180 * On-disk representation of a "pointer" to a journal entry.
181 * These are used in the journal header to locate the beginning
182 * and end of the journal, and in the journal index to locate
183 * other transactions.
184 */
185 typedef struct {
186 unsigned char serial[4]; /*%< SOA serial before update. */
187 /*
188 * XXXRTH Should offset be 8 bytes?
189 * XXXDCL ... probably, since off_t is 8 bytes on many OSs.
190 * XXXAG ... but we will not be able to seek >2G anyway on many
191 * platforms as long as we are using fseek() rather
192 * than lseek().
193 */
194 unsigned char offset[4]; /*%< Offset from beginning of file. */
195 } journal_rawpos_t;
196
197 /*%
198 * The header is of a fixed size, with some spare room for future
199 * extensions.
200 */
201 #define JOURNAL_HEADER_SIZE 64 /* Bytes. */
202
203 typedef enum {
204 XHDR_VERSION1 = 1,
205 XHDR_VERSION2 = 2,
206 } xhdr_version_t;
207
208 /*%
209 * The on-disk representation of the journal header.
210 * All numbers are stored in big-endian order.
211 */
212 typedef union {
213 struct {
214 /*% File format version ID. */
215 unsigned char format[16];
216 /*% Position of the first addressable transaction */
217 journal_rawpos_t begin;
218 /*% Position of the next (yet nonexistent) transaction. */
219 journal_rawpos_t end;
220 /*% Number of index entries following the header. */
221 unsigned char index_size[4];
222 /*% Source serial number. */
223 unsigned char sourceserial[4];
224 unsigned char flags;
225 } h;
226 /* Pad the header to a fixed size. */
227 unsigned char pad[JOURNAL_HEADER_SIZE];
228 } journal_rawheader_t;
229
230 /*%
231 * The on-disk representation of the transaction header, version 2.
232 * There is one of these at the beginning of each transaction.
233 */
234 typedef struct {
235 unsigned char size[4]; /*%< In bytes, excluding header. */
236 unsigned char count[4]; /*%< Number of records in transaction */
237 unsigned char serial0[4]; /*%< SOA serial before update. */
238 unsigned char serial1[4]; /*%< SOA serial after update. */
239 } journal_rawxhdr_t;
240
241 /*%
242 * Old-style raw transaction header, version 1, used for backward
243 * compatibility mode.
244 */
245 typedef struct {
246 unsigned char size[4];
247 unsigned char serial0[4];
248 unsigned char serial1[4];
249 } journal_rawxhdr_ver1_t;
250
251 /*%
252 * The on-disk representation of the RR header.
253 * There is one of these at the beginning of each RR.
254 */
255 typedef struct {
256 unsigned char size[4]; /*%< In bytes, excluding header. */
257 } journal_rawrrhdr_t;
258
259 /*%
260 * The in-core representation of the journal header.
261 */
262 typedef struct {
263 uint32_t serial;
264 off_t offset;
265 } journal_pos_t;
266
267 #define POS_VALID(pos) ((pos).offset != 0)
268 #define POS_INVALIDATE(pos) ((pos).offset = 0, (pos).serial = 0)
269
270 typedef struct {
271 unsigned char format[16];
272 journal_pos_t begin;
273 journal_pos_t end;
274 uint32_t index_size;
275 uint32_t sourceserial;
276 bool serialset;
277 } journal_header_t;
278
279 /*%
280 * The in-core representation of the transaction header.
281 */
282 typedef struct {
283 uint32_t size;
284 uint32_t count;
285 uint32_t serial0;
286 uint32_t serial1;
287 } journal_xhdr_t;
288
289 /*%
290 * The in-core representation of the RR header.
291 */
292 typedef struct {
293 uint32_t size;
294 } journal_rrhdr_t;
295
296 /*%
297 * Initial contents to store in the header of a newly created
298 * journal file.
299 *
300 * The header starts with the magic string ";BIND LOG V9.2\n"
301 * to identify the file as a BIND 9 journal file. An ASCII
302 * identification string is used rather than a binary magic
303 * number to be consistent with BIND 8 (BIND 8 journal files
304 * are ASCII text files).
305 */
306
307 static journal_header_t journal_header_ver1 = {
308 ";BIND LOG V9\n", { 0, 0 }, { 0, 0 }, 0, 0, 0
309 };
310 static journal_header_t initial_journal_header = {
311 ";BIND LOG V9.2\n", { 0, 0 }, { 0, 0 }, 0, 0, 0
312 };
313
314 #define JOURNAL_EMPTY(h) ((h)->begin.offset == (h)->end.offset)
315
316 typedef enum {
317 JOURNAL_STATE_INVALID,
318 JOURNAL_STATE_READ,
319 JOURNAL_STATE_WRITE,
320 JOURNAL_STATE_TRANSACTION,
321 JOURNAL_STATE_INLINE
322 } journal_state_t;
323
324 struct dns_journal {
325 unsigned int magic; /*%< JOUR */
326 isc_mem_t *mctx; /*%< Memory context */
327 journal_state_t state;
328 xhdr_version_t xhdr_version; /*%< Expected transaction header version */
329 bool header_ver1; /*%< Transaction header compatibility
330 * mode is allowed */
331 bool recovered; /*%< A recoverable error was found
332 * while reading the journal */
333 char *filename; /*%< Journal file name */
334 FILE *fp; /*%< File handle */
335 off_t offset; /*%< Current file offset */
336 journal_xhdr_t curxhdr; /*%< Current transaction header */
337 journal_header_t header; /*%< In-core journal header */
338 unsigned char *rawindex; /*%< In-core buffer for journal index
339 * in on-disk format */
340 journal_pos_t *index; /*%< In-core journal index */
341
342 /*% Current transaction state (when writing). */
343 struct {
344 unsigned int n_soa; /*%< Number of SOAs seen */
345 unsigned int n_rr; /*%< Number of RRs to write */
346 journal_pos_t pos[2]; /*%< Begin/end position */
347 } x;
348
349 /*% Iteration state (when reading). */
350 struct {
351 /* These define the part of the journal we iterate over. */
352 journal_pos_t bpos; /*%< Position before first, */
353 journal_pos_t cpos; /*%< before current, */
354 journal_pos_t epos; /*%< and after last transaction */
355 /* The rest is iterator state. */
356 uint32_t current_serial; /*%< Current SOA serial */
357 isc_buffer_t source; /*%< Data from disk */
358 isc_buffer_t target; /*%< Data from _fromwire check */
359 dns_decompress_t dctx; /*%< Dummy decompression ctx */
360 dns_name_t name; /*%< Current domain name */
361 dns_rdata_t rdata; /*%< Current rdata */
362 uint32_t ttl; /*%< Current TTL */
363 unsigned int xsize; /*%< Size of transaction data */
364 unsigned int xpos; /*%< Current position in it */
365 isc_result_t result; /*%< Result of last call */
366 } it;
367 };
368
369 #define DNS_JOURNAL_MAGIC ISC_MAGIC('J', 'O', 'U', 'R')
370 #define DNS_JOURNAL_VALID(t) ISC_MAGIC_VALID(t, DNS_JOURNAL_MAGIC)
371
372 static void
373 journal_pos_decode(journal_rawpos_t *raw, journal_pos_t *cooked) {
374 cooked->serial = decode_uint32(raw->serial);
375 cooked->offset = decode_uint32(raw->offset);
376 }
377
378 static void
379 journal_pos_encode(journal_rawpos_t *raw, journal_pos_t *cooked) {
380 encode_uint32(cooked->serial, raw->serial);
381 encode_uint32(cooked->offset, raw->offset);
382 }
383
384 static void
385 journal_header_decode(journal_rawheader_t *raw, journal_header_t *cooked) {
386 INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
387
388 memmove(cooked->format, raw->h.format, sizeof(cooked->format));
389 journal_pos_decode(&raw->h.begin, &cooked->begin);
390 journal_pos_decode(&raw->h.end, &cooked->end);
391 cooked->index_size = decode_uint32(raw->h.index_size);
392 cooked->sourceserial = decode_uint32(raw->h.sourceserial);
393 cooked->serialset = ((raw->h.flags & JOURNAL_SERIALSET) != 0);
394 }
395
396 static void
397 journal_header_encode(journal_header_t *cooked, journal_rawheader_t *raw) {
398 unsigned char flags = 0;
399
400 INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
401
402 memset(raw->pad, 0, sizeof(raw->pad));
403 memmove(raw->h.format, cooked->format, sizeof(raw->h.format));
404 journal_pos_encode(&raw->h.begin, &cooked->begin);
405 journal_pos_encode(&raw->h.end, &cooked->end);
406 encode_uint32(cooked->index_size, raw->h.index_size);
407 encode_uint32(cooked->sourceserial, raw->h.sourceserial);
408 if (cooked->serialset) {
409 flags |= JOURNAL_SERIALSET;
410 }
411 raw->h.flags = flags;
412 }
413
414 /*
415 * Journal file I/O subroutines, with error checking and reporting.
416 */
417 static isc_result_t
418 journal_seek(dns_journal_t *j, uint32_t offset) {
419 isc_result_t result;
420
421 result = isc_stdio_seek(j->fp, (off_t)offset, SEEK_SET);
422 if (result != ISC_R_SUCCESS) {
423 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
424 "%s: seek: %s", j->filename,
425 isc_result_totext(result));
426 return ISC_R_UNEXPECTED;
427 }
428 j->offset = offset;
429 return ISC_R_SUCCESS;
430 }
431
432 static isc_result_t
433 journal_read(dns_journal_t *j, void *mem, size_t nbytes) {
434 isc_result_t result;
435
436 result = isc_stdio_read(mem, 1, nbytes, j->fp, NULL);
437 if (result != ISC_R_SUCCESS) {
438 if (result == ISC_R_EOF) {
439 return ISC_R_NOMORE;
440 }
441 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
442 "%s: read: %s", j->filename,
443 isc_result_totext(result));
444 return ISC_R_UNEXPECTED;
445 }
446 j->offset += (off_t)nbytes;
447 return ISC_R_SUCCESS;
448 }
449
450 static isc_result_t
451 journal_write(dns_journal_t *j, void *mem, size_t nbytes) {
452 isc_result_t result;
453
454 result = isc_stdio_write(mem, 1, nbytes, j->fp, NULL);
455 if (result != ISC_R_SUCCESS) {
456 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
457 "%s: write: %s", j->filename,
458 isc_result_totext(result));
459 return ISC_R_UNEXPECTED;
460 }
461 j->offset += (off_t)nbytes;
462 return ISC_R_SUCCESS;
463 }
464
465 static isc_result_t
466 journal_fsync(dns_journal_t *j) {
467 isc_result_t result;
468
469 result = isc_stdio_flush(j->fp);
470 if (result != ISC_R_SUCCESS) {
471 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
472 "%s: flush: %s", j->filename,
473 isc_result_totext(result));
474 return ISC_R_UNEXPECTED;
475 }
476 result = isc_stdio_sync(j->fp);
477 if (result != ISC_R_SUCCESS) {
478 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
479 "%s: fsync: %s", j->filename,
480 isc_result_totext(result));
481 return ISC_R_UNEXPECTED;
482 }
483 return ISC_R_SUCCESS;
484 }
485
486 /*
487 * Read/write a transaction header at the current file position.
488 */
489 static isc_result_t
490 journal_read_xhdr(dns_journal_t *j, journal_xhdr_t *xhdr) {
491 isc_result_t result;
492
493 j->it.cpos.offset = j->offset;
494
495 switch (j->xhdr_version) {
496 case XHDR_VERSION1: {
497 journal_rawxhdr_ver1_t raw;
498 result = journal_read(j, &raw, sizeof(raw));
499 if (result != ISC_R_SUCCESS) {
500 return result;
501 }
502 xhdr->size = decode_uint32(raw.size);
503 xhdr->count = 0;
504 xhdr->serial0 = decode_uint32(raw.serial0);
505 xhdr->serial1 = decode_uint32(raw.serial1);
506 j->curxhdr = *xhdr;
507 return ISC_R_SUCCESS;
508 }
509
510 case XHDR_VERSION2: {
511 journal_rawxhdr_t raw;
512 result = journal_read(j, &raw, sizeof(raw));
513 if (result != ISC_R_SUCCESS) {
514 return result;
515 }
516 xhdr->size = decode_uint32(raw.size);
517 xhdr->count = decode_uint32(raw.count);
518 xhdr->serial0 = decode_uint32(raw.serial0);
519 xhdr->serial1 = decode_uint32(raw.serial1);
520 j->curxhdr = *xhdr;
521 return ISC_R_SUCCESS;
522 }
523
524 default:
525 return ISC_R_NOTIMPLEMENTED;
526 }
527 }
528
529 static isc_result_t
530 journal_write_xhdr(dns_journal_t *j, uint32_t size, uint32_t count,
531 uint32_t serial0, uint32_t serial1) {
532 if (j->header_ver1) {
533 journal_rawxhdr_ver1_t raw;
534 encode_uint32(size, raw.size);
535 encode_uint32(serial0, raw.serial0);
536 encode_uint32(serial1, raw.serial1);
537 return journal_write(j, &raw, sizeof(raw));
538 } else {
539 journal_rawxhdr_t raw;
540 encode_uint32(size, raw.size);
541 encode_uint32(count, raw.count);
542 encode_uint32(serial0, raw.serial0);
543 encode_uint32(serial1, raw.serial1);
544 return journal_write(j, &raw, sizeof(raw));
545 }
546 }
547
548 /*
549 * Read an RR header at the current file position.
550 */
551
552 static isc_result_t
553 journal_read_rrhdr(dns_journal_t *j, journal_rrhdr_t *rrhdr) {
554 journal_rawrrhdr_t raw;
555 isc_result_t result;
556
557 result = journal_read(j, &raw, sizeof(raw));
558 if (result != ISC_R_SUCCESS) {
559 return result;
560 }
561 rrhdr->size = decode_uint32(raw.size);
562 return ISC_R_SUCCESS;
563 }
564
565 static isc_result_t
566 journal_file_create(isc_mem_t *mctx, bool downgrade, const char *filename) {
567 FILE *fp = NULL;
568 isc_result_t result;
569 journal_header_t header;
570 journal_rawheader_t rawheader;
571 int index_size = 56; /* XXX configurable */
572 int size;
573 void *mem = NULL; /* Memory for temporary index image. */
574
575 INSIST(sizeof(journal_rawheader_t) == JOURNAL_HEADER_SIZE);
576
577 result = isc_stdio_open(filename, "wb", &fp);
578 if (result != ISC_R_SUCCESS) {
579 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
580 "%s: create: %s", filename,
581 isc_result_totext(result));
582 return ISC_R_UNEXPECTED;
583 }
584
585 if (downgrade) {
586 header = journal_header_ver1;
587 } else {
588 header = initial_journal_header;
589 }
590 header.index_size = index_size;
591 journal_header_encode(&header, &rawheader);
592
593 size = sizeof(journal_rawheader_t) +
594 ISC_CHECKED_MUL(index_size, sizeof(journal_rawpos_t));
595
596 mem = isc_mem_cget(mctx, 1, size);
597 memmove(mem, &rawheader, sizeof(rawheader));
598
599 result = isc_stdio_write(mem, 1, (size_t)size, fp, NULL);
600 if (result != ISC_R_SUCCESS) {
601 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
602 "%s: write: %s", filename,
603 isc_result_totext(result));
604 (void)isc_stdio_close(fp);
605 (void)isc_file_remove(filename);
606 isc_mem_put(mctx, mem, size);
607 return ISC_R_UNEXPECTED;
608 }
609 isc_mem_put(mctx, mem, size);
610
611 result = isc_stdio_close(fp);
612 if (result != ISC_R_SUCCESS) {
613 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
614 "%s: close: %s", filename,
615 isc_result_totext(result));
616 (void)isc_file_remove(filename);
617 return ISC_R_UNEXPECTED;
618 }
619
620 return ISC_R_SUCCESS;
621 }
622
623 static isc_result_t
624 journal_open(isc_mem_t *mctx, const char *filename, bool writable, bool create,
625 bool downgrade, dns_journal_t **journalp) {
626 FILE *fp = NULL;
627 isc_result_t result;
628 journal_rawheader_t rawheader;
629 dns_journal_t *j;
630
631 REQUIRE(journalp != NULL && *journalp == NULL);
632
633 j = isc_mem_get(mctx, sizeof(*j));
634 *j = (dns_journal_t){ .state = JOURNAL_STATE_INVALID,
635 .filename = isc_mem_strdup(mctx, filename),
636 .xhdr_version = XHDR_VERSION2 };
637 isc_mem_attach(mctx, &j->mctx);
638
639 result = isc_stdio_open(j->filename, writable ? "rb+" : "rb", &fp);
640 if (result == ISC_R_FILENOTFOUND) {
641 if (create) {
642 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(1),
643 "journal file %s does not exist, "
644 "creating it",
645 j->filename);
646 CHECK(journal_file_create(mctx, downgrade, filename));
647 /*
648 * Retry.
649 */
650 result = isc_stdio_open(j->filename, "rb+", &fp);
651 } else {
652 FAIL(ISC_R_NOTFOUND);
653 }
654 }
655 if (result != ISC_R_SUCCESS) {
656 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
657 "%s: open: %s", j->filename,
658 isc_result_totext(result));
659 FAIL(ISC_R_UNEXPECTED);
660 }
661
662 j->fp = fp;
663
664 /*
665 * Set magic early so that seek/read can succeed.
666 */
667 j->magic = DNS_JOURNAL_MAGIC;
668
669 CHECK(journal_seek(j, 0));
670 CHECK(journal_read(j, &rawheader, sizeof(rawheader)));
671
672 if (memcmp(rawheader.h.format, journal_header_ver1.format,
673 sizeof(journal_header_ver1.format)) == 0)
674 {
675 /*
676 * The file header says it's the old format, but it
677 * still might have the new xhdr format because we
678 * forgot to change the format string when we introduced
679 * the new xhdr. When we first try to read it, we assume
680 * it uses the new xhdr format. If that fails, we'll be
681 * called a second time with compat set to true, in which
682 * case we can lower xhdr_version to 1 if we find a
683 * corrupt transaction.
684 */
685 j->header_ver1 = true;
686 } else if (memcmp(rawheader.h.format, initial_journal_header.format,
687 sizeof(initial_journal_header.format)) == 0)
688 {
689 /*
690 * File header says this is format version 2; all
691 * transactions have to match.
692 */
693 j->header_ver1 = false;
694 } else {
695 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
696 "%s: journal format not recognized", j->filename);
697 FAIL(ISC_R_UNEXPECTED);
698 }
699 journal_header_decode(&rawheader, &j->header);
700
701 /*
702 * If there is an index, read the raw index into a dynamically
703 * allocated buffer and then convert it into a cooked index.
704 */
705 if (j->header.index_size != 0) {
706 unsigned int i;
707 unsigned int rawbytes;
708 unsigned char *p;
709
710 rawbytes = ISC_CHECKED_MUL(j->header.index_size,
711 sizeof(journal_rawpos_t));
712 j->rawindex = isc_mem_get(mctx, rawbytes);
713
714 CHECK(journal_read(j, j->rawindex, rawbytes));
715
716 j->index = isc_mem_cget(mctx, j->header.index_size,
717 sizeof(journal_pos_t));
718
719 p = j->rawindex;
720 for (i = 0; i < j->header.index_size; i++) {
721 j->index[i].serial = decode_uint32(p);
722 p += 4;
723 j->index[i].offset = decode_uint32(p);
724 p += 4;
725 }
726 INSIST(p == j->rawindex + rawbytes);
727 }
728 j->offset = -1; /* Invalid, must seek explicitly. */
729
730 /*
731 * Initialize the iterator.
732 */
733 dns_name_init(&j->it.name, NULL);
734 dns_rdata_init(&j->it.rdata);
735
736 /*
737 * Set up empty initial buffers for unchecked and checked
738 * wire format RR data. They will be reallocated
739 * later.
740 */
741 isc_buffer_init(&j->it.source, NULL, 0);
742 isc_buffer_init(&j->it.target, NULL, 0);
743 j->it.dctx = DNS_DECOMPRESS_NEVER;
744
745 j->state = writable ? JOURNAL_STATE_WRITE : JOURNAL_STATE_READ;
746
747 *journalp = j;
748 return ISC_R_SUCCESS;
749
750 failure:
751 j->magic = 0;
752 if (j->rawindex != NULL) {
753 isc_mem_cput(j->mctx, j->rawindex, j->header.index_size,
754 sizeof(journal_rawpos_t));
755 }
756 if (j->index != NULL) {
757 isc_mem_cput(j->mctx, j->index, j->header.index_size,
758 sizeof(journal_pos_t));
759 }
760 isc_mem_free(j->mctx, j->filename);
761 if (j->fp != NULL) {
762 (void)isc_stdio_close(j->fp);
763 }
764 isc_mem_putanddetach(&j->mctx, j, sizeof(*j));
765 return result;
766 }
767
768 isc_result_t
769 dns_journal_open(isc_mem_t *mctx, const char *filename, unsigned int mode,
770 dns_journal_t **journalp) {
771 isc_result_t result;
772 size_t namelen;
773 char backup[1024];
774 bool writable, create;
775
776 create = ((mode & DNS_JOURNAL_CREATE) != 0);
777 writable = ((mode & (DNS_JOURNAL_WRITE | DNS_JOURNAL_CREATE)) != 0);
778
779 result = journal_open(mctx, filename, writable, create, false,
780 journalp);
781 if (result == ISC_R_NOTFOUND) {
782 namelen = strlen(filename);
783 if (namelen > 4U && strcmp(filename + namelen - 4, ".jnl") == 0)
784 {
785 namelen -= 4;
786 }
787
788 result = snprintf(backup, sizeof(backup), "%.*s.jbk",
789 (int)namelen, filename);
790 if (result >= sizeof(backup)) {
791 return ISC_R_NOSPACE;
792 }
793 result = journal_open(mctx, backup, writable, writable, false,
794 journalp);
795 }
796 return result;
797 }
798
799 /*
800 * A comparison function defining the sorting order for
801 * entries in the IXFR-style journal file.
802 *
803 * The IXFR format requires that deletions are sorted before
804 * additions, and within either one, SOA records are sorted
805 * before others.
806 *
807 * Also sort the non-SOA records by type as a courtesy to the
808 * server receiving the IXFR - it may help reduce the amount of
809 * rdataset merging it has to do.
810 */
811 static int
812 ixfr_order(const void *av, const void *bv) {
813 dns_difftuple_t const *const *ap = av;
814 dns_difftuple_t const *const *bp = bv;
815 dns_difftuple_t const *a = *ap;
816 dns_difftuple_t const *b = *bp;
817 int r;
818 int bop = 0, aop = 0;
819
820 switch (a->op) {
821 case DNS_DIFFOP_DEL:
822 case DNS_DIFFOP_DELRESIGN:
823 aop = 1;
824 break;
825 case DNS_DIFFOP_ADD:
826 case DNS_DIFFOP_ADDRESIGN:
827 aop = 0;
828 break;
829 default:
830 UNREACHABLE();
831 }
832
833 switch (b->op) {
834 case DNS_DIFFOP_DEL:
835 case DNS_DIFFOP_DELRESIGN:
836 bop = 1;
837 break;
838 case DNS_DIFFOP_ADD:
839 case DNS_DIFFOP_ADDRESIGN:
840 bop = 0;
841 break;
842 default:
843 UNREACHABLE();
844 }
845
846 r = bop - aop;
847 if (r != 0) {
848 return r;
849 }
850
851 r = (b->rdata.type == dns_rdatatype_soa) -
852 (a->rdata.type == dns_rdatatype_soa);
853 if (r != 0) {
854 return r;
855 }
856
857 r = (a->rdata.type - b->rdata.type);
858 return r;
859 }
860
861 static isc_result_t
862 maybe_fixup_xhdr(dns_journal_t *j, journal_xhdr_t *xhdr, uint32_t serial,
863 off_t offset) {
864 isc_result_t result = ISC_R_SUCCESS;
865
866 /*
867 * Handle mixture of version 1 and version 2
868 * transaction headers in a version 1 journal.
869 */
870 if (xhdr->serial0 != serial ||
871 isc_serial_le(xhdr->serial1, xhdr->serial0))
872 {
873 if (j->xhdr_version == XHDR_VERSION1 && xhdr->serial1 == serial)
874 {
875 isc_log_write(
876 JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(3),
877 "%s: XHDR_VERSION1 -> XHDR_VERSION2 at %u",
878 j->filename, serial);
879 j->xhdr_version = XHDR_VERSION2;
880 CHECK(journal_seek(j, offset));
881 CHECK(journal_read_xhdr(j, xhdr));
882 j->recovered = true;
883 } else if (j->xhdr_version == XHDR_VERSION2 &&
884 xhdr->count == serial)
885 {
886 isc_log_write(
887 JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(3),
888 "%s: XHDR_VERSION2 -> XHDR_VERSION1 at %u",
889 j->filename, serial);
890 j->xhdr_version = XHDR_VERSION1;
891 CHECK(journal_seek(j, offset));
892 CHECK(journal_read_xhdr(j, xhdr));
893 j->recovered = true;
894 }
895 }
896
897 /*
898 * Handle <size, serial0, serial1, 0> transaction header.
899 */
900 if (j->xhdr_version == XHDR_VERSION1) {
901 uint32_t value;
902
903 CHECK(journal_read(j, &value, sizeof(value)));
904 if (value != 0L) {
905 CHECK(journal_seek(j, offset + 12));
906 } else {
907 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(3),
908 "%s: XHDR_VERSION1 count zero at %u",
909 j->filename, serial);
910 j->xhdr_version = XHDR_VERSION2;
911 j->recovered = true;
912 }
913 } else if (j->xhdr_version == XHDR_VERSION2 && xhdr->count == serial &&
914 xhdr->serial1 == 0U &&
915 isc_serial_gt(xhdr->serial0, xhdr->count))
916 {
917 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(3),
918 "%s: XHDR_VERSION2 count zero at %u", j->filename,
919 serial);
920 xhdr->serial1 = xhdr->serial0;
921 xhdr->serial0 = xhdr->count;
922 xhdr->count = 0;
923 j->recovered = true;
924 }
925
926 failure:
927 return result;
928 }
929
930 /*
931 * Advance '*pos' to the next journal transaction.
932 *
933 * Requires:
934 * *pos refers to a valid journal transaction.
935 *
936 * Ensures:
937 * When ISC_R_SUCCESS is returned,
938 * *pos refers to the next journal transaction.
939 *
940 * Returns one of:
941 *
942 * ISC_R_SUCCESS
943 * ISC_R_NOMORE *pos pointed at the last transaction
944 * Other results due to file errors are possible.
945 */
946 static isc_result_t
947 journal_next(dns_journal_t *j, journal_pos_t *pos) {
948 isc_result_t result;
949 journal_xhdr_t xhdr;
950 size_t hdrsize;
951
952 REQUIRE(DNS_JOURNAL_VALID(j));
953
954 result = journal_seek(j, pos->offset);
955 if (result != ISC_R_SUCCESS) {
956 return result;
957 }
958
959 if (pos->serial == j->header.end.serial) {
960 return ISC_R_NOMORE;
961 }
962
963 /*
964 * Read the header of the current transaction.
965 * This will return ISC_R_NOMORE if we are at EOF.
966 */
967 result = journal_read_xhdr(j, &xhdr);
968 if (result != ISC_R_SUCCESS) {
969 return result;
970 }
971
972 if (j->header_ver1) {
973 CHECK(maybe_fixup_xhdr(j, &xhdr, pos->serial, pos->offset));
974 }
975
976 /*
977 * Check serial number consistency.
978 */
979 if (xhdr.serial0 != pos->serial ||
980 isc_serial_le(xhdr.serial1, xhdr.serial0))
981 {
982 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
983 "%s: journal file corrupt: "
984 "expected serial %u, got %u",
985 j->filename, pos->serial, xhdr.serial0);
986 return ISC_R_UNEXPECTED;
987 }
988
989 /*
990 * Check for offset wraparound.
991 */
992 hdrsize = (j->xhdr_version == XHDR_VERSION2)
993 ? sizeof(journal_rawxhdr_t)
994 : sizeof(journal_rawxhdr_ver1_t);
995
996 if ((off_t)(pos->offset + hdrsize + xhdr.size) < pos->offset) {
997 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
998 "%s: offset too large", j->filename);
999 return ISC_R_UNEXPECTED;
1000 }
1001
1002 pos->offset += hdrsize + xhdr.size;
1003 pos->serial = xhdr.serial1;
1004 return ISC_R_SUCCESS;
1005
1006 failure:
1007 return result;
1008 }
1009
1010 /*
1011 * If the index of the journal 'j' contains an entry "better"
1012 * than '*best_guess', replace '*best_guess' with it.
1013 *
1014 * "Better" means having a serial number closer to 'serial'
1015 * but not greater than 'serial'.
1016 */
1017 static void
1018 index_find(dns_journal_t *j, uint32_t serial, journal_pos_t *best_guess) {
1019 unsigned int i;
1020 if (j->index == NULL) {
1021 return;
1022 }
1023 for (i = 0; i < j->header.index_size; i++) {
1024 if (POS_VALID(j->index[i]) &&
1025 DNS_SERIAL_GE(serial, j->index[i].serial) &&
1026 DNS_SERIAL_GT(j->index[i].serial, best_guess->serial))
1027 {
1028 *best_guess = j->index[i];
1029 }
1030 }
1031 }
1032
1033 /*
1034 * Add a new index entry. If there is no room, make room by removing
1035 * the odd-numbered entries and compacting the others into the first
1036 * half of the index. This decimates old index entries exponentially
1037 * over time, so that the index always contains a much larger fraction
1038 * of recent serial numbers than of old ones. This is deliberate -
1039 * most index searches are for outgoing IXFR, and IXFR tends to request
1040 * recent versions more often than old ones.
1041 */
1042 static void
1043 index_add(dns_journal_t *j, journal_pos_t *pos) {
1044 unsigned int i;
1045
1046 if (j->index == NULL) {
1047 return;
1048 }
1049
1050 /*
1051 * Search for a vacant position.
1052 */
1053 for (i = 0; i < j->header.index_size; i++) {
1054 if (!POS_VALID(j->index[i])) {
1055 break;
1056 }
1057 }
1058 if (i == j->header.index_size) {
1059 unsigned int k = 0;
1060 /*
1061 * Found no vacant position. Make some room.
1062 */
1063 for (i = 0; i < j->header.index_size; i += 2) {
1064 j->index[k++] = j->index[i];
1065 }
1066 i = k; /* 'i' identifies the first vacant position. */
1067 while (k < j->header.index_size) {
1068 POS_INVALIDATE(j->index[k]);
1069 k++;
1070 }
1071 }
1072 INSIST(i < j->header.index_size);
1073 INSIST(!POS_VALID(j->index[i]));
1074
1075 /*
1076 * Store the new index entry.
1077 */
1078 j->index[i] = *pos;
1079 }
1080
1081 /*
1082 * Invalidate any existing index entries that could become
1083 * ambiguous when a new transaction with number 'serial' is added.
1084 */
1085 static void
1086 index_invalidate(dns_journal_t *j, uint32_t serial) {
1087 unsigned int i;
1088 if (j->index == NULL) {
1089 return;
1090 }
1091 for (i = 0; i < j->header.index_size; i++) {
1092 if (!DNS_SERIAL_GT(serial, j->index[i].serial)) {
1093 POS_INVALIDATE(j->index[i]);
1094 }
1095 }
1096 }
1097
1098 /*
1099 * Try to find a transaction with initial serial number 'serial'
1100 * in the journal 'j'.
1101 *
1102 * If found, store its position at '*pos' and return ISC_R_SUCCESS.
1103 *
1104 * If 'serial' is current (= the ending serial number of the
1105 * last transaction in the journal), set '*pos' to
1106 * the position immediately following the last transaction and
1107 * return ISC_R_SUCCESS.
1108 *
1109 * If 'serial' is within the range of addressable serial numbers
1110 * covered by the journal but that particular serial number is missing
1111 * (from the journal, not just from the index), return ISC_R_NOTFOUND.
1112 *
1113 * If 'serial' is outside the range of addressable serial numbers
1114 * covered by the journal, return ISC_R_RANGE.
1115 *
1116 */
1117 static isc_result_t
1118 journal_find(dns_journal_t *j, uint32_t serial, journal_pos_t *pos) {
1119 isc_result_t result;
1120 journal_pos_t current_pos;
1121
1122 REQUIRE(DNS_JOURNAL_VALID(j));
1123
1124 if (DNS_SERIAL_GT(j->header.begin.serial, serial)) {
1125 return ISC_R_RANGE;
1126 }
1127 if (DNS_SERIAL_GT(serial, j->header.end.serial)) {
1128 return ISC_R_RANGE;
1129 }
1130 if (serial == j->header.end.serial) {
1131 *pos = j->header.end;
1132 return ISC_R_SUCCESS;
1133 }
1134
1135 current_pos = j->header.begin;
1136 index_find(j, serial, ¤t_pos);
1137
1138 while (current_pos.serial != serial) {
1139 if (DNS_SERIAL_GT(current_pos.serial, serial)) {
1140 return ISC_R_NOTFOUND;
1141 }
1142 result = journal_next(j, ¤t_pos);
1143 if (result != ISC_R_SUCCESS) {
1144 return result;
1145 }
1146 }
1147 *pos = current_pos;
1148 return ISC_R_SUCCESS;
1149 }
1150
1151 isc_result_t
1152 dns_journal_begin_transaction(dns_journal_t *j) {
1153 uint32_t offset;
1154 isc_result_t result;
1155
1156 REQUIRE(DNS_JOURNAL_VALID(j));
1157 REQUIRE(j->state == JOURNAL_STATE_WRITE ||
1158 j->state == JOURNAL_STATE_INLINE);
1159
1160 /*
1161 * Find the file offset where the new transaction should
1162 * be written, and seek there.
1163 */
1164 if (JOURNAL_EMPTY(&j->header)) {
1165 offset = sizeof(journal_rawheader_t) +
1166 ISC_CHECKED_MUL(j->header.index_size,
1167 sizeof(journal_rawpos_t));
1168 } else {
1169 offset = j->header.end.offset;
1170 }
1171 j->x.pos[0].offset = offset;
1172 j->x.pos[1].offset = offset; /* Initial value, will be incremented. */
1173 j->x.n_soa = 0;
1174
1175 CHECK(journal_seek(j, offset));
1176
1177 /*
1178 * Write a dummy transaction header of all zeroes to reserve
1179 * space. It will be filled in when the transaction is
1180 * finished.
1181 */
1182 CHECK(journal_write_xhdr(j, 0, 0, 0, 0));
1183 j->x.pos[1].offset = j->offset;
1184
1185 j->state = JOURNAL_STATE_TRANSACTION;
1186 result = ISC_R_SUCCESS;
1187 failure:
1188 return result;
1189 }
1190
1191 isc_result_t
1192 dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff) {
1193 dns_difftuple_t *t;
1194 isc_buffer_t buffer;
1195 void *mem = NULL;
1196 uint64_t size = 0;
1197 uint32_t rrcount = 0;
1198 isc_result_t result;
1199 isc_region_t used;
1200
1201 REQUIRE(DNS_DIFF_VALID(diff));
1202 REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
1203
1204 isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "writing to journal");
1205 (void)dns_diff_print(diff, NULL);
1206
1207 /*
1208 * Pass 1: determine the buffer size needed, and
1209 * keep track of SOA serial numbers.
1210 */
1211 for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
1212 t = ISC_LIST_NEXT(t, link))
1213 {
1214 if (t->rdata.type == dns_rdatatype_soa) {
1215 if (j->x.n_soa < 2) {
1216 j->x.pos[j->x.n_soa].serial =
1217 dns_soa_getserial(&t->rdata);
1218 }
1219 j->x.n_soa++;
1220 }
1221 size += sizeof(journal_rawrrhdr_t);
1222 size += t->name.length; /* XXX should have access macro? */
1223 size += 10;
1224 size += t->rdata.length;
1225 }
1226
1227 if (size >= DNS_JOURNAL_SIZE_MAX) {
1228 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1229 "dns_journal_writediff: %s: journal entry "
1230 "too big to be stored: %" PRIu64 " bytes",
1231 j->filename, size);
1232 return ISC_R_NOSPACE;
1233 }
1234
1235 mem = isc_mem_get(j->mctx, size);
1236
1237 isc_buffer_init(&buffer, mem, size);
1238
1239 /*
1240 * Pass 2. Write RRs to buffer.
1241 */
1242 for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
1243 t = ISC_LIST_NEXT(t, link))
1244 {
1245 /*
1246 * Write the RR header.
1247 */
1248 isc_buffer_putuint32(&buffer,
1249 t->name.length + 10 + t->rdata.length);
1250 /*
1251 * Write the owner name, RR header, and RR data.
1252 */
1253 isc_buffer_putmem(&buffer, t->name.ndata, t->name.length);
1254 isc_buffer_putuint16(&buffer, t->rdata.type);
1255 isc_buffer_putuint16(&buffer, t->rdata.rdclass);
1256 isc_buffer_putuint32(&buffer, t->ttl);
1257 isc_buffer_putuint16(&buffer, (uint16_t)t->rdata.length);
1258 INSIST(isc_buffer_availablelength(&buffer) >= t->rdata.length);
1259 isc_buffer_putmem(&buffer, t->rdata.data, t->rdata.length);
1260
1261 rrcount++;
1262 }
1263
1264 isc_buffer_usedregion(&buffer, &used);
1265 INSIST(used.length == size);
1266
1267 j->x.pos[1].offset += used.length;
1268 j->x.n_rr = rrcount;
1269
1270 /*
1271 * Write the buffer contents to the journal file.
1272 */
1273 CHECK(journal_write(j, used.base, used.length));
1274
1275 result = ISC_R_SUCCESS;
1276
1277 failure:
1278 if (mem != NULL) {
1279 isc_mem_put(j->mctx, mem, size);
1280 }
1281 return result;
1282 }
1283
1284 isc_result_t
1285 dns_journal_commit(dns_journal_t *j) {
1286 isc_result_t result;
1287 journal_rawheader_t rawheader;
1288 uint64_t total;
1289
1290 REQUIRE(DNS_JOURNAL_VALID(j));
1291 REQUIRE(j->state == JOURNAL_STATE_TRANSACTION ||
1292 j->state == JOURNAL_STATE_INLINE);
1293
1294 /*
1295 * Just write out a updated header.
1296 */
1297 if (j->state == JOURNAL_STATE_INLINE) {
1298 CHECK(journal_fsync(j));
1299 journal_header_encode(&j->header, &rawheader);
1300 CHECK(journal_seek(j, 0));
1301 CHECK(journal_write(j, &rawheader, sizeof(rawheader)));
1302 CHECK(journal_fsync(j));
1303 j->state = JOURNAL_STATE_WRITE;
1304 return ISC_R_SUCCESS;
1305 }
1306
1307 /*
1308 * Perform some basic consistency checks.
1309 */
1310 if (j->x.n_soa != 2) {
1311 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1312 "%s: malformed transaction: %d SOAs", j->filename,
1313 j->x.n_soa);
1314 return ISC_R_UNEXPECTED;
1315 }
1316 if (!DNS_SERIAL_GT(j->x.pos[1].serial, j->x.pos[0].serial)) {
1317 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1318 "%s: malformed transaction: serial number "
1319 "did not increase",
1320 j->filename);
1321 return ISC_R_UNEXPECTED;
1322 }
1323 if (!JOURNAL_EMPTY(&j->header)) {
1324 if (j->x.pos[0].serial != j->header.end.serial) {
1325 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1326 "malformed transaction: "
1327 "%s last serial %u != "
1328 "transaction first serial %u",
1329 j->filename, j->header.end.serial,
1330 j->x.pos[0].serial);
1331 return ISC_R_UNEXPECTED;
1332 }
1333 }
1334
1335 /*
1336 * We currently don't support huge journal entries.
1337 */
1338 total = j->x.pos[1].offset - j->x.pos[0].offset;
1339 if (total >= DNS_JOURNAL_SIZE_MAX) {
1340 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1341 "transaction too big to be stored in journal: "
1342 "%" PRIu64 "b (max is %" PRIu64 "b)",
1343 total, (uint64_t)DNS_JOURNAL_SIZE_MAX);
1344 return ISC_R_UNEXPECTED;
1345 }
1346
1347 /*
1348 * Some old journal entries may become non-addressable
1349 * when we increment the current serial number. Purge them
1350 * by stepping header.begin forward to the first addressable
1351 * transaction. Also purge them from the index.
1352 */
1353 if (!JOURNAL_EMPTY(&j->header)) {
1354 while (!DNS_SERIAL_GT(j->x.pos[1].serial,
1355 j->header.begin.serial))
1356 {
1357 CHECK(journal_next(j, &j->header.begin));
1358 }
1359 index_invalidate(j, j->x.pos[1].serial);
1360 }
1361 #ifdef notyet
1362 if (DNS_SERIAL_GT(last_dumped_serial, j->x.pos[1].serial)) {
1363 force_dump(...);
1364 }
1365 #endif /* ifdef notyet */
1366
1367 /*
1368 * Commit the transaction data to stable storage.
1369 */
1370 CHECK(journal_fsync(j));
1371
1372 if (j->state == JOURNAL_STATE_TRANSACTION) {
1373 off_t offset;
1374 offset = (j->x.pos[1].offset - j->x.pos[0].offset) -
1375 (j->header_ver1 ? sizeof(journal_rawxhdr_ver1_t)
1376 : sizeof(journal_rawxhdr_t));
1377 /*
1378 * Update the transaction header.
1379 */
1380 CHECK(journal_seek(j, j->x.pos[0].offset));
1381 CHECK(journal_write_xhdr(j, offset, j->x.n_rr,
1382 j->x.pos[0].serial,
1383 j->x.pos[1].serial));
1384 }
1385
1386 /*
1387 * Update the journal header.
1388 */
1389 if (JOURNAL_EMPTY(&j->header)) {
1390 j->header.begin = j->x.pos[0];
1391 }
1392 j->header.end = j->x.pos[1];
1393 journal_header_encode(&j->header, &rawheader);
1394 CHECK(journal_seek(j, 0));
1395 CHECK(journal_write(j, &rawheader, sizeof(rawheader)));
1396
1397 /*
1398 * Update the index.
1399 */
1400 index_add(j, &j->x.pos[0]);
1401
1402 /*
1403 * Convert the index into on-disk format and write
1404 * it to disk.
1405 */
1406 CHECK(index_to_disk(j));
1407
1408 /*
1409 * Commit the header to stable storage.
1410 */
1411 CHECK(journal_fsync(j));
1412
1413 /*
1414 * We no longer have a transaction open.
1415 */
1416 j->state = JOURNAL_STATE_WRITE;
1417
1418 result = ISC_R_SUCCESS;
1419
1420 failure:
1421 return result;
1422 }
1423
1424 isc_result_t
1425 dns_journal_write_transaction(dns_journal_t *j, dns_diff_t *diff) {
1426 isc_result_t result;
1427
1428 CHECK(dns_diff_sort(diff, ixfr_order));
1429 CHECK(dns_journal_begin_transaction(j));
1430 CHECK(dns_journal_writediff(j, diff));
1431 CHECK(dns_journal_commit(j));
1432 result = ISC_R_SUCCESS;
1433 failure:
1434 return result;
1435 }
1436
1437 void
1438 dns_journal_destroy(dns_journal_t **journalp) {
1439 dns_journal_t *j = NULL;
1440
1441 REQUIRE(journalp != NULL);
1442 REQUIRE(DNS_JOURNAL_VALID(*journalp));
1443
1444 j = *journalp;
1445 *journalp = NULL;
1446
1447 j->it.result = ISC_R_FAILURE;
1448 dns_name_invalidate(&j->it.name);
1449 if (j->rawindex != NULL) {
1450 isc_mem_cput(j->mctx, j->rawindex, j->header.index_size,
1451 sizeof(journal_rawpos_t));
1452 }
1453 if (j->index != NULL) {
1454 isc_mem_cput(j->mctx, j->index, j->header.index_size,
1455 sizeof(journal_pos_t));
1456 }
1457 if (j->it.target.base != NULL) {
1458 isc_mem_put(j->mctx, j->it.target.base, j->it.target.length);
1459 }
1460 if (j->it.source.base != NULL) {
1461 isc_mem_put(j->mctx, j->it.source.base, j->it.source.length);
1462 }
1463 if (j->filename != NULL) {
1464 isc_mem_free(j->mctx, j->filename);
1465 }
1466 if (j->fp != NULL) {
1467 (void)isc_stdio_close(j->fp);
1468 }
1469 j->magic = 0;
1470 isc_mem_putanddetach(&j->mctx, j, sizeof(*j));
1471 }
1472
1473 /*
1474 * Roll the open journal 'j' into the database 'db'.
1475 * A new database version will be created.
1476 */
1477
1478 /* XXX Share code with incoming IXFR? */
1479
1480 isc_result_t
1481 dns_journal_rollforward(dns_journal_t *j, dns_db_t *db, unsigned int options) {
1482 isc_buffer_t source; /* Transaction data from disk */
1483 isc_buffer_t target; /* Ditto after _fromwire check */
1484 uint32_t db_serial; /* Database SOA serial */
1485 uint32_t end_serial; /* Last journal SOA serial */
1486 isc_result_t result;
1487 dns_dbversion_t *ver = NULL;
1488 journal_pos_t pos;
1489 dns_diff_t diff;
1490 unsigned int n_soa = 0;
1491 unsigned int n_put = 0;
1492 dns_diffop_t op;
1493
1494 REQUIRE(DNS_JOURNAL_VALID(j));
1495 REQUIRE(DNS_DB_VALID(db));
1496
1497 dns_diff_init(j->mctx, &diff);
1498
1499 /*
1500 * Set up empty initial buffers for unchecked and checked
1501 * wire format transaction data. They will be reallocated
1502 * later.
1503 */
1504 isc_buffer_init(&source, NULL, 0);
1505 isc_buffer_init(&target, NULL, 0);
1506
1507 /*
1508 * Create the new database version.
1509 */
1510 CHECK(dns_db_newversion(db, &ver));
1511
1512 /*
1513 * Get the current database SOA serial number.
1514 */
1515 CHECK(dns_db_getsoaserial(db, ver, &db_serial));
1516
1517 /*
1518 * Locate a journal entry for the current database serial.
1519 */
1520 CHECK(journal_find(j, db_serial, &pos));
1521
1522 end_serial = dns_journal_last_serial(j);
1523
1524 /*
1525 * If we're reading a version 1 file, scan all the transactions
1526 * to see if the journal needs rewriting: if any outdated
1527 * transaction headers are found, j->recovered will be set.
1528 */
1529 if (j->header_ver1) {
1530 uint32_t start_serial = dns_journal_first_serial(j);
1531
1532 CHECK(dns_journal_iter_init(j, start_serial, db_serial, NULL));
1533 for (result = dns_journal_first_rr(j); result == ISC_R_SUCCESS;
1534 result = dns_journal_next_rr(j))
1535 {
1536 continue;
1537 }
1538 }
1539
1540 if (db_serial == end_serial) {
1541 CHECK(DNS_R_UPTODATE);
1542 }
1543
1544 CHECK(dns_journal_iter_init(j, db_serial, end_serial, NULL));
1545 for (result = dns_journal_first_rr(j); result == ISC_R_SUCCESS;
1546 result = dns_journal_next_rr(j))
1547 {
1548 dns_name_t *name = NULL;
1549 dns_rdata_t *rdata = NULL;
1550 dns_difftuple_t *tuple = NULL;
1551 uint32_t ttl;
1552
1553 dns_journal_current_rr(j, &name, &ttl, &rdata);
1554
1555 if (rdata->type == dns_rdatatype_soa) {
1556 n_soa++;
1557 if (n_soa == 2) {
1558 db_serial = j->it.current_serial;
1559 }
1560 }
1561
1562 if (n_soa == 3) {
1563 n_soa = 1;
1564 }
1565 if (n_soa == 0) {
1566 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1567 "%s: journal file corrupt: missing "
1568 "initial SOA",
1569 j->filename);
1570 FAIL(ISC_R_UNEXPECTED);
1571 }
1572 if ((options & DNS_JOURNALOPT_RESIGN) != 0) {
1573 op = (n_soa == 1) ? DNS_DIFFOP_DELRESIGN
1574 : DNS_DIFFOP_ADDRESIGN;
1575 } else {
1576 op = (n_soa == 1) ? DNS_DIFFOP_DEL : DNS_DIFFOP_ADD;
1577 }
1578
1579 CHECK(dns_difftuple_create(diff.mctx, op, name, ttl, rdata,
1580 &tuple));
1581 dns_diff_append(&diff, &tuple);
1582
1583 if (++n_put > 100) {
1584 isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1585 "%s: applying diff to database (%u)",
1586 j->filename, db_serial);
1587 (void)dns_diff_print(&diff, NULL);
1588 CHECK(dns_diff_apply(&diff, db, ver));
1589 dns_diff_clear(&diff);
1590 n_put = 0;
1591 }
1592 }
1593 if (result == ISC_R_NOMORE) {
1594 result = ISC_R_SUCCESS;
1595 }
1596 CHECK(result);
1597
1598 if (n_put != 0) {
1599 isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1600 "%s: applying final diff to database (%u)",
1601 j->filename, db_serial);
1602 (void)dns_diff_print(&diff, NULL);
1603 CHECK(dns_diff_apply(&diff, db, ver));
1604 dns_diff_clear(&diff);
1605 }
1606
1607 failure:
1608 if (ver != NULL) {
1609 dns_db_closeversion(db, &ver,
1610 result == ISC_R_SUCCESS ? true : false);
1611 }
1612
1613 if (source.base != NULL) {
1614 isc_mem_put(j->mctx, source.base, source.length);
1615 }
1616 if (target.base != NULL) {
1617 isc_mem_put(j->mctx, target.base, target.length);
1618 }
1619
1620 dns_diff_clear(&diff);
1621
1622 INSIST(ver == NULL);
1623
1624 return result;
1625 }
1626
1627 isc_result_t
1628 dns_journal_print(isc_mem_t *mctx, uint32_t flags, const char *filename,
1629 FILE *file) {
1630 dns_journal_t *j = NULL;
1631 isc_buffer_t source; /* Transaction data from disk */
1632 isc_buffer_t target; /* Ditto after _fromwire check */
1633 uint32_t start_serial; /* Database SOA serial */
1634 uint32_t end_serial; /* Last journal SOA serial */
1635 isc_result_t result;
1636 dns_diff_t diff;
1637 unsigned int n_soa = 0;
1638 unsigned int n_put = 0;
1639 bool printxhdr = ((flags & DNS_JOURNAL_PRINTXHDR) != 0);
1640
1641 REQUIRE(filename != NULL);
1642
1643 result = dns_journal_open(mctx, filename, DNS_JOURNAL_READ, &j);
1644 if (result == ISC_R_NOTFOUND) {
1645 isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no journal file");
1646 return DNS_R_NOJOURNAL;
1647 } else if (result != ISC_R_SUCCESS) {
1648 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1649 "journal open failure: %s: %s",
1650 isc_result_totext(result), filename);
1651 return result;
1652 }
1653
1654 if (printxhdr) {
1655 fprintf(file, "Journal format = %sHeader version = %d\n",
1656 j->header.format + 1, j->header_ver1 ? 1 : 2);
1657 fprintf(file, "Start serial = %u\n", j->header.begin.serial);
1658 fprintf(file, "End serial = %u\n", j->header.end.serial);
1659 fprintf(file, "Index (size = %u):\n", j->header.index_size);
1660 for (uint32_t i = 0; i < j->header.index_size; i++) {
1661 if (j->index[i].offset == 0) {
1662 fputc('\n', file);
1663 break;
1664 }
1665 fprintf(file, "%lld", (long long)j->index[i].offset);
1666 fputc((i + 1) % 8 == 0 ? '\n' : ' ', file);
1667 }
1668 }
1669 if (j->header.serialset) {
1670 fprintf(file, "Source serial = %u\n", j->header.sourceserial);
1671 }
1672 dns_diff_init(j->mctx, &diff);
1673
1674 /*
1675 * Set up empty initial buffers for unchecked and checked
1676 * wire format transaction data. They will be reallocated
1677 * later.
1678 */
1679 isc_buffer_init(&source, NULL, 0);
1680 isc_buffer_init(&target, NULL, 0);
1681
1682 start_serial = dns_journal_first_serial(j);
1683 end_serial = dns_journal_last_serial(j);
1684
1685 CHECK(dns_journal_iter_init(j, start_serial, end_serial, NULL));
1686
1687 for (result = dns_journal_first_rr(j); result == ISC_R_SUCCESS;
1688 result = dns_journal_next_rr(j))
1689 {
1690 dns_name_t *name = NULL;
1691 dns_rdata_t *rdata = NULL;
1692 dns_difftuple_t *tuple = NULL;
1693 static uint32_t i = 0;
1694 bool print = false;
1695 uint32_t ttl;
1696
1697 dns_journal_current_rr(j, &name, &ttl, &rdata);
1698
1699 if (rdata->type == dns_rdatatype_soa) {
1700 n_soa++;
1701 if (n_soa == 3) {
1702 n_soa = 1;
1703 }
1704 if (n_soa == 1) {
1705 print = printxhdr;
1706 }
1707 }
1708 if (n_soa == 0) {
1709 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1710 "%s: journal file corrupt: missing "
1711 "initial SOA",
1712 j->filename);
1713 FAIL(ISC_R_UNEXPECTED);
1714 }
1715
1716 if (print) {
1717 fprintf(file,
1718 "Transaction: version %d offset %lld size %u "
1719 "rrcount %u start %u end %u\n",
1720 j->xhdr_version, (long long)j->it.cpos.offset,
1721 j->curxhdr.size, j->curxhdr.count,
1722 j->curxhdr.serial0, j->curxhdr.serial1);
1723 if (j->it.cpos.offset > j->index[i].offset) {
1724 fprintf(file,
1725 "ERROR: Offset mismatch, "
1726 "expected %lld\n",
1727 (long long)j->index[i].offset);
1728 } else if (j->it.cpos.offset == j->index[i].offset) {
1729 i++;
1730 }
1731 }
1732 CHECK(dns_difftuple_create(
1733 diff.mctx, n_soa == 1 ? DNS_DIFFOP_DEL : DNS_DIFFOP_ADD,
1734 name, ttl, rdata, &tuple));
1735 dns_diff_append(&diff, &tuple);
1736
1737 if (++n_put > 100 || printxhdr) {
1738 result = dns_diff_print(&diff, file);
1739 dns_diff_clear(&diff);
1740 n_put = 0;
1741 if (result != ISC_R_SUCCESS) {
1742 break;
1743 }
1744 }
1745 }
1746 if (result == ISC_R_NOMORE) {
1747 result = ISC_R_SUCCESS;
1748 }
1749 CHECK(result);
1750
1751 if (n_put != 0) {
1752 result = dns_diff_print(&diff, file);
1753 dns_diff_clear(&diff);
1754 }
1755 goto cleanup;
1756
1757 failure:
1758 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1759 "%s: cannot print: journal file corrupt", j->filename);
1760
1761 cleanup:
1762 if (source.base != NULL) {
1763 isc_mem_put(j->mctx, source.base, source.length);
1764 }
1765 if (target.base != NULL) {
1766 isc_mem_put(j->mctx, target.base, target.length);
1767 }
1768
1769 dns_diff_clear(&diff);
1770 dns_journal_destroy(&j);
1771
1772 return result;
1773 }
1774
1775 /**************************************************************************/
1776 /*
1777 * Miscellaneous accessors.
1778 */
1779 bool
1780 dns_journal_empty(dns_journal_t *j) {
1781 return JOURNAL_EMPTY(&j->header);
1782 }
1783
1784 bool
1785 dns_journal_recovered(dns_journal_t *j) {
1786 return j->recovered;
1787 }
1788
1789 uint32_t
1790 dns_journal_first_serial(dns_journal_t *j) {
1791 return j->header.begin.serial;
1792 }
1793
1794 uint32_t
1795 dns_journal_last_serial(dns_journal_t *j) {
1796 return j->header.end.serial;
1797 }
1798
1799 void
1800 dns_journal_set_sourceserial(dns_journal_t *j, uint32_t sourceserial) {
1801 REQUIRE(j->state == JOURNAL_STATE_WRITE ||
1802 j->state == JOURNAL_STATE_INLINE ||
1803 j->state == JOURNAL_STATE_TRANSACTION);
1804
1805 j->header.sourceserial = sourceserial;
1806 j->header.serialset = true;
1807 if (j->state == JOURNAL_STATE_WRITE) {
1808 j->state = JOURNAL_STATE_INLINE;
1809 }
1810 }
1811
1812 bool
1813 dns_journal_get_sourceserial(dns_journal_t *j, uint32_t *sourceserial) {
1814 REQUIRE(sourceserial != NULL);
1815
1816 if (!j->header.serialset) {
1817 return false;
1818 }
1819 *sourceserial = j->header.sourceserial;
1820 return true;
1821 }
1822
1823 /**************************************************************************/
1824 /*
1825 * Iteration support.
1826 *
1827 * When serving an outgoing IXFR, we transmit a part the journal starting
1828 * at the serial number in the IXFR request and ending at the serial
1829 * number that is current when the IXFR request arrives. The ending
1830 * serial number is not necessarily at the end of the journal:
1831 * the journal may grow while the IXFR is in progress, but we stop
1832 * when we reach the serial number that was current when the IXFR started.
1833 */
1834
1835 static isc_result_t
1836 read_one_rr(dns_journal_t *j);
1837
1838 /*
1839 * Make sure the buffer 'b' is has at least 'size' bytes
1840 * allocated, and clear it.
1841 *
1842 * Requires:
1843 * Either b->base is NULL, or it points to b->length bytes of memory
1844 * previously allocated by isc_mem_get().
1845 */
1846
1847 static isc_result_t
1848 size_buffer(isc_mem_t *mctx, isc_buffer_t *b, unsigned int size) {
1849 if (b->length < size) {
1850 void *mem = isc_mem_get(mctx, size);
1851 if (mem == NULL) {
1852 return ISC_R_NOMEMORY;
1853 }
1854 if (b->base != NULL) {
1855 isc_mem_put(mctx, b->base, b->length);
1856 }
1857 b->base = mem;
1858 b->length = size;
1859 }
1860 isc_buffer_clear(b);
1861 return ISC_R_SUCCESS;
1862 }
1863
1864 isc_result_t
1865 dns_journal_iter_init(dns_journal_t *j, uint32_t begin_serial,
1866 uint32_t end_serial, size_t *xfrsizep) {
1867 isc_result_t result;
1868
1869 CHECK(journal_find(j, begin_serial, &j->it.bpos));
1870 INSIST(j->it.bpos.serial == begin_serial);
1871
1872 CHECK(journal_find(j, end_serial, &j->it.epos));
1873 INSIST(j->it.epos.serial == end_serial);
1874
1875 if (xfrsizep != NULL) {
1876 journal_pos_t pos = j->it.bpos;
1877 journal_xhdr_t xhdr;
1878 uint64_t size = 0;
1879 uint32_t count = 0;
1880
1881 /*
1882 * We already know the beginning and ending serial
1883 * numbers are in the journal. Scan through them,
1884 * adding up sizes and RR counts so we can calculate
1885 * the IXFR size.
1886 */
1887 do {
1888 CHECK(journal_seek(j, pos.offset));
1889 CHECK(journal_read_xhdr(j, &xhdr));
1890
1891 if (j->header_ver1) {
1892 CHECK(maybe_fixup_xhdr(j, &xhdr, pos.serial,
1893 pos.offset));
1894 }
1895
1896 /*
1897 * Check that xhdr is consistent.
1898 */
1899 if (xhdr.serial0 != pos.serial ||
1900 isc_serial_le(xhdr.serial1, xhdr.serial0))
1901 {
1902 CHECK(ISC_R_UNEXPECTED);
1903 }
1904
1905 size += xhdr.size;
1906 count += xhdr.count;
1907
1908 result = journal_next(j, &pos);
1909 if (result == ISC_R_NOMORE) {
1910 result = ISC_R_SUCCESS;
1911 }
1912 CHECK(result);
1913 } while (pos.serial != end_serial);
1914
1915 /*
1916 * For each RR, subtract the length of the RR header,
1917 * as this would not be present in IXFR messages.
1918 * (We don't need to worry about the transaction header
1919 * because that was already excluded from xdr.size.)
1920 */
1921 *xfrsizep = size - (ISC_CHECKED_MUL(
1922 count, sizeof(journal_rawrrhdr_t)));
1923 }
1924
1925 result = ISC_R_SUCCESS;
1926 failure:
1927 j->it.result = result;
1928 return j->it.result;
1929 }
1930
1931 isc_result_t
1932 dns_journal_first_rr(dns_journal_t *j) {
1933 isc_result_t result;
1934
1935 /*
1936 * Seek to the beginning of the first transaction we are
1937 * interested in.
1938 */
1939 CHECK(journal_seek(j, j->it.bpos.offset));
1940 j->it.current_serial = j->it.bpos.serial;
1941
1942 j->it.xsize = 0; /* We have no transaction data yet... */
1943 j->it.xpos = 0; /* ...and haven't used any of it. */
1944
1945 return read_one_rr(j);
1946
1947 failure:
1948 return result;
1949 }
1950
1951 static isc_result_t
1952 read_one_rr(dns_journal_t *j) {
1953 isc_result_t result;
1954 dns_rdatatype_t rdtype;
1955 dns_rdataclass_t rdclass;
1956 unsigned int rdlen;
1957 uint32_t ttl;
1958 journal_xhdr_t xhdr;
1959 journal_rrhdr_t rrhdr;
1960 dns_journal_t save = *j;
1961
1962 if (j->offset > j->it.epos.offset) {
1963 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1964 "%s: journal corrupt: possible integer overflow",
1965 j->filename);
1966 return ISC_R_UNEXPECTED;
1967 }
1968 if (j->offset == j->it.epos.offset) {
1969 return ISC_R_NOMORE;
1970 }
1971 if (j->it.xpos == j->it.xsize) {
1972 /*
1973 * We are at a transaction boundary.
1974 * Read another transaction header.
1975 */
1976 CHECK(journal_read_xhdr(j, &xhdr));
1977 if (xhdr.size == 0) {
1978 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1979 "%s: journal corrupt: empty transaction",
1980 j->filename);
1981 FAIL(ISC_R_UNEXPECTED);
1982 }
1983
1984 if (j->header_ver1) {
1985 CHECK(maybe_fixup_xhdr(j, &xhdr, j->it.current_serial,
1986 save.offset));
1987 }
1988
1989 if (xhdr.serial0 != j->it.current_serial ||
1990 isc_serial_le(xhdr.serial1, xhdr.serial0))
1991 {
1992 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1993 "%s: journal file corrupt: "
1994 "expected serial %u, got %u",
1995 j->filename, j->it.current_serial,
1996 xhdr.serial0);
1997 FAIL(ISC_R_UNEXPECTED);
1998 }
1999
2000 j->it.xsize = xhdr.size;
2001 j->it.xpos = 0;
2002 }
2003 /*
2004 * Read an RR.
2005 */
2006 CHECK(journal_read_rrhdr(j, &rrhdr));
2007 /*
2008 * Perform a sanity check on the journal RR size.
2009 * The smallest possible RR has a 1-byte owner name
2010 * and a 10-byte header. The largest possible
2011 * RR has 65535 bytes of data, a header, and a maximum-
2012 * size owner name, well below 70 k total.
2013 */
2014 if (rrhdr.size < 1 + 10 || rrhdr.size > 70000) {
2015 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
2016 "%s: journal corrupt: impossible RR size "
2017 "(%d bytes)",
2018 j->filename, rrhdr.size);
2019 FAIL(ISC_R_UNEXPECTED);
2020 }
2021
2022 CHECK(size_buffer(j->mctx, &j->it.source, rrhdr.size));
2023 CHECK(journal_read(j, j->it.source.base, rrhdr.size));
2024 isc_buffer_add(&j->it.source, rrhdr.size);
2025
2026 /*
2027 * The target buffer is made the same size
2028 * as the source buffer, with the assumption that when
2029 * no compression in present, the output of dns_*_fromwire()
2030 * is no larger than the input.
2031 */
2032 CHECK(size_buffer(j->mctx, &j->it.target, rrhdr.size));
2033
2034 /*
2035 * Parse the owner name. We don't know where it
2036 * ends yet, so we make the entire "remaining"
2037 * part of the buffer "active".
2038 */
2039 isc_buffer_setactive(&j->it.source,
2040 j->it.source.used - j->it.source.current);
2041 CHECK(dns_name_fromwire(&j->it.name, &j->it.source, j->it.dctx,
2042 &j->it.target));
2043
2044 /*
2045 * Check that the RR header is there, and parse it.
2046 */
2047 if (isc_buffer_remaininglength(&j->it.source) < 10) {
2048 FAIL(DNS_R_FORMERR);
2049 }
2050
2051 rdtype = isc_buffer_getuint16(&j->it.source);
2052 rdclass = isc_buffer_getuint16(&j->it.source);
2053 ttl = isc_buffer_getuint32(&j->it.source);
2054 rdlen = isc_buffer_getuint16(&j->it.source);
2055
2056 if (rdlen > DNS_RDATA_MAXLENGTH) {
2057 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
2058 "%s: journal corrupt: impossible rdlen "
2059 "(%u bytes)",
2060 j->filename, rdlen);
2061 FAIL(ISC_R_FAILURE);
2062 }
2063
2064 /*
2065 * Parse the rdata.
2066 */
2067 if (isc_buffer_remaininglength(&j->it.source) != rdlen) {
2068 FAIL(DNS_R_FORMERR);
2069 }
2070 isc_buffer_setactive(&j->it.source, rdlen);
2071 dns_rdata_reset(&j->it.rdata);
2072 CHECK(dns_rdata_fromwire(&j->it.rdata, rdclass, rdtype, &j->it.source,
2073 j->it.dctx, &j->it.target));
2074 j->it.ttl = ttl;
2075
2076 j->it.xpos += sizeof(journal_rawrrhdr_t) + rrhdr.size;
2077 if (rdtype == dns_rdatatype_soa) {
2078 /* XXX could do additional consistency checks here */
2079 j->it.current_serial = dns_soa_getserial(&j->it.rdata);
2080 }
2081
2082 result = ISC_R_SUCCESS;
2083
2084 failure:
2085 j->it.result = result;
2086 return result;
2087 }
2088
2089 isc_result_t
2090 dns_journal_next_rr(dns_journal_t *j) {
2091 j->it.result = read_one_rr(j);
2092 return j->it.result;
2093 }
2094
2095 void
2096 dns_journal_current_rr(dns_journal_t *j, dns_name_t **name, uint32_t *ttl,
2097 dns_rdata_t **rdata) {
2098 REQUIRE(j->it.result == ISC_R_SUCCESS);
2099 *name = &j->it.name;
2100 *ttl = j->it.ttl;
2101 *rdata = &j->it.rdata;
2102 }
2103
2104 /**************************************************************************/
2105 /*
2106 * Generating diffs from databases
2107 */
2108
2109 /*
2110 * Construct a diff containing all the RRs at the current name of the
2111 * database iterator 'dbit' in database 'db', version 'ver'.
2112 * Set '*name' to the current name, and append the diff to 'diff'.
2113 * All new tuples will have the operation 'op'.
2114 *
2115 * Requires: 'name' must have buffer large enough to hold the name.
2116 * Typically, a dns_fixedname_t would be used.
2117 */
2118 static isc_result_t
2119 get_name_diff(dns_db_t *db, dns_dbversion_t *ver, isc_stdtime_t now,
2120 dns_dbiterator_t *dbit, dns_name_t *name, dns_diffop_t op,
2121 dns_diff_t *diff) {
2122 isc_result_t result;
2123 dns_dbnode_t *node = NULL;
2124 dns_rdatasetiter_t *rdsiter = NULL;
2125 dns_difftuple_t *tuple = NULL;
2126
2127 result = dns_dbiterator_current(dbit, &node, name);
2128 if (result != ISC_R_SUCCESS) {
2129 return result;
2130 }
2131
2132 result = dns_db_allrdatasets(db, node, ver, 0, now, &rdsiter);
2133 if (result != ISC_R_SUCCESS) {
2134 goto cleanup_node;
2135 }
2136
2137 for (result = dns_rdatasetiter_first(rdsiter); result == ISC_R_SUCCESS;
2138 result = dns_rdatasetiter_next(rdsiter))
2139 {
2140 dns_rdataset_t rdataset;
2141
2142 dns_rdataset_init(&rdataset);
2143 dns_rdatasetiter_current(rdsiter, &rdataset);
2144
2145 for (result = dns_rdataset_first(&rdataset);
2146 result == ISC_R_SUCCESS;
2147 result = dns_rdataset_next(&rdataset))
2148 {
2149 dns_rdata_t rdata = DNS_RDATA_INIT;
2150 dns_rdataset_current(&rdataset, &rdata);
2151 result = dns_difftuple_create(diff->mctx, op, name,
2152 rdataset.ttl, &rdata,
2153 &tuple);
2154 if (result != ISC_R_SUCCESS) {
2155 dns_rdataset_disassociate(&rdataset);
2156 goto cleanup_iterator;
2157 }
2158 dns_diff_append(diff, &tuple);
2159 }
2160 dns_rdataset_disassociate(&rdataset);
2161 if (result != ISC_R_NOMORE) {
2162 goto cleanup_iterator;
2163 }
2164 }
2165 if (result != ISC_R_NOMORE) {
2166 goto cleanup_iterator;
2167 }
2168
2169 result = ISC_R_SUCCESS;
2170
2171 cleanup_iterator:
2172 dns_rdatasetiter_destroy(&rdsiter);
2173
2174 cleanup_node:
2175 dns_db_detachnode(db, &node);
2176
2177 return result;
2178 }
2179
2180 /*
2181 * Comparison function for use by dns_diff_subtract when sorting
2182 * the diffs to be subtracted. The sort keys are the rdata type
2183 * and the rdata itself. The owner name is ignored, because
2184 * it is known to be the same for all tuples.
2185 */
2186 static int
2187 rdata_order(const void *av, const void *bv) {
2188 dns_difftuple_t const *const *ap = av;
2189 dns_difftuple_t const *const *bp = bv;
2190 dns_difftuple_t const *a = *ap;
2191 dns_difftuple_t const *b = *bp;
2192 int r;
2193 r = (b->rdata.type - a->rdata.type);
2194 if (r != 0) {
2195 return r;
2196 }
2197 r = dns_rdata_compare(&a->rdata, &b->rdata);
2198 return r;
2199 }
2200
2201 static isc_result_t
2202 dns_diff_subtract(dns_diff_t diff[2], dns_diff_t *r) {
2203 isc_result_t result;
2204 dns_difftuple_t *p[2];
2205 int i, t;
2206 bool append;
2207 dns_difftuplelist_t add, del;
2208
2209 CHECK(dns_diff_sort(&diff[0], rdata_order));
2210 CHECK(dns_diff_sort(&diff[1], rdata_order));
2211 ISC_LIST_INIT(add);
2212 ISC_LIST_INIT(del);
2213
2214 for (;;) {
2215 p[0] = ISC_LIST_HEAD(diff[0].tuples);
2216 p[1] = ISC_LIST_HEAD(diff[1].tuples);
2217 if (p[0] == NULL && p[1] == NULL) {
2218 break;
2219 }
2220
2221 for (i = 0; i < 2; i++) {
2222 if (p[!i] == NULL) {
2223 dns_difftuplelist_t *l = (i == 0) ? &add : &del;
2224 ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
2225 ISC_LIST_APPEND(*l, p[i], link);
2226 goto next;
2227 }
2228 }
2229 t = rdata_order(&p[0], &p[1]);
2230 if (t < 0) {
2231 ISC_LIST_UNLINK(diff[0].tuples, p[0], link);
2232 ISC_LIST_APPEND(add, p[0], link);
2233 goto next;
2234 }
2235 if (t > 0) {
2236 ISC_LIST_UNLINK(diff[1].tuples, p[1], link);
2237 ISC_LIST_APPEND(del, p[1], link);
2238 goto next;
2239 }
2240 INSIST(t == 0);
2241 /*
2242 * Identical RRs in both databases; skip them both
2243 * if the ttl differs.
2244 */
2245 append = (p[0]->ttl != p[1]->ttl);
2246 for (i = 0; i < 2; i++) {
2247 ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
2248 if (append) {
2249 dns_difftuplelist_t *l = (i == 0) ? &add : &del;
2250 ISC_LIST_APPEND(*l, p[i], link);
2251 } else {
2252 dns_difftuple_free(&p[i]);
2253 }
2254 }
2255 next:;
2256 }
2257 ISC_LIST_APPENDLIST(r->tuples, del, link);
2258 ISC_LIST_APPENDLIST(r->tuples, add, link);
2259 result = ISC_R_SUCCESS;
2260 failure:
2261 return result;
2262 }
2263
2264 static isc_result_t
2265 diff_namespace(dns_db_t *dba, dns_dbversion_t *dbvera, dns_db_t *dbb,
2266 dns_dbversion_t *dbverb, unsigned int options,
2267 dns_diff_t *resultdiff) {
2268 dns_db_t *db[2];
2269 dns_dbversion_t *ver[2];
2270 dns_dbiterator_t *dbit[2] = { NULL, NULL };
2271 bool have[2] = { false, false };
2272 dns_fixedname_t fixname[2];
2273 isc_result_t result, itresult[2];
2274 dns_diff_t diff[2];
2275 int i, t;
2276
2277 db[0] = dba, db[1] = dbb;
2278 ver[0] = dbvera, ver[1] = dbverb;
2279
2280 dns_diff_init(resultdiff->mctx, &diff[0]);
2281 dns_diff_init(resultdiff->mctx, &diff[1]);
2282
2283 dns_fixedname_init(&fixname[0]);
2284 dns_fixedname_init(&fixname[1]);
2285
2286 result = dns_db_createiterator(db[0], options, &dbit[0]);
2287 if (result != ISC_R_SUCCESS) {
2288 return result;
2289 }
2290 result = dns_db_createiterator(db[1], options, &dbit[1]);
2291 if (result != ISC_R_SUCCESS) {
2292 goto cleanup_iterator;
2293 }
2294
2295 itresult[0] = dns_dbiterator_first(dbit[0]);
2296 itresult[1] = dns_dbiterator_first(dbit[1]);
2297
2298 for (;;) {
2299 for (i = 0; i < 2; i++) {
2300 if (!have[i] && itresult[i] == ISC_R_SUCCESS) {
2301 CHECK(get_name_diff(
2302 db[i], ver[i], 0, dbit[i],
2303 dns_fixedname_name(&fixname[i]),
2304 i == 0 ? DNS_DIFFOP_ADD
2305 : DNS_DIFFOP_DEL,
2306 &diff[i]));
2307 itresult[i] = dns_dbiterator_next(dbit[i]);
2308 have[i] = true;
2309 }
2310 }
2311
2312 if (!have[0] && !have[1]) {
2313 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
2314 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
2315 break;
2316 }
2317
2318 for (i = 0; i < 2; i++) {
2319 if (!have[!i]) {
2320 ISC_LIST_APPENDLIST(resultdiff->tuples,
2321 diff[i].tuples, link);
2322 INSIST(ISC_LIST_EMPTY(diff[i].tuples));
2323 have[i] = false;
2324 goto next;
2325 }
2326 }
2327
2328 t = dns_name_compare(dns_fixedname_name(&fixname[0]),
2329 dns_fixedname_name(&fixname[1]));
2330 if (t < 0) {
2331 ISC_LIST_APPENDLIST(resultdiff->tuples, diff[0].tuples,
2332 link);
2333 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
2334 have[0] = false;
2335 continue;
2336 }
2337 if (t > 0) {
2338 ISC_LIST_APPENDLIST(resultdiff->tuples, diff[1].tuples,
2339 link);
2340 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
2341 have[1] = false;
2342 continue;
2343 }
2344 INSIST(t == 0);
2345 CHECK(dns_diff_subtract(diff, resultdiff));
2346 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
2347 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
2348 have[0] = have[1] = false;
2349 next:;
2350 }
2351 if (itresult[0] != ISC_R_NOMORE) {
2352 FAIL(itresult[0]);
2353 }
2354 if (itresult[1] != ISC_R_NOMORE) {
2355 FAIL(itresult[1]);
2356 }
2357
2358 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
2359 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
2360
2361 failure:
2362 dns_dbiterator_destroy(&dbit[1]);
2363
2364 cleanup_iterator:
2365 dns_dbiterator_destroy(&dbit[0]);
2366 dns_diff_clear(&diff[0]);
2367 dns_diff_clear(&diff[1]);
2368 return result;
2369 }
2370
2371 /*
2372 * Compare the databases 'dba' and 'dbb' and generate a journal
2373 * entry containing the changes to make 'dba' from 'dbb' (note
2374 * the order). This journal entry will consist of a single,
2375 * possibly very large transaction.
2376 */
2377 isc_result_t
2378 dns_db_diff(isc_mem_t *mctx, dns_db_t *dba, dns_dbversion_t *dbvera,
2379 dns_db_t *dbb, dns_dbversion_t *dbverb, const char *filename) {
2380 isc_result_t result;
2381 dns_diff_t diff;
2382
2383 dns_diff_init(mctx, &diff);
2384
2385 result = dns_db_diffx(&diff, dba, dbvera, dbb, dbverb, filename);
2386
2387 dns_diff_clear(&diff);
2388
2389 return result;
2390 }
2391
2392 isc_result_t
2393 dns_db_diffx(dns_diff_t *diff, dns_db_t *dba, dns_dbversion_t *dbvera,
2394 dns_db_t *dbb, dns_dbversion_t *dbverb, const char *filename) {
2395 isc_result_t result;
2396 dns_journal_t *journal = NULL;
2397
2398 if (filename != NULL) {
2399 result = dns_journal_open(diff->mctx, filename,
2400 DNS_JOURNAL_CREATE, &journal);
2401 if (result != ISC_R_SUCCESS) {
2402 return result;
2403 }
2404 }
2405
2406 CHECK(diff_namespace(dba, dbvera, dbb, dbverb, DNS_DB_NONSEC3, diff));
2407 CHECK(diff_namespace(dba, dbvera, dbb, dbverb, DNS_DB_NSEC3ONLY, diff));
2408
2409 if (journal != NULL) {
2410 if (ISC_LIST_EMPTY(diff->tuples)) {
2411 isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no changes");
2412 } else {
2413 CHECK(dns_journal_write_transaction(journal, diff));
2414 }
2415 }
2416
2417 failure:
2418 if (journal != NULL) {
2419 dns_journal_destroy(&journal);
2420 }
2421 return result;
2422 }
2423
2424 static uint32_t
2425 rrcount(unsigned char *buf, unsigned int size) {
2426 isc_buffer_t b;
2427 uint32_t rrsize, count = 0;
2428
2429 isc_buffer_init(&b, buf, size);
2430 isc_buffer_add(&b, size);
2431 while (isc_buffer_remaininglength(&b) > 0) {
2432 rrsize = isc_buffer_getuint32(&b);
2433 INSIST(isc_buffer_remaininglength(&b) >= rrsize);
2434 isc_buffer_forward(&b, rrsize);
2435 count++;
2436 }
2437
2438 return count;
2439 }
2440
2441 static bool
2442 check_delta(unsigned char *buf, size_t size) {
2443 isc_buffer_t b;
2444 uint32_t rrsize;
2445
2446 isc_buffer_init(&b, buf, size);
2447 isc_buffer_add(&b, size);
2448 while (isc_buffer_remaininglength(&b) > 0) {
2449 if (isc_buffer_remaininglength(&b) < 4) {
2450 return false;
2451 }
2452 rrsize = isc_buffer_getuint32(&b);
2453 /* "." + type + class + ttl + rdlen => 11U */
2454 if (rrsize < 11U || isc_buffer_remaininglength(&b) < rrsize) {
2455 return false;
2456 }
2457 isc_buffer_forward(&b, rrsize);
2458 }
2459
2460 return true;
2461 }
2462
2463 isc_result_t
2464 dns_journal_compact(isc_mem_t *mctx, char *filename, uint32_t serial,
2465 uint32_t flags, uint32_t target_size) {
2466 unsigned int i;
2467 journal_pos_t best_guess;
2468 journal_pos_t current_pos;
2469 dns_journal_t *j1 = NULL;
2470 dns_journal_t *j2 = NULL;
2471 journal_rawheader_t rawheader;
2472 unsigned int len;
2473 size_t namelen;
2474 unsigned char *buf = NULL;
2475 unsigned int size = 0;
2476 isc_result_t result;
2477 unsigned int indexend;
2478 char newname[PATH_MAX];
2479 char backup[PATH_MAX];
2480 bool is_backup = false;
2481 bool rewrite = false;
2482 bool downgrade = false;
2483
2484 REQUIRE(filename != NULL);
2485
2486 namelen = strlen(filename);
2487 if (namelen > 4U && strcmp(filename + namelen - 4, ".jnl") == 0) {
2488 namelen -= 4;
2489 }
2490
2491 result = snprintf(newname, sizeof(newname), "%.*s.jnw", (int)namelen,
2492 filename);
2493 RUNTIME_CHECK(result < sizeof(newname));
2494
2495 result = snprintf(backup, sizeof(backup), "%.*s.jbk", (int)namelen,
2496 filename);
2497 RUNTIME_CHECK(result < sizeof(backup));
2498
2499 result = journal_open(mctx, filename, false, false, false, &j1);
2500 if (result == ISC_R_NOTFOUND) {
2501 is_backup = true;
2502 result = journal_open(mctx, backup, false, false, false, &j1);
2503 }
2504 if (result != ISC_R_SUCCESS) {
2505 return result;
2506 }
2507
2508 /*
2509 * Always perform a re-write when processing a version 1 journal.
2510 */
2511 rewrite = j1->header_ver1;
2512
2513 /*
2514 * Check whether we need to rewrite the whole journal
2515 * file (for example, to upversion it).
2516 */
2517 if ((flags & DNS_JOURNAL_COMPACTALL) != 0) {
2518 if ((flags & DNS_JOURNAL_VERSION1) != 0) {
2519 downgrade = true;
2520 }
2521 rewrite = true;
2522 serial = dns_journal_first_serial(j1);
2523 } else if (JOURNAL_EMPTY(&j1->header)) {
2524 dns_journal_destroy(&j1);
2525 return ISC_R_SUCCESS;
2526 }
2527
2528 if (DNS_SERIAL_GT(j1->header.begin.serial, serial) ||
2529 DNS_SERIAL_GT(serial, j1->header.end.serial))
2530 {
2531 dns_journal_destroy(&j1);
2532 return ISC_R_RANGE;
2533 }
2534
2535 /*
2536 * Cope with very small target sizes.
2537 */
2538 indexend = sizeof(journal_rawheader_t) +
2539 ISC_CHECKED_MUL(j1->header.index_size,
2540 sizeof(journal_rawpos_t));
2541 if (target_size < DNS_JOURNAL_SIZE_MIN) {
2542 target_size = DNS_JOURNAL_SIZE_MIN;
2543 }
2544 if (target_size < indexend * 2) {
2545 target_size = target_size / 2 + indexend;
2546 }
2547
2548 /*
2549 * See if there is any work to do.
2550 */
2551 if (!rewrite && (uint32_t)j1->header.end.offset < target_size) {
2552 dns_journal_destroy(&j1);
2553 return ISC_R_SUCCESS;
2554 }
2555
2556 CHECK(journal_open(mctx, newname, true, true, downgrade, &j2));
2557 CHECK(journal_seek(j2, indexend));
2558
2559 /*
2560 * Remove overhead so space test below can succeed.
2561 */
2562 if (target_size >= indexend) {
2563 target_size -= indexend;
2564 }
2565
2566 /*
2567 * Find if we can create enough free space.
2568 */
2569 best_guess = j1->header.begin;
2570 for (i = 0; i < j1->header.index_size; i++) {
2571 if (POS_VALID(j1->index[i]) &&
2572 DNS_SERIAL_GE(serial, j1->index[i].serial) &&
2573 ((uint32_t)(j1->header.end.offset - j1->index[i].offset) >=
2574 target_size / 2) &&
2575 j1->index[i].offset > best_guess.offset)
2576 {
2577 best_guess = j1->index[i];
2578 }
2579 }
2580
2581 current_pos = best_guess;
2582 while (current_pos.serial != serial) {
2583 CHECK(journal_next(j1, ¤t_pos));
2584 if (current_pos.serial == j1->header.end.serial) {
2585 break;
2586 }
2587
2588 if (DNS_SERIAL_GE(serial, current_pos.serial) &&
2589 ((uint32_t)(j1->header.end.offset - current_pos.offset) >=
2590 (target_size / 2)) &&
2591 current_pos.offset > best_guess.offset)
2592 {
2593 best_guess = current_pos;
2594 } else {
2595 break;
2596 }
2597 }
2598
2599 INSIST(best_guess.serial != j1->header.end.serial);
2600 if (best_guess.serial != serial) {
2601 CHECK(journal_next(j1, &best_guess));
2602 serial = best_guess.serial;
2603 }
2604
2605 /*
2606 * We should now be roughly half target_size provided
2607 * we did not reach 'serial'. If not we will just copy
2608 * all uncommitted deltas regardless of the size.
2609 */
2610 len = j1->header.end.offset - best_guess.offset;
2611 if (len != 0) {
2612 CHECK(journal_seek(j1, best_guess.offset));
2613
2614 /* Prepare new header */
2615 j2->header.begin.serial = best_guess.serial;
2616 j2->header.begin.offset = indexend;
2617 j2->header.sourceserial = j1->header.sourceserial;
2618 j2->header.serialset = j1->header.serialset;
2619 j2->header.end.serial = j1->header.end.serial;
2620
2621 /*
2622 * Only use this method if we're rewriting the
2623 * journal to fix outdated transaction headers;
2624 * otherwise we'll copy the whole journal without
2625 * parsing individual deltas below.
2626 */
2627 while (rewrite && len > 0) {
2628 journal_xhdr_t xhdr;
2629 off_t offset = j1->offset;
2630 uint32_t count;
2631
2632 result = journal_read_xhdr(j1, &xhdr);
2633 if (rewrite && result == ISC_R_NOMORE) {
2634 break;
2635 }
2636 CHECK(result);
2637
2638 size = xhdr.size;
2639 if (size > len) {
2640 isc_log_write(JOURNAL_COMMON_LOGARGS,
2641 ISC_LOG_ERROR,
2642 "%s: journal file corrupt, "
2643 "transaction too large",
2644 j1->filename);
2645 CHECK(ISC_R_FAILURE);
2646 }
2647 buf = isc_mem_get(mctx, size);
2648 result = journal_read(j1, buf, size);
2649
2650 /*
2651 * If we're repairing an outdated journal, the
2652 * xhdr format may be wrong.
2653 */
2654 if (rewrite && (result != ISC_R_SUCCESS ||
2655 !check_delta(buf, size)))
2656 {
2657 if (j1->xhdr_version == XHDR_VERSION2) {
2658 /* XHDR_VERSION2 -> XHDR_VERSION1 */
2659 j1->xhdr_version = XHDR_VERSION1;
2660 CHECK(journal_seek(j1, offset));
2661 CHECK(journal_read_xhdr(j1, &xhdr));
2662 } else if (j1->xhdr_version == XHDR_VERSION1) {
2663 /* XHDR_VERSION1 -> XHDR_VERSION2 */
2664 j1->xhdr_version = XHDR_VERSION2;
2665 CHECK(journal_seek(j1, offset));
2666 CHECK(journal_read_xhdr(j1, &xhdr));
2667 }
2668
2669 /* Check again */
2670 isc_mem_put(mctx, buf, size);
2671 size = xhdr.size;
2672 if (size > len) {
2673 isc_log_write(
2674 JOURNAL_COMMON_LOGARGS,
2675 ISC_LOG_ERROR,
2676 "%s: journal file corrupt, "
2677 "transaction too large",
2678 j1->filename);
2679 CHECK(ISC_R_FAILURE);
2680 }
2681 buf = isc_mem_get(mctx, size);
2682 CHECK(journal_read(j1, buf, size));
2683
2684 if (!check_delta(buf, size)) {
2685 CHECK(ISC_R_UNEXPECTED);
2686 }
2687 } else {
2688 CHECK(result);
2689 }
2690
2691 /*
2692 * Recover from incorrectly written transaction header.
2693 * The incorrect header was written as size, serial0,
2694 * serial1, and 0. XHDR_VERSION2 is expecting size,
2695 * count, serial0, and serial1.
2696 */
2697 if (j1->xhdr_version == XHDR_VERSION2 &&
2698 xhdr.count == serial && xhdr.serial1 == 0U &&
2699 isc_serial_gt(xhdr.serial0, xhdr.count))
2700 {
2701 xhdr.serial1 = xhdr.serial0;
2702 xhdr.serial0 = xhdr.count;
2703 xhdr.count = 0;
2704 }
2705
2706 /*
2707 * Check that xhdr is consistent.
2708 */
2709 if (xhdr.serial0 != serial ||
2710 isc_serial_le(xhdr.serial1, xhdr.serial0))
2711 {
2712 CHECK(ISC_R_UNEXPECTED);
2713 }
2714
2715 /*
2716 * Extract record count from the transaction. This
2717 * is needed when converting from XHDR_VERSION1 to
2718 * XHDR_VERSION2, and when recovering from an
2719 * incorrectly written XHDR_VERSION2.
2720 */
2721 count = rrcount(buf, size);
2722 CHECK(journal_write_xhdr(j2, xhdr.size, count,
2723 xhdr.serial0, xhdr.serial1));
2724 CHECK(journal_write(j2, buf, size));
2725
2726 j2->header.end.offset = j2->offset;
2727
2728 serial = xhdr.serial1;
2729
2730 len = j1->header.end.offset - j1->offset;
2731 isc_mem_put(mctx, buf, size);
2732 }
2733
2734 /*
2735 * If we're not rewriting transaction headers, we can use
2736 * this faster method instead.
2737 */
2738 if (!rewrite) {
2739 size = ISC_MIN(64 * 1024, len);
2740 buf = isc_mem_get(mctx, size);
2741 for (i = 0; i < len; i += size) {
2742 unsigned int blob = ISC_MIN(size, len - i);
2743 CHECK(journal_read(j1, buf, blob));
2744 CHECK(journal_write(j2, buf, blob));
2745 }
2746
2747 j2->header.end.offset = indexend + len;
2748 }
2749
2750 CHECK(journal_fsync(j2));
2751
2752 /*
2753 * Update the journal header.
2754 */
2755 journal_header_encode(&j2->header, &rawheader);
2756 CHECK(journal_seek(j2, 0));
2757 CHECK(journal_write(j2, &rawheader, sizeof(rawheader)));
2758 CHECK(journal_fsync(j2));
2759
2760 /*
2761 * Build new index.
2762 */
2763 current_pos = j2->header.begin;
2764 while (current_pos.serial != j2->header.end.serial) {
2765 index_add(j2, ¤t_pos);
2766 CHECK(journal_next(j2, ¤t_pos));
2767 }
2768
2769 /*
2770 * Write index.
2771 */
2772 CHECK(index_to_disk(j2));
2773 CHECK(journal_fsync(j2));
2774
2775 indexend = j2->header.end.offset;
2776 POST(indexend);
2777 }
2778
2779 /*
2780 * Close both journals before trying to rename files.
2781 */
2782 dns_journal_destroy(&j1);
2783 dns_journal_destroy(&j2);
2784
2785 /*
2786 * With a UFS file system this should just succeed and be atomic.
2787 * Any IXFR outs will just continue and the old journal will be
2788 * removed on final close.
2789 *
2790 * With MSDOS / NTFS we need to do a two stage rename, triggered
2791 * by EEXIST. (If any IXFR's are running in other threads, however,
2792 * this will fail, and the journal will not be compacted. But
2793 * if so, hopefully they'll be finished by the next time we
2794 * compact.)
2795 */
2796 if (rename(newname, filename) == -1) {
2797 if (errno == EEXIST && !is_backup) {
2798 result = isc_file_remove(backup);
2799 if (result != ISC_R_SUCCESS &&
2800 result != ISC_R_FILENOTFOUND)
2801 {
2802 goto failure;
2803 }
2804 if (rename(filename, backup) == -1) {
2805 goto maperrno;
2806 }
2807 if (rename(newname, filename) == -1) {
2808 goto maperrno;
2809 }
2810 (void)isc_file_remove(backup);
2811 } else {
2812 maperrno:
2813 result = ISC_R_FAILURE;
2814 goto failure;
2815 }
2816 }
2817
2818 result = ISC_R_SUCCESS;
2819
2820 failure:
2821 (void)isc_file_remove(newname);
2822 if (buf != NULL) {
2823 isc_mem_put(mctx, buf, size);
2824 }
2825 if (j1 != NULL) {
2826 dns_journal_destroy(&j1);
2827 }
2828 if (j2 != NULL) {
2829 dns_journal_destroy(&j2);
2830 }
2831 return result;
2832 }
2833
2834 static isc_result_t
2835 index_to_disk(dns_journal_t *j) {
2836 isc_result_t result = ISC_R_SUCCESS;
2837
2838 if (j->header.index_size != 0) {
2839 unsigned int i;
2840 unsigned char *p;
2841 unsigned int rawbytes;
2842
2843 rawbytes = ISC_CHECKED_MUL(j->header.index_size,
2844 sizeof(journal_rawpos_t));
2845
2846 p = j->rawindex;
2847 for (i = 0; i < j->header.index_size; i++) {
2848 encode_uint32(j->index[i].serial, p);
2849 p += 4;
2850 encode_uint32(j->index[i].offset, p);
2851 p += 4;
2852 }
2853 INSIST(p == j->rawindex + rawbytes);
2854
2855 CHECK(journal_seek(j, sizeof(journal_rawheader_t)));
2856 CHECK(journal_write(j, j->rawindex, rawbytes));
2857 }
2858 failure:
2859 return result;
2860 }
2861