2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.
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.
18 /* $Id: zt.c,v 1.38.18.5 2005/11/30 03:44:39 marka Exp $ */
25 #include <isc/magic.h>
27 #include <isc/string.h>
33 #include <dns/rdataclass.h>
34 #include <dns/result.h>
43 dns_rdataclass_t rdclass;
46 isc_uint32_t references;
50 #define ZTMAGIC ISC_MAGIC('Z', 'T', 'b', 'l')
51 #define VALID_ZT(zt) ISC_MAGIC_VALID(zt, ZTMAGIC)
54 auto_detach(void *, void *);
57 load(dns_zone_t *zone, void *uap);
60 loadnew(dns_zone_t *zone, void *uap);
63 freezezones(dns_zone_t *zone, void *uap);
66 dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **ztp) {
70 REQUIRE(ztp != NULL && *ztp == NULL);
72 zt = isc_mem_get(mctx, sizeof(*zt));
74 return (ISC_R_NOMEMORY);
77 result = dns_rbt_create(mctx, auto_detach, zt, &zt->table);
78 if (result != ISC_R_SUCCESS)
81 result = isc_rwlock_init(&zt->rwlock, 0, 0);
82 if (result != ISC_R_SUCCESS)
87 zt->rdclass = rdclass;
91 return (ISC_R_SUCCESS);
94 dns_rbt_destroy(&zt->table);
97 isc_mem_put(mctx, zt, sizeof(*zt));
103 dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
105 dns_zone_t *dummy = NULL;
108 REQUIRE(VALID_ZT(zt));
110 name = dns_zone_getorigin(zone);
112 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
114 result = dns_rbt_addname(zt->table, name, zone);
115 if (result == ISC_R_SUCCESS)
116 dns_zone_attach(zone, &dummy);
118 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
124 dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
128 REQUIRE(VALID_ZT(zt));
130 name = dns_zone_getorigin(zone);
132 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
134 result = dns_rbt_deletename(zt->table, name, ISC_FALSE);
136 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
142 dns_zt_find(dns_zt_t *zt, dns_name_t *name, unsigned int options,
143 dns_name_t *foundname, dns_zone_t **zonep)
146 dns_zone_t *dummy = NULL;
147 unsigned int rbtoptions = 0;
149 REQUIRE(VALID_ZT(zt));
151 if ((options & DNS_ZTFIND_NOEXACT) != 0)
152 rbtoptions |= DNS_RBTFIND_NOEXACT;
154 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
156 result = dns_rbt_findname(zt->table, name, rbtoptions, foundname,
157 (void **) (void*)&dummy);
158 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
159 dns_zone_attach(dummy, zonep);
161 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
167 dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {
169 REQUIRE(VALID_ZT(zt));
170 REQUIRE(ztp != NULL && *ztp == NULL);
172 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
174 INSIST(zt->references > 0);
176 INSIST(zt->references != 0);
178 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
184 flush(dns_zone_t *zone, void *uap) {
186 return (dns_zone_flush(zone));
190 zt_flushanddetach(dns_zt_t **ztp, isc_boolean_t need_flush) {
191 isc_boolean_t destroy = ISC_FALSE;
194 REQUIRE(ztp != NULL && VALID_ZT(*ztp));
198 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
200 INSIST(zt->references > 0);
202 if (zt->references == 0)
205 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
209 (void)dns_zt_apply(zt, ISC_FALSE, flush, NULL);
210 dns_rbt_destroy(&zt->table);
211 isc_rwlock_destroy(&zt->rwlock);
213 isc_mem_put(zt->mctx, zt, sizeof(*zt));
220 dns_zt_flushanddetach(dns_zt_t **ztp) {
221 zt_flushanddetach(ztp, ISC_TRUE);
225 dns_zt_detach(dns_zt_t **ztp) {
226 zt_flushanddetach(ztp, ISC_FALSE);
230 dns_zt_load(dns_zt_t *zt, isc_boolean_t stop) {
233 REQUIRE(VALID_ZT(zt));
235 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
236 result = dns_zt_apply(zt, stop, load, NULL);
237 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
242 load(dns_zone_t *zone, void *uap) {
245 result = dns_zone_load(zone);
246 if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE)
247 result = ISC_R_SUCCESS;
252 dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop) {
255 REQUIRE(VALID_ZT(zt));
257 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
258 result = dns_zt_apply(zt, stop, loadnew, NULL);
259 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
264 loadnew(dns_zone_t *zone, void *uap) {
267 result = dns_zone_loadnew(zone);
268 if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE ||
269 result == DNS_R_DYNAMIC)
270 result = ISC_R_SUCCESS;
275 dns_zt_freezezones(dns_zt_t *zt, isc_boolean_t freeze) {
276 isc_result_t result, tresult;
278 REQUIRE(VALID_ZT(zt));
280 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
281 result = dns_zt_apply2(zt, ISC_FALSE, &tresult, freezezones, &freeze);
282 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
283 return ((result == ISC_R_SUCCESS) ? tresult : result);
287 freezezones(dns_zone_t *zone, void *uap) {
288 isc_boolean_t freeze = *(isc_boolean_t *)uap;
289 isc_boolean_t frozen;
290 isc_result_t result = ISC_R_SUCCESS;
291 char classstr[DNS_RDATACLASS_FORMATSIZE];
292 char zonename[DNS_NAME_FORMATSIZE];
299 if (dns_zone_gettype(zone) != dns_zone_master)
300 return (ISC_R_SUCCESS);
302 frozen = dns_zone_getupdatedisabled(zone);
305 result = DNS_R_FROZEN;
306 if (result == ISC_R_SUCCESS)
307 result = dns_zone_flush(zone);
308 if (result == ISC_R_SUCCESS) {
309 journal = dns_zone_getjournal(zone);
311 (void)isc_file_remove(journal);
315 result = dns_zone_load(zone);
316 if (result == DNS_R_CONTINUE ||
317 result == DNS_R_UPTODATE)
318 result = ISC_R_SUCCESS;
321 if (result == ISC_R_SUCCESS)
322 dns_zone_setupdatedisabled(zone, freeze);
323 view = dns_zone_getview(zone);
324 if (strcmp(view->name, "_bind") == 0 ||
325 strcmp(view->name, "_default") == 0)
333 dns_rdataclass_format(dns_zone_getclass(zone), classstr,
335 dns_name_format(dns_zone_getorigin(zone), zonename, sizeof(zonename));
336 level = (result != ISC_R_SUCCESS) ? ISC_LOG_ERROR : ISC_LOG_DEBUG(1);
337 isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE,
338 level, "%s zone '%s/%s'%s%s: %s",
339 freeze ? "freezing" : "thawing",
340 zonename, classstr, sep, vname,
341 isc_result_totext(result));
346 dns_zt_apply(dns_zt_t *zt, isc_boolean_t stop,
347 isc_result_t (*action)(dns_zone_t *, void *), void *uap)
349 return (dns_zt_apply2(zt, stop, NULL, action, uap));
353 dns_zt_apply2(dns_zt_t *zt, isc_boolean_t stop, isc_result_t *sub,
354 isc_result_t (*action)(dns_zone_t *, void *), void *uap)
357 dns_rbtnodechain_t chain;
358 isc_result_t result, tresult = ISC_R_SUCCESS;
361 REQUIRE(VALID_ZT(zt));
362 REQUIRE(action != NULL);
364 dns_rbtnodechain_init(&chain, zt->mctx);
365 result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
366 if (result == ISC_R_NOTFOUND) {
370 result = ISC_R_NOMORE;
372 while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
373 result = dns_rbtnodechain_current(&chain, NULL, NULL,
375 if (result == ISC_R_SUCCESS) {
378 result = (action)(zone, uap);
379 if (result != ISC_R_SUCCESS && stop) {
381 goto cleanup; /* don't break */
382 } else if (result != ISC_R_SUCCESS &&
383 tresult == ISC_R_SUCCESS)
386 result = dns_rbtnodechain_next(&chain, NULL, NULL);
388 if (result == ISC_R_NOMORE)
389 result = ISC_R_SUCCESS;
392 dns_rbtnodechain_invalidate(&chain);
404 auto_detach(void *data, void *arg) {
405 dns_zone_t *zone = data;
409 dns_zone_detach(&zone);