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.3 2010/08/13 07:25:21 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;
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;
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 if (covers == dns_rdatatype_any) {
5480 * We're adding an negative cache entry
5481 * which covers all types (NXDOMAIN,
5482 * NODATA(QTYPE=ANY)).
5484 * We make all other data stale so that the
5485 * only rdataset that can be found at this
5486 * node is the negative cache entry.
5488 for (topheader = rbtnode->data;
5490 topheader = topheader->next) {
5491 set_ttl(rbtdb, topheader, 0);
5492 topheader->attributes |=
5493 RDATASET_ATTR_STALE;
5498 negtype = RBTDB_RDATATYPE_VALUE(covers, 0);
5501 * We're adding something that isn't a
5502 * negative cache entry. Look for an extant
5503 * non-stale NXDOMAIN/NODATA(QTYPE=ANY) negative
5506 for (topheader = rbtnode->data;
5508 topheader = topheader->next) {
5509 if (topheader->type ==
5510 RBTDB_RDATATYPE_NCACHEANY)
5513 if (topheader != NULL && EXISTS(topheader) &&
5514 topheader->rdh_ttl > now) {
5518 if (trust < topheader->trust) {
5520 * The NXDOMAIN/NODATA(QTYPE=ANY)
5523 free_rdataset(rbtdb,
5526 if (addedrdataset != NULL)
5527 bind_rdataset(rbtdb, rbtnode,
5530 return (DNS_R_UNCHANGED);
5533 * The new rdataset is better. Expire the
5534 * NXDOMAIN/NODATA(QTYPE=ANY).
5536 set_ttl(rbtdb, topheader, 0);
5537 topheader->attributes |= RDATASET_ATTR_STALE;
5542 negtype = RBTDB_RDATATYPE_VALUE(0, rdtype);
5546 for (topheader = rbtnode->data;
5548 topheader = topheader->next) {
5549 if (topheader->type == newheader->type ||
5550 topheader->type == negtype)
5552 topheader_prev = topheader;
5557 * If header isn't NULL, we've found the right type. There may be
5558 * IGNORE rdatasets between the top of the chain and the first real
5559 * data. We skip over them.
5562 while (header != NULL && IGNORE(header))
5563 header = header->down;
5564 if (header != NULL) {
5565 header_nx = NONEXISTENT(header) ? ISC_TRUE : ISC_FALSE;
5568 * Deleting an already non-existent rdataset has no effect.
5570 if (header_nx && newheader_nx) {
5571 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5572 return (DNS_R_UNCHANGED);
5576 * Trying to add an rdataset with lower trust to a cache DB
5577 * has no effect, provided that the cache data isn't stale.
5579 if (rbtversion == NULL && trust < header->trust &&
5580 (header->rdh_ttl > now || header_nx)) {
5581 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5582 if (addedrdataset != NULL)
5583 bind_rdataset(rbtdb, rbtnode, header, now,
5585 return (DNS_R_UNCHANGED);
5589 * Don't merge if a nonexistent rdataset is involved.
5591 if (merge && (header_nx || newheader_nx))
5595 * If 'merge' is ISC_TRUE, we'll try to create a new rdataset
5596 * that is the union of 'newheader' and 'header'.
5599 unsigned int flags = 0;
5600 INSIST(rbtversion->serial >= header->serial);
5602 result = ISC_R_SUCCESS;
5604 if ((options & DNS_DBADD_EXACT) != 0)
5605 flags |= DNS_RDATASLAB_EXACT;
5606 if ((options & DNS_DBADD_EXACTTTL) != 0 &&
5607 newheader->rdh_ttl != header->rdh_ttl)
5608 result = DNS_R_NOTEXACT;
5609 else if (newheader->rdh_ttl != header->rdh_ttl)
5610 flags |= DNS_RDATASLAB_FORCE;
5611 if (result == ISC_R_SUCCESS)
5612 result = dns_rdataslab_merge(
5613 (unsigned char *)header,
5614 (unsigned char *)newheader,
5615 (unsigned int)(sizeof(*newheader)),
5617 rbtdb->common.rdclass,
5618 (dns_rdatatype_t)header->type,
5620 if (result == ISC_R_SUCCESS) {
5622 * If 'header' has the same serial number as
5623 * we do, we could clean it up now if we knew
5624 * that our caller had no references to it.
5625 * We don't know this, however, so we leave it
5626 * alone. It will get cleaned up when
5627 * clean_zone_node() runs.
5629 free_rdataset(rbtdb, rbtdb->common.mctx,
5631 newheader = (rdatasetheader_t *)merged;
5632 if (loading && RESIGN(newheader) &&
5634 header->resign < newheader->resign)
5635 newheader->resign = header->resign;
5637 free_rdataset(rbtdb, rbtdb->common.mctx,
5643 * Don't replace existing NS, A and AAAA RRsets
5644 * in the cache if they are already exist. This
5645 * prevents named being locked to old servers.
5646 * Don't lower trust of existing record if the
5649 if (IS_CACHE(rbtdb) && header->rdh_ttl > now &&
5650 header->type == dns_rdatatype_ns &&
5651 !header_nx && !newheader_nx &&
5652 header->trust >= newheader->trust &&
5653 dns_rdataslab_equalx((unsigned char *)header,
5654 (unsigned char *)newheader,
5655 (unsigned int)(sizeof(*newheader)),
5656 rbtdb->common.rdclass,
5657 (dns_rdatatype_t)header->type)) {
5659 * Honour the new ttl if it is less than the
5662 if (header->rdh_ttl > newheader->rdh_ttl)
5663 set_ttl(rbtdb, header, newheader->rdh_ttl);
5664 if (header->noqname == NULL &&
5665 newheader->noqname != NULL) {
5666 header->noqname = newheader->noqname;
5667 newheader->noqname = NULL;
5669 if (header->closest == NULL &&
5670 newheader->closest != NULL) {
5671 header->closest = newheader->closest;
5672 newheader->closest = NULL;
5674 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5675 if (addedrdataset != NULL)
5676 bind_rdataset(rbtdb, rbtnode, header, now,
5678 return (ISC_R_SUCCESS);
5680 if (IS_CACHE(rbtdb) && header->rdh_ttl > now &&
5681 (header->type == dns_rdatatype_a ||
5682 header->type == dns_rdatatype_aaaa) &&
5683 !header_nx && !newheader_nx &&
5684 header->trust >= newheader->trust &&
5685 dns_rdataslab_equal((unsigned char *)header,
5686 (unsigned char *)newheader,
5687 (unsigned int)(sizeof(*newheader)))) {
5689 * Honour the new ttl if it is less than the
5692 if (header->rdh_ttl > newheader->rdh_ttl)
5693 set_ttl(rbtdb, header, newheader->rdh_ttl);
5694 if (header->noqname == NULL &&
5695 newheader->noqname != NULL) {
5696 header->noqname = newheader->noqname;
5697 newheader->noqname = NULL;
5699 if (header->closest == NULL &&
5700 newheader->closest != NULL) {
5701 header->closest = newheader->closest;
5702 newheader->closest = NULL;
5704 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5705 if (addedrdataset != NULL)
5706 bind_rdataset(rbtdb, rbtnode, header, now,
5708 return (ISC_R_SUCCESS);
5710 INSIST(rbtversion == NULL ||
5711 rbtversion->serial >= topheader->serial);
5712 if (topheader_prev != NULL)
5713 topheader_prev->next = newheader;
5715 rbtnode->data = newheader;
5716 newheader->next = topheader->next;
5719 * There are no other references to 'header' when
5720 * loading, so we MAY clean up 'header' now.
5721 * Since we don't generate changed records when
5722 * loading, we MUST clean up 'header' now.
5724 newheader->down = NULL;
5725 free_rdataset(rbtdb, rbtdb->common.mctx, header);
5727 newheader->down = topheader;
5728 topheader->next = newheader;
5730 if (changed != NULL)
5731 changed->dirty = ISC_TRUE;
5732 if (rbtversion == NULL) {
5733 set_ttl(rbtdb, header, 0);
5734 header->attributes |= RDATASET_ATTR_STALE;
5736 idx = newheader->node->locknum;
5737 if (IS_CACHE(rbtdb)) {
5738 ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
5741 * XXXMLG We don't check the return value
5742 * here. If it fails, we will not do TTL
5743 * based expiry on this node. However, we
5744 * will do it on the LRU side, so memory
5745 * will not leak... for long.
5747 isc_heap_insert(rbtdb->heaps[idx], newheader);
5748 } else if (RESIGN(newheader))
5749 resign_insert(rbtdb, idx, newheader);
5753 * No non-IGNORED rdatasets of the given type exist at
5758 * If we're trying to delete the type, don't bother.
5761 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
5762 return (DNS_R_UNCHANGED);
5765 if (topheader != NULL) {
5767 * We have an list of rdatasets of the given type,
5768 * but they're all marked IGNORE. We simply insert
5769 * the new rdataset at the head of the list.
5771 * Ignored rdatasets cannot occur during loading, so
5775 INSIST(rbtversion == NULL ||
5776 rbtversion->serial >= topheader->serial);
5777 if (topheader_prev != NULL)
5778 topheader_prev->next = newheader;
5780 rbtnode->data = newheader;
5781 newheader->next = topheader->next;
5782 newheader->down = topheader;
5783 topheader->next = newheader;
5785 if (changed != NULL)
5786 changed->dirty = ISC_TRUE;
5789 * No rdatasets of the given type exist at the node.
5791 newheader->next = rbtnode->data;
5792 newheader->down = NULL;
5793 rbtnode->data = newheader;
5795 idx = newheader->node->locknum;
5796 if (IS_CACHE(rbtdb)) {
5797 ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
5799 isc_heap_insert(rbtdb->heaps[idx], newheader);
5800 } else if (RESIGN(newheader)) {
5801 resign_insert(rbtdb, idx, newheader);
5806 * Check if the node now contains CNAME and other data.
5808 if (rbtversion != NULL &&
5809 cname_and_other_data(rbtnode, rbtversion->serial))
5810 return (DNS_R_CNAMEANDOTHER);
5812 if (addedrdataset != NULL)
5813 bind_rdataset(rbtdb, rbtnode, newheader, now, addedrdataset);
5815 return (ISC_R_SUCCESS);
5818 static inline isc_boolean_t
5819 delegating_type(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
5820 rbtdb_rdatatype_t type)
5822 if (IS_CACHE(rbtdb)) {
5823 if (type == dns_rdatatype_dname)
5827 } else if (type == dns_rdatatype_dname ||
5828 (type == dns_rdatatype_ns &&
5829 (node != rbtdb->origin_node || IS_STUB(rbtdb))))
5834 static inline isc_result_t
5835 addnoqname(dns_rbtdb_t *rbtdb, rdatasetheader_t *newheader,
5836 dns_rdataset_t *rdataset)
5838 struct noqname *noqname;
5839 isc_mem_t *mctx = rbtdb->common.mctx;
5841 dns_rdataset_t neg, negsig;
5842 isc_result_t result;
5845 dns_name_init(&name, NULL);
5846 dns_rdataset_init(&neg);
5847 dns_rdataset_init(&negsig);
5849 result = dns_rdataset_getnoqname(rdataset, &name, &neg, &negsig);
5850 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5852 noqname = isc_mem_get(mctx, sizeof(*noqname));
5853 if (noqname == NULL) {
5854 result = ISC_R_NOMEMORY;
5857 dns_name_init(&noqname->name, NULL);
5858 noqname->neg = NULL;
5859 noqname->negsig = NULL;
5860 noqname->type = neg.type;
5861 result = dns_name_dup(&name, mctx, &noqname->name);
5862 if (result != ISC_R_SUCCESS)
5864 result = dns_rdataslab_fromrdataset(&neg, mctx, &r, 0);
5865 if (result != ISC_R_SUCCESS)
5867 noqname->neg = r.base;
5868 result = dns_rdataslab_fromrdataset(&negsig, mctx, &r, 0);
5869 if (result != ISC_R_SUCCESS)
5871 noqname->negsig = r.base;
5872 dns_rdataset_disassociate(&neg);
5873 dns_rdataset_disassociate(&negsig);
5874 newheader->noqname = noqname;
5875 return (ISC_R_SUCCESS);
5878 dns_rdataset_disassociate(&neg);
5879 dns_rdataset_disassociate(&negsig);
5880 free_noqname(mctx, &noqname);
5884 static inline isc_result_t
5885 addclosest(dns_rbtdb_t *rbtdb, rdatasetheader_t *newheader,
5886 dns_rdataset_t *rdataset)
5888 struct noqname *closest;
5889 isc_mem_t *mctx = rbtdb->common.mctx;
5891 dns_rdataset_t neg, negsig;
5892 isc_result_t result;
5895 dns_name_init(&name, NULL);
5896 dns_rdataset_init(&neg);
5897 dns_rdataset_init(&negsig);
5899 result = dns_rdataset_getclosest(rdataset, &name, &neg, &negsig);
5900 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5902 closest = isc_mem_get(mctx, sizeof(*closest));
5903 if (closest == NULL) {
5904 result = ISC_R_NOMEMORY;
5907 dns_name_init(&closest->name, NULL);
5908 closest->neg = NULL;
5909 closest->negsig = NULL;
5910 closest->type = neg.type;
5911 result = dns_name_dup(&name, mctx, &closest->name);
5912 if (result != ISC_R_SUCCESS)
5914 result = dns_rdataslab_fromrdataset(&neg, mctx, &r, 0);
5915 if (result != ISC_R_SUCCESS)
5917 closest->neg = r.base;
5918 result = dns_rdataslab_fromrdataset(&negsig, mctx, &r, 0);
5919 if (result != ISC_R_SUCCESS)
5921 closest->negsig = r.base;
5922 dns_rdataset_disassociate(&neg);
5923 dns_rdataset_disassociate(&negsig);
5924 newheader->closest = closest;
5925 return (ISC_R_SUCCESS);
5928 dns_rdataset_disassociate(&neg);
5929 dns_rdataset_disassociate(&negsig);
5930 free_noqname(mctx, &closest);
5934 static dns_dbmethods_t zone_methods;
5937 addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
5938 isc_stdtime_t now, dns_rdataset_t *rdataset, unsigned int options,
5939 dns_rdataset_t *addedrdataset)
5941 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
5942 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
5943 rbtdb_version_t *rbtversion = version;
5944 isc_region_t region;
5945 rdatasetheader_t *newheader;
5946 rdatasetheader_t *header;
5947 isc_result_t result;
5948 isc_boolean_t delegating;
5949 isc_boolean_t tree_locked = ISC_FALSE;
5950 isc_boolean_t cache_is_overmem = ISC_FALSE;
5952 REQUIRE(VALID_RBTDB(rbtdb));
5954 if (rbtdb->common.methods == &zone_methods)
5955 REQUIRE(((rbtnode->nsec3 &&
5956 (rdataset->type == dns_rdatatype_nsec3 ||
5957 rdataset->covers == dns_rdatatype_nsec3)) ||
5959 rdataset->type != dns_rdatatype_nsec3 &&
5960 rdataset->covers != dns_rdatatype_nsec3)));
5962 if (rbtversion == NULL) {
5964 isc_stdtime_get(&now);
5968 result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
5970 sizeof(rdatasetheader_t));
5971 if (result != ISC_R_SUCCESS)
5974 newheader = (rdatasetheader_t *)region.base;
5975 init_rdataset(rbtdb, newheader);
5976 set_ttl(rbtdb, newheader, rdataset->ttl + now);
5977 newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
5979 newheader->attributes = 0;
5980 newheader->noqname = NULL;
5981 newheader->closest = NULL;
5982 newheader->count = init_count++;
5983 newheader->trust = rdataset->trust;
5984 newheader->additional_auth = NULL;
5985 newheader->additional_glue = NULL;
5986 newheader->last_used = now;
5987 newheader->node = rbtnode;
5988 if (rbtversion != NULL) {
5989 newheader->serial = rbtversion->serial;
5992 if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
5993 newheader->attributes |= RDATASET_ATTR_RESIGN;
5994 newheader->resign = rdataset->resign;
5996 newheader->resign = 0;
5998 newheader->serial = 1;
5999 newheader->resign = 0;
6000 if ((rdataset->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
6001 newheader->attributes |= RDATASET_ATTR_NXDOMAIN;
6002 if ((rdataset->attributes & DNS_RDATASETATTR_OPTOUT) != 0)
6003 newheader->attributes |= RDATASET_ATTR_OPTOUT;
6004 if ((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0) {
6005 result = addnoqname(rbtdb, newheader, rdataset);
6006 if (result != ISC_R_SUCCESS) {
6007 free_rdataset(rbtdb, rbtdb->common.mctx,
6012 if ((rdataset->attributes & DNS_RDATASETATTR_CLOSEST) != 0) {
6013 result = addclosest(rbtdb, newheader, rdataset);
6014 if (result != ISC_R_SUCCESS) {
6015 free_rdataset(rbtdb, rbtdb->common.mctx,
6023 * If we're adding a delegation type (e.g. NS or DNAME for a zone,
6024 * just DNAME for the cache), then we need to set the callback bit
6027 if (delegating_type(rbtdb, rbtnode, rdataset->type))
6028 delegating = ISC_TRUE;
6030 delegating = ISC_FALSE;
6033 * If we're adding a delegation type or the DB is a cache in an overmem
6034 * state, hold an exclusive lock on the tree. In the latter case
6035 * the lock does not necessarily have to be acquired but it will help
6036 * purge stale entries more effectively.
6038 if (IS_CACHE(rbtdb) && isc_mem_isovermem(rbtdb->common.mctx))
6039 cache_is_overmem = ISC_TRUE;
6040 if (delegating || cache_is_overmem) {
6041 tree_locked = ISC_TRUE;
6042 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
6045 if (cache_is_overmem)
6046 overmem_purge(rbtdb, rbtnode->locknum, now, tree_locked);
6048 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6049 isc_rwlocktype_write);
6051 if (rbtdb->rrsetstats != NULL) {
6052 newheader->attributes |= RDATASET_ATTR_STATCOUNT;
6053 update_rrsetstats(rbtdb, newheader, ISC_TRUE);
6056 if (IS_CACHE(rbtdb)) {
6058 cleanup_dead_nodes(rbtdb, rbtnode->locknum);
6060 header = isc_heap_element(rbtdb->heaps[rbtnode->locknum], 1);
6061 if (header && header->rdh_ttl <= now - RBTDB_VIRTUAL)
6062 expire_header(rbtdb, header, tree_locked);
6065 * If we've been holding a write lock on the tree just for
6066 * cleaning, we can release it now. However, we still need the
6069 if (tree_locked && !delegating) {
6070 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
6071 tree_locked = ISC_FALSE;
6075 result = add(rbtdb, rbtnode, rbtversion, newheader, options, ISC_FALSE,
6076 addedrdataset, now);
6077 if (result == ISC_R_SUCCESS && delegating)
6078 rbtnode->find_callback = 1;
6080 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6081 isc_rwlocktype_write);
6084 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
6087 * Update the zone's secure status. If version is non-NULL
6088 * this is deferred until closeversion() is called.
6090 if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
6091 iszonesecure(db, version, rbtdb->origin_node);
6097 subtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
6098 dns_rdataset_t *rdataset, unsigned int options,
6099 dns_rdataset_t *newrdataset)
6101 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6102 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
6103 rbtdb_version_t *rbtversion = version;
6104 rdatasetheader_t *topheader, *topheader_prev, *header, *newheader;
6105 unsigned char *subresult;
6106 isc_region_t region;
6107 isc_result_t result;
6108 rbtdb_changed_t *changed;
6110 REQUIRE(VALID_RBTDB(rbtdb));
6112 if (rbtdb->common.methods == &zone_methods)
6113 REQUIRE(((rbtnode->nsec3 &&
6114 (rdataset->type == dns_rdatatype_nsec3 ||
6115 rdataset->covers == dns_rdatatype_nsec3)) ||
6117 rdataset->type != dns_rdatatype_nsec3 &&
6118 rdataset->covers != dns_rdatatype_nsec3)));
6120 result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
6122 sizeof(rdatasetheader_t));
6123 if (result != ISC_R_SUCCESS)
6125 newheader = (rdatasetheader_t *)region.base;
6126 init_rdataset(rbtdb, newheader);
6127 set_ttl(rbtdb, newheader, rdataset->ttl);
6128 newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
6130 newheader->attributes = 0;
6131 newheader->serial = rbtversion->serial;
6132 newheader->trust = 0;
6133 newheader->noqname = NULL;
6134 newheader->closest = NULL;
6135 newheader->count = init_count++;
6136 newheader->additional_auth = NULL;
6137 newheader->additional_glue = NULL;
6138 newheader->last_used = 0;
6139 newheader->node = rbtnode;
6140 if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
6141 newheader->attributes |= RDATASET_ATTR_RESIGN;
6142 newheader->resign = rdataset->resign;
6144 newheader->resign = 0;
6146 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6147 isc_rwlocktype_write);
6149 changed = add_changed(rbtdb, rbtversion, rbtnode);
6150 if (changed == NULL) {
6151 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6152 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6153 isc_rwlocktype_write);
6154 return (ISC_R_NOMEMORY);
6157 topheader_prev = NULL;
6158 for (topheader = rbtnode->data;
6160 topheader = topheader->next) {
6161 if (topheader->type == newheader->type)
6163 topheader_prev = topheader;
6166 * If header isn't NULL, we've found the right type. There may be
6167 * IGNORE rdatasets between the top of the chain and the first real
6168 * data. We skip over them.
6171 while (header != NULL && IGNORE(header))
6172 header = header->down;
6173 if (header != NULL && EXISTS(header)) {
6174 unsigned int flags = 0;
6176 result = ISC_R_SUCCESS;
6177 if ((options & DNS_DBSUB_EXACT) != 0) {
6178 flags |= DNS_RDATASLAB_EXACT;
6179 if (newheader->rdh_ttl != header->rdh_ttl)
6180 result = DNS_R_NOTEXACT;
6182 if (result == ISC_R_SUCCESS)
6183 result = dns_rdataslab_subtract(
6184 (unsigned char *)header,
6185 (unsigned char *)newheader,
6186 (unsigned int)(sizeof(*newheader)),
6188 rbtdb->common.rdclass,
6189 (dns_rdatatype_t)header->type,
6191 if (result == ISC_R_SUCCESS) {
6192 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6193 newheader = (rdatasetheader_t *)subresult;
6194 init_rdataset(rbtdb, newheader);
6196 * We have to set the serial since the rdataslab
6197 * subtraction routine copies the reserved portion of
6198 * header, not newheader.
6200 newheader->serial = rbtversion->serial;
6202 * XXXJT: dns_rdataslab_subtract() copied the pointers
6203 * to additional info. We need to clear these fields
6204 * to avoid having duplicated references.
6206 newheader->additional_auth = NULL;
6207 newheader->additional_glue = NULL;
6208 } else if (result == DNS_R_NXRRSET) {
6210 * This subtraction would remove all of the rdata;
6211 * add a nonexistent header instead.
6213 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6214 newheader = new_rdataset(rbtdb, rbtdb->common.mctx);
6215 if (newheader == NULL) {
6216 result = ISC_R_NOMEMORY;
6219 set_ttl(rbtdb, newheader, 0);
6220 newheader->type = topheader->type;
6221 newheader->attributes = RDATASET_ATTR_NONEXISTENT;
6222 newheader->trust = 0;
6223 newheader->serial = rbtversion->serial;
6224 newheader->noqname = NULL;
6225 newheader->closest = NULL;
6226 newheader->count = 0;
6227 newheader->additional_auth = NULL;
6228 newheader->additional_glue = NULL;
6229 newheader->node = rbtnode;
6230 newheader->resign = 0;
6231 newheader->last_used = 0;
6233 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6238 * If we're here, we want to link newheader in front of
6241 INSIST(rbtversion->serial >= topheader->serial);
6242 if (topheader_prev != NULL)
6243 topheader_prev->next = newheader;
6245 rbtnode->data = newheader;
6246 newheader->next = topheader->next;
6247 newheader->down = topheader;
6248 topheader->next = newheader;
6250 changed->dirty = ISC_TRUE;
6253 * The rdataset doesn't exist, so we don't need to do anything
6254 * to satisfy the deletion request.
6256 free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
6257 if ((options & DNS_DBSUB_EXACT) != 0)
6258 result = DNS_R_NOTEXACT;
6260 result = DNS_R_UNCHANGED;
6263 if (result == ISC_R_SUCCESS && newrdataset != NULL)
6264 bind_rdataset(rbtdb, rbtnode, newheader, 0, newrdataset);
6267 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6268 isc_rwlocktype_write);
6271 * Update the zone's secure status. If version is non-NULL
6272 * this is deferred until closeversion() is called.
6274 if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
6275 iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
6281 deleterdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
6282 dns_rdatatype_t type, dns_rdatatype_t covers)
6284 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6285 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
6286 rbtdb_version_t *rbtversion = version;
6287 isc_result_t result;
6288 rdatasetheader_t *newheader;
6290 REQUIRE(VALID_RBTDB(rbtdb));
6292 if (type == dns_rdatatype_any)
6293 return (ISC_R_NOTIMPLEMENTED);
6294 if (type == dns_rdatatype_rrsig && covers == 0)
6295 return (ISC_R_NOTIMPLEMENTED);
6297 newheader = new_rdataset(rbtdb, rbtdb->common.mctx);
6298 if (newheader == NULL)
6299 return (ISC_R_NOMEMORY);
6300 set_ttl(rbtdb, newheader, 0);
6301 newheader->type = RBTDB_RDATATYPE_VALUE(type, covers);
6302 newheader->attributes = RDATASET_ATTR_NONEXISTENT;
6303 newheader->trust = 0;
6304 newheader->noqname = NULL;
6305 newheader->closest = NULL;
6306 newheader->additional_auth = NULL;
6307 newheader->additional_glue = NULL;
6308 if (rbtversion != NULL)
6309 newheader->serial = rbtversion->serial;
6311 newheader->serial = 0;
6312 newheader->count = 0;
6313 newheader->last_used = 0;
6314 newheader->node = rbtnode;
6316 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6317 isc_rwlocktype_write);
6319 result = add(rbtdb, rbtnode, rbtversion, newheader, DNS_DBADD_FORCE,
6320 ISC_FALSE, NULL, 0);
6322 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
6323 isc_rwlocktype_write);
6326 * Update the zone's secure status. If version is non-NULL
6327 * this is deferred until closeversion() is called.
6329 if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
6330 iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
6336 loading_addrdataset(void *arg, dns_name_t *name, dns_rdataset_t *rdataset) {
6337 rbtdb_load_t *loadctx = arg;
6338 dns_rbtdb_t *rbtdb = loadctx->rbtdb;
6339 dns_rbtnode_t *node;
6340 isc_result_t result;
6341 isc_region_t region;
6342 rdatasetheader_t *newheader;
6345 * This routine does no node locking. See comments in
6346 * 'load' below for more information on loading and
6352 * SOA records are only allowed at top of zone.
6354 if (rdataset->type == dns_rdatatype_soa &&
6355 !IS_CACHE(rbtdb) && !dns_name_equal(name, &rbtdb->common.origin))
6356 return (DNS_R_NOTZONETOP);
6358 if (rdataset->type != dns_rdatatype_nsec3 &&
6359 rdataset->covers != dns_rdatatype_nsec3)
6360 add_empty_wildcards(rbtdb, name);
6362 if (dns_name_iswildcard(name)) {
6364 * NS record owners cannot legally be wild cards.
6366 if (rdataset->type == dns_rdatatype_ns)
6367 return (DNS_R_INVALIDNS);
6369 * NSEC3 record owners cannot legally be wild cards.
6371 if (rdataset->type == dns_rdatatype_nsec3)
6372 return (DNS_R_INVALIDNSEC3);
6373 result = add_wildcard_magic(rbtdb, name);
6374 if (result != ISC_R_SUCCESS)
6379 if (rdataset->type == dns_rdatatype_nsec3 ||
6380 rdataset->covers == dns_rdatatype_nsec3) {
6381 result = dns_rbt_addnode(rbtdb->nsec3, name, &node);
6382 if (result == ISC_R_SUCCESS)
6385 result = dns_rbt_addnode(rbtdb->tree, name, &node);
6386 if (result == ISC_R_SUCCESS)
6389 if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS)
6391 if (result != ISC_R_EXISTS) {
6392 dns_name_t foundname;
6393 dns_name_init(&foundname, NULL);
6394 dns_rbt_namefromnode(node, &foundname);
6395 #ifdef DNS_RBT_USEHASH
6396 node->locknum = node->hashval % rbtdb->node_lock_count;
6398 node->locknum = dns_name_hash(&foundname, ISC_TRUE) %
6399 rbtdb->node_lock_count;
6403 result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
6405 sizeof(rdatasetheader_t));
6406 if (result != ISC_R_SUCCESS)
6408 newheader = (rdatasetheader_t *)region.base;
6409 init_rdataset(rbtdb, newheader);
6410 set_ttl(rbtdb, newheader,
6411 rdataset->ttl + loadctx->now); /* XXX overflow check */
6412 newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
6414 newheader->attributes = 0;
6415 newheader->trust = rdataset->trust;
6416 newheader->serial = 1;
6417 newheader->noqname = NULL;
6418 newheader->closest = NULL;
6419 newheader->count = init_count++;
6420 newheader->additional_auth = NULL;
6421 newheader->additional_glue = NULL;
6422 newheader->last_used = 0;
6423 newheader->node = node;
6424 if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
6425 newheader->attributes |= RDATASET_ATTR_RESIGN;
6426 newheader->resign = rdataset->resign;
6428 newheader->resign = 0;
6430 result = add(rbtdb, node, rbtdb->current_version, newheader,
6431 DNS_DBADD_MERGE, ISC_TRUE, NULL, 0);
6432 if (result == ISC_R_SUCCESS &&
6433 delegating_type(rbtdb, node, rdataset->type))
6434 node->find_callback = 1;
6435 else if (result == DNS_R_UNCHANGED)
6436 result = ISC_R_SUCCESS;
6442 beginload(dns_db_t *db, dns_addrdatasetfunc_t *addp, dns_dbload_t **dbloadp) {
6443 rbtdb_load_t *loadctx;
6446 rbtdb = (dns_rbtdb_t *)db;
6448 REQUIRE(VALID_RBTDB(rbtdb));
6450 loadctx = isc_mem_get(rbtdb->common.mctx, sizeof(*loadctx));
6451 if (loadctx == NULL)
6452 return (ISC_R_NOMEMORY);
6454 loadctx->rbtdb = rbtdb;
6455 if (IS_CACHE(rbtdb))
6456 isc_stdtime_get(&loadctx->now);
6460 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
6462 REQUIRE((rbtdb->attributes & (RBTDB_ATTR_LOADED|RBTDB_ATTR_LOADING))
6464 rbtdb->attributes |= RBTDB_ATTR_LOADING;
6466 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
6468 *addp = loading_addrdataset;
6471 return (ISC_R_SUCCESS);
6475 endload(dns_db_t *db, dns_dbload_t **dbloadp) {
6476 rbtdb_load_t *loadctx;
6477 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6479 REQUIRE(VALID_RBTDB(rbtdb));
6480 REQUIRE(dbloadp != NULL);
6482 REQUIRE(loadctx->rbtdb == rbtdb);
6484 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
6486 REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADING) != 0);
6487 REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADED) == 0);
6489 rbtdb->attributes &= ~RBTDB_ATTR_LOADING;
6490 rbtdb->attributes |= RBTDB_ATTR_LOADED;
6492 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
6495 * If there's a KEY rdataset at the zone origin containing a
6496 * zone key, we consider the zone secure.
6498 if (! IS_CACHE(rbtdb))
6499 iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
6503 isc_mem_put(rbtdb->common.mctx, loadctx, sizeof(*loadctx));
6505 return (ISC_R_SUCCESS);
6509 dump(dns_db_t *db, dns_dbversion_t *version, const char *filename,
6510 dns_masterformat_t masterformat) {
6513 rbtdb = (dns_rbtdb_t *)db;
6515 REQUIRE(VALID_RBTDB(rbtdb));
6517 return (dns_master_dump2(rbtdb->common.mctx, db, version,
6518 &dns_master_style_default,
6519 filename, masterformat));
6523 delete_callback(void *data, void *arg) {
6524 dns_rbtdb_t *rbtdb = arg;
6525 rdatasetheader_t *current, *next;
6526 unsigned int locknum;
6529 locknum = current->node->locknum;
6530 NODE_LOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
6531 while (current != NULL) {
6532 next = current->next;
6533 free_rdataset(rbtdb, rbtdb->common.mctx, current);
6536 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
6539 static isc_boolean_t
6540 issecure(dns_db_t *db) {
6542 isc_boolean_t secure;
6544 rbtdb = (dns_rbtdb_t *)db;
6546 REQUIRE(VALID_RBTDB(rbtdb));
6548 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6549 secure = ISC_TF(rbtdb->current_version->secure == dns_db_secure);
6550 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6555 static isc_boolean_t
6556 isdnssec(dns_db_t *db) {
6558 isc_boolean_t dnssec;
6560 rbtdb = (dns_rbtdb_t *)db;
6562 REQUIRE(VALID_RBTDB(rbtdb));
6564 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6565 dnssec = ISC_TF(rbtdb->current_version->secure != dns_db_insecure);
6566 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6572 nodecount(dns_db_t *db) {
6576 rbtdb = (dns_rbtdb_t *)db;
6578 REQUIRE(VALID_RBTDB(rbtdb));
6580 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6581 count = dns_rbt_nodecount(rbtdb->tree);
6582 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6588 settask(dns_db_t *db, isc_task_t *task) {
6591 rbtdb = (dns_rbtdb_t *)db;
6593 REQUIRE(VALID_RBTDB(rbtdb));
6595 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
6596 if (rbtdb->task != NULL)
6597 isc_task_detach(&rbtdb->task);
6599 isc_task_attach(task, &rbtdb->task);
6600 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
6603 static isc_boolean_t
6604 ispersistent(dns_db_t *db) {
6610 getoriginnode(dns_db_t *db, dns_dbnode_t **nodep) {
6611 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6612 dns_rbtnode_t *onode;
6613 isc_result_t result = ISC_R_SUCCESS;
6615 REQUIRE(VALID_RBTDB(rbtdb));
6616 REQUIRE(nodep != NULL && *nodep == NULL);
6618 /* Note that the access to origin_node doesn't require a DB lock */
6619 onode = (dns_rbtnode_t *)rbtdb->origin_node;
6620 if (onode != NULL) {
6621 NODE_STRONGLOCK(&rbtdb->node_locks[onode->locknum].lock);
6622 new_reference(rbtdb, onode);
6623 NODE_STRONGUNLOCK(&rbtdb->node_locks[onode->locknum].lock);
6625 *nodep = rbtdb->origin_node;
6627 INSIST(IS_CACHE(rbtdb));
6628 result = ISC_R_NOTFOUND;
6635 getnsec3parameters(dns_db_t *db, dns_dbversion_t *version, dns_hash_t *hash,
6636 isc_uint8_t *flags, isc_uint16_t *iterations,
6637 unsigned char *salt, size_t *salt_length)
6640 isc_result_t result = ISC_R_NOTFOUND;
6641 rbtdb_version_t *rbtversion = version;
6643 rbtdb = (dns_rbtdb_t *)db;
6645 REQUIRE(VALID_RBTDB(rbtdb));
6647 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6649 if (rbtversion == NULL)
6650 rbtversion = rbtdb->current_version;
6652 if (rbtversion->havensec3) {
6654 *hash = rbtversion->hash;
6655 if (salt != NULL && salt_length != NULL) {
6656 REQUIRE(*salt_length >= rbtversion->salt_length);
6657 memcpy(salt, rbtversion->salt, rbtversion->salt_length);
6659 if (salt_length != NULL)
6660 *salt_length = rbtversion->salt_length;
6661 if (iterations != NULL)
6662 *iterations = rbtversion->iterations;
6664 *flags = rbtversion->flags;
6665 result = ISC_R_SUCCESS;
6667 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
6673 setsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, isc_stdtime_t resign) {
6674 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6675 isc_stdtime_t oldresign;
6676 isc_result_t result = ISC_R_SUCCESS;
6677 rdatasetheader_t *header;
6679 REQUIRE(VALID_RBTDB(rbtdb));
6680 REQUIRE(!IS_CACHE(rbtdb));
6681 REQUIRE(rdataset != NULL);
6683 header = rdataset->private3;
6686 NODE_LOCK(&rbtdb->node_locks[header->node->locknum].lock,
6687 isc_rwlocktype_write);
6689 oldresign = header->resign;
6690 header->resign = resign;
6691 if (header->heap_index != 0) {
6692 INSIST(RESIGN(header));
6694 isc_heap_delete(rbtdb->heaps[header->node->locknum],
6695 header->heap_index);
6696 header->heap_index = 0;
6697 } else if (resign < oldresign)
6698 isc_heap_increased(rbtdb->heaps[header->node->locknum],
6699 header->heap_index);
6701 isc_heap_decreased(rbtdb->heaps[header->node->locknum],
6702 header->heap_index);
6703 } else if (resign && header->heap_index == 0) {
6704 header->attributes |= RDATASET_ATTR_RESIGN;
6705 result = resign_insert(rbtdb, header->node->locknum, header);
6707 NODE_UNLOCK(&rbtdb->node_locks[header->node->locknum].lock,
6708 isc_rwlocktype_write);
6713 getsigningtime(dns_db_t *db, dns_rdataset_t *rdataset,
6714 dns_name_t *foundname)
6716 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6717 rdatasetheader_t *header = NULL, *this;
6719 isc_result_t result = ISC_R_NOTFOUND;
6720 unsigned int locknum;
6722 REQUIRE(VALID_RBTDB(rbtdb));
6724 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
6726 for (i = 0; i < rbtdb->node_lock_count; i++) {
6727 NODE_LOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_read);
6728 this = isc_heap_element(rbtdb->heaps[i], 1);
6730 NODE_UNLOCK(&rbtdb->node_locks[i].lock,
6731 isc_rwlocktype_read);
6736 else if (isc_serial_lt(this->resign, header->resign)) {
6737 locknum = header->node->locknum;
6738 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
6739 isc_rwlocktype_read);
6742 NODE_UNLOCK(&rbtdb->node_locks[i].lock,
6743 isc_rwlocktype_read);
6749 bind_rdataset(rbtdb, header->node, header, 0, rdataset);
6751 if (foundname != NULL)
6752 dns_rbt_fullnamefromnode(header->node, foundname);
6754 NODE_UNLOCK(&rbtdb->node_locks[header->node->locknum].lock,
6755 isc_rwlocktype_read);
6757 result = ISC_R_SUCCESS;
6760 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
6766 resigned(dns_db_t *db, dns_rdataset_t *rdataset, dns_dbversion_t *version)
6768 rbtdb_version_t *rbtversion = (rbtdb_version_t *)version;
6769 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6770 dns_rbtnode_t *node;
6771 rdatasetheader_t *header;
6773 REQUIRE(VALID_RBTDB(rbtdb));
6774 REQUIRE(rdataset != NULL);
6775 REQUIRE(rbtdb->future_version == rbtversion);
6776 REQUIRE(rbtversion->writer);
6778 node = rdataset->private2;
6779 header = rdataset->private3;
6782 RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
6783 NODE_LOCK(&rbtdb->node_locks[node->locknum].lock,
6784 isc_rwlocktype_write);
6786 * Delete from heap and save to re-signed list so that it can
6787 * be restored if we backout of this change.
6789 new_reference(rbtdb, node);
6790 isc_heap_delete(rbtdb->heaps[node->locknum], header->heap_index);
6791 header->heap_index = 0;
6792 ISC_LIST_APPEND(rbtversion->resigned_list, header, link);
6794 NODE_UNLOCK(&rbtdb->node_locks[node->locknum].lock,
6795 isc_rwlocktype_write);
6796 RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
6799 static dns_stats_t *
6800 getrrsetstats(dns_db_t *db) {
6801 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
6803 REQUIRE(VALID_RBTDB(rbtdb));
6804 REQUIRE(IS_CACHE(rbtdb)); /* current restriction */
6806 return (rbtdb->rrsetstats);
6809 static dns_dbmethods_t zone_methods = {
6848 static dns_dbmethods_t cache_methods = {
6888 #ifdef DNS_RBTDB_VERSION64
6893 (isc_mem_t *mctx, dns_name_t *origin, dns_dbtype_t type,
6894 dns_rdataclass_t rdclass, unsigned int argc, char *argv[],
6895 void *driverarg, dns_db_t **dbp)
6898 isc_result_t result;
6901 isc_boolean_t (*sooner)(void *, void *);
6903 /* Keep the compiler happy. */
6908 rbtdb = isc_mem_get(mctx, sizeof(*rbtdb));
6910 return (ISC_R_NOMEMORY);
6912 memset(rbtdb, '\0', sizeof(*rbtdb));
6913 dns_name_init(&rbtdb->common.origin, NULL);
6914 rbtdb->common.attributes = 0;
6915 if (type == dns_dbtype_cache) {
6916 rbtdb->common.methods = &cache_methods;
6917 rbtdb->common.attributes |= DNS_DBATTR_CACHE;
6918 } else if (type == dns_dbtype_stub) {
6919 rbtdb->common.methods = &zone_methods;
6920 rbtdb->common.attributes |= DNS_DBATTR_STUB;
6922 rbtdb->common.methods = &zone_methods;
6923 rbtdb->common.rdclass = rdclass;
6924 rbtdb->common.mctx = NULL;
6926 result = RBTDB_INITLOCK(&rbtdb->lock);
6927 if (result != ISC_R_SUCCESS)
6930 result = isc_rwlock_init(&rbtdb->tree_lock, 0, 0);
6931 if (result != ISC_R_SUCCESS)
6935 * Initialize node_lock_count in a generic way to support future
6936 * extension which allows the user to specify this value on creation.
6937 * Note that when specified for a cache DB it must be larger than 1
6938 * as commented with the definition of DEFAULT_CACHE_NODE_LOCK_COUNT.
6940 if (rbtdb->node_lock_count == 0) {
6941 if (IS_CACHE(rbtdb))
6942 rbtdb->node_lock_count = DEFAULT_CACHE_NODE_LOCK_COUNT;
6944 rbtdb->node_lock_count = DEFAULT_NODE_LOCK_COUNT;
6945 } else if (rbtdb->node_lock_count < 2 && IS_CACHE(rbtdb)) {
6946 result = ISC_R_RANGE;
6947 goto cleanup_tree_lock;
6949 INSIST(rbtdb->node_lock_count < (1 << DNS_RBT_LOCKLENGTH));
6950 rbtdb->node_locks = isc_mem_get(mctx, rbtdb->node_lock_count *
6951 sizeof(rbtdb_nodelock_t));
6952 if (rbtdb->node_locks == NULL) {
6953 result = ISC_R_NOMEMORY;
6954 goto cleanup_tree_lock;
6957 rbtdb->rrsetstats = NULL;
6958 if (IS_CACHE(rbtdb)) {
6959 result = dns_rdatasetstats_create(mctx, &rbtdb->rrsetstats);
6960 if (result != ISC_R_SUCCESS)
6961 goto cleanup_node_locks;
6962 rbtdb->rdatasets = isc_mem_get(mctx, rbtdb->node_lock_count *
6963 sizeof(rdatasetheaderlist_t));
6964 if (rbtdb->rdatasets == NULL) {
6965 result = ISC_R_NOMEMORY;
6966 goto cleanup_rrsetstats;
6968 for (i = 0; i < (int)rbtdb->node_lock_count; i++)
6969 ISC_LIST_INIT(rbtdb->rdatasets[i]);
6971 rbtdb->rdatasets = NULL;
6976 rbtdb->heaps = isc_mem_get(mctx, rbtdb->node_lock_count *
6977 sizeof(isc_heap_t *));
6978 if (rbtdb->heaps == NULL) {
6979 result = ISC_R_NOMEMORY;
6980 goto cleanup_rdatasets;
6982 for (i = 0; i < (int)rbtdb->node_lock_count; i++)
6983 rbtdb->heaps[i] = NULL;
6984 sooner = IS_CACHE(rbtdb) ? ttl_sooner : resign_sooner;
6985 for (i = 0; i < (int)rbtdb->node_lock_count; i++) {
6986 result = isc_heap_create(mctx, sooner, set_index, 0,
6988 if (result != ISC_R_SUCCESS)
6993 * Create deadnode lists.
6995 rbtdb->deadnodes = isc_mem_get(mctx, rbtdb->node_lock_count *
6996 sizeof(rbtnodelist_t));
6997 if (rbtdb->deadnodes == NULL) {
6998 result = ISC_R_NOMEMORY;
7001 for (i = 0; i < (int)rbtdb->node_lock_count; i++)
7002 ISC_LIST_INIT(rbtdb->deadnodes[i]);
7004 rbtdb->active = rbtdb->node_lock_count;
7006 for (i = 0; i < (int)(rbtdb->node_lock_count); i++) {
7007 result = NODE_INITLOCK(&rbtdb->node_locks[i].lock);
7008 if (result == ISC_R_SUCCESS) {
7009 result = isc_refcount_init(&rbtdb->node_locks[i].references, 0);
7010 if (result != ISC_R_SUCCESS)
7011 NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
7013 if (result != ISC_R_SUCCESS) {
7015 NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
7016 isc_refcount_decrement(&rbtdb->node_locks[i].references, NULL);
7017 isc_refcount_destroy(&rbtdb->node_locks[i].references);
7019 goto cleanup_deadnodes;
7021 rbtdb->node_locks[i].exiting = ISC_FALSE;
7025 * Attach to the mctx. The database will persist so long as there
7026 * are references to it, and attaching to the mctx ensures that our
7027 * mctx won't disappear out from under us.
7029 isc_mem_attach(mctx, &rbtdb->common.mctx);
7032 * Must be initialized before free_rbtdb() is called.
7034 isc_ondestroy_init(&rbtdb->common.ondest);
7037 * Make a copy of the origin name.
7039 result = dns_name_dupwithoffsets(origin, mctx, &rbtdb->common.origin);
7040 if (result != ISC_R_SUCCESS) {
7041 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7046 * Make the Red-Black Trees.
7048 result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->tree);
7049 if (result != ISC_R_SUCCESS) {
7050 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7054 result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->nsec3);
7055 if (result != ISC_R_SUCCESS) {
7056 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7061 * In order to set the node callback bit correctly in zone databases,
7062 * we need to know if the node has the origin name of the zone.
7063 * In loading_addrdataset() we could simply compare the new name
7064 * to the origin name, but this is expensive. Also, we don't know the
7065 * node name in addrdataset(), so we need another way of knowing the
7068 * We now explicitly create a node for the zone's origin, and then
7069 * we simply remember the node's address. This is safe, because
7070 * the top-of-zone node can never be deleted, nor can its address
7073 if (!IS_CACHE(rbtdb)) {
7074 rbtdb->origin_node = NULL;
7075 result = dns_rbt_addnode(rbtdb->tree, &rbtdb->common.origin,
7076 &rbtdb->origin_node);
7077 if (result != ISC_R_SUCCESS) {
7078 INSIST(result != ISC_R_EXISTS);
7079 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7082 rbtdb->origin_node->nsec3 = 0;
7084 * We need to give the origin node the right locknum.
7086 dns_name_init(&name, NULL);
7087 dns_rbt_namefromnode(rbtdb->origin_node, &name);
7088 #ifdef DNS_RBT_USEHASH
7089 rbtdb->origin_node->locknum =
7090 rbtdb->origin_node->hashval %
7091 rbtdb->node_lock_count;
7093 rbtdb->origin_node->locknum =
7094 dns_name_hash(&name, ISC_TRUE) %
7095 rbtdb->node_lock_count;
7100 * Misc. Initialization.
7102 result = isc_refcount_init(&rbtdb->references, 1);
7103 if (result != ISC_R_SUCCESS) {
7104 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7107 rbtdb->attributes = 0;
7111 * Version Initialization.
7113 rbtdb->current_serial = 1;
7114 rbtdb->least_serial = 1;
7115 rbtdb->next_serial = 2;
7116 rbtdb->current_version = allocate_version(mctx, 1, 1, ISC_FALSE);
7117 if (rbtdb->current_version == NULL) {
7118 isc_refcount_decrement(&rbtdb->references, NULL);
7119 isc_refcount_destroy(&rbtdb->references);
7120 free_rbtdb(rbtdb, ISC_FALSE, NULL);
7121 return (ISC_R_NOMEMORY);
7123 rbtdb->current_version->secure = dns_db_insecure;
7124 rbtdb->current_version->havensec3 = ISC_FALSE;
7125 rbtdb->current_version->flags = 0;
7126 rbtdb->current_version->iterations = 0;
7127 rbtdb->current_version->hash = 0;
7128 rbtdb->current_version->salt_length = 0;
7129 memset(rbtdb->current_version->salt, 0,
7130 sizeof(rbtdb->current_version->salt));
7131 rbtdb->future_version = NULL;
7132 ISC_LIST_INIT(rbtdb->open_versions);
7134 * Keep the current version in the open list so that list operation
7135 * won't happen in normal lookup operations.
7137 PREPEND(rbtdb->open_versions, rbtdb->current_version, link);
7139 rbtdb->common.magic = DNS_DB_MAGIC;
7140 rbtdb->common.impmagic = RBTDB_MAGIC;
7142 *dbp = (dns_db_t *)rbtdb;
7144 return (ISC_R_SUCCESS);
7147 isc_mem_put(mctx, rbtdb->deadnodes,
7148 rbtdb->node_lock_count * sizeof(rbtnodelist_t));
7151 if (rbtdb->heaps != NULL) {
7152 for (i = 0 ; i < (int)rbtdb->node_lock_count ; i++)
7153 if (rbtdb->heaps[i] != NULL)
7154 isc_heap_destroy(&rbtdb->heaps[i]);
7155 isc_mem_put(mctx, rbtdb->heaps,
7156 rbtdb->node_lock_count * sizeof(isc_heap_t *));
7160 if (rbtdb->rdatasets != NULL)
7161 isc_mem_put(mctx, rbtdb->rdatasets, rbtdb->node_lock_count *
7162 sizeof(rdatasetheaderlist_t));
7164 if (rbtdb->rrsetstats != NULL)
7165 dns_stats_detach(&rbtdb->rrsetstats);
7168 isc_mem_put(mctx, rbtdb->node_locks,
7169 rbtdb->node_lock_count * sizeof(rbtdb_nodelock_t));
7172 isc_rwlock_destroy(&rbtdb->tree_lock);
7175 RBTDB_DESTROYLOCK(&rbtdb->lock);
7178 isc_mem_put(mctx, rbtdb, sizeof(*rbtdb));
7184 * Slabbed Rdataset Methods
7188 rdataset_disassociate(dns_rdataset_t *rdataset) {
7189 dns_db_t *db = rdataset->private1;
7190 dns_dbnode_t *node = rdataset->private2;
7192 detachnode(db, &node);
7196 rdataset_first(dns_rdataset_t *rdataset) {
7197 unsigned char *raw = rdataset->private3; /* RDATASLAB */
7200 count = raw[0] * 256 + raw[1];
7202 rdataset->private5 = NULL;
7203 return (ISC_R_NOMORE);
7206 #if DNS_RDATASET_FIXED
7207 if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) == 0)
7208 raw += 2 + (4 * count);
7214 * The privateuint4 field is the number of rdata beyond the
7215 * cursor position, so we decrement the total count by one
7216 * before storing it.
7218 * If DNS_RDATASETATTR_LOADORDER is not set 'raw' points to the
7219 * first record. If DNS_RDATASETATTR_LOADORDER is set 'raw' points
7220 * to the first entry in the offset table.
7223 rdataset->privateuint4 = count;
7224 rdataset->private5 = raw;
7226 return (ISC_R_SUCCESS);
7230 rdataset_next(dns_rdataset_t *rdataset) {
7232 unsigned int length;
7233 unsigned char *raw; /* RDATASLAB */
7235 count = rdataset->privateuint4;
7237 return (ISC_R_NOMORE);
7239 rdataset->privateuint4 = count;
7242 * Skip forward one record (length + 4) or one offset (4).
7244 raw = rdataset->private5;
7245 #if DNS_RDATASET_FIXED
7246 if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) == 0) {
7248 length = raw[0] * 256 + raw[1];
7250 #if DNS_RDATASET_FIXED
7252 rdataset->private5 = raw + 4; /* length(2) + order(2) */
7254 rdataset->private5 = raw + 2; /* length(2) */
7257 return (ISC_R_SUCCESS);
7261 rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
7262 unsigned char *raw = rdataset->private5; /* RDATASLAB */
7263 #if DNS_RDATASET_FIXED
7264 unsigned int offset;
7266 unsigned int length;
7268 unsigned int flags = 0;
7270 REQUIRE(raw != NULL);
7273 * Find the start of the record if not already in private5
7274 * then skip the length and order fields.
7276 #if DNS_RDATASET_FIXED
7277 if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) != 0) {
7278 offset = (raw[0] << 24) + (raw[1] << 16) +
7279 (raw[2] << 8) + raw[3];
7280 raw = rdataset->private3;
7284 length = raw[0] * 256 + raw[1];
7285 #if DNS_RDATASET_FIXED
7290 if (rdataset->type == dns_rdatatype_rrsig) {
7291 if (*raw & DNS_RDATASLAB_OFFLINE)
7292 flags |= DNS_RDATA_OFFLINE;
7298 dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
7299 rdata->flags |= flags;
7303 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
7304 dns_db_t *db = source->private1;
7305 dns_dbnode_t *node = source->private2;
7306 dns_dbnode_t *cloned_node = NULL;
7308 attachnode(db, node, &cloned_node);
7312 * Reset iterator state.
7314 target->privateuint4 = 0;
7315 target->private5 = NULL;
7319 rdataset_count(dns_rdataset_t *rdataset) {
7320 unsigned char *raw = rdataset->private3; /* RDATASLAB */
7323 count = raw[0] * 256 + raw[1];
7329 rdataset_getnoqname(dns_rdataset_t *rdataset, dns_name_t *name,
7330 dns_rdataset_t *nsec, dns_rdataset_t *nsecsig)
7332 dns_db_t *db = rdataset->private1;
7333 dns_dbnode_t *node = rdataset->private2;
7334 dns_dbnode_t *cloned_node;
7335 struct noqname *noqname = rdataset->private6;
7338 attachnode(db, node, &cloned_node);
7339 nsec->methods = &rdataset_methods;
7340 nsec->rdclass = db->rdclass;
7341 nsec->type = noqname->type;
7343 nsec->ttl = rdataset->ttl;
7344 nsec->trust = rdataset->trust;
7345 nsec->private1 = rdataset->private1;
7346 nsec->private2 = rdataset->private2;
7347 nsec->private3 = noqname->neg;
7348 nsec->privateuint4 = 0;
7349 nsec->private5 = NULL;
7350 nsec->private6 = NULL;
7351 nsec->private7 = NULL;
7354 attachnode(db, node, &cloned_node);
7355 nsecsig->methods = &rdataset_methods;
7356 nsecsig->rdclass = db->rdclass;
7357 nsecsig->type = dns_rdatatype_rrsig;
7358 nsecsig->covers = noqname->type;
7359 nsecsig->ttl = rdataset->ttl;
7360 nsecsig->trust = rdataset->trust;
7361 nsecsig->private1 = rdataset->private1;
7362 nsecsig->private2 = rdataset->private2;
7363 nsecsig->private3 = noqname->negsig;
7364 nsecsig->privateuint4 = 0;
7365 nsecsig->private5 = NULL;
7366 nsec->private6 = NULL;
7367 nsec->private7 = NULL;
7369 dns_name_clone(&noqname->name, name);
7371 return (ISC_R_SUCCESS);
7375 rdataset_getclosest(dns_rdataset_t *rdataset, dns_name_t *name,
7376 dns_rdataset_t *nsec, dns_rdataset_t *nsecsig)
7378 dns_db_t *db = rdataset->private1;
7379 dns_dbnode_t *node = rdataset->private2;
7380 dns_dbnode_t *cloned_node;
7381 struct noqname *closest = rdataset->private7;
7384 attachnode(db, node, &cloned_node);
7385 nsec->methods = &rdataset_methods;
7386 nsec->rdclass = db->rdclass;
7387 nsec->type = closest->type;
7389 nsec->ttl = rdataset->ttl;
7390 nsec->trust = rdataset->trust;
7391 nsec->private1 = rdataset->private1;
7392 nsec->private2 = rdataset->private2;
7393 nsec->private3 = closest->neg;
7394 nsec->privateuint4 = 0;
7395 nsec->private5 = NULL;
7396 nsec->private6 = NULL;
7397 nsec->private7 = NULL;
7400 attachnode(db, node, &cloned_node);
7401 nsecsig->methods = &rdataset_methods;
7402 nsecsig->rdclass = db->rdclass;
7403 nsecsig->type = dns_rdatatype_rrsig;
7404 nsecsig->covers = closest->type;
7405 nsecsig->ttl = rdataset->ttl;
7406 nsecsig->trust = rdataset->trust;
7407 nsecsig->private1 = rdataset->private1;
7408 nsecsig->private2 = rdataset->private2;
7409 nsecsig->private3 = closest->negsig;
7410 nsecsig->privateuint4 = 0;
7411 nsecsig->private5 = NULL;
7412 nsec->private6 = NULL;
7413 nsec->private7 = NULL;
7415 dns_name_clone(&closest->name, name);
7417 return (ISC_R_SUCCESS);
7421 rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust) {
7422 dns_rbtdb_t *rbtdb = rdataset->private1;
7423 dns_rbtnode_t *rbtnode = rdataset->private2;
7424 rdatasetheader_t *header = rdataset->private3;
7427 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7428 isc_rwlocktype_write);
7429 header->trust = rdataset->trust = trust;
7430 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7431 isc_rwlocktype_write);
7435 rdataset_expire(dns_rdataset_t *rdataset) {
7436 dns_rbtdb_t *rbtdb = rdataset->private1;
7437 dns_rbtnode_t *rbtnode = rdataset->private2;
7438 rdatasetheader_t *header = rdataset->private3;
7441 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7442 isc_rwlocktype_write);
7443 expire_header(rbtdb, header, ISC_FALSE);
7444 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7445 isc_rwlocktype_write);
7449 * Rdataset Iterator Methods
7453 rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp) {
7454 rbtdb_rdatasetiter_t *rbtiterator;
7456 rbtiterator = (rbtdb_rdatasetiter_t *)(*iteratorp);
7458 if (rbtiterator->common.version != NULL)
7459 closeversion(rbtiterator->common.db,
7460 &rbtiterator->common.version, ISC_FALSE);
7461 detachnode(rbtiterator->common.db, &rbtiterator->common.node);
7462 isc_mem_put(rbtiterator->common.db->mctx, rbtiterator,
7463 sizeof(*rbtiterator));
7469 rdatasetiter_first(dns_rdatasetiter_t *iterator) {
7470 rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
7471 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
7472 dns_rbtnode_t *rbtnode = rbtiterator->common.node;
7473 rbtdb_version_t *rbtversion = rbtiterator->common.version;
7474 rdatasetheader_t *header, *top_next;
7475 rbtdb_serial_t serial;
7478 if (IS_CACHE(rbtdb)) {
7480 now = rbtiterator->common.now;
7482 serial = rbtversion->serial;
7486 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7487 isc_rwlocktype_read);
7489 for (header = rbtnode->data; header != NULL; header = top_next) {
7490 top_next = header->next;
7492 if (header->serial <= serial && !IGNORE(header)) {
7494 * Is this a "this rdataset doesn't exist"
7495 * record? Or is it too old in the cache?
7497 * Note: unlike everywhere else, we
7498 * check for now > header->rdh_ttl instead
7499 * of now >= header->rdh_ttl. This allows
7500 * ANY and RRSIG queries for 0 TTL
7501 * rdatasets to work.
7503 if (NONEXISTENT(header) ||
7504 (now != 0 && now > header->rdh_ttl))
7508 header = header->down;
7509 } while (header != NULL);
7514 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7515 isc_rwlocktype_read);
7517 rbtiterator->current = header;
7520 return (ISC_R_NOMORE);
7522 return (ISC_R_SUCCESS);
7526 rdatasetiter_next(dns_rdatasetiter_t *iterator) {
7527 rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
7528 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
7529 dns_rbtnode_t *rbtnode = rbtiterator->common.node;
7530 rbtdb_version_t *rbtversion = rbtiterator->common.version;
7531 rdatasetheader_t *header, *top_next;
7532 rbtdb_serial_t serial;
7534 rbtdb_rdatatype_t type, negtype;
7535 dns_rdatatype_t rdtype, covers;
7537 header = rbtiterator->current;
7539 return (ISC_R_NOMORE);
7541 if (IS_CACHE(rbtdb)) {
7543 now = rbtiterator->common.now;
7545 serial = rbtversion->serial;
7549 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7550 isc_rwlocktype_read);
7552 type = header->type;
7553 rdtype = RBTDB_RDATATYPE_BASE(header->type);
7555 covers = RBTDB_RDATATYPE_EXT(header->type);
7556 negtype = RBTDB_RDATATYPE_VALUE(covers, 0);
7558 negtype = RBTDB_RDATATYPE_VALUE(0, rdtype);
7559 for (header = header->next; header != NULL; header = top_next) {
7560 top_next = header->next;
7562 * If not walking back up the down list.
7564 if (header->type != type && header->type != negtype) {
7566 if (header->serial <= serial &&
7569 * Is this a "this rdataset doesn't
7572 * Note: unlike everywhere else, we
7573 * check for now > header->ttl instead
7574 * of now >= header->ttl. This allows
7575 * ANY and RRSIG queries for 0 TTL
7576 * rdatasets to work.
7578 if ((header->attributes &
7579 RDATASET_ATTR_NONEXISTENT) != 0 ||
7580 (now != 0 && now > header->rdh_ttl))
7584 header = header->down;
7585 } while (header != NULL);
7591 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7592 isc_rwlocktype_read);
7594 rbtiterator->current = header;
7597 return (ISC_R_NOMORE);
7599 return (ISC_R_SUCCESS);
7603 rdatasetiter_current(dns_rdatasetiter_t *iterator, dns_rdataset_t *rdataset) {
7604 rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
7605 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
7606 dns_rbtnode_t *rbtnode = rbtiterator->common.node;
7607 rdatasetheader_t *header;
7609 header = rbtiterator->current;
7610 REQUIRE(header != NULL);
7612 NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7613 isc_rwlocktype_read);
7615 bind_rdataset(rbtdb, rbtnode, header, rbtiterator->common.now,
7618 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
7619 isc_rwlocktype_read);
7624 * Database Iterator Methods
7628 reference_iter_node(rbtdb_dbiterator_t *rbtdbiter) {
7629 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7630 dns_rbtnode_t *node = rbtdbiter->node;
7635 INSIST(rbtdbiter->tree_locked != isc_rwlocktype_none);
7636 reactivate_node(rbtdb, node, rbtdbiter->tree_locked);
7640 dereference_iter_node(rbtdb_dbiterator_t *rbtdbiter) {
7641 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7642 dns_rbtnode_t *node = rbtdbiter->node;
7648 lock = &rbtdb->node_locks[node->locknum].lock;
7649 NODE_LOCK(lock, isc_rwlocktype_read);
7650 decrement_reference(rbtdb, node, 0, isc_rwlocktype_read,
7651 rbtdbiter->tree_locked, ISC_FALSE);
7652 NODE_UNLOCK(lock, isc_rwlocktype_read);
7654 rbtdbiter->node = NULL;
7658 flush_deletions(rbtdb_dbiterator_t *rbtdbiter) {
7659 dns_rbtnode_t *node;
7660 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7661 isc_boolean_t was_read_locked = ISC_FALSE;
7665 if (rbtdbiter->delete != 0) {
7667 * Note that "%d node of %d in tree" can report things like
7668 * "flush_deletions: 59 nodes of 41 in tree". This means
7669 * That some nodes appear on the deletions list more than
7670 * once. Only the last occurence will actually be deleted.
7672 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
7673 DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
7674 "flush_deletions: %d nodes of %d in tree",
7676 dns_rbt_nodecount(rbtdb->tree));
7678 if (rbtdbiter->tree_locked == isc_rwlocktype_read) {
7679 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
7680 was_read_locked = ISC_TRUE;
7682 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
7683 rbtdbiter->tree_locked = isc_rwlocktype_write;
7685 for (i = 0; i < rbtdbiter->delete; i++) {
7686 node = rbtdbiter->deletions[i];
7687 lock = &rbtdb->node_locks[node->locknum].lock;
7689 NODE_LOCK(lock, isc_rwlocktype_read);
7690 decrement_reference(rbtdb, node, 0,
7691 isc_rwlocktype_read,
7692 rbtdbiter->tree_locked, ISC_FALSE);
7693 NODE_UNLOCK(lock, isc_rwlocktype_read);
7696 rbtdbiter->delete = 0;
7698 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
7699 if (was_read_locked) {
7700 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
7701 rbtdbiter->tree_locked = isc_rwlocktype_read;
7704 rbtdbiter->tree_locked = isc_rwlocktype_none;
7710 resume_iteration(rbtdb_dbiterator_t *rbtdbiter) {
7711 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7713 REQUIRE(rbtdbiter->paused);
7714 REQUIRE(rbtdbiter->tree_locked == isc_rwlocktype_none);
7716 RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
7717 rbtdbiter->tree_locked = isc_rwlocktype_read;
7719 rbtdbiter->paused = ISC_FALSE;
7723 dbiterator_destroy(dns_dbiterator_t **iteratorp) {
7724 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)(*iteratorp);
7725 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
7726 dns_db_t *db = NULL;
7728 if (rbtdbiter->tree_locked == isc_rwlocktype_read) {
7729 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
7730 rbtdbiter->tree_locked = isc_rwlocktype_none;
7732 INSIST(rbtdbiter->tree_locked == isc_rwlocktype_none);
7734 dereference_iter_node(rbtdbiter);
7736 flush_deletions(rbtdbiter);
7738 dns_db_attach(rbtdbiter->common.db, &db);
7739 dns_db_detach(&rbtdbiter->common.db);
7741 dns_rbtnodechain_reset(&rbtdbiter->chain);
7742 dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
7743 isc_mem_put(db->mctx, rbtdbiter, sizeof(*rbtdbiter));
7750 dbiterator_first(dns_dbiterator_t *iterator) {
7751 isc_result_t result;
7752 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7753 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7754 dns_name_t *name, *origin;
7756 if (rbtdbiter->result != ISC_R_SUCCESS &&
7757 rbtdbiter->result != ISC_R_NOMORE)
7758 return (rbtdbiter->result);
7760 if (rbtdbiter->paused)
7761 resume_iteration(rbtdbiter);
7763 dereference_iter_node(rbtdbiter);
7765 name = dns_fixedname_name(&rbtdbiter->name);
7766 origin = dns_fixedname_name(&rbtdbiter->origin);
7767 dns_rbtnodechain_reset(&rbtdbiter->chain);
7768 dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
7770 if (rbtdbiter->nsec3only) {
7771 rbtdbiter->current = &rbtdbiter->nsec3chain;
7772 result = dns_rbtnodechain_first(rbtdbiter->current,
7773 rbtdb->nsec3, name, origin);
7775 rbtdbiter->current = &rbtdbiter->chain;
7776 result = dns_rbtnodechain_first(rbtdbiter->current,
7777 rbtdb->tree, name, origin);
7778 if (!rbtdbiter->nonsec3 && result == ISC_R_NOTFOUND) {
7779 rbtdbiter->current = &rbtdbiter->nsec3chain;
7780 result = dns_rbtnodechain_first(rbtdbiter->current,
7785 if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
7786 result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
7787 NULL, &rbtdbiter->node);
7788 if (result == ISC_R_SUCCESS) {
7789 rbtdbiter->new_origin = ISC_TRUE;
7790 reference_iter_node(rbtdbiter);
7793 INSIST(result == ISC_R_NOTFOUND);
7794 result = ISC_R_NOMORE; /* The tree is empty. */
7797 rbtdbiter->result = result;
7803 dbiterator_last(dns_dbiterator_t *iterator) {
7804 isc_result_t result;
7805 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7806 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7807 dns_name_t *name, *origin;
7809 if (rbtdbiter->result != ISC_R_SUCCESS &&
7810 rbtdbiter->result != ISC_R_NOMORE)
7811 return (rbtdbiter->result);
7813 if (rbtdbiter->paused)
7814 resume_iteration(rbtdbiter);
7816 dereference_iter_node(rbtdbiter);
7818 name = dns_fixedname_name(&rbtdbiter->name);
7819 origin = dns_fixedname_name(&rbtdbiter->origin);
7820 dns_rbtnodechain_reset(&rbtdbiter->chain);
7821 dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
7823 result = ISC_R_NOTFOUND;
7824 if (rbtdbiter->nsec3only && !rbtdbiter->nonsec3) {
7825 rbtdbiter->current = &rbtdbiter->nsec3chain;
7826 result = dns_rbtnodechain_last(rbtdbiter->current,
7827 rbtdb->nsec3, name, origin);
7829 if (!rbtdbiter->nsec3only && result == ISC_R_NOTFOUND) {
7830 rbtdbiter->current = &rbtdbiter->chain;
7831 result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->tree,
7834 if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
7835 result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
7836 NULL, &rbtdbiter->node);
7837 if (result == ISC_R_SUCCESS) {
7838 rbtdbiter->new_origin = ISC_TRUE;
7839 reference_iter_node(rbtdbiter);
7842 INSIST(result == ISC_R_NOTFOUND);
7843 result = ISC_R_NOMORE; /* The tree is empty. */
7846 rbtdbiter->result = result;
7852 dbiterator_seek(dns_dbiterator_t *iterator, dns_name_t *name) {
7853 isc_result_t result;
7854 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7855 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7856 dns_name_t *iname, *origin;
7858 if (rbtdbiter->result != ISC_R_SUCCESS &&
7859 rbtdbiter->result != ISC_R_NOTFOUND &&
7860 rbtdbiter->result != ISC_R_NOMORE)
7861 return (rbtdbiter->result);
7863 if (rbtdbiter->paused)
7864 resume_iteration(rbtdbiter);
7866 dereference_iter_node(rbtdbiter);
7868 iname = dns_fixedname_name(&rbtdbiter->name);
7869 origin = dns_fixedname_name(&rbtdbiter->origin);
7870 dns_rbtnodechain_reset(&rbtdbiter->chain);
7871 dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
7873 if (rbtdbiter->nsec3only) {
7874 rbtdbiter->current = &rbtdbiter->nsec3chain;
7875 result = dns_rbt_findnode(rbtdb->nsec3, name, NULL,
7878 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
7879 } else if (rbtdbiter->nonsec3) {
7880 rbtdbiter->current = &rbtdbiter->chain;
7881 result = dns_rbt_findnode(rbtdb->tree, name, NULL,
7884 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
7887 * Stay on main chain if not found on either chain.
7889 rbtdbiter->current = &rbtdbiter->chain;
7890 result = dns_rbt_findnode(rbtdb->tree, name, NULL,
7893 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
7894 if (result == DNS_R_PARTIALMATCH) {
7895 dns_rbtnode_t *node = NULL;
7896 result = dns_rbt_findnode(rbtdb->nsec3, name, NULL,
7897 &node, &rbtdbiter->nsec3chain,
7898 DNS_RBTFIND_EMPTYDATA,
7900 if (result == ISC_R_SUCCESS) {
7901 rbtdbiter->node = node;
7902 rbtdbiter->current = &rbtdbiter->nsec3chain;
7908 if (result == ISC_R_SUCCESS) {
7909 result = dns_rbtnodechain_current(rbtdbiter->current, iname,
7911 if (result == ISC_R_SUCCESS) {
7912 rbtdbiter->new_origin = ISC_TRUE;
7913 reference_iter_node(rbtdbiter);
7915 } else if (result == DNS_R_PARTIALMATCH) {
7916 result = ISC_R_NOTFOUND;
7917 rbtdbiter->node = NULL;
7920 rbtdbiter->result = result;
7922 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
7923 isc_result_t tresult;
7924 tresult = dns_rbtnodechain_current(rbtdbiter->current, iname,
7926 if (tresult == ISC_R_SUCCESS) {
7927 rbtdbiter->new_origin = ISC_TRUE;
7928 reference_iter_node(rbtdbiter);
7931 rbtdbiter->node = NULL;
7934 rbtdbiter->node = NULL;
7936 rbtdbiter->result = (result == DNS_R_PARTIALMATCH) ?
7937 ISC_R_SUCCESS : result;
7944 dbiterator_prev(dns_dbiterator_t *iterator) {
7945 isc_result_t result;
7946 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7947 dns_name_t *name, *origin;
7948 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7950 REQUIRE(rbtdbiter->node != NULL);
7952 if (rbtdbiter->result != ISC_R_SUCCESS)
7953 return (rbtdbiter->result);
7955 if (rbtdbiter->paused)
7956 resume_iteration(rbtdbiter);
7958 name = dns_fixedname_name(&rbtdbiter->name);
7959 origin = dns_fixedname_name(&rbtdbiter->origin);
7960 result = dns_rbtnodechain_prev(rbtdbiter->current, name, origin);
7961 if (result == ISC_R_NOMORE && !rbtdbiter->nsec3only &&
7962 !rbtdbiter->nonsec3 &&
7963 &rbtdbiter->nsec3chain == rbtdbiter->current) {
7964 rbtdbiter->current = &rbtdbiter->chain;
7965 dns_rbtnodechain_reset(rbtdbiter->current);
7966 result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->tree,
7968 if (result == ISC_R_NOTFOUND)
7969 result = ISC_R_NOMORE;
7972 dereference_iter_node(rbtdbiter);
7974 if (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
7975 rbtdbiter->new_origin = ISC_TF(result == DNS_R_NEWORIGIN);
7976 result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
7977 NULL, &rbtdbiter->node);
7980 if (result == ISC_R_SUCCESS)
7981 reference_iter_node(rbtdbiter);
7983 rbtdbiter->result = result;
7989 dbiterator_next(dns_dbiterator_t *iterator) {
7990 isc_result_t result;
7991 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
7992 dns_name_t *name, *origin;
7993 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
7995 REQUIRE(rbtdbiter->node != NULL);
7997 if (rbtdbiter->result != ISC_R_SUCCESS)
7998 return (rbtdbiter->result);
8000 if (rbtdbiter->paused)
8001 resume_iteration(rbtdbiter);
8003 name = dns_fixedname_name(&rbtdbiter->name);
8004 origin = dns_fixedname_name(&rbtdbiter->origin);
8005 result = dns_rbtnodechain_next(rbtdbiter->current, name, origin);
8006 if (result == ISC_R_NOMORE && !rbtdbiter->nsec3only &&
8007 !rbtdbiter->nonsec3 && &rbtdbiter->chain == rbtdbiter->current) {
8008 rbtdbiter->current = &rbtdbiter->nsec3chain;
8009 dns_rbtnodechain_reset(rbtdbiter->current);
8010 result = dns_rbtnodechain_first(rbtdbiter->current,
8011 rbtdb->nsec3, name, origin);
8012 if (result == ISC_R_NOTFOUND)
8013 result = ISC_R_NOMORE;
8016 dereference_iter_node(rbtdbiter);
8018 if (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
8019 rbtdbiter->new_origin = ISC_TF(result == DNS_R_NEWORIGIN);
8020 result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
8021 NULL, &rbtdbiter->node);
8023 if (result == ISC_R_SUCCESS)
8024 reference_iter_node(rbtdbiter);
8026 rbtdbiter->result = result;
8032 dbiterator_current(dns_dbiterator_t *iterator, dns_dbnode_t **nodep,
8035 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
8036 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
8037 dns_rbtnode_t *node = rbtdbiter->node;
8038 isc_result_t result;
8039 dns_name_t *nodename = dns_fixedname_name(&rbtdbiter->name);
8040 dns_name_t *origin = dns_fixedname_name(&rbtdbiter->origin);
8042 REQUIRE(rbtdbiter->result == ISC_R_SUCCESS);
8043 REQUIRE(rbtdbiter->node != NULL);
8045 if (rbtdbiter->paused)
8046 resume_iteration(rbtdbiter);
8049 if (rbtdbiter->common.relative_names)
8051 result = dns_name_concatenate(nodename, origin, name, NULL);
8052 if (result != ISC_R_SUCCESS)
8054 if (rbtdbiter->common.relative_names && rbtdbiter->new_origin)
8055 result = DNS_R_NEWORIGIN;
8057 result = ISC_R_SUCCESS;
8059 NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
8060 new_reference(rbtdb, node);
8061 NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
8063 *nodep = rbtdbiter->node;
8065 if (iterator->cleaning && result == ISC_R_SUCCESS) {
8066 isc_result_t expire_result;
8069 * If the deletion array is full, flush it before trying
8070 * to expire the current node. The current node can't
8071 * fully deleted while the iteration cursor is still on it.
8073 if (rbtdbiter->delete == DELETION_BATCH_MAX)
8074 flush_deletions(rbtdbiter);
8076 expire_result = expirenode(iterator->db, *nodep, 0);
8079 * expirenode() currently always returns success.
8081 if (expire_result == ISC_R_SUCCESS && node->down == NULL) {
8084 rbtdbiter->deletions[rbtdbiter->delete++] = node;
8085 NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
8086 dns_rbtnode_refincrement(node, &refs);
8088 NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
8096 dbiterator_pause(dns_dbiterator_t *iterator) {
8097 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
8098 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
8100 if (rbtdbiter->result != ISC_R_SUCCESS &&
8101 rbtdbiter->result != ISC_R_NOMORE)
8102 return (rbtdbiter->result);
8104 if (rbtdbiter->paused)
8105 return (ISC_R_SUCCESS);
8107 rbtdbiter->paused = ISC_TRUE;
8109 if (rbtdbiter->tree_locked != isc_rwlocktype_none) {
8110 INSIST(rbtdbiter->tree_locked == isc_rwlocktype_read);
8111 RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
8112 rbtdbiter->tree_locked = isc_rwlocktype_none;
8115 flush_deletions(rbtdbiter);
8117 return (ISC_R_SUCCESS);
8121 dbiterator_origin(dns_dbiterator_t *iterator, dns_name_t *name) {
8122 rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
8123 dns_name_t *origin = dns_fixedname_name(&rbtdbiter->origin);
8125 if (rbtdbiter->result != ISC_R_SUCCESS)
8126 return (rbtdbiter->result);
8128 return (dns_name_copy(origin, name, NULL));
8132 * Additional cache routines.
8135 rdataset_getadditional(dns_rdataset_t *rdataset, dns_rdatasetadditional_t type,
8136 dns_rdatatype_t qtype, dns_acache_t *acache,
8137 dns_zone_t **zonep, dns_db_t **dbp,
8138 dns_dbversion_t **versionp, dns_dbnode_t **nodep,
8139 dns_name_t *fname, dns_message_t *msg,
8142 dns_rbtdb_t *rbtdb = rdataset->private1;
8143 dns_rbtnode_t *rbtnode = rdataset->private2;
8144 unsigned char *raw = rdataset->private3; /* RDATASLAB */
8145 unsigned int current_count = rdataset->privateuint4;
8147 rdatasetheader_t *header;
8148 nodelock_t *nodelock;
8149 unsigned int total_count;
8150 acachectl_t *acarray;
8151 dns_acacheentry_t *entry;
8152 isc_result_t result;
8154 UNUSED(qtype); /* we do not use this value at least for now */
8157 header = (struct rdatasetheader *)(raw - sizeof(*header));
8159 total_count = raw[0] * 256 + raw[1];
8160 INSIST(total_count > current_count);
8161 count = total_count - current_count - 1;
8165 nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
8166 NODE_LOCK(nodelock, isc_rwlocktype_read);
8169 case dns_rdatasetadditional_fromauth:
8170 acarray = header->additional_auth;
8172 case dns_rdatasetadditional_fromcache:
8175 case dns_rdatasetadditional_fromglue:
8176 acarray = header->additional_glue;
8182 if (acarray == NULL) {
8183 if (type != dns_rdatasetadditional_fromcache)
8184 dns_acache_countquerymiss(acache);
8185 NODE_UNLOCK(nodelock, isc_rwlocktype_read);
8186 return (ISC_R_NOTFOUND);
8189 if (acarray[count].entry == NULL) {
8190 dns_acache_countquerymiss(acache);
8191 NODE_UNLOCK(nodelock, isc_rwlocktype_read);
8192 return (ISC_R_NOTFOUND);
8196 dns_acache_attachentry(acarray[count].entry, &entry);
8198 NODE_UNLOCK(nodelock, isc_rwlocktype_read);
8200 result = dns_acache_getentry(entry, zonep, dbp, versionp,
8201 nodep, fname, msg, now);
8203 dns_acache_detachentry(&entry);
8209 acache_callback(dns_acacheentry_t *entry, void **arg) {
8211 dns_rbtnode_t *rbtnode;
8212 nodelock_t *nodelock;
8213 acachectl_t *acarray = NULL;
8214 acache_cbarg_t *cbarg;
8217 REQUIRE(arg != NULL);
8221 * The caller must hold the entry lock.
8224 rbtdb = (dns_rbtdb_t *)cbarg->db;
8225 rbtnode = (dns_rbtnode_t *)cbarg->node;
8227 nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
8228 NODE_LOCK(nodelock, isc_rwlocktype_write);
8230 switch (cbarg->type) {
8231 case dns_rdatasetadditional_fromauth:
8232 acarray = cbarg->header->additional_auth;
8234 case dns_rdatasetadditional_fromglue:
8235 acarray = cbarg->header->additional_glue;
8241 count = cbarg->count;
8242 if (acarray != NULL && acarray[count].entry == entry) {
8243 acarray[count].entry = NULL;
8244 INSIST(acarray[count].cbarg == cbarg);
8245 isc_mem_put(rbtdb->common.mctx, cbarg, sizeof(acache_cbarg_t));
8246 acarray[count].cbarg = NULL;
8248 isc_mem_put(rbtdb->common.mctx, cbarg, sizeof(acache_cbarg_t));
8250 dns_acache_detachentry(&entry);
8252 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8254 dns_db_detachnode((dns_db_t *)rbtdb, (dns_dbnode_t **)(void*)&rbtnode);
8255 dns_db_detach((dns_db_t **)(void*)&rbtdb);
8261 acache_cancelentry(isc_mem_t *mctx, dns_acacheentry_t *entry,
8262 acache_cbarg_t **cbargp)
8264 acache_cbarg_t *cbarg;
8266 REQUIRE(mctx != NULL);
8267 REQUIRE(entry != NULL);
8268 REQUIRE(cbargp != NULL && *cbargp != NULL);
8272 dns_acache_cancelentry(entry);
8273 dns_db_detachnode(cbarg->db, &cbarg->node);
8274 dns_db_detach(&cbarg->db);
8276 isc_mem_put(mctx, cbarg, sizeof(acache_cbarg_t));
8282 rdataset_setadditional(dns_rdataset_t *rdataset, dns_rdatasetadditional_t type,
8283 dns_rdatatype_t qtype, dns_acache_t *acache,
8284 dns_zone_t *zone, dns_db_t *db,
8285 dns_dbversion_t *version, dns_dbnode_t *node,
8288 dns_rbtdb_t *rbtdb = rdataset->private1;
8289 dns_rbtnode_t *rbtnode = rdataset->private2;
8290 unsigned char *raw = rdataset->private3; /* RDATASLAB */
8291 unsigned int current_count = rdataset->privateuint4;
8292 rdatasetheader_t *header;
8293 unsigned int total_count, count;
8294 nodelock_t *nodelock;
8295 isc_result_t result;
8296 acachectl_t *acarray;
8297 dns_acacheentry_t *newentry, *oldentry = NULL;
8298 acache_cbarg_t *newcbarg, *oldcbarg = NULL;
8302 if (type == dns_rdatasetadditional_fromcache)
8303 return (ISC_R_SUCCESS);
8305 header = (struct rdatasetheader *)(raw - sizeof(*header));
8307 total_count = raw[0] * 256 + raw[1];
8308 INSIST(total_count > current_count);
8309 count = total_count - current_count - 1; /* should be private data */
8311 newcbarg = isc_mem_get(rbtdb->common.mctx, sizeof(*newcbarg));
8312 if (newcbarg == NULL)
8313 return (ISC_R_NOMEMORY);
8314 newcbarg->type = type;
8315 newcbarg->count = count;
8316 newcbarg->header = header;
8317 newcbarg->db = NULL;
8318 dns_db_attach((dns_db_t *)rbtdb, &newcbarg->db);
8319 newcbarg->node = NULL;
8320 dns_db_attachnode((dns_db_t *)rbtdb, (dns_dbnode_t *)rbtnode,
8323 result = dns_acache_createentry(acache, (dns_db_t *)rbtdb,
8324 acache_callback, newcbarg, &newentry);
8325 if (result != ISC_R_SUCCESS)
8327 /* Set cache data in the new entry. */
8328 result = dns_acache_setentry(acache, newentry, zone, db,
8329 version, node, fname);
8330 if (result != ISC_R_SUCCESS)
8333 nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
8334 NODE_LOCK(nodelock, isc_rwlocktype_write);
8338 case dns_rdatasetadditional_fromauth:
8339 acarray = header->additional_auth;
8341 case dns_rdatasetadditional_fromglue:
8342 acarray = header->additional_glue;
8348 if (acarray == NULL) {
8351 acarray = isc_mem_get(rbtdb->common.mctx, total_count *
8352 sizeof(acachectl_t));
8354 if (acarray == NULL) {
8355 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8359 for (i = 0; i < total_count; i++) {
8360 acarray[i].entry = NULL;
8361 acarray[i].cbarg = NULL;
8365 case dns_rdatasetadditional_fromauth:
8366 header->additional_auth = acarray;
8368 case dns_rdatasetadditional_fromglue:
8369 header->additional_glue = acarray;
8375 if (acarray[count].entry != NULL) {
8377 * Swap the entry. Delay cleaning-up the old entry since
8378 * it would require a node lock.
8380 oldentry = acarray[count].entry;
8381 INSIST(acarray[count].cbarg != NULL);
8382 oldcbarg = acarray[count].cbarg;
8384 acarray[count].entry = newentry;
8385 acarray[count].cbarg = newcbarg;
8387 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8389 if (oldentry != NULL) {
8390 acache_cancelentry(rbtdb->common.mctx, oldentry, &oldcbarg);
8391 dns_acache_detachentry(&oldentry);
8394 return (ISC_R_SUCCESS);
8397 if (newcbarg != NULL) {
8398 if (newentry != NULL) {
8399 acache_cancelentry(rbtdb->common.mctx, newentry,
8401 dns_acache_detachentry(&newentry);
8403 dns_db_detachnode((dns_db_t *)rbtdb, &newcbarg->node);
8404 dns_db_detach(&newcbarg->db);
8405 isc_mem_put(rbtdb->common.mctx, newcbarg,
8414 rdataset_putadditional(dns_acache_t *acache, dns_rdataset_t *rdataset,
8415 dns_rdatasetadditional_t type, dns_rdatatype_t qtype)
8417 dns_rbtdb_t *rbtdb = rdataset->private1;
8418 dns_rbtnode_t *rbtnode = rdataset->private2;
8419 unsigned char *raw = rdataset->private3; /* RDATASLAB */
8420 unsigned int current_count = rdataset->privateuint4;
8421 rdatasetheader_t *header;
8422 nodelock_t *nodelock;
8423 unsigned int total_count, count;
8424 acachectl_t *acarray;
8425 dns_acacheentry_t *entry;
8426 acache_cbarg_t *cbarg;
8428 UNUSED(qtype); /* we do not use this value at least for now */
8431 if (type == dns_rdatasetadditional_fromcache)
8432 return (ISC_R_SUCCESS);
8434 header = (struct rdatasetheader *)(raw - sizeof(*header));
8436 total_count = raw[0] * 256 + raw[1];
8437 INSIST(total_count > current_count);
8438 count = total_count - current_count - 1;
8443 nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
8444 NODE_LOCK(nodelock, isc_rwlocktype_write);
8447 case dns_rdatasetadditional_fromauth:
8448 acarray = header->additional_auth;
8450 case dns_rdatasetadditional_fromglue:
8451 acarray = header->additional_glue;
8457 if (acarray == NULL) {
8458 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8459 return (ISC_R_NOTFOUND);
8462 entry = acarray[count].entry;
8463 if (entry == NULL) {
8464 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8465 return (ISC_R_NOTFOUND);
8468 acarray[count].entry = NULL;
8469 cbarg = acarray[count].cbarg;
8470 acarray[count].cbarg = NULL;
8472 NODE_UNLOCK(nodelock, isc_rwlocktype_write);
8474 if (entry != NULL) {
8476 acache_cancelentry(rbtdb->common.mctx, entry, &cbarg);
8477 dns_acache_detachentry(&entry);
8480 return (ISC_R_SUCCESS);
8484 * Routines for LRU-based cache management.
8488 * See if a given cache entry that is being reused needs to be updated
8489 * in the LRU-list. From the LRU management point of view, this function is
8490 * expected to return true for almost all cases. When used with threads,
8491 * however, this may cause a non-negligible performance penalty because a
8492 * writer lock will have to be acquired before updating the list.
8493 * If DNS_RBTDB_LIMITLRUUPDATE is defined to be non 0 at compilation time, this
8494 * function returns true if the entry has not been updated for some period of
8495 * time. We differentiate the NS or glue address case and the others since
8496 * experiments have shown that the former tends to be accessed relatively
8497 * infrequently and the cost of cache miss is higher (e.g., a missing NS records
8498 * may cause external queries at a higher level zone, involving more
8501 * Caller must hold the node (read or write) lock.
8503 static inline isc_boolean_t
8504 need_headerupdate(rdatasetheader_t *header, isc_stdtime_t now) {
8505 if ((header->attributes &
8506 (RDATASET_ATTR_NONEXISTENT|RDATASET_ATTR_STALE)) != 0)
8509 #if DNS_RBTDB_LIMITLRUUPDATE
8510 if (header->type == dns_rdatatype_ns ||
8511 (header->trust == dns_trust_glue &&
8512 (header->type == dns_rdatatype_a ||
8513 header->type == dns_rdatatype_aaaa))) {
8515 * Glue records are updated if at least 60 seconds have passed
8516 * since the previous update time.
8518 return (header->last_used + 60 <= now);
8521 /* Other records are updated if 5 minutes have passed. */
8522 return (header->last_used + 300 <= now);
8531 * Update the timestamp of a given cache entry and move it to the head
8532 * of the corresponding LRU list.
8534 * Caller must hold the node (write) lock.
8536 * Note that the we do NOT touch the heap here, as the TTL has not changed.
8539 update_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
8542 INSIST(IS_CACHE(rbtdb));
8544 /* To be checked: can we really assume this? XXXMLG */
8545 INSIST(ISC_LINK_LINKED(header, link));
8547 ISC_LIST_UNLINK(rbtdb->rdatasets[header->node->locknum], header, link);
8548 header->last_used = now;
8549 ISC_LIST_PREPEND(rbtdb->rdatasets[header->node->locknum], header, link);
8553 * Purge some expired and/or stale (i.e. unused for some period) cache entries
8554 * under an overmem condition. To recover from this condition quickly, up to
8555 * 2 entries will be purged. This process is triggered while adding a new
8556 * entry, and we specifically avoid purging entries in the same LRU bucket as
8557 * the one to which the new entry will belong. Otherwise, we might purge
8558 * entries of the same name of different RR types while adding RRsets from a
8559 * single response (consider the case where we're adding A and AAAA glue records
8560 * of the same NS name).
8563 overmem_purge(dns_rbtdb_t *rbtdb, unsigned int locknum_start,
8564 isc_stdtime_t now, isc_boolean_t tree_locked)
8566 rdatasetheader_t *header, *header_prev;
8567 unsigned int locknum;
8570 for (locknum = (locknum_start + 1) % rbtdb->node_lock_count;
8571 locknum != locknum_start && purgecount > 0;
8572 locknum = (locknum + 1) % rbtdb->node_lock_count) {
8573 NODE_LOCK(&rbtdb->node_locks[locknum].lock,
8574 isc_rwlocktype_write);
8576 header = isc_heap_element(rbtdb->heaps[locknum], 1);
8577 if (header && header->rdh_ttl <= now - RBTDB_VIRTUAL) {
8578 expire_header(rbtdb, header, tree_locked);
8582 for (header = ISC_LIST_TAIL(rbtdb->rdatasets[locknum]);
8583 header != NULL && purgecount > 0;
8584 header = header_prev) {
8585 header_prev = ISC_LIST_PREV(header, link);
8587 * Unlink the entry at this point to avoid checking it
8588 * again even if it's currently used someone else and
8589 * cannot be purged at this moment. This entry won't be
8590 * referenced any more (so unlinking is safe) since the
8591 * TTL was reset to 0.
8593 ISC_LIST_UNLINK(rbtdb->rdatasets[locknum], header,
8595 expire_header(rbtdb, header, tree_locked);
8599 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
8600 isc_rwlocktype_write);
8605 expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
8606 isc_boolean_t tree_locked)
8608 set_ttl(rbtdb, header, 0);
8609 header->attributes |= RDATASET_ATTR_STALE;
8610 header->node->dirty = 1;
8613 * Caller must hold the node (write) lock.
8616 if (dns_rbtnode_refcurrent(header->node) == 0) {
8618 * If no one else is using the node, we can clean it up now.
8619 * We first need to gain a new reference to the node to meet a
8620 * requirement of decrement_reference().
8622 new_reference(rbtdb, header->node);
8623 decrement_reference(rbtdb, header->node, 0,
8624 isc_rwlocktype_write,
8625 tree_locked ? isc_rwlocktype_write :
8626 isc_rwlocktype_none, ISC_FALSE);