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