No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / dbtable.c
blobec17a6069a88edf009f83113df5c001e974d4252
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
21 * Id: dbtable.c,v 1.33 2007/06/19 23:47:16 tbox Exp
24 /*! \file
25 * \author
26 * Principal Author: DCL
29 #include <config.h>
31 #include <isc/mem.h>
32 #include <isc/rwlock.h>
33 #include <isc/util.h>
35 #include <dns/dbtable.h>
36 #include <dns/db.h>
37 #include <dns/rbt.h>
38 #include <dns/result.h>
40 struct dns_dbtable {
41 /* Unlocked. */
42 unsigned int magic;
43 isc_mem_t * mctx;
44 dns_rdataclass_t rdclass;
45 isc_mutex_t lock;
46 isc_rwlock_t tree_lock;
47 /* Locked by lock. */
48 unsigned int references;
49 /* Locked by tree_lock. */
50 dns_rbt_t * rbt;
51 dns_db_t * default_db;
54 #define DBTABLE_MAGIC ISC_MAGIC('D', 'B', '-', '-')
55 #define VALID_DBTABLE(dbtable) ISC_MAGIC_VALID(dbtable, DBTABLE_MAGIC)
57 static void
58 dbdetach(void *data, void *arg) {
59 dns_db_t *db = data;
61 UNUSED(arg);
63 dns_db_detach(&db);
66 isc_result_t
67 dns_dbtable_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
68 dns_dbtable_t **dbtablep)
70 dns_dbtable_t *dbtable;
71 isc_result_t result;
73 REQUIRE(mctx != NULL);
74 REQUIRE(dbtablep != NULL && *dbtablep == NULL);
76 dbtable = (dns_dbtable_t *)isc_mem_get(mctx, sizeof(*dbtable));
77 if (dbtable == NULL)
78 return (ISC_R_NOMEMORY);
80 dbtable->rbt = NULL;
81 result = dns_rbt_create(mctx, dbdetach, NULL, &dbtable->rbt);
82 if (result != ISC_R_SUCCESS)
83 goto clean1;
85 result = isc_mutex_init(&dbtable->lock);
86 if (result != ISC_R_SUCCESS)
87 goto clean2;
89 result = isc_rwlock_init(&dbtable->tree_lock, 0, 0);
90 if (result != ISC_R_SUCCESS)
91 goto clean3;
93 dbtable->default_db = NULL;
94 dbtable->mctx = mctx;
95 dbtable->rdclass = rdclass;
96 dbtable->magic = DBTABLE_MAGIC;
97 dbtable->references = 1;
99 *dbtablep = dbtable;
101 return (ISC_R_SUCCESS);
103 clean3:
104 DESTROYLOCK(&dbtable->lock);
106 clean2:
107 dns_rbt_destroy(&dbtable->rbt);
109 clean1:
110 isc_mem_put(mctx, dbtable, sizeof(*dbtable));
112 return (result);
115 static inline void
116 dbtable_free(dns_dbtable_t *dbtable) {
118 * Caller must ensure that it is safe to call.
121 RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
123 if (dbtable->default_db != NULL)
124 dns_db_detach(&dbtable->default_db);
126 dns_rbt_destroy(&dbtable->rbt);
128 RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
130 isc_rwlock_destroy(&dbtable->tree_lock);
132 dbtable->magic = 0;
134 isc_mem_put(dbtable->mctx, dbtable, sizeof(*dbtable));
137 void
138 dns_dbtable_attach(dns_dbtable_t *source, dns_dbtable_t **targetp) {
139 REQUIRE(VALID_DBTABLE(source));
140 REQUIRE(targetp != NULL && *targetp == NULL);
142 LOCK(&source->lock);
144 INSIST(source->references > 0);
145 source->references++;
146 INSIST(source->references != 0);
148 UNLOCK(&source->lock);
150 *targetp = source;
153 void
154 dns_dbtable_detach(dns_dbtable_t **dbtablep) {
155 dns_dbtable_t *dbtable;
156 isc_boolean_t free_dbtable = ISC_FALSE;
158 REQUIRE(dbtablep != NULL);
159 dbtable = *dbtablep;
160 REQUIRE(VALID_DBTABLE(dbtable));
162 LOCK(&dbtable->lock);
164 INSIST(dbtable->references > 0);
165 dbtable->references--;
166 if (dbtable->references == 0)
167 free_dbtable = ISC_TRUE;
169 UNLOCK(&dbtable->lock);
171 if (free_dbtable)
172 dbtable_free(dbtable);
174 *dbtablep = NULL;
177 isc_result_t
178 dns_dbtable_add(dns_dbtable_t *dbtable, dns_db_t *db) {
179 isc_result_t result;
180 dns_db_t *clone;
182 REQUIRE(VALID_DBTABLE(dbtable));
183 REQUIRE(dns_db_class(db) == dbtable->rdclass);
185 clone = NULL;
186 dns_db_attach(db, &clone);
188 RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
189 result = dns_rbt_addname(dbtable->rbt, dns_db_origin(clone), clone);
190 RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
192 return (result);
195 void
196 dns_dbtable_remove(dns_dbtable_t *dbtable, dns_db_t *db) {
197 dns_db_t *stored_data = NULL;
198 isc_result_t result;
199 dns_name_t *name;
201 REQUIRE(VALID_DBTABLE(dbtable));
203 name = dns_db_origin(db);
206 * There is a requirement that the association of name with db
207 * be verified. With the current rbt.c this is expensive to do,
208 * because effectively two find operations are being done, but
209 * deletion is relatively infrequent.
210 * XXXDCL ... this could be cheaper now with dns_rbt_deletenode.
213 RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
215 result = dns_rbt_findname(dbtable->rbt, name, 0, NULL,
216 (void **) (void *)&stored_data);
218 if (result == ISC_R_SUCCESS) {
219 INSIST(stored_data == db);
221 (void)dns_rbt_deletename(dbtable->rbt, name, ISC_FALSE);
224 RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
227 void
228 dns_dbtable_adddefault(dns_dbtable_t *dbtable, dns_db_t *db) {
229 REQUIRE(VALID_DBTABLE(dbtable));
230 REQUIRE(dbtable->default_db == NULL);
231 REQUIRE(dns_name_compare(dns_db_origin(db), dns_rootname) == 0);
233 RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
235 dbtable->default_db = NULL;
236 dns_db_attach(db, &dbtable->default_db);
238 RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
241 void
242 dns_dbtable_getdefault(dns_dbtable_t *dbtable, dns_db_t **dbp) {
243 REQUIRE(VALID_DBTABLE(dbtable));
244 REQUIRE(dbp != NULL && *dbp == NULL);
246 RWLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
248 dns_db_attach(dbtable->default_db, dbp);
250 RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
253 void
254 dns_dbtable_removedefault(dns_dbtable_t *dbtable) {
255 REQUIRE(VALID_DBTABLE(dbtable));
257 RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
259 dns_db_detach(&dbtable->default_db);
261 RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
264 isc_result_t
265 dns_dbtable_find(dns_dbtable_t *dbtable, dns_name_t *name,
266 unsigned int options, dns_db_t **dbp)
268 dns_db_t *stored_data = NULL;
269 isc_result_t result;
270 unsigned int rbtoptions = 0;
272 REQUIRE(dbp != NULL && *dbp == NULL);
274 if ((options & DNS_DBTABLEFIND_NOEXACT) != 0)
275 rbtoptions |= DNS_RBTFIND_NOEXACT;
277 RWLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
279 result = dns_rbt_findname(dbtable->rbt, name, rbtoptions, NULL,
280 (void **) (void *)&stored_data);
282 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
283 dns_db_attach(stored_data, dbp);
284 else if (dbtable->default_db != NULL) {
285 dns_db_attach(dbtable->default_db, dbp);
286 result = DNS_R_PARTIALMATCH;
287 } else
288 result = ISC_R_NOTFOUND;
290 RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
292 return (result);