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