1/*
2 * Copyright (C) 2004-2009, 2012  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id$ */
19
20#ifndef DNS_JOURNAL_H
21#define DNS_JOURNAL_H 1
22
23/*****
24 ***** Module Info
25 *****/
26
27/*! \file dns/journal.h
28 * \brief
29 * Database journaling.
30 */
31
32/***
33 *** Imports
34 ***/
35
36#include <isc/lang.h>
37#include <isc/magic.h>
38
39#include <dns/name.h>
40#include <dns/diff.h>
41#include <dns/rdata.h>
42#include <dns/types.h>
43
44/***
45 *** Defines.
46 ***/
47#define DNS_JOURNALOPT_RESIGN	0x00000001
48
49/***
50 *** Types
51 ***/
52
53/*%
54 * A dns_journal_t represents an open journal file.  This is an opaque type.
55 *
56 * A particular dns_journal_t object may be opened for writing, in which case
57 * it can be used for writing transactions to a journal file, or it can be
58 * opened for reading, in which case it can be used for reading transactions
59 * from (iterating over) a journal file.  A single dns_journal_t object may
60 * not be used for both purposes.
61 */
62typedef struct dns_journal dns_journal_t;
63
64
65/***
66 *** Functions
67 ***/
68
69ISC_LANG_BEGINDECLS
70
71/**************************************************************************/
72
73isc_result_t
74dns_db_createsoatuple(dns_db_t *db, dns_dbversion_t *ver, isc_mem_t *mctx,
75		   dns_diffop_t op, dns_difftuple_t **tp);
76/*!< brief
77 * Create a diff tuple for the current database SOA.
78 * XXX this probably belongs somewhere else.
79 */
80
81
82/*@{*/
83#define DNS_SERIAL_GT(a, b) ((int)(((a) - (b)) & 0xFFFFFFFF) > 0)
84#define DNS_SERIAL_GE(a, b) ((int)(((a) - (b)) & 0xFFFFFFFF) >= 0)
85/*!< brief
86 * Compare SOA serial numbers.  DNS_SERIAL_GT(a, b) returns true iff
87 * a is "greater than" b where "greater than" is as defined in RFC1982.
88 * DNS_SERIAL_GE(a, b) returns true iff a is "greater than or equal to" b.
89 */
90/*@}*/
91
92/**************************************************************************/
93/*
94 * Journal object creation and destruction.
95 */
96
97isc_result_t
98dns_journal_open(isc_mem_t *mctx, const char *filename, isc_boolean_t write,
99		 dns_journal_t **journalp);
100/*%<
101 * Open the journal file 'filename' and create a dns_journal_t object for it.
102 *
103 * If 'write' is ISC_TRUE, the journal is open for writing.  If it does
104 * not exist, it is created.
105 *
106 * If 'write' is ISC_FALSE, the journal is open for reading.  If it does
107 * not exist, ISC_R_NOTFOUND is returned.
108 */
109
110void
111dns_journal_destroy(dns_journal_t **journalp);
112/*%<
113 * Destroy a dns_journal_t, closing any open files and freeing its memory.
114 */
115
116/**************************************************************************/
117/*
118 * Writing transactions to journals.
119 */
120
121isc_result_t
122dns_journal_begin_transaction(dns_journal_t *j);
123/*%<
124 * Prepare to write a new transaction to the open journal file 'j'.
125 *
126 * Requires:
127 *     \li 'j' is open for writing.
128 */
129
130isc_result_t
131dns_journal_writediff(dns_journal_t *j, dns_diff_t *diff);
132/*%<
133 * Write 'diff' to the current transaction of journal file 'j'.
134 *
135 * Requires:
136 * \li     'j' is open for writing and dns_journal_begin_transaction()
137 * 	has been called.
138 *
139 *\li 	'diff' is a full or partial, correctly ordered IXFR
140 *      difference sequence.
141 */
142
143isc_result_t
144dns_journal_commit(dns_journal_t *j);
145/*%<
146 * Commit the current transaction of journal file 'j'.
147 *
148 * Requires:
149 * \li     'j' is open for writing and dns_journal_begin_transaction()
150 * 	has been called.
151 *
152 *   \li   dns_journal_writediff() has been called one or more times
153 * 	to form a complete, correctly ordered IXFR difference
154 *      sequence.
155 */
156
157isc_result_t
158dns_journal_write_transaction(dns_journal_t *j, dns_diff_t *diff);
159/*%
160 * Write a complete transaction at once to a journal file,
161 * sorting it if necessary, and commit it.  Equivalent to calling
162 * dns_diff_sort(), dns_journal_begin_transaction(),
163 * dns_journal_writediff(), and dns_journal_commit().
164 *
165 * Requires:
166 *\li      'j' is open for writing.
167 *
168 * \li	'diff' contains exactly one SOA deletion, one SOA addition
169 *       with a greater serial number, and possibly other changes,
170 *       in arbitrary order.
171 */
172
173/**************************************************************************/
174/*
175 * Reading transactions from journals.
176 */
177
178isc_uint32_t
179dns_journal_first_serial(dns_journal_t *j);
180isc_uint32_t
181dns_journal_last_serial(dns_journal_t *j);
182/*%<
183 * Get the first and last addressable serial number in the journal.
184 */
185
186isc_result_t
187dns_journal_iter_init(dns_journal_t *j,
188		      isc_uint32_t begin_serial, isc_uint32_t end_serial);
189/*%<
190 * Prepare to iterate over the transactions that will bring the database
191 * from SOA serial number 'begin_serial' to 'end_serial'.
192 *
193 * Returns:
194 *\li	ISC_R_SUCCESS
195 *\li	ISC_R_RANGE	begin_serial is outside the addressable range.
196 *\li	ISC_R_NOTFOUND	begin_serial is within the range of addressable
197 *			serial numbers covered by the journal, but
198 *			this particular serial number does not exist.
199 */
200
201/*@{*/
202isc_result_t
203dns_journal_first_rr(dns_journal_t *j);
204isc_result_t
205dns_journal_next_rr(dns_journal_t *j);
206/*%<
207 * Position the iterator at the first/next RR in a journal
208 * transaction sequence established using dns_journal_iter_init().
209 *
210 * Requires:
211 *    \li  dns_journal_iter_init() has been called.
212 *
213 */
214/*@}*/
215
216void
217dns_journal_current_rr(dns_journal_t *j, dns_name_t **name, isc_uint32_t *ttl,
218		       dns_rdata_t **rdata);
219/*%<
220 * Get the name, ttl, and rdata of the current journal RR.
221 *
222 * Requires:
223 * \li     The last call to dns_journal_first_rr() or dns_journal_next_rr()
224 *      returned ISC_R_SUCCESS.
225 */
226
227/**************************************************************************/
228/*
229 * Database roll-forward.
230 */
231
232isc_result_t
233dns_journal_rollforward(isc_mem_t *mctx, dns_db_t *db, unsigned int options,
234			const char *filename);
235
236isc_result_t
237dns_journal_rollforward2(isc_mem_t *mctx, dns_db_t *db, unsigned int options,
238			 isc_uint32_t resign, const char *filename);
239/*%<
240 * Roll forward (play back) the journal file "filename" into the
241 * database "db".  This should be called when the server starts
242 * after a shutdown or crash.  'resign' is how many seconds before
243 * a RRSIG is due to expire it should be scheduled to be regenerated.
244 *
245 * Requires:
246 *\li	dns_journal_rollforward() requires that DNS_JOURNALOPT_RESIGN
247 *	is not set.
248 *\li   'mctx' is a valid memory context.
249 *\li	'db' is a valid database which does not have a version
250 *           open for writing.
251 *\li   'filename' is the name of the journal file belonging to 'db'.
252 *
253 * Returns:
254 *\li	DNS_R_NOJOURNAL when journal does not exist.
255 *\li	ISC_R_NOTFOUND when current serial in not in journal.
256 *\li	ISC_R_RANGE when current serial in not in journals range.
257 *\li	ISC_R_SUCCESS journal has been applied successfully to database.
258 *	others
259 */
260
261isc_result_t
262dns_journal_print(isc_mem_t *mctx, const char *filename, FILE *file);
263/* For debugging not general use */
264
265isc_result_t
266dns_db_diff(isc_mem_t *mctx,
267	    dns_db_t *dba, dns_dbversion_t *dbvera,
268	    dns_db_t *dbb, dns_dbversion_t *dbverb,
269	    const char *journal_filename);
270/*%<
271 * Compare the databases 'dba' and 'dbb' and generate a journal
272 * entry containing the changes to make 'dba' from 'dbb' (note
273 * the order).  This journal entry will consist of a single,
274 * possibly very large transaction.  Append the journal
275 * entry to the journal file specified by 'journal_filename'.
276 */
277
278isc_result_t
279dns_journal_compact(isc_mem_t *mctx, char *filename, isc_uint32_t serial,
280		    isc_uint32_t target_size);
281/*%<
282 * Attempt to compact the journal if it is greater that 'target_size'.
283 * Changes from 'serial' onwards will be preserved.  If the journal
284 * exists and is non-empty 'serial' must exist in the journal.
285 */
286
287ISC_LANG_ENDDECLS
288
289#endif /* DNS_JOURNAL_H */
290