Merge from vendor branch NTPD:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / zt.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  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.2.3 2004/04/15 01:38:09 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
31 struct 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
45 static void
46 auto_detach(void *, void *);
47
48 static isc_result_t
49 load(dns_zone_t *zone, void *uap);
50
51 static isc_result_t
52 loadnew(dns_zone_t *zone, void *uap);
53
54 isc_result_t
55 dns_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
96 isc_result_t
97 dns_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
117 isc_result_t
118 dns_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
135 isc_result_t
136 dns_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
160 void
161 dns_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
177 static isc_result_t
178 flush(dns_zone_t *zone, void *uap) {
179         UNUSED(uap);
180         return (dns_zone_flush(zone));
181 }
182
183 static void
184 zt_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
213 void
214 dns_zt_flushanddetach(dns_zt_t **ztp) {
215         zt_flushanddetach(ztp, ISC_TRUE);
216 }
217
218 void
219 dns_zt_detach(dns_zt_t **ztp) {
220         zt_flushanddetach(ztp, ISC_FALSE);
221 }
222
223 isc_result_t
224 dns_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
235 static isc_result_t
236 load(dns_zone_t *zone, void *uap) {
237         UNUSED(uap);
238         return (dns_zone_load(zone));
239 }
240
241 isc_result_t
242 dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop) {
243         isc_result_t result;
244
245         REQUIRE(VALID_ZT(zt));
246
247         RWLOCK(&zt->rwlock, isc_rwlocktype_read);
248         result = dns_zt_apply(zt, stop, loadnew, NULL);
249         RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
250         return (result);
251 }
252
253 static isc_result_t
254 loadnew(dns_zone_t *zone, void *uap) {
255         isc_result_t result;
256         UNUSED(uap);
257         result = dns_zone_loadnew(zone);
258         if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE ||
259             result == DNS_R_DYNAMIC)
260                 result = ISC_R_SUCCESS;
261         return (result);
262 }
263
264 isc_result_t
265 dns_zt_apply(dns_zt_t *zt, isc_boolean_t stop,
266              isc_result_t (*action)(dns_zone_t *, void *), void *uap)
267 {
268         dns_rbtnode_t *node;
269         dns_rbtnodechain_t chain;
270         isc_result_t result;
271         dns_zone_t *zone;
272
273         REQUIRE(VALID_ZT(zt));
274         REQUIRE(action != NULL);
275
276         dns_rbtnodechain_init(&chain, zt->mctx);
277         result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
278         if (result == ISC_R_NOTFOUND) {
279                 /*
280                  * The tree is empty.
281                  */
282                 result = ISC_R_NOMORE;
283         }
284         while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
285                 result = dns_rbtnodechain_current(&chain, NULL, NULL,
286                                                   &node);
287                 if (result == ISC_R_SUCCESS) {
288                         zone = node->data;
289                         if (zone != NULL)
290                                 result = (action)(zone, uap);
291                         if (result != ISC_R_SUCCESS && stop)
292                                 goto cleanup;   /* don't break */
293                 }
294                 result = dns_rbtnodechain_next(&chain, NULL, NULL);
295         }
296         if (result == ISC_R_NOMORE)
297                 result = ISC_R_SUCCESS;
298
299  cleanup:
300         dns_rbtnodechain_invalidate(&chain);
301
302         return (result);
303 }
304
305 /***
306  *** Private
307  ***/
308
309 static void
310 auto_detach(void *data, void *arg) {
311         dns_zone_t *zone = data;
312
313         UNUSED(arg);
314
315         dns_zone_detach(&zone);
316 }