2 * Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 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: rbtdb.c,v 1.270.12.16.10.6 2010/11/16 07:46:23 marka Exp $ */
23 * Principal Author: Bob Halley
30 #include <isc/event.h>
33 #include <isc/mutex.h>
34 #include <isc/platform.h>
35 #include <isc/print.h>
36 #include <isc/random.h>
37 #include <isc/refcount.h>
38 #include <isc/rwlock.h>
39 #include <isc/serial.h>
40 #include <isc/string.h>
45 #include <dns/acache.h>
47 #include <dns/dbiterator.h>
48 #include <dns/events.h>
49 #include <dns/fixedname.h>
52 #include <dns/masterdump.h>
54 #include <dns/nsec3.h>
56 #include <dns/rdata.h>
57 #include <dns/rdataset.h>
58 #include <dns/rdatasetiter.h>
59 #include <dns/rdataslab.h>
60 #include <dns/rdatastruct.h>
61 #include <dns/result.h>
62 #include <dns/stats.h>
65 #include <dns/zonekey.h>
67 #ifdef DNS_RBTDB_VERSION64
73 #ifdef DNS_RBTDB_VERSION64
74 #define RBTDB_MAGIC ISC_MAGIC('R', 'B', 'D', '8')
76 #define RBTDB_MAGIC ISC_MAGIC('R', 'B', 'D', '4')
80 * Note that "impmagic" is not the first four bytes of the struct, so
81 * ISC_MAGIC_VALID cannot be used.
83 #define VALID_RBTDB(rbtdb) ((rbtdb) != NULL && \
84 (rbtdb)->common.impmagic == RBTDB_MAGIC)
86 #ifdef DNS_RBTDB_VERSION64
87 typedef isc_uint64_t rbtdb_serial_t;
89 * Make casting easier in symbolic debuggers by using different names
90 * for the 64 bit version.
92 #define dns_rbtdb_t dns_rbtdb64_t
93 #define rdatasetheader_t rdatasetheader64_t
94 #define rbtdb_version_t rbtdb_version64_t
96 typedef isc_uint32_t rbtdb_serial_t;
99 typedef isc_uint32_t rbtdb_rdatatype_t;
101 #define RBTDB_RDATATYPE_BASE(type) ((dns_rdatatype_t)((type) & 0xFFFF))
102 #define RBTDB_RDATATYPE_EXT(type) ((dns_rdatatype_t)((type) >> 16))
103 #define RBTDB_RDATATYPE_VALUE(b, e) ((rbtdb_rdatatype_t)((e) << 16) | (b))
105 #define RBTDB_RDATATYPE_SIGNSEC \
106 RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_nsec)
107 #define RBTDB_RDATATYPE_SIGNSEC3 \
108 RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_nsec3)
109 #define RBTDB_RDATATYPE_SIGNS \
110 RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_ns)
111 #define RBTDB_RDATATYPE_SIGCNAME \
112 RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_cname)
113 #define RBTDB_RDATATYPE_SIGDNAME \
114 RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_dname)
115 #define RBTDB_RDATATYPE_NCACHEANY \
116 RBTDB_RDATATYPE_VALUE(0, dns_rdatatype_any)
119 * We use rwlock for DB lock only when ISC_RWLOCK_USEATOMIC is non 0.
120 * Using rwlock is effective with regard to lookup performance only when
121 * it is implemented in an efficient way.
122 * Otherwise, it is generally wise to stick to the simple locking since rwlock
123 * would require more memory or can even make lookups slower due to its own
124 * overhead (when it internally calls mutex locks).
126 #ifdef ISC_RWLOCK_USEATOMIC
127 #define DNS_RBTDB_USERWLOCK 1
129 #define DNS_RBTDB_USERWLOCK 0
132 #if DNS_RBTDB_USERWLOCK
133 #define RBTDB_INITLOCK(l) isc_rwlock_init((l), 0, 0)
134 #define RBTDB_DESTROYLOCK(l) isc_rwlock_destroy(l)
135 #define RBTDB_LOCK(l, t) RWLOCK((l), (t))
136 #define RBTDB_UNLOCK(l, t) RWUNLOCK((l), (t))
138 #define RBTDB_INITLOCK(l) isc_mutex_init(l)
139 #define RBTDB_DESTROYLOCK(l) DESTROYLOCK(l)
140 #define RBTDB_LOCK(l, t) LOCK(l)
141 #define RBTDB_UNLOCK(l, t) UNLOCK(l)
145 * Since node locking is sensitive to both performance and memory footprint,
146 * we need some trick here. If we have both high-performance rwlock and
147 * high performance and small-memory reference counters, we use rwlock for
148 * node lock and isc_refcount for node references. In this case, we don't have
149 * to protect the access to the counters by locks.
150 * Otherwise, we simply use ordinary mutex lock for node locking, and use
151 * simple integers as reference counters which is protected by the lock.
152 * In most cases, we can simply use wrapper macros such as NODE_LOCK and
153 * NODE_UNLOCK. In some other cases, however, we need to protect reference
154 * counters first and then protect other parts of a node as read-only data.
155 * Special additional macros, NODE_STRONGLOCK(), NODE_WEAKLOCK(), etc, are also
156 * provided for these special cases. When we can use the efficient backend
157 * routines, we should only protect the "other members" by NODE_WEAKLOCK(read).
158 * Otherwise, we should use NODE_STRONGLOCK() to protect the entire critical
159 * section including the access to the reference counter.
160 * Note that we cannot use NODE_LOCK()/NODE_UNLOCK() wherever the protected
161 * section is also protected by NODE_STRONGLOCK().
163 #if defined(ISC_RWLOCK_USEATOMIC) && defined(DNS_RBT_USEISCREFCOUNT)
164 typedef isc_rwlock_t nodelock_t;
166 #define NODE_INITLOCK(l) isc_rwlock_init((l), 0, 0)
167 #define NODE_DESTROYLOCK(l) isc_rwlock_destroy(l)
168 #define NODE_LOCK(l, t) RWLOCK((l), (t))
169 #define NODE_UNLOCK(l, t) RWUNLOCK((l), (t))
170 #define NODE_TRYUPGRADE(l) isc_rwlock_tryupgrade(l)
172 #define NODE_STRONGLOCK(l) ((void)0)
173 #define NODE_STRONGUNLOCK(l) ((void)0)
174 #define NODE_WEAKLOCK(l, t) NODE_LOCK(l, t)
175 #define NODE_WEAKUNLOCK(l, t) NODE_UNLOCK(l, t)
176 #define NODE_WEAKDOWNGRADE(l) isc_rwlock_downgrade(l)
178 typedef isc_mutex_t nodelock_t;
180 #define NODE_INITLOCK(l) isc_mutex_init(l)
181 #define NODE_DESTROYLOCK(l) DESTROYLOCK(l)
182 #define NODE_LOCK(l, t) LOCK(l)
183 #define NODE_UNLOCK(l, t) UNLOCK(l)
184 #define NODE_TRYUPGRADE(l) ISC_R_SUCCESS
186 #define NODE_STRONGLOCK(l) LOCK(l)
187 #define NODE_STRONGUNLOCK(l) UNLOCK(l)
188 #define NODE_WEAKLOCK(l, t) ((void)0)
189 #define NODE_WEAKUNLOCK(l, t) ((void)0)
190 #define NODE_WEAKDOWNGRADE(l) ((void)0)
194 * Whether to rate-limit updating the LRU to avoid possible thread contention.
195 * Our performance measurement has shown the cost is marginal, so it's defined
196 * to be 0 by default either with or without threads.
198 #ifndef DNS_RBTDB_LIMITLRUUPDATE
199 #define DNS_RBTDB_LIMITLRUUPDATE 0
203 * Allow clients with a virtual time of up to 5 minutes in the past to see
204 * records that would have otherwise have expired.
206 #define RBTDB_VIRTUAL 300
212 dns_rdatatype_t type;
215 typedef struct acachectl acachectl_t;
217 typedef struct rdatasetheader {
219 * Locked by the owning node's lock.
221 rbtdb_serial_t serial;
223 rbtdb_rdatatype_t type;
224 isc_uint16_t attributes;
226 struct noqname *noqname;
227 struct noqname *closest;
229 * We don't use the LIST macros, because the LIST structure has
230 * both head and tail pointers, and is doubly linked.
233 struct rdatasetheader *next;
235 * If this is the top header for an rdataset, 'next' points
236 * to the top header for the next rdataset (i.e., the next type).
237 * Otherwise, it points up to the header whose down pointer points
241 struct rdatasetheader *down;
243 * Points to the header for the next older version of
249 * Monotonously increased every time this rdataset is bound so that
250 * it is used as the base of the starting point in DNS responses
251 * when the "cyclic" rrset-order is required. Since the ordering
252 * should not be so crucial, no lock is set for the counter for
253 * performance reasons.
256 acachectl_t *additional_auth;
257 acachectl_t *additional_glue;
260 isc_stdtime_t last_used;
261 ISC_LINK(struct rdatasetheader) link;
263 unsigned int heap_index;
265 * Used for TTL-based cache cleaning.
267 isc_stdtime_t resign;
270 typedef ISC_LIST(rdatasetheader_t) rdatasetheaderlist_t;
271 typedef ISC_LIST(dns_rbtnode_t) rbtnodelist_t;
273 #define RDATASET_ATTR_NONEXISTENT 0x0001
274 #define RDATASET_ATTR_STALE 0x0002
275 #define RDATASET_ATTR_IGNORE 0x0004
276 #define RDATASET_ATTR_RETAIN 0x0008
277 #define RDATASET_ATTR_NXDOMAIN 0x0010
278 #define RDATASET_ATTR_RESIGN 0x0020
279 #define RDATASET_ATTR_STATCOUNT 0x0040
280 #define RDATASET_ATTR_OPTOUT 0x0080
282 typedef struct acache_cbarg {
283 dns_rdatasetadditional_t type;
287 rdatasetheader_t *header;
291 dns_acacheentry_t *entry;
292 acache_cbarg_t *cbarg;
297 * When the cache will pre-expire data (due to memory low or other
298 * situations) before the rdataset's TTL has expired, it MUST
299 * respect the RETAIN bit and not expire the data until its TTL is
303 #undef IGNORE /* WIN32 winbase.h defines this. */
305 #define EXISTS(header) \
306 (((header)->attributes & RDATASET_ATTR_NONEXISTENT) == 0)
307 #define NONEXISTENT(header) \
308 (((header)->attributes & RDATASET_ATTR_NONEXISTENT) != 0)
309 #define IGNORE(header) \
310 (((header)->attributes & RDATASET_ATTR_IGNORE) != 0)
311 #define RETAIN(header) \
312 (((header)->attributes & RDATASET_ATTR_RETAIN) != 0)
313 #define NXDOMAIN(header) \
314 (((header)->attributes & RDATASET_ATTR_NXDOMAIN) != 0)
315 #define RESIGN(header) \
316 (((header)->attributes & RDATASET_ATTR_RESIGN) != 0)
317 #define OPTOUT(header) \
318 (((header)->attributes & RDATASET_ATTR_OPTOUT) != 0)
320 #define DEFAULT_NODE_LOCK_COUNT 7 /*%< Should be prime. */
323 * Number of buckets for cache DB entries (locks, LRU lists, TTL heaps).
324 * There is a tradeoff issue about configuring this value: if this is too
325 * small, it may cause heavier contention between threads; if this is too large,
326 * LRU purge algorithm won't work well (entries tend to be purged prematurely).
327 * The default value should work well for most environments, but this can
328 * also be configurable at compilation time via the
329 * DNS_RBTDB_CACHE_NODE_LOCK_COUNT variable. This value must be larger than
330 * 1 due to the assumption of overmem_purge().
332 #ifdef DNS_RBTDB_CACHE_NODE_LOCK_COUNT
333 #if DNS_RBTDB_CACHE_NODE_LOCK_COUNT <= 1
334 #error "DNS_RBTDB_CACHE_NODE_LOCK_COUNT must be larger than 1"
336 #define DEFAULT_CACHE_NODE_LOCK_COUNT DNS_RBTDB_CACHE_NODE_LOCK_COUNT
339 #define DEFAULT_CACHE_NODE_LOCK_COUNT 16
340 #endif /* DNS_RBTDB_CACHE_NODE_LOCK_COUNT */
344 /* Protected in the refcount routines. */
345 isc_refcount_t references;
346 /* Locked by lock. */
347 isc_boolean_t exiting;
350 typedef struct rbtdb_changed {
351 dns_rbtnode_t * node;
353 ISC_LINK(struct rbtdb_changed) link;
356 typedef ISC_LIST(rbtdb_changed_t) rbtdb_changedlist_t;
364 typedef struct rbtdb_version {
366 rbtdb_serial_t serial;
368 * Protected in the refcount routines.
369 * XXXJT: should we change the lock policy based on the refcount
372 isc_refcount_t references;
373 /* Locked by database lock. */
374 isc_boolean_t writer;
375 isc_boolean_t commit_ok;
376 rbtdb_changedlist_t changed_list;
377 rdatasetheaderlist_t resigned_list;
378 ISC_LINK(struct rbtdb_version) link;
379 dns_db_secure_t secure;
380 isc_boolean_t havensec3;
381 /* NSEC3 parameters */
384 isc_uint16_t iterations;
385 isc_uint8_t salt_length;
386 unsigned char salt[DNS_NSEC3_SALTSIZE];
389 typedef ISC_LIST(rbtdb_version_t) rbtdb_versionlist_t;
394 #if DNS_RBTDB_USERWLOCK
399 isc_rwlock_t tree_lock;
400 unsigned int node_lock_count;
401 rbtdb_nodelock_t * node_locks;
402 dns_rbtnode_t * origin_node;
403 dns_stats_t * rrsetstats; /* cache DB only */
404 /* Locked by lock. */
406 isc_refcount_t references;
407 unsigned int attributes;
408 rbtdb_serial_t current_serial;
409 rbtdb_serial_t least_serial;
410 rbtdb_serial_t next_serial;
411 rbtdb_version_t * current_version;
412 rbtdb_version_t * future_version;
413 rbtdb_versionlist_t open_versions;
415 dns_dbnode_t *soanode;
416 dns_dbnode_t *nsnode;
419 * This is a linked list used to implement the LRU cache. There will
420 * be node_lock_count linked lists here. Nodes in bucket 1 will be
421 * placed on the linked list rdatasets[1].
423 rdatasetheaderlist_t *rdatasets;
426 * Temporary storage for stale cache nodes and dynamically deleted
427 * nodes that await being cleaned up.
429 rbtnodelist_t *deadnodes;
432 * Heaps. Each of these is used for TTL based expiry.
436 /* Locked by tree_lock. */
441 unsigned int quantum;
444 #define RBTDB_ATTR_LOADED 0x01
445 #define RBTDB_ATTR_LOADING 0x02
452 rbtdb_version_t * rbtversion;
453 rbtdb_serial_t serial;
454 unsigned int options;
455 dns_rbtnodechain_t chain;
456 isc_boolean_t copy_name;
457 isc_boolean_t need_cleanup;
459 dns_rbtnode_t * zonecut;
460 rdatasetheader_t * zonecut_rdataset;
461 rdatasetheader_t * zonecut_sigrdataset;
462 dns_fixedname_t zonecut_name;
474 static void rdataset_disassociate(dns_rdataset_t *rdataset);
475 static isc_result_t rdataset_first(dns_rdataset_t *rdataset);
476 static isc_result_t rdataset_next(dns_rdataset_t *rdataset);
477 static void rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata);
478 static void rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target);
479 static unsigned int rdataset_count(dns_rdataset_t *rdataset);
480 static isc_result_t rdataset_getnoqname(dns_rdataset_t *rdataset,
483 dns_rdataset_t *negsig);
484 static isc_result_t rdataset_getclosest(dns_rdataset_t *rdataset,
487 dns_rdataset_t *negsig);
488 static isc_result_t rdataset_getadditional(dns_rdataset_t *rdataset,
489 dns_rdatasetadditional_t type,
490 dns_rdatatype_t qtype,
491 dns_acache_t *acache,
494 dns_dbversion_t **versionp,
495 dns_dbnode_t **nodep,
499 static isc_result_t rdataset_setadditional(dns_rdataset_t *rdataset,
500 dns_rdatasetadditional_t type,
501 dns_rdatatype_t qtype,
502 dns_acache_t *acache,
505 dns_dbversion_t *version,
508 static isc_result_t rdataset_putadditional(dns_acache_t *acache,
509 dns_rdataset_t *rdataset,
510 dns_rdatasetadditional_t type,
511 dns_rdatatype_t qtype);
512 static inline isc_boolean_t need_headerupdate(rdatasetheader_t *header,
514 static void update_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
516 static void expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
517 isc_boolean_t tree_locked);
518 static void overmem_purge(dns_rbtdb_t *rbtdb, unsigned int locknum_start,
519 isc_stdtime_t now, isc_boolean_t tree_locked);
520 static isc_result_t resign_insert(dns_rbtdb_t *rbtdb, int idx,
521 rdatasetheader_t *newheader);
522 static void prune_tree(isc_task_t *task, isc_event_t *event);
523 static void rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust);
524 static void rdataset_expire(dns_rdataset_t *rdataset);
526 static dns_rdatasetmethods_t rdataset_methods = {
527 rdataset_disassociate,
537 rdataset_getadditional,
538 rdataset_setadditional,
539 rdataset_putadditional,
544 static void rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp);
545 static isc_result_t rdatasetiter_first(dns_rdatasetiter_t *iterator);
546 static isc_result_t rdatasetiter_next(dns_rdatasetiter_t *iterator);
547 static void rdatasetiter_current(dns_rdatasetiter_t *iterator,
548 dns_rdataset_t *rdataset);
550 static dns_rdatasetitermethods_t rdatasetiter_methods = {
551 rdatasetiter_destroy,
557 typedef struct rbtdb_rdatasetiter {
558 dns_rdatasetiter_t common;
559 rdatasetheader_t * current;
560 } rbtdb_rdatasetiter_t;
562 static void dbiterator_destroy(dns_dbiterator_t **iteratorp);
563 static isc_result_t dbiterator_first(dns_dbiterator_t *iterator);
564 static isc_result_t dbiterator_last(dns_dbiterator_t *iterator);
565 static isc_result_t dbiterator_seek(dns_dbiterator_t *iterator,
567 static isc_result_t dbiterator_prev(dns_dbiterator_t *iterator);
568 static isc_result_t dbiterator_next(dns_dbiterator_t *iterator);
569 static isc_result_t dbiterator_current(dns_dbiterator_t *iterator,
570 dns_dbnode_t **nodep,
572 static isc_result_t dbiterator_pause(dns_dbiterator_t *iterator);
573 static isc_result_t dbiterator_origin(dns_dbiterator_t *iterator,
576 static dns_dbiteratormethods_t dbiterator_methods = {
588 #define DELETION_BATCH_MAX 64
591 * If 'paused' is ISC_TRUE, then the tree lock is not being held.
593 typedef struct rbtdb_dbiterator {
594 dns_dbiterator_t common;
595 isc_boolean_t paused;
596 isc_boolean_t new_origin;
597 isc_rwlocktype_t tree_locked;
599 dns_fixedname_t name;
600 dns_fixedname_t origin;
601 dns_rbtnodechain_t chain;
602 dns_rbtnodechain_t nsec3chain;
603 dns_rbtnodechain_t *current;
605 dns_rbtnode_t *deletions[DELETION_BATCH_MAX];
607 isc_boolean_t nsec3only;
608 isc_boolean_t nonsec3;
609 } rbtdb_dbiterator_t;
612 #define IS_STUB(rbtdb) (((rbtdb)->common.attributes & DNS_DBATTR_STUB) != 0)
613 #define IS_CACHE(rbtdb) (((rbtdb)->common.attributes & DNS_DBATTR_CACHE) != 0)
615 static void free_rbtdb(dns_rbtdb_t *rbtdb, isc_boolean_t log,
617 static void overmem(dns_db_t *db, isc_boolean_t overmem);
618 static void setnsec3parameters(dns_db_t *db, rbtdb_version_t *version,
619 isc_boolean_t *nsec3createflag);
622 * 'init_count' is used to initialize 'newheader->count' which inturn
623 * is used to determine where in the cycle rrset-order cyclic starts.
624 * We don't lock this as we don't care about simultaneous updates.
627 * Both init_count and header->count can be ISC_UINT32_MAX.
628 * The count on the returned rdataset however can't be as
629 * that indicates that the database does not implement cyclic
632 static unsigned int init_count;
637 * If a routine is going to lock more than one lock in this module, then
638 * the locking must be done in the following order:
642 * Node Lock (Only one from the set may be locked at one time by
647 * Failure to follow this hierarchy can result in deadlock.
653 * For zone databases the node for the origin of the zone MUST NOT be deleted.
662 attach(dns_db_t *source, dns_db_t **targetp) {
663 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)source;
665 REQUIRE(VALID_RBTDB(rbtdb));
667 isc_refcount_increment(&rbtdb->references, NULL);
673 free_rbtdb_callback(isc_task_t *task, isc_event_t *event) {
674 dns_rbtdb_t *rbtdb = event->ev_arg;
678 free_rbtdb(rbtdb, ISC_TRUE, event);
682 update_rrsetstats(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
683 isc_boolean_t increment)
685 dns_rdatastatstype_t statattributes = 0;
686 dns_rdatastatstype_t base = 0;
687 dns_rdatastatstype_t type;
689 /* At the moment we count statistics only for cache DB */
690 INSIST(IS_CACHE(rbtdb));
692 if (NXDOMAIN(header))
693 statattributes = DNS_RDATASTATSTYPE_ATTR_NXDOMAIN;
694 else if (RBTDB_RDATATYPE_BASE(header->type) == 0) {
695 statattributes = DNS_RDATASTATSTYPE_ATTR_NXRRSET;
696 base = RBTDB_RDATATYPE_EXT(header->type);
698 base = RBTDB_RDATATYPE_BASE(header->type);
700 type = DNS_RDATASTATSTYPE_VALUE(base, statattributes);
702 dns_rdatasetstats_increment(rbtdb->rrsetstats, type);
704 dns_rdatasetstats_decrement(rbtdb->rrsetstats, type);
708 set_ttl(dns_rbtdb_t *rbtdb, rdatasetheader_t *header, dns_ttl_t newttl) {
713 oldttl = header->rdh_ttl;
714 header->rdh_ttl = newttl;
716 if (!IS_CACHE(rbtdb))
720 * It's possible the rbtdb is not a cache. If this is the case,
721 * we will not have a heap, and we move on. If we do, though,
722 * we might need to adjust things.
724 if (header->heap_index == 0 || newttl == oldttl)
726 idx = header->node->locknum;
727 if (rbtdb->heaps == NULL || rbtdb->heaps[idx] == NULL)
729 heap = rbtdb->heaps[idx];
732 isc_heap_increased(heap, header->heap_index);
734 isc_heap_decreased(heap, header->heap_index);
738 * These functions allow the heap code to rank the priority of each
739 * element. It returns ISC_TRUE if v1 happens "sooner" than v2.
742 ttl_sooner(void *v1, void *v2) {
743 rdatasetheader_t *h1 = v1;
744 rdatasetheader_t *h2 = v2;
746 if (h1->rdh_ttl < h2->rdh_ttl)
752 resign_sooner(void *v1, void *v2) {
753 rdatasetheader_t *h1 = v1;
754 rdatasetheader_t *h2 = v2;
756 if (h1->resign < h2->resign)
762 * This function sets the heap index into the header.
765 set_index(void *what, unsigned int index) {
766 rdatasetheader_t *h = what;
768 h->heap_index = index;
772 * Work out how many nodes can be deleted in the time between two
773 * requests to the nameserver. Smooth the resulting number and use it
774 * as a estimate for the number of nodes to be deleted in the next
778 adjust_quantum(unsigned int old, isc_time_t *start) {
779 unsigned int pps = dns_pps; /* packets per second */
780 unsigned int interval;
789 interval = 1000000 / pps; /* interval in usec */
792 usecs = isc_time_microdiff(&end, start);
795 * We were unable to measure the amount of time taken.
796 * Double the nodes deleted next time.
803 new = old * interval;
804 new /= (unsigned int)usecs;
811 new = (new + old * 3) / 4;
813 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
814 ISC_LOG_DEBUG(1), "adjust_quantum -> %d", new);
820 free_rbtdb(dns_rbtdb_t *rbtdb, isc_boolean_t log, isc_event_t *event) {
822 isc_ondestroy_t ondest;
824 char buf[DNS_NAME_FORMATSIZE];
827 if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in)
828 overmem((dns_db_t *)rbtdb, (isc_boolean_t)-1);
830 REQUIRE(rbtdb->current_version != NULL || EMPTY(rbtdb->open_versions));
831 REQUIRE(rbtdb->future_version == NULL);
833 if (rbtdb->current_version != NULL) {
836 isc_refcount_decrement(&rbtdb->current_version->references,
839 UNLINK(rbtdb->open_versions, rbtdb->current_version, link);
840 isc_refcount_destroy(&rbtdb->current_version->references);
841 isc_mem_put(rbtdb->common.mctx, rbtdb->current_version,
842 sizeof(rbtdb_version_t));
846 * We assume the number of remaining dead nodes is reasonably small;
847 * the overhead of unlinking all nodes here should be negligible.
849 for (i = 0; i < rbtdb->node_lock_count; i++) {
852 node = ISC_LIST_HEAD(rbtdb->deadnodes[i]);
853 while (node != NULL) {
854 ISC_LIST_UNLINK(rbtdb->deadnodes[i], node, deadlink);
855 node = ISC_LIST_HEAD(rbtdb->deadnodes[i]);
860 rbtdb->quantum = (rbtdb->task != NULL) ? 100 : 0;
862 if (rbtdb->tree != NULL) {
863 isc_time_now(&start);
864 result = dns_rbt_destroy2(&rbtdb->tree, rbtdb->quantum);
865 if (result == ISC_R_QUOTA) {
866 INSIST(rbtdb->task != NULL);
867 if (rbtdb->quantum != 0)
868 rbtdb->quantum = adjust_quantum(rbtdb->quantum,
871 event = isc_event_allocate(rbtdb->common.mctx,
873 DNS_EVENT_FREESTORAGE,
876 sizeof(isc_event_t));
879 isc_task_send(rbtdb->task, &event);
882 INSIST(result == ISC_R_SUCCESS && rbtdb->tree == NULL);
885 if (rbtdb->nsec3 != NULL) {
886 isc_time_now(&start);
887 result = dns_rbt_destroy2(&rbtdb->nsec3, rbtdb->quantum);
888 if (result == ISC_R_QUOTA) {
889 INSIST(rbtdb->task != NULL);
890 if (rbtdb->quantum != 0)
891 rbtdb->quantum = adjust_quantum(rbtdb->quantum,
894 event = isc_event_allocate(rbtdb->common.mctx,
896 DNS_EVENT_FREESTORAGE,
899 sizeof(isc_event_t));
902 isc_task_send(rbtdb->task, &event);
905 INSIST(result == ISC_R_SUCCESS && rbtdb->nsec3 == NULL);
909 isc_event_free(&event);
911 if (dns_name_dynamic(&rbtdb->common.origin))
912 dns_name_format(&rbtdb->common.origin, buf,
915 strcpy(buf, "<UNKNOWN>");
916 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
917 DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
918 "done free_rbtdb(%s)", buf);
920 if (dns_name_dynamic(&rbtdb->common.origin))
921 dns_name_free(&rbtdb->common.origin, rbtdb->common.mctx);
922 for (i = 0; i < rbtdb->node_lock_count; i++) {
923 isc_refcount_destroy(&rbtdb->node_locks[i].references);
924 NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
928 * Clean up LRU / re-signing order lists.
930 if (rbtdb->rdatasets != NULL) {
931 for (i = 0; i < rbtdb->node_lock_count; i++)
932 INSIST(ISC_LIST_EMPTY(rbtdb->rdatasets[i]));
933 isc_mem_put(rbtdb->common.mctx, rbtdb->rdatasets,
934 rbtdb->node_lock_count *
935 sizeof(rdatasetheaderlist_t));
938 * Clean up dead node buckets.
940 if (rbtdb->deadnodes != NULL) {
941 for (i = 0; i < rbtdb->node_lock_count; i++)
942 INSIST(ISC_LIST_EMPTY(rbtdb->deadnodes[i]));
943 isc_mem_put(rbtdb->common.mctx, rbtdb->deadnodes,
944 rbtdb->node_lock_count * sizeof(rbtnodelist_t));
947 * Clean up heap objects.
949 if (rbtdb->heaps != NULL) {
950 for (i = 0; i < rbtdb->node_lock_count; i++)
951 isc_heap_destroy(&rbtdb->heaps[i]);
952 isc_mem_put(rbtdb->common.mctx, rbtdb->heaps,
953 rbtdb->node_lock_count *
954 sizeof(isc_heap_t *));
957 if (rbtdb->rrsetstats != NULL)
958 dns_stats_detach(&rbtdb->rrsetstats);
960 isc_mem_put(rbtdb->common.mctx, rbtdb->node_locks,
961 rbtdb->node_lock_count * sizeof(rbtdb_nodelock_t));
962 isc_rwlock_destroy(&rbtdb->tree_lock);
963 isc_refcount_destroy(&rbtdb->references);
964 if (rbtdb->task != NULL)
965 isc_task_detach(&rbtdb->task);
967 RBTDB_DESTROYLOCK(&rbtdb->lock);
968 rbtdb->common.magic = 0;
969 rbtdb->common.impmagic = 0;
970 ondest = rbtdb->common.ondest;
971 isc_mem_putanddetach(&rbtdb->common.mctx, rbtdb, sizeof(*rbtdb));
972 isc_ondestroy_notify(&ondest, rbtdb);
976 maybe_free_rbtdb(dns_rbtdb_t *rbtdb) {
977 isc_boolean_t want_free = ISC_FALSE;
979 unsigned int inactive = 0;
981 /* XXX check for open versions here */
983 if (rbtdb->soanode != NULL)
984 dns_db_detachnode((dns_db_t *)rbtdb, &rbtdb->soanode);
985 if (rbtdb->nsnode != NULL)
986 dns_db_detachnode((dns_db_t *)rbtdb, &rbtdb->nsnode);
989 * Even though there are no external direct references, there still
990 * may be nodes in use.
992 for (i = 0; i < rbtdb->node_lock_count; i++) {
993 NODE_LOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_write);
994 rbtdb->node_locks[i].exiting = ISC_TRUE;
995 NODE_UNLOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_write);
996 if (isc_refcount_current(&rbtdb->node_locks[i].references)
1002 if (inactive != 0) {
1003 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
1004 rbtdb->active -= inactive;
1005 if (rbtdb->active == 0)
1006 want_free = ISC_TRUE;
1007 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
1009 char buf[DNS_NAME_FORMATSIZE];
1010 if (dns_name_dynamic(&rbtdb->common.origin))
1011 dns_name_format(&rbtdb->common.origin, buf,
1014 strcpy(buf, "<UNKNOWN>");
1015 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
1016 DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
1017 "calling free_rbtdb(%s)", buf);
1018 free_rbtdb(rbtdb, ISC_TRUE, NULL);
1024 detach(dns_db_t **dbp) {
1025 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(*dbp);
1028 REQUIRE(VALID_RBTDB(rbtdb));
1030 isc_refcount_decrement(&rbtdb->references, &refs);
1033 maybe_free_rbtdb(rbtdb);
1039 currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
1040 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1041 rbtdb_version_t *version;
1044 REQUIRE(VALID_RBTDB(rbtdb));
1046 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
1047 version = rbtdb->current_version;
1048 isc_refcount_increment(&version->references, &refs);
1049 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
1051 *versionp = (dns_dbversion_t *)version;
1054 static inline rbtdb_version_t *
1055 allocate_version(isc_mem_t *mctx, rbtdb_serial_t serial,
1056 unsigned int references, isc_boolean_t writer)
1058 isc_result_t result;
1059 rbtdb_version_t *version;
1061 version = isc_mem_get(mctx, sizeof(*version));
1062 if (version == NULL)
1064 version->serial = serial;
1065 result = isc_refcount_init(&version->references, references);
1066 if (result != ISC_R_SUCCESS) {
1067 isc_mem_put(mctx, version, sizeof(*version));
1070 version->writer = writer;
1071 version->commit_ok = ISC_FALSE;
1072 ISC_LIST_INIT(version->changed_list);
1073 ISC_LIST_INIT(version->resigned_list);
1074 ISC_LINK_INIT(version, link);
1080 newversion(dns_db_t *db, dns_dbversion_t **versionp) {
1081 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1082 rbtdb_version_t *version;
1084 REQUIRE(VALID_RBTDB(rbtdb));
1085 REQUIRE(versionp != NULL && *versionp == NULL);
1086 REQUIRE(rbtdb->future_version == NULL);
1088 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
1089 RUNTIME_CHECK(rbtdb->next_serial != 0); /* XXX Error? */
1090 version = allocate_version(rbtdb->common.mctx, rbtdb->next_serial, 1,
1092 if (version != NULL) {
1093 version->commit_ok = ISC_TRUE;
1094 version->secure = rbtdb->current_version->secure;
1095 version->havensec3 = rbtdb->current_version->havensec3;
1096 if (version->havensec3) {
1097 version->flags = rbtdb->current_version->flags;
1098 version->iterations =
1099 rbtdb->current_version->iterations;
1100 version->hash = rbtdb->current_version->hash;
1101 version->salt_length =
1102 rbtdb->current_version->salt_length;
1103 memcpy(version->salt, rbtdb->current_version->salt,
1104 version->salt_length);
1107 version->iterations = 0;
1109 version->salt_length = 0;
1110 memset(version->salt, 0, sizeof(version->salt));
1112 rbtdb->next_serial++;
1113 rbtdb->future_version = version;
1115 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
1117 if (version == NULL)
1118 return (ISC_R_NOMEMORY);
1120 *versionp = version;
1122 return (ISC_R_SUCCESS);
1126 attachversion(dns_db_t *db, dns_dbversion_t *source,
1127 dns_dbversion_t **targetp)
1129 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1130 rbtdb_version_t *rbtversion = source;
1133 REQUIRE(VALID_RBTDB(rbtdb));
1135 isc_refcount_increment(&rbtversion->references, &refs);
1138 *targetp = rbtversion;
1141 static rbtdb_changed_t *
1142 add_changed(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
1143 dns_rbtnode_t *node)
1145 rbtdb_changed_t *changed;
1149 * Caller must be holding the node lock if its reference must be
1150 * protected by the lock.
1153 changed = isc_mem_get(rbtdb->common.mctx, sizeof(*changed));
1155 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
1157 REQUIRE(version->writer);
1159 if (changed != NULL) {
1160 dns_rbtnode_refincrement(node, &refs);
1162 changed->node = node;
1163 changed->dirty = ISC_FALSE;
1164 ISC_LIST_INITANDAPPEND(version->changed_list, changed, link);
1166 version->commit_ok = ISC_FALSE;
1168 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
1174 free_acachearray(isc_mem_t *mctx, rdatasetheader_t *header,
1179 unsigned char *raw; /* RDATASLAB */
1182 * The caller must be holding the corresponding node lock.
1188 raw = (unsigned char *)header + sizeof(*header);
1189 count = raw[0] * 256 + raw[1];
1192 * Sanity check: since an additional cache entry has a reference to
1193 * the original DB node (in the callback arg), there should be no
1194 * acache entries when the node can be freed.
1196 for (i = 0; i < count; i++)
1197 INSIST(array[i].entry == NULL && array[i].cbarg == NULL);
1199 isc_mem_put(mctx, array, count * sizeof(acachectl_t));
1203 free_noqname(isc_mem_t *mctx, struct noqname **noqname) {
1205 if (dns_name_dynamic(&(*noqname)->name))
1206 dns_name_free(&(*noqname)->name, mctx);
1207 if ((*noqname)->neg != NULL)
1208 isc_mem_put(mctx, (*noqname)->neg,
1209 dns_rdataslab_size((*noqname)->neg, 0));
1210 if ((*noqname)->negsig != NULL)
1211 isc_mem_put(mctx, (*noqname)->negsig,
1212 dns_rdataslab_size((*noqname)->negsig, 0));
1213 isc_mem_put(mctx, *noqname, sizeof(**noqname));
1218 init_rdataset(dns_rbtdb_t *rbtdb, rdatasetheader_t *h)
1220 ISC_LINK_INIT(h, link);
1224 if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in)
1225 fprintf(stderr, "initialized header: %p\n", h);
1231 static inline rdatasetheader_t *
1232 new_rdataset(dns_rbtdb_t *rbtdb, isc_mem_t *mctx)
1234 rdatasetheader_t *h;
1236 h = isc_mem_get(mctx, sizeof(*h));
1241 if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in)
1242 fprintf(stderr, "allocated header: %p\n", h);
1244 init_rdataset(rbtdb, h);
1249 free_rdataset(dns_rbtdb_t *rbtdb, isc_mem_t *mctx, rdatasetheader_t *rdataset)
1254 if (EXISTS(rdataset) &&
1255 (rdataset->attributes & RDATASET_ATTR_STATCOUNT) != 0) {
1256 update_rrsetstats(rbtdb, rdataset, ISC_FALSE);
1259 idx = rdataset->node->locknum;
1260 if (ISC_LINK_LINKED(rdataset, link)) {
1261 INSIST(IS_CACHE(rbtdb));
1262 ISC_LIST_UNLINK(rbtdb->rdatasets[idx], rdataset, link);
1264 if (rdataset->heap_index != 0)
1265 isc_heap_delete(rbtdb->heaps[idx], rdataset->heap_index);
1266 rdataset->heap_index = 0;
1268 if (rdataset->noqname != NULL)
1269 free_noqname(mctx, &rdataset->noqname);
1270 if (rdataset->closest != NULL)
1271 free_noqname(mctx, &rdataset->closest);
1273 free_acachearray(mctx, rdataset, rdataset->additional_auth);
1274 free_acachearray(mctx, rdataset, rdataset->additional_glue);
1276 if ((rdataset->attributes & RDATASET_ATTR_NONEXISTENT) != 0)
1277 size = sizeof(*rdataset);
1279 size = dns_rdataslab_size((unsigned char *)rdataset,
1281 isc_mem_put(mctx, rdataset, size);
1285 rollback_node(dns_rbtnode_t *node, rbtdb_serial_t serial) {
1286 rdatasetheader_t *header, *dcurrent;
1287 isc_boolean_t make_dirty = ISC_FALSE;
1290 * Caller must hold the node lock.
1294 * We set the IGNORE attribute on rdatasets with serial number
1295 * 'serial'. When the reference count goes to zero, these rdatasets
1296 * will be cleaned up; until that time, they will be ignored.
1298 for (header = node->data; header != NULL; header = header->next) {
1299 if (header->serial == serial) {
1300 header->attributes |= RDATASET_ATTR_IGNORE;
1301 make_dirty = ISC_TRUE;
1303 for (dcurrent = header->down;
1305 dcurrent = dcurrent->down) {
1306 if (dcurrent->serial == serial) {
1307 dcurrent->attributes |= RDATASET_ATTR_IGNORE;
1308 make_dirty = ISC_TRUE;
1317 clean_stale_headers(dns_rbtdb_t *rbtdb, isc_mem_t *mctx, rdatasetheader_t *top)
1319 rdatasetheader_t *d, *down_next;
1321 for (d = top->down; d != NULL; d = down_next) {
1322 down_next = d->down;
1323 free_rdataset(rbtdb, mctx, d);
1329 clean_cache_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
1330 rdatasetheader_t *current, *top_prev, *top_next;
1331 isc_mem_t *mctx = rbtdb->common.mctx;
1334 * Caller must be holding the node lock.
1338 for (current = node->data; current != NULL; current = top_next) {
1339 top_next = current->next;
1340 clean_stale_headers(rbtdb, mctx, current);
1342 * If current is nonexistent or stale, we can clean it up.
1344 if ((current->attributes &
1345 (RDATASET_ATTR_NONEXISTENT|RDATASET_ATTR_STALE)) != 0) {
1346 if (top_prev != NULL)
1347 top_prev->next = current->next;
1349 node->data = current->next;
1350 free_rdataset(rbtdb, mctx, current);
1358 clean_zone_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
1359 rbtdb_serial_t least_serial)
1361 rdatasetheader_t *current, *dcurrent, *down_next, *dparent;
1362 rdatasetheader_t *top_prev, *top_next;
1363 isc_mem_t *mctx = rbtdb->common.mctx;
1364 isc_boolean_t still_dirty = ISC_FALSE;
1367 * Caller must be holding the node lock.
1369 REQUIRE(least_serial != 0);
1372 for (current = node->data; current != NULL; current = top_next) {
1373 top_next = current->next;
1376 * First, we clean up any instances of multiple rdatasets
1377 * with the same serial number, or that have the IGNORE
1381 for (dcurrent = current->down;
1383 dcurrent = down_next) {
1384 down_next = dcurrent->down;
1385 INSIST(dcurrent->serial <= dparent->serial);
1386 if (dcurrent->serial == dparent->serial ||
1388 if (down_next != NULL)
1389 down_next->next = dparent;
1390 dparent->down = down_next;
1391 free_rdataset(rbtdb, mctx, dcurrent);
1397 * We've now eliminated all IGNORE datasets with the possible
1398 * exception of current, which we now check.
1400 if (IGNORE(current)) {
1401 down_next = current->down;
1402 if (down_next == NULL) {
1403 if (top_prev != NULL)
1404 top_prev->next = current->next;
1406 node->data = current->next;
1407 free_rdataset(rbtdb, mctx, current);
1409 * current no longer exists, so we can
1410 * just continue with the loop.
1415 * Pull up current->down, making it the new
1418 if (top_prev != NULL)
1419 top_prev->next = down_next;
1421 node->data = down_next;
1422 down_next->next = top_next;
1423 free_rdataset(rbtdb, mctx, current);
1424 current = down_next;
1429 * We now try to find the first down node less than the
1433 for (dcurrent = current->down;
1435 dcurrent = down_next) {
1436 down_next = dcurrent->down;
1437 if (dcurrent->serial < least_serial)
1443 * If there is a such an rdataset, delete it and any older
1446 if (dcurrent != NULL) {
1448 down_next = dcurrent->down;
1449 INSIST(dcurrent->serial <= least_serial);
1450 free_rdataset(rbtdb, mctx, dcurrent);
1451 dcurrent = down_next;
1452 } while (dcurrent != NULL);
1453 dparent->down = NULL;
1457 * Note. The serial number of 'current' might be less than
1458 * least_serial too, but we cannot delete it because it is
1459 * the most recent version, unless it is a NONEXISTENT
1462 if (current->down != NULL) {
1463 still_dirty = ISC_TRUE;
1467 * If this is a NONEXISTENT rdataset, we can delete it.
1469 if (NONEXISTENT(current)) {
1470 if (top_prev != NULL)
1471 top_prev->next = current->next;
1473 node->data = current->next;
1474 free_rdataset(rbtdb, mctx, current);
1484 * Clean up dead nodes. These are nodes which have no references, and
1485 * have no data. They are dead but we could not or chose not to delete
1486 * them when we deleted all the data at that node because we did not want
1487 * to wait for the tree write lock.
1489 * The caller must hold a tree write lock and bucketnum'th node (write) lock.
1492 cleanup_dead_nodes(dns_rbtdb_t *rbtdb, int bucketnum) {
1493 dns_rbtnode_t *node;
1494 isc_result_t result;
1495 int count = 10; /* XXXJT: should be adjustable */
1497 node = ISC_LIST_HEAD(rbtdb->deadnodes[bucketnum]);
1498 while (node != NULL && count > 0) {
1499 ISC_LIST_UNLINK(rbtdb->deadnodes[bucketnum], node, deadlink);
1502 * Since we're holding a tree write lock, it should be
1503 * impossible for this node to be referenced by others.
1505 INSIST(dns_rbtnode_refcurrent(node) == 0 &&
1506 node->data == NULL);
1508 INSIST(!ISC_LINK_LINKED(node, deadlink));
1510 result = dns_rbt_deletenode(rbtdb->nsec3, node,
1513 result = dns_rbt_deletenode(rbtdb->tree, node,
1515 if (result != ISC_R_SUCCESS)
1516 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
1517 DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
1518 "cleanup_dead_nodes: "
1519 "dns_rbt_deletenode: %s",
1520 isc_result_totext(result));
1521 node = ISC_LIST_HEAD(rbtdb->deadnodes[bucketnum]);
1527 * Caller must be holding the node lock if its reference must be protected
1531 new_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
1532 unsigned int lockrefs, noderefs;
1533 isc_refcount_t *lockref;
1535 dns_rbtnode_refincrement0(node, &noderefs);
1536 if (noderefs == 1) { /* this is the first reference to the node */
1537 lockref = &rbtdb->node_locks[node->locknum].references;
1538 isc_refcount_increment0(lockref, &lockrefs);
1539 INSIST(lockrefs != 0);
1541 INSIST(noderefs != 0);
1545 * This function is assumed to be called when a node is newly referenced
1546 * and can be in the deadnode list. In that case the node must be retrieved
1547 * from the list because it is going to be used. In addition, if the caller
1548 * happens to hold a write lock on the tree, it's a good chance to purge dead
1550 * Note: while a new reference is gained in multiple places, there are only very
1551 * few cases where the node can be in the deadnode list (only empty nodes can
1552 * have been added to the list).
1555 reactivate_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
1556 isc_rwlocktype_t treelocktype)
1558 isc_boolean_t need_relock = ISC_FALSE;
1560 NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
1561 new_reference(rbtdb, node);
1563 NODE_WEAKLOCK(&rbtdb->node_locks[node->locknum].lock,
1564 isc_rwlocktype_read);
1565 if (ISC_LINK_LINKED(node, deadlink))
1566 need_relock = ISC_TRUE;
1567 else if (!ISC_LIST_EMPTY(rbtdb->deadnodes[node->locknum]) &&
1568 treelocktype == isc_rwlocktype_write)
1569 need_relock = ISC_TRUE;
1570 NODE_WEAKUNLOCK(&rbtdb->node_locks[node->locknum].lock,
1571 isc_rwlocktype_read);
1573 NODE_WEAKLOCK(&rbtdb->node_locks[node->locknum].lock,
1574 isc_rwlocktype_write);
1575 if (ISC_LINK_LINKED(node, deadlink))
1576 ISC_LIST_UNLINK(rbtdb->deadnodes[node->locknum],
1578 if (treelocktype == isc_rwlocktype_write)
1579 cleanup_dead_nodes(rbtdb, node->locknum);
1580 NODE_WEAKUNLOCK(&rbtdb->node_locks[node->locknum].lock,
1581 isc_rwlocktype_write);
1584 NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
1588 * Caller must be holding the node lock; either the "strong", read or write
1589 * lock. Note that the lock must be held even when node references are
1590 * atomically modified; in that case the decrement operation itself does not
1591 * have to be protected, but we must avoid a race condition where multiple
1592 * threads are decreasing the reference to zero simultaneously and at least
1593 * one of them is going to free the node.
1594 * This function returns ISC_TRUE if and only if the node reference decreases
1597 static isc_boolean_t
1598 decrement_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
1599 rbtdb_serial_t least_serial,
1600 isc_rwlocktype_t nlock, isc_rwlocktype_t tlock,
1601 isc_boolean_t pruning)
1603 isc_result_t result;
1604 isc_boolean_t write_locked;
1605 rbtdb_nodelock_t *nodelock;
1606 unsigned int refs, nrefs;
1607 int bucket = node->locknum;
1608 isc_boolean_t no_reference;
1610 nodelock = &rbtdb->node_locks[bucket];
1612 /* Handle easy and typical case first. */
1613 if (!node->dirty && (node->data != NULL || node->down != NULL)) {
1614 dns_rbtnode_refdecrement(node, &nrefs);
1615 INSIST((int)nrefs >= 0);
1617 isc_refcount_decrement(&nodelock->references, &refs);
1618 INSIST((int)refs >= 0);
1620 return ((nrefs == 0) ? ISC_TRUE : ISC_FALSE);
1623 /* Upgrade the lock? */
1624 if (nlock == isc_rwlocktype_read) {
1625 NODE_WEAKUNLOCK(&nodelock->lock, isc_rwlocktype_read);
1626 NODE_WEAKLOCK(&nodelock->lock, isc_rwlocktype_write);
1628 dns_rbtnode_refdecrement(node, &nrefs);
1629 INSIST((int)nrefs >= 0);
1631 /* Restore the lock? */
1632 if (nlock == isc_rwlocktype_read)
1633 NODE_WEAKDOWNGRADE(&nodelock->lock);
1637 if (node->dirty && dns_rbtnode_refcurrent(node) == 0) {
1638 if (IS_CACHE(rbtdb))
1639 clean_cache_node(rbtdb, node);
1641 if (least_serial == 0) {
1643 * Caller doesn't know the least serial.
1646 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
1647 least_serial = rbtdb->least_serial;
1648 RBTDB_UNLOCK(&rbtdb->lock,
1649 isc_rwlocktype_read);
1651 clean_zone_node(rbtdb, node, least_serial);
1655 isc_refcount_decrement(&nodelock->references, &refs);
1656 INSIST((int)refs >= 0);
1659 * XXXDCL should this only be done for cache zones?
1661 if (node->data != NULL || node->down != NULL) {
1662 /* Restore the lock? */
1663 if (nlock == isc_rwlocktype_read)
1664 NODE_WEAKDOWNGRADE(&nodelock->lock);
1669 * Attempt to switch to a write lock on the tree. If this fails,
1670 * we will add this node to a linked list of nodes in this locking
1671 * bucket which we will free later.
1673 if (tlock != isc_rwlocktype_write) {
1675 * Locking hierarchy notwithstanding, we don't need to free
1676 * the node lock before acquiring the tree write lock because
1677 * we only do a trylock.
1679 if (tlock == isc_rwlocktype_read)
1680 result = isc_rwlock_tryupgrade(&rbtdb->tree_lock);
1682 result = isc_rwlock_trylock(&rbtdb->tree_lock,
1683 isc_rwlocktype_write);
1684 RUNTIME_CHECK(result == ISC_R_SUCCESS ||
1685 result == ISC_R_LOCKBUSY);
1687 write_locked = ISC_TF(result == ISC_R_SUCCESS);
1689 write_locked = ISC_TRUE;
1691 no_reference = ISC_TRUE;
1692 if (write_locked && dns_rbtnode_refcurrent(node) == 0) {
1694 * We can now delete the node if the reference counter is
1695 * zero. This should be typically the case, but a different
1696 * thread may still gain a (new) reference just before the
1697 * current thread locks the tree (e.g., in findnode()).
1701 * If this node is the only one in the level it's in, deleting
1702 * this node may recursively make its parent the only node in
1703 * the parent level; if so, and if no one is currently using
1704 * the parent node, this is almost the only opportunity to
1705 * clean it up. But the recursive cleanup is not that trivial
1706 * since the child and parent may be in different lock buckets,
1707 * which would cause a lock order reversal problem. To avoid
1708 * the trouble, we'll dispatch a separate event for batch
1709 * cleaning. We need to check whether we're deleting the node
1710 * as a result of pruning to avoid infinite dispatching.
1711 * Note: pruning happens only when a task has been set for the
1712 * rbtdb. If the user of the rbtdb chooses not to set a task,
1713 * it's their responsibility to purge stale leaves (e.g. by
1714 * periodic walk-through).
1716 if (!pruning && node->parent != NULL &&
1717 node->parent->down == node && node->left == NULL &&
1718 node->right == NULL && rbtdb->task != NULL) {
1722 ev = isc_event_allocate(rbtdb->common.mctx, NULL,
1725 sizeof(isc_event_t));
1727 new_reference(rbtdb, node);
1729 attach((dns_db_t *)rbtdb, &db);
1731 isc_task_send(rbtdb->task, &ev);
1732 no_reference = ISC_FALSE;
1735 * XXX: this is a weird situation. We could
1736 * ignore this error case, but then the stale
1737 * node will unlikely be purged except via a
1738 * rare condition such as manual cleanup. So
1739 * we queue it in the deadnodes list, hoping
1740 * the memory shortage is temporary and the node
1741 * will be deleted later.
1743 isc_log_write(dns_lctx,
1744 DNS_LOGCATEGORY_DATABASE,
1745 DNS_LOGMODULE_CACHE,
1747 "decrement_reference: failed to "
1748 "allocate pruning event");
1749 INSIST(!ISC_LINK_LINKED(node, deadlink));
1750 ISC_LIST_APPEND(rbtdb->deadnodes[bucket], node,
1754 if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(1))) {
1755 char printname[DNS_NAME_FORMATSIZE];
1757 isc_log_write(dns_lctx,
1758 DNS_LOGCATEGORY_DATABASE,
1759 DNS_LOGMODULE_CACHE,
1761 "decrement_reference: "
1762 "delete from rbt: %p %s",
1764 dns_rbt_formatnodename(node,
1766 sizeof(printname)));
1769 INSIST(!ISC_LINK_LINKED(node, deadlink));
1771 result = dns_rbt_deletenode(rbtdb->nsec3, node,
1774 result = dns_rbt_deletenode(rbtdb->tree, node,
1776 if (result != ISC_R_SUCCESS) {
1777 isc_log_write(dns_lctx,
1778 DNS_LOGCATEGORY_DATABASE,
1779 DNS_LOGMODULE_CACHE,
1781 "decrement_reference: "
1782 "dns_rbt_deletenode: %s",
1783 isc_result_totext(result));
1786 } else if (dns_rbtnode_refcurrent(node) == 0) {
1787 INSIST(!ISC_LINK_LINKED(node, deadlink));
1788 ISC_LIST_APPEND(rbtdb->deadnodes[bucket], node, deadlink);
1790 no_reference = ISC_FALSE;
1792 /* Restore the lock? */
1793 if (nlock == isc_rwlocktype_read)
1794 NODE_WEAKDOWNGRADE(&nodelock->lock);
1797 * Relock a read lock, or unlock the write lock if no lock was held.
1799 if (tlock == isc_rwlocktype_none)
1801 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
1803 if (tlock == isc_rwlocktype_read)
1805 isc_rwlock_downgrade(&rbtdb->tree_lock);
1807 return (no_reference);
1811 * Prune the tree by recursively cleaning-up single leaves. In the worst
1812 * case, the number of iteration is the number of tree levels, which is at
1813 * most the maximum number of domain name labels, i.e, 127. In practice, this
1814 * should be much smaller (only a few times), and even the worst case would be
1815 * acceptable for a single event.
1818 prune_tree(isc_task_t *task, isc_event_t *event) {
1819 dns_rbtdb_t *rbtdb = event->ev_sender;
1820 dns_rbtnode_t *node = event->ev_arg;
1821 dns_rbtnode_t *parent;
1822 unsigned int locknum;
1826 isc_event_free(&event);
1828 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
1829 locknum = node->locknum;
1830 NODE_LOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
1832 parent = node->parent;
1833 decrement_reference(rbtdb, node, 0, isc_rwlocktype_write,
1834 isc_rwlocktype_write, ISC_TRUE);
1836 if (parent != NULL && parent->down == NULL) {
1838 * node was the only down child of the parent and has
1839 * just been removed. We'll then need to examine the
1840 * parent. Keep the lock if possible; otherwise,
1841 * release the old lock and acquire one for the parent.
1843 if (parent->locknum != locknum) {
1844 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
1845 isc_rwlocktype_write);
1846 locknum = parent->locknum;
1847 NODE_LOCK(&rbtdb->node_locks[locknum].lock,
1848 isc_rwlocktype_write);
1852 * We need to gain a reference to the node before
1853 * decrementing it in the next iteration. In addition,
1854 * if the node is in the dead-nodes list, extract it
1855 * from the list beforehand as we do in
1856 * reactivate_node().
1858 new_reference(rbtdb, parent);
1859 if (ISC_LINK_LINKED(parent, deadlink)) {
1860 ISC_LIST_UNLINK(rbtdb->deadnodes[locknum],
1867 } while (node != NULL);
1868 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
1869 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
1871 detach((dns_db_t **)&rbtdb);
1875 make_least_version(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
1876 rbtdb_changedlist_t *cleanup_list)
1879 * Caller must be holding the database lock.
1882 rbtdb->least_serial = version->serial;
1883 *cleanup_list = version->changed_list;
1884 ISC_LIST_INIT(version->changed_list);
1888 cleanup_nondirty(rbtdb_version_t *version, rbtdb_changedlist_t *cleanup_list) {
1889 rbtdb_changed_t *changed, *next_changed;
1892 * If the changed record is dirty, then
1893 * an update created multiple versions of
1894 * a given rdataset. We keep this list
1895 * until we're the least open version, at
1896 * which point it's safe to get rid of any
1899 * If the changed record isn't dirty, then
1900 * we don't need it anymore since we're
1901 * committing and not rolling back.
1903 * The caller must be holding the database lock.
1905 for (changed = HEAD(version->changed_list);
1907 changed = next_changed) {
1908 next_changed = NEXT(changed, link);
1909 if (!changed->dirty) {
1910 UNLINK(version->changed_list,
1912 APPEND(*cleanup_list,
1919 iszonesecure(dns_db_t *db, rbtdb_version_t *version, dns_dbnode_t *origin) {
1920 dns_rdataset_t keyset;
1921 dns_rdataset_t nsecset, signsecset;
1922 dns_rdata_t rdata = DNS_RDATA_INIT;
1923 isc_boolean_t haszonekey = ISC_FALSE;
1924 isc_boolean_t hasnsec = ISC_FALSE;
1925 isc_boolean_t hasoptbit = ISC_FALSE;
1926 isc_boolean_t nsec3createflag = ISC_FALSE;
1927 isc_result_t result;
1929 dns_rdataset_init(&keyset);
1930 result = dns_db_findrdataset(db, origin, version, dns_rdatatype_dnskey,
1931 0, 0, &keyset, NULL);
1932 if (result == ISC_R_SUCCESS) {
1933 dns_rdata_t keyrdata = DNS_RDATA_INIT;
1934 result = dns_rdataset_first(&keyset);
1935 while (result == ISC_R_SUCCESS) {
1936 dns_rdataset_current(&keyset, &keyrdata);
1937 if (dns_zonekey_iszonekey(&keyrdata)) {
1938 haszonekey = ISC_TRUE;
1941 result = dns_rdataset_next(&keyset);
1943 dns_rdataset_disassociate(&keyset);
1946 version->secure = dns_db_insecure;
1947 version->havensec3 = ISC_FALSE;
1951 dns_rdataset_init(&nsecset);
1952 dns_rdataset_init(&signsecset);
1953 result = dns_db_findrdataset(db, origin, version, dns_rdatatype_nsec,
1954 0, 0, &nsecset, &signsecset);
1955 if (result == ISC_R_SUCCESS) {
1956 if (dns_rdataset_isassociated(&signsecset)) {
1958 result = dns_rdataset_first(&nsecset);
1959 if (result == ISC_R_SUCCESS) {
1960 dns_rdataset_current(&nsecset, &rdata);
1961 hasoptbit = dns_nsec_typepresent(&rdata,
1964 dns_rdataset_disassociate(&signsecset);
1966 dns_rdataset_disassociate(&nsecset);
1969 setnsec3parameters(db, version, &nsec3createflag);
1972 * Do we have a valid NSEC/NSEC3 chain?
1974 if (version->havensec3 || (hasnsec && !hasoptbit))
1975 version->secure = dns_db_secure;
1977 * Do we have a NSEC/NSEC3 chain under creation?
1979 else if (hasoptbit || nsec3createflag)
1980 version->secure = dns_db_partial;
1982 version->secure = dns_db_insecure;
1986 * Walk the origin node looking for NSEC3PARAM records.
1987 * Cache the nsec3 parameters.
1990 setnsec3parameters(dns_db_t *db, rbtdb_version_t *version,
1991 isc_boolean_t *nsec3createflag)
1993 dns_rbtnode_t *node;
1994 dns_rdata_nsec3param_t nsec3param;
1995 dns_rdata_t rdata = DNS_RDATA_INIT;
1996 isc_region_t region;
1997 isc_result_t result;
1998 rdatasetheader_t *header, *header_next;
1999 unsigned char *raw; /* RDATASLAB */
2000 unsigned int count, length;
2001 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
2003 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
2004 version->havensec3 = ISC_FALSE;
2005 node = rbtdb->origin_node;
2006 NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
2007 isc_rwlocktype_read);
2008 for (header = node->data;
2010 header = header_next) {
2011 header_next = header->next;
2013 if (header->serial <= version->serial &&
2015 if (NONEXISTENT(header))
2019 header = header->down;
2020 } while (header != NULL);
2022 if (header != NULL &&
2023 header->type == dns_rdatatype_nsec3param) {
2025 * Find A NSEC3PARAM with a supported algorithm.
2027 raw = (unsigned char *)header + sizeof(*header);
2028 count = raw[0] * 256 + raw[1]; /* count */
2029 #if DNS_RDATASET_FIXED
2030 raw += count * 4 + 2;
2034 while (count-- > 0U) {
2035 length = raw[0] * 256 + raw[1];
2036 #if DNS_RDATASET_FIXED
2042 region.length = length;
2044 dns_rdata_fromregion(&rdata,
2045 rbtdb->common.rdclass,
2046 dns_rdatatype_nsec3param,
2048 result = dns_rdata_tostruct(&rdata,
2051 INSIST(result == ISC_R_SUCCESS);
2052 dns_rdata_reset(&rdata);
2054 if (nsec3param.hash != DNS_NSEC3_UNKNOWNALG &&
2055 !dns_nsec3_supportedhash(nsec3param.hash))
2058 #ifdef RFC5155_STRICT
2059 if (nsec3param.flags != 0)
2062 if ((nsec3param.flags & DNS_NSEC3FLAG_CREATE)
2064 *nsec3createflag = ISC_TRUE;
2065 if ((nsec3param.flags & ~DNS_NSEC3FLAG_OPTOUT)
2070 memcpy(version->salt, nsec3param.salt,
2071 nsec3param.salt_length);
2072 version->hash = nsec3param.hash;
2073 version->salt_length = nsec3param.salt_length;
2074 version->iterations = nsec3param.iterations;
2075 version->flags = nsec3param.flags;
2076 version->havensec3 = ISC_TRUE;
2078 * Look for a better algorithm than the
2079 * unknown test algorithm.
2081 if (nsec3param.hash != DNS_NSEC3_UNKNOWNALG)
2087 NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
2088 isc_rwlocktype_read);
2089 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
2093 closeversion(dns_db_t *db, dns_dbversion_t **versionp, isc_boolean_t commit) {
2094 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
2095 rbtdb_version_t *version, *cleanup_version, *least_greater;
2096 isc_boolean_t rollback = ISC_FALSE;
2097 rbtdb_changedlist_t cleanup_list;
2098 rdatasetheaderlist_t resigned_list;
2099 rbtdb_changed_t *changed, *next_changed;
2100 rbtdb_serial_t serial, least_serial;
2101 dns_rbtnode_t *rbtnode;
2103 rdatasetheader_t *header;
2104 isc_boolean_t writer;
2106 REQUIRE(VALID_RBTDB(rbtdb));
2107 version = (rbtdb_version_t *)*versionp;
2109 cleanup_version = NULL;
2110 ISC_LIST_INIT(cleanup_list);
2111 ISC_LIST_INIT(resigned_list);
2113 isc_refcount_decrement(&version->references, &refs);
2114 if (refs > 0) { /* typical and easy case first */
2116 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
2117 INSIST(!version->writer);
2118 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
2123 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
2124 serial = version->serial;
2125 writer = version->writer;
2126 if (version->writer) {
2129 rbtdb_version_t *cur_version;
2131 INSIST(version->commit_ok);
2132 INSIST(version == rbtdb->future_version);
2134 * The current version is going to be replaced.
2135 * Release the (likely last) reference to it from the
2136 * DB itself and unlink it from the open list.
2138 cur_version = rbtdb->current_version;
2139 isc_refcount_decrement(&cur_version->references,
2142 if (cur_version->serial == rbtdb->least_serial)
2143 INSIST(EMPTY(cur_version->changed_list));
2144 UNLINK(rbtdb->open_versions,
2147 if (EMPTY(rbtdb->open_versions)) {
2149 * We're going to become the least open
2152 make_least_version(rbtdb, version,
2156 * Some other open version is the
2157 * least version. We can't cleanup
2158 * records that were changed in this
2159 * version because the older versions
2160 * may still be in use by an open
2163 * We can, however, discard the
2164 * changed records for things that
2165 * we've added that didn't exist in
2168 cleanup_nondirty(version, &cleanup_list);
2171 * If the (soon to be former) current version
2172 * isn't being used by anyone, we can clean
2176 cleanup_version = cur_version;
2177 APPENDLIST(version->changed_list,
2178 cleanup_version->changed_list,
2182 * Become the current version.
2184 version->writer = ISC_FALSE;
2185 rbtdb->current_version = version;
2186 rbtdb->current_serial = version->serial;
2187 rbtdb->future_version = NULL;
2190 * Keep the current version in the open list, and
2191 * gain a reference for the DB itself (see the DB
2192 * creation function below). This must be the only
2193 * case where we need to increment the counter from
2194 * zero and need to use isc_refcount_increment0().
2196 isc_refcount_increment0(&version->references,
2198 INSIST(cur_ref == 1);
2199 PREPEND(rbtdb->open_versions,
2200 rbtdb->current_version, link);
2201 resigned_list = version->resigned_list;
2202 ISC_LIST_INIT(version->resigned_list);
2205 * We're rolling back this transaction.
2207 cleanup_list = version->changed_list;
2208 ISC_LIST_INIT(version->changed_list);
2209 resigned_list = version->resigned_list;
2210 ISC_LIST_INIT(version->resigned_list);
2211 rollback = ISC_TRUE;
2212 cleanup_version = version;
2213 rbtdb->future_version = NULL;
2216 if (version != rbtdb->current_version) {
2218 * There are no external or internal references
2219 * to this version and it can be cleaned up.
2221 cleanup_version = version;
2224 * Find the version with the least serial
2225 * number greater than ours.
2227 least_greater = PREV(version, link);
2228 if (least_greater == NULL)
2229 least_greater = rbtdb->current_version;
2231 INSIST(version->serial < least_greater->serial);
2233 * Is this the least open version?
2235 if (version->serial == rbtdb->least_serial) {
2237 * Yes. Install the new least open
2240 make_least_version(rbtdb,
2245 * Add any unexecuted cleanups to
2246 * those of the least greater version.
2248 APPENDLIST(least_greater->changed_list,
2249 version->changed_list,
2252 } else if (version->serial == rbtdb->least_serial)
2253 INSIST(EMPTY(version->changed_list));
2254 UNLINK(rbtdb->open_versions, version, link);
2256 least_serial = rbtdb->least_serial;
2257 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
2260 * Update the zone's secure status.
2262 if (writer && commit && !IS_CACHE(rbtdb))
2263 iszonesecure(db, version, rbtdb->origin_node);
2265 if (cleanup_version != NULL) {
2266 INSIST(EMPTY(cleanup_version->changed_list));
2267 isc_mem_put(rbtdb->common.mctx, cleanup_version,
2268 sizeof(*cleanup_version));
2272 * Commit/rollback re-signed headers.
2274 for (header = HEAD(resigned_list);
2276 header = HEAD(resigned_list)) {
2279 ISC_LIST_UNLINK(resigned_list, header, link);
2281 lock = &rbtdb->node_locks[header->node->locknum].lock;
2282 NODE_LOCK(lock, isc_rwlocktype_write);
2284 resign_insert(rbtdb, header->node->locknum, header);
2285 decrement_reference(rbtdb, header->node, least_serial,
2286 isc_rwlocktype_write, isc_rwlocktype_none,
2288 NODE_UNLOCK(lock, isc_rwlocktype_write);
2291 if (!EMPTY(cleanup_list)) {
2293 * We acquire a tree write lock here in order to make sure
2294 * that stale nodes will be removed in decrement_reference().
2295 * If we didn't have the lock, those nodes could miss the
2296 * chance to be removed until the server stops. The write lock
2297 * is expensive, but this event should be rare enough to justify
2300 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
2301 for (changed = HEAD(cleanup_list);
2303 changed = next_changed) {
2306 next_changed = NEXT(changed, link);
2307 rbtnode = changed->node;
2308 lock = &rbtdb->node_locks[rbtnode->locknum].lock;
2310 NODE_LOCK(lock, isc_rwlocktype_write);
2312 * This is a good opportunity to purge any dead nodes,
2315 cleanup_dead_nodes(rbtdb, rbtnode->locknum);
2318 rollback_node(rbtnode, serial);
2319 decrement_reference(rbtdb, rbtnode, least_serial,
2320 isc_rwlocktype_write,
2321 isc_rwlocktype_write, ISC_FALSE);
2323 NODE_UNLOCK(lock, isc_rwlocktype_write);
2325 isc_mem_put(rbtdb->common.mctx, changed,
2328 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
2336 * Add the necessary magic for the wildcard name 'name'
2337 * to be found in 'rbtdb'.
2339 * In order for wildcard matching to work correctly in
2340 * zone_find(), we must ensure that a node for the wildcarding
2341 * level exists in the database, and has its 'find_callback'
2342 * and 'wild' bits set.
2344 * E.g. if the wildcard name is "*.sub.example." then we
2345 * must ensure that "sub.example." exists and is marked as
2349 add_wildcard_magic(dns_rbtdb_t *rbtdb, dns_name_t *name) {
2350 isc_result_t result;
2351 dns_name_t foundname;
2352 dns_offsets_t offsets;
2354 dns_rbtnode_t *node = NULL;
2356 dns_name_init(&foundname, offsets);
2357 n = dns_name_countlabels(name);
2360 dns_name_getlabelsequence(name, 1, n, &foundname);
2361 result = dns_rbt_addnode(rbtdb->tree, &foundname, &node);
2362 if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS)
2365 node->find_callback = 1;
2367 return (ISC_R_SUCCESS);
2371 add_empty_wildcards(dns_rbtdb_t *rbtdb, dns_name_t *name) {
2372 isc_result_t result;
2373 dns_name_t foundname;
2374 dns_offsets_t offsets;
2375 unsigned int n, l, i;
2377 dns_name_init(&foundname, offsets);
2378 n = dns_name_countlabels(name);
2379 l = dns_name_countlabels(&rbtdb->common.origin);
2382 dns_rbtnode_t *node = NULL; /* dummy */
2383 dns_name_getlabelsequence(name, n - i, i, &foundname);
2384 if (dns_name_iswildcard(&foundname)) {
2385 result = add_wildcard_magic(rbtdb, &foundname);
2386 if (result != ISC_R_SUCCESS)
2388 result = dns_rbt_addnode(rbtdb->tree, &foundname,
2390 if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS)
2396 return (ISC_R_SUCCESS);
2400 findnode(dns_db_t *db, dns_name_t *name, isc_boolean_t create,
2401 dns_dbnode_t **nodep)
2403 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
2404 dns_rbtnode_t *node = NULL;
2405 dns_name_t nodename;
2406 isc_result_t result;
2407 isc_rwlocktype_t locktype = isc_rwlocktype_read;
2409 REQUIRE(VALID_RBTDB(rbtdb));
2411 dns_name_init(&nodename, NULL);
2412 RWLOCK(&rbtdb->tree_lock, locktype);
2413 result = dns_rbt_findnode(rbtdb->tree, name, NULL, &node, NULL,
2414 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
2415 if (result != ISC_R_SUCCESS) {
2416 RWUNLOCK(&rbtdb->tree_lock, locktype);
2418 if (result == DNS_R_PARTIALMATCH)
2419 result = ISC_R_NOTFOUND;
2423 * It would be nice to try to upgrade the lock instead of
2424 * unlocking then relocking.
2426 locktype = isc_rwlocktype_write;
2427 RWLOCK(&rbtdb->tree_lock, locktype);
2429 result = dns_rbt_addnode(rbtdb->tree, name, &node);
2430 if (result == ISC_R_SUCCESS) {
2431 dns_rbt_namefromnode(node, &nodename);
2432 #ifdef DNS_RBT_USEHASH
2433 node->locknum = node->hashval % rbtdb->node_lock_count;
2435 node->locknum = dns_name_hash(&nodename, ISC_TRUE) %
2436 rbtdb->node_lock_count;
2439 add_empty_wildcards(rbtdb, name);
2441 if (dns_name_iswildcard(name)) {
2442 result = add_wildcard_magic(rbtdb, name);
2443 if (result != ISC_R_SUCCESS) {
2444 RWUNLOCK(&rbtdb->tree_lock, locktype);
2448 } else if (result != ISC_R_EXISTS) {
2449 RWUNLOCK(&rbtdb->tree_lock, locktype);
2453 reactivate_node(rbtdb, node, locktype);
2454 RWUNLOCK(&rbtdb->tree_lock, locktype);
2456 *nodep = (dns_dbnode_t *)node;
2458 return (ISC_R_SUCCESS);
2462 findnsec3node(dns_db_t *db, dns_name_t *name, isc_boolean_t create,
2463 dns_dbnode_t **nodep)
2465 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
2466 dns_rbtnode_t *node = NULL;
2467 dns_name_t nodename;
2468 isc_result_t result;
2469 isc_rwlocktype_t locktype = isc_rwlocktype_read;
2471 REQUIRE(VALID_RBTDB(rbtdb));
2473 dns_name_init(&nodename, NULL);
2474 RWLOCK(&rbtdb->tree_lock, locktype);
2475 result = dns_rbt_findnode(rbtdb->nsec3, name, NULL, &node, NULL,
2476 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
2477 if (result != ISC_R_SUCCESS) {
2478 RWUNLOCK(&rbtdb->tree_lock, locktype);
2480 if (result == DNS_R_PARTIALMATCH)
2481 result = ISC_R_NOTFOUND;
2485 * It would be nice to try to upgrade the lock instead of
2486 * unlocking then relocking.
2488 locktype = isc_rwlocktype_write;
2489 RWLOCK(&rbtdb->tree_lock, locktype);
2491 result = dns_rbt_addnode(rbtdb->nsec3, name, &node);
2492 if (result == ISC_R_SUCCESS) {
2493 dns_rbt_namefromnode(node, &nodename);
2494 #ifdef DNS_RBT_USEHASH
2495 node->locknum = node->hashval % rbtdb->node_lock_count;
2497 node->locknum = dns_name_hash(&nodename, ISC_TRUE) %
2498 rbtdb->node_lock_count;
2501 } else if (result != ISC_R_EXISTS) {
2502 RWUNLOCK(&rbtdb->tree_lock, locktype);
2506 INSIST(node->nsec3);
2507 NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
2508 new_reference(rbtdb, node);
2509 NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
2510 RWUNLOCK(&rbtdb->tree_lock, locktype);
2512 *nodep = (dns_dbnode_t *)node;
2514 return (ISC_R_SUCCESS);
2518 zone_zonecut_callback(dns_rbtnode_t *node, dns_name_t *name, void *arg) {
2519 rbtdb_search_t *search = arg;
2520 rdatasetheader_t *header, *header_next;
2521 rdatasetheader_t *dname_header, *sigdname_header, *ns_header;
2522 rdatasetheader_t *found;
2523 isc_result_t result;
2524 dns_rbtnode_t *onode;
2527 * We only want to remember the topmost zone cut, since it's the one
2528 * that counts, so we'll just continue if we've already found a
2531 if (search->zonecut != NULL)
2532 return (DNS_R_CONTINUE);
2535 result = DNS_R_CONTINUE;
2536 onode = search->rbtdb->origin_node;
2538 NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
2539 isc_rwlocktype_read);
2542 * Look for an NS or DNAME rdataset active in our version.
2545 dname_header = NULL;
2546 sigdname_header = NULL;
2547 for (header = node->data; header != NULL; header = header_next) {
2548 header_next = header->next;
2549 if (header->type == dns_rdatatype_ns ||
2550 header->type == dns_rdatatype_dname ||
2551 header->type == RBTDB_RDATATYPE_SIGDNAME) {
2553 if (header->serial <= search->serial &&
2556 * Is this a "this rdataset doesn't
2559 if (NONEXISTENT(header))
2563 header = header->down;
2564 } while (header != NULL);
2565 if (header != NULL) {
2566 if (header->type == dns_rdatatype_dname)
2567 dname_header = header;
2568 else if (header->type ==
2569 RBTDB_RDATATYPE_SIGDNAME)
2570 sigdname_header = header;
2571 else if (node != onode ||
2572 IS_STUB(search->rbtdb)) {
2574 * We've found an NS rdataset that
2575 * isn't at the origin node. We check
2576 * that they're not at the origin node,
2577 * because otherwise we'd erroneously
2578 * treat the zone top as if it were
2588 * Did we find anything?
2590 if (dname_header != NULL) {
2592 * Note that DNAME has precedence over NS if both exist.
2594 found = dname_header;
2595 search->zonecut_sigrdataset = sigdname_header;
2596 } else if (ns_header != NULL) {
2598 search->zonecut_sigrdataset = NULL;
2601 if (found != NULL) {
2603 * We increment the reference count on node to ensure that
2604 * search->zonecut_rdataset will still be valid later.
2606 new_reference(search->rbtdb, node);
2607 search->zonecut = node;
2608 search->zonecut_rdataset = found;
2609 search->need_cleanup = ISC_TRUE;
2611 * Since we've found a zonecut, anything beneath it is
2612 * glue and is not subject to wildcard matching, so we
2613 * may clear search->wild.
2615 search->wild = ISC_FALSE;
2616 if ((search->options & DNS_DBFIND_GLUEOK) == 0) {
2618 * If the caller does not want to find glue, then
2619 * this is the best answer and the search should
2622 result = DNS_R_PARTIALMATCH;
2627 * The search will continue beneath the zone cut.
2628 * This may or may not be the best match. In case it
2629 * is, we need to remember the node name.
2631 zcname = dns_fixedname_name(&search->zonecut_name);
2632 RUNTIME_CHECK(dns_name_copy(name, zcname, NULL) ==
2634 search->copy_name = ISC_TRUE;
2638 * There is no zonecut at this node which is active in this
2641 * If this is a "wild" node and the caller hasn't disabled
2642 * wildcard matching, remember that we've seen a wild node
2643 * in case we need to go searching for wildcard matches
2646 if (node->wild && (search->options & DNS_DBFIND_NOWILD) == 0)
2647 search->wild = ISC_TRUE;
2650 NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
2651 isc_rwlocktype_read);
2657 bind_rdataset(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
2658 rdatasetheader_t *header, isc_stdtime_t now,
2659 dns_rdataset_t *rdataset)
2661 unsigned char *raw; /* RDATASLAB */
2664 * Caller must be holding the node reader lock.
2665 * XXXJT: technically, we need a writer lock, since we'll increment
2666 * the header count below. However, since the actual counter value
2667 * doesn't matter, we prioritize performance here. (We may want to
2668 * use atomic increment when available).
2671 if (rdataset == NULL)
2674 new_reference(rbtdb, node);
2676 INSIST(rdataset->methods == NULL); /* We must be disassociated. */
2678 rdataset->methods = &rdataset_methods;
2679 rdataset->rdclass = rbtdb->common.rdclass;
2680 rdataset->type = RBTDB_RDATATYPE_BASE(header->type);
2681 rdataset->covers = RBTDB_RDATATYPE_EXT(header->type);
2682 rdataset->ttl = header->rdh_ttl - now;
2683 rdataset->trust = header->trust;
2684 if (NXDOMAIN(header))
2685 rdataset->attributes |= DNS_RDATASETATTR_NXDOMAIN;
2687 rdataset->attributes |= DNS_RDATASETATTR_OPTOUT;
2688 rdataset->private1 = rbtdb;
2689 rdataset->private2 = node;
2690 raw = (unsigned char *)header + sizeof(*header);
2691 rdataset->private3 = raw;
2692 rdataset->count = header->count++;
2693 if (rdataset->count == ISC_UINT32_MAX)
2694 rdataset->count = 0;
2697 * Reset iterator state.
2699 rdataset->privateuint4 = 0;
2700 rdataset->private5 = NULL;
2703 * Add noqname proof.
2705 rdataset->private6 = header->noqname;
2706 if (rdataset->private6 != NULL)
2707 rdataset->attributes |= DNS_RDATASETATTR_NOQNAME;
2708 rdataset->private7 = header->closest;
2709 if (rdataset->private7 != NULL)
2710 rdataset->attributes |= DNS_RDATASETATTR_CLOSEST;
2713 * Copy out re-signing information.
2715 if (RESIGN(header)) {
2716 rdataset->attributes |= DNS_RDATASETATTR_RESIGN;
2717 rdataset->resign = header->resign;
2719 rdataset->resign = 0;
2722 static inline isc_result_t
2723 setup_delegation(rbtdb_search_t *search, dns_dbnode_t **nodep,
2724 dns_name_t *foundname, dns_rdataset_t *rdataset,
2725 dns_rdataset_t *sigrdataset)
2727 isc_result_t result;
2729 rbtdb_rdatatype_t type;
2730 dns_rbtnode_t *node;
2733 * The caller MUST NOT be holding any node locks.
2736 node = search->zonecut;
2737 type = search->zonecut_rdataset->type;
2740 * If we have to set foundname, we do it before anything else.
2741 * If we were to set foundname after we had set nodep or bound the
2742 * rdataset, then we'd have to undo that work if dns_name_copy()
2743 * failed. By setting foundname first, there's nothing to undo if
2746 if (foundname != NULL && search->copy_name) {
2747 zcname = dns_fixedname_name(&search->zonecut_name);
2748 result = dns_name_copy(zcname, foundname, NULL);
2749 if (result != ISC_R_SUCCESS)
2752 if (nodep != NULL) {
2754 * Note that we don't have to increment the node's reference
2755 * count here because we're going to use the reference we
2756 * already have in the search block.
2759 search->need_cleanup = ISC_FALSE;
2761 if (rdataset != NULL) {
2762 NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
2763 isc_rwlocktype_read);
2764 bind_rdataset(search->rbtdb, node, search->zonecut_rdataset,
2765 search->now, rdataset);
2766 if (sigrdataset != NULL && search->zonecut_sigrdataset != NULL)
2767 bind_rdataset(search->rbtdb, node,
2768 search->zonecut_sigrdataset,
2769 search->now, sigrdataset);
2770 NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
2771 isc_rwlocktype_read);
2774 if (type == dns_rdatatype_dname)
2775 return (DNS_R_DNAME);
2776 return (DNS_R_DELEGATION);
2779 static inline isc_boolean_t
2780 valid_glue(rbtdb_search_t *search, dns_name_t *name, rbtdb_rdatatype_t type,
2781 dns_rbtnode_t *node)
2783 unsigned char *raw; /* RDATASLAB */
2784 unsigned int count, size;
2786 isc_boolean_t valid = ISC_FALSE;
2787 dns_offsets_t offsets;
2788 isc_region_t region;
2789 rdatasetheader_t *header;
2792 * No additional locking is required.
2796 * Valid glue types are A, AAAA, A6. NS is also a valid glue type
2797 * if it occurs at a zone cut, but is not valid below it.
2799 if (type == dns_rdatatype_ns) {
2800 if (node != search->zonecut) {
2803 } else if (type != dns_rdatatype_a &&
2804 type != dns_rdatatype_aaaa &&
2805 type != dns_rdatatype_a6) {
2809 header = search->zonecut_rdataset;
2810 raw = (unsigned char *)header + sizeof(*header);
2811 count = raw[0] * 256 + raw[1];
2812 #if DNS_RDATASET_FIXED
2813 raw += 2 + (4 * count);
2820 size = raw[0] * 256 + raw[1];
2821 #if DNS_RDATASET_FIXED
2827 region.length = size;
2830 * XXX Until we have rdata structures, we have no choice but
2831 * to directly access the rdata format.
2833 dns_name_init(&ns_name, offsets);
2834 dns_name_fromregion(&ns_name, ®ion);
2835 if (dns_name_compare(&ns_name, name) == 0) {
2844 static inline isc_boolean_t
2845 activeempty(rbtdb_search_t *search, dns_rbtnodechain_t *chain,
2848 dns_fixedname_t fnext;
2849 dns_fixedname_t forigin;
2854 dns_rbtnode_t *node;
2855 isc_result_t result;
2856 isc_boolean_t answer = ISC_FALSE;
2857 rdatasetheader_t *header;
2859 rbtdb = search->rbtdb;
2861 dns_name_init(&prefix, NULL);
2862 dns_fixedname_init(&fnext);
2863 next = dns_fixedname_name(&fnext);
2864 dns_fixedname_init(&forigin);
2865 origin = dns_fixedname_name(&forigin);
2867 result = dns_rbtnodechain_next(chain, NULL, NULL);
2868 while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
2870 result = dns_rbtnodechain_current(chain, &prefix,
2872 if (result != ISC_R_SUCCESS)
2874 NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
2875 isc_rwlocktype_read);
2876 for (header = node->data;
2878 header = header->next) {
2879 if (header->serial <= search->serial &&
2880 !IGNORE(header) && EXISTS(header))
2883 NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
2884 isc_rwlocktype_read);
2887 result = dns_rbtnodechain_next(chain, NULL, NULL);
2889 if (result == ISC_R_SUCCESS)
2890 result = dns_name_concatenate(&prefix, origin, next, NULL);
2891 if (result == ISC_R_SUCCESS && dns_name_issubdomain(next, name))
2896 static inline isc_boolean_t
2897 activeemtpynode(rbtdb_search_t *search, dns_name_t *qname, dns_name_t *wname) {
2898 dns_fixedname_t fnext;
2899 dns_fixedname_t forigin;
2900 dns_fixedname_t fprev;
2908 dns_rbtnode_t *node;
2909 dns_rbtnodechain_t chain;
2910 isc_boolean_t check_next = ISC_TRUE;
2911 isc_boolean_t check_prev = ISC_TRUE;
2912 isc_boolean_t answer = ISC_FALSE;
2913 isc_result_t result;
2914 rdatasetheader_t *header;
2917 rbtdb = search->rbtdb;
2919 dns_name_init(&name, NULL);
2920 dns_name_init(&tname, NULL);
2921 dns_name_init(&rname, NULL);
2922 dns_fixedname_init(&fnext);
2923 next = dns_fixedname_name(&fnext);
2924 dns_fixedname_init(&fprev);
2925 prev = dns_fixedname_name(&fprev);
2926 dns_fixedname_init(&forigin);
2927 origin = dns_fixedname_name(&forigin);
2930 * Find if qname is at or below a empty node.
2931 * Use our own copy of the chain.
2934 chain = search->chain;
2937 result = dns_rbtnodechain_current(&chain, &name,
2939 if (result != ISC_R_SUCCESS)
2941 NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
2942 isc_rwlocktype_read);
2943 for (header = node->data;
2945 header = header->next) {
2946 if (header->serial <= search->serial &&
2947 !IGNORE(header) && EXISTS(header))
2950 NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
2951 isc_rwlocktype_read);
2954 result = dns_rbtnodechain_prev(&chain, NULL, NULL);
2955 } while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN);
2956 if (result == ISC_R_SUCCESS)
2957 result = dns_name_concatenate(&name, origin, prev, NULL);
2958 if (result != ISC_R_SUCCESS)
2959 check_prev = ISC_FALSE;
2961 result = dns_rbtnodechain_next(&chain, NULL, NULL);
2962 while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
2964 result = dns_rbtnodechain_current(&chain, &name,
2966 if (result != ISC_R_SUCCESS)
2968 NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
2969 isc_rwlocktype_read);
2970 for (header = node->data;
2972 header = header->next) {
2973 if (header->serial <= search->serial &&
2974 !IGNORE(header) && EXISTS(header))
2977 NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
2978 isc_rwlocktype_read);
2981 result = dns_rbtnodechain_next(&chain, NULL, NULL);
2983 if (result == ISC_R_SUCCESS)
2984 result = dns_name_concatenate(&name, origin, next, NULL);
2985 if (result != ISC_R_SUCCESS)
2986 check_next = ISC_FALSE;
2988 dns_name_clone(qname, &rname);
2991 * Remove the wildcard label to find the terminal name.
2993 n = dns_name_countlabels(wname);
2994 dns_name_getlabelsequence(wname, 1, n - 1, &tname);
2997 if ((check_prev && dns_name_issubdomain(prev, &rname)) ||
2998 (check_next && dns_name_issubdomain(next, &rname))) {
3003 * Remove the left hand label.
3005 n = dns_name_countlabels(&rname);
3006 dns_name_getlabelsequence(&rname, 1, n - 1, &rname);
3007 } while (!dns_name_equal(&rname, &tname));
3011 static inline isc_result_t
3012 find_wildcard(rbtdb_search_t *search, dns_rbtnode_t **nodep,
3016 dns_rbtnode_t *node, *level_node, *wnode;
3017 rdatasetheader_t *header;
3018 isc_result_t result = ISC_R_NOTFOUND;
3021 dns_fixedname_t fwname;
3023 isc_boolean_t done, wild, active;
3024 dns_rbtnodechain_t wchain;
3027 * Caller must be holding the tree lock and MUST NOT be holding
3032 * Examine each ancestor level. If the level's wild bit
3033 * is set, then construct the corresponding wildcard name and
3034 * search for it. If the wildcard node exists, and is active in
3035 * this version, we're done. If not, then we next check to see
3036 * if the ancestor is active in this version. If so, then there
3037 * can be no possible wildcard match and again we're done. If not,
3038 * continue the search.
3041 rbtdb = search->rbtdb;
3042 i = search->chain.level_matches;
3046 NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
3047 isc_rwlocktype_read);
3050 * First we try to figure out if this node is active in
3051 * the search's version. We do this now, even though we
3052 * may not need the information, because it simplifies the
3053 * locking and code flow.
3055 for (header = node->data;
3057 header = header->next) {
3058 if (header->serial <= search->serial &&
3059 !IGNORE(header) && EXISTS(header))
3072 NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
3073 isc_rwlocktype_read);
3077 * Construct the wildcard name for this level.
3079 dns_name_init(&name, NULL);
3080 dns_rbt_namefromnode(node, &name);
3081 dns_fixedname_init(&fwname);
3082 wname = dns_fixedname_name(&fwname);
3083 result = dns_name_concatenate(dns_wildcardname, &name,
3086 while (result == ISC_R_SUCCESS && j != 0) {
3088 level_node = search->chain.levels[j];
3089 dns_name_init(&name, NULL);
3090 dns_rbt_namefromnode(level_node, &name);
3091 result = dns_name_concatenate(wname,
3096 if (result != ISC_R_SUCCESS)
3100 dns_rbtnodechain_init(&wchain, NULL);
3101 result = dns_rbt_findnode(rbtdb->tree, wname,
3102 NULL, &wnode, &wchain,
3103 DNS_RBTFIND_EMPTYDATA,
3105 if (result == ISC_R_SUCCESS) {
3109 * We have found the wildcard node. If it
3110 * is active in the search's version, we're
3113 lock = &rbtdb->node_locks[wnode->locknum].lock;
3114 NODE_LOCK(lock, isc_rwlocktype_read);
3115 for (header = wnode->data;
3117 header = header->next) {
3118 if (header->serial <= search->serial &&
3119 !IGNORE(header) && EXISTS(header))
3122 NODE_UNLOCK(lock, isc_rwlocktype_read);
3123 if (header != NULL ||
3124 activeempty(search, &wchain, wname)) {
3125 if (activeemtpynode(search, qname,
3127 return (ISC_R_NOTFOUND);
3130 * The wildcard node is active!
3132 * Note: result is still ISC_R_SUCCESS
3133 * so we don't have to set it.
3138 } else if (result != ISC_R_NOTFOUND &&
3139 result != DNS_R_PARTIALMATCH) {
3141 * An error has occurred. Bail out.
3149 * The level node is active. Any wildcarding
3150 * present at higher levels has no
3151 * effect and we're done.
3153 result = ISC_R_NOTFOUND;
3159 node = search->chain.levels[i];
3167 static isc_boolean_t
3168 matchparams(rdatasetheader_t *header, rbtdb_search_t *search)
3170 dns_rdata_t rdata = DNS_RDATA_INIT;
3171 dns_rdata_nsec3_t nsec3;
3172 unsigned char *raw; /* RDATASLAB */
3173 unsigned int rdlen, count;
3174 isc_region_t region;
3175 isc_result_t result;
3177 REQUIRE(header->type == dns_rdatatype_nsec3);
3179 raw = (unsigned char *)header + sizeof(*header);
3180 count = raw[0] * 256 + raw[1]; /* count */
3181 #if DNS_RDATASET_FIXED
3182 raw += count * 4 + 2;
3186 while (count-- > 0) {
3187 rdlen = raw[0] * 256 + raw[1];
3188 #if DNS_RDATASET_FIXED
3194 region.length = rdlen;
3195 dns_rdata_fromregion(&rdata, search->rbtdb->common.rdclass,
3196 dns_rdatatype_nsec3, ®ion);
3198 result = dns_rdata_tostruct(&rdata, &nsec3, NULL);
3199 INSIST(result == ISC_R_SUCCESS);
3200 if (nsec3.hash == search->rbtversion->hash &&
3201 nsec3.iterations == search->rbtversion->iterations &&
3202 nsec3.salt_length == search->rbtversion->salt_length &&
3203 memcmp(nsec3.salt, search->rbtversion->salt,
3204 nsec3.salt_length) == 0)
3206 dns_rdata_reset(&rdata);
3212 * Find node of the NSEC/NSEC3 record that is 'name'.
3214 static inline isc_result_t
3215 find_closest_nsec(rbtdb_search_t *search, dns_dbnode_t **nodep,
3216 dns_name_t *foundname, dns_rdataset_t *rdataset,
3217 dns_rdataset_t *sigrdataset, dns_rbt_t *tree,
3218 dns_db_secure_t secure)
3220 dns_rbtnode_t *node;
3221 rdatasetheader_t *header, *header_next, *found, *foundsig;
3222 isc_boolean_t empty_node;
3223 isc_result_t result;
3224 dns_fixedname_t fname, forigin;
3225 dns_name_t *name, *origin;
3226 dns_rdatatype_t type;
3227 rbtdb_rdatatype_t sigtype;
3228 isc_boolean_t wraps;
3229 isc_boolean_t need_sig = ISC_TF(secure == dns_db_secure);
3231 if (tree == search->rbtdb->nsec3) {
3232 type = dns_rdatatype_nsec3;
3233 sigtype = RBTDB_RDATATYPE_SIGNSEC3;
3236 type = dns_rdatatype_nsec;
3237 sigtype = RBTDB_RDATATYPE_SIGNSEC;
3244 dns_fixedname_init(&fname);
3245 name = dns_fixedname_name(&fname);
3246 dns_fixedname_init(&forigin);
3247 origin = dns_fixedname_name(&forigin);
3248 result = dns_rbtnodechain_current(&search->chain, name,
3250 if (result != ISC_R_SUCCESS)
3252 NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
3253 isc_rwlocktype_read);
3256 empty_node = ISC_TRUE;
3257 for (header = node->data;
3259 header = header_next) {
3260 header_next = header->next;
3262 * Look for an active, extant NSEC or RRSIG NSEC.
3265 if (header->serial <= search->serial &&
3268 * Is this a "this rdataset doesn't
3271 if (NONEXISTENT(header))
3275 header = header->down;
3276 } while (header != NULL);
3277 if (header != NULL) {
3279 * We now know that there is at least one
3280 * active rdataset at this node.
3282 empty_node = ISC_FALSE;
3283 if (header->type == type) {
3285 if (foundsig != NULL)
3287 } else if (header->type == sigtype) {
3295 if (found != NULL && search->rbtversion->havensec3 &&
3296 found->type == dns_rdatatype_nsec3 &&
3297 !matchparams(found, search)) {
3298 empty_node = ISC_TRUE;
3301 result = dns_rbtnodechain_prev(&search->chain,
3303 } else if (found != NULL &&
3304 (foundsig != NULL || !need_sig))
3307 * We've found the right NSEC/NSEC3 record.
3309 * Note: for this to really be the right
3310 * NSEC record, it's essential that the NSEC
3311 * records of any nodes obscured by a zone
3312 * cut have been removed; we assume this is
3315 result = dns_name_concatenate(name, origin,
3317 if (result == ISC_R_SUCCESS) {
3318 if (nodep != NULL) {
3319 new_reference(search->rbtdb,
3323 bind_rdataset(search->rbtdb, node,
3326 if (foundsig != NULL)
3327 bind_rdataset(search->rbtdb,
3333 } else if (found == NULL && foundsig == NULL) {
3335 * This node is active, but has no NSEC or
3336 * RRSIG NSEC. That means it's glue or
3337 * other obscured zone data that isn't
3338 * relevant for our search. Treat the
3339 * node as if it were empty and keep looking.
3341 empty_node = ISC_TRUE;
3342 result = dns_rbtnodechain_prev(&search->chain,
3346 * We found an active node, but either the
3347 * NSEC or the RRSIG NSEC is missing. This
3350 result = DNS_R_BADDB;
3354 * This node isn't active. We've got to keep
3357 result = dns_rbtnodechain_prev(&search->chain, NULL,
3360 NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
3361 isc_rwlocktype_read);
3362 } while (empty_node && result == ISC_R_SUCCESS);
3364 if (result == ISC_R_NOMORE && wraps) {
3365 result = dns_rbtnodechain_last(&search->chain, tree,
3367 if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
3374 * If the result is ISC_R_NOMORE, then we got to the beginning of
3375 * the database and didn't find a NSEC record. This shouldn't
3378 if (result == ISC_R_NOMORE)
3379 result = DNS_R_BADDB;
3385 zone_find(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
3386 dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
3387 dns_dbnode_t **nodep, dns_name_t *foundname,
3388 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
3390 dns_rbtnode_t *node = NULL;
3391 isc_result_t result;
3392 rbtdb_search_t search;
3393 isc_boolean_t cname_ok = ISC_TRUE;
3394 isc_boolean_t close_version = ISC_FALSE;
3395 isc_boolean_t maybe_zonecut = ISC_FALSE;
3396 isc_boolean_t at_zonecut = ISC_FALSE;
3398 isc_boolean_t empty_node;
3399 rdatasetheader_t *header, *header_next, *found, *nsecheader;
3400 rdatasetheader_t *foundsig, *cnamesig, *nsecsig;
3401 rbtdb_rdatatype_t sigtype;
3402 isc_boolean_t active;
3403 dns_rbtnodechain_t chain;
3407 search.rbtdb = (dns_rbtdb_t *)db;
3409 REQUIRE(VALID_RBTDB(search.rbtdb));
3412 * We don't care about 'now'.
3417 * If the caller didn't supply a version, attach to the current
3420 if (version == NULL) {
3421 currentversion(db, &version);
3422 close_version = ISC_TRUE;
3425 search.rbtversion = version;
3426 search.serial = search.rbtversion->serial;
3427 search.options = options;
3428 search.copy_name = ISC_FALSE;
3429 search.need_cleanup = ISC_FALSE;
3430 search.wild = ISC_FALSE;
3431 search.zonecut = NULL;
3432 dns_fixedname_init(&search.zonecut_name);
3433 dns_rbtnodechain_init(&search.chain, search.rbtdb->common.mctx);
3437 * 'wild' will be true iff. we've matched a wildcard.
3441 RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
3444 * Search down from the root of the tree. If, while going down, we
3445 * encounter a callback node, zone_zonecut_callback() will search the
3446 * rdatasets at the zone cut for active DNAME or NS rdatasets.
3448 tree = (options & DNS_DBFIND_FORCENSEC3) != 0 ? search.rbtdb->nsec3 :
3450 result = dns_rbt_findnode(tree, name, foundname, &node,
3451 &search.chain, DNS_RBTFIND_EMPTYDATA,
3452 zone_zonecut_callback, &search);
3454 if (result == DNS_R_PARTIALMATCH) {
3456 if (search.zonecut != NULL) {
3457 result = setup_delegation(&search, nodep, foundname,
3458 rdataset, sigrdataset);
3464 * At least one of the levels in the search chain
3465 * potentially has a wildcard. For each such level,
3466 * we must see if there's a matching wildcard active
3467 * in the current version.
3469 result = find_wildcard(&search, &node, name);
3470 if (result == ISC_R_SUCCESS) {
3471 result = dns_name_copy(name, foundname, NULL);
3472 if (result != ISC_R_SUCCESS)
3477 else if (result != ISC_R_NOTFOUND)
3481 chain = search.chain;
3482 active = activeempty(&search, &chain, name);
3485 * If we're here, then the name does not exist, is not
3486 * beneath a zonecut, and there's no matching wildcard.
3488 if ((search.rbtversion->secure == dns_db_secure &&
3489 !search.rbtversion->havensec3) ||
3490 (search.options & DNS_DBFIND_FORCENSEC) != 0 ||
3491 (search.options & DNS_DBFIND_FORCENSEC3) != 0)
3493 result = find_closest_nsec(&search, nodep, foundname,
3494 rdataset, sigrdataset, tree,
3495 search.rbtversion->secure);
3496 if (result == ISC_R_SUCCESS)
3497 result = active ? DNS_R_EMPTYNAME :
3500 result = active ? DNS_R_EMPTYNAME : DNS_R_NXDOMAIN;
3502 } else if (result != ISC_R_SUCCESS)
3507 * We have found a node whose name is the desired name, or we
3508 * have matched a wildcard.
3511 if (search.zonecut != NULL) {
3513 * If we're beneath a zone cut, we don't want to look for
3514 * CNAMEs because they're not legitimate zone glue.
3516 cname_ok = ISC_FALSE;
3519 * The node may be a zone cut itself. If it might be one,
3520 * make sure we check for it later.
3522 * DS records live above the zone cut in ordinary zone so
3523 * we want to ignore any referral.
3525 * Stub zones don't have anything "above" the delgation so
3526 * we always return a referral.
3528 if (node->find_callback &&
3529 ((node != search.rbtdb->origin_node &&
3530 !dns_rdatatype_atparent(type)) ||
3531 IS_STUB(search.rbtdb)))
3532 maybe_zonecut = ISC_TRUE;
3536 * Certain DNSSEC types are not subject to CNAME matching
3537 * (RFC4035, section 2.5 and RFC3007).
3539 * We don't check for RRSIG, because we don't store RRSIG records
3542 if (type == dns_rdatatype_key || type == dns_rdatatype_nsec)
3543 cname_ok = ISC_FALSE;
3546 * We now go looking for rdata...
3549 lock = &search.rbtdb->node_locks[node->locknum].lock;
3550 NODE_LOCK(lock, isc_rwlocktype_read);
3554 sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
3558 empty_node = ISC_TRUE;
3559 for (header = node->data; header != NULL; header = header_next) {
3560 header_next = header->next;
3562 * Look for an active, extant rdataset.
3565 if (header->serial <= search.serial &&
3568 * Is this a "this rdataset doesn't
3571 if (NONEXISTENT(header))
3575 header = header->down;
3576 } while (header != NULL);
3577 if (header != NULL) {
3579 * We now know that there is at least one active
3580 * rdataset at this node.
3582 empty_node = ISC_FALSE;
3585 * Do special zone cut handling, if requested.
3587 if (maybe_zonecut &&
3588 header->type == dns_rdatatype_ns) {
3590 * We increment the reference count on node to
3591 * ensure that search->zonecut_rdataset will
3592 * still be valid later.
3594 new_reference(search.rbtdb, node);
3595 search.zonecut = node;
3596 search.zonecut_rdataset = header;
3597 search.zonecut_sigrdataset = NULL;
3598 search.need_cleanup = ISC_TRUE;
3599 maybe_zonecut = ISC_FALSE;
3600 at_zonecut = ISC_TRUE;
3602 * It is not clear if KEY should still be
3603 * allowed at the parent side of the zone
3604 * cut or not. It is needed for RFC3007
3605 * validated updates.
3607 if ((search.options & DNS_DBFIND_GLUEOK) == 0
3608 && type != dns_rdatatype_nsec
3609 && type != dns_rdatatype_key) {
3611 * Glue is not OK, but any answer we
3612 * could return would be glue. Return
3618 if (found != NULL && foundsig != NULL)
3624 * If the NSEC3 record doesn't match the chain
3625 * we are using behave as if it isn't here.
3627 if (header->type == dns_rdatatype_nsec3 &&
3628 !matchparams(header, &search)) {
3629 NODE_UNLOCK(lock, isc_rwlocktype_read);
3633 * If we found a type we were looking for,
3636 if (header->type == type ||
3637 type == dns_rdatatype_any ||
3638 (header->type == dns_rdatatype_cname &&
3641 * We've found the answer!
3644 if (header->type == dns_rdatatype_cname &&
3647 * We may be finding a CNAME instead
3648 * of the desired type.
3650 * If we've already got the CNAME RRSIG,
3651 * use it, otherwise change sigtype
3652 * so that we find it.
3654 if (cnamesig != NULL)
3655 foundsig = cnamesig;
3658 RBTDB_RDATATYPE_SIGCNAME;
3661 * If we've got all we need, end the search.
3663 if (!maybe_zonecut && foundsig != NULL)
3665 } else if (header->type == sigtype) {
3667 * We've found the RRSIG rdataset for our
3668 * target type. Remember it.
3672 * If we've got all we need, end the search.
3674 if (!maybe_zonecut && found != NULL)
3676 } else if (header->type == dns_rdatatype_nsec &&
3677 !search.rbtversion->havensec3) {
3679 * Remember a NSEC rdataset even if we're
3680 * not specifically looking for it, because
3681 * we might need it later.
3683 nsecheader = header;
3684 } else if (header->type == RBTDB_RDATATYPE_SIGNSEC &&
3685 !search.rbtversion->havensec3) {
3687 * If we need the NSEC rdataset, we'll also
3688 * need its signature.
3691 } else if (cname_ok &&
3692 header->type == RBTDB_RDATATYPE_SIGCNAME) {
3694 * If we get a CNAME match, we'll also need
3704 * We have an exact match for the name, but there are no
3705 * active rdatasets in the desired version. That means that
3706 * this node doesn't exist in the desired version, and that
3707 * we really have a partial match.
3710 NODE_UNLOCK(lock, isc_rwlocktype_read);
3716 * If we didn't find what we were looking for...
3718 if (found == NULL) {
3719 if (search.zonecut != NULL) {
3721 * We were trying to find glue at a node beneath a
3722 * zone cut, but didn't.
3724 * Return the delegation.
3726 NODE_UNLOCK(lock, isc_rwlocktype_read);
3727 result = setup_delegation(&search, nodep, foundname,
3728 rdataset, sigrdataset);
3732 * The desired type doesn't exist.
3734 result = DNS_R_NXRRSET;
3735 if (search.rbtversion->secure == dns_db_secure &&
3736 !search.rbtversion->havensec3 &&
3737 (nsecheader == NULL || nsecsig == NULL)) {
3739 * The zone is secure but there's no NSEC,
3740 * or the NSEC has no signature!
3743 result = DNS_R_BADDB;
3747 NODE_UNLOCK(lock, isc_rwlocktype_read);
3748 result = find_closest_nsec(&search, nodep, foundname,
3749 rdataset, sigrdataset,
3751 search.rbtversion->secure);
3752 if (result == ISC_R_SUCCESS)
3753 result = DNS_R_EMPTYWILD;
3756 if ((search.options & DNS_DBFIND_FORCENSEC) != 0 &&
3760 * There's no NSEC record, and we were told
3763 result = DNS_R_BADDB;
3766 if (nodep != NULL) {
3767 new_reference(search.rbtdb, node);
3770 if ((search.rbtversion->secure == dns_db_secure &&
3771 !search.rbtversion->havensec3) ||
3772 (search.options & DNS_DBFIND_FORCENSEC) != 0)
3774 bind_rdataset(search.rbtdb, node, nsecheader,
3776 if (nsecsig != NULL)
3777 bind_rdataset(search.rbtdb, node,
3778 nsecsig, 0, sigrdataset);
3781 foundname->attributes |= DNS_NAMEATTR_WILDCARD;
3786 * We found what we were looking for, or we found a CNAME.
3789 if (type != found->type &&
3790 type != dns_rdatatype_any &&
3791 found->type == dns_rdatatype_cname) {
3793 * We weren't doing an ANY query and we found a CNAME instead
3794 * of the type we were looking for, so we need to indicate
3795 * that result to the caller.
3797 result = DNS_R_CNAME;
3798 } else if (search.zonecut != NULL) {
3800 * If we're beneath a zone cut, we must indicate that the
3801 * result is glue, unless we're actually at the zone cut
3802 * and the type is NSEC or KEY.
3804 if (search.zonecut == node) {
3806 * It is not clear if KEY should still be
3807 * allowed at the parent side of the zone
3808 * cut or not. It is needed for RFC3007
3809 * validated updates.
3811 if (type == dns_rdatatype_nsec ||
3812 type == dns_rdatatype_nsec3 ||
3813 type == dns_rdatatype_key)
3814 result = ISC_R_SUCCESS;
3815 else if (type == dns_rdatatype_any)
3816 result = DNS_R_ZONECUT;
3818 result = DNS_R_GLUE;
3820 result = DNS_R_GLUE;
3822 * We might have found data that isn't glue, but was occluded
3823 * by a dynamic update. If the caller cares about this, they
3824 * will have told us to validate glue.
3826 * XXX We should cache the glue validity state!
3828 if (result == DNS_R_GLUE &&
3829 (search.options & DNS_DBFIND_VALIDATEGLUE) != 0 &&
3830 !valid_glue(&search, foundname, type, node)) {
3831 NODE_UNLOCK(lock, isc_rwlocktype_read);
3832 result = setup_delegation(&search, nodep, foundname,
3833 rdataset, sigrdataset);
3838 * An ordinary successful query!
3840 result = ISC_R_SUCCESS;
3843 if (nodep != NULL) {
3845 new_reference(search.rbtdb, node);
3847 search.need_cleanup = ISC_FALSE;
3851 if (type != dns_rdatatype_any) {
3852 bind_rdataset(search.rbtdb, node, found, 0, rdataset);
3853 if (foundsig != NULL)
3854 bind_rdataset(search.rbtdb, node, foundsig, 0,
3859 foundname->attributes |= DNS_NAMEATTR_WILDCARD;
3862 NODE_UNLOCK(lock, isc_rwlocktype_read);
3865 RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
3868 * If we found a zonecut but aren't going to use it, we have to
3871 if (search.need_cleanup) {
3872 node = search.zonecut;
3873 lock = &(search.rbtdb->node_locks[node->locknum].lock);
3875 NODE_LOCK(lock, isc_rwlocktype_read);
3876 decrement_reference(search.rbtdb, node, 0,
3877 isc_rwlocktype_read, isc_rwlocktype_none,
3879 NODE_UNLOCK(lock, isc_rwlocktype_read);
3883 closeversion(db, &version, ISC_FALSE);
3885 dns_rbtnodechain_reset(&search.chain);
3891 zone_findzonecut(dns_db_t *db, dns_name_t *name, unsigned int options,
3892 isc_stdtime_t now, dns_dbnode_t **nodep,
3893 dns_name_t *foundname,
3894 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
3903 UNUSED(sigrdataset);
3905 FATAL_ERROR(__FILE__, __LINE__, "zone_findzonecut() called!");
3907 return (ISC_R_NOTIMPLEMENTED);
3911 cache_zonecut_callback(dns_rbtnode_t *node, dns_name_t *name, void *arg) {
3912 rbtdb_search_t *search = arg;
3913 rdatasetheader_t *header, *header_prev, *header_next;
3914 rdatasetheader_t *dname_header, *sigdname_header;
3915 isc_result_t result;
3917 isc_rwlocktype_t locktype;
3921 REQUIRE(search->zonecut == NULL);
3924 * Keep compiler silent.
3928 lock = &(search->rbtdb->node_locks[node->locknum].lock);
3929 locktype = isc_rwlocktype_read;
3930 NODE_LOCK(lock, locktype);
3933 * Look for a DNAME or RRSIG DNAME rdataset.
3935 dname_header = NULL;
3936 sigdname_header = NULL;
3938 for (header = node->data; header != NULL; header = header_next) {
3939 header_next = header->next;
3940 if (header->rdh_ttl <= search->now) {
3942 * This rdataset is stale. If no one else is
3943 * using the node, we can clean it up right
3944 * now, otherwise we mark it as stale, and
3945 * the node as dirty, so it will get cleaned
3948 if ((header->rdh_ttl <= search->now - RBTDB_VIRTUAL) &&
3949 (locktype == isc_rwlocktype_write ||
3950 NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
3952 * We update the node's status only when we
3953 * can get write access; otherwise, we leave
3954 * others to this work. Periodical cleaning
3955 * will eventually take the job as the last
3957 * We won't downgrade the lock, since other
3958 * rdatasets are probably stale, too.
3960 locktype = isc_rwlocktype_write;
3962 if (dns_rbtnode_refcurrent(node) == 0) {
3966 * header->down can be non-NULL if the
3967 * refcount has just decremented to 0
3968 * but decrement_reference() has not
3969 * performed clean_cache_node(), in
3970 * which case we need to purge the
3971 * stale headers first.
3973 mctx = search->rbtdb->common.mctx;
3974 clean_stale_headers(search->rbtdb,
3977 if (header_prev != NULL)
3981 node->data = header->next;
3982 free_rdataset(search->rbtdb, mctx,
3985 header->attributes |=
3986 RDATASET_ATTR_STALE;
3988 header_prev = header;
3991 header_prev = header;
3992 } else if (header->type == dns_rdatatype_dname &&
3994 dname_header = header;
3995 header_prev = header;
3996 } else if (header->type == RBTDB_RDATATYPE_SIGDNAME &&
3998 sigdname_header = header;
3999 header_prev = header;
4001 header_prev = header;
4004 if (dname_header != NULL &&
4005 (!DNS_TRUST_PENDING(dname_header->trust) ||
4006 (search->options & DNS_DBFIND_PENDINGOK) != 0)) {
4008 * We increment the reference count on node to ensure that
4009 * search->zonecut_rdataset will still be valid later.
4011 new_reference(search->rbtdb, node);
4012 INSIST(!ISC_LINK_LINKED(node, deadlink));
4013 search->zonecut = node;
4014 search->zonecut_rdataset = dname_header;
4015 search->zonecut_sigrdataset = sigdname_header;
4016 search->need_cleanup = ISC_TRUE;
4017 result = DNS_R_PARTIALMATCH;
4019 result = DNS_R_CONTINUE;
4021 NODE_UNLOCK(lock, locktype);
4026 static inline isc_result_t
4027 find_deepest_zonecut(rbtdb_search_t *search, dns_rbtnode_t *node,
4028 dns_dbnode_t **nodep, dns_name_t *foundname,
4029 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
4032 dns_rbtnode_t *level_node;
4033 rdatasetheader_t *header, *header_prev, *header_next;
4034 rdatasetheader_t *found, *foundsig;
4035 isc_result_t result = ISC_R_NOTFOUND;
4040 isc_rwlocktype_t locktype;
4043 * Caller must be holding the tree lock.
4046 rbtdb = search->rbtdb;
4047 i = search->chain.level_matches;
4050 locktype = isc_rwlocktype_read;
4051 lock = &rbtdb->node_locks[node->locknum].lock;
4052 NODE_LOCK(lock, locktype);
4055 * Look for NS and RRSIG NS rdatasets.
4060 for (header = node->data;
4062 header = header_next) {
4063 header_next = header->next;
4064 if (header->rdh_ttl <= search->now) {
4066 * This rdataset is stale. If no one else is
4067 * using the node, we can clean it up right
4068 * now, otherwise we mark it as stale, and
4069 * the node as dirty, so it will get cleaned
4072 if ((header->rdh_ttl <= search->now -
4074 (locktype == isc_rwlocktype_write ||
4075 NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
4077 * We update the node's status only
4078 * when we can get write access.
4080 locktype = isc_rwlocktype_write;
4082 if (dns_rbtnode_refcurrent(node)
4086 m = search->rbtdb->common.mctx;
4087 clean_stale_headers(
4090 if (header_prev != NULL)
4096 free_rdataset(rbtdb, m,
4099 header->attributes |=
4100 RDATASET_ATTR_STALE;
4102 header_prev = header;
4105 header_prev = header;
4106 } else if (EXISTS(header)) {
4108 * We've found an extant rdataset. See if
4109 * we're interested in it.
4111 if (header->type == dns_rdatatype_ns) {
4113 if (foundsig != NULL)
4115 } else if (header->type ==
4116 RBTDB_RDATATYPE_SIGNS) {
4121 header_prev = header;
4123 header_prev = header;
4126 if (found != NULL) {
4128 * If we have to set foundname, we do it before
4129 * anything else. If we were to set foundname after
4130 * we had set nodep or bound the rdataset, then we'd
4131 * have to undo that work if dns_name_concatenate()
4132 * failed. By setting foundname first, there's
4133 * nothing to undo if we have trouble.
4135 if (foundname != NULL) {
4136 dns_name_init(&name, NULL);
4137 dns_rbt_namefromnode(node, &name);
4138 result = dns_name_copy(&name, foundname, NULL);
4139 while (result == ISC_R_SUCCESS && i > 0) {
4141 level_node = search->chain.levels[i];
4142 dns_name_init(&name, NULL);
4143 dns_rbt_namefromnode(level_node,
4146 dns_name_concatenate(foundname,
4151 if (result != ISC_R_SUCCESS) {
4156 result = DNS_R_DELEGATION;
4157 if (nodep != NULL) {
4158 new_reference(search->rbtdb, node);
4161 bind_rdataset(search->rbtdb, node, found, search->now,
4163 if (foundsig != NULL)
4164 bind_rdataset(search->rbtdb, node, foundsig,
4165 search->now, sigrdataset);
4166 if (need_headerupdate(found, search->now) ||
4167 (foundsig != NULL &&
4168 need_headerupdate(foundsig, search->now))) {
4169 if (locktype != isc_rwlocktype_write) {
4170 NODE_UNLOCK(lock, locktype);
4171 NODE_LOCK(lock, isc_rwlocktype_write);
4172 locktype = isc_rwlocktype_write;
4174 if (need_headerupdate(found, search->now))
4175 update_header(search->rbtdb, found,
4177 if (foundsig != NULL &&
4178 need_headerupdate(foundsig, search->now)) {
4179 update_header(search->rbtdb, foundsig,
4186 NODE_UNLOCK(lock, locktype);
4188 if (found == NULL && i > 0) {
4190 node = search->chain.levels[i];
4200 find_coveringnsec(rbtdb_search_t *search, dns_dbnode_t **nodep,
4201 isc_stdtime_t now, dns_name_t *foundname,
4202 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
4204 dns_rbtnode_t *node;
4205 rdatasetheader_t *header, *header_next, *header_prev;
4206 rdatasetheader_t *found, *foundsig;
4207 isc_boolean_t empty_node;
4208 isc_result_t result;
4209 dns_fixedname_t fname, forigin;
4210 dns_name_t *name, *origin;
4211 rbtdb_rdatatype_t matchtype, sigmatchtype;
4213 isc_rwlocktype_t locktype;
4215 matchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_nsec, 0);
4216 sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig,
4217 dns_rdatatype_nsec);
4221 dns_fixedname_init(&fname);
4222 name = dns_fixedname_name(&fname);
4223 dns_fixedname_init(&forigin);
4224 origin = dns_fixedname_name(&forigin);
4225 result = dns_rbtnodechain_current(&search->chain, name,
4227 if (result != ISC_R_SUCCESS)
4229 locktype = isc_rwlocktype_read;
4230 lock = &(search->rbtdb->node_locks[node->locknum].lock);
4231 NODE_LOCK(lock, locktype);
4234 empty_node = ISC_TRUE;
4236 for (header = node->data;
4238 header = header_next) {
4239 header_next = header->next;
4240 if (header->rdh_ttl <= now) {
4242 * This rdataset is stale. If no one else is
4243 * using the node, we can clean it up right
4244 * now, otherwise we mark it as stale, and the
4245 * node as dirty, so it will get cleaned up
4248 if ((header->rdh_ttl <= now - RBTDB_VIRTUAL) &&
4249 (locktype == isc_rwlocktype_write ||
4250 NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
4252 * We update the node's status only
4253 * when we can get write access.
4255 locktype = isc_rwlocktype_write;
4257 if (dns_rbtnode_refcurrent(node)
4261 m = search->rbtdb->common.mctx;
4262 clean_stale_headers(
4265 if (header_prev != NULL)
4269 node->data = header->next;
4270 free_rdataset(search->rbtdb, m,
4273 header->attributes |=
4274 RDATASET_ATTR_STALE;
4276 header_prev = header;
4279 header_prev = header;
4282 if (NONEXISTENT(header) ||
4283 RBTDB_RDATATYPE_BASE(header->type) == 0) {
4284 header_prev = header;
4287 empty_node = ISC_FALSE;
4288 if (header->type == matchtype)
4290 else if (header->type == sigmatchtype)
4292 header_prev = header;
4294 if (found != NULL) {
4295 result = dns_name_concatenate(name, origin,
4297 if (result != ISC_R_SUCCESS)
4299 bind_rdataset(search->rbtdb, node, found,
4301 if (foundsig != NULL)
4302 bind_rdataset(search->rbtdb, node, foundsig,
4304 new_reference(search->rbtdb, node);
4306 result = DNS_R_COVERINGNSEC;
4307 } else if (!empty_node) {
4308 result = ISC_R_NOTFOUND;
4310 result = dns_rbtnodechain_prev(&search->chain, NULL,
4313 NODE_UNLOCK(lock, locktype);
4314 } while (empty_node && result == ISC_R_SUCCESS);
4319 cache_find(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
4320 dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
4321 dns_dbnode_t **nodep, dns_name_t *foundname,
4322 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
4324 dns_rbtnode_t *node = NULL;
4325 isc_result_t result;
4326 rbtdb_search_t search;
4327 isc_boolean_t cname_ok = ISC_TRUE;
4328 isc_boolean_t empty_node;
4330 isc_rwlocktype_t locktype;
4331 rdatasetheader_t *header, *header_prev, *header_next;
4332 rdatasetheader_t *found, *nsheader;
4333 rdatasetheader_t *foundsig, *nssig, *cnamesig;
4334 rdatasetheader_t *update, *updatesig;
4335 rbtdb_rdatatype_t sigtype, negtype;
4339 search.rbtdb = (dns_rbtdb_t *)db;
4341 REQUIRE(VALID_RBTDB(search.rbtdb));
4342 REQUIRE(version == NULL);
4345 isc_stdtime_get(&now);
4347 search.rbtversion = NULL;
4349 search.options = options;
4350 search.copy_name = ISC_FALSE;
4351 search.need_cleanup = ISC_FALSE;
4352 search.wild = ISC_FALSE;
4353 search.zonecut = NULL;
4354 dns_fixedname_init(&search.zonecut_name);
4355 dns_rbtnodechain_init(&search.chain, search.rbtdb->common.mctx);
4360 RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
4363 * Search down from the root of the tree. If, while going down, we
4364 * encounter a callback node, cache_zonecut_callback() will search the
4365 * rdatasets at the zone cut for a DNAME rdataset.
4367 result = dns_rbt_findnode(search.rbtdb->tree, name, foundname, &node,
4368 &search.chain, DNS_RBTFIND_EMPTYDATA,
4369 cache_zonecut_callback, &search);
4371 if (result == DNS_R_PARTIALMATCH) {
4372 if ((search.options & DNS_DBFIND_COVERINGNSEC) != 0) {
4373 result = find_coveringnsec(&search, nodep, now,
4374 foundname, rdataset,
4376 if (result == DNS_R_COVERINGNSEC)
4379 if (search.zonecut != NULL) {
4380 result = setup_delegation(&search, nodep, foundname,
4381 rdataset, sigrdataset);
4385 result = find_deepest_zonecut(&search, node, nodep,
4386 foundname, rdataset,
4390 } else if (result != ISC_R_SUCCESS)
4394 * Certain DNSSEC types are not subject to CNAME matching
4395 * (RFC4035, section 2.5 and RFC3007).
4397 * We don't check for RRSIG, because we don't store RRSIG records
4400 if (type == dns_rdatatype_key || type == dns_rdatatype_nsec)
4401 cname_ok = ISC_FALSE;
4404 * We now go looking for rdata...
4407 lock = &(search.rbtdb->node_locks[node->locknum].lock);
4408 locktype = isc_rwlocktype_read;
4409 NODE_LOCK(lock, locktype);
4413 sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
4414 negtype = RBTDB_RDATATYPE_VALUE(0, type);
4418 empty_node = ISC_TRUE;
4420 for (header = node->data; header != NULL; header = header_next) {
4421 header_next = header->next;
4422 if (header->rdh_ttl <= now) {
4424 * This rdataset is stale. If no one else is using the
4425 * node, we can clean it up right now, otherwise we
4426 * mark it as stale, and the node as dirty, so it will
4427 * get cleaned up later.
4429 if ((header->rdh_ttl <= now - RBTDB_VIRTUAL) &&
4430 (locktype == isc_rwlocktype_write ||
4431 NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
4433 * We update the node's status only when we
4434 * can get write access.
4436 locktype = isc_rwlocktype_write;
4438 if (dns_rbtnode_refcurrent(node) == 0) {
4441 mctx = search.rbtdb->common.mctx;
4442 clean_stale_headers(search.rbtdb, mctx,
4444 if (header_prev != NULL)
4448 node->data = header->next;
4449 free_rdataset(search.rbtdb, mctx,
4452 header->attributes |=
4453 RDATASET_ATTR_STALE;
4455 header_prev = header;
4458 header_prev = header;
4459 } else if (EXISTS(header)) {
4461 * We now know that there is at least one active
4462 * non-stale rdataset at this node.
4464 empty_node = ISC_FALSE;
4467 * If we found a type we were looking for, remember
4470 if (header->type == type ||
4471 (type == dns_rdatatype_any &&
4472 RBTDB_RDATATYPE_BASE(header->type) != 0) ||
4473 (cname_ok && header->type ==
4474 dns_rdatatype_cname)) {
4476 * We've found the answer.
4479 if (header->type == dns_rdatatype_cname &&
4483 * If we've already got the CNAME RRSIG,
4484 * use it, otherwise change sigtype
4485 * so that we find it.
4487 if (cnamesig != NULL)
4488 foundsig = cnamesig;
4491 RBTDB_RDATATYPE_SIGCNAME;
4492 foundsig = cnamesig;
4494 } else if (header->type == sigtype) {
4496 * We've found the RRSIG rdataset for our
4497 * target type. Remember it.
4500 } else if (header->type == RBTDB_RDATATYPE_NCACHEANY ||
4501 header->type == negtype) {
4503 * We've found a negative cache entry.
4506 } else if (header->type == dns_rdatatype_ns) {
4508 * Remember a NS rdataset even if we're
4509 * not specifically looking for it, because
4510 * we might need it later.
4513 } else if (header->type == RBTDB_RDATATYPE_SIGNS) {
4515 * If we need the NS rdataset, we'll also
4516 * need its signature.
4519 } else if (cname_ok &&
4520 header->type == RBTDB_RDATATYPE_SIGCNAME) {
4522 * If we get a CNAME match, we'll also need
4527 header_prev = header;
4529 header_prev = header;
4534 * We have an exact match for the name, but there are no
4535 * extant rdatasets. That means that this node doesn't
4536 * meaningfully exist, and that we really have a partial match.
4538 NODE_UNLOCK(lock, locktype);
4543 * If we didn't find what we were looking for...
4545 if (found == NULL ||
4546 (DNS_TRUST_ADDITIONAL(found->trust) &&
4547 ((options & DNS_DBFIND_ADDITIONALOK) == 0)) ||
4548 (found->trust == dns_trust_glue &&
4549 ((options & DNS_DBFIND_GLUEOK) == 0)) ||
4550 (DNS_TRUST_PENDING(found->trust) &&
4551 ((options & DNS_DBFIND_PENDINGOK) == 0))) {
4553 * If there is an NS rdataset at this node, then this is the
4556 if (nsheader != NULL) {
4557 if (nodep != NULL) {
4558 new_reference(search.rbtdb, node);
4559 INSIST(!ISC_LINK_LINKED(node, deadlink));
4562 bind_rdataset(search.rbtdb, node, nsheader, search.now,
4564 if (need_headerupdate(nsheader, search.now))
4566 if (nssig != NULL) {
4567 bind_rdataset(search.rbtdb, node, nssig,
4568 search.now, sigrdataset);
4569 if (need_headerupdate(nssig, search.now))
4572 result = DNS_R_DELEGATION;
4577 * Go find the deepest zone cut.
4579 NODE_UNLOCK(lock, locktype);
4584 * We found what we were looking for, or we found a CNAME.
4587 if (nodep != NULL) {
4588 new_reference(search.rbtdb, node);
4589 INSIST(!ISC_LINK_LINKED(node, deadlink));
4593 if (RBTDB_RDATATYPE_BASE(found->type) == 0) {
4595 * We found a negative cache entry.
4597 if (NXDOMAIN(found))
4598 result = DNS_R_NCACHENXDOMAIN;
4600 result = DNS_R_NCACHENXRRSET;
4601 } else if (type != found->type &&
4602 type != dns_rdatatype_any &&
4603 found->type == dns_rdatatype_cname) {
4605 * We weren't doing an ANY query and we found a CNAME instead
4606 * of the type we were looking for, so we need to indicate
4607 * that result to the caller.
4609 result = DNS_R_CNAME;
4612 * An ordinary successful query!
4614 result = ISC_R_SUCCESS;
4617 if (type != dns_rdatatype_any || result == DNS_R_NCACHENXDOMAIN ||
4618 result == DNS_R_NCACHENXRRSET) {
4619 bind_rdataset(search.rbtdb, node, found, search.now,
4621 if (need_headerupdate(found, search.now))
4623 if (foundsig != NULL) {
4624 bind_rdataset(search.rbtdb, node, foundsig, search.now,
4626 if (need_headerupdate(foundsig, search.now))
4627 updatesig = foundsig;
4632 if ((update != NULL || updatesig != NULL) &&
4633 locktype != isc_rwlocktype_write) {
4634 NODE_UNLOCK(lock, locktype);
4635 NODE_LOCK(lock, isc_rwlocktype_write);
4636 locktype = isc_rwlocktype_write;
4638 if (update != NULL && need_headerupdate(update, search.now))
4639 update_header(search.rbtdb, update, search.now);
4640 if (updatesig != NULL && need_headerupdate(updatesig, search.now))
4641 update_header(search.rbtdb, updatesig, search.now);
4643 NODE_UNLOCK(lock, locktype);
4646 RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
4649 * If we found a zonecut but aren't going to use it, we have to
4652 if (search.need_cleanup) {
4653 node = search.zonecut;
4654 lock = &(search.rbtdb->node_locks[node->locknum].lock);
4656 NODE_LOCK(lock, isc_rwlocktype_read);
4657 decrement_reference(search.rbtdb, node, 0,
4658 isc_rwlocktype_read, isc_rwlocktype_none,
4660 NODE_UNLOCK(lock, isc_rwlocktype_read);
4663 dns_rbtnodechain_reset(&search.chain);
4669 cache_findzonecut(dns_db_t *db, dns_name_t *name, unsigned int options,
4670 isc_stdtime_t now, dns_dbnode_t **nodep,
4671 dns_name_t *foundname,
4672 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
4674 dns_rbtnode_t *node = NULL;
4676 isc_result_t result;
4677 rbtdb_search_t search;
4678 rdatasetheader_t *header, *header_prev, *header_next;
4679 rdatasetheader_t *found, *foundsig;
4680 unsigned int rbtoptions = DNS_RBTFIND_EMPTYDATA;
4681 isc_rwlocktype_t locktype;
4683 search.rbtdb = (dns_rbtdb_t *)db;
4685 REQUIRE(VALID_RBTDB(search.rbtdb));
4688 isc_stdtime_get(&now);
4690 search.rbtversion = NULL;
4692 search.options = options;
4693 search.copy_name = ISC_FALSE;
4694 search.need_cleanup = ISC_FALSE;
4695 search.wild = ISC_FALSE;
4696 search.zonecut = NULL;
4697 dns_fixedname_init(&search.zonecut_name);
4698 dns_rbtnodechain_init(&search.chain, search.rbtdb->common.mctx);
4701 if ((options & DNS_DBFIND_NOEXACT) != 0)
4702 rbtoptions |= DNS_RBTFIND_NOEXACT;
4704 RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
4707 * Search down from the root of the tree.
4709 result = dns_rbt_findnode(search.rbtdb->tree, name, foundname, &node,
4710 &search.chain, rbtoptions, NULL, &search);
4712 if (result == DNS_R_PARTIALMATCH) {
4714 result = find_deepest_zonecut(&search, node, nodep, foundname,
4715 rdataset, sigrdataset);
4717 } else if (result != ISC_R_SUCCESS)
4721 * We now go looking for an NS rdataset at the node.
4724 lock = &(search.rbtdb->node_locks[node->locknum].lock);
4725 locktype = isc_rwlocktype_read;
4726 NODE_LOCK(lock, locktype);
4731 for (header = node->data; header != NULL; header = header_next) {
4732 header_next = header->next;
4733 if (header->rdh_ttl <= now) {
4735 * This rdataset is stale. If no one else is using the
4736 * node, we can clean it up right now, otherwise we
4737 * mark it as stale, and the node as dirty, so it will
4738 * get cleaned up later.
4740 if ((header->rdh_ttl <= now - RBTDB_VIRTUAL) &&
4741 (locktype == isc_rwlocktype_write ||
4742 NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
4744 * We update the node's status only when we
4745 * can get write access.
4747 locktype = isc_rwlocktype_write;
4749 if (dns_rbtnode_refcurrent(node) == 0) {
4752 mctx = search.rbtdb->common.mctx;
4753 clean_stale_headers(search.rbtdb, mctx,
4755 if (header_prev != NULL)
4759 node->data = header->next;
4760 free_rdataset(search.rbtdb, mctx,
4763 header->attributes |=
4764 RDATASET_ATTR_STALE;
4766 header_prev = header;
4769 header_prev = header;
4770 } else if (EXISTS(header)) {
4772 * If we found a type we were looking for, remember
4775 if (header->type == dns_rdatatype_ns) {
4777 * Remember a NS rdataset even if we're
4778 * not specifically looking for it, because
4779 * we might need it later.
4782 } else if (header->type == RBTDB_RDATATYPE_SIGNS) {
4784 * If we need the NS rdataset, we'll also
4785 * need its signature.
4789 header_prev = header;
4791 header_prev = header;
4794 if (found == NULL) {
4796 * No NS records here.
4798 NODE_UNLOCK(lock, locktype);
4802 if (nodep != NULL) {
4803 new_reference(search.rbtdb, node);
4804 INSIST(!ISC_LINK_LINKED(node, deadlink));
4808 bind_rdataset(search.rbtdb, node, found, search.now, rdataset);
4809 if (foundsig != NULL)
4810 bind_rdataset(search.rbtdb, node, foundsig, search.now,
4813 if (need_headerupdate(found, search.now) ||
4814 (foundsig != NULL && need_headerupdate(foundsig, search.now))) {
4815 if (locktype != isc_rwlocktype_write) {
4816 NODE_UNLOCK(lock, locktype);
4817 NODE_LOCK(lock, isc_rwlocktype_write);
4818 locktype = isc_rwlocktype_write;
4820 if (need_headerupdate(found, search.now))
4821 update_header(search.rbtdb, found, search.now);
4822 if (foundsig != NULL &&
4823 need_headerupdate(foundsig, search.now)) {
4824 update_header(search.rbtdb, foundsig, search.now);
4828 NODE_UNLOCK(lock, locktype);
4831 RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
4833 INSIST(!search.need_cleanup);
4835 dns_rbtnodechain_reset(&search.chain);
4837 if (result == DNS_R_DELEGATION)
4838 result = ISC_R_SUCCESS;
4844 attachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) {
4845 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
4846 dns_rbtnode_t *node = (dns_rbtnode_t *)source;
4849 REQUIRE(VALID_RBTDB(rbtdb));
4850 REQUIRE(targetp != NULL && *targetp == NULL);
4852 NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
4853 dns_rbtnode_refincrement(node, &refs);
4855 NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
4861 detachnode(dns_db_t *db, dns_dbnode_t **targetp) {
4862 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
4863 dns_rbtnode_t *node;
4864 isc_boolean_t want_free = ISC_FALSE;
4865 isc_boolean_t inactive = ISC_FALSE;
4866 rbtdb_nodelock_t *nodelock;
4868 REQUIRE(VALID_RBTDB(rbtdb));
4869 REQUIRE(targetp != NULL && *targetp != NULL);
4871 node = (dns_rbtnode_t *)(*targetp);
4872 nodelock = &rbtdb->node_locks[node->locknum];
4874 NODE_LOCK(&nodelock->lock, isc_rwlocktype_read);
4876 if (decrement_reference(rbtdb, node, 0, isc_rwlocktype_read,
4877 isc_rwlocktype_none, ISC_FALSE)) {
4878 if (isc_refcount_current(&nodelock->references) == 0 &&
4879 nodelock->exiting) {
4880 inactive = ISC_TRUE;
4884 NODE_UNLOCK(&nodelock->lock, isc_rwlocktype_read);
4889 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
4891 if (rbtdb->active == 0)
4892 want_free = ISC_TRUE;
4893 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
4895 char buf[DNS_NAME_FORMATSIZE];
4896 if (dns_name_dynamic(&rbtdb->common.origin))
4897 dns_name_format(&rbtdb->common.origin, buf,
4900 strcpy(buf, "<UNKNOWN>");
4901 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
4902 DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
4903 "calling free_rbtdb(%s)", buf);
4904 free_rbtdb(rbtdb, ISC_TRUE, NULL);
4910 expirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {
4911 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
4912 dns_rbtnode_t *rbtnode = node;
4913 rdatasetheader_t *header;
4914 isc_boolean_t force_expire = ISC_FALSE;
4916 * These are the category and module used by the cache cleaner.
4918 isc_boolean_t log = ISC_FALSE;
4919 isc_logcategory_t *category = DNS_LOGCATEGORY_DATABASE;
4920 isc_logmodule_t *module = DNS_LOGMODULE_CACHE;
4921 int level = ISC_LOG_DEBUG(2);
4922 char printname[DNS_NAME_FORMATSIZE];
4924 REQUIRE(VALID_RBTDB(rbtdb));
4927 * Caller must hold a tree lock.
4931 isc_stdtime_get(&now);
4933 if (isc_mem_isovermem(rbtdb->common.mctx)) {
4936 isc_random_get(&val);
4938 * XXXDCL Could stand to have a better policy, like LRU.
4940 force_expire = ISC_TF(rbtnode->down == NULL && val % 4 == 0);
4943 * Note that 'log' can be true IFF overmem is also true.
4944 * overmem can currently only be true for cache
4945 * databases -- hence all of the "overmem cache" log strings.
4947 log = ISC_TF(isc_log_wouldlog(dns_lctx, level));
4949 isc_log_write(dns_lctx, category, module, level,
4950 "overmem cache: %s %s",
4951 force_expire ? "FORCE" : "check",
4952 dns_rbt_formatnodename(rbtnode,
4954 sizeof(printname)));
4958 * We may not need write access, but this code path is not performance
4959 * sensitive, so it should be okay to always lock as a writer.
4961 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
4962 isc_rwlocktype_write);
4964 for (header = rbtnode->data; header != NULL; header = header->next)
4965 if (header->rdh_ttl <= now - RBTDB_VIRTUAL) {
4967 * We don't check if refcurrent(rbtnode) == 0 and try
4968 * to free like we do in cache_find(), because
4969 * refcurrent(rbtnode) must be non-zero. This is so
4970 * because 'node' is an argument to the function.
4972 header->attributes |= RDATASET_ATTR_STALE;
4975 isc_log_write(dns_lctx, category, module,
4976 level, "overmem cache: stale %s",
4978 } else if (force_expire) {
4979 if (! RETAIN(header)) {
4980 set_ttl(rbtdb, header, 0);
4981 header->attributes |= RDATASET_ATTR_STALE;
4984 isc_log_write(dns_lctx, category, module,
4985 level, "overmem cache: "
4986 "reprieve by RETAIN() %s",
4989 } else if (isc_mem_isovermem(rbtdb->common.mctx) && log)
4990 isc_log_write(dns_lctx, category, module, level,
4991 "overmem cache: saved %s", printname);
4993 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
4994 isc_rwlocktype_write);
4996 return (ISC_R_SUCCESS);
5000 overmem(dns_db_t *db, isc_boolean_t overmem) {
5001 /* This is an empty callback. See adb.c:water() */
5010 printnode(dns_db_t *db, dns_dbnode_t *node, FILE *out) {
5011 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
5012 dns_rbtnode_t *rbtnode = node;
5013 isc_boolean_t first;
5015 REQUIRE(VALID_RBTDB(rbtdb));
5017 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
5018 isc_rwlocktype_read);
5020 fprintf(out, "node %p, %u references, locknum = %u\n",
5021 rbtnode, dns_rbtnode_refcurrent(rbtnode),
5023 if (rbtnode->data != NULL) {
5024 rdatasetheader_t *current, *top_next;
5026 for (current = rbtnode->data; current != NULL;
5027 current = top_next) {
5028 top_next = current->next;
5030 fprintf(out, "\ttype %u", current->type);
5036 "\tserial = %lu, ttl = %u, "
5037 "trust = %u, attributes = %u, "
5039 (unsigned long)current->serial,
5042 current->attributes,
5044 current = current->down;
5045 } while (current != NULL);
5048 fprintf(out, "(empty)\n");
5050 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
5051 isc_rwlocktype_read);
5055 createiterator(dns_db_t *db, unsigned int options, dns_dbiterator_t **iteratorp)
5057 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
5058 rbtdb_dbiterator_t *rbtdbiter;
5060 REQUIRE(VALID_RBTDB(rbtdb));
5062 rbtdbiter = isc_mem_get(rbtdb->common.mctx, sizeof(*rbtdbiter));
5063 if (rbtdbiter == NULL)
5064 return (ISC_R_NOMEMORY);
5066 rbtdbiter->common.methods = &dbiterator_methods;
5067 rbtdbiter->common.db = NULL;
5068 dns_db_attach(db, &rbtdbiter->common.db);
5069 rbtdbiter->common.relative_names =
5070 ISC_TF((options & DNS_DB_RELATIVENAMES) != 0);
5071 rbtdbiter->common.magic = DNS_DBITERATOR_MAGIC;
5072 rbtdbiter->common.cleaning = ISC_FALSE;
5073 rbtdbiter->paused = ISC_TRUE;
5074 rbtdbiter->tree_locked = isc_rwlocktype_none;
5075 rbtdbiter->result = ISC_R_SUCCESS;
5076 dns_fixedname_init(&rbtdbiter->name);
5077 dns_fixedname_init(&rbtdbiter->origin);
5078 rbtdbiter->node = NULL;
5079 rbtdbiter->delete = 0;
5080 rbtdbiter->nsec3only = ISC_TF((options & DNS_DB_NSEC3ONLY) != 0);
5081 rbtdbiter->nonsec3 = ISC_TF((options & DNS_DB_NONSEC3) != 0);
5082 memset(rbtdbiter->deletions, 0, sizeof(rbtdbiter->deletions));
5083 dns_rbtnodechain_init(&rbtdbiter->chain, db->mctx);
5084 dns_rbtnodechain_init(&rbtdbiter->nsec3chain, db->mctx);
5085 if (rbtdbiter->nsec3only)
5086 rbtdbiter->current = &rbtdbiter->nsec3chain;
5088 rbtdbiter->current = &rbtdbiter->chain;
5090 *iteratorp = (dns_dbiterator_t *)rbtdbiter;
5092 return (ISC_R_SUCCESS);
5096 zone_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
5097 dns_rdatatype_t type, dns_rdatatype_t covers,
5098 isc_stdtime_t now, dns_rdataset_t *rdataset,
5099 dns_rdataset_t *sigrdataset)
5101 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
5102 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
5103 rdatasetheader_t *header, *header_next, *found, *foundsig;
5104 rbtdb_serial_t serial;
5105 rbtdb_version_t *rbtversion = version;
5106 isc_boolean_t close_version = ISC_FALSE;
5107 rbtdb_rdatatype_t matchtype, sigmatchtype;
5109 REQUIRE(VALID_RBTDB(rbtdb));
5110 REQUIRE(type != dns_rdatatype_any);
5112 if (rbtversion == NULL) {
5113 currentversion(db, (dns_dbversion_t **) (void *)(&rbtversion));
5114 close_version = ISC_TRUE;
5116 serial = rbtversion->serial;
5119 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
5120 isc_rwlocktype_read);
5124 matchtype = RBTDB_RDATATYPE_VALUE(type, covers);
5126 sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
5130 for (header = rbtnode->data; header != NULL; header = header_next) {
5131 header_next = header->next;
5133 if (header->serial <= serial &&
5136 * Is this a "this rdataset doesn't
5139 if (NONEXISTENT(header))
5143 header = header->down;
5144 } while (header != NULL);
5145 if (header != NULL) {
5147 * We have an active, extant rdataset. If it's a
5148 * type we're looking for, remember it.
5150 if (header->type == matchtype) {
5152 if (foundsig != NULL)
5154 } else if (header->type == sigmatchtype) {
5161 if (found != NULL) {
5162 bind_rdataset(rbtdb, rbtnode, found, now, rdataset);
5163 if (foundsig != NULL)
5164 bind_rdataset(rbtdb, rbtnode, foundsig, now,
5168 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
5169 isc_rwlocktype_read);
5172 closeversion(db, (dns_dbversion_t **) (void *)(&rbtversion),
5176 return (ISC_R_NOTFOUND);
5178 return (ISC_R_SUCCESS);
5182 cache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
5183 dns_rdatatype_t type, dns_rdatatype_t covers,
5184 isc_stdtime_t now, dns_rdataset_t *rdataset,
5185 dns_rdataset_t *sigrdataset)
5187 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
5188 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
5189 rdatasetheader_t *header, *header_next, *found, *foundsig;
5190 rbtdb_rdatatype_t matchtype, sigmatchtype, negtype;
5191 isc_result_t result;
5193 isc_rwlocktype_t locktype;
5195 REQUIRE(VALID_RBTDB(rbtdb));
5196 REQUIRE(type != dns_rdatatype_any);
5200 result = ISC_R_SUCCESS;
5203 isc_stdtime_get(&now);
5205 lock = &rbtdb->node_locks[rbtnode->locknum].lock;
5206 locktype = isc_rwlocktype_read;
5207 NODE_LOCK(lock, locktype);
5211 matchtype = RBTDB_RDATATYPE_VALUE(type, covers);
5212 negtype = RBTDB_RDATATYPE_VALUE(0, type);
5214 sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
5218 for (header = rbtnode->data; header != NULL; header = header_next) {
5219 header_next = header->next;
5220 if (header->rdh_ttl <= now) {
5221 if ((header->rdh_ttl <= now - RBTDB_VIRTUAL) &&
5222 (locktype == isc_rwlocktype_write ||
5223 NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
5225 * We update the node's status only when we
5226 * can get write access.
5228 locktype = isc_rwlocktype_write;
5231 * We don't check if refcurrent(rbtnode) == 0
5232 * and try to free like we do in cache_find(),
5233 * because refcurrent(rbtnode) must be
5234 * non-zero. This is so because 'node' is an
5235 * argument to the function.
5237 header->attributes |= RDATASET_ATTR_STALE;
5240 } else if (EXISTS(header)) {
5241 if (header->type == matchtype)
5243 else if (header->type == RBTDB_RDATATYPE_NCACHEANY ||
5244 header->type == negtype)
5246 else if (header->type == sigmatchtype)
5250 if (found != NULL) {
5251 bind_rdataset(rbtdb, rbtnode, found, now, rdataset);
5252 if (foundsig != NULL)
5253 bind_rdataset(rbtdb, rbtnode, foundsig, now,
5257 NODE_UNLOCK(lock, locktype);
5260 return (ISC_R_NOTFOUND);
5262 if (RBTDB_RDATATYPE_BASE(found->type) == 0) {
5264 * We found a negative cache entry.
5266 if (NXDOMAIN(found))
5267 result = DNS_R_NCACHENXDOMAIN;
5269 result = DNS_R_NCACHENXRRSET;
5276 allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
5277 isc_stdtime_t now, dns_rdatasetiter_t **iteratorp)
5279 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
5280 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
5281 rbtdb_version_t *rbtversion = version;
5282 rbtdb_rdatasetiter_t *iterator;
5285 REQUIRE(VALID_RBTDB(rbtdb));
5287 iterator = isc_mem_get(rbtdb->common.mctx, sizeof(*iterator));
5288 if (iterator == NULL)
5289 return (ISC_R_NOMEMORY);
5291 if ((db->attributes & DNS_DBATTR_CACHE) == 0) {
5293 if (rbtversion == NULL)
5295 (dns_dbversion_t **) (void *)(&rbtversion));
5299 isc_refcount_increment(&rbtversion->references,
5305 isc_stdtime_get(&now);
5309 iterator->common.magic = DNS_RDATASETITER_MAGIC;
5310 iterator->common.methods = &rdatasetiter_methods;
5311 iterator->common.db = db;
5312 iterator->common.node = node;
5313 iterator->common.version = (dns_dbversion_t *)rbtversion;
5314 iterator->common.now = now;
5316 NODE_STRONGLOCK(&rbtdb->node_locks[rbtnode->locknum].lock);
5318 dns_rbtnode_refincrement(rbtnode, &refs);
5321 iterator->current = NULL;
5323 NODE_STRONGUNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock);
5325 *iteratorp = (dns_rdatasetiter_t *)iterator;
5327 return (ISC_R_SUCCESS);
5330 static isc_boolean_t
5331 cname_and_other_data(dns_rbtnode_t *node, rbtdb_serial_t serial) {
5332 rdatasetheader_t *header, *header_next;
5333 isc_boolean_t cname, other_data;
5334 dns_rdatatype_t rdtype;
5337 * The caller must hold the node lock.
5341 * Look for CNAME and "other data" rdatasets active in our version.
5344 other_data = ISC_FALSE;
5345 for (header = node->data; header != NULL; header = header_next) {
5346 header_next = header->next;
5347 if (header->type == dns_rdatatype_cname) {
5349 * Look for an active extant CNAME.
5352 if (header->serial <= serial &&
5355 * Is this a "this rdataset doesn't
5358 if (NONEXISTENT(header))
5362 header = header->down;
5363 } while (header != NULL);
5368 * Look for active extant "other data".
5370 * "Other data" is any rdataset whose type is not
5371 * KEY, NSEC, SIG or RRSIG.
5373 rdtype = RBTDB_RDATATYPE_BASE(header->type);
5374 if (rdtype != dns_rdatatype_key &&
5375 rdtype != dns_rdatatype_sig &&
5376 rdtype != dns_rdatatype_nsec &&
5377 rdtype != dns_rdatatype_rrsig) {
5379 * Is it active and extant?
5382 if (header->serial <= serial &&
5385 * Is this a "this rdataset
5386 * doesn't exist" record?
5388 if (NONEXISTENT(header))
5392 header = header->down;
5393 } while (header != NULL);
5395 other_data = ISC_TRUE;
5400 if (cname && other_data)
5407 resign_insert(dns_rbtdb_t *rbtdb, int idx, rdatasetheader_t *newheader) {
5408 isc_result_t result;
5410 INSIST(!IS_CACHE(rbtdb));
5411 INSIST(newheader->heap_index == 0);
5412 INSIST(!ISC_LINK_LINKED(newheader, link));
5414 result = isc_heap_insert(rbtdb->heaps[idx], newheader);
5419 add(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
5420 rdatasetheader_t *newheader, unsigned int options, isc_boolean_t loading,
5421 dns_rdataset_t *addedrdataset, isc_stdtime_t now)
5423 rbtdb_changed_t *changed = NULL;
5424 rdatasetheader_t *topheader, *topheader_prev, *header, *sigheader;
5425 unsigned char *merged;
5426 isc_result_t result;
5427 isc_boolean_t header_nx;
5428 isc_boolean_t newheader_nx;
5429 isc_boolean_t merge;
5430 dns_rdatatype_t rdtype, covers;
5431 rbtdb_rdatatype_t negtype, sigtype;
5436 * Add an rdatasetheader_t to a node.
5440 * Caller must be holding the node lock.
5443 if ((options & DNS_DBADD_MERGE) != 0) {
5444 REQUIRE(rbtversion != NULL);
5449 if ((options & DNS_DBADD_FORCE) != 0)
5450 trust = dns_trust_ultimate;
5452 trust = newheader->trust;
5454 if (rbtversion != NULL && !loading) {
5456 * We always add a changed record, even if no changes end up
5457 * being made to this node, because it's harmless and
5458 * simplifies the code.
5460 changed = add_changed(rbtdb, rbtversion, rbtnode);
5461 if (changed == NULL) {
5462 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5463 return (ISC_R_NOMEMORY);
5467 newheader_nx = NONEXISTENT(newheader) ? ISC_TRUE : ISC_FALSE;
5468 topheader_prev = NULL;
5471 if (rbtversion == NULL && !newheader_nx) {
5472 rdtype = RBTDB_RDATATYPE_BASE(newheader->type);
5475 * We're adding a negative cache entry.
5477 covers = RBTDB_RDATATYPE_EXT(newheader->type);
5478 sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig,
5480 for (topheader = rbtnode->data;
5482 topheader = topheader->next) {
5484 * If we're adding an negative cache entry
5485 * which covers all types (NXDOMAIN,
5486 * NODATA(QTYPE=ANY)).
5488 * We make all other data stale so that the
5489 * only rdataset that can be found at this
5490 * node is the negative cache entry.
5492 * Otherwise look for any RRSIGs of the
5493 * given type so they can be marked stale
5496 if (covers == dns_rdatatype_any) {
5497 set_ttl(rbtdb, topheader, 0);
5498 topheader->attributes |=
5499 RDATASET_ATTR_STALE;
5501 } else if (topheader->type == sigtype)
5502 sigheader = topheader;
5504 if (covers == dns_rdatatype_any)
5506 negtype = RBTDB_RDATATYPE_VALUE(covers, 0);
5509 * We're adding something that isn't a
5510 * negative cache entry. Look for an extant
5511 * non-stale NXDOMAIN/NODATA(QTYPE=ANY) negative
5514 for (topheader = rbtnode->data;
5516 topheader = topheader->next) {
5517 if (topheader->type ==
5518 RBTDB_RDATATYPE_NCACHEANY)
5521 if (topheader != NULL && EXISTS(topheader) &&
5522 topheader->rdh_ttl > now) {
5526 if (trust < topheader->trust) {
5528 * The NXDOMAIN/NODATA(QTYPE=ANY)
5531 free_rdataset(rbtdb,
5534 if (addedrdataset != NULL)
5535 bind_rdataset(rbtdb, rbtnode,
5538 return (DNS_R_UNCHANGED);
5541 * The new rdataset is better. Expire the
5542 * NXDOMAIN/NODATA(QTYPE=ANY).
5544 set_ttl(rbtdb, topheader, 0);
5545 topheader->attributes |= RDATASET_ATTR_STALE;
5550 negtype = RBTDB_RDATATYPE_VALUE(0, rdtype);
5554 for (topheader = rbtnode->data;
5556 topheader = topheader->next) {
5557 if (topheader->type == newheader->type ||
5558 topheader->type == negtype)
5560 topheader_prev = topheader;
5565 * If header isn't NULL, we've found the right type. There may be
5566 * IGNORE rdatasets between the top of the chain and the first real
5567 * data. We skip over them.
5570 while (header != NULL && IGNORE(header))
5571 header = header->down;
5572 if (header != NULL) {
5573 header_nx = NONEXISTENT(header) ? ISC_TRUE : ISC_FALSE;
5576 * Deleting an already non-existent rdataset has no effect.
5578 if (header_nx && newheader_nx) {
5579 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5580 return (DNS_R_UNCHANGED);
5584 * Trying to add an rdataset with lower trust to a cache DB
5585 * has no effect, provided that the cache data isn't stale.
5587 if (rbtversion == NULL && trust < header->trust &&
5588 (header->rdh_ttl > now || header_nx)) {
5589 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5590 if (addedrdataset != NULL)
5591 bind_rdataset(rbtdb, rbtnode, header, now,
5593 return (DNS_R_UNCHANGED);
5597 * Don't merge if a nonexistent rdataset is involved.
5599 if (merge && (header_nx || newheader_nx))
5603 * If 'merge' is ISC_TRUE, we'll try to create a new rdataset
5604 * that is the union of 'newheader' and 'header'.
5607 unsigned int flags = 0;
5608 INSIST(rbtversion->serial >= header->serial);
5610 result = ISC_R_SUCCESS;
5612 if ((options & DNS_DBADD_EXACT) != 0)
5613 flags |= DNS_RDATASLAB_EXACT;
5614 if ((options & DNS_DBADD_EXACTTTL) != 0 &&
5615 newheader->rdh_ttl != header->rdh_ttl)
5616 result = DNS_R_NOTEXACT;
5617 else if (newheader->rdh_ttl != header->rdh_ttl)
5618 flags |= DNS_RDATASLAB_FORCE;
5619 if (result == ISC_R_SUCCESS)
5620 result = dns_rdataslab_merge(
5621 (unsigned char *)header,
5622 (unsigned char *)newheader,
5623 (unsigned int)(sizeof(*newheader)),
5625 rbtdb->common.rdclass,
5626 (dns_rdatatype_t)header->type,
5628 if (result == ISC_R_SUCCESS) {
5630 * If 'header' has the same serial number as
5631 * we do, we could clean it up now if we knew
5632 * that our caller had no references to it.
5633 * We don't know this, however, so we leave it
5634 * alone. It will get cleaned up when
5635 * clean_zone_node() runs.
5637 free_rdataset(rbtdb, rbtdb->common.mctx,
5639 newheader = (rdatasetheader_t *)merged;
5640 if (loading && RESIGN(newheader) &&
5642 header->resign < newheader->resign)
5643 newheader->resign = header->resign;
5645 free_rdataset(rbtdb, rbtdb->common.mctx,
5651 * Don't replace existing NS, A and AAAA RRsets
5652 * in the cache if they are already exist. This
5653 * prevents named being locked to old servers.
5654 * Don't lower trust of existing record if the
5657 if (IS_CACHE(rbtdb) && header->rdh_ttl > now &&
5658 header->type == dns_rdatatype_ns &&
5659 !header_nx && !newheader_nx &&
5660 header->trust >= newheader->trust &&
5661 dns_rdataslab_equalx((unsigned char *)header,
5662 (unsigned char *)newheader,
5663 (unsigned int)(sizeof(*newheader)),
5664 rbtdb->common.rdclass,
5665 (dns_rdatatype_t)header->type)) {
5667 * Honour the new ttl if it is less than the
5670 if (header->rdh_ttl > newheader->rdh_ttl)
5671 set_ttl(rbtdb, header, newheader->rdh_ttl);
5672 if (header->noqname == NULL &&
5673 newheader->noqname != NULL) {
5674 header->noqname = newheader->noqname;
5675 newheader->noqname = NULL;
5677 if (header->closest == NULL &&
5678 newheader->closest != NULL) {
5679 header->closest = newheader->closest;
5680 newheader->closest = NULL;
5682 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5683 if (addedrdataset != NULL)
5684 bind_rdataset(rbtdb, rbtnode, header, now,
5686 return (ISC_R_SUCCESS);
5688 if (IS_CACHE(rbtdb) && header->rdh_ttl > now &&
5689 (header->type == dns_rdatatype_a ||
5690 header->type == dns_rdatatype_aaaa) &&
5691 !header_nx && !newheader_nx &&
5692 header->trust >= newheader->trust &&
5693 dns_rdataslab_equal((unsigned char *)header,
5694 (unsigned char *)newheader,
5695 (unsigned int)(sizeof(*newheader)))) {
5697 * Honour the new ttl if it is less than the
5700 if (header->rdh_ttl > newheader->rdh_ttl)
5701 set_ttl(rbtdb, header, newheader->rdh_ttl);
5702 if (header->noqname == NULL &&
5703 newheader->noqname != NULL) {
5704 header->noqname = newheader->noqname;
5705 newheader->noqname = NULL;
5707 if (header->closest == NULL &&
5708 newheader->closest != NULL) {
5709 header->closest = newheader->closest;
5710 newheader->closest = NULL;
5712 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5713 if (addedrdataset != NULL)
5714 bind_rdataset(rbtdb, rbtnode, header, now,
5716 return (ISC_R_SUCCESS);
5718 INSIST(rbtversion == NULL ||
5719 rbtversion->serial >= topheader->serial);
5720 if (topheader_prev != NULL)
5721 topheader_prev->next = newheader;
5723 rbtnode->data = newheader;
5724 newheader->next = topheader->next;
5727 * There are no other references to 'header' when
5728 * loading, so we MAY clean up 'header' now.
5729 * Since we don't generate changed records when
5730 * loading, we MUST clean up 'header' now.
5732 newheader->down = NULL;
5733 free_rdataset(rbtdb, rbtdb->common.mctx, header);
5735 newheader->down = topheader;
5736 topheader->next = newheader;
5738 if (changed != NULL)
5739 changed->dirty = ISC_TRUE;
5740 if (rbtversion == NULL) {
5741 set_ttl(rbtdb, header, 0);
5742 header->attributes |= RDATASET_ATTR_STALE;
5743 if (sigheader != NULL) {
5744 set_ttl(rbtdb, sigheader, 0);
5745 sigheader->attributes |=
5746 RDATASET_ATTR_STALE;
5749 idx = newheader->node->locknum;
5750 if (IS_CACHE(rbtdb)) {
5751 ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
5754 * XXXMLG We don't check the return value
5755 * here. If it fails, we will not do TTL
5756 * based expiry on this node. However, we
5757 * will do it on the LRU side, so memory
5758 * will not leak... for long.
5760 isc_heap_insert(rbtdb->heaps[idx], newheader);
5761 } else if (RESIGN(newheader))
5762 resign_insert(rbtdb, idx, newheader);
5766 * No non-IGNORED rdatasets of the given type exist at
5771 * If we're trying to delete the type, don't bother.
5774 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5775 return (DNS_R_UNCHANGED);
5778 if (topheader != NULL) {
5780 * We have an list of rdatasets of the given type,
5781 * but they're all marked IGNORE. We simply insert
5782 * the new rdataset at the head of the list.
5784 * Ignored rdatasets cannot occur during loading, so
5788 INSIST(rbtversion == NULL ||
5789 rbtversion->serial >= topheader->serial);
5790 if (topheader_prev != NULL)
5791 topheader_prev->next = newheader;
5793 rbtnode->data = newheader;
5794 newheader->next = topheader->next;
5795 newheader->down = topheader;
5796 topheader->next = newheader;
5798 if (changed != NULL)
5799 changed->dirty = ISC_TRUE;
5802 * No rdatasets of the given type exist at the node.
5804 newheader->next = rbtnode->data;
5805 newheader->down = NULL;
5806 rbtnode->data = newheader;
5808 idx = newheader->node->locknum;
5809 if (IS_CACHE(rbtdb)) {
5810 ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
5812 isc_heap_insert(rbtdb->heaps[idx], newheader);
5813 } else if (RESIGN(newheader)) {
5814 resign_insert(rbtdb, idx, newheader);
5819 * Check if the node now contains CNAME and other data.
5821 if (rbtversion != NULL &&
5822 cname_and_other_data(rbtnode, rbtversion->serial))
5823 return (DNS_R_CNAMEANDOTHER);
5825 if (addedrdataset != NULL)
5826 bind_rdataset(rbtdb, rbtnode, newheader, now, addedrdataset);
5828 return (ISC_R_SUCCESS);
5831 static inline isc_boolean_t
5832 delegating_type(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
5833 rbtdb_rdatatype_t type)
5835 if (IS_CACHE(rbtdb)) {
5836 if (type == dns_rdatatype_dname)
5840 } else if (type == dns_rdatatype_dname ||
5841 (type == dns_rdatatype_ns &&
5842 (node != rbtdb->origin_node || IS_STUB(rbtdb))))
5847 static inline isc_result_t
5848 addnoqname(dns_rbtdb_t *rbtdb, rdatasetheader_t *newheader,
5849 dns_rdataset_t *rdataset)
5851 struct noqname *noqname;
5852 isc_mem_t *mctx = rbtdb->common.mctx;
5854 dns_rdataset_t neg, negsig;
5855 isc_result_t result;
5858 dns_name_init(&name, NULL);
5859 dns_rdataset_init(&neg);
5860 dns_rdataset_init(&negsig);
5862 result = dns_rdataset_getnoqname(rdataset, &name, &neg, &negsig);
5863 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5865 noqname = isc_mem_get(mctx, sizeof(*noqname));
5866 if (noqname == NULL) {
5867 result = ISC_R_NOMEMORY;
5870 dns_name_init(&noqname->name, NULL);
5871 noqname->neg = NULL;
5872 noqname->negsig = NULL;
5873 noqname->type = neg.type;
5874 result = dns_name_dup(&name, mctx, &noqname->name);
5875 if (result != ISC_R_SUCCESS)
5877 result = dns_rdataslab_fromrdataset(&neg, mctx, &r, 0);
5878 if (result != ISC_R_SUCCESS)
5880 noqname->neg = r.base;
5881 result = dns_rdataslab_fromrdataset(&negsig, mctx, &r, 0);
5882 if (result != ISC_R_SUCCESS)
5884 noqname->negsig = r.base;
5885 dns_rdataset_disassociate(&neg);
5886 dns_rdataset_disassociate(&negsig);
5887 newheader->noqname = noqname;
5888 return (ISC_R_SUCCESS);
5891 dns_rdataset_disassociate(&neg);
5892 dns_rdataset_disassociate(&negsig);
5893 free_noqname(mctx, &noqname);
5897 static inline isc_result_t
5898 addclosest(dns_rbtdb_t *rbtdb, rdatasetheader_t *newheader,
5899 dns_rdataset_t *rdataset)
5901 struct noqname *closest;
5902 isc_mem_t *mctx = rbtdb->common.mctx;
5904 dns_rdataset_t neg, negsig;
5905 isc_result_t result;
5908 dns_name_init(&name, NULL);
5909 dns_rdataset_init(&neg);
5910 dns_rdataset_init(&negsig);
5912 result = dns_rdataset_getclosest(rdataset, &name, &neg, &negsig);
5913 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5915 closest = isc_mem_get(mctx, sizeof(*closest));
5916 if (closest == NULL) {
5917 result = ISC_R_NOMEMORY;
5920 dns_name_init(&closest->name, NULL);
5921 closest->neg = NULL;
5922 closest->negsig = NULL;
5923 closest->type = neg.type;
5924 result = dns_name_dup(&name, mctx, &closest->name);
5925 if (result != ISC_R_SUCCESS)
5927 result = dns_rdataslab_fromrdataset(&neg, mctx, &r, 0);
5928 if (result != ISC_R_SUCCESS)
5930 closest->neg = r.base;
5931 result = dns_rdataslab_fromrdataset(&negsig, mctx, &r, 0);
5932 if (result != ISC_R_SUCCESS)
5934 closest->negsig = r.base;
5935 dns_rdataset_disassociate(&neg);
5936 dns_rdataset_disassociate(&negsig);
5937 newheader->closest = closest;
5938 return (ISC_R_SUCCESS);
5941 dns_rdataset_disassociate(&neg);
5942 dns_rdataset_disassociate(&negsig);
5943 free_noqname(mctx, &closest);
5947 static dns_dbmethods_t zone_methods;
5950 addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
5951 isc_stdtime_t now, dns_rdataset_t *rdataset, unsigned int options,
5952 dns_rdataset_t *addedrdataset)
5954 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
5955 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
5956 rbtdb_version_t *rbtversion = version;
5957 isc_region_t region;
5958 rdatasetheader_t *newheader;
5959 rdatasetheader_t *header;
5960 isc_result_t result;
5961 isc_boolean_t delegating;
5962 isc_boolean_t tree_locked = ISC_FALSE;
5963 isc_boolean_t cache_is_overmem = ISC_FALSE;
5965 REQUIRE(VALID_RBTDB(rbtdb));
5967 if (rbtdb->common.methods == &zone_methods)
5968 REQUIRE(((rbtnode->nsec3 &&
5969 (rdataset->type == dns_rdatatype_nsec3 ||
5970 rdataset->covers == dns_rdatatype_nsec3)) ||
5972 rdataset->type != dns_rdatatype_nsec3 &&
5973 rdataset->covers != dns_rdatatype_nsec3)));
5975 if (rbtversion == NULL) {
5977 isc_stdtime_get(&now);
5981 result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
5983 sizeof(rdatasetheader_t));
5984 if (result != ISC_R_SUCCESS)
5987 newheader = (rdatasetheader_t *)region.base;
5988 init_rdataset(rbtdb, newheader);
5989 set_ttl(rbtdb, newheader, rdataset->ttl + now);
5990 newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
5992 newheader->attributes = 0;
5993 newheader->noqname = NULL;
5994 newheader->closest = NULL;
5995 newheader->count = init_count++;
5996 newheader->trust = rdataset->trust;
5997 newheader->additional_auth = NULL;
5998 newheader->additional_glue = NULL;
5999 newheader->last_used = now;
6000 newheader->node = rbtnode;
6001 if (rbtversion != NULL) {
6002 newheader->serial = rbtversion->serial;
6005 if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
6006 newheader->attributes |= RDATASET_ATTR_RESIGN;
6007 newheader->resign = rdataset->resign;
6009 newheader->resign = 0;
6011 newheader->serial = 1;
6012 newheader->resign = 0;
6013 if ((rdataset->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
6014 newheader->attributes |= RDATASET_ATTR_NXDOMAIN;
6015 if ((rdataset->attributes & DNS_RDATASETATTR_OPTOUT) != 0)
6016 newheader->attributes |= RDATASET_ATTR_OPTOUT;
6017 if ((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0) {
6018 result = addnoqname(rbtdb, newheader, rdataset);
6019 if (result != ISC_R_SUCCESS) {
6020 free_rdataset(rbtdb, rbtdb->common.mctx,
6025 if ((rdataset->attributes & DNS_RDATASETATTR_CLOSEST) != 0) {
6026 result = addclosest(rbtdb, newheader, rdataset);
6027 if (result != ISC_R_SUCCESS) {
6028 free_rdataset(rbtdb, rbtdb->common.mctx,
6036 * If we're adding a delegation type (e.g. NS or DNAME for a zone,
6037 * just DNAME for the cache), then we need to set the callback bit
6040 if (delegating_type(rbtdb, rbtnode, rdataset->type))
6041 delegating = ISC_TRUE;
6043 delegating = ISC_FALSE;
6046 * If we're adding a delegation type or the DB is a cache in an overmem
6047 * state, hold an exclusive lock on the tree. In the latter case
6048 * the lock does not necessarily have to be acquired but it will help
6049 * purge stale entries more effectively.
6051 if (IS_CACHE(rbtdb) && isc_mem_isovermem(rbtdb->common.mctx))
6052 cache_is_overmem = ISC_TRUE;
6053 if (delegating || cache_is_overmem) {
6054 tree_locked = ISC_TRUE;
6055 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
6058 if (cache_is_overmem)
6059 overmem_purge(rbtdb, rbtnode->locknum, now, tree_locked);
6061 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6062 isc_rwlocktype_write);
6064 if (rbtdb->rrsetstats != NULL) {
6065 newheader->attributes |= RDATASET_ATTR_STATCOUNT;
6066 update_rrsetstats(rbtdb, newheader, ISC_TRUE);
6069 if (IS_CACHE(rbtdb)) {
6071 cleanup_dead_nodes(rbtdb, rbtnode->locknum);
6073 header = isc_heap_element(rbtdb->heaps[rbtnode->locknum], 1);
6074 if (header && header->rdh_ttl <= now - RBTDB_VIRTUAL)
6075 expire_header(rbtdb, header, tree_locked);
6078 * If we've been holding a write lock on the tree just for
6079 * cleaning, we can release it now. However, we still need the
6082 if (tree_locked && !delegating) {
6083 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
6084 tree_locked = ISC_FALSE;
6088 result = add(rbtdb, rbtnode, rbtversion, newheader, options, ISC_FALSE,
6089 addedrdataset, now);
6090 if (result == ISC_R_SUCCESS && delegating)
6091 rbtnode->find_callback = 1;
6093 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6094 isc_rwlocktype_write);
6097 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
6100 * Update the zone's secure status. If version is non-NULL
6101 * this is deferred until closeversion() is called.
6103 if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
6104 iszonesecure(db, version, rbtdb->origin_node);
6110 subtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
6111 dns_rdataset_t *rdataset, unsigned int options,
6112 dns_rdataset_t *newrdataset)
6114 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6115 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
6116 rbtdb_version_t *rbtversion = version;
6117 rdatasetheader_t *topheader, *topheader_prev, *header, *newheader;
6118 unsigned char *subresult;
6119 isc_region_t region;
6120 isc_result_t result;
6121 rbtdb_changed_t *changed;
6123 REQUIRE(VALID_RBTDB(rbtdb));
6125 if (rbtdb->common.methods == &zone_methods)
6126 REQUIRE(((rbtnode->nsec3 &&
6127 (rdataset->type == dns_rdatatype_nsec3 ||
6128 rdataset->covers == dns_rdatatype_nsec3)) ||
6130 rdataset->type != dns_rdatatype_nsec3 &&
6131 rdataset->covers != dns_rdatatype_nsec3)));
6133 result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
6135 sizeof(rdatasetheader_t));
6136 if (result != ISC_R_SUCCESS)
6138 newheader = (rdatasetheader_t *)region.base;
6139 init_rdataset(rbtdb, newheader);
6140 set_ttl(rbtdb, newheader, rdataset->ttl);
6141 newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
6143 newheader->attributes = 0;
6144 newheader->serial = rbtversion->serial;
6145 newheader->trust = 0;
6146 newheader->noqname = NULL;
6147 newheader->closest = NULL;
6148 newheader->count = init_count++;
6149 newheader->additional_auth = NULL;
6150 newheader->additional_glue = NULL;
6151 newheader->last_used = 0;
6152 newheader->node = rbtnode;
6153 if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
6154 newheader->attributes |= RDATASET_ATTR_RESIGN;
6155 newheader->resign = rdataset->resign;
6157 newheader->resign = 0;
6159 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6160 isc_rwlocktype_write);
6162 changed = add_changed(rbtdb, rbtversion, rbtnode);
6163 if (changed == NULL) {
6164 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6165 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6166 isc_rwlocktype_write);
6167 return (ISC_R_NOMEMORY);
6170 topheader_prev = NULL;
6171 for (topheader = rbtnode->data;
6173 topheader = topheader->next) {
6174 if (topheader->type == newheader->type)
6176 topheader_prev = topheader;
6179 * If header isn't NULL, we've found the right type. There may be
6180 * IGNORE rdatasets between the top of the chain and the first real
6181 * data. We skip over them.
6184 while (header != NULL && IGNORE(header))
6185 header = header->down;
6186 if (header != NULL && EXISTS(header)) {
6187 unsigned int flags = 0;
6189 result = ISC_R_SUCCESS;
6190 if ((options & DNS_DBSUB_EXACT) != 0) {
6191 flags |= DNS_RDATASLAB_EXACT;
6192 if (newheader->rdh_ttl != header->rdh_ttl)
6193 result = DNS_R_NOTEXACT;
6195 if (result == ISC_R_SUCCESS)
6196 result = dns_rdataslab_subtract(
6197 (unsigned char *)header,
6198 (unsigned char *)newheader,
6199 (unsigned int)(sizeof(*newheader)),
6201 rbtdb->common.rdclass,
6202 (dns_rdatatype_t)header->type,
6204 if (result == ISC_R_SUCCESS) {
6205 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6206 newheader = (rdatasetheader_t *)subresult;
6207 init_rdataset(rbtdb, newheader);
6209 * We have to set the serial since the rdataslab
6210 * subtraction routine copies the reserved portion of
6211 * header, not newheader.
6213 newheader->serial = rbtversion->serial;
6215 * XXXJT: dns_rdataslab_subtract() copied the pointers
6216 * to additional info. We need to clear these fields
6217 * to avoid having duplicated references.
6219 newheader->additional_auth = NULL;
6220 newheader->additional_glue = NULL;
6221 } else if (result == DNS_R_NXRRSET) {
6223 * This subtraction would remove all of the rdata;
6224 * add a nonexistent header instead.
6226 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6227 newheader = new_rdataset(rbtdb, rbtdb->common.mctx);
6228 if (newheader == NULL) {
6229 result = ISC_R_NOMEMORY;
6232 set_ttl(rbtdb, newheader, 0);
6233 newheader->type = topheader->type;
6234 newheader->attributes = RDATASET_ATTR_NONEXISTENT;
6235 newheader->trust = 0;
6236 newheader->serial = rbtversion->serial;
6237 newheader->noqname = NULL;
6238 newheader->closest = NULL;
6239 newheader->count = 0;
6240 newheader->additional_auth = NULL;
6241 newheader->additional_glue = NULL;
6242 newheader->node = rbtnode;
6243 newheader->resign = 0;
6244 newheader->last_used = 0;
6246 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6251 * If we're here, we want to link newheader in front of
6254 INSIST(rbtversion->serial >= topheader->serial);
6255 if (topheader_prev != NULL)
6256 topheader_prev->next = newheader;
6258 rbtnode->data = newheader;
6259 newheader->next = topheader->next;
6260 newheader->down = topheader;
6261 topheader->next = newheader;
6263 changed->dirty = ISC_TRUE;
6266 * The rdataset doesn't exist, so we don't need to do anything
6267 * to satisfy the deletion request.
6269 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6270 if ((options & DNS_DBSUB_EXACT) != 0)
6271 result = DNS_R_NOTEXACT;
6273 result = DNS_R_UNCHANGED;
6276 if (result == ISC_R_SUCCESS && newrdataset != NULL)
6277 bind_rdataset(rbtdb, rbtnode, newheader, 0, newrdataset);
6280 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6281 isc_rwlocktype_write);
6284 * Update the zone's secure status. If version is non-NULL
6285 * this is deferred until closeversion() is called.
6287 if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
6288 iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
6294 deleterdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
6295 dns_rdatatype_t type, dns_rdatatype_t covers)
6297 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6298 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
6299 rbtdb_version_t *rbtversion = version;
6300 isc_result_t result;
6301 rdatasetheader_t *newheader;
6303 REQUIRE(VALID_RBTDB(rbtdb));
6305 if (type == dns_rdatatype_any)
6306 return (ISC_R_NOTIMPLEMENTED);
6307 if (type == dns_rdatatype_rrsig && covers == 0)
6308 return (ISC_R_NOTIMPLEMENTED);
6310 newheader = new_rdataset(rbtdb, rbtdb->common.mctx);
6311 if (newheader == NULL)
6312 return (ISC_R_NOMEMORY);
6313 set_ttl(rbtdb, newheader, 0);
6314 newheader->type = RBTDB_RDATATYPE_VALUE(type, covers);
6315 newheader->attributes = RDATASET_ATTR_NONEXISTENT;
6316 newheader->trust = 0;
6317 newheader->noqname = NULL;
6318 newheader->closest = NULL;
6319 newheader->additional_auth = NULL;
6320 newheader->additional_glue = NULL;
6321 if (rbtversion != NULL)
6322 newheader->serial = rbtversion->serial;
6324 newheader->serial = 0;
6325 newheader->count = 0;
6326 newheader->last_used = 0;
6327 newheader->node = rbtnode;
6329 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6330 isc_rwlocktype_write);
6332 result = add(rbtdb, rbtnode, rbtversion, newheader, DNS_DBADD_FORCE,
6333 ISC_FALSE, NULL, 0);
6335 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6336 isc_rwlocktype_write);
6339 * Update the zone's secure status. If version is non-NULL
6340 * this is deferred until closeversion() is called.
6342 if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
6343 iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
6349 loading_addrdataset(void *arg, dns_name_t *name, dns_rdataset_t *rdataset) {
6350 rbtdb_load_t *loadctx = arg;
6351 dns_rbtdb_t *rbtdb = loadctx->rbtdb;
6352 dns_rbtnode_t *node;
6353 isc_result_t result;
6354 isc_region_t region;
6355 rdatasetheader_t *newheader;
6358 * This routine does no node locking. See comments in
6359 * 'load' below for more information on loading and
6365 * SOA records are only allowed at top of zone.
6367 if (rdataset->type == dns_rdatatype_soa &&
6368 !IS_CACHE(rbtdb) && !dns_name_equal(name, &rbtdb->common.origin))
6369 return (DNS_R_NOTZONETOP);
6371 if (rdataset->type != dns_rdatatype_nsec3 &&
6372 rdataset->covers != dns_rdatatype_nsec3)
6373 add_empty_wildcards(rbtdb, name);
6375 if (dns_name_iswildcard(name)) {
6377 * NS record owners cannot legally be wild cards.
6379 if (rdataset->type == dns_rdatatype_ns)
6380 return (DNS_R_INVALIDNS);
6382 * NSEC3 record owners cannot legally be wild cards.
6384 if (rdataset->type == dns_rdatatype_nsec3)
6385 return (DNS_R_INVALIDNSEC3);
6386 result = add_wildcard_magic(rbtdb, name);
6387 if (result != ISC_R_SUCCESS)
6392 if (rdataset->type == dns_rdatatype_nsec3 ||
6393 rdataset->covers == dns_rdatatype_nsec3) {
6394 result = dns_rbt_addnode(rbtdb->nsec3, name, &node);
6395 if (result == ISC_R_SUCCESS)
6398 result = dns_rbt_addnode(rbtdb->tree, name, &node);
6399 if (result == ISC_R_SUCCESS)
6402 if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS)
6404 if (result != ISC_R_EXISTS) {
6405 dns_name_t foundname;
6406 dns_name_init(&foundname, NULL);
6407 dns_rbt_namefromnode(node, &foundname);
6408 #ifdef DNS_RBT_USEHASH
6409 node->locknum = node->hashval % rbtdb->node_lock_count;
6411 node->locknum = dns_name_hash(&foundname, ISC_TRUE) %
6412 rbtdb->node_lock_count;
6416 result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
6418 sizeof(rdatasetheader_t));
6419 if (result != ISC_R_SUCCESS)
6421 newheader = (rdatasetheader_t *)region.base;
6422 init_rdataset(rbtdb, newheader);
6423 set_ttl(rbtdb, newheader,
6424 rdataset->ttl + loadctx->now); /* XXX overflow check */
6425 newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
6427 newheader->attributes = 0;
6428 newheader->trust = rdataset->trust;
6429 newheader->serial = 1;
6430 newheader->noqname = NULL;
6431 newheader->closest = NULL;
6432 newheader->count = init_count++;
6433 newheader->additional_auth = NULL;
6434 newheader->additional_glue = NULL;
6435 newheader->last_used = 0;
6436 newheader->node = node;
6437 if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
6438 newheader->attributes |= RDATASET_ATTR_RESIGN;
6439 newheader->resign = rdataset->resign;
6441 newheader->resign = 0;
6443 result = add(rbtdb, node, rbtdb->current_version, newheader,
6444 DNS_DBADD_MERGE, ISC_TRUE, NULL, 0);
6445 if (result == ISC_R_SUCCESS &&
6446 delegating_type(rbtdb, node, rdataset->type))
6447 node->find_callback = 1;
6448 else if (result == DNS_R_UNCHANGED)
6449 result = ISC_R_SUCCESS;
6455 beginload(dns_db_t *db, dns_addrdatasetfunc_t *addp, dns_dbload_t **dbloadp) {
6456 rbtdb_load_t *loadctx;
6459 rbtdb = (dns_rbtdb_t *)db;
6461 REQUIRE(VALID_RBTDB(rbtdb));
6463 loadctx = isc_mem_get(rbtdb->common.mctx, sizeof(*loadctx));
6464 if (loadctx == NULL)
6465 return (ISC_R_NOMEMORY);
6467 loadctx->rbtdb = rbtdb;
6468 if (IS_CACHE(rbtdb))
6469 isc_stdtime_get(&loadctx->now);
6473 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
6475 REQUIRE((rbtdb->attributes & (RBTDB_ATTR_LOADED|RBTDB_ATTR_LOADING))
6477 rbtdb->attributes |= RBTDB_ATTR_LOADING;
6479 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
6481 *addp = loading_addrdataset;
6484 return (ISC_R_SUCCESS);
6488 endload(dns_db_t *db, dns_dbload_t **dbloadp) {
6489 rbtdb_load_t *loadctx;
6490 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6492 REQUIRE(VALID_RBTDB(rbtdb));
6493 REQUIRE(dbloadp != NULL);
6495 REQUIRE(loadctx->rbtdb == rbtdb);
6497 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
6499 REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADING) != 0);
6500 REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADED) == 0);
6502 rbtdb->attributes &= ~RBTDB_ATTR_LOADING;
6503 rbtdb->attributes |= RBTDB_ATTR_LOADED;
6505 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
6508 * If there's a KEY rdataset at the zone origin containing a
6509 * zone key, we consider the zone secure.
6511 if (! IS_CACHE(rbtdb))
6512 iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
6516 isc_mem_put(rbtdb->common.mctx, loadctx, sizeof(*loadctx));
6518 return (ISC_R_SUCCESS);
6522 dump(dns_db_t *db, dns_dbversion_t *version, const char *filename,
6523 dns_masterformat_t masterformat) {
6526 rbtdb = (dns_rbtdb_t *)db;
6528 REQUIRE(VALID_RBTDB(rbtdb));
6530 return (dns_master_dump2(rbtdb->common.mctx, db, version,
6531 &dns_master_style_default,
6532 filename, masterformat));
6536 delete_callback(void *data, void *arg) {
6537 dns_rbtdb_t *rbtdb = arg;
6538 rdatasetheader_t *current, *next;
6539 unsigned int locknum;
6542 locknum = current->node->locknum;
6543 NODE_LOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
6544 while (current != NULL) {
6545 next = current->next;
6546 free_rdataset(rbtdb, rbtdb->common.mctx, current);
6549 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
6552 static isc_boolean_t
6553 issecure(dns_db_t *db) {
6555 isc_boolean_t secure;
6557 rbtdb = (dns_rbtdb_t *)db;
6559 REQUIRE(VALID_RBTDB(rbtdb));
6561 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6562 secure = ISC_TF(rbtdb->current_version->secure == dns_db_secure);
6563 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6568 static isc_boolean_t
6569 isdnssec(dns_db_t *db) {
6571 isc_boolean_t dnssec;
6573 rbtdb = (dns_rbtdb_t *)db;
6575 REQUIRE(VALID_RBTDB(rbtdb));
6577 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6578 dnssec = ISC_TF(rbtdb->current_version->secure != dns_db_insecure);
6579 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6585 nodecount(dns_db_t *db) {
6589 rbtdb = (dns_rbtdb_t *)db;
6591 REQUIRE(VALID_RBTDB(rbtdb));
6593 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6594 count = dns_rbt_nodecount(rbtdb->tree);
6595 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6601 settask(dns_db_t *db, isc_task_t *task) {
6604 rbtdb = (dns_rbtdb_t *)db;
6606 REQUIRE(VALID_RBTDB(rbtdb));
6608 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
6609 if (rbtdb->task != NULL)
6610 isc_task_detach(&rbtdb->task);
6612 isc_task_attach(task, &rbtdb->task);
6613 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
6616 static isc_boolean_t
6617 ispersistent(dns_db_t *db) {
6623 getoriginnode(dns_db_t *db, dns_dbnode_t **nodep) {
6624 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6625 dns_rbtnode_t *onode;
6626 isc_result_t result = ISC_R_SUCCESS;
6628 REQUIRE(VALID_RBTDB(rbtdb));
6629 REQUIRE(nodep != NULL && *nodep == NULL);
6631 /* Note that the access to origin_node doesn't require a DB lock */
6632 onode = (dns_rbtnode_t *)rbtdb->origin_node;
6633 if (onode != NULL) {
6634 NODE_STRONGLOCK(&rbtdb->node_locks[onode->locknum].lock);
6635 new_reference(rbtdb, onode);
6636 NODE_STRONGUNLOCK(&rbtdb->node_locks[onode->locknum].lock);
6638 *nodep = rbtdb->origin_node;
6640 INSIST(IS_CACHE(rbtdb));
6641 result = ISC_R_NOTFOUND;
6648 getnsec3parameters(dns_db_t *db, dns_dbversion_t *version, dns_hash_t *hash,
6649 isc_uint8_t *flags, isc_uint16_t *iterations,
6650 unsigned char *salt, size_t *salt_length)
6653 isc_result_t result = ISC_R_NOTFOUND;
6654 rbtdb_version_t *rbtversion = version;
6656 rbtdb = (dns_rbtdb_t *)db;
6658 REQUIRE(VALID_RBTDB(rbtdb));
6660 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6662 if (rbtversion == NULL)
6663 rbtversion = rbtdb->current_version;
6665 if (rbtversion->havensec3) {
6667 *hash = rbtversion->hash;
6668 if (salt != NULL && salt_length != NULL) {
6669 REQUIRE(*salt_length >= rbtversion->salt_length);
6670 memcpy(salt, rbtversion->salt, rbtversion->salt_length);
6672 if (salt_length != NULL)
6673 *salt_length = rbtversion->salt_length;
6674 if (iterations != NULL)
6675 *iterations = rbtversion->iterations;
6677 *flags = rbtversion->flags;
6678 result = ISC_R_SUCCESS;
6680 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6686 setsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, isc_stdtime_t resign) {
6687 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6688 isc_stdtime_t oldresign;
6689 isc_result_t result = ISC_R_SUCCESS;
6690 rdatasetheader_t *header;
6692 REQUIRE(VALID_RBTDB(rbtdb));
6693 REQUIRE(!IS_CACHE(rbtdb));
6694 REQUIRE(rdataset != NULL);
6696 header = rdataset->private3;
6699 NODE_LOCK(&rbtdb->node_locks[header->node->locknum].lock,
6700 isc_rwlocktype_write);
6702 oldresign = header->resign;
6703 header->resign = resign;
6704 if (header->heap_index != 0) {
6705 INSIST(RESIGN(header));
6707 isc_heap_delete(rbtdb->heaps[header->node->locknum],
6708 header->heap_index);
6709 header->heap_index = 0;
6710 } else if (resign < oldresign)
6711 isc_heap_increased(rbtdb->heaps[header->node->locknum],
6712 header->heap_index);
6714 isc_heap_decreased(rbtdb->heaps[header->node->locknum],
6715 header->heap_index);
6716 } else if (resign && header->heap_index == 0) {
6717 header->attributes |= RDATASET_ATTR_RESIGN;
6718 result = resign_insert(rbtdb, header->node->locknum, header);
6720 NODE_UNLOCK(&rbtdb->node_locks[header->node->locknum].lock,
6721 isc_rwlocktype_write);
6726 getsigningtime(dns_db_t *db, dns_rdataset_t *rdataset,
6727 dns_name_t *foundname)
6729 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6730 rdatasetheader_t *header = NULL, *this;
6732 isc_result_t result = ISC_R_NOTFOUND;
6733 unsigned int locknum;
6735 REQUIRE(VALID_RBTDB(rbtdb));
6737 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
6739 for (i = 0; i < rbtdb->node_lock_count; i++) {
6740 NODE_LOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_read);
6741 this = isc_heap_element(rbtdb->heaps[i], 1);
6743 NODE_UNLOCK(&rbtdb->node_locks[i].lock,
6744 isc_rwlocktype_read);
6749 else if (isc_serial_lt(this->resign, header->resign)) {
6750 locknum = header->node->locknum;
6751 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
6752 isc_rwlocktype_read);
6755 NODE_UNLOCK(&rbtdb->node_locks[i].lock,
6756 isc_rwlocktype_read);
6762 bind_rdataset(rbtdb, header->node, header, 0, rdataset);
6764 if (foundname != NULL)
6765 dns_rbt_fullnamefromnode(header->node, foundname);
6767 NODE_UNLOCK(&rbtdb->node_locks[header->node->locknum].lock,
6768 isc_rwlocktype_read);
6770 result = ISC_R_SUCCESS;
6773 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
6779 resigned(dns_db_t *db, dns_rdataset_t *rdataset, dns_dbversion_t *version)
6781 rbtdb_version_t *rbtversion = (rbtdb_version_t *)version;
6782 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6783 dns_rbtnode_t *node;
6784 rdatasetheader_t *header;
6786 REQUIRE(VALID_RBTDB(rbtdb));
6787 REQUIRE(rdataset != NULL);
6788 REQUIRE(rbtdb->future_version == rbtversion);
6789 REQUIRE(rbtversion->writer);
6791 node = rdataset->private2;
6792 header = rdataset->private3;
6795 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
6796 NODE_LOCK(&rbtdb->node_locks[node->locknum].lock,
6797 isc_rwlocktype_write);
6799 * Delete from heap and save to re-signed list so that it can
6800 * be restored if we backout of this change.
6802 new_reference(rbtdb, node);
6803 isc_heap_delete(rbtdb->heaps[node->locknum], header->heap_index);
6804 header->heap_index = 0;
6805 ISC_LIST_APPEND(rbtversion->resigned_list, header, link);
6807 NODE_UNLOCK(&rbtdb->node_locks[node->locknum].lock,
6808 isc_rwlocktype_write);
6809 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
6812 static dns_stats_t *
6813 getrrsetstats(dns_db_t *db) {
6814 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6816 REQUIRE(VALID_RBTDB(rbtdb));
6817 REQUIRE(IS_CACHE(rbtdb)); /* current restriction */
6819 return (rbtdb->rrsetstats);
6822 static dns_dbmethods_t zone_methods = {
6861 static dns_dbmethods_t cache_methods = {
6901 #ifdef DNS_RBTDB_VERSION64
6906 (isc_mem_t *mctx, dns_name_t *origin, dns_dbtype_t type,
6907 dns_rdataclass_t rdclass, unsigned int argc, char *argv[],
6908 void *driverarg, dns_db_t **dbp)
6911 isc_result_t result;
6914 isc_boolean_t (*sooner)(void *, void *);
6916 /* Keep the compiler happy. */
6921 rbtdb = isc_mem_get(mctx, sizeof(*rbtdb));
6923 return (ISC_R_NOMEMORY);
6925 memset(rbtdb, '\0', sizeof(*rbtdb));
6926 dns_name_init(&rbtdb->common.origin, NULL);
6927 rbtdb->common.attributes = 0;
6928 if (type == dns_dbtype_cache) {
6929 rbtdb->common.methods = &cache_methods;
6930 rbtdb->common.attributes |= DNS_DBATTR_CACHE;
6931 } else if (type == dns_dbtype_stub) {
6932 rbtdb->common.methods = &zone_methods;
6933 rbtdb->common.attributes |= DNS_DBATTR_STUB;
6935 rbtdb->common.methods = &zone_methods;
6936 rbtdb->common.rdclass = rdclass;
6937 rbtdb->common.mctx = NULL;
6939 result = RBTDB_INITLOCK(&rbtdb->lock);
6940 if (result != ISC_R_SUCCESS)
6943 result = isc_rwlock_init(&rbtdb->tree_lock, 0, 0);
6944 if (result != ISC_R_SUCCESS)
6948 * Initialize node_lock_count in a generic way to support future
6949 * extension which allows the user to specify this value on creation.
6950 * Note that when specified for a cache DB it must be larger than 1
6951 * as commented with the definition of DEFAULT_CACHE_NODE_LOCK_COUNT.
6953 if (rbtdb->node_lock_count == 0) {
6954 if (IS_CACHE(rbtdb))
6955 rbtdb->node_lock_count = DEFAULT_CACHE_NODE_LOCK_COUNT;
6957 rbtdb->node_lock_count = DEFAULT_NODE_LOCK_COUNT;
6958 } else if (rbtdb->node_lock_count < 2 && IS_CACHE(rbtdb)) {
6959 result = ISC_R_RANGE;
6960 goto cleanup_tree_lock;
6962 INSIST(rbtdb->node_lock_count < (1 << DNS_RBT_LOCKLENGTH));
6963 rbtdb->node_locks = isc_mem_get(mctx, rbtdb->node_lock_count *
6964 sizeof(rbtdb_nodelock_t));
6965 if (rbtdb->node_locks == NULL) {
6966 result = ISC_R_NOMEMORY;
6967 goto cleanup_tree_lock;
6970 rbtdb->rrsetstats = NULL;
6971 if (IS_CACHE(rbtdb)) {
6972 result = dns_rdatasetstats_create(mctx, &rbtdb->rrsetstats);
6973 if (result != ISC_R_SUCCESS)
6974 goto cleanup_node_locks;
6975 rbtdb->rdatasets = isc_mem_get(mctx, rbtdb->node_lock_count *
6976 sizeof(rdatasetheaderlist_t));
6977 if (rbtdb->rdatasets == NULL) {
6978 result = ISC_R_NOMEMORY;
6979 goto cleanup_rrsetstats;
6981 for (i = 0; i < (int)rbtdb->node_lock_count; i++)
6982 ISC_LIST_INIT(rbtdb->rdatasets[i]);
6984 rbtdb->rdatasets = NULL;
6989 rbtdb->heaps = isc_mem_get(mctx, rbtdb->node_lock_count *
6990 sizeof(isc_heap_t *));
6991 if (rbtdb->heaps == NULL) {
6992 result = ISC_R_NOMEMORY;
6993 goto cleanup_rdatasets;
6995 for (i = 0; i < (int)rbtdb->node_lock_count; i++)
6996 rbtdb->heaps[i] = NULL;
6997 sooner = IS_CACHE(rbtdb) ? ttl_sooner : resign_sooner;
6998 for (i = 0; i < (int)rbtdb->node_lock_count; i++) {
6999 result = isc_heap_create(mctx, sooner, set_index, 0,
7001 if (result != ISC_R_SUCCESS)
7006 * Create deadnode lists.
7008 rbtdb->deadnodes = isc_mem_get(mctx, rbtdb->node_lock_count *
7009 sizeof(rbtnodelist_t));
7010 if (rbtdb->deadnodes == NULL) {
7011 result = ISC_R_NOMEMORY;
7014 for (i = 0; i < (int)rbtdb->node_lock_count; i++)
7015 ISC_LIST_INIT(rbtdb->deadnodes[i]);
7017 rbtdb->active = rbtdb->node_lock_count;
7019 for (i = 0; i < (int)(rbtdb->node_lock_count); i++) {
7020 result = NODE_INITLOCK(&rbtdb->node_locks[i].lock);
7021 if (result == ISC_R_SUCCESS) {
7022 result = isc_refcount_init(&rbtdb->node_locks[i].references, 0);
7023 if (result != ISC_R_SUCCESS)
7024 NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
7026 if (result != ISC_R_SUCCESS) {
7028 NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
7029 isc_refcount_decrement(&rbtdb->node_locks[i].references, NULL);
7030 isc_refcount_destroy(&rbtdb->node_locks[i].references);
7032 goto cleanup_deadnodes;
7034 rbtdb->node_locks[i].exiting = ISC_FALSE;
7038 * Attach to the mctx. The database will persist so long as there
7039 * are references to it, and attaching to the mctx ensures that our
7040 * mctx won't disappear out from under us.
7042 isc_mem_attach(mctx, &rbtdb->common.mctx);
7045 * Must be initialized before free_rbtdb() is called.
7047 isc_ondestroy_init(&rbtdb->common.ondest);
7050 * Make a copy of the origin name.
7052 result = dns_name_dupwithoffsets(origin, mctx, &rbtdb->common.origin);
7053 if (result != ISC_R_SUCCESS) {
7054 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7059 * Make the Red-Black Trees.
7061 result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->tree);
7062 if (result != ISC_R_SUCCESS) {
7063 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7067 result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->nsec3);
7068 if (result != ISC_R_SUCCESS) {
7069 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7074 * In order to set the node callback bit correctly in zone databases,
7075 * we need to know if the node has the origin name of the zone.
7076 * In loading_addrdataset() we could simply compare the new name
7077 * to the origin name, but this is expensive. Also, we don't know the
7078 * node name in addrdataset(), so we need another way of knowing the
7081 * We now explicitly create a node for the zone's origin, and then
7082 * we simply remember the node's address. This is safe, because
7083 * the top-of-zone node can never be deleted, nor can its address
7086 if (!IS_CACHE(rbtdb)) {
7087 dns_rbtnode_t *nsec3node;
7089 rbtdb->origin_node = NULL;
7090 result = dns_rbt_addnode(rbtdb->tree, &rbtdb->common.origin,
7091 &rbtdb->origin_node);
7092 if (result != ISC_R_SUCCESS) {
7093 INSIST(result != ISC_R_EXISTS);
7094 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7097 rbtdb->origin_node->nsec3 = 0;
7099 * We need to give the origin node the right locknum.
7101 dns_name_init(&name, NULL);
7102 dns_rbt_namefromnode(rbtdb->origin_node, &name);
7103 #ifdef DNS_RBT_USEHASH
7104 rbtdb->origin_node->locknum =
7105 rbtdb->origin_node->hashval %
7106 rbtdb->node_lock_count;
7108 rbtdb->origin_node->locknum =
7109 dns_name_hash(&name, ISC_TRUE) %
7110 rbtdb->node_lock_count;
7113 * Add an apex node to the NSEC3 tree so that NSEC3 searches
7114 * return partial matches when there is only a single NSEC3
7115 * record in the tree.
7118 result = dns_rbt_addnode(rbtdb->nsec3, &rbtdb->common.origin,
7120 if (result != ISC_R_SUCCESS) {
7121 INSIST(result != ISC_R_EXISTS);
7122 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7125 nsec3node->nsec3 = 1;
7127 * We need to give the nsec3 origin node the right locknum.
7129 dns_name_init(&name, NULL);
7130 dns_rbt_namefromnode(nsec3node, &name);
7131 #ifdef DNS_RBT_USEHASH
7132 nsec3node->locknum = nsec3node->hashval %
7133 rbtdb->node_lock_count;
7135 nsec3node->locknum = dns_name_hash(&name, ISC_TRUE) %
7136 rbtdb->node_lock_count;
7141 * Misc. Initialization.
7143 result = isc_refcount_init(&rbtdb->references, 1);
7144 if (result != ISC_R_SUCCESS) {
7145 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7148 rbtdb->attributes = 0;
7152 * Version Initialization.
7154 rbtdb->current_serial = 1;
7155 rbtdb->least_serial = 1;
7156 rbtdb->next_serial = 2;
7157 rbtdb->current_version = allocate_version(mctx, 1, 1, ISC_FALSE);
7158 if (rbtdb->current_version == NULL) {
7159 isc_refcount_decrement(&rbtdb->references, NULL);
7160 isc_refcount_destroy(&rbtdb->references);
7161 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7162 return (ISC_R_NOMEMORY);
7164 rbtdb->current_version->secure = dns_db_insecure;
7165 rbtdb->current_version->havensec3 = ISC_FALSE;
7166 rbtdb->current_version->flags = 0;
7167 rbtdb->current_version->iterations = 0;
7168 rbtdb->current_version->hash = 0;
7169 rbtdb->current_version->salt_length = 0;
7170 memset(rbtdb->current_version->salt, 0,
7171 sizeof(rbtdb->current_version->salt));
7172 rbtdb->future_version = NULL;
7173 ISC_LIST_INIT(rbtdb->open_versions);
7175 * Keep the current version in the open list so that list operation
7176 * won't happen in normal lookup operations.
7178 PREPEND(rbtdb->open_versions, rbtdb->current_version, link);
7180 rbtdb->common.magic = DNS_DB_MAGIC;
7181 rbtdb->common.impmagic = RBTDB_MAGIC;
7183 *dbp = (dns_db_t *)rbtdb;
7185 return (ISC_R_SUCCESS);
7188 isc_mem_put(mctx, rbtdb->deadnodes,
7189 rbtdb->node_lock_count * sizeof(rbtnodelist_t));
7192 if (rbtdb->heaps != NULL) {
7193 for (i = 0 ; i < (int)rbtdb->node_lock_count ; i++)
7194 if (rbtdb->heaps[i] != NULL)
7195 isc_heap_destroy(&rbtdb->heaps[i]);
7196 isc_mem_put(mctx, rbtdb->heaps,
7197 rbtdb->node_lock_count * sizeof(isc_heap_t *));
7201 if (rbtdb->rdatasets != NULL)
7202 isc_mem_put(mctx, rbtdb->rdatasets, rbtdb->node_lock_count *
7203 sizeof(rdatasetheaderlist_t));
7205 if (rbtdb->rrsetstats != NULL)
7206 dns_stats_detach(&rbtdb->rrsetstats);
7209 isc_mem_put(mctx, rbtdb->node_locks,
7210 rbtdb->node_lock_count * sizeof(rbtdb_nodelock_t));
7213 isc_rwlock_destroy(&rbtdb->tree_lock);
7216 RBTDB_DESTROYLOCK(&rbtdb->lock);
7219 isc_mem_put(mctx, rbtdb, sizeof(*rbtdb));
7225 * Slabbed Rdataset Methods
7229 rdataset_disassociate(dns_rdataset_t *rdataset) {
7230 dns_db_t *db = rdataset->private1;
7231 dns_dbnode_t *node = rdataset->private2;
7233 detachnode(db, &node);
7237 rdataset_first(dns_rdataset_t *rdataset) {
7238 unsigned char *raw = rdataset->private3; /* RDATASLAB */
7241 count = raw[0] * 256 + raw[1];
7243 rdataset->private5 = NULL;
7244 return (ISC_R_NOMORE);
7247 #if DNS_RDATASET_FIXED
7248 if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) == 0)
7249 raw += 2 + (4 * count);
7255 * The privateuint4 field is the number of rdata beyond the
7256 * cursor position, so we decrement the total count by one
7257 * before storing it.
7259 * If DNS_RDATASETATTR_LOADORDER is not set 'raw' points to the
7260 * first record. If DNS_RDATASETATTR_LOADORDER is set 'raw' points
7261 * to the first entry in the offset table.
7264 rdataset->privateuint4 = count;
7265 rdataset->private5 = raw;
7267 return (ISC_R_SUCCESS);
7271 rdataset_next(dns_rdataset_t *rdataset) {
7273 unsigned int length;
7274 unsigned char *raw; /* RDATASLAB */
7276 count = rdataset->privateuint4;
7278 return (ISC_R_NOMORE);
7280 rdataset->privateuint4 = count;
7283 * Skip forward one record (length + 4) or one offset (4).
7285 raw = rdataset->private5;
7286 #if DNS_RDATASET_FIXED
7287 if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) == 0) {
7289 length = raw[0] * 256 + raw[1];
7291 #if DNS_RDATASET_FIXED
7293 rdataset->private5 = raw + 4; /* length(2) + order(2) */
7295 rdataset->private5 = raw + 2; /* length(2) */
7298 return (ISC_R_SUCCESS);
7302 rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
7303 unsigned char *raw = rdataset->private5; /* RDATASLAB */
7304 #if DNS_RDATASET_FIXED
7305 unsigned int offset;
7307 unsigned int length;
7309 unsigned int flags = 0;
7311 REQUIRE(raw != NULL);
7314 * Find the start of the record if not already in private5
7315 * then skip the length and order fields.
7317 #if DNS_RDATASET_FIXED
7318 if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) != 0) {
7319 offset = (raw[0] << 24) + (raw[1] << 16) +
7320 (raw[2] << 8) + raw[3];
7321 raw = rdataset->private3;
7325 length = raw[0] * 256 + raw[1];
7326 #if DNS_RDATASET_FIXED
7331 if (rdataset->type == dns_rdatatype_rrsig) {
7332 if (*raw & DNS_RDATASLAB_OFFLINE)
7333 flags |= DNS_RDATA_OFFLINE;
7339 dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
7340 rdata->flags |= flags;
7344 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
7345 dns_db_t *db = source->private1;
7346 dns_dbnode_t *node = source->private2;
7347 dns_dbnode_t *cloned_node = NULL;
7349 attachnode(db, node, &cloned_node);
7353 * Reset iterator state.
7355 target->privateuint4 = 0;
7356 target->private5 = NULL;
7360 rdataset_count(dns_rdataset_t *rdataset) {
7361 unsigned char *raw = rdataset->private3; /* RDATASLAB */
7364 count = raw[0] * 256 + raw[1];
7370 rdataset_getnoqname(dns_rdataset_t *rdataset, dns_name_t *name,
7371 dns_rdataset_t *nsec, dns_rdataset_t *nsecsig)
7373 dns_db_t *db = rdataset->private1;
7374 dns_dbnode_t *node = rdataset->private2;
7375 dns_dbnode_t *cloned_node;
7376 struct noqname *noqname = rdataset->private6;
7379 attachnode(db, node, &cloned_node);
7380 nsec->methods = &rdataset_methods;
7381 nsec->rdclass = db->rdclass;
7382 nsec->type = noqname->type;
7384 nsec->ttl = rdataset->ttl;
7385 nsec->trust = rdataset->trust;
7386 nsec->private1 = rdataset->private1;
7387 nsec->private2 = rdataset->private2;
7388 nsec->private3 = noqname->neg;
7389 nsec->privateuint4 = 0;
7390 nsec->private5 = NULL;
7391 nsec->private6 = NULL;
7392 nsec->private7 = NULL;
7395 attachnode(db, node, &cloned_node);
7396 nsecsig->methods = &rdataset_methods;
7397 nsecsig->rdclass = db->rdclass;
7398 nsecsig->type = dns_rdatatype_rrsig;
7399 nsecsig->covers = noqname->type;
7400 nsecsig->ttl = rdataset->ttl;
7401 nsecsig->trust = rdataset->trust;
7402 nsecsig->private1 = rdataset->private1;
7403 nsecsig->private2 = rdataset->private2;
7404 nsecsig->private3 = noqname->negsig;
7405 nsecsig->privateuint4 = 0;
7406 nsecsig->private5 = NULL;
7407 nsec->private6 = NULL;
7408 nsec->private7 = NULL;
7410 dns_name_clone(&noqname->name, name);
7412 return (ISC_R_SUCCESS);
7416 rdataset_getclosest(dns_rdataset_t *rdataset, dns_name_t *name,
7417 dns_rdataset_t *nsec, dns_rdataset_t *nsecsig)
7419 dns_db_t *db = rdataset->private1;
7420 dns_dbnode_t *node = rdataset->private2;
7421 dns_dbnode_t *cloned_node;
7422 struct noqname *closest = rdataset->private7;
7425 attachnode(db, node, &cloned_node);
7426 nsec->methods = &rdataset_methods;
7427 nsec->rdclass = db->rdclass;
7428 nsec->type = closest->type;
7430 nsec->ttl = rdataset->ttl;
7431 nsec->trust = rdataset->trust;
7432 nsec->private1 = rdataset->private1;
7433 nsec->private2 = rdataset->private2;
7434 nsec->private3 = closest->neg;
7435 nsec->privateuint4 = 0;
7436 nsec->private5 = NULL;
7437 nsec->private6 = NULL;
7438 nsec->private7 = NULL;
7441 attachnode(db, node, &cloned_node);
7442 nsecsig->methods = &rdataset_methods;
7443 nsecsig->rdclass = db->rdclass;
7444 nsecsig->type = dns_rdatatype_rrsig;
7445 nsecsig->covers = closest->type;
7446 nsecsig->ttl = rdataset->ttl;
7447 nsecsig->trust = rdataset->trust;
7448 nsecsig->private1 = rdataset->private1;
7449 nsecsig->private2 = rdataset->private2;
7450 nsecsig->private3 = closest->negsig;
7451 nsecsig->privateuint4 = 0;
7452 nsecsig->private5 = NULL;
7453 nsec->private6 = NULL;
7454 nsec->private7 = NULL;
7456 dns_name_clone(&closest->name, name);
7458 return (ISC_R_SUCCESS);
7462 rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust) {
7463 dns_rbtdb_t *rbtdb = rdataset->private1;
7464 dns_rbtnode_t *rbtnode = rdataset->private2;
7465 rdatasetheader_t *header = rdataset->private3;
7468 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7469 isc_rwlocktype_write);
7470 header->trust = rdataset->trust = trust;
7471 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7472 isc_rwlocktype_write);
7476 rdataset_expire(dns_rdataset_t *rdataset) {
7477 dns_rbtdb_t *rbtdb = rdataset->private1;
7478 dns_rbtnode_t *rbtnode = rdataset->private2;
7479 rdatasetheader_t *header = rdataset->private3;
7482 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7483 isc_rwlocktype_write);
7484 expire_header(rbtdb, header, ISC_FALSE);
7485 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7486 isc_rwlocktype_write);
7490 * Rdataset Iterator Methods
7494 rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp) {
7495 rbtdb_rdatasetiter_t *rbtiterator;
7497 rbtiterator = (rbtdb_rdatasetiter_t *)(*iteratorp);
7499 if (rbtiterator->common.version != NULL)
7500 closeversion(rbtiterator->common.db,
7501 &rbtiterator->common.version, ISC_FALSE);
7502 detachnode(rbtiterator->common.db, &rbtiterator->common.node);
7503 isc_mem_put(rbtiterator->common.db->mctx, rbtiterator,
7504 sizeof(*rbtiterator));
7510 rdatasetiter_first(dns_rdatasetiter_t *iterator) {
7511 rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
7512 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
7513 dns_rbtnode_t *rbtnode = rbtiterator->common.node;
7514 rbtdb_version_t *rbtversion = rbtiterator->common.version;
7515 rdatasetheader_t *header, *top_next;
7516 rbtdb_serial_t serial;
7519 if (IS_CACHE(rbtdb)) {
7521 now = rbtiterator->common.now;
7523 serial = rbtversion->serial;
7527 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7528 isc_rwlocktype_read);
7530 for (header = rbtnode->data; header != NULL; header = top_next) {
7531 top_next = header->next;
7533 if (header->serial <= serial && !IGNORE(header)) {
7535 * Is this a "this rdataset doesn't exist"
7536 * record? Or is it too old in the cache?
7538 * Note: unlike everywhere else, we
7539 * check for now > header->rdh_ttl instead
7540 * of now >= header->rdh_ttl. This allows
7541 * ANY and RRSIG queries for 0 TTL
7542 * rdatasets to work.
7544 if (NONEXISTENT(header) ||
7545 (now != 0 && now > header->rdh_ttl))
7549 header = header->down;
7550 } while (header != NULL);
7555 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7556 isc_rwlocktype_read);
7558 rbtiterator->current = header;
7561 return (ISC_R_NOMORE);
7563 return (ISC_R_SUCCESS);
7567 rdatasetiter_next(dns_rdatasetiter_t *iterator) {
7568 rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
7569 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
7570 dns_rbtnode_t *rbtnode = rbtiterator->common.node;
7571 rbtdb_version_t *rbtversion = rbtiterator->common.version;
7572 rdatasetheader_t *header, *top_next;
7573 rbtdb_serial_t serial;
7575 rbtdb_rdatatype_t type, negtype;
7576 dns_rdatatype_t rdtype, covers;
7578 header = rbtiterator->current;
7580 return (ISC_R_NOMORE);
7582 if (IS_CACHE(rbtdb)) {
7584 now = rbtiterator->common.now;
7586 serial = rbtversion->serial;
7590 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7591 isc_rwlocktype_read);
7593 type = header->type;
7594 rdtype = RBTDB_RDATATYPE_BASE(header->type);
7596 covers = RBTDB_RDATATYPE_EXT(header->type);
7597 negtype = RBTDB_RDATATYPE_VALUE(covers, 0);
7599 negtype = RBTDB_RDATATYPE_VALUE(0, rdtype);
7600 for (header = header->next; header != NULL; header = top_next) {
7601 top_next = header->next;
7603 * If not walking back up the down list.
7605 if (header->type != type && header->type != negtype) {
7607 if (header->serial <= serial &&
7610 * Is this a "this rdataset doesn't
7613 * Note: unlike everywhere else, we
7614 * check for now > header->ttl instead
7615 * of now >= header->ttl. This allows
7616 * ANY and RRSIG queries for 0 TTL
7617 * rdatasets to work.
7619 if ((header->attributes &
7620 RDATASET_ATTR_NONEXISTENT) != 0 ||
7621 (now != 0 && now > header->rdh_ttl))
7625 header = header->down;
7626 } while (header != NULL);
7632 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7633 isc_rwlocktype_read);
7635 rbtiterator->current = header;
7638 return (ISC_R_NOMORE);
7640 return (ISC_R_SUCCESS);
7644 rdatasetiter_current(dns_rdatasetiter_t *iterator, dns_rdataset_t *rdataset) {
7645 rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
7646 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
7647 dns_rbtnode_t *rbtnode = rbtiterator->common.node;
7648 rdatasetheader_t *header;
7650 header = rbtiterator->current;
7651 REQUIRE(header != NULL);
7653 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7654 isc_rwlocktype_read);
7656 bind_rdataset(rbtdb, rbtnode, header, rbtiterator->common.now,
7659 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7660 isc_rwlocktype_read);
7665 * Database Iterator Methods
7669 reference_iter_node(rbtdb_dbiterator_t *rbtdbiter) {
7670 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7671 dns_rbtnode_t *node = rbtdbiter->node;
7676 INSIST(rbtdbiter->tree_locked != isc_rwlocktype_none);
7677 reactivate_node(rbtdb, node, rbtdbiter->tree_locked);
7681 dereference_iter_node(rbtdb_dbiterator_t *rbtdbiter) {
7682 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7683 dns_rbtnode_t *node = rbtdbiter->node;
7689 lock = &rbtdb->node_locks[node->locknum].lock;
7690 NODE_LOCK(lock, isc_rwlocktype_read);
7691 decrement_reference(rbtdb, node, 0, isc_rwlocktype_read,
7692 rbtdbiter->tree_locked, ISC_FALSE);
7693 NODE_UNLOCK(lock, isc_rwlocktype_read);
7695 rbtdbiter->node = NULL;
7699 flush_deletions(rbtdb_dbiterator_t *rbtdbiter) {
7700 dns_rbtnode_t *node;
7701 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7702 isc_boolean_t was_read_locked = ISC_FALSE;
7706 if (rbtdbiter->delete != 0) {
7708 * Note that "%d node of %d in tree" can report things like
7709 * "flush_deletions: 59 nodes of 41 in tree". This means
7710 * That some nodes appear on the deletions list more than
7711 * once. Only the last occurence will actually be deleted.
7713 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
7714 DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
7715 "flush_deletions: %d nodes of %d in tree",
7717 dns_rbt_nodecount(rbtdb->tree));
7719 if (rbtdbiter->tree_locked == isc_rwlocktype_read) {
7720 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
7721 was_read_locked = ISC_TRUE;
7723 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
7724 rbtdbiter->tree_locked = isc_rwlocktype_write;
7726 for (i = 0; i < rbtdbiter->delete; i++) {
7727 node = rbtdbiter->deletions[i];
7728 lock = &rbtdb->node_locks[node->locknum].lock;
7730 NODE_LOCK(lock, isc_rwlocktype_read);
7731 decrement_reference(rbtdb, node, 0,
7732 isc_rwlocktype_read,
7733 rbtdbiter->tree_locked, ISC_FALSE);
7734 NODE_UNLOCK(lock, isc_rwlocktype_read);
7737 rbtdbiter->delete = 0;
7739 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
7740 if (was_read_locked) {
7741 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
7742 rbtdbiter->tree_locked = isc_rwlocktype_read;
7745 rbtdbiter->tree_locked = isc_rwlocktype_none;
7751 resume_iteration(rbtdb_dbiterator_t *rbtdbiter) {
7752 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7754 REQUIRE(rbtdbiter->paused);
7755 REQUIRE(rbtdbiter->tree_locked == isc_rwlocktype_none);
7757 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
7758 rbtdbiter->tree_locked = isc_rwlocktype_read;
7760 rbtdbiter->paused = ISC_FALSE;
7764 dbiterator_destroy(dns_dbiterator_t **iteratorp) {
7765 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)(*iteratorp);
7766 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7767 dns_db_t *db = NULL;
7769 if (rbtdbiter->tree_locked == isc_rwlocktype_read) {
7770 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
7771 rbtdbiter->tree_locked = isc_rwlocktype_none;
7773 INSIST(rbtdbiter->tree_locked == isc_rwlocktype_none);
7775 dereference_iter_node(rbtdbiter);
7777 flush_deletions(rbtdbiter);
7779 dns_db_attach(rbtdbiter->common.db, &db);
7780 dns_db_detach(&rbtdbiter->common.db);
7782 dns_rbtnodechain_reset(&rbtdbiter->chain);
7783 dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
7784 isc_mem_put(db->mctx, rbtdbiter, sizeof(*rbtdbiter));
7791 dbiterator_first(dns_dbiterator_t *iterator) {
7792 isc_result_t result;
7793 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7794 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7795 dns_name_t *name, *origin;
7797 if (rbtdbiter->result != ISC_R_SUCCESS &&
7798 rbtdbiter->result != ISC_R_NOMORE)
7799 return (rbtdbiter->result);
7801 if (rbtdbiter->paused)
7802 resume_iteration(rbtdbiter);
7804 dereference_iter_node(rbtdbiter);
7806 name = dns_fixedname_name(&rbtdbiter->name);
7807 origin = dns_fixedname_name(&rbtdbiter->origin);
7808 dns_rbtnodechain_reset(&rbtdbiter->chain);
7809 dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
7811 if (rbtdbiter->nsec3only) {
7812 rbtdbiter->current = &rbtdbiter->nsec3chain;
7813 result = dns_rbtnodechain_first(rbtdbiter->current,
7814 rbtdb->nsec3, name, origin);
7816 rbtdbiter->current = &rbtdbiter->chain;
7817 result = dns_rbtnodechain_first(rbtdbiter->current,
7818 rbtdb->tree, name, origin);
7819 if (!rbtdbiter->nonsec3 && result == ISC_R_NOTFOUND) {
7820 rbtdbiter->current = &rbtdbiter->nsec3chain;
7821 result = dns_rbtnodechain_first(rbtdbiter->current,
7826 if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
7827 result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
7828 NULL, &rbtdbiter->node);
7829 if (result == ISC_R_SUCCESS) {
7830 rbtdbiter->new_origin = ISC_TRUE;
7831 reference_iter_node(rbtdbiter);
7834 INSIST(result == ISC_R_NOTFOUND);
7835 result = ISC_R_NOMORE; /* The tree is empty. */
7838 rbtdbiter->result = result;
7844 dbiterator_last(dns_dbiterator_t *iterator) {
7845 isc_result_t result;
7846 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7847 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7848 dns_name_t *name, *origin;
7850 if (rbtdbiter->result != ISC_R_SUCCESS &&
7851 rbtdbiter->result != ISC_R_NOMORE)
7852 return (rbtdbiter->result);
7854 if (rbtdbiter->paused)
7855 resume_iteration(rbtdbiter);
7857 dereference_iter_node(rbtdbiter);
7859 name = dns_fixedname_name(&rbtdbiter->name);
7860 origin = dns_fixedname_name(&rbtdbiter->origin);
7861 dns_rbtnodechain_reset(&rbtdbiter->chain);
7862 dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
7864 result = ISC_R_NOTFOUND;
7865 if (rbtdbiter->nsec3only && !rbtdbiter->nonsec3) {
7866 rbtdbiter->current = &rbtdbiter->nsec3chain;
7867 result = dns_rbtnodechain_last(rbtdbiter->current,
7868 rbtdb->nsec3, name, origin);
7870 if (!rbtdbiter->nsec3only && result == ISC_R_NOTFOUND) {
7871 rbtdbiter->current = &rbtdbiter->chain;
7872 result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->tree,
7875 if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
7876 result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
7877 NULL, &rbtdbiter->node);
7878 if (result == ISC_R_SUCCESS) {
7879 rbtdbiter->new_origin = ISC_TRUE;
7880 reference_iter_node(rbtdbiter);
7883 INSIST(result == ISC_R_NOTFOUND);
7884 result = ISC_R_NOMORE; /* The tree is empty. */
7887 rbtdbiter->result = result;
7893 dbiterator_seek(dns_dbiterator_t *iterator, dns_name_t *name) {
7894 isc_result_t result;
7895 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7896 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7897 dns_name_t *iname, *origin;
7899 if (rbtdbiter->result != ISC_R_SUCCESS &&
7900 rbtdbiter->result != ISC_R_NOTFOUND &&
7901 rbtdbiter->result != ISC_R_NOMORE)
7902 return (rbtdbiter->result);
7904 if (rbtdbiter->paused)
7905 resume_iteration(rbtdbiter);
7907 dereference_iter_node(rbtdbiter);
7909 iname = dns_fixedname_name(&rbtdbiter->name);
7910 origin = dns_fixedname_name(&rbtdbiter->origin);
7911 dns_rbtnodechain_reset(&rbtdbiter->chain);
7912 dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
7914 if (rbtdbiter->nsec3only) {
7915 rbtdbiter->current = &rbtdbiter->nsec3chain;
7916 result = dns_rbt_findnode(rbtdb->nsec3, name, NULL,
7919 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
7920 } else if (rbtdbiter->nonsec3) {
7921 rbtdbiter->current = &rbtdbiter->chain;
7922 result = dns_rbt_findnode(rbtdb->tree, name, NULL,
7925 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
7928 * Stay on main chain if not found on either chain.
7930 rbtdbiter->current = &rbtdbiter->chain;
7931 result = dns_rbt_findnode(rbtdb->tree, name, NULL,
7934 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
7935 if (result == DNS_R_PARTIALMATCH) {
7936 dns_rbtnode_t *node = NULL;
7937 result = dns_rbt_findnode(rbtdb->nsec3, name, NULL,
7938 &node, &rbtdbiter->nsec3chain,
7939 DNS_RBTFIND_EMPTYDATA,
7941 if (result == ISC_R_SUCCESS) {
7942 rbtdbiter->node = node;
7943 rbtdbiter->current = &rbtdbiter->nsec3chain;
7949 if (result == ISC_R_SUCCESS) {
7950 result = dns_rbtnodechain_current(rbtdbiter->current, iname,
7952 if (result == ISC_R_SUCCESS) {
7953 rbtdbiter->new_origin = ISC_TRUE;
7954 reference_iter_node(rbtdbiter);
7956 } else if (result == DNS_R_PARTIALMATCH) {
7957 result = ISC_R_NOTFOUND;
7958 rbtdbiter->node = NULL;
7961 rbtdbiter->result = result;
7963 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
7964 isc_result_t tresult;
7965 tresult = dns_rbtnodechain_current(rbtdbiter->current, iname,
7967 if (tresult == ISC_R_SUCCESS) {
7968 rbtdbiter->new_origin = ISC_TRUE;
7969 reference_iter_node(rbtdbiter);
7972 rbtdbiter->node = NULL;
7975 rbtdbiter->node = NULL;
7977 rbtdbiter->result = (result == DNS_R_PARTIALMATCH) ?
7978 ISC_R_SUCCESS : result;
7985 dbiterator_prev(dns_dbiterator_t *iterator) {
7986 isc_result_t result;
7987 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7988 dns_name_t *name, *origin;
7989 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7991 REQUIRE(rbtdbiter->node != NULL);
7993 if (rbtdbiter->result != ISC_R_SUCCESS)
7994 return (rbtdbiter->result);
7996 if (rbtdbiter->paused)
7997 resume_iteration(rbtdbiter);
7999 name = dns_fixedname_name(&rbtdbiter->name);
8000 origin = dns_fixedname_name(&rbtdbiter->origin);
8001 result = dns_rbtnodechain_prev(rbtdbiter->current, name, origin);
8002 if (result == ISC_R_NOMORE && !rbtdbiter->nsec3only &&
8003 !rbtdbiter->nonsec3 &&
8004 &rbtdbiter->nsec3chain == rbtdbiter->current) {
8005 rbtdbiter->current = &rbtdbiter->chain;
8006 dns_rbtnodechain_reset(rbtdbiter->current);
8007 result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->tree,
8009 if (result == ISC_R_NOTFOUND)
8010 result = ISC_R_NOMORE;
8013 dereference_iter_node(rbtdbiter);
8015 if (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
8016 rbtdbiter->new_origin = ISC_TF(result == DNS_R_NEWORIGIN);
8017 result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
8018 NULL, &rbtdbiter->node);
8021 if (result == ISC_R_SUCCESS)
8022 reference_iter_node(rbtdbiter);
8024 rbtdbiter->result = result;
8030 dbiterator_next(dns_dbiterator_t *iterator) {
8031 isc_result_t result;
8032 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
8033 dns_name_t *name, *origin;
8034 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
8036 REQUIRE(rbtdbiter->node != NULL);
8038 if (rbtdbiter->result != ISC_R_SUCCESS)
8039 return (rbtdbiter->result);
8041 if (rbtdbiter->paused)
8042 resume_iteration(rbtdbiter);
8044 name = dns_fixedname_name(&rbtdbiter->name);
8045 origin = dns_fixedname_name(&rbtdbiter->origin);
8046 result = dns_rbtnodechain_next(rbtdbiter->current, name, origin);
8047 if (result == ISC_R_NOMORE && !rbtdbiter->nsec3only &&
8048 !rbtdbiter->nonsec3 && &rbtdbiter->chain == rbtdbiter->current) {
8049 rbtdbiter->current = &rbtdbiter->nsec3chain;
8050 dns_rbtnodechain_reset(rbtdbiter->current);
8051 result = dns_rbtnodechain_first(rbtdbiter->current,
8052 rbtdb->nsec3, name, origin);
8053 if (result == ISC_R_NOTFOUND)
8054 result = ISC_R_NOMORE;
8057 dereference_iter_node(rbtdbiter);
8059 if (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
8060 rbtdbiter->new_origin = ISC_TF(result == DNS_R_NEWORIGIN);
8061 result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
8062 NULL, &rbtdbiter->node);
8064 if (result == ISC_R_SUCCESS)
8065 reference_iter_node(rbtdbiter);
8067 rbtdbiter->result = result;
8073 dbiterator_current(dns_dbiterator_t *iterator, dns_dbnode_t **nodep,
8076 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
8077 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
8078 dns_rbtnode_t *node = rbtdbiter->node;
8079 isc_result_t result;
8080 dns_name_t *nodename = dns_fixedname_name(&rbtdbiter->name);
8081 dns_name_t *origin = dns_fixedname_name(&rbtdbiter->origin);
8083 REQUIRE(rbtdbiter->result == ISC_R_SUCCESS);
8084 REQUIRE(rbtdbiter->node != NULL);
8086 if (rbtdbiter->paused)
8087 resume_iteration(rbtdbiter);
8090 if (rbtdbiter->common.relative_names)
8092 result = dns_name_concatenate(nodename, origin, name, NULL);
8093 if (result != ISC_R_SUCCESS)
8095 if (rbtdbiter->common.relative_names && rbtdbiter->new_origin)
8096 result = DNS_R_NEWORIGIN;
8098 result = ISC_R_SUCCESS;
8100 NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
8101 new_reference(rbtdb, node);
8102 NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
8104 *nodep = rbtdbiter->node;
8106 if (iterator->cleaning && result == ISC_R_SUCCESS) {
8107 isc_result_t expire_result;
8110 * If the deletion array is full, flush it before trying
8111 * to expire the current node. The current node can't
8112 * fully deleted while the iteration cursor is still on it.
8114 if (rbtdbiter->delete == DELETION_BATCH_MAX)
8115 flush_deletions(rbtdbiter);
8117 expire_result = expirenode(iterator->db, *nodep, 0);
8120 * expirenode() currently always returns success.
8122 if (expire_result == ISC_R_SUCCESS && node->down == NULL) {
8125 rbtdbiter->deletions[rbtdbiter->delete++] = node;
8126 NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
8127 dns_rbtnode_refincrement(node, &refs);
8129 NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
8137 dbiterator_pause(dns_dbiterator_t *iterator) {
8138 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
8139 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
8141 if (rbtdbiter->result != ISC_R_SUCCESS &&
8142 rbtdbiter->result != ISC_R_NOMORE)
8143 return (rbtdbiter->result);
8145 if (rbtdbiter->paused)
8146 return (ISC_R_SUCCESS);
8148 rbtdbiter->paused = ISC_TRUE;
8150 if (rbtdbiter->tree_locked != isc_rwlocktype_none) {
8151 INSIST(rbtdbiter->tree_locked == isc_rwlocktype_read);
8152 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
8153 rbtdbiter->tree_locked = isc_rwlocktype_none;
8156 flush_deletions(rbtdbiter);
8158 return (ISC_R_SUCCESS);
8162 dbiterator_origin(dns_dbiterator_t *iterator, dns_name_t *name) {
8163 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
8164 dns_name_t *origin = dns_fixedname_name(&rbtdbiter->origin);
8166 if (rbtdbiter->result != ISC_R_SUCCESS)
8167 return (rbtdbiter->result);
8169 return (dns_name_copy(origin, name, NULL));
8173 * Additional cache routines.
8176 rdataset_getadditional(dns_rdataset_t *rdataset, dns_rdatasetadditional_t type,
8177 dns_rdatatype_t qtype, dns_acache_t *acache,
8178 dns_zone_t **zonep, dns_db_t **dbp,
8179 dns_dbversion_t **versionp, dns_dbnode_t **nodep,
8180 dns_name_t *fname, dns_message_t *msg,
8183 dns_rbtdb_t *rbtdb = rdataset->private1;
8184 dns_rbtnode_t *rbtnode = rdataset->private2;
8185 unsigned char *raw = rdataset->private3; /* RDATASLAB */
8186 unsigned int current_count = rdataset->privateuint4;
8188 rdatasetheader_t *header;
8189 nodelock_t *nodelock;
8190 unsigned int total_count;
8191 acachectl_t *acarray;
8192 dns_acacheentry_t *entry;
8193 isc_result_t result;
8195 UNUSED(qtype); /* we do not use this value at least for now */
8198 header = (struct rdatasetheader *)(raw - sizeof(*header));
8200 total_count = raw[0] * 256 + raw[1];
8201 INSIST(total_count > current_count);
8202 count = total_count - current_count - 1;
8206 nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
8207 NODE_LOCK(nodelock, isc_rwlocktype_read);
8210 case dns_rdatasetadditional_fromauth:
8211 acarray = header->additional_auth;
8213 case dns_rdatasetadditional_fromcache:
8216 case dns_rdatasetadditional_fromglue:
8217 acarray = header->additional_glue;
8223 if (acarray == NULL) {
8224 if (type != dns_rdatasetadditional_fromcache)
8225 dns_acache_countquerymiss(acache);
8226 NODE_UNLOCK(nodelock, isc_rwlocktype_read);
8227 return (ISC_R_NOTFOUND);
8230 if (acarray[count].entry == NULL) {
8231 dns_acache_countquerymiss(acache);
8232 NODE_UNLOCK(nodelock, isc_rwlocktype_read);
8233 return (ISC_R_NOTFOUND);
8237 dns_acache_attachentry(acarray[count].entry, &entry);
8239 NODE_UNLOCK(nodelock, isc_rwlocktype_read);
8241 result = dns_acache_getentry(entry, zonep, dbp, versionp,
8242 nodep, fname, msg, now);
8244 dns_acache_detachentry(&entry);
8250 acache_callback(dns_acacheentry_t *entry, void **arg) {
8252 dns_rbtnode_t *rbtnode;
8253 nodelock_t *nodelock;
8254 acachectl_t *acarray = NULL;
8255 acache_cbarg_t *cbarg;
8258 REQUIRE(arg != NULL);
8262 * The caller must hold the entry lock.
8265 rbtdb = (dns_rbtdb_t *)cbarg->db;
8266 rbtnode = (dns_rbtnode_t *)cbarg->node;
8268 nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
8269 NODE_LOCK(nodelock, isc_rwlocktype_write);
8271 switch (cbarg->type) {
8272 case dns_rdatasetadditional_fromauth:
8273 acarray = cbarg->header->additional_auth;
8275 case dns_rdatasetadditional_fromglue:
8276 acarray = cbarg->header->additional_glue;
8282 count = cbarg->count;
8283 if (acarray != NULL && acarray[count].entry == entry) {
8284 acarray[count].entry = NULL;
8285 INSIST(acarray[count].cbarg == cbarg);
8286 isc_mem_put(rbtdb->common.mctx, cbarg, sizeof(acache_cbarg_t));
8287 acarray[count].cbarg = NULL;
8289 isc_mem_put(rbtdb->common.mctx, cbarg, sizeof(acache_cbarg_t));
8291 dns_acache_detachentry(&entry);
8293 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8295 dns_db_detachnode((dns_db_t *)rbtdb, (dns_dbnode_t **)(void*)&rbtnode);
8296 dns_db_detach((dns_db_t **)(void*)&rbtdb);
8302 acache_cancelentry(isc_mem_t *mctx, dns_acacheentry_t *entry,
8303 acache_cbarg_t **cbargp)
8305 acache_cbarg_t *cbarg;
8307 REQUIRE(mctx != NULL);
8308 REQUIRE(entry != NULL);
8309 REQUIRE(cbargp != NULL && *cbargp != NULL);
8313 dns_acache_cancelentry(entry);
8314 dns_db_detachnode(cbarg->db, &cbarg->node);
8315 dns_db_detach(&cbarg->db);
8317 isc_mem_put(mctx, cbarg, sizeof(acache_cbarg_t));
8323 rdataset_setadditional(dns_rdataset_t *rdataset, dns_rdatasetadditional_t type,
8324 dns_rdatatype_t qtype, dns_acache_t *acache,
8325 dns_zone_t *zone, dns_db_t *db,
8326 dns_dbversion_t *version, dns_dbnode_t *node,
8329 dns_rbtdb_t *rbtdb = rdataset->private1;
8330 dns_rbtnode_t *rbtnode = rdataset->private2;
8331 unsigned char *raw = rdataset->private3; /* RDATASLAB */
8332 unsigned int current_count = rdataset->privateuint4;
8333 rdatasetheader_t *header;
8334 unsigned int total_count, count;
8335 nodelock_t *nodelock;
8336 isc_result_t result;
8337 acachectl_t *acarray;
8338 dns_acacheentry_t *newentry, *oldentry = NULL;
8339 acache_cbarg_t *newcbarg, *oldcbarg = NULL;
8343 if (type == dns_rdatasetadditional_fromcache)
8344 return (ISC_R_SUCCESS);
8346 header = (struct rdatasetheader *)(raw - sizeof(*header));
8348 total_count = raw[0] * 256 + raw[1];
8349 INSIST(total_count > current_count);
8350 count = total_count - current_count - 1; /* should be private data */
8352 newcbarg = isc_mem_get(rbtdb->common.mctx, sizeof(*newcbarg));
8353 if (newcbarg == NULL)
8354 return (ISC_R_NOMEMORY);
8355 newcbarg->type = type;
8356 newcbarg->count = count;
8357 newcbarg->header = header;
8358 newcbarg->db = NULL;
8359 dns_db_attach((dns_db_t *)rbtdb, &newcbarg->db);
8360 newcbarg->node = NULL;
8361 dns_db_attachnode((dns_db_t *)rbtdb, (dns_dbnode_t *)rbtnode,
8364 result = dns_acache_createentry(acache, (dns_db_t *)rbtdb,
8365 acache_callback, newcbarg, &newentry);
8366 if (result != ISC_R_SUCCESS)
8368 /* Set cache data in the new entry. */
8369 result = dns_acache_setentry(acache, newentry, zone, db,
8370 version, node, fname);
8371 if (result != ISC_R_SUCCESS)
8374 nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
8375 NODE_LOCK(nodelock, isc_rwlocktype_write);
8379 case dns_rdatasetadditional_fromauth:
8380 acarray = header->additional_auth;
8382 case dns_rdatasetadditional_fromglue:
8383 acarray = header->additional_glue;
8389 if (acarray == NULL) {
8392 acarray = isc_mem_get(rbtdb->common.mctx, total_count *
8393 sizeof(acachectl_t));
8395 if (acarray == NULL) {
8396 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8400 for (i = 0; i < total_count; i++) {
8401 acarray[i].entry = NULL;
8402 acarray[i].cbarg = NULL;
8406 case dns_rdatasetadditional_fromauth:
8407 header->additional_auth = acarray;
8409 case dns_rdatasetadditional_fromglue:
8410 header->additional_glue = acarray;
8416 if (acarray[count].entry != NULL) {
8418 * Swap the entry. Delay cleaning-up the old entry since
8419 * it would require a node lock.
8421 oldentry = acarray[count].entry;
8422 INSIST(acarray[count].cbarg != NULL);
8423 oldcbarg = acarray[count].cbarg;
8425 acarray[count].entry = newentry;
8426 acarray[count].cbarg = newcbarg;
8428 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8430 if (oldentry != NULL) {
8431 acache_cancelentry(rbtdb->common.mctx, oldentry, &oldcbarg);
8432 dns_acache_detachentry(&oldentry);
8435 return (ISC_R_SUCCESS);
8438 if (newcbarg != NULL) {
8439 if (newentry != NULL) {
8440 acache_cancelentry(rbtdb->common.mctx, newentry,
8442 dns_acache_detachentry(&newentry);
8444 dns_db_detachnode((dns_db_t *)rbtdb, &newcbarg->node);
8445 dns_db_detach(&newcbarg->db);
8446 isc_mem_put(rbtdb->common.mctx, newcbarg,
8455 rdataset_putadditional(dns_acache_t *acache, dns_rdataset_t *rdataset,
8456 dns_rdatasetadditional_t type, dns_rdatatype_t qtype)
8458 dns_rbtdb_t *rbtdb = rdataset->private1;
8459 dns_rbtnode_t *rbtnode = rdataset->private2;
8460 unsigned char *raw = rdataset->private3; /* RDATASLAB */
8461 unsigned int current_count = rdataset->privateuint4;
8462 rdatasetheader_t *header;
8463 nodelock_t *nodelock;
8464 unsigned int total_count, count;
8465 acachectl_t *acarray;
8466 dns_acacheentry_t *entry;
8467 acache_cbarg_t *cbarg;
8469 UNUSED(qtype); /* we do not use this value at least for now */
8472 if (type == dns_rdatasetadditional_fromcache)
8473 return (ISC_R_SUCCESS);
8475 header = (struct rdatasetheader *)(raw - sizeof(*header));
8477 total_count = raw[0] * 256 + raw[1];
8478 INSIST(total_count > current_count);
8479 count = total_count - current_count - 1;
8484 nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
8485 NODE_LOCK(nodelock, isc_rwlocktype_write);
8488 case dns_rdatasetadditional_fromauth:
8489 acarray = header->additional_auth;
8491 case dns_rdatasetadditional_fromglue:
8492 acarray = header->additional_glue;
8498 if (acarray == NULL) {
8499 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8500 return (ISC_R_NOTFOUND);
8503 entry = acarray[count].entry;
8504 if (entry == NULL) {
8505 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8506 return (ISC_R_NOTFOUND);
8509 acarray[count].entry = NULL;
8510 cbarg = acarray[count].cbarg;
8511 acarray[count].cbarg = NULL;
8513 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8515 if (entry != NULL) {
8517 acache_cancelentry(rbtdb->common.mctx, entry, &cbarg);
8518 dns_acache_detachentry(&entry);
8521 return (ISC_R_SUCCESS);
8525 * Routines for LRU-based cache management.
8529 * See if a given cache entry that is being reused needs to be updated
8530 * in the LRU-list. From the LRU management point of view, this function is
8531 * expected to return true for almost all cases. When used with threads,
8532 * however, this may cause a non-negligible performance penalty because a
8533 * writer lock will have to be acquired before updating the list.
8534 * If DNS_RBTDB_LIMITLRUUPDATE is defined to be non 0 at compilation time, this
8535 * function returns true if the entry has not been updated for some period of
8536 * time. We differentiate the NS or glue address case and the others since
8537 * experiments have shown that the former tends to be accessed relatively
8538 * infrequently and the cost of cache miss is higher (e.g., a missing NS records
8539 * may cause external queries at a higher level zone, involving more
8542 * Caller must hold the node (read or write) lock.
8544 static inline isc_boolean_t
8545 need_headerupdate(rdatasetheader_t *header, isc_stdtime_t now) {
8546 if ((header->attributes &
8547 (RDATASET_ATTR_NONEXISTENT|RDATASET_ATTR_STALE)) != 0)
8550 #if DNS_RBTDB_LIMITLRUUPDATE
8551 if (header->type == dns_rdatatype_ns ||
8552 (header->trust == dns_trust_glue &&
8553 (header->type == dns_rdatatype_a ||
8554 header->type == dns_rdatatype_aaaa))) {
8556 * Glue records are updated if at least 60 seconds have passed
8557 * since the previous update time.
8559 return (header->last_used + 60 <= now);
8562 /* Other records are updated if 5 minutes have passed. */
8563 return (header->last_used + 300 <= now);
8572 * Update the timestamp of a given cache entry and move it to the head
8573 * of the corresponding LRU list.
8575 * Caller must hold the node (write) lock.
8577 * Note that the we do NOT touch the heap here, as the TTL has not changed.
8580 update_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
8583 INSIST(IS_CACHE(rbtdb));
8585 /* To be checked: can we really assume this? XXXMLG */
8586 INSIST(ISC_LINK_LINKED(header, link));
8588 ISC_LIST_UNLINK(rbtdb->rdatasets[header->node->locknum], header, link);
8589 header->last_used = now;
8590 ISC_LIST_PREPEND(rbtdb->rdatasets[header->node->locknum], header, link);
8594 * Purge some expired and/or stale (i.e. unused for some period) cache entries
8595 * under an overmem condition. To recover from this condition quickly, up to
8596 * 2 entries will be purged. This process is triggered while adding a new
8597 * entry, and we specifically avoid purging entries in the same LRU bucket as
8598 * the one to which the new entry will belong. Otherwise, we might purge
8599 * entries of the same name of different RR types while adding RRsets from a
8600 * single response (consider the case where we're adding A and AAAA glue records
8601 * of the same NS name).
8604 overmem_purge(dns_rbtdb_t *rbtdb, unsigned int locknum_start,
8605 isc_stdtime_t now, isc_boolean_t tree_locked)
8607 rdatasetheader_t *header, *header_prev;
8608 unsigned int locknum;
8611 for (locknum = (locknum_start + 1) % rbtdb->node_lock_count;
8612 locknum != locknum_start && purgecount > 0;
8613 locknum = (locknum + 1) % rbtdb->node_lock_count) {
8614 NODE_LOCK(&rbtdb->node_locks[locknum].lock,
8615 isc_rwlocktype_write);
8617 header = isc_heap_element(rbtdb->heaps[locknum], 1);
8618 if (header && header->rdh_ttl <= now - RBTDB_VIRTUAL) {
8619 expire_header(rbtdb, header, tree_locked);
8623 for (header = ISC_LIST_TAIL(rbtdb->rdatasets[locknum]);
8624 header != NULL && purgecount > 0;
8625 header = header_prev) {
8626 header_prev = ISC_LIST_PREV(header, link);
8628 * Unlink the entry at this point to avoid checking it
8629 * again even if it's currently used someone else and
8630 * cannot be purged at this moment. This entry won't be
8631 * referenced any more (so unlinking is safe) since the
8632 * TTL was reset to 0.
8634 ISC_LIST_UNLINK(rbtdb->rdatasets[locknum], header,
8636 expire_header(rbtdb, header, tree_locked);
8640 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
8641 isc_rwlocktype_write);
8646 expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
8647 isc_boolean_t tree_locked)
8649 set_ttl(rbtdb, header, 0);
8650 header->attributes |= RDATASET_ATTR_STALE;
8651 header->node->dirty = 1;
8654 * Caller must hold the node (write) lock.
8657 if (dns_rbtnode_refcurrent(header->node) == 0) {
8659 * If no one else is using the node, we can clean it up now.
8660 * We first need to gain a new reference to the node to meet a
8661 * requirement of decrement_reference().
8663 new_reference(rbtdb, header->node);
8664 decrement_reference(rbtdb, header->node, 0,
8665 isc_rwlocktype_write,
8666 tree_locked ? isc_rwlocktype_write :
8667 isc_rwlocktype_none, ISC_FALSE);