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