Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / journal.c
blob9c9fa8233fc7f218e450871c5de85782ee5a2b57
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2002 Internet Software Consortium.
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.
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.
20 /* Id: journal.c,v 1.110 2009/11/04 23:48:18 tbox Exp */
22 #include <config.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <errno.h>
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>
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>
46 /*! \file
47 * \brief Journaling.
49 * A journal file consists of
51 * \li A fixed-size header of type journal_rawheader_t.
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.
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.
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.
83 /*%
84 * When true, accept IXFR difference sequences where the
85 * SOA serial number does not change (BIND 8 sends such
86 * sequences).
88 static isc_boolean_t bind8_compat = ISC_TRUE; /* XXX config */
90 /**************************************************************************/
92 * Miscellaneous utilities.
95 #define JOURNAL_COMMON_LOGARGS \
96 dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_JOURNAL
98 #define JOURNAL_DEBUG_LOGARGS(n) \
99 JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(n)
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".
106 #define FAIL(code) \
107 do { result = (code); \
108 if (result != ISC_R_SUCCESS) goto failure; \
109 } while (0)
111 #define CHECK(op) \
112 do { result = (op); \
113 if (result != ISC_R_SUCCESS) goto failure; \
114 } while (0)
116 static isc_result_t index_to_disk(dns_journal_t *);
118 static inline isc_uint32_t
119 decode_uint32(unsigned char *p) {
120 return ((p[0] << 24) +
121 (p[1] << 16) +
122 (p[2] << 8) +
123 (p[3] << 0));
126 static inline void
127 encode_uint32(isc_uint32_t val, unsigned char *p) {
128 p[0] = (isc_uint8_t)(val >> 24);
129 p[1] = (isc_uint8_t)(val >> 16);
130 p[2] = (isc_uint8_t)(val >> 8);
131 p[3] = (isc_uint8_t)(val >> 0);
134 isc_result_t
135 dns_db_createsoatuple(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
136 dns_diffop_t op, dns_difftuple_t **tp)
138 isc_result_t result;
139 dns_dbnode_t *node;
140 dns_rdataset_t rdataset;
141 dns_rdata_t rdata = DNS_RDATA_INIT;
142 dns_name_t *zonename;
144 zonename = dns_db_origin(db);
146 node = NULL;
147 result = dns_db_findnode(db, zonename, ISC_FALSE, &node);
148 if (result != ISC_R_SUCCESS)
149 goto nonode;
151 dns_rdataset_init(&rdataset);
152 result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
153 (isc_stdtime_t)0, &rdataset, NULL);
154 if (result != ISC_R_SUCCESS)
155 goto freenode;
157 result = dns_rdataset_first(&rdataset);
158 if (result != ISC_R_SUCCESS)
159 goto freenode;
161 dns_rdataset_current(&rdataset, &rdata);
163 result = dns_difftuple_create(mctx, op, zonename, rdataset.ttl,
164 &rdata, tp);
166 dns_rdataset_disassociate(&rdataset);
167 dns_db_detachnode(db, &node);
168 return (ISC_R_SUCCESS);
170 freenode:
171 dns_db_detachnode(db, &node);
172 nonode:
173 UNEXPECTED_ERROR(__FILE__, __LINE__, "missing SOA");
174 return (result);
177 /* Journaling */
180 * On-disk representation of a "pointer" to a journal entry.
181 * These are used in the journal header to locate the beginning
182 * and end of the journal, and in the journal index to locate
183 * other transactions.
185 typedef struct {
186 unsigned char serial[4]; /*%< SOA serial before update. */
188 * XXXRTH Should offset be 8 bytes?
189 * XXXDCL ... probably, since isc_offset_t is 8 bytes on many OSs.
190 * XXXAG ... but we will not be able to seek >2G anyway on many
191 * platforms as long as we are using fseek() rather
192 * than lseek().
194 unsigned char offset[4]; /*%< Offset from beginning of file. */
195 } journal_rawpos_t;
199 * The header is of a fixed size, with some spare room for future
200 * extensions.
202 #define JOURNAL_HEADER_SIZE 64 /* Bytes. */
205 * The on-disk representation of the journal header.
206 * All numbers are stored in big-endian order.
208 typedef union {
209 struct {
210 /*% File format version ID. */
211 unsigned char format[16];
212 /*% Position of the first addressable transaction */
213 journal_rawpos_t begin;
214 /*% Position of the next (yet nonexistent) transaction. */
215 journal_rawpos_t end;
216 /*% Number of index entries following the header. */
217 unsigned char index_size[4];
218 } h;
219 /* Pad the header to a fixed size. */
220 unsigned char pad[JOURNAL_HEADER_SIZE];
221 } journal_rawheader_t;
224 * The on-disk representation of the transaction header.
225 * There is one of these at the beginning of each transaction.
227 typedef struct {
228 unsigned char size[4]; /*%< In bytes, excluding header. */
229 unsigned char serial0[4]; /*%< SOA serial before update. */
230 unsigned char serial1[4]; /*%< SOA serial after update. */
231 } journal_rawxhdr_t;
234 * The on-disk representation of the RR header.
235 * There is one of these at the beginning of each RR.
237 typedef struct {
238 unsigned char size[4]; /*%< In bytes, excluding header. */
239 } journal_rawrrhdr_t;
242 * The in-core representation of the journal header.
244 typedef struct {
245 isc_uint32_t serial;
246 isc_offset_t offset;
247 } journal_pos_t;
249 #define POS_VALID(pos) ((pos).offset != 0)
250 #define POS_INVALIDATE(pos) ((pos).offset = 0, (pos).serial = 0)
252 typedef struct {
253 unsigned char format[16];
254 journal_pos_t begin;
255 journal_pos_t end;
256 isc_uint32_t index_size;
257 } journal_header_t;
260 * The in-core representation of the transaction header.
263 typedef struct {
264 isc_uint32_t size;
265 isc_uint32_t serial0;
266 isc_uint32_t serial1;
267 } journal_xhdr_t;
270 * The in-core representation of the RR header.
272 typedef struct {
273 isc_uint32_t size;
274 } journal_rrhdr_t;
278 * Initial contents to store in the header of a newly created
279 * journal file.
281 * The header starts with the magic string ";BIND LOG V9\n"
282 * to identify the file as a BIND 9 journal file. An ASCII
283 * identification string is used rather than a binary magic
284 * number to be consistent with BIND 8 (BIND 8 journal files
285 * are ASCII text files).
288 static journal_header_t
289 initial_journal_header = { ";BIND LOG V9\n", { 0, 0 }, { 0, 0 }, 0 };
291 #define JOURNAL_EMPTY(h) ((h)->begin.offset == (h)->end.offset)
293 typedef enum {
294 JOURNAL_STATE_INVALID,
295 JOURNAL_STATE_READ,
296 JOURNAL_STATE_WRITE,
297 JOURNAL_STATE_TRANSACTION
298 } journal_state_t;
300 struct dns_journal {
301 unsigned int magic; /*%< JOUR */
302 isc_mem_t *mctx; /*%< Memory context */
303 journal_state_t state;
304 const char *filename; /*%< Journal file name */
305 FILE * fp; /*%< File handle */
306 isc_offset_t offset; /*%< Current file offset */
307 journal_header_t header; /*%< In-core journal header */
308 unsigned char *rawindex; /*%< In-core buffer for journal index in on-disk format */
309 journal_pos_t *index; /*%< In-core journal index */
311 /*% Current transaction state (when writing). */
312 struct {
313 unsigned int n_soa; /*%< Number of SOAs seen */
314 journal_pos_t pos[2]; /*%< Begin/end position */
315 } x;
317 /*% Iteration state (when reading). */
318 struct {
319 /* These define the part of the journal we iterate over. */
320 journal_pos_t bpos; /*%< Position before first, */
321 journal_pos_t epos; /*%< and after last transaction */
322 /* The rest is iterator state. */
323 isc_uint32_t current_serial; /*%< Current SOA serial */
324 isc_buffer_t source; /*%< Data from disk */
325 isc_buffer_t target; /*%< Data from _fromwire check */
326 dns_decompress_t dctx; /*%< Dummy decompression ctx */
327 dns_name_t name; /*%< Current domain name */
328 dns_rdata_t rdata; /*%< Current rdata */
329 isc_uint32_t ttl; /*%< Current TTL */
330 unsigned int xsize; /*%< Size of transaction data */
331 unsigned int xpos; /*%< Current position in it */
332 isc_result_t result; /*%< Result of last call */
333 } it;
336 #define DNS_JOURNAL_MAGIC ISC_MAGIC('J', 'O', 'U', 'R')
337 #define DNS_JOURNAL_VALID(t) ISC_MAGIC_VALID(t, DNS_JOURNAL_MAGIC)
339 static void
340 journal_pos_decode(journal_rawpos_t *raw, journal_pos_t *cooked) {
341 cooked->serial = decode_uint32(raw->serial);
342 cooked->offset = decode_uint32(raw->offset);
345 static void
346 journal_pos_encode(journal_rawpos_t *raw, journal_pos_t *cooked) {
347 encode_uint32(cooked->serial, raw->serial);
348 encode_uint32(cooked->offset, raw->offset);
351 static void
352 journal_header_decode(journal_rawheader_t *raw, journal_header_t *cooked) {
353 INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
354 memcpy(cooked->format, raw->h.format, sizeof(cooked->format));
355 journal_pos_decode(&raw->h.begin, &cooked->begin);
356 journal_pos_decode(&raw->h.end, &cooked->end);
357 cooked->index_size = decode_uint32(raw->h.index_size);
360 static void
361 journal_header_encode(journal_header_t *cooked, journal_rawheader_t *raw) {
362 INSIST(sizeof(cooked->format) == sizeof(raw->h.format));
363 memset(raw->pad, 0, sizeof(raw->pad));
364 memcpy(raw->h.format, cooked->format, sizeof(raw->h.format));
365 journal_pos_encode(&raw->h.begin, &cooked->begin);
366 journal_pos_encode(&raw->h.end, &cooked->end);
367 encode_uint32(cooked->index_size, raw->h.index_size);
371 * Journal file I/O subroutines, with error checking and reporting.
373 static isc_result_t
374 journal_seek(dns_journal_t *j, isc_uint32_t offset) {
375 isc_result_t result;
376 result = isc_stdio_seek(j->fp, (long)offset, SEEK_SET);
377 if (result != ISC_R_SUCCESS) {
378 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
379 "%s: seek: %s", j->filename,
380 isc_result_totext(result));
381 return (ISC_R_UNEXPECTED);
383 j->offset = offset;
384 return (ISC_R_SUCCESS);
387 static isc_result_t
388 journal_read(dns_journal_t *j, void *mem, size_t nbytes) {
389 isc_result_t result;
391 result = isc_stdio_read(mem, 1, nbytes, j->fp, NULL);
392 if (result != ISC_R_SUCCESS) {
393 if (result == ISC_R_EOF)
394 return (ISC_R_NOMORE);
395 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
396 "%s: read: %s",
397 j->filename, isc_result_totext(result));
398 return (ISC_R_UNEXPECTED);
400 j->offset += nbytes;
401 return (ISC_R_SUCCESS);
404 static isc_result_t
405 journal_write(dns_journal_t *j, void *mem, size_t nbytes) {
406 isc_result_t result;
408 result = isc_stdio_write(mem, 1, nbytes, j->fp, NULL);
409 if (result != ISC_R_SUCCESS) {
410 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
411 "%s: write: %s",
412 j->filename, isc_result_totext(result));
413 return (ISC_R_UNEXPECTED);
415 j->offset += nbytes;
416 return (ISC_R_SUCCESS);
419 static isc_result_t
420 journal_fsync(dns_journal_t *j) {
421 isc_result_t result;
422 result = isc_stdio_flush(j->fp);
423 if (result != ISC_R_SUCCESS) {
424 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
425 "%s: flush: %s",
426 j->filename, isc_result_totext(result));
427 return (ISC_R_UNEXPECTED);
429 result = isc_stdio_sync(j->fp);
430 if (result != ISC_R_SUCCESS) {
431 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
432 "%s: fsync: %s",
433 j->filename, isc_result_totext(result));
434 return (ISC_R_UNEXPECTED);
436 return (ISC_R_SUCCESS);
440 * Read/write a transaction header at the current file position.
443 static isc_result_t
444 journal_read_xhdr(dns_journal_t *j, journal_xhdr_t *xhdr) {
445 journal_rawxhdr_t raw;
446 isc_result_t result;
447 result = journal_read(j, &raw, sizeof(raw));
448 if (result != ISC_R_SUCCESS)
449 return (result);
450 xhdr->size = decode_uint32(raw.size);
451 xhdr->serial0 = decode_uint32(raw.serial0);
452 xhdr->serial1 = decode_uint32(raw.serial1);
453 return (ISC_R_SUCCESS);
456 static isc_result_t
457 journal_write_xhdr(dns_journal_t *j, isc_uint32_t size,
458 isc_uint32_t serial0, isc_uint32_t serial1)
460 journal_rawxhdr_t raw;
461 encode_uint32(size, raw.size);
462 encode_uint32(serial0, raw.serial0);
463 encode_uint32(serial1, raw.serial1);
464 return (journal_write(j, &raw, sizeof(raw)));
469 * Read an RR header at the current file position.
472 static isc_result_t
473 journal_read_rrhdr(dns_journal_t *j, journal_rrhdr_t *rrhdr) {
474 journal_rawrrhdr_t raw;
475 isc_result_t result;
476 result = journal_read(j, &raw, sizeof(raw));
477 if (result != ISC_R_SUCCESS)
478 return (result);
479 rrhdr->size = decode_uint32(raw.size);
480 return (ISC_R_SUCCESS);
483 static isc_result_t
484 journal_file_create(isc_mem_t *mctx, const char *filename) {
485 FILE *fp = NULL;
486 isc_result_t result;
487 journal_header_t header;
488 journal_rawheader_t rawheader;
489 int index_size = 56; /* XXX configurable */
490 int size;
491 void *mem; /* Memory for temporary index image. */
493 INSIST(sizeof(journal_rawheader_t) == JOURNAL_HEADER_SIZE);
495 result = isc_stdio_open(filename, "wb", &fp);
496 if (result != ISC_R_SUCCESS) {
497 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
498 "%s: create: %s",
499 filename, isc_result_totext(result));
500 return (ISC_R_UNEXPECTED);
503 header = initial_journal_header;
504 header.index_size = index_size;
505 journal_header_encode(&header, &rawheader);
507 size = sizeof(journal_rawheader_t) +
508 index_size * sizeof(journal_rawpos_t);
510 mem = isc_mem_get(mctx, size);
511 if (mem == NULL) {
512 (void)isc_stdio_close(fp);
513 (void)isc_file_remove(filename);
514 return (ISC_R_NOMEMORY);
516 memset(mem, 0, size);
517 memcpy(mem, &rawheader, sizeof(rawheader));
519 result = isc_stdio_write(mem, 1, (size_t) size, fp, NULL);
520 if (result != ISC_R_SUCCESS) {
521 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
522 "%s: write: %s",
523 filename, isc_result_totext(result));
524 (void)isc_stdio_close(fp);
525 (void)isc_file_remove(filename);
526 isc_mem_put(mctx, mem, size);
527 return (ISC_R_UNEXPECTED);
529 isc_mem_put(mctx, mem, size);
531 result = isc_stdio_close(fp);
532 if (result != ISC_R_SUCCESS) {
533 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
534 "%s: close: %s",
535 filename, isc_result_totext(result));
536 (void)isc_file_remove(filename);
537 return (ISC_R_UNEXPECTED);
540 return (ISC_R_SUCCESS);
543 static isc_result_t
544 journal_open(isc_mem_t *mctx, const char *filename, isc_boolean_t write,
545 isc_boolean_t create, dns_journal_t **journalp) {
546 FILE *fp = NULL;
547 isc_result_t result;
548 journal_rawheader_t rawheader;
549 dns_journal_t *j;
551 INSIST(journalp != NULL && *journalp == NULL);
552 j = isc_mem_get(mctx, sizeof(*j));
553 if (j == NULL)
554 return (ISC_R_NOMEMORY);
556 j->mctx = mctx;
557 j->state = JOURNAL_STATE_INVALID;
558 j->fp = NULL;
559 j->filename = filename;
560 j->index = NULL;
561 j->rawindex = NULL;
563 result = isc_stdio_open(j->filename, write ? "rb+" : "rb", &fp);
565 if (result == ISC_R_FILENOTFOUND) {
566 if (create) {
567 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_DEBUG(1),
568 "journal file %s does not exist, "
569 "creating it", j->filename);
570 CHECK(journal_file_create(mctx, filename));
572 * Retry.
574 result = isc_stdio_open(j->filename, "rb+", &fp);
575 } else {
576 FAIL(ISC_R_NOTFOUND);
579 if (result != ISC_R_SUCCESS) {
580 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
581 "%s: open: %s",
582 j->filename, isc_result_totext(result));
583 FAIL(ISC_R_UNEXPECTED);
586 j->fp = fp;
589 * Set magic early so that seek/read can succeed.
591 j->magic = DNS_JOURNAL_MAGIC;
593 CHECK(journal_seek(j, 0));
594 CHECK(journal_read(j, &rawheader, sizeof(rawheader)));
596 if (memcmp(rawheader.h.format, initial_journal_header.format,
597 sizeof(initial_journal_header.format)) != 0) {
598 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
599 "%s: journal format not recognized",
600 j->filename);
601 FAIL(ISC_R_UNEXPECTED);
603 journal_header_decode(&rawheader, &j->header);
606 * If there is an index, read the raw index into a dynamically
607 * allocated buffer and then convert it into a cooked index.
609 if (j->header.index_size != 0) {
610 unsigned int i;
611 unsigned int rawbytes;
612 unsigned char *p;
614 rawbytes = j->header.index_size * sizeof(journal_rawpos_t);
615 j->rawindex = isc_mem_get(mctx, rawbytes);
616 if (j->rawindex == NULL)
617 FAIL(ISC_R_NOMEMORY);
619 CHECK(journal_read(j, j->rawindex, rawbytes));
621 j->index = isc_mem_get(mctx, j->header.index_size *
622 sizeof(journal_pos_t));
623 if (j->index == NULL)
624 FAIL(ISC_R_NOMEMORY);
626 p = j->rawindex;
627 for (i = 0; i < j->header.index_size; i++) {
628 j->index[i].serial = decode_uint32(p);
629 p += 4;
630 j->index[i].offset = decode_uint32(p);
631 p += 4;
633 INSIST(p == j->rawindex + rawbytes);
635 j->offset = -1; /* Invalid, must seek explicitly. */
638 * Initialize the iterator.
640 dns_name_init(&j->it.name, NULL);
641 dns_rdata_init(&j->it.rdata);
644 * Set up empty initial buffers for unchecked and checked
645 * wire format RR data. They will be reallocated
646 * later.
648 isc_buffer_init(&j->it.source, NULL, 0);
649 isc_buffer_init(&j->it.target, NULL, 0);
650 dns_decompress_init(&j->it.dctx, -1, DNS_DECOMPRESS_NONE);
652 j->state =
653 write ? JOURNAL_STATE_WRITE : JOURNAL_STATE_READ;
655 *journalp = j;
656 return (ISC_R_SUCCESS);
658 failure:
659 j->magic = 0;
660 if (j->index != NULL) {
661 isc_mem_put(j->mctx, j->index, j->header.index_size *
662 sizeof(journal_rawpos_t));
663 j->index = NULL;
665 if (j->fp != NULL)
666 (void)isc_stdio_close(j->fp);
667 isc_mem_put(j->mctx, j, sizeof(*j));
668 return (result);
671 isc_result_t
672 dns_journal_open(isc_mem_t *mctx, const char *filename, isc_boolean_t write,
673 dns_journal_t **journalp) {
674 isc_result_t result;
675 int namelen;
676 char backup[1024];
678 result = journal_open(mctx, filename, write, write, journalp);
679 if (result == ISC_R_NOTFOUND) {
680 namelen = strlen(filename);
681 if (namelen > 4 && strcmp(filename + namelen - 4, ".jnl") == 0)
682 namelen -= 4;
684 result = isc_string_printf(backup, sizeof(backup), "%.*s.jbk",
685 namelen, filename);
686 if (result != ISC_R_SUCCESS)
687 return (result);
688 result = journal_open(mctx, backup, write, write, journalp);
690 return (result);
694 * A comparison function defining the sorting order for
695 * entries in the IXFR-style journal file.
697 * The IXFR format requires that deletions are sorted before
698 * additions, and within either one, SOA records are sorted
699 * before others.
701 * Also sort the non-SOA records by type as a courtesy to the
702 * server receiving the IXFR - it may help reduce the amount of
703 * rdataset merging it has to do.
705 static int
706 ixfr_order(const void *av, const void *bv) {
707 dns_difftuple_t const * const *ap = av;
708 dns_difftuple_t const * const *bp = bv;
709 dns_difftuple_t const *a = *ap;
710 dns_difftuple_t const *b = *bp;
711 int r;
712 int bop = 0, aop = 0;
714 switch (a->op) {
715 case DNS_DIFFOP_DEL:
716 case DNS_DIFFOP_DELRESIGN:
717 aop = 1;
718 break;
719 case DNS_DIFFOP_ADD:
720 case DNS_DIFFOP_ADDRESIGN:
721 aop = 0;
722 break;
723 default:
724 INSIST(0);
727 switch (b->op) {
728 case DNS_DIFFOP_DEL:
729 case DNS_DIFFOP_DELRESIGN:
730 bop = 1;
731 break;
732 case DNS_DIFFOP_ADD:
733 case DNS_DIFFOP_ADDRESIGN:
734 bop = 0;
735 break;
736 default:
737 INSIST(0);
740 r = bop - aop;
741 if (r != 0)
742 return (r);
744 r = (b->rdata.type == dns_rdatatype_soa) -
745 (a->rdata.type == dns_rdatatype_soa);
746 if (r != 0)
747 return (r);
749 r = (a->rdata.type - b->rdata.type);
750 return (r);
754 * Advance '*pos' to the next journal transaction.
756 * Requires:
757 * *pos refers to a valid journal transaction.
759 * Ensures:
760 * When ISC_R_SUCCESS is returned,
761 * *pos refers to the next journal transaction.
763 * Returns one of:
765 * ISC_R_SUCCESS
766 * ISC_R_NOMORE *pos pointed at the last transaction
767 * Other results due to file errors are possible.
769 static isc_result_t
770 journal_next(dns_journal_t *j, journal_pos_t *pos) {
771 isc_result_t result;
772 journal_xhdr_t xhdr;
773 REQUIRE(DNS_JOURNAL_VALID(j));
775 result = journal_seek(j, pos->offset);
776 if (result != ISC_R_SUCCESS)
777 return (result);
779 if (pos->serial == j->header.end.serial)
780 return (ISC_R_NOMORE);
782 * Read the header of the current transaction.
783 * This will return ISC_R_NOMORE if we are at EOF.
785 result = journal_read_xhdr(j, &xhdr);
786 if (result != ISC_R_SUCCESS)
787 return (result);
790 * Check serial number consistency.
792 if (xhdr.serial0 != pos->serial) {
793 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
794 "%s: journal file corrupt: "
795 "expected serial %u, got %u",
796 j->filename, pos->serial, xhdr.serial0);
797 return (ISC_R_UNEXPECTED);
801 * Check for offset wraparound.
803 if ((isc_offset_t)(pos->offset + sizeof(journal_rawxhdr_t) + xhdr.size)
804 < pos->offset) {
805 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
806 "%s: offset too large", j->filename);
807 return (ISC_R_UNEXPECTED);
810 pos->offset += sizeof(journal_rawxhdr_t) + xhdr.size;
811 pos->serial = xhdr.serial1;
812 return (ISC_R_SUCCESS);
816 * If the index of the journal 'j' contains an entry "better"
817 * than '*best_guess', replace '*best_guess' with it.
819 * "Better" means having a serial number closer to 'serial'
820 * but not greater than 'serial'.
822 static void
823 index_find(dns_journal_t *j, isc_uint32_t serial, journal_pos_t *best_guess) {
824 unsigned int i;
825 if (j->index == NULL)
826 return;
827 for (i = 0; i < j->header.index_size; i++) {
828 if (POS_VALID(j->index[i]) &&
829 DNS_SERIAL_GE(serial, j->index[i].serial) &&
830 DNS_SERIAL_GT(j->index[i].serial, best_guess->serial))
831 *best_guess = j->index[i];
836 * Add a new index entry. If there is no room, make room by removing
837 * the odd-numbered entries and compacting the others into the first
838 * half of the index. This decimates old index entries exponentially
839 * over time, so that the index always contains a much larger fraction
840 * of recent serial numbers than of old ones. This is deliberate -
841 * most index searches are for outgoing IXFR, and IXFR tends to request
842 * recent versions more often than old ones.
844 static void
845 index_add(dns_journal_t *j, journal_pos_t *pos) {
846 unsigned int i;
847 if (j->index == NULL)
848 return;
850 * Search for a vacant position.
852 for (i = 0; i < j->header.index_size; i++) {
853 if (! POS_VALID(j->index[i]))
854 break;
856 if (i == j->header.index_size) {
857 unsigned int k = 0;
859 * Found no vacant position. Make some room.
861 for (i = 0; i < j->header.index_size; i += 2) {
862 j->index[k++] = j->index[i];
864 i = k; /* 'i' identifies the first vacant position. */
865 while (k < j->header.index_size) {
866 POS_INVALIDATE(j->index[k]);
867 k++;
870 INSIST(i < j->header.index_size);
871 INSIST(! POS_VALID(j->index[i]));
874 * Store the new index entry.
876 j->index[i] = *pos;
880 * Invalidate any existing index entries that could become
881 * ambiguous when a new transaction with number 'serial' is added.
883 static void
884 index_invalidate(dns_journal_t *j, isc_uint32_t serial) {
885 unsigned int i;
886 if (j->index == NULL)
887 return;
888 for (i = 0; i < j->header.index_size; i++) {
889 if (! DNS_SERIAL_GT(serial, j->index[i].serial))
890 POS_INVALIDATE(j->index[i]);
895 * Try to find a transaction with initial serial number 'serial'
896 * in the journal 'j'.
898 * If found, store its position at '*pos' and return ISC_R_SUCCESS.
900 * If 'serial' is current (= the ending serial number of the
901 * last transaction in the journal), set '*pos' to
902 * the position immediately following the last transaction and
903 * return ISC_R_SUCCESS.
905 * If 'serial' is within the range of addressable serial numbers
906 * covered by the journal but that particular serial number is missing
907 * (from the journal, not just from the index), return ISC_R_NOTFOUND.
909 * If 'serial' is outside the range of addressable serial numbers
910 * covered by the journal, return ISC_R_RANGE.
913 static isc_result_t
914 journal_find(dns_journal_t *j, isc_uint32_t serial, journal_pos_t *pos) {
915 isc_result_t result;
916 journal_pos_t current_pos;
917 REQUIRE(DNS_JOURNAL_VALID(j));
919 if (DNS_SERIAL_GT(j->header.begin.serial, serial))
920 return (ISC_R_RANGE);
921 if (DNS_SERIAL_GT(serial, j->header.end.serial))
922 return (ISC_R_RANGE);
923 if (serial == j->header.end.serial) {
924 *pos = j->header.end;
925 return (ISC_R_SUCCESS);
928 current_pos = j->header.begin;
929 index_find(j, serial, &current_pos);
931 while (current_pos.serial != serial) {
932 if (DNS_SERIAL_GT(current_pos.serial, serial))
933 return (ISC_R_NOTFOUND);
934 result = journal_next(j, &current_pos);
935 if (result != ISC_R_SUCCESS)
936 return (result);
938 *pos = current_pos;
939 return (ISC_R_SUCCESS);
942 isc_result_t
943 dns_journal_begin_transaction(dns_journal_t *j) {
944 isc_uint32_t offset;
945 isc_result_t result;
946 journal_rawxhdr_t hdr;
948 REQUIRE(DNS_JOURNAL_VALID(j));
949 REQUIRE(j->state == JOURNAL_STATE_WRITE);
952 * Find the file offset where the new transaction should
953 * be written, and seek there.
955 if (JOURNAL_EMPTY(&j->header)) {
956 offset = sizeof(journal_rawheader_t) +
957 j->header.index_size * sizeof(journal_rawpos_t);
958 } else {
959 offset = j->header.end.offset;
961 j->x.pos[0].offset = offset;
962 j->x.pos[1].offset = offset; /* Initial value, will be incremented. */
963 j->x.n_soa = 0;
965 CHECK(journal_seek(j, offset));
968 * Write a dummy transaction header of all zeroes to reserve
969 * space. It will be filled in when the transaction is
970 * finished.
972 memset(&hdr, 0, sizeof(hdr));
973 CHECK(journal_write(j, &hdr, sizeof(hdr)));
974 j->x.pos[1].offset = j->offset;
976 j->state = JOURNAL_STATE_TRANSACTION;
977 result = ISC_R_SUCCESS;
978 failure:
979 return (result);
982 isc_result_t
983 dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff) {
984 dns_difftuple_t *t;
985 isc_buffer_t buffer;
986 void *mem = NULL;
987 unsigned int size;
988 isc_result_t result;
989 isc_region_t used;
991 REQUIRE(DNS_DIFF_VALID(diff));
992 REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
994 isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "writing to journal");
995 (void)dns_diff_print(diff, NULL);
998 * Pass 1: determine the buffer size needed, and
999 * keep track of SOA serial numbers.
1001 size = 0;
1002 for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
1003 t = ISC_LIST_NEXT(t, link))
1005 if (t->rdata.type == dns_rdatatype_soa) {
1006 if (j->x.n_soa < 2)
1007 j->x.pos[j->x.n_soa].serial =
1008 dns_soa_getserial(&t->rdata);
1009 j->x.n_soa++;
1011 size += sizeof(journal_rawrrhdr_t);
1012 size += t->name.length; /* XXX should have access macro? */
1013 size += 10;
1014 size += t->rdata.length;
1017 mem = isc_mem_get(j->mctx, size);
1018 if (mem == NULL)
1019 return (ISC_R_NOMEMORY);
1021 isc_buffer_init(&buffer, mem, size);
1024 * Pass 2. Write RRs to buffer.
1026 for (t = ISC_LIST_HEAD(diff->tuples); t != NULL;
1027 t = ISC_LIST_NEXT(t, link))
1030 * Write the RR header.
1032 isc_buffer_putuint32(&buffer, t->name.length + 10 +
1033 t->rdata.length);
1035 * Write the owner name, RR header, and RR data.
1037 isc_buffer_putmem(&buffer, t->name.ndata, t->name.length);
1038 isc_buffer_putuint16(&buffer, t->rdata.type);
1039 isc_buffer_putuint16(&buffer, t->rdata.rdclass);
1040 isc_buffer_putuint32(&buffer, t->ttl);
1041 INSIST(t->rdata.length < 65536);
1042 isc_buffer_putuint16(&buffer, (isc_uint16_t)t->rdata.length);
1043 INSIST(isc_buffer_availablelength(&buffer) >= t->rdata.length);
1044 isc_buffer_putmem(&buffer, t->rdata.data, t->rdata.length);
1047 isc_buffer_usedregion(&buffer, &used);
1048 INSIST(used.length == size);
1050 j->x.pos[1].offset += used.length;
1053 * Write the buffer contents to the journal file.
1055 CHECK(journal_write(j, used.base, used.length));
1057 result = ISC_R_SUCCESS;
1059 failure:
1060 if (mem != NULL)
1061 isc_mem_put(j->mctx, mem, size);
1062 return (result);
1066 isc_result_t
1067 dns_journal_commit(dns_journal_t *j) {
1068 isc_result_t result;
1069 journal_rawheader_t rawheader;
1071 REQUIRE(DNS_JOURNAL_VALID(j));
1072 REQUIRE(j->state == JOURNAL_STATE_TRANSACTION);
1075 * Perform some basic consistency checks.
1077 if (j->x.n_soa != 2) {
1078 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1079 "%s: malformed transaction: %d SOAs",
1080 j->filename, j->x.n_soa);
1081 return (ISC_R_UNEXPECTED);
1083 if (! (DNS_SERIAL_GT(j->x.pos[1].serial, j->x.pos[0].serial) ||
1084 (bind8_compat &&
1085 j->x.pos[1].serial == j->x.pos[0].serial)))
1087 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1088 "%s: malformed transaction: serial number "
1089 "would decrease", j->filename);
1090 return (ISC_R_UNEXPECTED);
1092 if (! JOURNAL_EMPTY(&j->header)) {
1093 if (j->x.pos[0].serial != j->header.end.serial) {
1094 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1095 "malformed transaction: "
1096 "%s last serial %u != "
1097 "transaction first serial %u",
1098 j->filename,
1099 j->header.end.serial,
1100 j->x.pos[0].serial);
1101 return (ISC_R_UNEXPECTED);
1106 * Some old journal entries may become non-addressable
1107 * when we increment the current serial number. Purge them
1108 * by stepping header.begin forward to the first addressable
1109 * transaction. Also purge them from the index.
1111 if (! JOURNAL_EMPTY(&j->header)) {
1112 while (! DNS_SERIAL_GT(j->x.pos[1].serial,
1113 j->header.begin.serial)) {
1114 CHECK(journal_next(j, &j->header.begin));
1116 index_invalidate(j, j->x.pos[1].serial);
1118 #ifdef notyet
1119 if (DNS_SERIAL_GT(last_dumped_serial, j->x.pos[1].serial)) {
1120 force_dump(...);
1122 #endif
1125 * Commit the transaction data to stable storage.
1127 CHECK(journal_fsync(j));
1130 * Update the transaction header.
1132 CHECK(journal_seek(j, j->x.pos[0].offset));
1133 CHECK(journal_write_xhdr(j, (j->x.pos[1].offset - j->x.pos[0].offset) -
1134 sizeof(journal_rawxhdr_t),
1135 j->x.pos[0].serial, j->x.pos[1].serial));
1138 * Update the journal header.
1140 if (JOURNAL_EMPTY(&j->header)) {
1141 j->header.begin = j->x.pos[0];
1143 j->header.end = j->x.pos[1];
1144 journal_header_encode(&j->header, &rawheader);
1145 CHECK(journal_seek(j, 0));
1146 CHECK(journal_write(j, &rawheader, sizeof(rawheader)));
1149 * Update the index.
1151 index_add(j, &j->x.pos[0]);
1154 * Convert the index into on-disk format and write
1155 * it to disk.
1157 CHECK(index_to_disk(j));
1160 * Commit the header to stable storage.
1162 CHECK(journal_fsync(j));
1165 * We no longer have a transaction open.
1167 j->state = JOURNAL_STATE_WRITE;
1169 result = ISC_R_SUCCESS;
1171 failure:
1172 return (result);
1175 isc_result_t
1176 dns_journal_write_transaction(dns_journal_t *j, dns_diff_t *diff) {
1177 isc_result_t result;
1178 CHECK(dns_diff_sort(diff, ixfr_order));
1179 CHECK(dns_journal_begin_transaction(j));
1180 CHECK(dns_journal_writediff(j, diff));
1181 CHECK(dns_journal_commit(j));
1182 result = ISC_R_SUCCESS;
1183 failure:
1184 return (result);
1187 void
1188 dns_journal_destroy(dns_journal_t **journalp) {
1189 dns_journal_t *j = *journalp;
1190 REQUIRE(DNS_JOURNAL_VALID(j));
1192 j->it.result = ISC_R_FAILURE;
1193 dns_name_invalidate(&j->it.name);
1194 dns_decompress_invalidate(&j->it.dctx);
1195 if (j->rawindex != NULL)
1196 isc_mem_put(j->mctx, j->rawindex, j->header.index_size *
1197 sizeof(journal_rawpos_t));
1198 if (j->index != NULL)
1199 isc_mem_put(j->mctx, j->index, j->header.index_size *
1200 sizeof(journal_pos_t));
1201 if (j->it.target.base != NULL)
1202 isc_mem_put(j->mctx, j->it.target.base, j->it.target.length);
1203 if (j->it.source.base != NULL)
1204 isc_mem_put(j->mctx, j->it.source.base, j->it.source.length);
1206 if (j->fp != NULL)
1207 (void)isc_stdio_close(j->fp);
1208 j->magic = 0;
1209 isc_mem_put(j->mctx, j, sizeof(*j));
1210 *journalp = NULL;
1214 * Roll the open journal 'j' into the database 'db'.
1215 * A new database version will be created.
1218 /* XXX Share code with incoming IXFR? */
1220 static isc_result_t
1221 roll_forward(dns_journal_t *j, dns_db_t *db, unsigned int options,
1222 isc_uint32_t resign)
1224 isc_buffer_t source; /* Transaction data from disk */
1225 isc_buffer_t target; /* Ditto after _fromwire check */
1226 isc_uint32_t db_serial; /* Database SOA serial */
1227 isc_uint32_t end_serial; /* Last journal SOA serial */
1228 isc_result_t result;
1229 dns_dbversion_t *ver = NULL;
1230 journal_pos_t pos;
1231 dns_diff_t diff;
1232 unsigned int n_soa = 0;
1233 unsigned int n_put = 0;
1234 dns_diffop_t op;
1236 REQUIRE(DNS_JOURNAL_VALID(j));
1237 REQUIRE(DNS_DB_VALID(db));
1239 dns_diff_init(j->mctx, &diff);
1240 diff.resign = resign;
1243 * Set up empty initial buffers for unchecked and checked
1244 * wire format transaction data. They will be reallocated
1245 * later.
1247 isc_buffer_init(&source, NULL, 0);
1248 isc_buffer_init(&target, NULL, 0);
1251 * Create the new database version.
1253 CHECK(dns_db_newversion(db, &ver));
1256 * Get the current database SOA serial number.
1258 CHECK(dns_db_getsoaserial(db, ver, &db_serial));
1261 * Locate a journal entry for the current database serial.
1263 CHECK(journal_find(j, db_serial, &pos));
1265 * XXX do more drastic things, like marking zone stale,
1266 * if this fails?
1269 * XXXRTH The zone code should probably mark the zone as bad and
1270 * scream loudly into the log if this is a dynamic update
1271 * log reply that failed.
1274 end_serial = dns_journal_last_serial(j);
1275 if (db_serial == end_serial)
1276 CHECK(DNS_R_UPTODATE);
1278 CHECK(dns_journal_iter_init(j, db_serial, end_serial));
1280 for (result = dns_journal_first_rr(j);
1281 result == ISC_R_SUCCESS;
1282 result = dns_journal_next_rr(j))
1284 dns_name_t *name;
1285 isc_uint32_t ttl;
1286 dns_rdata_t *rdata;
1287 dns_difftuple_t *tuple = NULL;
1289 name = NULL;
1290 rdata = NULL;
1291 dns_journal_current_rr(j, &name, &ttl, &rdata);
1293 if (rdata->type == dns_rdatatype_soa) {
1294 n_soa++;
1295 if (n_soa == 2)
1296 db_serial = j->it.current_serial;
1299 if (n_soa == 3)
1300 n_soa = 1;
1301 if (n_soa == 0) {
1302 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1303 "%s: journal file corrupt: missing "
1304 "initial SOA", j->filename);
1305 FAIL(ISC_R_UNEXPECTED);
1307 if ((options & DNS_JOURNALOPT_RESIGN) != 0)
1308 op = (n_soa == 1) ? DNS_DIFFOP_DELRESIGN :
1309 DNS_DIFFOP_ADDRESIGN;
1310 else
1311 op = (n_soa == 1) ? DNS_DIFFOP_DEL : DNS_DIFFOP_ADD;
1313 CHECK(dns_difftuple_create(diff.mctx, op, name, ttl, rdata,
1314 &tuple));
1315 dns_diff_append(&diff, &tuple);
1317 if (++n_put > 100) {
1318 isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1319 "%s: applying diff to database (%u)",
1320 j->filename, db_serial);
1321 (void)dns_diff_print(&diff, NULL);
1322 CHECK(dns_diff_apply(&diff, db, ver));
1323 dns_diff_clear(&diff);
1324 n_put = 0;
1327 if (result == ISC_R_NOMORE)
1328 result = ISC_R_SUCCESS;
1329 CHECK(result);
1331 if (n_put != 0) {
1332 isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1333 "%s: applying final diff to database (%u)",
1334 j->filename, db_serial);
1335 (void)dns_diff_print(&diff, NULL);
1336 CHECK(dns_diff_apply(&diff, db, ver));
1337 dns_diff_clear(&diff);
1340 failure:
1341 if (ver != NULL)
1342 dns_db_closeversion(db, &ver, result == ISC_R_SUCCESS ?
1343 ISC_TRUE : ISC_FALSE);
1345 if (source.base != NULL)
1346 isc_mem_put(j->mctx, source.base, source.length);
1347 if (target.base != NULL)
1348 isc_mem_put(j->mctx, target.base, target.length);
1350 dns_diff_clear(&diff);
1352 return (result);
1355 isc_result_t
1356 dns_journal_rollforward(isc_mem_t *mctx, dns_db_t *db,
1357 unsigned int options, const char *filename)
1359 REQUIRE((options & DNS_JOURNALOPT_RESIGN) == 0);
1360 return (dns_journal_rollforward2(mctx, db, options, 0, filename));
1363 isc_result_t
1364 dns_journal_rollforward2(isc_mem_t *mctx, dns_db_t *db, unsigned int options,
1365 isc_uint32_t resign, const char *filename)
1367 dns_journal_t *j;
1368 isc_result_t result;
1370 REQUIRE(DNS_DB_VALID(db));
1371 REQUIRE(filename != NULL);
1373 j = NULL;
1374 result = dns_journal_open(mctx, filename, ISC_FALSE, &j);
1375 if (result == ISC_R_NOTFOUND) {
1376 isc_log_write(JOURNAL_DEBUG_LOGARGS(3),
1377 "no journal file, but that's OK");
1378 return (DNS_R_NOJOURNAL);
1380 if (result != ISC_R_SUCCESS)
1381 return (result);
1382 if (JOURNAL_EMPTY(&j->header))
1383 result = DNS_R_UPTODATE;
1384 else
1385 result = roll_forward(j, db, options, resign);
1387 dns_journal_destroy(&j);
1389 return (result);
1392 isc_result_t
1393 dns_journal_print(isc_mem_t *mctx, const char *filename, FILE *file) {
1394 dns_journal_t *j;
1395 isc_buffer_t source; /* Transaction data from disk */
1396 isc_buffer_t target; /* Ditto after _fromwire check */
1397 isc_uint32_t start_serial; /* Database SOA serial */
1398 isc_uint32_t end_serial; /* Last journal SOA serial */
1399 isc_result_t result;
1400 dns_diff_t diff;
1401 unsigned int n_soa = 0;
1402 unsigned int n_put = 0;
1404 REQUIRE(filename != NULL);
1406 j = NULL;
1407 result = dns_journal_open(mctx, filename, ISC_FALSE, &j);
1408 if (result == ISC_R_NOTFOUND) {
1409 isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no journal file");
1410 return (DNS_R_NOJOURNAL);
1413 if (result != ISC_R_SUCCESS) {
1414 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1415 "journal open failure: %s: %s",
1416 isc_result_totext(result), filename);
1417 return (result);
1420 dns_diff_init(j->mctx, &diff);
1423 * Set up empty initial buffers for unchecked and checked
1424 * wire format transaction data. They will be reallocated
1425 * later.
1427 isc_buffer_init(&source, NULL, 0);
1428 isc_buffer_init(&target, NULL, 0);
1430 start_serial = dns_journal_first_serial(j);
1431 end_serial = dns_journal_last_serial(j);
1433 CHECK(dns_journal_iter_init(j, start_serial, end_serial));
1435 for (result = dns_journal_first_rr(j);
1436 result == ISC_R_SUCCESS;
1437 result = dns_journal_next_rr(j))
1439 dns_name_t *name;
1440 isc_uint32_t ttl;
1441 dns_rdata_t *rdata;
1442 dns_difftuple_t *tuple = NULL;
1444 name = NULL;
1445 rdata = NULL;
1446 dns_journal_current_rr(j, &name, &ttl, &rdata);
1448 if (rdata->type == dns_rdatatype_soa)
1449 n_soa++;
1451 if (n_soa == 3)
1452 n_soa = 1;
1453 if (n_soa == 0) {
1454 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1455 "%s: journal file corrupt: missing "
1456 "initial SOA", j->filename);
1457 FAIL(ISC_R_UNEXPECTED);
1459 CHECK(dns_difftuple_create(diff.mctx, n_soa == 1 ?
1460 DNS_DIFFOP_DEL : DNS_DIFFOP_ADD,
1461 name, ttl, rdata, &tuple));
1462 dns_diff_append(&diff, &tuple);
1464 if (++n_put > 100) {
1465 result = dns_diff_print(&diff, file);
1466 dns_diff_clear(&diff);
1467 n_put = 0;
1468 if (result != ISC_R_SUCCESS)
1469 break;
1472 if (result == ISC_R_NOMORE)
1473 result = ISC_R_SUCCESS;
1474 CHECK(result);
1476 if (n_put != 0) {
1477 result = dns_diff_print(&diff, file);
1478 dns_diff_clear(&diff);
1480 goto cleanup;
1482 failure:
1483 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1484 "%s: cannot print: journal file corrupt", j->filename);
1486 cleanup:
1487 if (source.base != NULL)
1488 isc_mem_put(j->mctx, source.base, source.length);
1489 if (target.base != NULL)
1490 isc_mem_put(j->mctx, target.base, target.length);
1492 dns_diff_clear(&diff);
1493 dns_journal_destroy(&j);
1495 return (result);
1498 /**************************************************************************/
1500 * Miscellaneous accessors.
1502 isc_uint32_t dns_journal_first_serial(dns_journal_t *j) {
1503 return (j->header.begin.serial);
1506 isc_uint32_t dns_journal_last_serial(dns_journal_t *j) {
1507 return (j->header.end.serial);
1510 /**************************************************************************/
1512 * Iteration support.
1514 * When serving an outgoing IXFR, we transmit a part the journal starting
1515 * at the serial number in the IXFR request and ending at the serial
1516 * number that is current when the IXFR request arrives. The ending
1517 * serial number is not necessarily at the end of the journal:
1518 * the journal may grow while the IXFR is in progress, but we stop
1519 * when we reach the serial number that was current when the IXFR started.
1522 static isc_result_t read_one_rr(dns_journal_t *j);
1525 * Make sure the buffer 'b' is has at least 'size' bytes
1526 * allocated, and clear it.
1528 * Requires:
1529 * Either b->base is NULL, or it points to b->length bytes of memory
1530 * previously allocated by isc_mem_get().
1533 static isc_result_t
1534 size_buffer(isc_mem_t *mctx, isc_buffer_t *b, unsigned size) {
1535 if (b->length < size) {
1536 void *mem = isc_mem_get(mctx, size);
1537 if (mem == NULL)
1538 return (ISC_R_NOMEMORY);
1539 if (b->base != NULL)
1540 isc_mem_put(mctx, b->base, b->length);
1541 b->base = mem;
1542 b->length = size;
1544 isc_buffer_clear(b);
1545 return (ISC_R_SUCCESS);
1548 isc_result_t
1549 dns_journal_iter_init(dns_journal_t *j,
1550 isc_uint32_t begin_serial, isc_uint32_t end_serial)
1552 isc_result_t result;
1554 CHECK(journal_find(j, begin_serial, &j->it.bpos));
1555 INSIST(j->it.bpos.serial == begin_serial);
1557 CHECK(journal_find(j, end_serial, &j->it.epos));
1558 INSIST(j->it.epos.serial == end_serial);
1560 result = ISC_R_SUCCESS;
1561 failure:
1562 j->it.result = result;
1563 return (j->it.result);
1567 isc_result_t
1568 dns_journal_first_rr(dns_journal_t *j) {
1569 isc_result_t result;
1572 * Seek to the beginning of the first transaction we are
1573 * interested in.
1575 CHECK(journal_seek(j, j->it.bpos.offset));
1576 j->it.current_serial = j->it.bpos.serial;
1578 j->it.xsize = 0; /* We have no transaction data yet... */
1579 j->it.xpos = 0; /* ...and haven't used any of it. */
1581 return (read_one_rr(j));
1583 failure:
1584 return (result);
1587 static isc_result_t
1588 read_one_rr(dns_journal_t *j) {
1589 isc_result_t result;
1591 dns_rdatatype_t rdtype;
1592 dns_rdataclass_t rdclass;
1593 unsigned int rdlen;
1594 isc_uint32_t ttl;
1595 journal_xhdr_t xhdr;
1596 journal_rrhdr_t rrhdr;
1598 INSIST(j->offset <= j->it.epos.offset);
1599 if (j->offset == j->it.epos.offset)
1600 return (ISC_R_NOMORE);
1601 if (j->it.xpos == j->it.xsize) {
1603 * We are at a transaction boundary.
1604 * Read another transaction header.
1606 CHECK(journal_read_xhdr(j, &xhdr));
1607 if (xhdr.size == 0) {
1608 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1609 "%s: journal corrupt: empty transaction",
1610 j->filename);
1611 FAIL(ISC_R_UNEXPECTED);
1613 if (xhdr.serial0 != j->it.current_serial) {
1614 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1615 "%s: journal file corrupt: "
1616 "expected serial %u, got %u",
1617 j->filename,
1618 j->it.current_serial, xhdr.serial0);
1619 FAIL(ISC_R_UNEXPECTED);
1621 j->it.xsize = xhdr.size;
1622 j->it.xpos = 0;
1625 * Read an RR.
1627 CHECK(journal_read_rrhdr(j, &rrhdr));
1629 * Perform a sanity check on the journal RR size.
1630 * The smallest possible RR has a 1-byte owner name
1631 * and a 10-byte header. The largest possible
1632 * RR has 65535 bytes of data, a header, and a maximum-
1633 * size owner name, well below 70 k total.
1635 if (rrhdr.size < 1+10 || rrhdr.size > 70000) {
1636 isc_log_write(JOURNAL_COMMON_LOGARGS, ISC_LOG_ERROR,
1637 "%s: journal corrupt: impossible RR size "
1638 "(%d bytes)", j->filename, rrhdr.size);
1639 FAIL(ISC_R_UNEXPECTED);
1642 CHECK(size_buffer(j->mctx, &j->it.source, rrhdr.size));
1643 CHECK(journal_read(j, j->it.source.base, rrhdr.size));
1644 isc_buffer_add(&j->it.source, rrhdr.size);
1647 * The target buffer is made the same size
1648 * as the source buffer, with the assumption that when
1649 * no compression in present, the output of dns_*_fromwire()
1650 * is no larger than the input.
1652 CHECK(size_buffer(j->mctx, &j->it.target, rrhdr.size));
1655 * Parse the owner name. We don't know where it
1656 * ends yet, so we make the entire "remaining"
1657 * part of the buffer "active".
1659 isc_buffer_setactive(&j->it.source,
1660 j->it.source.used - j->it.source.current);
1661 CHECK(dns_name_fromwire(&j->it.name, &j->it.source,
1662 &j->it.dctx, 0, &j->it.target));
1665 * Check that the RR header is there, and parse it.
1667 if (isc_buffer_remaininglength(&j->it.source) < 10)
1668 FAIL(DNS_R_FORMERR);
1670 rdtype = isc_buffer_getuint16(&j->it.source);
1671 rdclass = isc_buffer_getuint16(&j->it.source);
1672 ttl = isc_buffer_getuint32(&j->it.source);
1673 rdlen = isc_buffer_getuint16(&j->it.source);
1676 * Parse the rdata.
1678 if (isc_buffer_remaininglength(&j->it.source) != rdlen)
1679 FAIL(DNS_R_FORMERR);
1680 isc_buffer_setactive(&j->it.source, rdlen);
1681 dns_rdata_reset(&j->it.rdata);
1682 CHECK(dns_rdata_fromwire(&j->it.rdata, rdclass,
1683 rdtype, &j->it.source, &j->it.dctx,
1684 0, &j->it.target));
1685 j->it.ttl = ttl;
1687 j->it.xpos += sizeof(journal_rawrrhdr_t) + rrhdr.size;
1688 if (rdtype == dns_rdatatype_soa) {
1689 /* XXX could do additional consistency checks here */
1690 j->it.current_serial = dns_soa_getserial(&j->it.rdata);
1693 result = ISC_R_SUCCESS;
1695 failure:
1696 j->it.result = result;
1697 return (result);
1700 isc_result_t
1701 dns_journal_next_rr(dns_journal_t *j) {
1702 j->it.result = read_one_rr(j);
1703 return (j->it.result);
1706 void
1707 dns_journal_current_rr(dns_journal_t *j, dns_name_t **name, isc_uint32_t *ttl,
1708 dns_rdata_t **rdata)
1710 REQUIRE(j->it.result == ISC_R_SUCCESS);
1711 *name = &j->it.name;
1712 *ttl = j->it.ttl;
1713 *rdata = &j->it.rdata;
1716 /**************************************************************************/
1718 * Generating diffs from databases
1722 * Construct a diff containing all the RRs at the current name of the
1723 * database iterator 'dbit' in database 'db', version 'ver'.
1724 * Set '*name' to the current name, and append the diff to 'diff'.
1725 * All new tuples will have the operation 'op'.
1727 * Requires: 'name' must have buffer large enough to hold the name.
1728 * Typically, a dns_fixedname_t would be used.
1730 static isc_result_t
1731 get_name_diff(dns_db_t *db, dns_dbversion_t *ver, isc_stdtime_t now,
1732 dns_dbiterator_t *dbit, dns_name_t *name, dns_diffop_t op,
1733 dns_diff_t *diff)
1735 isc_result_t result;
1736 dns_dbnode_t *node = NULL;
1737 dns_rdatasetiter_t *rdsiter = NULL;
1738 dns_difftuple_t *tuple = NULL;
1740 result = dns_dbiterator_current(dbit, &node, name);
1741 if (result != ISC_R_SUCCESS)
1742 return (result);
1744 result = dns_db_allrdatasets(db, node, ver, now, &rdsiter);
1745 if (result != ISC_R_SUCCESS)
1746 goto cleanup_node;
1748 for (result = dns_rdatasetiter_first(rdsiter);
1749 result == ISC_R_SUCCESS;
1750 result = dns_rdatasetiter_next(rdsiter))
1752 dns_rdataset_t rdataset;
1754 dns_rdataset_init(&rdataset);
1755 dns_rdatasetiter_current(rdsiter, &rdataset);
1757 for (result = dns_rdataset_first(&rdataset);
1758 result == ISC_R_SUCCESS;
1759 result = dns_rdataset_next(&rdataset))
1761 dns_rdata_t rdata = DNS_RDATA_INIT;
1762 dns_rdataset_current(&rdataset, &rdata);
1763 result = dns_difftuple_create(diff->mctx, op, name,
1764 rdataset.ttl, &rdata,
1765 &tuple);
1766 if (result != ISC_R_SUCCESS) {
1767 dns_rdataset_disassociate(&rdataset);
1768 goto cleanup_iterator;
1770 dns_diff_append(diff, &tuple);
1772 dns_rdataset_disassociate(&rdataset);
1773 if (result != ISC_R_NOMORE)
1774 goto cleanup_iterator;
1776 if (result != ISC_R_NOMORE)
1777 goto cleanup_iterator;
1779 result = ISC_R_SUCCESS;
1781 cleanup_iterator:
1782 dns_rdatasetiter_destroy(&rdsiter);
1784 cleanup_node:
1785 dns_db_detachnode(db, &node);
1787 return (result);
1791 * Comparison function for use by dns_diff_subtract when sorting
1792 * the diffs to be subtracted. The sort keys are the rdata type
1793 * and the rdata itself. The owner name is ignored, because
1794 * it is known to be the same for all tuples.
1796 static int
1797 rdata_order(const void *av, const void *bv) {
1798 dns_difftuple_t const * const *ap = av;
1799 dns_difftuple_t const * const *bp = bv;
1800 dns_difftuple_t const *a = *ap;
1801 dns_difftuple_t const *b = *bp;
1802 int r;
1803 r = (b->rdata.type - a->rdata.type);
1804 if (r != 0)
1805 return (r);
1806 r = dns_rdata_compare(&a->rdata, &b->rdata);
1807 return (r);
1810 static isc_result_t
1811 dns_diff_subtract(dns_diff_t diff[2], dns_diff_t *r) {
1812 isc_result_t result;
1813 dns_difftuple_t *p[2];
1814 int i, t;
1815 isc_boolean_t append;
1817 CHECK(dns_diff_sort(&diff[0], rdata_order));
1818 CHECK(dns_diff_sort(&diff[1], rdata_order));
1820 for (;;) {
1821 p[0] = ISC_LIST_HEAD(diff[0].tuples);
1822 p[1] = ISC_LIST_HEAD(diff[1].tuples);
1823 if (p[0] == NULL && p[1] == NULL)
1824 break;
1826 for (i = 0; i < 2; i++)
1827 if (p[!i] == NULL) {
1828 ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
1829 ISC_LIST_APPEND(r->tuples, p[i], link);
1830 goto next;
1832 t = rdata_order(&p[0], &p[1]);
1833 if (t < 0) {
1834 ISC_LIST_UNLINK(diff[0].tuples, p[0], link);
1835 ISC_LIST_APPEND(r->tuples, p[0], link);
1836 goto next;
1838 if (t > 0) {
1839 ISC_LIST_UNLINK(diff[1].tuples, p[1], link);
1840 ISC_LIST_APPEND(r->tuples, p[1], link);
1841 goto next;
1843 INSIST(t == 0);
1845 * Identical RRs in both databases; skip them both
1846 * if the ttl differs.
1848 append = ISC_TF(p[0]->ttl != p[1]->ttl);
1849 for (i = 0; i < 2; i++) {
1850 ISC_LIST_UNLINK(diff[i].tuples, p[i], link);
1851 if (append) {
1852 ISC_LIST_APPEND(r->tuples, p[i], link);
1853 } else {
1854 dns_difftuple_free(&p[i]);
1857 next: ;
1859 result = ISC_R_SUCCESS;
1860 failure:
1861 return (result);
1864 static isc_result_t
1865 diff_namespace(isc_mem_t *mctx,
1866 dns_db_t *dba, dns_dbversion_t *dbvera,
1867 dns_db_t *dbb, dns_dbversion_t *dbverb,
1868 unsigned int options, dns_diff_t *resultdiff)
1870 dns_db_t *db[2];
1871 dns_dbversion_t *ver[2];
1872 dns_dbiterator_t *dbit[2] = { NULL, NULL };
1873 isc_boolean_t have[2] = { ISC_FALSE, ISC_FALSE };
1874 dns_fixedname_t fixname[2];
1875 isc_result_t result, itresult[2];
1876 dns_diff_t diff[2];
1877 int i, t;
1879 db[0] = dba, db[1] = dbb;
1880 ver[0] = dbvera, ver[1] = dbverb;
1882 dns_diff_init(mctx, &diff[0]);
1883 dns_diff_init(mctx, &diff[1]);
1885 dns_fixedname_init(&fixname[0]);
1886 dns_fixedname_init(&fixname[1]);
1888 result = dns_db_createiterator(db[0], options, &dbit[0]);
1889 if (result != ISC_R_SUCCESS)
1890 return (result);
1891 result = dns_db_createiterator(db[1], options, &dbit[1]);
1892 if (result != ISC_R_SUCCESS)
1893 goto cleanup_iterator;
1895 itresult[0] = dns_dbiterator_first(dbit[0]);
1896 itresult[1] = dns_dbiterator_first(dbit[1]);
1898 for (;;) {
1899 for (i = 0; i < 2; i++) {
1900 if (! have[i] && itresult[i] == ISC_R_SUCCESS) {
1901 CHECK(get_name_diff(db[i], ver[i], 0, dbit[i],
1902 dns_fixedname_name(&fixname[i]),
1903 i == 0 ?
1904 DNS_DIFFOP_ADD :
1905 DNS_DIFFOP_DEL,
1906 &diff[i]));
1907 itresult[i] = dns_dbiterator_next(dbit[i]);
1908 have[i] = ISC_TRUE;
1912 if (! have[0] && ! have[1]) {
1913 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
1914 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
1915 break;
1918 for (i = 0; i < 2; i++) {
1919 if (! have[!i]) {
1920 ISC_LIST_APPENDLIST(resultdiff->tuples,
1921 diff[i].tuples, link);
1922 INSIST(ISC_LIST_EMPTY(diff[i].tuples));
1923 have[i] = ISC_FALSE;
1924 goto next;
1928 t = dns_name_compare(dns_fixedname_name(&fixname[0]),
1929 dns_fixedname_name(&fixname[1]));
1930 if (t < 0) {
1931 ISC_LIST_APPENDLIST(resultdiff->tuples,
1932 diff[0].tuples, link);
1933 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
1934 have[0] = ISC_FALSE;
1935 continue;
1937 if (t > 0) {
1938 ISC_LIST_APPENDLIST(resultdiff->tuples,
1939 diff[1].tuples, link);
1940 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
1941 have[1] = ISC_FALSE;
1942 continue;
1944 INSIST(t == 0);
1945 CHECK(dns_diff_subtract(diff, resultdiff));
1946 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
1947 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
1948 have[0] = have[1] = ISC_FALSE;
1949 next: ;
1951 if (itresult[0] != ISC_R_NOMORE)
1952 FAIL(itresult[0]);
1953 if (itresult[1] != ISC_R_NOMORE)
1954 FAIL(itresult[1]);
1956 INSIST(ISC_LIST_EMPTY(diff[0].tuples));
1957 INSIST(ISC_LIST_EMPTY(diff[1].tuples));
1959 failure:
1960 dns_dbiterator_destroy(&dbit[1]);
1961 cleanup_iterator:
1962 dns_dbiterator_destroy(&dbit[0]);
1963 return (result);
1967 * Compare the databases 'dba' and 'dbb' and generate a journal
1968 * entry containing the changes to make 'dba' from 'dbb' (note
1969 * the order). This journal entry will consist of a single,
1970 * possibly very large transaction.
1972 isc_result_t
1973 dns_db_diff(isc_mem_t *mctx,
1974 dns_db_t *dba, dns_dbversion_t *dbvera,
1975 dns_db_t *dbb, dns_dbversion_t *dbverb,
1976 const char *journal_filename)
1978 isc_result_t result;
1979 dns_journal_t *journal = NULL;
1980 dns_diff_t resultdiff;
1982 result = dns_journal_open(mctx, journal_filename, ISC_TRUE, &journal);
1983 if (result != ISC_R_SUCCESS)
1984 return (result);
1986 dns_diff_init(mctx, &resultdiff);
1988 CHECK(diff_namespace(mctx, dba, dbvera, dbb, dbverb,
1989 DNS_DB_NONSEC3, &resultdiff));
1990 CHECK(diff_namespace(mctx, dba, dbvera, dbb, dbverb,
1991 DNS_DB_NSEC3ONLY, &resultdiff));
1992 if (ISC_LIST_EMPTY(resultdiff.tuples)) {
1993 isc_log_write(JOURNAL_DEBUG_LOGARGS(3), "no changes");
1994 } else {
1995 CHECK(dns_journal_write_transaction(journal, &resultdiff));
1997 failure:
1998 dns_diff_clear(&resultdiff);
1999 dns_journal_destroy(&journal);
2000 return (result);
2003 isc_result_t
2004 dns_journal_compact(isc_mem_t *mctx, char *filename, isc_uint32_t serial,
2005 isc_uint32_t target_size)
2007 unsigned int i;
2008 journal_pos_t best_guess;
2009 journal_pos_t current_pos;
2010 dns_journal_t *j = NULL;
2011 dns_journal_t *new = NULL;
2012 journal_rawheader_t rawheader;
2013 unsigned int copy_length;
2014 int namelen;
2015 char *buf = NULL;
2016 unsigned int size = 0;
2017 isc_result_t result;
2018 unsigned int indexend;
2019 char newname[1024];
2020 char backup[1024];
2021 isc_boolean_t is_backup = ISC_FALSE;
2023 namelen = strlen(filename);
2024 if (namelen > 4 && strcmp(filename + namelen - 4, ".jnl") == 0)
2025 namelen -= 4;
2027 result = isc_string_printf(newname, sizeof(newname), "%.*s.jnw",
2028 namelen, filename);
2029 if (result != ISC_R_SUCCESS)
2030 return (result);
2032 result = isc_string_printf(backup, sizeof(backup), "%.*s.jbk",
2033 namelen, filename);
2034 if (result != ISC_R_SUCCESS)
2035 return (result);
2037 result = journal_open(mctx, filename, ISC_FALSE, ISC_FALSE, &j);
2038 if (result == ISC_R_NOTFOUND) {
2039 is_backup = ISC_TRUE;
2040 result = journal_open(mctx, backup, ISC_FALSE, ISC_FALSE, &j);
2042 if (result != ISC_R_SUCCESS)
2043 return (result);
2045 if (JOURNAL_EMPTY(&j->header)) {
2046 dns_journal_destroy(&j);
2047 return (ISC_R_SUCCESS);
2050 if (DNS_SERIAL_GT(j->header.begin.serial, serial) ||
2051 DNS_SERIAL_GT(serial, j->header.end.serial)) {
2052 dns_journal_destroy(&j);
2053 return (ISC_R_RANGE);
2057 * Cope with very small target sizes.
2059 indexend = sizeof(journal_rawheader_t) +
2060 j->header.index_size * sizeof(journal_rawpos_t);
2061 if (target_size < indexend * 2)
2062 target_size = target_size/2 + indexend;
2065 * See if there is any work to do.
2067 if ((isc_uint32_t) j->header.end.offset < target_size) {
2068 dns_journal_destroy(&j);
2069 return (ISC_R_SUCCESS);
2072 CHECK(journal_open(mctx, newname, ISC_TRUE, ISC_TRUE, &new));
2075 * Remove overhead so space test below can succeed.
2077 if (target_size >= indexend)
2078 target_size -= indexend;
2081 * Find if we can create enough free space.
2083 best_guess = j->header.begin;
2084 for (i = 0; i < j->header.index_size; i++) {
2085 if (POS_VALID(j->index[i]) &&
2086 DNS_SERIAL_GE(serial, j->index[i].serial) &&
2087 ((isc_uint32_t)(j->header.end.offset - j->index[i].offset)
2088 >= target_size / 2) &&
2089 j->index[i].offset > best_guess.offset)
2090 best_guess = j->index[i];
2093 current_pos = best_guess;
2094 while (current_pos.serial != serial) {
2095 CHECK(journal_next(j, &current_pos));
2096 if (current_pos.serial == j->header.end.serial)
2097 break;
2099 if (DNS_SERIAL_GE(serial, current_pos.serial) &&
2100 ((isc_uint32_t)(j->header.end.offset - current_pos.offset)
2101 >= (target_size / 2)) &&
2102 current_pos.offset > best_guess.offset)
2103 best_guess = current_pos;
2104 else
2105 break;
2108 INSIST(best_guess.serial != j->header.end.serial);
2109 if (best_guess.serial != serial)
2110 CHECK(journal_next(j, &best_guess));
2113 * We should now be roughly half target_size provided
2114 * we did not reach 'serial'. If not we will just copy
2115 * all uncommitted deltas regardless of the size.
2117 copy_length = j->header.end.offset - best_guess.offset;
2119 if (copy_length != 0) {
2121 * Copy best_guess to end into space just freed.
2123 size = 64*1024;
2124 if (copy_length < size)
2125 size = copy_length;
2126 buf = isc_mem_get(mctx, size);
2127 if (buf == NULL) {
2128 result = ISC_R_NOMEMORY;
2129 goto failure;
2132 CHECK(journal_seek(j, best_guess.offset));
2133 CHECK(journal_seek(new, indexend));
2134 for (i = 0; i < copy_length; i += size) {
2135 unsigned int len = (copy_length - i) > size ? size :
2136 (copy_length - i);
2137 CHECK(journal_read(j, buf, len));
2138 CHECK(journal_write(new, buf, len));
2141 CHECK(journal_fsync(new));
2144 * Compute new header.
2146 new->header.begin.serial = best_guess.serial;
2147 new->header.begin.offset = indexend;
2148 new->header.end.serial = j->header.end.serial;
2149 new->header.end.offset = indexend + copy_length;
2152 * Update the journal header.
2154 journal_header_encode(&new->header, &rawheader);
2155 CHECK(journal_seek(new, 0));
2156 CHECK(journal_write(new, &rawheader, sizeof(rawheader)));
2157 CHECK(journal_fsync(new));
2160 * Build new index.
2162 current_pos = new->header.begin;
2163 while (current_pos.serial != new->header.end.serial) {
2164 index_add(new, &current_pos);
2165 CHECK(journal_next(new, &current_pos));
2169 * Write index.
2171 CHECK(index_to_disk(new));
2172 CHECK(journal_fsync(new));
2174 indexend = new->header.end.offset;
2176 dns_journal_destroy(&new);
2179 * With a UFS file system this should just succeed and be atomic.
2180 * Any IXFR outs will just continue and the old journal will be
2181 * removed on final close.
2183 * With MSDOS / NTFS we need to do a two stage rename triggered
2184 * bu EEXISTS. Hopefully all IXFR's that were active at the last
2185 * rename are now complete.
2187 if (rename(newname, filename) == -1) {
2188 if (errno == EACCES && !is_backup) {
2189 result = isc_file_remove(backup);
2190 if (result != ISC_R_SUCCESS &&
2191 result != ISC_R_FILENOTFOUND)
2192 goto failure;
2193 if (rename(filename, backup) == -1)
2194 goto maperrno;
2195 if (rename(newname, filename) == -1)
2196 goto maperrno;
2197 (void)isc_file_remove(backup);
2198 } else {
2199 maperrno:
2200 result = ISC_R_FAILURE;
2201 goto failure;
2205 dns_journal_destroy(&j);
2206 result = ISC_R_SUCCESS;
2208 failure:
2209 (void)isc_file_remove(newname);
2210 if (buf != NULL)
2211 isc_mem_put(mctx, buf, size);
2212 if (j != NULL)
2213 dns_journal_destroy(&j);
2214 if (new != NULL)
2215 dns_journal_destroy(&new);
2216 return (result);
2219 static isc_result_t
2220 index_to_disk(dns_journal_t *j) {
2221 isc_result_t result = ISC_R_SUCCESS;
2223 if (j->header.index_size != 0) {
2224 unsigned int i;
2225 unsigned char *p;
2226 unsigned int rawbytes;
2228 rawbytes = j->header.index_size * sizeof(journal_rawpos_t);
2230 p = j->rawindex;
2231 for (i = 0; i < j->header.index_size; i++) {
2232 encode_uint32(j->index[i].serial, p);
2233 p += 4;
2234 encode_uint32(j->index[i].offset, p);
2235 p += 4;
2237 INSIST(p == j->rawindex + rawbytes);
2239 CHECK(journal_seek(j, sizeof(journal_rawheader_t)));
2240 CHECK(journal_write(j, j->rawindex, rawbytes));
2242 failure:
2243 return (result);