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