Detect FPU by checking CPUID features.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / zt.c
blobed7f28a4a97007af36d51cc723a033b90c5897e5
1 /*
2 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
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.
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.47 2007/06/19 23:47:16 tbox Exp $ */
20 /*! \file */
22 #include <config.h>
24 #include <isc/file.h>
25 #include <isc/magic.h>
26 #include <isc/mem.h>
27 #include <isc/string.h>
28 #include <isc/util.h>
30 #include <dns/log.h>
31 #include <dns/name.h>
32 #include <dns/rbt.h>
33 #include <dns/rdataclass.h>
34 #include <dns/result.h>
35 #include <dns/view.h>
36 #include <dns/zone.h>
37 #include <dns/zt.h>
39 struct dns_zt {
40 /* Unlocked. */
41 unsigned int magic;
42 isc_mem_t *mctx;
43 dns_rdataclass_t rdclass;
44 isc_rwlock_t rwlock;
45 /* Locked by lock. */
46 isc_uint32_t references;
47 dns_rbt_t *table;
50 #define ZTMAGIC ISC_MAGIC('Z', 'T', 'b', 'l')
51 #define VALID_ZT(zt) ISC_MAGIC_VALID(zt, ZTMAGIC)
53 static void
54 auto_detach(void *, void *);
56 static isc_result_t
57 load(dns_zone_t *zone, void *uap);
59 static isc_result_t
60 loadnew(dns_zone_t *zone, void *uap);
62 static isc_result_t
63 freezezones(dns_zone_t *zone, void *uap);
65 isc_result_t
66 dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **ztp)
68 dns_zt_t *zt;
69 isc_result_t result;
71 REQUIRE(ztp != NULL && *ztp == NULL);
73 zt = isc_mem_get(mctx, sizeof(*zt));
74 if (zt == NULL)
75 return (ISC_R_NOMEMORY);
77 zt->table = NULL;
78 result = dns_rbt_create(mctx, auto_detach, zt, &zt->table);
79 if (result != ISC_R_SUCCESS)
80 goto cleanup_zt;
82 result = isc_rwlock_init(&zt->rwlock, 0, 0);
83 if (result != ISC_R_SUCCESS)
84 goto cleanup_rbt;
86 zt->mctx = mctx;
87 zt->references = 1;
88 zt->rdclass = rdclass;
89 zt->magic = ZTMAGIC;
90 *ztp = zt;
92 return (ISC_R_SUCCESS);
94 cleanup_rbt:
95 dns_rbt_destroy(&zt->table);
97 cleanup_zt:
98 isc_mem_put(mctx, zt, sizeof(*zt));
100 return (result);
103 isc_result_t
104 dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
105 isc_result_t result;
106 dns_zone_t *dummy = NULL;
107 dns_name_t *name;
109 REQUIRE(VALID_ZT(zt));
111 name = dns_zone_getorigin(zone);
113 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
115 result = dns_rbt_addname(zt->table, name, zone);
116 if (result == ISC_R_SUCCESS)
117 dns_zone_attach(zone, &dummy);
119 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
121 return (result);
124 isc_result_t
125 dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
126 isc_result_t result;
127 dns_name_t *name;
129 REQUIRE(VALID_ZT(zt));
131 name = dns_zone_getorigin(zone);
133 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
135 result = dns_rbt_deletename(zt->table, name, ISC_FALSE);
137 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
139 return (result);
142 isc_result_t
143 dns_zt_find(dns_zt_t *zt, dns_name_t *name, unsigned int options,
144 dns_name_t *foundname, dns_zone_t **zonep)
146 isc_result_t result;
147 dns_zone_t *dummy = NULL;
148 unsigned int rbtoptions = 0;
150 REQUIRE(VALID_ZT(zt));
152 if ((options & DNS_ZTFIND_NOEXACT) != 0)
153 rbtoptions |= DNS_RBTFIND_NOEXACT;
155 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
157 result = dns_rbt_findname(zt->table, name, rbtoptions, foundname,
158 (void **) (void*)&dummy);
159 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
160 dns_zone_attach(dummy, zonep);
162 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
164 return (result);
167 void
168 dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {
170 REQUIRE(VALID_ZT(zt));
171 REQUIRE(ztp != NULL && *ztp == NULL);
173 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
175 INSIST(zt->references > 0);
176 zt->references++;
177 INSIST(zt->references != 0);
179 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
181 *ztp = zt;
184 static isc_result_t
185 flush(dns_zone_t *zone, void *uap) {
186 UNUSED(uap);
187 return (dns_zone_flush(zone));
190 static void
191 zt_flushanddetach(dns_zt_t **ztp, isc_boolean_t need_flush) {
192 isc_boolean_t destroy = ISC_FALSE;
193 dns_zt_t *zt;
195 REQUIRE(ztp != NULL && VALID_ZT(*ztp));
197 zt = *ztp;
199 RWLOCK(&zt->rwlock, isc_rwlocktype_write);
201 INSIST(zt->references > 0);
202 zt->references--;
203 if (zt->references == 0)
204 destroy = ISC_TRUE;
206 RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
208 if (destroy) {
209 if (need_flush)
210 (void)dns_zt_apply(zt, ISC_FALSE, flush, NULL);
211 dns_rbt_destroy(&zt->table);
212 isc_rwlock_destroy(&zt->rwlock);
213 zt->magic = 0;
214 isc_mem_put(zt->mctx, zt, sizeof(*zt));
217 *ztp = NULL;
220 void
221 dns_zt_flushanddetach(dns_zt_t **ztp) {
222 zt_flushanddetach(ztp, ISC_TRUE);
225 void
226 dns_zt_detach(dns_zt_t **ztp) {
227 zt_flushanddetach(ztp, ISC_FALSE);
230 isc_result_t
231 dns_zt_load(dns_zt_t *zt, isc_boolean_t stop) {
232 isc_result_t result;
234 REQUIRE(VALID_ZT(zt));
236 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
237 result = dns_zt_apply(zt, stop, load, NULL);
238 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
239 return (result);
242 static isc_result_t
243 load(dns_zone_t *zone, void *uap) {
244 isc_result_t result;
245 UNUSED(uap);
246 result = dns_zone_load(zone);
247 if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE)
248 result = ISC_R_SUCCESS;
249 return (result);
252 isc_result_t
253 dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop) {
254 isc_result_t result;
256 REQUIRE(VALID_ZT(zt));
258 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
259 result = dns_zt_apply(zt, stop, loadnew, NULL);
260 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
261 return (result);
264 static isc_result_t
265 loadnew(dns_zone_t *zone, void *uap) {
266 isc_result_t result;
267 UNUSED(uap);
268 result = dns_zone_loadnew(zone);
269 if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE ||
270 result == DNS_R_DYNAMIC)
271 result = ISC_R_SUCCESS;
272 return (result);
275 isc_result_t
276 dns_zt_freezezones(dns_zt_t *zt, isc_boolean_t freeze) {
277 isc_result_t result, tresult;
279 REQUIRE(VALID_ZT(zt));
281 RWLOCK(&zt->rwlock, isc_rwlocktype_read);
282 result = dns_zt_apply2(zt, ISC_FALSE, &tresult, freezezones, &freeze);
283 RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
284 return ((result == ISC_R_SUCCESS) ? tresult : result);
287 static isc_result_t
288 freezezones(dns_zone_t *zone, void *uap) {
289 isc_boolean_t freeze = *(isc_boolean_t *)uap;
290 isc_boolean_t frozen;
291 isc_result_t result = ISC_R_SUCCESS;
292 char classstr[DNS_RDATACLASS_FORMATSIZE];
293 char zonename[DNS_NAME_FORMATSIZE];
294 dns_view_t *view;
295 char *journal;
296 const char *vname;
297 const char *sep;
298 int level;
300 if (dns_zone_gettype(zone) != dns_zone_master)
301 return (ISC_R_SUCCESS);
303 frozen = dns_zone_getupdatedisabled(zone);
304 if (freeze) {
305 if (frozen)
306 result = DNS_R_FROZEN;
307 if (result == ISC_R_SUCCESS)
308 result = dns_zone_flush(zone);
309 if (result == ISC_R_SUCCESS) {
310 journal = dns_zone_getjournal(zone);
311 if (journal != NULL)
312 (void)isc_file_remove(journal);
314 } else {
315 if (frozen) {
316 result = dns_zone_load(zone);
317 if (result == DNS_R_CONTINUE ||
318 result == DNS_R_UPTODATE)
319 result = ISC_R_SUCCESS;
322 if (result == ISC_R_SUCCESS)
323 dns_zone_setupdatedisabled(zone, freeze);
324 view = dns_zone_getview(zone);
325 if (strcmp(view->name, "_bind") == 0 ||
326 strcmp(view->name, "_default") == 0)
328 vname = "";
329 sep = "";
330 } else {
331 vname = view->name;
332 sep = " ";
334 dns_rdataclass_format(dns_zone_getclass(zone), classstr,
335 sizeof(classstr));
336 dns_name_format(dns_zone_getorigin(zone), zonename, sizeof(zonename));
337 level = (result != ISC_R_SUCCESS) ? ISC_LOG_ERROR : ISC_LOG_DEBUG(1);
338 isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE,
339 level, "%s zone '%s/%s'%s%s: %s",
340 freeze ? "freezing" : "thawing",
341 zonename, classstr, sep, vname,
342 isc_result_totext(result));
343 return (result);
346 isc_result_t
347 dns_zt_apply(dns_zt_t *zt, isc_boolean_t stop,
348 isc_result_t (*action)(dns_zone_t *, void *), void *uap)
350 return (dns_zt_apply2(zt, stop, NULL, action, uap));
353 isc_result_t
354 dns_zt_apply2(dns_zt_t *zt, isc_boolean_t stop, isc_result_t *sub,
355 isc_result_t (*action)(dns_zone_t *, void *), void *uap)
357 dns_rbtnode_t *node;
358 dns_rbtnodechain_t chain;
359 isc_result_t result, tresult = ISC_R_SUCCESS;
360 dns_zone_t *zone;
362 REQUIRE(VALID_ZT(zt));
363 REQUIRE(action != NULL);
365 dns_rbtnodechain_init(&chain, zt->mctx);
366 result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
367 if (result == ISC_R_NOTFOUND) {
369 * The tree is empty.
371 result = ISC_R_NOMORE;
373 while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
374 result = dns_rbtnodechain_current(&chain, NULL, NULL,
375 &node);
376 if (result == ISC_R_SUCCESS) {
377 zone = node->data;
378 if (zone != NULL)
379 result = (action)(zone, uap);
380 if (result != ISC_R_SUCCESS && stop) {
381 tresult = result;
382 goto cleanup; /* don't break */
383 } else if (result != ISC_R_SUCCESS &&
384 tresult == ISC_R_SUCCESS)
385 tresult = result;
387 result = dns_rbtnodechain_next(&chain, NULL, NULL);
389 if (result == ISC_R_NOMORE)
390 result = ISC_R_SUCCESS;
392 cleanup:
393 dns_rbtnodechain_invalidate(&chain);
394 if (sub != NULL)
395 *sub = tresult;
397 return (result);
400 /***
401 *** Private
402 ***/
404 static void
405 auto_detach(void *data, void *arg) {
406 dns_zone_t *zone = data;
408 UNUSED(arg);
410 dns_zone_detach(&zone);