zt.c revision 135446
1/*
2 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002  Internet Software Consortium.
4 *
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.
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: zt.c,v 1.33.12.6 2004/03/08 21:06:28 marka Exp $ */
19
20#include <config.h>
21
22#include <isc/magic.h>
23#include <isc/mem.h>
24#include <isc/util.h>
25
26#include <dns/rbt.h>
27#include <dns/result.h>
28#include <dns/zone.h>
29#include <dns/zt.h>
30
31struct dns_zt {
32	/* Unlocked. */
33	unsigned int		magic;
34	isc_mem_t		*mctx;
35	dns_rdataclass_t	rdclass;
36	isc_rwlock_t		rwlock;
37	/* Locked by lock. */
38	isc_uint32_t		references;
39	dns_rbt_t		*table;
40};
41
42#define ZTMAGIC			ISC_MAGIC('Z', 'T', 'b', 'l')
43#define VALID_ZT(zt) 		ISC_MAGIC_VALID(zt, ZTMAGIC)
44
45static void
46auto_detach(void *, void *);
47
48static isc_result_t
49load(dns_zone_t *zone, void *uap);
50
51static isc_result_t
52loadnew(dns_zone_t *zone, void *uap);
53
54isc_result_t
55dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **ztp) {
56	dns_zt_t *zt;
57	isc_result_t result;
58
59	REQUIRE(ztp != NULL && *ztp == NULL);
60
61	zt = isc_mem_get(mctx, sizeof(*zt));
62	if (zt == NULL)
63		return (ISC_R_NOMEMORY);
64
65	zt->table = NULL;
66	result = dns_rbt_create(mctx, auto_detach, zt, &zt->table);
67	if (result != ISC_R_SUCCESS)
68		goto cleanup_zt;
69
70	result = isc_rwlock_init(&zt->rwlock, 0, 0);
71	if (result != ISC_R_SUCCESS) {
72		UNEXPECTED_ERROR(__FILE__, __LINE__,
73				 "isc_rwlock_init() failed: %s",
74				 isc_result_totext(result));
75		result = ISC_R_UNEXPECTED;
76		goto cleanup_rbt;
77	}
78
79	zt->mctx = mctx;
80	zt->references = 1;
81	zt->rdclass = rdclass;
82	zt->magic = ZTMAGIC;
83	*ztp = zt;
84
85	return (ISC_R_SUCCESS);
86
87   cleanup_rbt:
88	dns_rbt_destroy(&zt->table);
89
90   cleanup_zt:
91	isc_mem_put(mctx, zt, sizeof(*zt));
92
93	return (result);
94}
95
96isc_result_t
97dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
98	isc_result_t result;
99	dns_zone_t *dummy = NULL;
100	dns_name_t *name;
101
102	REQUIRE(VALID_ZT(zt));
103
104	name = dns_zone_getorigin(zone);
105
106	RWLOCK(&zt->rwlock, isc_rwlocktype_write);
107
108	result = dns_rbt_addname(zt->table, name, zone);
109	if (result == ISC_R_SUCCESS)
110		dns_zone_attach(zone, &dummy);
111
112	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
113
114	return (result);
115}
116
117isc_result_t
118dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
119	isc_result_t result;
120	dns_name_t *name;
121
122	REQUIRE(VALID_ZT(zt));
123
124	name = dns_zone_getorigin(zone);
125
126	RWLOCK(&zt->rwlock, isc_rwlocktype_write);
127
128	result = dns_rbt_deletename(zt->table, name, ISC_FALSE);
129
130	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
131
132	return (result);
133}
134
135isc_result_t
136dns_zt_find(dns_zt_t *zt, dns_name_t *name, unsigned int options,
137	    dns_name_t *foundname, dns_zone_t **zonep)
138{
139	isc_result_t result;
140	dns_zone_t *dummy = NULL;
141	unsigned int rbtoptions = 0;
142
143	REQUIRE(VALID_ZT(zt));
144
145	if ((options & DNS_ZTFIND_NOEXACT) != 0)
146		rbtoptions |= DNS_RBTFIND_NOEXACT;
147
148	RWLOCK(&zt->rwlock, isc_rwlocktype_read);
149
150	result = dns_rbt_findname(zt->table, name, rbtoptions, foundname,
151				  (void **) (void*)&dummy);
152	if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
153		dns_zone_attach(dummy, zonep);
154
155	RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
156
157	return (result);
158}
159
160void
161dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {
162
163	REQUIRE(VALID_ZT(zt));
164	REQUIRE(ztp != NULL && *ztp == NULL);
165
166	RWLOCK(&zt->rwlock, isc_rwlocktype_write);
167
168	INSIST(zt->references > 0);
169	zt->references++;
170	INSIST(zt->references != 0);
171
172	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
173
174	*ztp = zt;
175}
176
177static isc_result_t
178flush(dns_zone_t *zone, void *uap) {
179	UNUSED(uap);
180	return (dns_zone_flush(zone));
181}
182
183static void
184zt_flushanddetach(dns_zt_t **ztp, isc_boolean_t need_flush) {
185	isc_boolean_t destroy = ISC_FALSE;
186	dns_zt_t *zt;
187
188	REQUIRE(ztp != NULL && VALID_ZT(*ztp));
189
190	zt = *ztp;
191
192	RWLOCK(&zt->rwlock, isc_rwlocktype_write);
193
194	INSIST(zt->references > 0);
195	zt->references--;
196	if (zt->references == 0)
197		destroy = ISC_TRUE;
198
199	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
200
201	if (destroy) {
202		if (need_flush)
203			(void)dns_zt_apply(zt, ISC_FALSE, flush, NULL);
204		dns_rbt_destroy(&zt->table);
205		isc_rwlock_destroy(&zt->rwlock);
206		zt->magic = 0;
207		isc_mem_put(zt->mctx, zt, sizeof(*zt));
208	}
209
210	*ztp = NULL;
211}
212
213void
214dns_zt_flushanddetach(dns_zt_t **ztp) {
215	zt_flushanddetach(ztp, ISC_TRUE);
216}
217
218void
219dns_zt_detach(dns_zt_t **ztp) {
220	zt_flushanddetach(ztp, ISC_FALSE);
221}
222
223isc_result_t
224dns_zt_load(dns_zt_t *zt, isc_boolean_t stop) {
225	isc_result_t result;
226
227	REQUIRE(VALID_ZT(zt));
228
229	RWLOCK(&zt->rwlock, isc_rwlocktype_read);
230	result = dns_zt_apply(zt, stop, load, NULL);
231	RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
232	return (result);
233}
234
235static isc_result_t
236load(dns_zone_t *zone, void *uap) {
237	isc_result_t result;
238	UNUSED(uap);
239	result = dns_zone_load(zone);
240	if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE)
241		result = ISC_R_SUCCESS;
242	return (result);
243}
244
245isc_result_t
246dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop) {
247	isc_result_t result;
248
249	REQUIRE(VALID_ZT(zt));
250
251	RWLOCK(&zt->rwlock, isc_rwlocktype_read);
252	result = dns_zt_apply(zt, stop, loadnew, NULL);
253	RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
254	return (result);
255}
256
257static isc_result_t
258loadnew(dns_zone_t *zone, void *uap) {
259	isc_result_t result;
260	UNUSED(uap);
261	result = dns_zone_loadnew(zone);
262	if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE ||
263	    result == DNS_R_DYNAMIC)
264		result = ISC_R_SUCCESS;
265	return (result);
266}
267
268isc_result_t
269dns_zt_apply(dns_zt_t *zt, isc_boolean_t stop,
270	     isc_result_t (*action)(dns_zone_t *, void *), void *uap)
271{
272	dns_rbtnode_t *node;
273	dns_rbtnodechain_t chain;
274	isc_result_t result;
275	dns_zone_t *zone;
276
277	REQUIRE(VALID_ZT(zt));
278	REQUIRE(action != NULL);
279
280	dns_rbtnodechain_init(&chain, zt->mctx);
281	result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
282	if (result == ISC_R_NOTFOUND) {
283		/*
284		 * The tree is empty.
285		 */
286		result = ISC_R_NOMORE;
287	}
288	while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
289		result = dns_rbtnodechain_current(&chain, NULL, NULL,
290						  &node);
291		if (result == ISC_R_SUCCESS) {
292			zone = node->data;
293			if (zone != NULL)
294				result = (action)(zone, uap);
295			if (result != ISC_R_SUCCESS && stop)
296				goto cleanup;	/* don't break */
297		}
298		result = dns_rbtnodechain_next(&chain, NULL, NULL);
299	}
300	if (result == ISC_R_NOMORE)
301		result = ISC_R_SUCCESS;
302
303 cleanup:
304	dns_rbtnodechain_invalidate(&chain);
305
306	return (result);
307}
308
309/***
310 *** Private
311 ***/
312
313static void
314auto_detach(void *data, void *arg) {
315	dns_zone_t *zone = data;
316
317	UNUSED(arg);
318
319	dns_zone_detach(&zone);
320}
321