Home | History | Annotate | Line # | Download | only in dns
rbtdb.c revision 1.7
      1  1.6  christos /*	$NetBSD: rbtdb.c,v 1.7 2020/05/24 19:46:23 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  1.1  christos  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  1.1  christos  *
     10  1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     11  1.1  christos  * information regarding copyright ownership.
     12  1.1  christos  */
     13  1.1  christos 
     14  1.1  christos /*! \file */
     15  1.1  christos 
     16  1.1  christos /* #define inline */
     17  1.1  christos 
     18  1.3  christos #include <inttypes.h>
     19  1.3  christos #include <stdbool.h>
     20  1.1  christos 
     21  1.7  christos #include <isc/atomic.h>
     22  1.1  christos #include <isc/crc64.h>
     23  1.1  christos #include <isc/event.h>
     24  1.7  christos #include <isc/file.h>
     25  1.7  christos #include <isc/hash.h>
     26  1.1  christos #include <isc/heap.h>
     27  1.1  christos #include <isc/hex.h>
     28  1.1  christos #include <isc/mem.h>
     29  1.1  christos #include <isc/mutex.h>
     30  1.1  christos #include <isc/once.h>
     31  1.1  christos #include <isc/platform.h>
     32  1.1  christos #include <isc/print.h>
     33  1.1  christos #include <isc/random.h>
     34  1.1  christos #include <isc/refcount.h>
     35  1.1  christos #include <isc/rwlock.h>
     36  1.1  christos #include <isc/serial.h>
     37  1.1  christos #include <isc/socket.h>
     38  1.1  christos #include <isc/stdio.h>
     39  1.1  christos #include <isc/string.h>
     40  1.1  christos #include <isc/task.h>
     41  1.1  christos #include <isc/time.h>
     42  1.1  christos #include <isc/util.h>
     43  1.1  christos 
     44  1.1  christos #include <dns/callbacks.h>
     45  1.1  christos #include <dns/db.h>
     46  1.1  christos #include <dns/dbiterator.h>
     47  1.1  christos #include <dns/events.h>
     48  1.1  christos #include <dns/fixedname.h>
     49  1.1  christos #include <dns/lib.h>
     50  1.1  christos #include <dns/log.h>
     51  1.1  christos #include <dns/masterdump.h>
     52  1.1  christos #include <dns/nsec.h>
     53  1.1  christos #include <dns/nsec3.h>
     54  1.1  christos #include <dns/rbt.h>
     55  1.1  christos #include <dns/rdata.h>
     56  1.1  christos #include <dns/rdataset.h>
     57  1.1  christos #include <dns/rdatasetiter.h>
     58  1.1  christos #include <dns/rdataslab.h>
     59  1.1  christos #include <dns/rdatastruct.h>
     60  1.1  christos #include <dns/result.h>
     61  1.1  christos #include <dns/stats.h>
     62  1.1  christos #include <dns/time.h>
     63  1.1  christos #include <dns/version.h>
     64  1.1  christos #include <dns/view.h>
     65  1.1  christos #include <dns/zone.h>
     66  1.1  christos #include <dns/zonekey.h>
     67  1.1  christos 
     68  1.1  christos #ifndef WIN32
     69  1.1  christos #include <sys/mman.h>
     70  1.7  christos #else /* ifndef WIN32 */
     71  1.7  christos #define PROT_READ   0x01
     72  1.7  christos #define PROT_WRITE  0x02
     73  1.7  christos #define MAP_PRIVATE 0x0002
     74  1.7  christos #define MAP_FAILED  ((void *)-1)
     75  1.7  christos #endif /* ifndef WIN32 */
     76  1.1  christos 
     77  1.1  christos #include "rbtdb.h"
     78  1.1  christos 
     79  1.7  christos #define RBTDB_MAGIC ISC_MAGIC('R', 'B', 'D', '4')
     80  1.1  christos 
     81  1.7  christos #define CHECK(op)                            \
     82  1.7  christos 	do {                                 \
     83  1.7  christos 		result = (op);               \
     84  1.7  christos 		if (result != ISC_R_SUCCESS) \
     85  1.7  christos 			goto failure;        \
     86  1.2  christos 	} while (/*CONSTCOND*/0)
     87  1.1  christos 
     88  1.1  christos /*
     89  1.1  christos  * This is the map file header for RBTDB images.  It is populated, and then
     90  1.1  christos  * written, as the LAST thing done to the file.  Writing this last (with
     91  1.1  christos  * zeros in the header area initially) will ensure that the header is only
     92  1.1  christos  * valid when the RBTDB image is also valid.
     93  1.1  christos  */
     94  1.1  christos typedef struct rbtdb_file_header rbtdb_file_header_t;
     95  1.1  christos 
     96  1.1  christos /* Header length, always the same size regardless of structure size */
     97  1.7  christos #define RBTDB_HEADER_LENGTH 1024
     98  1.1  christos 
     99  1.1  christos struct rbtdb_file_header {
    100  1.1  christos 	char version1[32];
    101  1.3  christos 	uint32_t ptrsize;
    102  1.7  christos 	unsigned int bigendian : 1;
    103  1.3  christos 	uint64_t tree;
    104  1.3  christos 	uint64_t nsec;
    105  1.3  christos 	uint64_t nsec3;
    106  1.1  christos 
    107  1.7  christos 	char version2[32]; /* repeated; must match version1 */
    108  1.1  christos };
    109  1.1  christos 
    110  1.1  christos /*%
    111  1.1  christos  * Note that "impmagic" is not the first four bytes of the struct, so
    112  1.1  christos  * ISC_MAGIC_VALID cannot be used.
    113  1.1  christos  */
    114  1.7  christos #define VALID_RBTDB(rbtdb) \
    115  1.7  christos 	((rbtdb) != NULL && (rbtdb)->common.impmagic == RBTDB_MAGIC)
    116  1.1  christos 
    117  1.7  christos typedef uint32_t rbtdb_serial_t;
    118  1.7  christos typedef uint32_t rbtdb_rdatatype_t;
    119  1.1  christos 
    120  1.7  christos #define RBTDB_RDATATYPE_BASE(type) ((dns_rdatatype_t)((type)&0xFFFF))
    121  1.7  christos #define RBTDB_RDATATYPE_EXT(type)  ((dns_rdatatype_t)((type) >> 16))
    122  1.7  christos #define RBTDB_RDATATYPE_VALUE(base, ext)              \
    123  1.7  christos 	((rbtdb_rdatatype_t)(((uint32_t)ext) << 16) | \
    124  1.7  christos 	 (((uint32_t)base) & 0xffff))
    125  1.1  christos 
    126  1.1  christos #define RBTDB_RDATATYPE_SIGNSEC \
    127  1.7  christos 	RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_nsec)
    128  1.1  christos #define RBTDB_RDATATYPE_SIGNSEC3 \
    129  1.7  christos 	RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_nsec3)
    130  1.1  christos #define RBTDB_RDATATYPE_SIGNS \
    131  1.7  christos 	RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_ns)
    132  1.1  christos #define RBTDB_RDATATYPE_SIGCNAME \
    133  1.7  christos 	RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_cname)
    134  1.1  christos #define RBTDB_RDATATYPE_SIGDNAME \
    135  1.7  christos 	RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_dname)
    136  1.7  christos #define RBTDB_RDATATYPE_SIGDS \
    137  1.7  christos 	RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_ds)
    138  1.7  christos #define RBTDB_RDATATYPE_SIGSOA \
    139  1.7  christos 	RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_soa)
    140  1.7  christos #define RBTDB_RDATATYPE_NCACHEANY RBTDB_RDATATYPE_VALUE(0, dns_rdatatype_any)
    141  1.7  christos 
    142  1.7  christos #define RBTDB_INITLOCK(l)    isc_rwlock_init((l), 0, 0)
    143  1.7  christos #define RBTDB_DESTROYLOCK(l) isc_rwlock_destroy(l)
    144  1.7  christos #define RBTDB_LOCK(l, t)     RWLOCK((l), (t))
    145  1.7  christos #define RBTDB_UNLOCK(l, t)   RWUNLOCK((l), (t))
    146  1.1  christos 
    147  1.1  christos /*
    148  1.1  christos  * Since node locking is sensitive to both performance and memory footprint,
    149  1.1  christos  * we need some trick here.  If we have both high-performance rwlock and
    150  1.1  christos  * high performance and small-memory reference counters, we use rwlock for
    151  1.1  christos  * node lock and isc_refcount for node references.  In this case, we don't have
    152  1.1  christos  * to protect the access to the counters by locks.
    153  1.1  christos  * Otherwise, we simply use ordinary mutex lock for node locking, and use
    154  1.1  christos  * simple integers as reference counters which is protected by the lock.
    155  1.1  christos  * In most cases, we can simply use wrapper macros such as NODE_LOCK and
    156  1.1  christos  * NODE_UNLOCK.  In some other cases, however, we need to protect reference
    157  1.1  christos  * counters first and then protect other parts of a node as read-only data.
    158  1.1  christos  * Special additional macros, NODE_STRONGLOCK(), NODE_WEAKLOCK(), etc, are also
    159  1.1  christos  * provided for these special cases.  When we can use the efficient backend
    160  1.1  christos  * routines, we should only protect the "other members" by NODE_WEAKLOCK(read).
    161  1.1  christos  * Otherwise, we should use NODE_STRONGLOCK() to protect the entire critical
    162  1.1  christos  * section including the access to the reference counter.
    163  1.1  christos  * Note that we cannot use NODE_LOCK()/NODE_UNLOCK() wherever the protected
    164  1.1  christos  * section is also protected by NODE_STRONGLOCK().
    165  1.1  christos  */
    166  1.1  christos typedef isc_rwlock_t nodelock_t;
    167  1.1  christos 
    168  1.7  christos #define NODE_INITLOCK(l)    isc_rwlock_init((l), 0, 0)
    169  1.7  christos #define NODE_DESTROYLOCK(l) isc_rwlock_destroy(l)
    170  1.7  christos #define NODE_LOCK(l, t)	    RWLOCK((l), (t))
    171  1.7  christos #define NODE_UNLOCK(l, t)   RWUNLOCK((l), (t))
    172  1.7  christos #define NODE_TRYUPGRADE(l)  isc_rwlock_tryupgrade(l)
    173  1.3  christos #define NODE_DOWNGRADE(l)   isc_rwlock_downgrade(l)
    174  1.1  christos 
    175  1.1  christos /*%
    176  1.1  christos  * Whether to rate-limit updating the LRU to avoid possible thread contention.
    177  1.7  christos  * Updating LRU requires write locking, so we don't do it every time the
    178  1.7  christos  * record is touched - only after some time passes.
    179  1.1  christos  */
    180  1.1  christos #ifndef DNS_RBTDB_LIMITLRUUPDATE
    181  1.7  christos #define DNS_RBTDB_LIMITLRUUPDATE 1
    182  1.1  christos #endif
    183  1.1  christos 
    184  1.7  christos /*% Time after which we update LRU for glue records, 5 minutes */
    185  1.7  christos #define DNS_RBTDB_LRUUPDATE_GLUE 300
    186  1.7  christos /*% Time after which we update LRU for all other records, 10 minutes */
    187  1.7  christos #define DNS_RBTDB_LRUUPDATE_REGULAR 600
    188  1.7  christos 
    189  1.1  christos /*
    190  1.1  christos  * Allow clients with a virtual time of up to 5 minutes in the past to see
    191  1.1  christos  * records that would have otherwise have expired.
    192  1.1  christos  */
    193  1.1  christos #define RBTDB_VIRTUAL 300
    194  1.1  christos 
    195  1.1  christos struct noqname {
    196  1.7  christos 	dns_name_t name;
    197  1.7  christos 	void *neg;
    198  1.7  christos 	void *negsig;
    199  1.7  christos 	dns_rdatatype_t type;
    200  1.1  christos };
    201  1.1  christos 
    202  1.1  christos typedef struct rdatasetheader {
    203  1.1  christos 	/*%
    204  1.1  christos 	 * Locked by the owning node's lock.
    205  1.1  christos 	 */
    206  1.7  christos 	rbtdb_serial_t serial;
    207  1.7  christos 	dns_ttl_t rdh_ttl;
    208  1.7  christos 	rbtdb_rdatatype_t type;
    209  1.7  christos 	uint16_t attributes;
    210  1.7  christos 	dns_trust_t trust;
    211  1.7  christos 	struct noqname *noqname;
    212  1.7  christos 	struct noqname *closest;
    213  1.7  christos 	unsigned int is_mmapped : 1;
    214  1.7  christos 	unsigned int next_is_relative : 1;
    215  1.7  christos 	unsigned int node_is_relative : 1;
    216  1.7  christos 	unsigned int resign_lsb : 1;
    217  1.1  christos 	/*%<
    218  1.1  christos 	 * We don't use the LIST macros, because the LIST structure has
    219  1.1  christos 	 * both head and tail pointers, and is doubly linked.
    220  1.1  christos 	 */
    221  1.1  christos 
    222  1.7  christos 	struct rdatasetheader *next;
    223  1.1  christos 	/*%<
    224  1.1  christos 	 * If this is the top header for an rdataset, 'next' points
    225  1.1  christos 	 * to the top header for the next rdataset (i.e., the next type).
    226  1.1  christos 	 * Otherwise, it points up to the header whose down pointer points
    227  1.1  christos 	 * at this header.
    228  1.1  christos 	 */
    229  1.1  christos 
    230  1.7  christos 	struct rdatasetheader *down;
    231  1.1  christos 	/*%<
    232  1.1  christos 	 * Points to the header for the next older version of
    233  1.1  christos 	 * this rdataset.
    234  1.1  christos 	 */
    235  1.1  christos 
    236  1.7  christos 	atomic_uint_fast32_t count;
    237  1.1  christos 	/*%<
    238  1.1  christos 	 * Monotonously increased every time this rdataset is bound so that
    239  1.1  christos 	 * it is used as the base of the starting point in DNS responses
    240  1.7  christos 	 * when the "cyclic" rrset-order is required.
    241  1.1  christos 	 */
    242  1.1  christos 
    243  1.7  christos 	dns_rbtnode_t *node;
    244  1.7  christos 	isc_stdtime_t last_used;
    245  1.1  christos 	ISC_LINK(struct rdatasetheader) link;
    246  1.1  christos 
    247  1.7  christos 	unsigned int heap_index;
    248  1.1  christos 	/*%<
    249  1.1  christos 	 * Used for TTL-based cache cleaning.
    250  1.1  christos 	 */
    251  1.7  christos 	isc_stdtime_t resign;
    252  1.1  christos 	/*%<
    253  1.1  christos 	 * Case vector.  If the bit is set then the corresponding
    254  1.1  christos 	 * character in the owner name needs to be AND'd with 0x20,
    255  1.1  christos 	 * rendering that character upper case.
    256  1.1  christos 	 */
    257  1.7  christos 	unsigned char upper[32];
    258  1.1  christos } rdatasetheader_t;
    259  1.1  christos 
    260  1.7  christos typedef ISC_LIST(rdatasetheader_t) rdatasetheaderlist_t;
    261  1.7  christos typedef ISC_LIST(dns_rbtnode_t) rbtnodelist_t;
    262  1.1  christos 
    263  1.7  christos #define RDATASET_ATTR_NONEXISTENT 0x0001
    264  1.1  christos /*%< May be potentially served as stale data. */
    265  1.7  christos #define RDATASET_ATTR_STALE	     0x0002
    266  1.7  christos #define RDATASET_ATTR_IGNORE	     0x0004
    267  1.7  christos #define RDATASET_ATTR_RETAIN	     0x0008
    268  1.7  christos #define RDATASET_ATTR_NXDOMAIN	     0x0010
    269  1.7  christos #define RDATASET_ATTR_RESIGN	     0x0020
    270  1.7  christos #define RDATASET_ATTR_STATCOUNT	     0x0040
    271  1.7  christos #define RDATASET_ATTR_OPTOUT	     0x0080
    272  1.7  christos #define RDATASET_ATTR_NEGATIVE	     0x0100
    273  1.7  christos #define RDATASET_ATTR_PREFETCH	     0x0200
    274  1.7  christos #define RDATASET_ATTR_CASESET	     0x0400
    275  1.7  christos #define RDATASET_ATTR_ZEROTTL	     0x0800
    276  1.7  christos #define RDATASET_ATTR_CASEFULLYLOWER 0x1000
    277  1.1  christos /*%< Ancient - awaiting cleanup. */
    278  1.7  christos #define RDATASET_ATTR_ANCIENT 0x2000
    279  1.1  christos 
    280  1.1  christos /*
    281  1.1  christos  * XXX
    282  1.1  christos  * When the cache will pre-expire data (due to memory low or other
    283  1.1  christos  * situations) before the rdataset's TTL has expired, it MUST
    284  1.1  christos  * respect the RETAIN bit and not expire the data until its TTL is
    285  1.1  christos  * expired.
    286  1.1  christos  */
    287  1.1  christos 
    288  1.7  christos #undef IGNORE /* WIN32 winbase.h defines this. */
    289  1.1  christos 
    290  1.7  christos #define EXISTS(header) (((header)->attributes & RDATASET_ATTR_NONEXISTENT) == 0)
    291  1.1  christos #define NONEXISTENT(header) \
    292  1.1  christos 	(((header)->attributes & RDATASET_ATTR_NONEXISTENT) != 0)
    293  1.7  christos #define IGNORE(header)	 (((header)->attributes & RDATASET_ATTR_IGNORE) != 0)
    294  1.7  christos #define RETAIN(header)	 (((header)->attributes & RDATASET_ATTR_RETAIN) != 0)
    295  1.7  christos #define NXDOMAIN(header) (((header)->attributes & RDATASET_ATTR_NXDOMAIN) != 0)
    296  1.7  christos #define STALE(header)	 (((header)->attributes & RDATASET_ATTR_STALE) != 0)
    297  1.7  christos #define RESIGN(header)	 (((header)->attributes & RDATASET_ATTR_RESIGN) != 0)
    298  1.7  christos #define OPTOUT(header)	 (((header)->attributes & RDATASET_ATTR_OPTOUT) != 0)
    299  1.7  christos #define NEGATIVE(header) (((header)->attributes & RDATASET_ATTR_NEGATIVE) != 0)
    300  1.7  christos #define PREFETCH(header) (((header)->attributes & RDATASET_ATTR_PREFETCH) != 0)
    301  1.7  christos #define CASESET(header)	 (((header)->attributes & RDATASET_ATTR_CASESET) != 0)
    302  1.7  christos #define ZEROTTL(header)	 (((header)->attributes & RDATASET_ATTR_ZEROTTL) != 0)
    303  1.1  christos #define CASEFULLYLOWER(header) \
    304  1.1  christos 	(((header)->attributes & RDATASET_ATTR_CASEFULLYLOWER) != 0)
    305  1.7  christos #define ANCIENT(header) (((header)->attributes & RDATASET_ATTR_ANCIENT) != 0)
    306  1.1  christos 
    307  1.7  christos #define ACTIVE(header, now)             \
    308  1.1  christos 	(((header)->rdh_ttl > (now)) || \
    309  1.1  christos 	 ((header)->rdh_ttl == (now) && ZEROTTL(header)))
    310  1.1  christos 
    311  1.7  christos #define DEFAULT_NODE_LOCK_COUNT	   53 /*%< Should be prime. */
    312  1.7  christos #define RBTDB_GLUE_TABLE_INIT_SIZE 2U
    313  1.1  christos 
    314  1.1  christos /*%
    315  1.1  christos  * Number of buckets for cache DB entries (locks, LRU lists, TTL heaps).
    316  1.1  christos  * There is a tradeoff issue about configuring this value: if this is too
    317  1.1  christos  * small, it may cause heavier contention between threads; if this is too large,
    318  1.1  christos  * LRU purge algorithm won't work well (entries tend to be purged prematurely).
    319  1.1  christos  * The default value should work well for most environments, but this can
    320  1.1  christos  * also be configurable at compilation time via the
    321  1.1  christos  * DNS_RBTDB_CACHE_NODE_LOCK_COUNT variable.  This value must be larger than
    322  1.1  christos  * 1 due to the assumption of overmem_purge().
    323  1.1  christos  */
    324  1.1  christos #ifdef DNS_RBTDB_CACHE_NODE_LOCK_COUNT
    325  1.1  christos #if DNS_RBTDB_CACHE_NODE_LOCK_COUNT <= 1
    326  1.1  christos #error "DNS_RBTDB_CACHE_NODE_LOCK_COUNT must be larger than 1"
    327  1.7  christos #else /* if DNS_RBTDB_CACHE_NODE_LOCK_COUNT <= 1 */
    328  1.1  christos #define DEFAULT_CACHE_NODE_LOCK_COUNT DNS_RBTDB_CACHE_NODE_LOCK_COUNT
    329  1.7  christos #endif /* if DNS_RBTDB_CACHE_NODE_LOCK_COUNT <= 1 */
    330  1.7  christos #else  /* ifdef DNS_RBTDB_CACHE_NODE_LOCK_COUNT */
    331  1.7  christos #define DEFAULT_CACHE_NODE_LOCK_COUNT 97
    332  1.7  christos #endif /* DNS_RBTDB_CACHE_NODE_LOCK_COUNT */
    333  1.1  christos 
    334  1.1  christos typedef struct {
    335  1.7  christos 	nodelock_t lock;
    336  1.1  christos 	/* Protected in the refcount routines. */
    337  1.7  christos 	isc_refcount_t references;
    338  1.1  christos 	/* Locked by lock. */
    339  1.7  christos 	bool exiting;
    340  1.1  christos } rbtdb_nodelock_t;
    341  1.1  christos 
    342  1.1  christos typedef struct rbtdb_changed {
    343  1.7  christos 	dns_rbtnode_t *node;
    344  1.7  christos 	bool dirty;
    345  1.7  christos 	ISC_LINK(struct rbtdb_changed) link;
    346  1.1  christos } rbtdb_changed_t;
    347  1.1  christos 
    348  1.7  christos typedef ISC_LIST(rbtdb_changed_t) rbtdb_changedlist_t;
    349  1.1  christos 
    350  1.7  christos typedef enum { dns_db_insecure, dns_db_partial, dns_db_secure } dns_db_secure_t;
    351  1.1  christos 
    352  1.1  christos typedef struct dns_rbtdb dns_rbtdb_t;
    353  1.1  christos 
    354  1.1  christos /* Reason for expiring a record from cache */
    355  1.7  christos typedef enum { expire_lru, expire_ttl, expire_flush } expire_t;
    356  1.1  christos 
    357  1.1  christos typedef struct rbtdb_glue rbtdb_glue_t;
    358  1.1  christos 
    359  1.1  christos typedef struct rbtdb_glue_table_node {
    360  1.1  christos 	struct rbtdb_glue_table_node *next;
    361  1.7  christos 	dns_rbtnode_t *node;
    362  1.7  christos 	rbtdb_glue_t *glue_list;
    363  1.1  christos } rbtdb_glue_table_node_t;
    364  1.1  christos 
    365  1.1  christos typedef enum {
    366  1.1  christos 	rdataset_ttl_fresh,
    367  1.1  christos 	rdataset_ttl_stale,
    368  1.1  christos 	rdataset_ttl_ancient
    369  1.1  christos } rdataset_ttl_t;
    370  1.1  christos 
    371  1.1  christos typedef struct rbtdb_version {
    372  1.1  christos 	/* Not locked */
    373  1.7  christos 	rbtdb_serial_t serial;
    374  1.7  christos 	dns_rbtdb_t *rbtdb;
    375  1.1  christos 	/*
    376  1.1  christos 	 * Protected in the refcount routines.
    377  1.1  christos 	 * XXXJT: should we change the lock policy based on the refcount
    378  1.1  christos 	 * performance?
    379  1.1  christos 	 */
    380  1.7  christos 	isc_refcount_t references;
    381  1.1  christos 	/* Locked by database lock. */
    382  1.7  christos 	bool writer;
    383  1.7  christos 	bool commit_ok;
    384  1.7  christos 	rbtdb_changedlist_t changed_list;
    385  1.7  christos 	rdatasetheaderlist_t resigned_list;
    386  1.7  christos 	ISC_LINK(struct rbtdb_version) link;
    387  1.7  christos 	dns_db_secure_t secure;
    388  1.7  christos 	bool havensec3;
    389  1.1  christos 	/* NSEC3 parameters */
    390  1.7  christos 	dns_hash_t hash;
    391  1.7  christos 	uint8_t flags;
    392  1.7  christos 	uint16_t iterations;
    393  1.7  christos 	uint8_t salt_length;
    394  1.7  christos 	unsigned char salt[DNS_NSEC3_SALTSIZE];
    395  1.1  christos 
    396  1.1  christos 	/*
    397  1.1  christos 	 * records and bytes are covered by rwlock.
    398  1.1  christos 	 */
    399  1.7  christos 	isc_rwlock_t rwlock;
    400  1.7  christos 	uint64_t records;
    401  1.7  christos 	uint64_t bytes;
    402  1.7  christos 
    403  1.7  christos 	isc_rwlock_t glue_rwlock;
    404  1.7  christos 	size_t glue_table_size;
    405  1.7  christos 	size_t glue_table_nodecount;
    406  1.7  christos 	rbtdb_glue_table_node_t **glue_table;
    407  1.1  christos } rbtdb_version_t;
    408  1.1  christos 
    409  1.7  christos typedef ISC_LIST(rbtdb_version_t) rbtdb_versionlist_t;
    410  1.1  christos 
    411  1.1  christos struct dns_rbtdb {
    412  1.1  christos 	/* Unlocked. */
    413  1.7  christos 	dns_db_t common;
    414  1.1  christos 	/* Locks the data in this struct */
    415  1.7  christos 	isc_rwlock_t lock;
    416  1.1  christos 	/* Locks the tree structure (prevents nodes appearing/disappearing) */
    417  1.7  christos 	isc_rwlock_t tree_lock;
    418  1.1  christos 	/* Locks for individual tree nodes */
    419  1.7  christos 	unsigned int node_lock_count;
    420  1.7  christos 	rbtdb_nodelock_t *node_locks;
    421  1.7  christos 	dns_rbtnode_t *origin_node;
    422  1.7  christos 	dns_rbtnode_t *nsec3_origin_node;
    423  1.7  christos 	dns_stats_t *rrsetstats;     /* cache DB only */
    424  1.7  christos 	isc_stats_t *cachestats;     /* cache DB only */
    425  1.7  christos 	isc_stats_t *gluecachestats; /* zone DB only */
    426  1.1  christos 	/* Locked by lock. */
    427  1.7  christos 	unsigned int active;
    428  1.7  christos 	isc_refcount_t references;
    429  1.7  christos 	unsigned int attributes;
    430  1.7  christos 	rbtdb_serial_t current_serial;
    431  1.7  christos 	rbtdb_serial_t least_serial;
    432  1.7  christos 	rbtdb_serial_t next_serial;
    433  1.7  christos 	rbtdb_version_t *current_version;
    434  1.7  christos 	rbtdb_version_t *future_version;
    435  1.7  christos 	rbtdb_versionlist_t open_versions;
    436  1.7  christos 	isc_task_t *task;
    437  1.7  christos 	dns_dbnode_t *soanode;
    438  1.7  christos 	dns_dbnode_t *nsnode;
    439  1.1  christos 
    440  1.1  christos 	/*
    441  1.1  christos 	 * Maximum length of time to keep using a stale answer past its
    442  1.1  christos 	 * normal TTL expiry.
    443  1.7  christos 	 */
    444  1.7  christos 	dns_ttl_t serve_stale_ttl;
    445  1.1  christos 
    446  1.1  christos 	/*
    447  1.1  christos 	 * This is a linked list used to implement the LRU cache.  There will
    448  1.1  christos 	 * be node_lock_count linked lists here.  Nodes in bucket 1 will be
    449  1.1  christos 	 * placed on the linked list rdatasets[1].
    450  1.1  christos 	 */
    451  1.7  christos 	rdatasetheaderlist_t *rdatasets;
    452  1.1  christos 
    453  1.1  christos 	/*%
    454  1.1  christos 	 * Temporary storage for stale cache nodes and dynamically deleted
    455  1.1  christos 	 * nodes that await being cleaned up.
    456  1.1  christos 	 */
    457  1.7  christos 	rbtnodelist_t *deadnodes;
    458  1.1  christos 
    459  1.1  christos 	/*
    460  1.1  christos 	 * Heaps.  These are used for TTL based expiry in a cache,
    461  1.1  christos 	 * or for zone resigning in a zone DB.  hmctx is the memory
    462  1.1  christos 	 * context to use for the heap (which differs from the main
    463  1.1  christos 	 * database memory context in the case of a cache).
    464  1.1  christos 	 */
    465  1.7  christos 	isc_mem_t *hmctx;
    466  1.7  christos 	isc_heap_t **heaps;
    467  1.1  christos 
    468  1.1  christos 	/*
    469  1.1  christos 	 * Base values for the mmap() code.
    470  1.1  christos 	 */
    471  1.7  christos 	void *mmap_location;
    472  1.7  christos 	size_t mmap_size;
    473  1.1  christos 
    474  1.1  christos 	/* Locked by tree_lock. */
    475  1.7  christos 	dns_rbt_t *tree;
    476  1.7  christos 	dns_rbt_t *nsec;
    477  1.7  christos 	dns_rbt_t *nsec3;
    478  1.1  christos 
    479  1.1  christos 	/* Unlocked */
    480  1.7  christos 	unsigned int quantum;
    481  1.1  christos };
    482  1.1  christos 
    483  1.7  christos #define RBTDB_ATTR_LOADED  0x01
    484  1.7  christos #define RBTDB_ATTR_LOADING 0x02
    485  1.1  christos 
    486  1.1  christos #define KEEPSTALE(rbtdb) ((rbtdb)->serve_stale_ttl > 0)
    487  1.1  christos 
    488  1.1  christos /*%
    489  1.1  christos  * Search Context
    490  1.1  christos  */
    491  1.1  christos typedef struct {
    492  1.7  christos 	dns_rbtdb_t *rbtdb;
    493  1.7  christos 	rbtdb_version_t *rbtversion;
    494  1.7  christos 	rbtdb_serial_t serial;
    495  1.7  christos 	unsigned int options;
    496  1.7  christos 	dns_rbtnodechain_t chain;
    497  1.7  christos 	bool copy_name;
    498  1.7  christos 	bool need_cleanup;
    499  1.7  christos 	bool wild;
    500  1.7  christos 	dns_rbtnode_t *zonecut;
    501  1.7  christos 	rdatasetheader_t *zonecut_rdataset;
    502  1.7  christos 	rdatasetheader_t *zonecut_sigrdataset;
    503  1.7  christos 	dns_fixedname_t zonecut_name;
    504  1.7  christos 	isc_stdtime_t now;
    505  1.1  christos } rbtdb_search_t;
    506  1.1  christos 
    507  1.1  christos /*%
    508  1.1  christos  * Load Context
    509  1.1  christos  */
    510  1.1  christos typedef struct {
    511  1.7  christos 	dns_rbtdb_t *rbtdb;
    512  1.7  christos 	isc_stdtime_t now;
    513  1.1  christos } rbtdb_load_t;
    514  1.1  christos 
    515  1.7  christos static void
    516  1.7  christos delete_callback(void *data, void *arg);
    517  1.7  christos static void
    518  1.7  christos rdataset_disassociate(dns_rdataset_t *rdataset);
    519  1.7  christos static isc_result_t
    520  1.7  christos rdataset_first(dns_rdataset_t *rdataset);
    521  1.7  christos static isc_result_t
    522  1.7  christos rdataset_next(dns_rdataset_t *rdataset);
    523  1.7  christos static void
    524  1.7  christos rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata);
    525  1.7  christos static void
    526  1.7  christos rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target);
    527  1.7  christos static unsigned int
    528  1.7  christos rdataset_count(dns_rdataset_t *rdataset);
    529  1.7  christos static isc_result_t
    530  1.7  christos rdataset_getnoqname(dns_rdataset_t *rdataset, dns_name_t *name,
    531  1.7  christos 		    dns_rdataset_t *neg, dns_rdataset_t *negsig);
    532  1.7  christos static isc_result_t
    533  1.7  christos rdataset_getclosest(dns_rdataset_t *rdataset, dns_name_t *name,
    534  1.7  christos 		    dns_rdataset_t *neg, dns_rdataset_t *negsig);
    535  1.7  christos static inline bool
    536  1.7  christos need_headerupdate(rdatasetheader_t *header, isc_stdtime_t now);
    537  1.7  christos static void
    538  1.7  christos update_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header, isc_stdtime_t now);
    539  1.7  christos static void
    540  1.7  christos expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header, bool tree_locked,
    541  1.7  christos 	      expire_t reason);
    542  1.7  christos static void
    543  1.7  christos overmem_purge(dns_rbtdb_t *rbtdb, unsigned int locknum_start, isc_stdtime_t now,
    544  1.7  christos 	      bool tree_locked);
    545  1.7  christos static isc_result_t
    546  1.7  christos resign_insert(dns_rbtdb_t *rbtdb, int idx, rdatasetheader_t *newheader);
    547  1.7  christos static void
    548  1.7  christos resign_delete(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
    549  1.7  christos 	      rdatasetheader_t *header);
    550  1.7  christos static void
    551  1.7  christos prune_tree(isc_task_t *task, isc_event_t *event);
    552  1.7  christos static void
    553  1.7  christos rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust);
    554  1.7  christos static void
    555  1.7  christos rdataset_expire(dns_rdataset_t *rdataset);
    556  1.7  christos static void
    557  1.7  christos rdataset_clearprefetch(dns_rdataset_t *rdataset);
    558  1.7  christos static void
    559  1.7  christos rdataset_setownercase(dns_rdataset_t *rdataset, const dns_name_t *name);
    560  1.7  christos static void
    561  1.7  christos rdataset_getownercase(const dns_rdataset_t *rdataset, dns_name_t *name);
    562  1.7  christos static isc_result_t
    563  1.7  christos rdataset_addglue(dns_rdataset_t *rdataset, dns_dbversion_t *version,
    564  1.7  christos 		 dns_message_t *msg);
    565  1.7  christos static void
    566  1.7  christos free_gluetable(rbtdb_version_t *version);
    567  1.1  christos 
    568  1.7  christos static dns_rdatasetmethods_t rdataset_methods = { rdataset_disassociate,
    569  1.7  christos 						  rdataset_first,
    570  1.7  christos 						  rdataset_next,
    571  1.7  christos 						  rdataset_current,
    572  1.7  christos 						  rdataset_clone,
    573  1.7  christos 						  rdataset_count,
    574  1.7  christos 						  NULL, /* addnoqname */
    575  1.7  christos 						  rdataset_getnoqname,
    576  1.7  christos 						  NULL, /* addclosest */
    577  1.7  christos 						  rdataset_getclosest,
    578  1.7  christos 						  rdataset_settrust,
    579  1.7  christos 						  rdataset_expire,
    580  1.7  christos 						  rdataset_clearprefetch,
    581  1.7  christos 						  rdataset_setownercase,
    582  1.7  christos 						  rdataset_getownercase,
    583  1.7  christos 						  rdataset_addglue };
    584  1.1  christos 
    585  1.1  christos static dns_rdatasetmethods_t slab_methods = {
    586  1.1  christos 	rdataset_disassociate,
    587  1.1  christos 	rdataset_first,
    588  1.1  christos 	rdataset_next,
    589  1.1  christos 	rdataset_current,
    590  1.1  christos 	rdataset_clone,
    591  1.1  christos 	rdataset_count,
    592  1.1  christos 	NULL, /* addnoqname */
    593  1.1  christos 	NULL, /* getnoqname */
    594  1.1  christos 	NULL, /* addclosest */
    595  1.1  christos 	NULL, /* getclosest */
    596  1.1  christos 	NULL, /* settrust */
    597  1.1  christos 	NULL, /* expire */
    598  1.1  christos 	NULL, /* clearprefetch */
    599  1.1  christos 	NULL, /* setownercase */
    600  1.1  christos 	NULL, /* getownercase */
    601  1.1  christos 	NULL  /* addglue */
    602  1.1  christos };
    603  1.1  christos 
    604  1.7  christos static void
    605  1.7  christos rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp);
    606  1.7  christos static isc_result_t
    607  1.7  christos rdatasetiter_first(dns_rdatasetiter_t *iterator);
    608  1.7  christos static isc_result_t
    609  1.7  christos rdatasetiter_next(dns_rdatasetiter_t *iterator);
    610  1.7  christos static void
    611  1.7  christos rdatasetiter_current(dns_rdatasetiter_t *iterator, dns_rdataset_t *rdataset);
    612  1.1  christos 
    613  1.1  christos static dns_rdatasetitermethods_t rdatasetiter_methods = {
    614  1.7  christos 	rdatasetiter_destroy, rdatasetiter_first, rdatasetiter_next,
    615  1.1  christos 	rdatasetiter_current
    616  1.1  christos };
    617  1.1  christos 
    618  1.1  christos typedef struct rbtdb_rdatasetiter {
    619  1.7  christos 	dns_rdatasetiter_t common;
    620  1.7  christos 	rdatasetheader_t *current;
    621  1.1  christos } rbtdb_rdatasetiter_t;
    622  1.1  christos 
    623  1.1  christos /*
    624  1.1  christos  * Note that these iterators, unless created with either DNS_DB_NSEC3ONLY or
    625  1.1  christos  * DNS_DB_NONSEC3, will transparently move between the last node of the
    626  1.1  christos  * "regular" RBT ("chain" field) and the root node of the NSEC3 RBT
    627  1.1  christos  * ("nsec3chain" field) of the database in question, as if the latter was a
    628  1.1  christos  * successor to the former in lexical order.  The "current" field always holds
    629  1.1  christos  * the address of either "chain" or "nsec3chain", depending on which RBT is
    630  1.1  christos  * being traversed at given time.
    631  1.1  christos  */
    632  1.7  christos static void
    633  1.7  christos dbiterator_destroy(dns_dbiterator_t **iteratorp);
    634  1.7  christos static isc_result_t
    635  1.7  christos dbiterator_first(dns_dbiterator_t *iterator);
    636  1.7  christos static isc_result_t
    637  1.7  christos dbiterator_last(dns_dbiterator_t *iterator);
    638  1.7  christos static isc_result_t
    639  1.7  christos dbiterator_seek(dns_dbiterator_t *iterator, const dns_name_t *name);
    640  1.7  christos static isc_result_t
    641  1.7  christos dbiterator_prev(dns_dbiterator_t *iterator);
    642  1.7  christos static isc_result_t
    643  1.7  christos dbiterator_next(dns_dbiterator_t *iterator);
    644  1.7  christos static isc_result_t
    645  1.7  christos dbiterator_current(dns_dbiterator_t *iterator, dns_dbnode_t **nodep,
    646  1.7  christos 		   dns_name_t *name);
    647  1.7  christos static isc_result_t
    648  1.7  christos dbiterator_pause(dns_dbiterator_t *iterator);
    649  1.7  christos static isc_result_t
    650  1.7  christos dbiterator_origin(dns_dbiterator_t *iterator, dns_name_t *name);
    651  1.1  christos 
    652  1.1  christos static dns_dbiteratormethods_t dbiterator_methods = {
    653  1.7  christos 	dbiterator_destroy, dbiterator_first, dbiterator_last,
    654  1.7  christos 	dbiterator_seek,    dbiterator_prev,  dbiterator_next,
    655  1.7  christos 	dbiterator_current, dbiterator_pause, dbiterator_origin
    656  1.1  christos };
    657  1.1  christos 
    658  1.1  christos #define DELETION_BATCH_MAX 64
    659  1.1  christos 
    660  1.1  christos /*
    661  1.3  christos  * If 'paused' is true, then the tree lock is not being held.
    662  1.1  christos  */
    663  1.1  christos typedef struct rbtdb_dbiterator {
    664  1.7  christos 	dns_dbiterator_t common;
    665  1.7  christos 	bool paused;
    666  1.7  christos 	bool new_origin;
    667  1.7  christos 	isc_rwlocktype_t tree_locked;
    668  1.7  christos 	isc_result_t result;
    669  1.7  christos 	dns_fixedname_t name;
    670  1.7  christos 	dns_fixedname_t origin;
    671  1.7  christos 	dns_rbtnodechain_t chain;
    672  1.7  christos 	dns_rbtnodechain_t nsec3chain;
    673  1.7  christos 	dns_rbtnodechain_t *current;
    674  1.7  christos 	dns_rbtnode_t *node;
    675  1.7  christos 	dns_rbtnode_t *deletions[DELETION_BATCH_MAX];
    676  1.7  christos 	int delcnt;
    677  1.7  christos 	bool nsec3only;
    678  1.7  christos 	bool nonsec3;
    679  1.1  christos } rbtdb_dbiterator_t;
    680  1.1  christos 
    681  1.7  christos #define IS_STUB(rbtdb)	(((rbtdb)->common.attributes & DNS_DBATTR_STUB) != 0)
    682  1.1  christos #define IS_CACHE(rbtdb) (((rbtdb)->common.attributes & DNS_DBATTR_CACHE) != 0)
    683  1.1  christos 
    684  1.7  christos static void
    685  1.7  christos free_rbtdb(dns_rbtdb_t *rbtdb, bool log, isc_event_t *event);
    686  1.7  christos static void
    687  1.7  christos overmem(dns_db_t *db, bool over);
    688  1.7  christos static void
    689  1.7  christos setnsec3parameters(dns_db_t *db, rbtdb_version_t *version);
    690  1.7  christos static void
    691  1.7  christos setownercase(rdatasetheader_t *header, const dns_name_t *name);
    692  1.1  christos 
    693  1.7  christos static bool
    694  1.7  christos match_header_version(rbtdb_file_header_t *header);
    695  1.1  christos 
    696  1.1  christos /* Pad to 32 bytes */
    697  1.1  christos static char FILE_VERSION[32] = "\0";
    698  1.1  christos 
    699  1.1  christos /*%
    700  1.1  christos  * 'init_count' is used to initialize 'newheader->count' which inturn
    701  1.1  christos  * is used to determine where in the cycle rrset-order cyclic starts.
    702  1.1  christos  * We don't lock this as we don't care about simultaneous updates.
    703  1.1  christos  *
    704  1.1  christos  * Note:
    705  1.3  christos  *      Both init_count and header->count can be UINT32_MAX.
    706  1.1  christos  *      The count on the returned rdataset however can't be as
    707  1.1  christos  *      that indicates that the database does not implement cyclic
    708  1.1  christos  *      processing.
    709  1.1  christos  */
    710  1.7  christos static atomic_uint_fast32_t init_count;
    711  1.1  christos 
    712  1.1  christos /*
    713  1.1  christos  * Locking
    714  1.1  christos  *
    715  1.1  christos  * If a routine is going to lock more than one lock in this module, then
    716  1.1  christos  * the locking must be done in the following order:
    717  1.1  christos  *
    718  1.1  christos  *      Tree Lock
    719  1.1  christos  *
    720  1.1  christos  *      Node Lock       (Only one from the set may be locked at one time by
    721  1.1  christos  *                       any caller)
    722  1.1  christos  *
    723  1.1  christos  *      Database Lock
    724  1.1  christos  *
    725  1.1  christos  * Failure to follow this hierarchy can result in deadlock.
    726  1.1  christos  */
    727  1.1  christos 
    728  1.1  christos /*
    729  1.1  christos  * Deleting Nodes
    730  1.1  christos  *
    731  1.1  christos  * For zone databases the node for the origin of the zone MUST NOT be deleted.
    732  1.1  christos  */
    733  1.1  christos 
    734  1.1  christos /*
    735  1.1  christos  * Debugging routines
    736  1.1  christos  */
    737  1.1  christos #ifdef DEBUG
    738  1.1  christos static void
    739  1.1  christos hexdump(const char *desc, unsigned char *data, size_t size) {
    740  1.1  christos 	char hexdump[BUFSIZ * 2 + 1];
    741  1.1  christos 	isc_buffer_t b;
    742  1.1  christos 	isc_region_t r;
    743  1.1  christos 	isc_result_t result;
    744  1.1  christos 	size_t bytes;
    745  1.1  christos 
    746  1.1  christos 	fprintf(stderr, "%s: ", desc);
    747  1.1  christos 	do {
    748  1.1  christos 		isc_buffer_init(&b, hexdump, sizeof(hexdump));
    749  1.1  christos 		r.base = data;
    750  1.1  christos 		r.length = bytes = (size > BUFSIZ) ? BUFSIZ : size;
    751  1.1  christos 		result = isc_hex_totext(&r, 0, "", &b);
    752  1.1  christos 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
    753  1.1  christos 		isc_buffer_putuint8(&b, 0);
    754  1.1  christos 		fprintf(stderr, "%s", hexdump);
    755  1.1  christos 		data += bytes;
    756  1.1  christos 		size -= bytes;
    757  1.1  christos 	} while (size > 0);
    758  1.1  christos 	fprintf(stderr, "\n");
    759  1.1  christos }
    760  1.7  christos #endif /* ifdef DEBUG */
    761  1.1  christos 
    762  1.5  christos /* Fixed RRSet helper macros */
    763  1.5  christos 
    764  1.5  christos #define DNS_RDATASET_LENGTH 2;
    765  1.5  christos 
    766  1.5  christos #if DNS_RDATASET_FIXED
    767  1.5  christos #define DNS_RDATASET_ORDER 2
    768  1.5  christos #define DNS_RDATASET_COUNT (count * 4)
    769  1.5  christos #else /* !DNS_RDATASET_FIXED */
    770  1.5  christos #define DNS_RDATASET_ORDER 0
    771  1.5  christos #define DNS_RDATASET_COUNT 0
    772  1.5  christos #endif /* DNS_RDATASET_FIXED */
    773  1.1  christos 
    774  1.1  christos /*
    775  1.1  christos  * DB Routines
    776  1.1  christos  */
    777  1.1  christos 
    778  1.1  christos static void
    779  1.1  christos attach(dns_db_t *source, dns_db_t **targetp) {
    780  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)source;
    781  1.1  christos 
    782  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
    783  1.1  christos 
    784  1.3  christos 	isc_refcount_increment(&rbtdb->references);
    785  1.1  christos 
    786  1.1  christos 	*targetp = source;
    787  1.1  christos }
    788  1.1  christos 
    789  1.1  christos static void
    790  1.1  christos free_rbtdb_callback(isc_task_t *task, isc_event_t *event) {
    791  1.1  christos 	dns_rbtdb_t *rbtdb = event->ev_arg;
    792  1.1  christos 
    793  1.1  christos 	UNUSED(task);
    794  1.1  christos 
    795  1.3  christos 	free_rbtdb(rbtdb, true, event);
    796  1.1  christos }
    797  1.1  christos 
    798  1.1  christos static void
    799  1.1  christos update_cachestats(dns_rbtdb_t *rbtdb, isc_result_t result) {
    800  1.1  christos 	INSIST(IS_CACHE(rbtdb));
    801  1.1  christos 
    802  1.7  christos 	if (rbtdb->cachestats == NULL) {
    803  1.1  christos 		return;
    804  1.7  christos 	}
    805  1.1  christos 
    806  1.1  christos 	switch (result) {
    807  1.1  christos 	case ISC_R_SUCCESS:
    808  1.1  christos 	case DNS_R_CNAME:
    809  1.1  christos 	case DNS_R_DNAME:
    810  1.1  christos 	case DNS_R_DELEGATION:
    811  1.1  christos 	case DNS_R_NCACHENXDOMAIN:
    812  1.1  christos 	case DNS_R_NCACHENXRRSET:
    813  1.1  christos 		isc_stats_increment(rbtdb->cachestats,
    814  1.1  christos 				    dns_cachestatscounter_hits);
    815  1.1  christos 		break;
    816  1.1  christos 	default:
    817  1.1  christos 		isc_stats_increment(rbtdb->cachestats,
    818  1.1  christos 				    dns_cachestatscounter_misses);
    819  1.1  christos 	}
    820  1.1  christos }
    821  1.1  christos 
    822  1.5  christos static bool
    823  1.5  christos do_stats(rdatasetheader_t *header) {
    824  1.5  christos 	return (EXISTS(header) &&
    825  1.5  christos 		(header->attributes & RDATASET_ATTR_STATCOUNT) != 0);
    826  1.5  christos }
    827  1.5  christos 
    828  1.1  christos static void
    829  1.1  christos update_rrsetstats(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
    830  1.7  christos 		  bool increment) {
    831  1.1  christos 	dns_rdatastatstype_t statattributes = 0;
    832  1.1  christos 	dns_rdatastatstype_t base = 0;
    833  1.1  christos 	dns_rdatastatstype_t type;
    834  1.1  christos 
    835  1.5  christos 	if (!do_stats(header)) {
    836  1.5  christos 		return;
    837  1.5  christos 	}
    838  1.5  christos 
    839  1.1  christos 	/* At the moment we count statistics only for cache DB */
    840  1.1  christos 	INSIST(IS_CACHE(rbtdb));
    841  1.1  christos 
    842  1.1  christos 	if (NEGATIVE(header)) {
    843  1.7  christos 		if (NXDOMAIN(header)) {
    844  1.1  christos 			statattributes = DNS_RDATASTATSTYPE_ATTR_NXDOMAIN;
    845  1.7  christos 		} else {
    846  1.1  christos 			statattributes = DNS_RDATASTATSTYPE_ATTR_NXRRSET;
    847  1.1  christos 			base = RBTDB_RDATATYPE_EXT(header->type);
    848  1.1  christos 		}
    849  1.5  christos 	} else {
    850  1.1  christos 		base = RBTDB_RDATATYPE_BASE(header->type);
    851  1.5  christos 	}
    852  1.1  christos 
    853  1.5  christos 	if (STALE(header)) {
    854  1.1  christos 		statattributes |= DNS_RDATASTATSTYPE_ATTR_STALE;
    855  1.5  christos 	}
    856  1.5  christos 	if (ANCIENT(header)) {
    857  1.5  christos 		statattributes |= DNS_RDATASTATSTYPE_ATTR_ANCIENT;
    858  1.5  christos 	}
    859  1.1  christos 
    860  1.1  christos 	type = DNS_RDATASTATSTYPE_VALUE(base, statattributes);
    861  1.5  christos 	if (increment) {
    862  1.1  christos 		dns_rdatasetstats_increment(rbtdb->rrsetstats, type);
    863  1.5  christos 	} else {
    864  1.1  christos 		dns_rdatasetstats_decrement(rbtdb->rrsetstats, type);
    865  1.5  christos 	}
    866  1.1  christos }
    867  1.1  christos 
    868  1.1  christos static void
    869  1.1  christos set_ttl(dns_rbtdb_t *rbtdb, rdatasetheader_t *header, dns_ttl_t newttl) {
    870  1.1  christos 	int idx;
    871  1.1  christos 	isc_heap_t *heap;
    872  1.1  christos 	dns_ttl_t oldttl;
    873  1.1  christos 
    874  1.1  christos 	if (!IS_CACHE(rbtdb)) {
    875  1.1  christos 		header->rdh_ttl = newttl;
    876  1.1  christos 		return;
    877  1.1  christos 	}
    878  1.1  christos 
    879  1.1  christos 	oldttl = header->rdh_ttl;
    880  1.1  christos 	header->rdh_ttl = newttl;
    881  1.1  christos 
    882  1.1  christos 	/*
    883  1.1  christos 	 * It's possible the rbtdb is not a cache.  If this is the case,
    884  1.1  christos 	 * we will not have a heap, and we move on.  If we do, though,
    885  1.1  christos 	 * we might need to adjust things.
    886  1.1  christos 	 */
    887  1.7  christos 	if (header->heap_index == 0 || newttl == oldttl) {
    888  1.1  christos 		return;
    889  1.7  christos 	}
    890  1.1  christos 	idx = header->node->locknum;
    891  1.7  christos 	if (rbtdb->heaps == NULL || rbtdb->heaps[idx] == NULL) {
    892  1.7  christos 		return;
    893  1.7  christos 	}
    894  1.1  christos 	heap = rbtdb->heaps[idx];
    895  1.1  christos 
    896  1.7  christos 	if (newttl < oldttl) {
    897  1.1  christos 		isc_heap_increased(heap, header->heap_index);
    898  1.7  christos 	} else {
    899  1.1  christos 		isc_heap_decreased(heap, header->heap_index);
    900  1.7  christos 	}
    901  1.1  christos }
    902  1.1  christos 
    903  1.1  christos /*%
    904  1.1  christos  * These functions allow the heap code to rank the priority of each
    905  1.3  christos  * element.  It returns true if v1 happens "sooner" than v2.
    906  1.1  christos  */
    907  1.3  christos static bool
    908  1.1  christos ttl_sooner(void *v1, void *v2) {
    909  1.1  christos 	rdatasetheader_t *h1 = v1;
    910  1.1  christos 	rdatasetheader_t *h2 = v2;
    911  1.1  christos 
    912  1.3  christos 	return (h1->rdh_ttl < h2->rdh_ttl);
    913  1.1  christos }
    914  1.1  christos 
    915  1.7  christos /*%
    916  1.7  christos  * Return which RRset should be resigned sooner.  If the RRsets have the
    917  1.7  christos  * same signing time, prefer the other RRset over the SOA RRset.
    918  1.7  christos  */
    919  1.3  christos static bool
    920  1.1  christos resign_sooner(void *v1, void *v2) {
    921  1.1  christos 	rdatasetheader_t *h1 = v1;
    922  1.1  christos 	rdatasetheader_t *h2 = v2;
    923  1.1  christos 
    924  1.3  christos 	return (h1->resign < h2->resign ||
    925  1.7  christos 		(h1->resign == h2->resign && h1->resign_lsb < h2->resign_lsb) ||
    926  1.7  christos 		(h1->resign == h2->resign && h1->resign_lsb == h2->resign_lsb &&
    927  1.7  christos 		 h2->type == RBTDB_RDATATYPE_SIGSOA));
    928  1.1  christos }
    929  1.1  christos 
    930  1.1  christos /*%
    931  1.1  christos  * This function sets the heap index into the header.
    932  1.1  christos  */
    933  1.1  christos static void
    934  1.1  christos set_index(void *what, unsigned int idx) {
    935  1.1  christos 	rdatasetheader_t *h = what;
    936  1.1  christos 
    937  1.1  christos 	h->heap_index = idx;
    938  1.1  christos }
    939  1.1  christos 
    940  1.1  christos /*%
    941  1.1  christos  * Work out how many nodes can be deleted in the time between two
    942  1.1  christos  * requests to the nameserver.  Smooth the resulting number and use it
    943  1.1  christos  * as a estimate for the number of nodes to be deleted in the next
    944  1.1  christos  * iteration.
    945  1.1  christos  */
    946  1.1  christos static unsigned int
    947  1.1  christos adjust_quantum(unsigned int old, isc_time_t *start) {
    948  1.7  christos 	unsigned int pps = dns_pps; /* packets per second */
    949  1.1  christos 	unsigned int interval;
    950  1.3  christos 	uint64_t usecs;
    951  1.1  christos 	isc_time_t end;
    952  1.1  christos 	unsigned int nodes;
    953  1.1  christos 
    954  1.7  christos 	if (pps < 100) {
    955  1.1  christos 		pps = 100;
    956  1.7  christos 	}
    957  1.1  christos 	isc_time_now(&end);
    958  1.1  christos 
    959  1.7  christos 	interval = 1000000 / pps; /* interval in usec */
    960  1.7  christos 	if (interval == 0) {
    961  1.1  christos 		interval = 1;
    962  1.7  christos 	}
    963  1.1  christos 	usecs = isc_time_microdiff(&end, start);
    964  1.1  christos 	if (usecs == 0) {
    965  1.1  christos 		/*
    966  1.1  christos 		 * We were unable to measure the amount of time taken.
    967  1.1  christos 		 * Double the nodes deleted next time.
    968  1.1  christos 		 */
    969  1.1  christos 		old *= 2;
    970  1.7  christos 		if (old > 1000) {
    971  1.1  christos 			old = 1000;
    972  1.7  christos 		}
    973  1.1  christos 		return (old);
    974  1.1  christos 	}
    975  1.1  christos 	nodes = old * interval;
    976  1.1  christos 	nodes /= (unsigned int)usecs;
    977  1.7  christos 	if (nodes == 0) {
    978  1.1  christos 		nodes = 1;
    979  1.7  christos 	} else if (nodes > 1000) {
    980  1.1  christos 		nodes = 1000;
    981  1.7  christos 	}
    982  1.1  christos 
    983  1.1  christos 	/* Smooth */
    984  1.1  christos 	nodes = (nodes + old * 3) / 4;
    985  1.1  christos 
    986  1.7  christos 	if (nodes != old) {
    987  1.1  christos 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
    988  1.1  christos 			      DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
    989  1.1  christos 			      "adjust_quantum: old=%d, new=%d", old, nodes);
    990  1.7  christos 	}
    991  1.1  christos 
    992  1.1  christos 	return (nodes);
    993  1.1  christos }
    994  1.1  christos 
    995  1.1  christos static void
    996  1.3  christos free_rbtdb(dns_rbtdb_t *rbtdb, bool log, isc_event_t *event) {
    997  1.1  christos 	unsigned int i;
    998  1.1  christos 	isc_result_t result;
    999  1.1  christos 	char buf[DNS_NAME_FORMATSIZE];
   1000  1.1  christos 	dns_rbt_t **treep;
   1001  1.1  christos 	isc_time_t start;
   1002  1.1  christos 	dns_dbonupdatelistener_t *listener, *listener_next;
   1003  1.1  christos 
   1004  1.7  christos 	if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in) {
   1005  1.3  christos 		overmem((dns_db_t *)rbtdb, (bool)-1);
   1006  1.7  christos 	}
   1007  1.1  christos 
   1008  1.1  christos 	REQUIRE(rbtdb->current_version != NULL || EMPTY(rbtdb->open_versions));
   1009  1.1  christos 	REQUIRE(rbtdb->future_version == NULL);
   1010  1.1  christos 
   1011  1.1  christos 	if (rbtdb->current_version != NULL) {
   1012  1.3  christos 		INSIST(isc_refcount_decrement(
   1013  1.7  christos 			       &rbtdb->current_version->references) == 1);
   1014  1.3  christos 
   1015  1.1  christos 		UNLINK(rbtdb->open_versions, rbtdb->current_version, link);
   1016  1.1  christos 		isc_rwlock_destroy(&rbtdb->current_version->glue_rwlock);
   1017  1.1  christos 		isc_refcount_destroy(&rbtdb->current_version->references);
   1018  1.1  christos 		isc_rwlock_destroy(&rbtdb->current_version->rwlock);
   1019  1.1  christos 		isc_mem_put(rbtdb->common.mctx, rbtdb->current_version,
   1020  1.1  christos 			    sizeof(rbtdb_version_t));
   1021  1.1  christos 	}
   1022  1.1  christos 
   1023  1.1  christos 	/*
   1024  1.1  christos 	 * We assume the number of remaining dead nodes is reasonably small;
   1025  1.1  christos 	 * the overhead of unlinking all nodes here should be negligible.
   1026  1.1  christos 	 */
   1027  1.1  christos 	for (i = 0; i < rbtdb->node_lock_count; i++) {
   1028  1.1  christos 		dns_rbtnode_t *node;
   1029  1.1  christos 
   1030  1.1  christos 		node = ISC_LIST_HEAD(rbtdb->deadnodes[i]);
   1031  1.1  christos 		while (node != NULL) {
   1032  1.1  christos 			ISC_LIST_UNLINK(rbtdb->deadnodes[i], node, deadlink);
   1033  1.1  christos 			node = ISC_LIST_HEAD(rbtdb->deadnodes[i]);
   1034  1.1  christos 		}
   1035  1.1  christos 	}
   1036  1.1  christos 
   1037  1.7  christos 	if (event == NULL) {
   1038  1.1  christos 		rbtdb->quantum = (rbtdb->task != NULL) ? 100 : 0;
   1039  1.7  christos 	}
   1040  1.1  christos 
   1041  1.1  christos 	for (;;) {
   1042  1.1  christos 		/*
   1043  1.1  christos 		 * pick the next tree to (start to) destroy
   1044  1.1  christos 		 */
   1045  1.1  christos 		treep = &rbtdb->tree;
   1046  1.1  christos 		if (*treep == NULL) {
   1047  1.1  christos 			treep = &rbtdb->nsec;
   1048  1.1  christos 			if (*treep == NULL) {
   1049  1.1  christos 				treep = &rbtdb->nsec3;
   1050  1.1  christos 				/*
   1051  1.1  christos 				 * we're finished after clear cutting
   1052  1.1  christos 				 */
   1053  1.7  christos 				if (*treep == NULL) {
   1054  1.1  christos 					break;
   1055  1.7  christos 				}
   1056  1.1  christos 			}
   1057  1.1  christos 		}
   1058  1.1  christos 
   1059  1.1  christos 		isc_time_now(&start);
   1060  1.1  christos 		result = dns_rbt_destroy2(treep, rbtdb->quantum);
   1061  1.1  christos 		if (result == ISC_R_QUOTA) {
   1062  1.1  christos 			INSIST(rbtdb->task != NULL);
   1063  1.7  christos 			if (rbtdb->quantum != 0) {
   1064  1.1  christos 				rbtdb->quantum = adjust_quantum(rbtdb->quantum,
   1065  1.1  christos 								&start);
   1066  1.7  christos 			}
   1067  1.7  christos 			if (event == NULL) {
   1068  1.7  christos 				event = isc_event_allocate(
   1069  1.7  christos 					rbtdb->common.mctx, NULL,
   1070  1.7  christos 					DNS_EVENT_FREESTORAGE,
   1071  1.7  christos 					free_rbtdb_callback, rbtdb,
   1072  1.7  christos 					sizeof(isc_event_t));
   1073  1.7  christos 			}
   1074  1.1  christos 			isc_task_send(rbtdb->task, &event);
   1075  1.1  christos 			return;
   1076  1.1  christos 		}
   1077  1.1  christos 		INSIST(result == ISC_R_SUCCESS && *treep == NULL);
   1078  1.1  christos 	}
   1079  1.1  christos 
   1080  1.7  christos 	if (event != NULL) {
   1081  1.1  christos 		isc_event_free(&event);
   1082  1.7  christos 	}
   1083  1.1  christos 	if (log) {
   1084  1.7  christos 		if (dns_name_dynamic(&rbtdb->common.origin)) {
   1085  1.1  christos 			dns_name_format(&rbtdb->common.origin, buf,
   1086  1.1  christos 					sizeof(buf));
   1087  1.7  christos 		} else {
   1088  1.1  christos 			strlcpy(buf, "<UNKNOWN>", sizeof(buf));
   1089  1.7  christos 		}
   1090  1.1  christos 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   1091  1.1  christos 			      DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
   1092  1.1  christos 			      "done free_rbtdb(%s)", buf);
   1093  1.1  christos 	}
   1094  1.7  christos 	if (dns_name_dynamic(&rbtdb->common.origin)) {
   1095  1.1  christos 		dns_name_free(&rbtdb->common.origin, rbtdb->common.mctx);
   1096  1.7  christos 	}
   1097  1.1  christos 	for (i = 0; i < rbtdb->node_lock_count; i++) {
   1098  1.1  christos 		isc_refcount_destroy(&rbtdb->node_locks[i].references);
   1099  1.1  christos 		NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
   1100  1.1  christos 	}
   1101  1.1  christos 
   1102  1.1  christos 	/*
   1103  1.1  christos 	 * Clean up LRU / re-signing order lists.
   1104  1.1  christos 	 */
   1105  1.1  christos 	if (rbtdb->rdatasets != NULL) {
   1106  1.7  christos 		for (i = 0; i < rbtdb->node_lock_count; i++) {
   1107  1.1  christos 			INSIST(ISC_LIST_EMPTY(rbtdb->rdatasets[i]));
   1108  1.7  christos 		}
   1109  1.1  christos 		isc_mem_put(rbtdb->common.mctx, rbtdb->rdatasets,
   1110  1.1  christos 			    rbtdb->node_lock_count *
   1111  1.7  christos 				    sizeof(rdatasetheaderlist_t));
   1112  1.1  christos 	}
   1113  1.1  christos 	/*
   1114  1.1  christos 	 * Clean up dead node buckets.
   1115  1.1  christos 	 */
   1116  1.1  christos 	if (rbtdb->deadnodes != NULL) {
   1117  1.7  christos 		for (i = 0; i < rbtdb->node_lock_count; i++) {
   1118  1.1  christos 			INSIST(ISC_LIST_EMPTY(rbtdb->deadnodes[i]));
   1119  1.7  christos 		}
   1120  1.1  christos 		isc_mem_put(rbtdb->common.mctx, rbtdb->deadnodes,
   1121  1.7  christos 			    rbtdb->node_lock_count * sizeof(rbtnodelist_t));
   1122  1.1  christos 	}
   1123  1.1  christos 	/*
   1124  1.1  christos 	 * Clean up heap objects.
   1125  1.1  christos 	 */
   1126  1.1  christos 	if (rbtdb->heaps != NULL) {
   1127  1.7  christos 		for (i = 0; i < rbtdb->node_lock_count; i++) {
   1128  1.1  christos 			isc_heap_destroy(&rbtdb->heaps[i]);
   1129  1.7  christos 		}
   1130  1.1  christos 		isc_mem_put(rbtdb->hmctx, rbtdb->heaps,
   1131  1.1  christos 			    rbtdb->node_lock_count * sizeof(isc_heap_t *));
   1132  1.1  christos 	}
   1133  1.1  christos 
   1134  1.7  christos 	if (rbtdb->rrsetstats != NULL) {
   1135  1.1  christos 		dns_stats_detach(&rbtdb->rrsetstats);
   1136  1.7  christos 	}
   1137  1.7  christos 	if (rbtdb->cachestats != NULL) {
   1138  1.1  christos 		isc_stats_detach(&rbtdb->cachestats);
   1139  1.7  christos 	}
   1140  1.7  christos 	if (rbtdb->gluecachestats != NULL) {
   1141  1.1  christos 		isc_stats_detach(&rbtdb->gluecachestats);
   1142  1.7  christos 	}
   1143  1.1  christos 
   1144  1.1  christos 	isc_mem_put(rbtdb->common.mctx, rbtdb->node_locks,
   1145  1.1  christos 		    rbtdb->node_lock_count * sizeof(rbtdb_nodelock_t));
   1146  1.1  christos 	isc_rwlock_destroy(&rbtdb->tree_lock);
   1147  1.1  christos 	isc_refcount_destroy(&rbtdb->references);
   1148  1.7  christos 	if (rbtdb->task != NULL) {
   1149  1.1  christos 		isc_task_detach(&rbtdb->task);
   1150  1.7  christos 	}
   1151  1.1  christos 
   1152  1.1  christos 	RBTDB_DESTROYLOCK(&rbtdb->lock);
   1153  1.1  christos 	rbtdb->common.magic = 0;
   1154  1.1  christos 	rbtdb->common.impmagic = 0;
   1155  1.1  christos 	isc_mem_detach(&rbtdb->hmctx);
   1156  1.1  christos 
   1157  1.7  christos 	if (rbtdb->mmap_location != NULL) {
   1158  1.7  christos 		isc_file_munmap(rbtdb->mmap_location, (size_t)rbtdb->mmap_size);
   1159  1.7  christos 	}
   1160  1.1  christos 
   1161  1.1  christos 	for (listener = ISC_LIST_HEAD(rbtdb->common.update_listeners);
   1162  1.7  christos 	     listener != NULL; listener = listener_next)
   1163  1.1  christos 	{
   1164  1.1  christos 		listener_next = ISC_LIST_NEXT(listener, link);
   1165  1.1  christos 		ISC_LIST_UNLINK(rbtdb->common.update_listeners, listener, link);
   1166  1.1  christos 		isc_mem_put(rbtdb->common.mctx, listener,
   1167  1.1  christos 			    sizeof(dns_dbonupdatelistener_t));
   1168  1.1  christos 	}
   1169  1.1  christos 
   1170  1.1  christos 	isc_mem_putanddetach(&rbtdb->common.mctx, rbtdb, sizeof(*rbtdb));
   1171  1.1  christos }
   1172  1.1  christos 
   1173  1.1  christos static inline void
   1174  1.1  christos maybe_free_rbtdb(dns_rbtdb_t *rbtdb) {
   1175  1.3  christos 	bool want_free = false;
   1176  1.1  christos 	unsigned int i;
   1177  1.1  christos 	unsigned int inactive = 0;
   1178  1.1  christos 
   1179  1.1  christos 	/* XXX check for open versions here */
   1180  1.1  christos 
   1181  1.7  christos 	if (rbtdb->soanode != NULL) {
   1182  1.1  christos 		dns_db_detachnode((dns_db_t *)rbtdb, &rbtdb->soanode);
   1183  1.7  christos 	}
   1184  1.7  christos 	if (rbtdb->nsnode != NULL) {
   1185  1.1  christos 		dns_db_detachnode((dns_db_t *)rbtdb, &rbtdb->nsnode);
   1186  1.7  christos 	}
   1187  1.1  christos 
   1188  1.1  christos 	/*
   1189  1.1  christos 	 * The current version's glue table needs to be freed early
   1190  1.1  christos 	 * so the nodes are dereferenced before we check the active
   1191  1.1  christos 	 * node count below.
   1192  1.1  christos 	 */
   1193  1.1  christos 	if (rbtdb->current_version != NULL) {
   1194  1.1  christos 		free_gluetable(rbtdb->current_version);
   1195  1.1  christos 	}
   1196  1.1  christos 
   1197  1.1  christos 	/*
   1198  1.1  christos 	 * Even though there are no external direct references, there still
   1199  1.1  christos 	 * may be nodes in use.
   1200  1.1  christos 	 */
   1201  1.1  christos 	for (i = 0; i < rbtdb->node_lock_count; i++) {
   1202  1.1  christos 		NODE_LOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_write);
   1203  1.3  christos 		rbtdb->node_locks[i].exiting = true;
   1204  1.1  christos 		NODE_UNLOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_write);
   1205  1.1  christos 		if (isc_refcount_current(&rbtdb->node_locks[i].references) == 0)
   1206  1.1  christos 		{
   1207  1.1  christos 			inactive++;
   1208  1.1  christos 		}
   1209  1.1  christos 	}
   1210  1.1  christos 
   1211  1.1  christos 	if (inactive != 0) {
   1212  1.1  christos 		RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
   1213  1.1  christos 		rbtdb->active -= inactive;
   1214  1.1  christos 		if (rbtdb->active == 0) {
   1215  1.3  christos 			want_free = true;
   1216  1.1  christos 		}
   1217  1.1  christos 		RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   1218  1.1  christos 		if (want_free) {
   1219  1.1  christos 			char buf[DNS_NAME_FORMATSIZE];
   1220  1.1  christos 			if (dns_name_dynamic(&rbtdb->common.origin)) {
   1221  1.1  christos 				dns_name_format(&rbtdb->common.origin, buf,
   1222  1.1  christos 						sizeof(buf));
   1223  1.1  christos 			} else {
   1224  1.1  christos 				strlcpy(buf, "<UNKNOWN>", sizeof(buf));
   1225  1.1  christos 			}
   1226  1.1  christos 			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   1227  1.1  christos 				      DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
   1228  1.1  christos 				      "calling free_rbtdb(%s)", buf);
   1229  1.3  christos 			free_rbtdb(rbtdb, true, NULL);
   1230  1.1  christos 		}
   1231  1.1  christos 	}
   1232  1.1  christos }
   1233  1.1  christos 
   1234  1.1  christos static void
   1235  1.1  christos detach(dns_db_t **dbp) {
   1236  1.3  christos 	REQUIRE(dbp != NULL && VALID_RBTDB((dns_rbtdb_t *)(*dbp)));
   1237  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(*dbp);
   1238  1.3  christos 	*dbp = NULL;
   1239  1.1  christos 
   1240  1.3  christos 	if (isc_refcount_decrement(&rbtdb->references) == 1) {
   1241  1.1  christos 		maybe_free_rbtdb(rbtdb);
   1242  1.3  christos 	}
   1243  1.1  christos }
   1244  1.1  christos 
   1245  1.1  christos static void
   1246  1.1  christos currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
   1247  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   1248  1.1  christos 	rbtdb_version_t *version;
   1249  1.1  christos 
   1250  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   1251  1.1  christos 
   1252  1.1  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   1253  1.1  christos 	version = rbtdb->current_version;
   1254  1.3  christos 	isc_refcount_increment(&version->references);
   1255  1.1  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   1256  1.1  christos 
   1257  1.1  christos 	*versionp = (dns_dbversion_t *)version;
   1258  1.1  christos }
   1259  1.1  christos 
   1260  1.1  christos static inline rbtdb_version_t *
   1261  1.1  christos allocate_version(isc_mem_t *mctx, rbtdb_serial_t serial,
   1262  1.7  christos 		 unsigned int references, bool writer) {
   1263  1.1  christos 	isc_result_t result;
   1264  1.1  christos 	rbtdb_version_t *version;
   1265  1.1  christos 	size_t i;
   1266  1.1  christos 
   1267  1.1  christos 	version = isc_mem_get(mctx, sizeof(*version));
   1268  1.1  christos 	version->serial = serial;
   1269  1.3  christos 
   1270  1.3  christos 	isc_refcount_init(&version->references, references);
   1271  1.3  christos 
   1272  1.1  christos 	result = isc_rwlock_init(&version->glue_rwlock, 0, 0);
   1273  1.1  christos 	if (result != ISC_R_SUCCESS) {
   1274  1.1  christos 		isc_refcount_destroy(&version->references);
   1275  1.1  christos 		isc_mem_put(mctx, version, sizeof(*version));
   1276  1.1  christos 		return (NULL);
   1277  1.1  christos 	}
   1278  1.1  christos 
   1279  1.1  christos 	version->glue_table_size = RBTDB_GLUE_TABLE_INIT_SIZE;
   1280  1.1  christos 	version->glue_table_nodecount = 0U;
   1281  1.7  christos 	version->glue_table = isc_mem_get(mctx, (version->glue_table_size *
   1282  1.7  christos 						 sizeof(*version->glue_table)));
   1283  1.1  christos 
   1284  1.1  christos 	version->writer = writer;
   1285  1.3  christos 	version->commit_ok = false;
   1286  1.1  christos 	ISC_LIST_INIT(version->changed_list);
   1287  1.1  christos 	ISC_LIST_INIT(version->resigned_list);
   1288  1.1  christos 	ISC_LINK_INIT(version, link);
   1289  1.1  christos 
   1290  1.7  christos 	for (i = 0; i < version->glue_table_size; i++) {
   1291  1.1  christos 		version->glue_table[i] = NULL;
   1292  1.7  christos 	}
   1293  1.1  christos 
   1294  1.1  christos 	return (version);
   1295  1.1  christos }
   1296  1.1  christos 
   1297  1.1  christos static isc_result_t
   1298  1.1  christos newversion(dns_db_t *db, dns_dbversion_t **versionp) {
   1299  1.1  christos 	isc_result_t result;
   1300  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   1301  1.1  christos 	rbtdb_version_t *version;
   1302  1.1  christos 
   1303  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   1304  1.1  christos 	REQUIRE(versionp != NULL && *versionp == NULL);
   1305  1.1  christos 	REQUIRE(rbtdb->future_version == NULL);
   1306  1.1  christos 
   1307  1.1  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
   1308  1.7  christos 	RUNTIME_CHECK(rbtdb->next_serial != 0); /* XXX Error? */
   1309  1.1  christos 	version = allocate_version(rbtdb->common.mctx, rbtdb->next_serial, 1,
   1310  1.3  christos 				   true);
   1311  1.1  christos 	if (version != NULL) {
   1312  1.1  christos 		version->rbtdb = rbtdb;
   1313  1.3  christos 		version->commit_ok = true;
   1314  1.1  christos 		version->secure = rbtdb->current_version->secure;
   1315  1.1  christos 		version->havensec3 = rbtdb->current_version->havensec3;
   1316  1.1  christos 		if (version->havensec3) {
   1317  1.1  christos 			version->flags = rbtdb->current_version->flags;
   1318  1.1  christos 			version->iterations =
   1319  1.1  christos 				rbtdb->current_version->iterations;
   1320  1.1  christos 			version->hash = rbtdb->current_version->hash;
   1321  1.1  christos 			version->salt_length =
   1322  1.1  christos 				rbtdb->current_version->salt_length;
   1323  1.1  christos 			memmove(version->salt, rbtdb->current_version->salt,
   1324  1.1  christos 				version->salt_length);
   1325  1.1  christos 		} else {
   1326  1.1  christos 			version->flags = 0;
   1327  1.1  christos 			version->iterations = 0;
   1328  1.1  christos 			version->hash = 0;
   1329  1.1  christos 			version->salt_length = 0;
   1330  1.1  christos 			memset(version->salt, 0, sizeof(version->salt));
   1331  1.1  christos 		}
   1332  1.1  christos 		result = isc_rwlock_init(&version->rwlock, 0, 0);
   1333  1.1  christos 		if (result != ISC_R_SUCCESS) {
   1334  1.1  christos 			free_gluetable(version);
   1335  1.1  christos 			isc_rwlock_destroy(&version->glue_rwlock);
   1336  1.1  christos 			isc_refcount_destroy(&version->references);
   1337  1.1  christos 			isc_mem_put(rbtdb->common.mctx, version,
   1338  1.1  christos 				    sizeof(*version));
   1339  1.1  christos 			version = NULL;
   1340  1.1  christos 		} else {
   1341  1.1  christos 			RWLOCK(&rbtdb->current_version->rwlock,
   1342  1.1  christos 			       isc_rwlocktype_read);
   1343  1.1  christos 			version->records = rbtdb->current_version->records;
   1344  1.1  christos 			version->bytes = rbtdb->current_version->bytes;
   1345  1.1  christos 			RWUNLOCK(&rbtdb->current_version->rwlock,
   1346  1.1  christos 				 isc_rwlocktype_read);
   1347  1.1  christos 			rbtdb->next_serial++;
   1348  1.1  christos 			rbtdb->future_version = version;
   1349  1.1  christos 		}
   1350  1.7  christos 	} else {
   1351  1.1  christos 		result = ISC_R_NOMEMORY;
   1352  1.7  christos 	}
   1353  1.1  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   1354  1.1  christos 
   1355  1.7  christos 	if (version == NULL) {
   1356  1.1  christos 		return (result);
   1357  1.7  christos 	}
   1358  1.1  christos 
   1359  1.1  christos 	*versionp = version;
   1360  1.1  christos 
   1361  1.1  christos 	return (ISC_R_SUCCESS);
   1362  1.1  christos }
   1363  1.1  christos 
   1364  1.1  christos static void
   1365  1.1  christos attachversion(dns_db_t *db, dns_dbversion_t *source,
   1366  1.7  christos 	      dns_dbversion_t **targetp) {
   1367  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   1368  1.1  christos 	rbtdb_version_t *rbtversion = source;
   1369  1.1  christos 
   1370  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   1371  1.1  christos 	INSIST(rbtversion != NULL && rbtversion->rbtdb == rbtdb);
   1372  1.1  christos 
   1373  1.3  christos 	isc_refcount_increment(&rbtversion->references);
   1374  1.1  christos 
   1375  1.1  christos 	*targetp = rbtversion;
   1376  1.1  christos }
   1377  1.1  christos 
   1378  1.1  christos static rbtdb_changed_t *
   1379  1.7  christos add_changed(dns_rbtdb_t *rbtdb, rbtdb_version_t *version, dns_rbtnode_t *node) {
   1380  1.1  christos 	rbtdb_changed_t *changed;
   1381  1.1  christos 
   1382  1.1  christos 	/*
   1383  1.1  christos 	 * Caller must be holding the node lock if its reference must be
   1384  1.1  christos 	 * protected by the lock.
   1385  1.1  christos 	 */
   1386  1.1  christos 
   1387  1.1  christos 	changed = isc_mem_get(rbtdb->common.mctx, sizeof(*changed));
   1388  1.1  christos 
   1389  1.1  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
   1390  1.1  christos 
   1391  1.1  christos 	REQUIRE(version->writer);
   1392  1.1  christos 
   1393  1.1  christos 	if (changed != NULL) {
   1394  1.3  christos 		isc_refcount_increment(&node->references);
   1395  1.1  christos 		changed->node = node;
   1396  1.3  christos 		changed->dirty = false;
   1397  1.1  christos 		ISC_LIST_INITANDAPPEND(version->changed_list, changed, link);
   1398  1.7  christos 	} else {
   1399  1.3  christos 		version->commit_ok = false;
   1400  1.7  christos 	}
   1401  1.1  christos 
   1402  1.1  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   1403  1.1  christos 
   1404  1.1  christos 	return (changed);
   1405  1.1  christos }
   1406  1.1  christos 
   1407  1.1  christos static inline void
   1408  1.1  christos free_noqname(isc_mem_t *mctx, struct noqname **noqname) {
   1409  1.7  christos 	if (dns_name_dynamic(&(*noqname)->name)) {
   1410  1.1  christos 		dns_name_free(&(*noqname)->name, mctx);
   1411  1.7  christos 	}
   1412  1.7  christos 	if ((*noqname)->neg != NULL) {
   1413  1.1  christos 		isc_mem_put(mctx, (*noqname)->neg,
   1414  1.1  christos 			    dns_rdataslab_size((*noqname)->neg, 0));
   1415  1.7  christos 	}
   1416  1.7  christos 	if ((*noqname)->negsig != NULL) {
   1417  1.1  christos 		isc_mem_put(mctx, (*noqname)->negsig,
   1418  1.1  christos 			    dns_rdataslab_size((*noqname)->negsig, 0));
   1419  1.7  christos 	}
   1420  1.1  christos 	isc_mem_put(mctx, *noqname, sizeof(**noqname));
   1421  1.1  christos 	*noqname = NULL;
   1422  1.1  christos }
   1423  1.1  christos 
   1424  1.1  christos static inline void
   1425  1.1  christos init_rdataset(dns_rbtdb_t *rbtdb, rdatasetheader_t *h) {
   1426  1.1  christos 	ISC_LINK_INIT(h, link);
   1427  1.1  christos 	h->heap_index = 0;
   1428  1.1  christos 	h->is_mmapped = 0;
   1429  1.1  christos 	h->next_is_relative = 0;
   1430  1.1  christos 	h->node_is_relative = 0;
   1431  1.1  christos 
   1432  1.1  christos #if TRACE_HEADER
   1433  1.7  christos 	if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in) {
   1434  1.1  christos 		fprintf(stderr, "initialized header: %p\n", h);
   1435  1.7  christos 	}
   1436  1.7  christos #else  /* if TRACE_HEADER */
   1437  1.1  christos 	UNUSED(rbtdb);
   1438  1.7  christos #endif /* if TRACE_HEADER */
   1439  1.1  christos }
   1440  1.1  christos 
   1441  1.1  christos /*
   1442  1.1  christos  * Update the copied values of 'next' and 'node' if they are relative.
   1443  1.1  christos  */
   1444  1.1  christos static void
   1445  1.1  christos update_newheader(rdatasetheader_t *newh, rdatasetheader_t *old) {
   1446  1.1  christos 	char *p;
   1447  1.1  christos 
   1448  1.1  christos 	if (old->next_is_relative) {
   1449  1.7  christos 		p = (char *)old;
   1450  1.1  christos 		p += (uintptr_t)old->next;
   1451  1.1  christos 		newh->next = (rdatasetheader_t *)p;
   1452  1.1  christos 	}
   1453  1.1  christos 	if (old->node_is_relative) {
   1454  1.7  christos 		p = (char *)old;
   1455  1.1  christos 		p += (uintptr_t)old->node;
   1456  1.1  christos 		newh->node = (dns_rbtnode_t *)p;
   1457  1.1  christos 	}
   1458  1.1  christos 	if (CASESET(old)) {
   1459  1.3  christos 		uint16_t attr;
   1460  1.1  christos 
   1461  1.1  christos 		memmove(newh->upper, old->upper, sizeof(old->upper));
   1462  1.7  christos 		attr = old->attributes &
   1463  1.7  christos 		       (RDATASET_ATTR_CASESET | RDATASET_ATTR_CASEFULLYLOWER);
   1464  1.1  christos 		newh->attributes |= attr;
   1465  1.1  christos 	}
   1466  1.1  christos }
   1467  1.1  christos 
   1468  1.1  christos static inline rdatasetheader_t *
   1469  1.1  christos new_rdataset(dns_rbtdb_t *rbtdb, isc_mem_t *mctx) {
   1470  1.1  christos 	rdatasetheader_t *h;
   1471  1.1  christos 
   1472  1.1  christos 	h = isc_mem_get(mctx, sizeof(*h));
   1473  1.1  christos 
   1474  1.1  christos #if TRACE_HEADER
   1475  1.7  christos 	if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in) {
   1476  1.1  christos 		fprintf(stderr, "allocated header: %p\n", h);
   1477  1.7  christos 	}
   1478  1.7  christos #endif /* if TRACE_HEADER */
   1479  1.1  christos 	memset(h->upper, 0xeb, sizeof(h->upper));
   1480  1.1  christos 	init_rdataset(rbtdb, h);
   1481  1.1  christos 	h->rdh_ttl = 0;
   1482  1.1  christos 	return (h);
   1483  1.1  christos }
   1484  1.1  christos 
   1485  1.1  christos static inline void
   1486  1.1  christos free_rdataset(dns_rbtdb_t *rbtdb, isc_mem_t *mctx, rdatasetheader_t *rdataset) {
   1487  1.1  christos 	unsigned int size;
   1488  1.1  christos 	int idx;
   1489  1.1  christos 
   1490  1.5  christos 	update_rrsetstats(rbtdb, rdataset, false);
   1491  1.1  christos 
   1492  1.1  christos 	idx = rdataset->node->locknum;
   1493  1.1  christos 	if (ISC_LINK_LINKED(rdataset, link)) {
   1494  1.1  christos 		INSIST(IS_CACHE(rbtdb));
   1495  1.1  christos 		ISC_LIST_UNLINK(rbtdb->rdatasets[idx], rdataset, link);
   1496  1.1  christos 	}
   1497  1.1  christos 
   1498  1.7  christos 	if (rdataset->heap_index != 0) {
   1499  1.1  christos 		isc_heap_delete(rbtdb->heaps[idx], rdataset->heap_index);
   1500  1.7  christos 	}
   1501  1.1  christos 	rdataset->heap_index = 0;
   1502  1.1  christos 
   1503  1.7  christos 	if (rdataset->noqname != NULL) {
   1504  1.1  christos 		free_noqname(mctx, &rdataset->noqname);
   1505  1.7  christos 	}
   1506  1.7  christos 	if (rdataset->closest != NULL) {
   1507  1.1  christos 		free_noqname(mctx, &rdataset->closest);
   1508  1.7  christos 	}
   1509  1.1  christos 
   1510  1.7  christos 	if (NONEXISTENT(rdataset)) {
   1511  1.1  christos 		size = sizeof(*rdataset);
   1512  1.7  christos 	} else {
   1513  1.1  christos 		size = dns_rdataslab_size((unsigned char *)rdataset,
   1514  1.1  christos 					  sizeof(*rdataset));
   1515  1.7  christos 	}
   1516  1.1  christos 
   1517  1.7  christos 	if (rdataset->is_mmapped == 1) {
   1518  1.1  christos 		return;
   1519  1.7  christos 	}
   1520  1.1  christos 
   1521  1.1  christos 	isc_mem_put(mctx, rdataset, size);
   1522  1.1  christos }
   1523  1.1  christos 
   1524  1.1  christos static inline void
   1525  1.1  christos rollback_node(dns_rbtnode_t *node, rbtdb_serial_t serial) {
   1526  1.1  christos 	rdatasetheader_t *header, *dcurrent;
   1527  1.3  christos 	bool make_dirty = false;
   1528  1.1  christos 
   1529  1.1  christos 	/*
   1530  1.1  christos 	 * Caller must hold the node lock.
   1531  1.1  christos 	 */
   1532  1.1  christos 
   1533  1.1  christos 	/*
   1534  1.1  christos 	 * We set the IGNORE attribute on rdatasets with serial number
   1535  1.1  christos 	 * 'serial'.  When the reference count goes to zero, these rdatasets
   1536  1.1  christos 	 * will be cleaned up; until that time, they will be ignored.
   1537  1.1  christos 	 */
   1538  1.1  christos 	for (header = node->data; header != NULL; header = header->next) {
   1539  1.1  christos 		if (header->serial == serial) {
   1540  1.1  christos 			header->attributes |= RDATASET_ATTR_IGNORE;
   1541  1.3  christos 			make_dirty = true;
   1542  1.1  christos 		}
   1543  1.7  christos 		for (dcurrent = header->down; dcurrent != NULL;
   1544  1.1  christos 		     dcurrent = dcurrent->down) {
   1545  1.1  christos 			if (dcurrent->serial == serial) {
   1546  1.1  christos 				dcurrent->attributes |= RDATASET_ATTR_IGNORE;
   1547  1.3  christos 				make_dirty = true;
   1548  1.1  christos 			}
   1549  1.1  christos 		}
   1550  1.1  christos 	}
   1551  1.7  christos 	if (make_dirty) {
   1552  1.1  christos 		node->dirty = 1;
   1553  1.7  christos 	}
   1554  1.1  christos }
   1555  1.1  christos 
   1556  1.1  christos static inline void
   1557  1.1  christos mark_header_ancient(dns_rbtdb_t *rbtdb, rdatasetheader_t *header) {
   1558  1.1  christos 	/*
   1559  1.1  christos 	 * If we are already ancient there is nothing to do.
   1560  1.1  christos 	 */
   1561  1.5  christos 	if (ANCIENT(header)) {
   1562  1.1  christos 		return;
   1563  1.5  christos 	}
   1564  1.5  christos 
   1565  1.5  christos 	/*
   1566  1.5  christos 	 * Decrement the stats counter for the appropriate RRtype.
   1567  1.5  christos 	 * If the STALE attribute is set, this will decrement the
   1568  1.5  christos 	 * stale type counter, otherwise it decrements the active
   1569  1.5  christos 	 * stats type counter.
   1570  1.5  christos 	 */
   1571  1.5  christos 	update_rrsetstats(rbtdb, header, false);
   1572  1.1  christos 
   1573  1.1  christos 	header->attributes |= RDATASET_ATTR_ANCIENT;
   1574  1.1  christos 	header->node->dirty = 1;
   1575  1.1  christos 
   1576  1.5  christos 	/* Increment the stats counter for the ancient RRtype. */
   1577  1.5  christos 	update_rrsetstats(rbtdb, header, true);
   1578  1.5  christos }
   1579  1.5  christos 
   1580  1.5  christos static inline void
   1581  1.5  christos mark_header_stale(dns_rbtdb_t *rbtdb, rdatasetheader_t *header) {
   1582  1.1  christos 	/*
   1583  1.5  christos 	 * If we are already stale there is nothing to do.
   1584  1.1  christos 	 */
   1585  1.5  christos 	if (STALE(header)) {
   1586  1.1  christos 		return;
   1587  1.5  christos 	}
   1588  1.5  christos 
   1589  1.5  christos 	/* Decrement the stats counter for the appropriate RRtype.
   1590  1.5  christos 	 * If the ANCIENT attribute is set (although it is very
   1591  1.5  christos 	 * unlikely that an RRset goes from ANCIENT to STALE), this
   1592  1.5  christos 	 * will decrement the ancient stale type counter, otherwise it
   1593  1.5  christos 	 * decrements the active stats type counter.
   1594  1.5  christos 	 */
   1595  1.5  christos 	update_rrsetstats(rbtdb, header, false);
   1596  1.1  christos 
   1597  1.5  christos 	header->attributes |= RDATASET_ATTR_STALE;
   1598  1.5  christos 
   1599  1.5  christos 	update_rrsetstats(rbtdb, header, true);
   1600  1.1  christos }
   1601  1.1  christos 
   1602  1.1  christos static inline void
   1603  1.7  christos clean_stale_headers(dns_rbtdb_t *rbtdb, isc_mem_t *mctx,
   1604  1.7  christos 		    rdatasetheader_t *top) {
   1605  1.1  christos 	rdatasetheader_t *d, *down_next;
   1606  1.1  christos 
   1607  1.1  christos 	for (d = top->down; d != NULL; d = down_next) {
   1608  1.1  christos 		down_next = d->down;
   1609  1.1  christos 		free_rdataset(rbtdb, mctx, d);
   1610  1.1  christos 	}
   1611  1.1  christos 	top->down = NULL;
   1612  1.1  christos }
   1613  1.1  christos 
   1614  1.1  christos static inline void
   1615  1.1  christos clean_cache_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
   1616  1.1  christos 	rdatasetheader_t *current, *top_prev, *top_next;
   1617  1.1  christos 	isc_mem_t *mctx = rbtdb->common.mctx;
   1618  1.1  christos 
   1619  1.1  christos 	/*
   1620  1.1  christos 	 * Caller must be holding the node lock.
   1621  1.1  christos 	 */
   1622  1.1  christos 
   1623  1.1  christos 	top_prev = NULL;
   1624  1.1  christos 	for (current = node->data; current != NULL; current = top_next) {
   1625  1.1  christos 		top_next = current->next;
   1626  1.1  christos 		clean_stale_headers(rbtdb, mctx, current);
   1627  1.1  christos 		/*
   1628  1.7  christos 		 * If current is nonexistent, ancient, or stale and
   1629  1.7  christos 		 * we are not keeping stale, we can clean it up.
   1630  1.1  christos 		 */
   1631  1.1  christos 		if (NONEXISTENT(current) || ANCIENT(current) ||
   1632  1.7  christos 		    (STALE(current) && !KEEPSTALE(rbtdb)))
   1633  1.7  christos 		{
   1634  1.7  christos 			if (top_prev != NULL) {
   1635  1.1  christos 				top_prev->next = current->next;
   1636  1.7  christos 			} else {
   1637  1.1  christos 				node->data = current->next;
   1638  1.7  christos 			}
   1639  1.1  christos 			free_rdataset(rbtdb, mctx, current);
   1640  1.7  christos 		} else {
   1641  1.1  christos 			top_prev = current;
   1642  1.7  christos 		}
   1643  1.1  christos 	}
   1644  1.1  christos 	node->dirty = 0;
   1645  1.1  christos }
   1646  1.1  christos 
   1647  1.1  christos static inline void
   1648  1.1  christos clean_zone_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
   1649  1.7  christos 		rbtdb_serial_t least_serial) {
   1650  1.1  christos 	rdatasetheader_t *current, *dcurrent, *down_next, *dparent;
   1651  1.1  christos 	rdatasetheader_t *top_prev, *top_next;
   1652  1.1  christos 	isc_mem_t *mctx = rbtdb->common.mctx;
   1653  1.3  christos 	bool still_dirty = false;
   1654  1.1  christos 
   1655  1.1  christos 	/*
   1656  1.1  christos 	 * Caller must be holding the node lock.
   1657  1.1  christos 	 */
   1658  1.1  christos 	REQUIRE(least_serial != 0);
   1659  1.1  christos 
   1660  1.1  christos 	top_prev = NULL;
   1661  1.1  christos 	for (current = node->data; current != NULL; current = top_next) {
   1662  1.1  christos 		top_next = current->next;
   1663  1.1  christos 
   1664  1.1  christos 		/*
   1665  1.1  christos 		 * First, we clean up any instances of multiple rdatasets
   1666  1.1  christos 		 * with the same serial number, or that have the IGNORE
   1667  1.1  christos 		 * attribute.
   1668  1.1  christos 		 */
   1669  1.1  christos 		dparent = current;
   1670  1.7  christos 		for (dcurrent = current->down; dcurrent != NULL;
   1671  1.1  christos 		     dcurrent = down_next) {
   1672  1.1  christos 			down_next = dcurrent->down;
   1673  1.1  christos 			INSIST(dcurrent->serial <= dparent->serial);
   1674  1.1  christos 			if (dcurrent->serial == dparent->serial ||
   1675  1.1  christos 			    IGNORE(dcurrent)) {
   1676  1.7  christos 				if (down_next != NULL) {
   1677  1.1  christos 					down_next->next = dparent;
   1678  1.7  christos 				}
   1679  1.1  christos 				dparent->down = down_next;
   1680  1.1  christos 				free_rdataset(rbtdb, mctx, dcurrent);
   1681  1.7  christos 			} else {
   1682  1.1  christos 				dparent = dcurrent;
   1683  1.7  christos 			}
   1684  1.1  christos 		}
   1685  1.1  christos 
   1686  1.1  christos 		/*
   1687  1.1  christos 		 * We've now eliminated all IGNORE datasets with the possible
   1688  1.1  christos 		 * exception of current, which we now check.
   1689  1.1  christos 		 */
   1690  1.1  christos 		if (IGNORE(current)) {
   1691  1.1  christos 			down_next = current->down;
   1692  1.1  christos 			if (down_next == NULL) {
   1693  1.7  christos 				if (top_prev != NULL) {
   1694  1.1  christos 					top_prev->next = current->next;
   1695  1.7  christos 				} else {
   1696  1.1  christos 					node->data = current->next;
   1697  1.7  christos 				}
   1698  1.1  christos 				free_rdataset(rbtdb, mctx, current);
   1699  1.1  christos 				/*
   1700  1.1  christos 				 * current no longer exists, so we can
   1701  1.1  christos 				 * just continue with the loop.
   1702  1.1  christos 				 */
   1703  1.1  christos 				continue;
   1704  1.1  christos 			} else {
   1705  1.1  christos 				/*
   1706  1.1  christos 				 * Pull up current->down, making it the new
   1707  1.1  christos 				 * current.
   1708  1.1  christos 				 */
   1709  1.7  christos 				if (top_prev != NULL) {
   1710  1.1  christos 					top_prev->next = down_next;
   1711  1.7  christos 				} else {
   1712  1.1  christos 					node->data = down_next;
   1713  1.7  christos 				}
   1714  1.1  christos 				down_next->next = top_next;
   1715  1.1  christos 				free_rdataset(rbtdb, mctx, current);
   1716  1.1  christos 				current = down_next;
   1717  1.1  christos 			}
   1718  1.1  christos 		}
   1719  1.1  christos 
   1720  1.1  christos 		/*
   1721  1.1  christos 		 * We now try to find the first down node less than the
   1722  1.1  christos 		 * least serial.
   1723  1.1  christos 		 */
   1724  1.1  christos 		dparent = current;
   1725  1.7  christos 		for (dcurrent = current->down; dcurrent != NULL;
   1726  1.1  christos 		     dcurrent = down_next) {
   1727  1.1  christos 			down_next = dcurrent->down;
   1728  1.7  christos 			if (dcurrent->serial < least_serial) {
   1729  1.1  christos 				break;
   1730  1.7  christos 			}
   1731  1.1  christos 			dparent = dcurrent;
   1732  1.1  christos 		}
   1733  1.1  christos 
   1734  1.1  christos 		/*
   1735  1.1  christos 		 * If there is a such an rdataset, delete it and any older
   1736  1.1  christos 		 * versions.
   1737  1.1  christos 		 */
   1738  1.1  christos 		if (dcurrent != NULL) {
   1739  1.1  christos 			do {
   1740  1.1  christos 				down_next = dcurrent->down;
   1741  1.1  christos 				INSIST(dcurrent->serial <= least_serial);
   1742  1.1  christos 				free_rdataset(rbtdb, mctx, dcurrent);
   1743  1.1  christos 				dcurrent = down_next;
   1744  1.1  christos 			} while (dcurrent != NULL);
   1745  1.1  christos 			dparent->down = NULL;
   1746  1.1  christos 		}
   1747  1.1  christos 
   1748  1.1  christos 		/*
   1749  1.1  christos 		 * Note.  The serial number of 'current' might be less than
   1750  1.1  christos 		 * least_serial too, but we cannot delete it because it is
   1751  1.1  christos 		 * the most recent version, unless it is a NONEXISTENT
   1752  1.1  christos 		 * rdataset.
   1753  1.1  christos 		 */
   1754  1.1  christos 		if (current->down != NULL) {
   1755  1.3  christos 			still_dirty = true;
   1756  1.1  christos 			top_prev = current;
   1757  1.1  christos 		} else {
   1758  1.1  christos 			/*
   1759  1.1  christos 			 * If this is a NONEXISTENT rdataset, we can delete it.
   1760  1.1  christos 			 */
   1761  1.1  christos 			if (NONEXISTENT(current)) {
   1762  1.7  christos 				if (top_prev != NULL) {
   1763  1.1  christos 					top_prev->next = current->next;
   1764  1.7  christos 				} else {
   1765  1.1  christos 					node->data = current->next;
   1766  1.7  christos 				}
   1767  1.1  christos 				free_rdataset(rbtdb, mctx, current);
   1768  1.7  christos 			} else {
   1769  1.1  christos 				top_prev = current;
   1770  1.7  christos 			}
   1771  1.1  christos 		}
   1772  1.1  christos 	}
   1773  1.7  christos 	if (!still_dirty) {
   1774  1.1  christos 		node->dirty = 0;
   1775  1.7  christos 	}
   1776  1.1  christos }
   1777  1.1  christos 
   1778  1.7  christos /*
   1779  1.7  christos  * tree_lock(write) must be held.
   1780  1.7  christos  */
   1781  1.1  christos static void
   1782  1.1  christos delete_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
   1783  1.1  christos 	dns_rbtnode_t *nsecnode;
   1784  1.1  christos 	dns_fixedname_t fname;
   1785  1.1  christos 	dns_name_t *name;
   1786  1.1  christos 	isc_result_t result = ISC_R_UNEXPECTED;
   1787  1.1  christos 
   1788  1.1  christos 	INSIST(!ISC_LINK_LINKED(node, deadlink));
   1789  1.1  christos 
   1790  1.1  christos 	if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(1))) {
   1791  1.1  christos 		char printname[DNS_NAME_FORMATSIZE];
   1792  1.7  christos 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   1793  1.7  christos 			      DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
   1794  1.7  christos 			      "delete_node(): %p %s (bucket %d)", node,
   1795  1.7  christos 			      dns_rbt_formatnodename(node, printname,
   1796  1.7  christos 						     sizeof(printname)),
   1797  1.1  christos 			      node->locknum);
   1798  1.1  christos 	}
   1799  1.1  christos 
   1800  1.1  christos 	switch (node->nsec) {
   1801  1.1  christos 	case DNS_RBT_NSEC_NORMAL:
   1802  1.1  christos 		/*
   1803  1.1  christos 		 * Though this may be wasteful, it has to be done before
   1804  1.1  christos 		 * node is deleted.
   1805  1.1  christos 		 */
   1806  1.1  christos 		name = dns_fixedname_initname(&fname);
   1807  1.1  christos 		dns_rbt_fullnamefromnode(node, name);
   1808  1.1  christos 
   1809  1.3  christos 		result = dns_rbt_deletenode(rbtdb->tree, node, false);
   1810  1.1  christos 		break;
   1811  1.1  christos 	case DNS_RBT_NSEC_HAS_NSEC:
   1812  1.1  christos 		name = dns_fixedname_initname(&fname);
   1813  1.1  christos 		dns_rbt_fullnamefromnode(node, name);
   1814  1.1  christos 		/*
   1815  1.1  christos 		 * Delete the corresponding node from the auxiliary NSEC
   1816  1.1  christos 		 * tree before deleting from the main tree.
   1817  1.1  christos 		 */
   1818  1.1  christos 		nsecnode = NULL;
   1819  1.1  christos 		result = dns_rbt_findnode(rbtdb->nsec, name, NULL, &nsecnode,
   1820  1.7  christos 					  NULL, DNS_RBTFIND_EMPTYDATA, NULL,
   1821  1.7  christos 					  NULL);
   1822  1.1  christos 		if (result != ISC_R_SUCCESS) {
   1823  1.1  christos 			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   1824  1.1  christos 				      DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
   1825  1.1  christos 				      "delete_node: "
   1826  1.1  christos 				      "dns_rbt_findnode(nsec): %s",
   1827  1.1  christos 				      isc_result_totext(result));
   1828  1.1  christos 		} else {
   1829  1.1  christos 			result = dns_rbt_deletenode(rbtdb->nsec, nsecnode,
   1830  1.3  christos 						    false);
   1831  1.1  christos 			if (result != ISC_R_SUCCESS) {
   1832  1.7  christos 				isc_log_write(
   1833  1.7  christos 					dns_lctx, DNS_LOGCATEGORY_DATABASE,
   1834  1.7  christos 					DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
   1835  1.7  christos 					"delete_node(): "
   1836  1.7  christos 					"dns_rbt_deletenode(nsecnode): %s",
   1837  1.7  christos 					isc_result_totext(result));
   1838  1.1  christos 			}
   1839  1.1  christos 		}
   1840  1.3  christos 		result = dns_rbt_deletenode(rbtdb->tree, node, false);
   1841  1.1  christos 		break;
   1842  1.1  christos 	case DNS_RBT_NSEC_NSEC:
   1843  1.3  christos 		result = dns_rbt_deletenode(rbtdb->nsec, node, false);
   1844  1.1  christos 		break;
   1845  1.1  christos 	case DNS_RBT_NSEC_NSEC3:
   1846  1.3  christos 		result = dns_rbt_deletenode(rbtdb->nsec3, node, false);
   1847  1.1  christos 		break;
   1848  1.1  christos 	}
   1849  1.1  christos 	if (result != ISC_R_SUCCESS) {
   1850  1.7  christos 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   1851  1.7  christos 			      DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
   1852  1.1  christos 			      "delete_node(): "
   1853  1.1  christos 			      "dns_rbt_deletenode: %s",
   1854  1.1  christos 			      isc_result_totext(result));
   1855  1.1  christos 	}
   1856  1.1  christos }
   1857  1.1  christos 
   1858  1.1  christos /*
   1859  1.1  christos  * Caller must be holding the node lock.
   1860  1.1  christos  */
   1861  1.1  christos static inline void
   1862  1.1  christos new_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
   1863  1.1  christos 	INSIST(!ISC_LINK_LINKED(node, deadlink));
   1864  1.7  christos 	if (isc_refcount_increment0(&node->references) == 0) {
   1865  1.3  christos 		/* this is the first reference to the node */
   1866  1.7  christos 		isc_refcount_increment0(
   1867  1.7  christos 			&rbtdb->node_locks[node->locknum].references);
   1868  1.1  christos 	}
   1869  1.1  christos }
   1870  1.1  christos 
   1871  1.1  christos /*%
   1872  1.7  christos  * The tree lock must be held for the result to be valid.
   1873  1.7  christos  */
   1874  1.7  christos static inline bool
   1875  1.7  christos is_leaf(dns_rbtnode_t *node) {
   1876  1.7  christos 	return (node->parent != NULL && node->parent->down == node &&
   1877  1.7  christos 		node->left == NULL && node->right == NULL);
   1878  1.7  christos }
   1879  1.7  christos 
   1880  1.7  christos static inline void
   1881  1.7  christos send_to_prune_tree(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
   1882  1.7  christos 	isc_event_t *ev;
   1883  1.7  christos 	dns_db_t *db;
   1884  1.7  christos 
   1885  1.7  christos 	ev = isc_event_allocate(rbtdb->common.mctx, NULL, DNS_EVENT_RBTPRUNE,
   1886  1.7  christos 				prune_tree, node, sizeof(isc_event_t));
   1887  1.7  christos 	new_reference(rbtdb, node);
   1888  1.7  christos 	db = NULL;
   1889  1.7  christos 	attach((dns_db_t *)rbtdb, &db);
   1890  1.7  christos 	ev->ev_sender = db;
   1891  1.7  christos 	isc_task_send(rbtdb->task, &ev);
   1892  1.7  christos }
   1893  1.7  christos 
   1894  1.7  christos /*%
   1895  1.1  christos  * Clean up dead nodes.  These are nodes which have no references, and
   1896  1.1  christos  * have no data.  They are dead but we could not or chose not to delete
   1897  1.1  christos  * them when we deleted all the data at that node because we did not want
   1898  1.1  christos  * to wait for the tree write lock.
   1899  1.1  christos  *
   1900  1.1  christos  * The caller must hold a tree write lock and bucketnum'th node (write) lock.
   1901  1.1  christos  */
   1902  1.1  christos static void
   1903  1.1  christos cleanup_dead_nodes(dns_rbtdb_t *rbtdb, int bucketnum) {
   1904  1.1  christos 	dns_rbtnode_t *node;
   1905  1.7  christos 	int count = 10; /* XXXJT: should be adjustable */
   1906  1.1  christos 
   1907  1.1  christos 	node = ISC_LIST_HEAD(rbtdb->deadnodes[bucketnum]);
   1908  1.1  christos 	while (node != NULL && count > 0) {
   1909  1.1  christos 		ISC_LIST_UNLINK(rbtdb->deadnodes[bucketnum], node, deadlink);
   1910  1.1  christos 
   1911  1.1  christos 		/*
   1912  1.1  christos 		 * Since we're holding a tree write lock, it should be
   1913  1.1  christos 		 * impossible for this node to be referenced by others.
   1914  1.7  christos 		 *
   1915  1.7  christos 		 * decrement_reference may not have tested node->down, as
   1916  1.7  christos 		 * the tree_lock was not held, before adding the node to
   1917  1.7  christos 		 * deadnodes so we test it here.
   1918  1.1  christos 		 */
   1919  1.3  christos 		INSIST(isc_refcount_current(&node->references) == 0 &&
   1920  1.1  christos 		       node->data == NULL);
   1921  1.1  christos 
   1922  1.7  christos 		if (is_leaf(node) && rbtdb->task != NULL) {
   1923  1.7  christos 			send_to_prune_tree(rbtdb, node);
   1924  1.7  christos 		} else if (node->down == NULL && node->data == NULL) {
   1925  1.7  christos 			/*
   1926  1.7  christos 			 * Not a interior node and not needing to be
   1927  1.7  christos 			 * reactivated.
   1928  1.7  christos 			 */
   1929  1.1  christos 			delete_node(rbtdb, node);
   1930  1.7  christos 		} else if (node->data == NULL) {
   1931  1.7  christos 			/*
   1932  1.7  christos 			 * A interior node without data. Leave linked to
   1933  1.7  christos 			 * to be cleaned up when node->down becomes NULL.
   1934  1.7  christos 			 */
   1935  1.7  christos 			ISC_LIST_APPEND(rbtdb->deadnodes[bucketnum], node,
   1936  1.7  christos 					deadlink);
   1937  1.1  christos 		}
   1938  1.1  christos 		node = ISC_LIST_HEAD(rbtdb->deadnodes[bucketnum]);
   1939  1.1  christos 		count--;
   1940  1.1  christos 	}
   1941  1.1  christos }
   1942  1.1  christos 
   1943  1.1  christos /*
   1944  1.1  christos  * This function is assumed to be called when a node is newly referenced
   1945  1.1  christos  * and can be in the deadnode list.  In that case the node must be retrieved
   1946  1.1  christos  * from the list because it is going to be used.  In addition, if the caller
   1947  1.1  christos  * happens to hold a write lock on the tree, it's a good chance to purge dead
   1948  1.1  christos  * nodes.
   1949  1.1  christos  * Note: while a new reference is gained in multiple places, there are only very
   1950  1.1  christos  * few cases where the node can be in the deadnode list (only empty nodes can
   1951  1.1  christos  * have been added to the list).
   1952  1.1  christos  */
   1953  1.1  christos static inline void
   1954  1.1  christos reactivate_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
   1955  1.7  christos 		isc_rwlocktype_t treelocktype) {
   1956  1.1  christos 	isc_rwlocktype_t locktype = isc_rwlocktype_read;
   1957  1.1  christos 	nodelock_t *nodelock = &rbtdb->node_locks[node->locknum].lock;
   1958  1.3  christos 	bool maybe_cleanup = false;
   1959  1.1  christos 
   1960  1.1  christos 	POST(locktype);
   1961  1.1  christos 
   1962  1.3  christos 	NODE_LOCK(nodelock, locktype);
   1963  1.1  christos 
   1964  1.1  christos 	/*
   1965  1.1  christos 	 * Check if we can possibly cleanup the dead node.  If so, upgrade
   1966  1.1  christos 	 * the node lock below to perform the cleanup.
   1967  1.1  christos 	 */
   1968  1.1  christos 	if (!ISC_LIST_EMPTY(rbtdb->deadnodes[node->locknum]) &&
   1969  1.7  christos 	    treelocktype == isc_rwlocktype_write)
   1970  1.7  christos 	{
   1971  1.3  christos 		maybe_cleanup = true;
   1972  1.1  christos 	}
   1973  1.1  christos 
   1974  1.1  christos 	if (ISC_LINK_LINKED(node, deadlink) || maybe_cleanup) {
   1975  1.1  christos 		/*
   1976  1.1  christos 		 * Upgrade the lock and test if we still need to unlink.
   1977  1.1  christos 		 */
   1978  1.3  christos 		NODE_UNLOCK(nodelock, locktype);
   1979  1.1  christos 		locktype = isc_rwlocktype_write;
   1980  1.1  christos 		POST(locktype);
   1981  1.3  christos 		NODE_LOCK(nodelock, locktype);
   1982  1.7  christos 		if (ISC_LINK_LINKED(node, deadlink)) {
   1983  1.7  christos 			ISC_LIST_UNLINK(rbtdb->deadnodes[node->locknum], node,
   1984  1.7  christos 					deadlink);
   1985  1.7  christos 		}
   1986  1.7  christos 		if (maybe_cleanup) {
   1987  1.1  christos 			cleanup_dead_nodes(rbtdb, node->locknum);
   1988  1.7  christos 		}
   1989  1.1  christos 	}
   1990  1.1  christos 
   1991  1.1  christos 	new_reference(rbtdb, node);
   1992  1.1  christos 
   1993  1.3  christos 	NODE_UNLOCK(nodelock, locktype);
   1994  1.1  christos }
   1995  1.1  christos 
   1996  1.1  christos /*
   1997  1.1  christos  * Caller must be holding the node lock; either the "strong", read or write
   1998  1.1  christos  * lock.  Note that the lock must be held even when node references are
   1999  1.1  christos  * atomically modified; in that case the decrement operation itself does not
   2000  1.1  christos  * have to be protected, but we must avoid a race condition where multiple
   2001  1.1  christos  * threads are decreasing the reference to zero simultaneously and at least
   2002  1.1  christos  * one of them is going to free the node.
   2003  1.1  christos  *
   2004  1.3  christos  * This function returns true if and only if the node reference decreases
   2005  1.1  christos  * to zero.
   2006  1.1  christos  *
   2007  1.1  christos  * NOTE: Decrementing the reference count of a node to zero does not mean it
   2008  1.1  christos  * will be immediately freed.
   2009  1.1  christos  */
   2010  1.3  christos static bool
   2011  1.1  christos decrement_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
   2012  1.7  christos 		    rbtdb_serial_t least_serial, isc_rwlocktype_t nlock,
   2013  1.7  christos 		    isc_rwlocktype_t tlock, bool pruning) {
   2014  1.1  christos 	isc_result_t result;
   2015  1.3  christos 	bool write_locked;
   2016  1.7  christos 	bool locked = tlock != isc_rwlocktype_none;
   2017  1.1  christos 	rbtdb_nodelock_t *nodelock;
   2018  1.1  christos 	int bucket = node->locknum;
   2019  1.3  christos 	bool no_reference = true;
   2020  1.4  christos 	uint_fast32_t refs;
   2021  1.1  christos 
   2022  1.1  christos 	nodelock = &rbtdb->node_locks[bucket];
   2023  1.1  christos 
   2024  1.7  christos #define KEEP_NODE(n, r, l)                                  \
   2025  1.7  christos 	((n)->data != NULL || ((l) && (n)->down != NULL) || \
   2026  1.1  christos 	 (n) == (r)->origin_node || (n) == (r)->nsec3_origin_node)
   2027  1.1  christos 
   2028  1.1  christos 	/* Handle easy and typical case first. */
   2029  1.7  christos 	if (!node->dirty && KEEP_NODE(node, rbtdb, locked)) {
   2030  1.3  christos 		if (isc_refcount_decrement(&node->references) == 1) {
   2031  1.4  christos 			refs = isc_refcount_decrement(&nodelock->references);
   2032  1.4  christos 			INSIST(refs > 0);
   2033  1.3  christos 			return (true);
   2034  1.3  christos 		} else {
   2035  1.3  christos 			return (false);
   2036  1.1  christos 		}
   2037  1.1  christos 	}
   2038  1.1  christos 
   2039  1.1  christos 	/* Upgrade the lock? */
   2040  1.1  christos 	if (nlock == isc_rwlocktype_read) {
   2041  1.3  christos 		NODE_UNLOCK(&nodelock->lock, isc_rwlocktype_read);
   2042  1.3  christos 		NODE_LOCK(&nodelock->lock, isc_rwlocktype_write);
   2043  1.1  christos 	}
   2044  1.1  christos 
   2045  1.3  christos 	if (isc_refcount_decrement(&node->references) > 1) {
   2046  1.1  christos 		/* Restore the lock? */
   2047  1.7  christos 		if (nlock == isc_rwlocktype_read) {
   2048  1.3  christos 			NODE_DOWNGRADE(&nodelock->lock);
   2049  1.7  christos 		}
   2050  1.3  christos 		return (false);
   2051  1.1  christos 	}
   2052  1.1  christos 
   2053  1.1  christos 	if (node->dirty) {
   2054  1.7  christos 		if (IS_CACHE(rbtdb)) {
   2055  1.1  christos 			clean_cache_node(rbtdb, node);
   2056  1.7  christos 		} else {
   2057  1.1  christos 			if (least_serial == 0) {
   2058  1.1  christos 				/*
   2059  1.1  christos 				 * Caller doesn't know the least serial.
   2060  1.1  christos 				 * Get it.
   2061  1.1  christos 				 */
   2062  1.1  christos 				RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   2063  1.1  christos 				least_serial = rbtdb->least_serial;
   2064  1.7  christos 				RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   2065  1.1  christos 			}
   2066  1.1  christos 			clean_zone_node(rbtdb, node, least_serial);
   2067  1.1  christos 		}
   2068  1.1  christos 	}
   2069  1.1  christos 
   2070  1.1  christos 	/*
   2071  1.1  christos 	 * Attempt to switch to a write lock on the tree.  If this fails,
   2072  1.1  christos 	 * we will add this node to a linked list of nodes in this locking
   2073  1.1  christos 	 * bucket which we will free later.
   2074  1.1  christos 	 */
   2075  1.1  christos 	if (tlock != isc_rwlocktype_write) {
   2076  1.1  christos 		/*
   2077  1.1  christos 		 * Locking hierarchy notwithstanding, we don't need to free
   2078  1.1  christos 		 * the node lock before acquiring the tree write lock because
   2079  1.1  christos 		 * we only do a trylock.
   2080  1.1  christos 		 */
   2081  1.7  christos 		if (tlock == isc_rwlocktype_read) {
   2082  1.1  christos 			result = isc_rwlock_tryupgrade(&rbtdb->tree_lock);
   2083  1.7  christos 		} else {
   2084  1.1  christos 			result = isc_rwlock_trylock(&rbtdb->tree_lock,
   2085  1.1  christos 						    isc_rwlocktype_write);
   2086  1.7  christos 		}
   2087  1.1  christos 		RUNTIME_CHECK(result == ISC_R_SUCCESS ||
   2088  1.1  christos 			      result == ISC_R_LOCKBUSY);
   2089  1.1  christos 
   2090  1.3  christos 		write_locked = (result == ISC_R_SUCCESS);
   2091  1.7  christos 	} else {
   2092  1.3  christos 		write_locked = true;
   2093  1.7  christos 	}
   2094  1.1  christos 
   2095  1.4  christos 	refs = isc_refcount_decrement(&nodelock->references);
   2096  1.4  christos 	INSIST(refs > 0);
   2097  1.1  christos 
   2098  1.7  christos 	if (KEEP_NODE(node, rbtdb, locked || write_locked)) {
   2099  1.1  christos 		goto restore_locks;
   2100  1.7  christos 	}
   2101  1.1  christos 
   2102  1.1  christos #undef KEEP_NODE
   2103  1.1  christos 
   2104  1.1  christos 	if (write_locked) {
   2105  1.1  christos 		/*
   2106  1.1  christos 		 * We can now delete the node.
   2107  1.1  christos 		 */
   2108  1.1  christos 
   2109  1.1  christos 		/*
   2110  1.1  christos 		 * If this node is the only one in the level it's in, deleting
   2111  1.1  christos 		 * this node may recursively make its parent the only node in
   2112  1.1  christos 		 * the parent level; if so, and if no one is currently using
   2113  1.1  christos 		 * the parent node, this is almost the only opportunity to
   2114  1.1  christos 		 * clean it up.  But the recursive cleanup is not that trivial
   2115  1.1  christos 		 * since the child and parent may be in different lock buckets,
   2116  1.1  christos 		 * which would cause a lock order reversal problem.  To avoid
   2117  1.1  christos 		 * the trouble, we'll dispatch a separate event for batch
   2118  1.1  christos 		 * cleaning.  We need to check whether we're deleting the node
   2119  1.1  christos 		 * as a result of pruning to avoid infinite dispatching.
   2120  1.1  christos 		 * Note: pruning happens only when a task has been set for the
   2121  1.1  christos 		 * rbtdb.  If the user of the rbtdb chooses not to set a task,
   2122  1.1  christos 		 * it's their responsibility to purge stale leaves (e.g. by
   2123  1.1  christos 		 * periodic walk-through).
   2124  1.1  christos 		 */
   2125  1.7  christos 		if (!pruning && is_leaf(node) && rbtdb->task != NULL) {
   2126  1.7  christos 			send_to_prune_tree(rbtdb, node);
   2127  1.7  christos 			no_reference = false;
   2128  1.1  christos 		} else {
   2129  1.1  christos 			delete_node(rbtdb, node);
   2130  1.1  christos 		}
   2131  1.1  christos 	} else {
   2132  1.1  christos 		INSIST(node->data == NULL);
   2133  1.1  christos 		INSIST(!ISC_LINK_LINKED(node, deadlink));
   2134  1.1  christos 		ISC_LIST_APPEND(rbtdb->deadnodes[bucket], node, deadlink);
   2135  1.1  christos 	}
   2136  1.1  christos 
   2137  1.7  christos restore_locks:
   2138  1.1  christos 	/* Restore the lock? */
   2139  1.7  christos 	if (nlock == isc_rwlocktype_read) {
   2140  1.3  christos 		NODE_DOWNGRADE(&nodelock->lock);
   2141  1.7  christos 	}
   2142  1.1  christos 
   2143  1.1  christos 	/*
   2144  1.1  christos 	 * Relock a read lock, or unlock the write lock if no lock was held.
   2145  1.1  christos 	 */
   2146  1.7  christos 	if (tlock == isc_rwlocktype_none) {
   2147  1.7  christos 		if (write_locked) {
   2148  1.1  christos 			RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   2149  1.7  christos 		}
   2150  1.7  christos 	}
   2151  1.1  christos 
   2152  1.7  christos 	if (tlock == isc_rwlocktype_read) {
   2153  1.7  christos 		if (write_locked) {
   2154  1.1  christos 			isc_rwlock_downgrade(&rbtdb->tree_lock);
   2155  1.7  christos 		}
   2156  1.7  christos 	}
   2157  1.1  christos 
   2158  1.1  christos 	return (no_reference);
   2159  1.1  christos }
   2160  1.1  christos 
   2161  1.1  christos /*
   2162  1.1  christos  * Prune the tree by recursively cleaning-up single leaves.  In the worst
   2163  1.1  christos  * case, the number of iteration is the number of tree levels, which is at
   2164  1.1  christos  * most the maximum number of domain name labels, i.e, 127.  In practice, this
   2165  1.1  christos  * should be much smaller (only a few times), and even the worst case would be
   2166  1.1  christos  * acceptable for a single event.
   2167  1.1  christos  */
   2168  1.1  christos static void
   2169  1.1  christos prune_tree(isc_task_t *task, isc_event_t *event) {
   2170  1.1  christos 	dns_rbtdb_t *rbtdb = event->ev_sender;
   2171  1.1  christos 	dns_rbtnode_t *node = event->ev_arg;
   2172  1.1  christos 	dns_rbtnode_t *parent;
   2173  1.1  christos 	unsigned int locknum;
   2174  1.1  christos 
   2175  1.1  christos 	UNUSED(task);
   2176  1.1  christos 
   2177  1.1  christos 	isc_event_free(&event);
   2178  1.1  christos 
   2179  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   2180  1.1  christos 	locknum = node->locknum;
   2181  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
   2182  1.1  christos 	do {
   2183  1.1  christos 		parent = node->parent;
   2184  1.1  christos 		decrement_reference(rbtdb, node, 0, isc_rwlocktype_write,
   2185  1.3  christos 				    isc_rwlocktype_write, true);
   2186  1.1  christos 
   2187  1.1  christos 		if (parent != NULL && parent->down == NULL) {
   2188  1.1  christos 			/*
   2189  1.1  christos 			 * node was the only down child of the parent and has
   2190  1.1  christos 			 * just been removed.  We'll then need to examine the
   2191  1.1  christos 			 * parent.  Keep the lock if possible; otherwise,
   2192  1.1  christos 			 * release the old lock and acquire one for the parent.
   2193  1.1  christos 			 */
   2194  1.1  christos 			if (parent->locknum != locknum) {
   2195  1.1  christos 				NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
   2196  1.1  christos 					    isc_rwlocktype_write);
   2197  1.1  christos 				locknum = parent->locknum;
   2198  1.1  christos 				NODE_LOCK(&rbtdb->node_locks[locknum].lock,
   2199  1.1  christos 					  isc_rwlocktype_write);
   2200  1.1  christos 			}
   2201  1.1  christos 
   2202  1.1  christos 			/*
   2203  1.1  christos 			 * We need to gain a reference to the node before
   2204  1.1  christos 			 * decrementing it in the next iteration.  In addition,
   2205  1.1  christos 			 * if the node is in the dead-nodes list, extract it
   2206  1.1  christos 			 * from the list beforehand as we do in
   2207  1.1  christos 			 * reactivate_node().
   2208  1.1  christos 			 */
   2209  1.7  christos 			if (ISC_LINK_LINKED(parent, deadlink)) {
   2210  1.1  christos 				ISC_LIST_UNLINK(rbtdb->deadnodes[locknum],
   2211  1.1  christos 						parent, deadlink);
   2212  1.7  christos 			}
   2213  1.1  christos 			new_reference(rbtdb, parent);
   2214  1.7  christos 		} else {
   2215  1.1  christos 			parent = NULL;
   2216  1.7  christos 		}
   2217  1.1  christos 
   2218  1.1  christos 		node = parent;
   2219  1.1  christos 	} while (node != NULL);
   2220  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
   2221  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   2222  1.1  christos 
   2223  1.2  christos 	detach((dns_db_t **)(void *)&rbtdb);
   2224  1.1  christos }
   2225  1.1  christos 
   2226  1.1  christos static inline void
   2227  1.1  christos make_least_version(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
   2228  1.7  christos 		   rbtdb_changedlist_t *cleanup_list) {
   2229  1.1  christos 	/*
   2230  1.1  christos 	 * Caller must be holding the database lock.
   2231  1.1  christos 	 */
   2232  1.1  christos 
   2233  1.1  christos 	rbtdb->least_serial = version->serial;
   2234  1.1  christos 	*cleanup_list = version->changed_list;
   2235  1.1  christos 	ISC_LIST_INIT(version->changed_list);
   2236  1.1  christos }
   2237  1.1  christos 
   2238  1.1  christos static inline void
   2239  1.1  christos cleanup_nondirty(rbtdb_version_t *version, rbtdb_changedlist_t *cleanup_list) {
   2240  1.1  christos 	rbtdb_changed_t *changed, *next_changed;
   2241  1.1  christos 
   2242  1.1  christos 	/*
   2243  1.1  christos 	 * If the changed record is dirty, then
   2244  1.1  christos 	 * an update created multiple versions of
   2245  1.1  christos 	 * a given rdataset.  We keep this list
   2246  1.1  christos 	 * until we're the least open version, at
   2247  1.1  christos 	 * which point it's safe to get rid of any
   2248  1.1  christos 	 * older versions.
   2249  1.1  christos 	 *
   2250  1.1  christos 	 * If the changed record isn't dirty, then
   2251  1.1  christos 	 * we don't need it anymore since we're
   2252  1.1  christos 	 * committing and not rolling back.
   2253  1.1  christos 	 *
   2254  1.1  christos 	 * The caller must be holding the database lock.
   2255  1.1  christos 	 */
   2256  1.7  christos 	for (changed = HEAD(version->changed_list); changed != NULL;
   2257  1.7  christos 	     changed = next_changed)
   2258  1.7  christos 	{
   2259  1.1  christos 		next_changed = NEXT(changed, link);
   2260  1.1  christos 		if (!changed->dirty) {
   2261  1.7  christos 			UNLINK(version->changed_list, changed, link);
   2262  1.7  christos 			APPEND(*cleanup_list, changed, link);
   2263  1.1  christos 		}
   2264  1.1  christos 	}
   2265  1.1  christos }
   2266  1.1  christos 
   2267  1.1  christos static void
   2268  1.1  christos iszonesecure(dns_db_t *db, rbtdb_version_t *version, dns_dbnode_t *origin) {
   2269  1.1  christos 	dns_rdataset_t keyset;
   2270  1.1  christos 	dns_rdataset_t nsecset, signsecset;
   2271  1.3  christos 	bool haszonekey = false;
   2272  1.3  christos 	bool hasnsec = false;
   2273  1.1  christos 	isc_result_t result;
   2274  1.1  christos 
   2275  1.1  christos 	dns_rdataset_init(&keyset);
   2276  1.1  christos 	result = dns_db_findrdataset(db, origin, version, dns_rdatatype_dnskey,
   2277  1.1  christos 				     0, 0, &keyset, NULL);
   2278  1.1  christos 	if (result == ISC_R_SUCCESS) {
   2279  1.1  christos 		result = dns_rdataset_first(&keyset);
   2280  1.1  christos 		while (result == ISC_R_SUCCESS) {
   2281  1.1  christos 			dns_rdata_t keyrdata = DNS_RDATA_INIT;
   2282  1.1  christos 			dns_rdataset_current(&keyset, &keyrdata);
   2283  1.1  christos 			if (dns_zonekey_iszonekey(&keyrdata)) {
   2284  1.3  christos 				haszonekey = true;
   2285  1.1  christos 				break;
   2286  1.1  christos 			}
   2287  1.1  christos 			result = dns_rdataset_next(&keyset);
   2288  1.1  christos 		}
   2289  1.1  christos 		dns_rdataset_disassociate(&keyset);
   2290  1.1  christos 	}
   2291  1.1  christos 	if (!haszonekey) {
   2292  1.1  christos 		version->secure = dns_db_insecure;
   2293  1.3  christos 		version->havensec3 = false;
   2294  1.1  christos 		return;
   2295  1.1  christos 	}
   2296  1.1  christos 
   2297  1.1  christos 	dns_rdataset_init(&nsecset);
   2298  1.1  christos 	dns_rdataset_init(&signsecset);
   2299  1.7  christos 	result = dns_db_findrdataset(db, origin, version, dns_rdatatype_nsec, 0,
   2300  1.7  christos 				     0, &nsecset, &signsecset);
   2301  1.1  christos 	if (result == ISC_R_SUCCESS) {
   2302  1.1  christos 		if (dns_rdataset_isassociated(&signsecset)) {
   2303  1.3  christos 			hasnsec = true;
   2304  1.1  christos 			dns_rdataset_disassociate(&signsecset);
   2305  1.1  christos 		}
   2306  1.1  christos 		dns_rdataset_disassociate(&nsecset);
   2307  1.1  christos 	}
   2308  1.1  christos 
   2309  1.1  christos 	setnsec3parameters(db, version);
   2310  1.1  christos 
   2311  1.1  christos 	/*
   2312  1.1  christos 	 * Do we have a valid NSEC/NSEC3 chain?
   2313  1.1  christos 	 */
   2314  1.7  christos 	if (version->havensec3 || hasnsec) {
   2315  1.1  christos 		version->secure = dns_db_secure;
   2316  1.7  christos 	} else {
   2317  1.1  christos 		version->secure = dns_db_insecure;
   2318  1.7  christos 	}
   2319  1.1  christos }
   2320  1.1  christos 
   2321  1.1  christos /*%<
   2322  1.1  christos  * Walk the origin node looking for NSEC3PARAM records.
   2323  1.1  christos  * Cache the nsec3 parameters.
   2324  1.1  christos  */
   2325  1.1  christos static void
   2326  1.1  christos setnsec3parameters(dns_db_t *db, rbtdb_version_t *version) {
   2327  1.1  christos 	dns_rbtnode_t *node;
   2328  1.1  christos 	dns_rdata_nsec3param_t nsec3param;
   2329  1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
   2330  1.1  christos 	isc_region_t region;
   2331  1.1  christos 	isc_result_t result;
   2332  1.1  christos 	rdatasetheader_t *header, *header_next;
   2333  1.7  christos 	unsigned char *raw; /* RDATASLAB */
   2334  1.1  christos 	unsigned int count, length;
   2335  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   2336  1.1  christos 
   2337  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   2338  1.3  christos 	version->havensec3 = false;
   2339  1.1  christos 	node = rbtdb->origin_node;
   2340  1.1  christos 	NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
   2341  1.1  christos 		  isc_rwlocktype_read);
   2342  1.7  christos 	for (header = node->data; header != NULL; header = header_next) {
   2343  1.1  christos 		header_next = header->next;
   2344  1.1  christos 		do {
   2345  1.1  christos 			if (header->serial <= version->serial &&
   2346  1.1  christos 			    !IGNORE(header)) {
   2347  1.7  christos 				if (NONEXISTENT(header)) {
   2348  1.1  christos 					header = NULL;
   2349  1.7  christos 				}
   2350  1.1  christos 				break;
   2351  1.7  christos 			} else {
   2352  1.1  christos 				header = header->down;
   2353  1.7  christos 			}
   2354  1.1  christos 		} while (header != NULL);
   2355  1.1  christos 
   2356  1.1  christos 		if (header != NULL &&
   2357  1.1  christos 		    (header->type == dns_rdatatype_nsec3param)) {
   2358  1.1  christos 			/*
   2359  1.1  christos 			 * Find A NSEC3PARAM with a supported algorithm.
   2360  1.1  christos 			 */
   2361  1.1  christos 			raw = (unsigned char *)header + sizeof(*header);
   2362  1.1  christos 			count = raw[0] * 256 + raw[1]; /* count */
   2363  1.5  christos 			raw += DNS_RDATASET_COUNT + DNS_RDATASET_LENGTH;
   2364  1.1  christos 			while (count-- > 0U) {
   2365  1.1  christos 				length = raw[0] * 256 + raw[1];
   2366  1.5  christos 				raw += DNS_RDATASET_ORDER + DNS_RDATASET_LENGTH;
   2367  1.1  christos 				region.base = raw;
   2368  1.1  christos 				region.length = length;
   2369  1.1  christos 				raw += length;
   2370  1.7  christos 				dns_rdata_fromregion(
   2371  1.7  christos 					&rdata, rbtdb->common.rdclass,
   2372  1.7  christos 					dns_rdatatype_nsec3param, &region);
   2373  1.7  christos 				result = dns_rdata_tostruct(&rdata, &nsec3param,
   2374  1.1  christos 							    NULL);
   2375  1.1  christos 				INSIST(result == ISC_R_SUCCESS);
   2376  1.1  christos 				dns_rdata_reset(&rdata);
   2377  1.1  christos 
   2378  1.1  christos 				if (nsec3param.hash != DNS_NSEC3_UNKNOWNALG &&
   2379  1.1  christos 				    !dns_nsec3_supportedhash(nsec3param.hash))
   2380  1.7  christos 				{
   2381  1.1  christos 					continue;
   2382  1.7  christos 				}
   2383  1.1  christos 
   2384  1.7  christos 				if (nsec3param.flags != 0) {
   2385  1.1  christos 					continue;
   2386  1.7  christos 				}
   2387  1.1  christos 
   2388  1.1  christos 				memmove(version->salt, nsec3param.salt,
   2389  1.1  christos 					nsec3param.salt_length);
   2390  1.1  christos 				version->hash = nsec3param.hash;
   2391  1.1  christos 				version->salt_length = nsec3param.salt_length;
   2392  1.1  christos 				version->iterations = nsec3param.iterations;
   2393  1.1  christos 				version->flags = nsec3param.flags;
   2394  1.3  christos 				version->havensec3 = true;
   2395  1.1  christos 				/*
   2396  1.1  christos 				 * Look for a better algorithm than the
   2397  1.1  christos 				 * unknown test algorithm.
   2398  1.1  christos 				 */
   2399  1.7  christos 				if (nsec3param.hash != DNS_NSEC3_UNKNOWNALG) {
   2400  1.1  christos 					goto unlock;
   2401  1.7  christos 				}
   2402  1.1  christos 			}
   2403  1.1  christos 		}
   2404  1.1  christos 	}
   2405  1.7  christos unlock:
   2406  1.1  christos 	NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
   2407  1.1  christos 		    isc_rwlocktype_read);
   2408  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   2409  1.1  christos }
   2410  1.1  christos 
   2411  1.1  christos static void
   2412  1.1  christos cleanup_dead_nodes_callback(isc_task_t *task, isc_event_t *event) {
   2413  1.1  christos 	dns_rbtdb_t *rbtdb = event->ev_arg;
   2414  1.3  christos 	bool again = false;
   2415  1.1  christos 	unsigned int locknum;
   2416  1.1  christos 
   2417  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   2418  1.1  christos 	for (locknum = 0; locknum < rbtdb->node_lock_count; locknum++) {
   2419  1.1  christos 		NODE_LOCK(&rbtdb->node_locks[locknum].lock,
   2420  1.1  christos 			  isc_rwlocktype_write);
   2421  1.1  christos 		cleanup_dead_nodes(rbtdb, locknum);
   2422  1.7  christos 		if (ISC_LIST_HEAD(rbtdb->deadnodes[locknum]) != NULL) {
   2423  1.3  christos 			again = true;
   2424  1.7  christos 		}
   2425  1.1  christos 		NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
   2426  1.1  christos 			    isc_rwlocktype_write);
   2427  1.1  christos 	}
   2428  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   2429  1.7  christos 	if (again) {
   2430  1.1  christos 		isc_task_send(task, &event);
   2431  1.7  christos 	} else {
   2432  1.1  christos 		isc_event_free(&event);
   2433  1.3  christos 		if (isc_refcount_decrement(&rbtdb->references) == 1) {
   2434  1.3  christos 			(void)isc_refcount_current(&rbtdb->references);
   2435  1.1  christos 			maybe_free_rbtdb(rbtdb);
   2436  1.3  christos 		}
   2437  1.1  christos 	}
   2438  1.1  christos }
   2439  1.1  christos 
   2440  1.1  christos static void
   2441  1.3  christos closeversion(dns_db_t *db, dns_dbversion_t **versionp, bool commit) {
   2442  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   2443  1.1  christos 	rbtdb_version_t *version, *cleanup_version, *least_greater;
   2444  1.3  christos 	bool rollback = false;
   2445  1.1  christos 	rbtdb_changedlist_t cleanup_list;
   2446  1.1  christos 	rdatasetheaderlist_t resigned_list;
   2447  1.1  christos 	rbtdb_changed_t *changed, *next_changed;
   2448  1.1  christos 	rbtdb_serial_t serial, least_serial;
   2449  1.1  christos 	dns_rbtnode_t *rbtnode;
   2450  1.1  christos 	rdatasetheader_t *header;
   2451  1.1  christos 
   2452  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   2453  1.1  christos 	version = (rbtdb_version_t *)*versionp;
   2454  1.1  christos 	INSIST(version->rbtdb == rbtdb);
   2455  1.1  christos 
   2456  1.1  christos 	cleanup_version = NULL;
   2457  1.1  christos 	ISC_LIST_INIT(cleanup_list);
   2458  1.1  christos 	ISC_LIST_INIT(resigned_list);
   2459  1.1  christos 
   2460  1.3  christos 	if (isc_refcount_decrement(&version->references) > 1) {
   2461  1.3  christos 		/* typical and easy case first */
   2462  1.1  christos 		if (commit) {
   2463  1.1  christos 			RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   2464  1.1  christos 			INSIST(!version->writer);
   2465  1.1  christos 			RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   2466  1.1  christos 		}
   2467  1.1  christos 		goto end;
   2468  1.1  christos 	}
   2469  1.1  christos 
   2470  1.1  christos 	/*
   2471  1.1  christos 	 * Update the zone's secure status in version before making
   2472  1.1  christos 	 * it the current version.
   2473  1.1  christos 	 */
   2474  1.7  christos 	if (version->writer && commit && !IS_CACHE(rbtdb)) {
   2475  1.1  christos 		iszonesecure(db, version, rbtdb->origin_node);
   2476  1.7  christos 	}
   2477  1.1  christos 
   2478  1.1  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
   2479  1.1  christos 	serial = version->serial;
   2480  1.1  christos 	if (version->writer) {
   2481  1.1  christos 		if (commit) {
   2482  1.1  christos 			unsigned cur_ref;
   2483  1.1  christos 			rbtdb_version_t *cur_version;
   2484  1.1  christos 
   2485  1.1  christos 			INSIST(version->commit_ok);
   2486  1.1  christos 			INSIST(version == rbtdb->future_version);
   2487  1.1  christos 			/*
   2488  1.1  christos 			 * The current version is going to be replaced.
   2489  1.1  christos 			 * Release the (likely last) reference to it from the
   2490  1.1  christos 			 * DB itself and unlink it from the open list.
   2491  1.1  christos 			 */
   2492  1.1  christos 			cur_version = rbtdb->current_version;
   2493  1.7  christos 			cur_ref = isc_refcount_decrement(
   2494  1.7  christos 				&cur_version->references);
   2495  1.3  christos 			if (cur_ref == 1) {
   2496  1.7  christos 				(void)isc_refcount_current(
   2497  1.7  christos 					&cur_version->references);
   2498  1.1  christos 				if (cur_version->serial == rbtdb->least_serial)
   2499  1.7  christos 				{
   2500  1.7  christos 					INSIST(EMPTY(
   2501  1.7  christos 						cur_version->changed_list));
   2502  1.7  christos 				}
   2503  1.7  christos 				UNLINK(rbtdb->open_versions, cur_version, link);
   2504  1.1  christos 			}
   2505  1.1  christos 			if (EMPTY(rbtdb->open_versions)) {
   2506  1.1  christos 				/*
   2507  1.1  christos 				 * We're going to become the least open
   2508  1.1  christos 				 * version.
   2509  1.1  christos 				 */
   2510  1.1  christos 				make_least_version(rbtdb, version,
   2511  1.1  christos 						   &cleanup_list);
   2512  1.1  christos 			} else {
   2513  1.1  christos 				/*
   2514  1.1  christos 				 * Some other open version is the
   2515  1.1  christos 				 * least version.  We can't cleanup
   2516  1.1  christos 				 * records that were changed in this
   2517  1.1  christos 				 * version because the older versions
   2518  1.1  christos 				 * may still be in use by an open
   2519  1.1  christos 				 * version.
   2520  1.1  christos 				 *
   2521  1.1  christos 				 * We can, however, discard the
   2522  1.1  christos 				 * changed records for things that
   2523  1.1  christos 				 * we've added that didn't exist in
   2524  1.1  christos 				 * prior versions.
   2525  1.1  christos 				 */
   2526  1.1  christos 				cleanup_nondirty(version, &cleanup_list);
   2527  1.1  christos 			}
   2528  1.1  christos 			/*
   2529  1.1  christos 			 * If the (soon to be former) current version
   2530  1.1  christos 			 * isn't being used by anyone, we can clean
   2531  1.1  christos 			 * it up.
   2532  1.1  christos 			 */
   2533  1.3  christos 			if (cur_ref == 1) {
   2534  1.1  christos 				cleanup_version = cur_version;
   2535  1.1  christos 				APPENDLIST(version->changed_list,
   2536  1.7  christos 					   cleanup_version->changed_list, link);
   2537  1.1  christos 			}
   2538  1.1  christos 			/*
   2539  1.1  christos 			 * Become the current version.
   2540  1.1  christos 			 */
   2541  1.3  christos 			version->writer = false;
   2542  1.1  christos 			rbtdb->current_version = version;
   2543  1.1  christos 			rbtdb->current_serial = version->serial;
   2544  1.1  christos 			rbtdb->future_version = NULL;
   2545  1.1  christos 
   2546  1.1  christos 			/*
   2547  1.1  christos 			 * Keep the current version in the open list, and
   2548  1.1  christos 			 * gain a reference for the DB itself (see the DB
   2549  1.1  christos 			 * creation function below).  This must be the only
   2550  1.1  christos 			 * case where we need to increment the counter from
   2551  1.1  christos 			 * zero and need to use isc_refcount_increment0().
   2552  1.1  christos 			 */
   2553  1.7  christos 			INSIST(isc_refcount_increment0(&version->references) ==
   2554  1.7  christos 			       0);
   2555  1.7  christos 			PREPEND(rbtdb->open_versions, rbtdb->current_version,
   2556  1.7  christos 				link);
   2557  1.1  christos 			resigned_list = version->resigned_list;
   2558  1.1  christos 			ISC_LIST_INIT(version->resigned_list);
   2559  1.1  christos 		} else {
   2560  1.1  christos 			/*
   2561  1.1  christos 			 * We're rolling back this transaction.
   2562  1.1  christos 			 */
   2563  1.1  christos 			cleanup_list = version->changed_list;
   2564  1.1  christos 			ISC_LIST_INIT(version->changed_list);
   2565  1.1  christos 			resigned_list = version->resigned_list;
   2566  1.1  christos 			ISC_LIST_INIT(version->resigned_list);
   2567  1.3  christos 			rollback = true;
   2568  1.1  christos 			cleanup_version = version;
   2569  1.1  christos 			rbtdb->future_version = NULL;
   2570  1.1  christos 		}
   2571  1.1  christos 	} else {
   2572  1.1  christos 		if (version != rbtdb->current_version) {
   2573  1.1  christos 			/*
   2574  1.1  christos 			 * There are no external or internal references
   2575  1.1  christos 			 * to this version and it can be cleaned up.
   2576  1.1  christos 			 */
   2577  1.1  christos 			cleanup_version = version;
   2578  1.1  christos 
   2579  1.1  christos 			/*
   2580  1.1  christos 			 * Find the version with the least serial
   2581  1.1  christos 			 * number greater than ours.
   2582  1.1  christos 			 */
   2583  1.1  christos 			least_greater = PREV(version, link);
   2584  1.7  christos 			if (least_greater == NULL) {
   2585  1.1  christos 				least_greater = rbtdb->current_version;
   2586  1.7  christos 			}
   2587  1.1  christos 
   2588  1.1  christos 			INSIST(version->serial < least_greater->serial);
   2589  1.1  christos 			/*
   2590  1.1  christos 			 * Is this the least open version?
   2591  1.1  christos 			 */
   2592  1.1  christos 			if (version->serial == rbtdb->least_serial) {
   2593  1.1  christos 				/*
   2594  1.1  christos 				 * Yes.  Install the new least open
   2595  1.1  christos 				 * version.
   2596  1.1  christos 				 */
   2597  1.7  christos 				make_least_version(rbtdb, least_greater,
   2598  1.1  christos 						   &cleanup_list);
   2599  1.1  christos 			} else {
   2600  1.1  christos 				/*
   2601  1.1  christos 				 * Add any unexecuted cleanups to
   2602  1.1  christos 				 * those of the least greater version.
   2603  1.1  christos 				 */
   2604  1.1  christos 				APPENDLIST(least_greater->changed_list,
   2605  1.7  christos 					   version->changed_list, link);
   2606  1.1  christos 			}
   2607  1.7  christos 		} else if (version->serial == rbtdb->least_serial) {
   2608  1.1  christos 			INSIST(EMPTY(version->changed_list));
   2609  1.7  christos 		}
   2610  1.1  christos 		UNLINK(rbtdb->open_versions, version, link);
   2611  1.1  christos 	}
   2612  1.1  christos 	least_serial = rbtdb->least_serial;
   2613  1.1  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   2614  1.1  christos 
   2615  1.1  christos 	if (cleanup_version != NULL) {
   2616  1.1  christos 		INSIST(EMPTY(cleanup_version->changed_list));
   2617  1.1  christos 		free_gluetable(cleanup_version);
   2618  1.1  christos 		isc_rwlock_destroy(&cleanup_version->glue_rwlock);
   2619  1.1  christos 		isc_rwlock_destroy(&cleanup_version->rwlock);
   2620  1.1  christos 		isc_mem_put(rbtdb->common.mctx, cleanup_version,
   2621  1.1  christos 			    sizeof(*cleanup_version));
   2622  1.1  christos 	}
   2623  1.1  christos 
   2624  1.1  christos 	/*
   2625  1.1  christos 	 * Commit/rollback re-signed headers.
   2626  1.1  christos 	 */
   2627  1.7  christos 	for (header = HEAD(resigned_list); header != NULL;
   2628  1.1  christos 	     header = HEAD(resigned_list)) {
   2629  1.1  christos 		nodelock_t *lock;
   2630  1.1  christos 
   2631  1.1  christos 		ISC_LIST_UNLINK(resigned_list, header, link);
   2632  1.1  christos 
   2633  1.1  christos 		lock = &rbtdb->node_locks[header->node->locknum].lock;
   2634  1.1  christos 		NODE_LOCK(lock, isc_rwlocktype_write);
   2635  1.1  christos 		if (rollback && !IGNORE(header)) {
   2636  1.1  christos 			isc_result_t result;
   2637  1.1  christos 			result = resign_insert(rbtdb, header->node->locknum,
   2638  1.1  christos 					       header);
   2639  1.7  christos 			if (result != ISC_R_SUCCESS) {
   2640  1.1  christos 				isc_log_write(dns_lctx,
   2641  1.1  christos 					      DNS_LOGCATEGORY_DATABASE,
   2642  1.1  christos 					      DNS_LOGMODULE_ZONE, ISC_LOG_ERROR,
   2643  1.1  christos 					      "Unable to reinsert header to "
   2644  1.1  christos 					      "re-signing heap: %s",
   2645  1.7  christos 					      dns_result_totext(result));
   2646  1.7  christos 			}
   2647  1.1  christos 		}
   2648  1.1  christos 		decrement_reference(rbtdb, header->node, least_serial,
   2649  1.1  christos 				    isc_rwlocktype_write, isc_rwlocktype_none,
   2650  1.3  christos 				    false);
   2651  1.1  christos 		NODE_UNLOCK(lock, isc_rwlocktype_write);
   2652  1.1  christos 	}
   2653  1.1  christos 
   2654  1.1  christos 	if (!EMPTY(cleanup_list)) {
   2655  1.1  christos 		isc_event_t *event = NULL;
   2656  1.1  christos 		isc_rwlocktype_t tlock = isc_rwlocktype_none;
   2657  1.1  christos 
   2658  1.7  christos 		if (rbtdb->task != NULL) {
   2659  1.1  christos 			event = isc_event_allocate(rbtdb->common.mctx, NULL,
   2660  1.1  christos 						   DNS_EVENT_RBTDEADNODES,
   2661  1.1  christos 						   cleanup_dead_nodes_callback,
   2662  1.1  christos 						   rbtdb, sizeof(isc_event_t));
   2663  1.7  christos 		}
   2664  1.1  christos 		if (event == NULL) {
   2665  1.1  christos 			/*
   2666  1.1  christos 			 * We acquire a tree write lock here in order to make
   2667  1.1  christos 			 * sure that stale nodes will be removed in
   2668  1.1  christos 			 * decrement_reference().  If we didn't have the lock,
   2669  1.1  christos 			 * those nodes could miss the chance to be removed
   2670  1.1  christos 			 * until the server stops.  The write lock is
   2671  1.1  christos 			 * expensive, but this event should be rare enough
   2672  1.1  christos 			 * to justify the cost.
   2673  1.1  christos 			 */
   2674  1.1  christos 			RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   2675  1.1  christos 			tlock = isc_rwlocktype_write;
   2676  1.1  christos 		}
   2677  1.1  christos 
   2678  1.7  christos 		for (changed = HEAD(cleanup_list); changed != NULL;
   2679  1.1  christos 		     changed = next_changed) {
   2680  1.1  christos 			nodelock_t *lock;
   2681  1.1  christos 
   2682  1.1  christos 			next_changed = NEXT(changed, link);
   2683  1.1  christos 			rbtnode = changed->node;
   2684  1.1  christos 			lock = &rbtdb->node_locks[rbtnode->locknum].lock;
   2685  1.1  christos 
   2686  1.1  christos 			NODE_LOCK(lock, isc_rwlocktype_write);
   2687  1.1  christos 			/*
   2688  1.1  christos 			 * This is a good opportunity to purge any dead nodes,
   2689  1.1  christos 			 * so use it.
   2690  1.1  christos 			 */
   2691  1.7  christos 			if (event == NULL) {
   2692  1.1  christos 				cleanup_dead_nodes(rbtdb, rbtnode->locknum);
   2693  1.7  christos 			}
   2694  1.1  christos 
   2695  1.7  christos 			if (rollback) {
   2696  1.1  christos 				rollback_node(rbtnode, serial);
   2697  1.7  christos 			}
   2698  1.1  christos 			decrement_reference(rbtdb, rbtnode, least_serial,
   2699  1.7  christos 					    isc_rwlocktype_write, tlock, false);
   2700  1.1  christos 
   2701  1.1  christos 			NODE_UNLOCK(lock, isc_rwlocktype_write);
   2702  1.1  christos 
   2703  1.1  christos 			isc_mem_put(rbtdb->common.mctx, changed,
   2704  1.1  christos 				    sizeof(*changed));
   2705  1.1  christos 		}
   2706  1.1  christos 		if (event != NULL) {
   2707  1.3  christos 			isc_refcount_increment(&rbtdb->references);
   2708  1.1  christos 			isc_task_send(rbtdb->task, &event);
   2709  1.7  christos 		} else {
   2710  1.1  christos 			RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   2711  1.7  christos 		}
   2712  1.1  christos 	}
   2713  1.1  christos 
   2714  1.7  christos end:
   2715  1.1  christos 	*versionp = NULL;
   2716  1.1  christos }
   2717  1.1  christos 
   2718  1.1  christos /*
   2719  1.1  christos  * Add the necessary magic for the wildcard name 'name'
   2720  1.1  christos  * to be found in 'rbtdb'.
   2721  1.1  christos  *
   2722  1.1  christos  * In order for wildcard matching to work correctly in
   2723  1.1  christos  * zone_find(), we must ensure that a node for the wildcarding
   2724  1.1  christos  * level exists in the database, and has its 'find_callback'
   2725  1.1  christos  * and 'wild' bits set.
   2726  1.1  christos  *
   2727  1.1  christos  * E.g. if the wildcard name is "*.sub.example." then we
   2728  1.1  christos  * must ensure that "sub.example." exists and is marked as
   2729  1.1  christos  * a wildcard level.
   2730  1.7  christos  *
   2731  1.7  christos  * tree_lock(write) must be held.
   2732  1.1  christos  */
   2733  1.1  christos static isc_result_t
   2734  1.1  christos add_wildcard_magic(dns_rbtdb_t *rbtdb, const dns_name_t *name) {
   2735  1.1  christos 	isc_result_t result;
   2736  1.1  christos 	dns_name_t foundname;
   2737  1.1  christos 	dns_offsets_t offsets;
   2738  1.1  christos 	unsigned int n;
   2739  1.1  christos 	dns_rbtnode_t *node = NULL;
   2740  1.1  christos 
   2741  1.1  christos 	dns_name_init(&foundname, offsets);
   2742  1.1  christos 	n = dns_name_countlabels(name);
   2743  1.1  christos 	INSIST(n >= 2);
   2744  1.1  christos 	n--;
   2745  1.1  christos 	dns_name_getlabelsequence(name, 1, n, &foundname);
   2746  1.1  christos 	result = dns_rbt_addnode(rbtdb->tree, &foundname, &node);
   2747  1.7  christos 	if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
   2748  1.1  christos 		return (result);
   2749  1.7  christos 	}
   2750  1.7  christos 	if (result == ISC_R_SUCCESS) {
   2751  1.1  christos 		node->nsec = DNS_RBT_NSEC_NORMAL;
   2752  1.7  christos 	}
   2753  1.1  christos 	node->find_callback = 1;
   2754  1.1  christos 	node->wild = 1;
   2755  1.1  christos 	return (ISC_R_SUCCESS);
   2756  1.1  christos }
   2757  1.1  christos 
   2758  1.7  christos /*
   2759  1.7  christos  * tree_lock(write) must be held.
   2760  1.7  christos  */
   2761  1.1  christos static isc_result_t
   2762  1.1  christos add_empty_wildcards(dns_rbtdb_t *rbtdb, const dns_name_t *name) {
   2763  1.1  christos 	isc_result_t result;
   2764  1.1  christos 	dns_name_t foundname;
   2765  1.1  christos 	dns_offsets_t offsets;
   2766  1.1  christos 	unsigned int n, l, i;
   2767  1.1  christos 
   2768  1.1  christos 	dns_name_init(&foundname, offsets);
   2769  1.1  christos 	n = dns_name_countlabels(name);
   2770  1.1  christos 	l = dns_name_countlabels(&rbtdb->common.origin);
   2771  1.1  christos 	i = l + 1;
   2772  1.1  christos 	while (i < n) {
   2773  1.7  christos 		dns_rbtnode_t *node = NULL; /* dummy */
   2774  1.1  christos 		dns_name_getlabelsequence(name, n - i, i, &foundname);
   2775  1.1  christos 		if (dns_name_iswildcard(&foundname)) {
   2776  1.1  christos 			result = add_wildcard_magic(rbtdb, &foundname);
   2777  1.7  christos 			if (result != ISC_R_SUCCESS) {
   2778  1.1  christos 				return (result);
   2779  1.7  christos 			}
   2780  1.1  christos 			result = dns_rbt_addnode(rbtdb->tree, &foundname,
   2781  1.1  christos 						 &node);
   2782  1.7  christos 			if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
   2783  1.1  christos 				return (result);
   2784  1.7  christos 			}
   2785  1.7  christos 			if (result == ISC_R_SUCCESS) {
   2786  1.1  christos 				node->nsec = DNS_RBT_NSEC_NORMAL;
   2787  1.7  christos 			}
   2788  1.1  christos 		}
   2789  1.1  christos 		i++;
   2790  1.1  christos 	}
   2791  1.1  christos 	return (ISC_R_SUCCESS);
   2792  1.1  christos }
   2793  1.1  christos 
   2794  1.1  christos static isc_result_t
   2795  1.1  christos findnodeintree(dns_rbtdb_t *rbtdb, dns_rbt_t *tree, const dns_name_t *name,
   2796  1.7  christos 	       bool create, dns_dbnode_t **nodep) {
   2797  1.1  christos 	dns_rbtnode_t *node = NULL;
   2798  1.1  christos 	dns_name_t nodename;
   2799  1.1  christos 	isc_result_t result;
   2800  1.1  christos 	isc_rwlocktype_t locktype = isc_rwlocktype_read;
   2801  1.1  christos 
   2802  1.1  christos 	INSIST(tree == rbtdb->tree || tree == rbtdb->nsec3);
   2803  1.1  christos 
   2804  1.1  christos 	dns_name_init(&nodename, NULL);
   2805  1.1  christos 	RWLOCK(&rbtdb->tree_lock, locktype);
   2806  1.1  christos 	result = dns_rbt_findnode(tree, name, NULL, &node, NULL,
   2807  1.1  christos 				  DNS_RBTFIND_EMPTYDATA, NULL, NULL);
   2808  1.1  christos 	if (result != ISC_R_SUCCESS) {
   2809  1.1  christos 		RWUNLOCK(&rbtdb->tree_lock, locktype);
   2810  1.1  christos 		if (!create) {
   2811  1.7  christos 			if (result == DNS_R_PARTIALMATCH) {
   2812  1.1  christos 				result = ISC_R_NOTFOUND;
   2813  1.7  christos 			}
   2814  1.1  christos 			return (result);
   2815  1.1  christos 		}
   2816  1.1  christos 		/*
   2817  1.1  christos 		 * It would be nice to try to upgrade the lock instead of
   2818  1.1  christos 		 * unlocking then relocking.
   2819  1.1  christos 		 */
   2820  1.1  christos 		locktype = isc_rwlocktype_write;
   2821  1.1  christos 		RWLOCK(&rbtdb->tree_lock, locktype);
   2822  1.1  christos 		node = NULL;
   2823  1.1  christos 		result = dns_rbt_addnode(tree, name, &node);
   2824  1.1  christos 		if (result == ISC_R_SUCCESS) {
   2825  1.1  christos 			dns_rbt_namefromnode(node, &nodename);
   2826  1.1  christos 			node->locknum = node->hashval % rbtdb->node_lock_count;
   2827  1.1  christos 			if (tree == rbtdb->tree) {
   2828  1.1  christos 				add_empty_wildcards(rbtdb, name);
   2829  1.1  christos 
   2830  1.1  christos 				if (dns_name_iswildcard(name)) {
   2831  1.3  christos 					result = add_wildcard_magic(rbtdb,
   2832  1.3  christos 								    name);
   2833  1.1  christos 					if (result != ISC_R_SUCCESS) {
   2834  1.3  christos 						RWUNLOCK(&rbtdb->tree_lock,
   2835  1.3  christos 							 locktype);
   2836  1.1  christos 						return (result);
   2837  1.1  christos 					}
   2838  1.1  christos 				}
   2839  1.1  christos 			}
   2840  1.7  christos 			if (tree == rbtdb->nsec3) {
   2841  1.1  christos 				node->nsec = DNS_RBT_NSEC_NSEC3;
   2842  1.7  christos 			}
   2843  1.1  christos 		} else if (result != ISC_R_EXISTS) {
   2844  1.1  christos 			RWUNLOCK(&rbtdb->tree_lock, locktype);
   2845  1.1  christos 			return (result);
   2846  1.1  christos 		}
   2847  1.1  christos 	}
   2848  1.1  christos 
   2849  1.7  christos 	if (tree == rbtdb->nsec3) {
   2850  1.1  christos 		INSIST(node->nsec == DNS_RBT_NSEC_NSEC3);
   2851  1.7  christos 	}
   2852  1.1  christos 
   2853  1.1  christos 	reactivate_node(rbtdb, node, locktype);
   2854  1.1  christos 
   2855  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, locktype);
   2856  1.1  christos 
   2857  1.1  christos 	*nodep = (dns_dbnode_t *)node;
   2858  1.1  christos 
   2859  1.1  christos 	return (ISC_R_SUCCESS);
   2860  1.1  christos }
   2861  1.1  christos 
   2862  1.1  christos static isc_result_t
   2863  1.3  christos findnode(dns_db_t *db, const dns_name_t *name, bool create,
   2864  1.7  christos 	 dns_dbnode_t **nodep) {
   2865  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   2866  1.1  christos 
   2867  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   2868  1.1  christos 
   2869  1.1  christos 	return (findnodeintree(rbtdb, rbtdb->tree, name, create, nodep));
   2870  1.1  christos }
   2871  1.1  christos 
   2872  1.1  christos static isc_result_t
   2873  1.3  christos findnsec3node(dns_db_t *db, const dns_name_t *name, bool create,
   2874  1.7  christos 	      dns_dbnode_t **nodep) {
   2875  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   2876  1.1  christos 
   2877  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   2878  1.1  christos 
   2879  1.1  christos 	return (findnodeintree(rbtdb, rbtdb->nsec3, name, create, nodep));
   2880  1.1  christos }
   2881  1.1  christos 
   2882  1.1  christos static isc_result_t
   2883  1.1  christos zone_zonecut_callback(dns_rbtnode_t *node, dns_name_t *name, void *arg) {
   2884  1.1  christos 	rbtdb_search_t *search = arg;
   2885  1.1  christos 	rdatasetheader_t *header, *header_next;
   2886  1.1  christos 	rdatasetheader_t *dname_header, *sigdname_header, *ns_header;
   2887  1.1  christos 	rdatasetheader_t *found;
   2888  1.1  christos 	isc_result_t result;
   2889  1.1  christos 	dns_rbtnode_t *onode;
   2890  1.1  christos 
   2891  1.1  christos 	/*
   2892  1.1  christos 	 * We only want to remember the topmost zone cut, since it's the one
   2893  1.1  christos 	 * that counts, so we'll just continue if we've already found a
   2894  1.1  christos 	 * zonecut.
   2895  1.1  christos 	 */
   2896  1.7  christos 	if (search->zonecut != NULL) {
   2897  1.1  christos 		return (DNS_R_CONTINUE);
   2898  1.7  christos 	}
   2899  1.1  christos 
   2900  1.1  christos 	found = NULL;
   2901  1.1  christos 	result = DNS_R_CONTINUE;
   2902  1.1  christos 	onode = search->rbtdb->origin_node;
   2903  1.1  christos 
   2904  1.1  christos 	NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
   2905  1.1  christos 		  isc_rwlocktype_read);
   2906  1.1  christos 
   2907  1.1  christos 	/*
   2908  1.1  christos 	 * Look for an NS or DNAME rdataset active in our version.
   2909  1.1  christos 	 */
   2910  1.1  christos 	ns_header = NULL;
   2911  1.1  christos 	dname_header = NULL;
   2912  1.1  christos 	sigdname_header = NULL;
   2913  1.1  christos 	for (header = node->data; header != NULL; header = header_next) {
   2914  1.1  christos 		header_next = header->next;
   2915  1.1  christos 		if (header->type == dns_rdatatype_ns ||
   2916  1.1  christos 		    header->type == dns_rdatatype_dname ||
   2917  1.7  christos 		    header->type == RBTDB_RDATATYPE_SIGDNAME)
   2918  1.7  christos 		{
   2919  1.1  christos 			do {
   2920  1.1  christos 				if (header->serial <= search->serial &&
   2921  1.1  christos 				    !IGNORE(header)) {
   2922  1.1  christos 					/*
   2923  1.1  christos 					 * Is this a "this rdataset doesn't
   2924  1.1  christos 					 * exist" record?
   2925  1.1  christos 					 */
   2926  1.7  christos 					if (NONEXISTENT(header)) {
   2927  1.1  christos 						header = NULL;
   2928  1.7  christos 					}
   2929  1.1  christos 					break;
   2930  1.7  christos 				} else {
   2931  1.1  christos 					header = header->down;
   2932  1.7  christos 				}
   2933  1.1  christos 			} while (header != NULL);
   2934  1.1  christos 			if (header != NULL) {
   2935  1.7  christos 				if (header->type == dns_rdatatype_dname) {
   2936  1.1  christos 					dname_header = header;
   2937  1.7  christos 				} else if (header->type ==
   2938  1.7  christos 					   RBTDB_RDATATYPE_SIGDNAME) {
   2939  1.1  christos 					sigdname_header = header;
   2940  1.7  christos 				} else if (node != onode ||
   2941  1.7  christos 					   IS_STUB(search->rbtdb)) {
   2942  1.1  christos 					/*
   2943  1.1  christos 					 * We've found an NS rdataset that
   2944  1.1  christos 					 * isn't at the origin node.  We check
   2945  1.1  christos 					 * that they're not at the origin node,
   2946  1.1  christos 					 * because otherwise we'd erroneously
   2947  1.1  christos 					 * treat the zone top as if it were
   2948  1.1  christos 					 * a delegation.
   2949  1.1  christos 					 */
   2950  1.1  christos 					ns_header = header;
   2951  1.1  christos 				}
   2952  1.1  christos 			}
   2953  1.1  christos 		}
   2954  1.1  christos 	}
   2955  1.1  christos 
   2956  1.1  christos 	/*
   2957  1.1  christos 	 * Did we find anything?
   2958  1.1  christos 	 */
   2959  1.1  christos 	if (!IS_CACHE(search->rbtdb) && !IS_STUB(search->rbtdb) &&
   2960  1.1  christos 	    ns_header != NULL) {
   2961  1.1  christos 		/*
   2962  1.1  christos 		 * Note that NS has precedence over DNAME if both exist
   2963  1.1  christos 		 * in a zone.  Otherwise DNAME take precedence over NS.
   2964  1.1  christos 		 */
   2965  1.1  christos 		found = ns_header;
   2966  1.1  christos 		search->zonecut_sigrdataset = NULL;
   2967  1.1  christos 	} else if (dname_header != NULL) {
   2968  1.1  christos 		found = dname_header;
   2969  1.1  christos 		search->zonecut_sigrdataset = sigdname_header;
   2970  1.1  christos 	} else if (ns_header != NULL) {
   2971  1.1  christos 		found = ns_header;
   2972  1.1  christos 		search->zonecut_sigrdataset = NULL;
   2973  1.1  christos 	}
   2974  1.1  christos 
   2975  1.1  christos 	if (found != NULL) {
   2976  1.1  christos 		/*
   2977  1.1  christos 		 * We increment the reference count on node to ensure that
   2978  1.1  christos 		 * search->zonecut_rdataset will still be valid later.
   2979  1.1  christos 		 */
   2980  1.1  christos 		new_reference(search->rbtdb, node);
   2981  1.1  christos 		search->zonecut = node;
   2982  1.1  christos 		search->zonecut_rdataset = found;
   2983  1.3  christos 		search->need_cleanup = true;
   2984  1.1  christos 		/*
   2985  1.1  christos 		 * Since we've found a zonecut, anything beneath it is
   2986  1.1  christos 		 * glue and is not subject to wildcard matching, so we
   2987  1.1  christos 		 * may clear search->wild.
   2988  1.1  christos 		 */
   2989  1.3  christos 		search->wild = false;
   2990  1.1  christos 		if ((search->options & DNS_DBFIND_GLUEOK) == 0) {
   2991  1.1  christos 			/*
   2992  1.1  christos 			 * If the caller does not want to find glue, then
   2993  1.1  christos 			 * this is the best answer and the search should
   2994  1.1  christos 			 * stop now.
   2995  1.1  christos 			 */
   2996  1.1  christos 			result = DNS_R_PARTIALMATCH;
   2997  1.1  christos 		} else {
   2998  1.1  christos 			dns_name_t *zcname;
   2999  1.1  christos 
   3000  1.1  christos 			/*
   3001  1.1  christos 			 * The search will continue beneath the zone cut.
   3002  1.1  christos 			 * This may or may not be the best match.  In case it
   3003  1.1  christos 			 * is, we need to remember the node name.
   3004  1.1  christos 			 */
   3005  1.1  christos 			zcname = dns_fixedname_name(&search->zonecut_name);
   3006  1.6  christos 			dns_name_copynf(name, zcname);
   3007  1.3  christos 			search->copy_name = true;
   3008  1.1  christos 		}
   3009  1.1  christos 	} else {
   3010  1.1  christos 		/*
   3011  1.1  christos 		 * There is no zonecut at this node which is active in this
   3012  1.1  christos 		 * version.
   3013  1.1  christos 		 *
   3014  1.1  christos 		 * If this is a "wild" node and the caller hasn't disabled
   3015  1.1  christos 		 * wildcard matching, remember that we've seen a wild node
   3016  1.1  christos 		 * in case we need to go searching for wildcard matches
   3017  1.1  christos 		 * later on.
   3018  1.1  christos 		 */
   3019  1.7  christos 		if (node->wild && (search->options & DNS_DBFIND_NOWILD) == 0) {
   3020  1.3  christos 			search->wild = true;
   3021  1.7  christos 		}
   3022  1.1  christos 	}
   3023  1.1  christos 
   3024  1.1  christos 	NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
   3025  1.1  christos 		    isc_rwlocktype_read);
   3026  1.1  christos 
   3027  1.1  christos 	return (result);
   3028  1.1  christos }
   3029  1.1  christos 
   3030  1.1  christos static inline void
   3031  1.7  christos bind_rdataset(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, rdatasetheader_t *header,
   3032  1.7  christos 	      isc_stdtime_t now, dns_rdataset_t *rdataset) {
   3033  1.7  christos 	unsigned char *raw; /* RDATASLAB */
   3034  1.1  christos 
   3035  1.1  christos 	/*
   3036  1.1  christos 	 * Caller must be holding the node reader lock.
   3037  1.1  christos 	 * XXXJT: technically, we need a writer lock, since we'll increment
   3038  1.1  christos 	 * the header count below.  However, since the actual counter value
   3039  1.1  christos 	 * doesn't matter, we prioritize performance here.  (We may want to
   3040  1.1  christos 	 * use atomic increment when available).
   3041  1.1  christos 	 */
   3042  1.1  christos 
   3043  1.7  christos 	if (rdataset == NULL) {
   3044  1.1  christos 		return;
   3045  1.7  christos 	}
   3046  1.1  christos 
   3047  1.1  christos 	new_reference(rbtdb, node);
   3048  1.1  christos 
   3049  1.7  christos 	INSIST(rdataset->methods == NULL); /* We must be disassociated. */
   3050  1.1  christos 
   3051  1.1  christos 	rdataset->methods = &rdataset_methods;
   3052  1.1  christos 	rdataset->rdclass = rbtdb->common.rdclass;
   3053  1.1  christos 	rdataset->type = RBTDB_RDATATYPE_BASE(header->type);
   3054  1.1  christos 	rdataset->covers = RBTDB_RDATATYPE_EXT(header->type);
   3055  1.1  christos 	rdataset->ttl = header->rdh_ttl - now;
   3056  1.1  christos 	rdataset->trust = header->trust;
   3057  1.7  christos 	if (NEGATIVE(header)) {
   3058  1.1  christos 		rdataset->attributes |= DNS_RDATASETATTR_NEGATIVE;
   3059  1.7  christos 	}
   3060  1.7  christos 	if (NXDOMAIN(header)) {
   3061  1.1  christos 		rdataset->attributes |= DNS_RDATASETATTR_NXDOMAIN;
   3062  1.7  christos 	}
   3063  1.7  christos 	if (OPTOUT(header)) {
   3064  1.1  christos 		rdataset->attributes |= DNS_RDATASETATTR_OPTOUT;
   3065  1.7  christos 	}
   3066  1.7  christos 	if (PREFETCH(header)) {
   3067  1.1  christos 		rdataset->attributes |= DNS_RDATASETATTR_PREFETCH;
   3068  1.7  christos 	}
   3069  1.1  christos 	if (STALE(header)) {
   3070  1.1  christos 		rdataset->attributes |= DNS_RDATASETATTR_STALE;
   3071  1.4  christos 		rdataset->stale_ttl =
   3072  1.4  christos 			(rbtdb->serve_stale_ttl + header->rdh_ttl) - now;
   3073  1.1  christos 		rdataset->ttl = 0;
   3074  1.1  christos 	}
   3075  1.1  christos 	rdataset->private1 = rbtdb;
   3076  1.1  christos 	rdataset->private2 = node;
   3077  1.1  christos 	raw = (unsigned char *)header + sizeof(*header);
   3078  1.1  christos 	rdataset->private3 = raw;
   3079  1.7  christos 	rdataset->count = atomic_fetch_add_relaxed(&header->count, 1);
   3080  1.7  christos 	if (rdataset->count == UINT32_MAX) {
   3081  1.1  christos 		rdataset->count = 0;
   3082  1.7  christos 	}
   3083  1.1  christos 
   3084  1.1  christos 	/*
   3085  1.1  christos 	 * Reset iterator state.
   3086  1.1  christos 	 */
   3087  1.1  christos 	rdataset->privateuint4 = 0;
   3088  1.1  christos 	rdataset->private5 = NULL;
   3089  1.1  christos 
   3090  1.1  christos 	/*
   3091  1.1  christos 	 * Add noqname proof.
   3092  1.1  christos 	 */
   3093  1.1  christos 	rdataset->private6 = header->noqname;
   3094  1.7  christos 	if (rdataset->private6 != NULL) {
   3095  1.7  christos 		rdataset->attributes |= DNS_RDATASETATTR_NOQNAME;
   3096  1.7  christos 	}
   3097  1.1  christos 	rdataset->private7 = header->closest;
   3098  1.7  christos 	if (rdataset->private7 != NULL) {
   3099  1.7  christos 		rdataset->attributes |= DNS_RDATASETATTR_CLOSEST;
   3100  1.7  christos 	}
   3101  1.1  christos 
   3102  1.1  christos 	/*
   3103  1.1  christos 	 * Copy out re-signing information.
   3104  1.1  christos 	 */
   3105  1.1  christos 	if (RESIGN(header)) {
   3106  1.7  christos 		rdataset->attributes |= DNS_RDATASETATTR_RESIGN;
   3107  1.1  christos 		rdataset->resign = (header->resign << 1) | header->resign_lsb;
   3108  1.7  christos 	} else {
   3109  1.1  christos 		rdataset->resign = 0;
   3110  1.7  christos 	}
   3111  1.1  christos }
   3112  1.1  christos 
   3113  1.1  christos static inline isc_result_t
   3114  1.1  christos setup_delegation(rbtdb_search_t *search, dns_dbnode_t **nodep,
   3115  1.1  christos 		 dns_name_t *foundname, dns_rdataset_t *rdataset,
   3116  1.7  christos 		 dns_rdataset_t *sigrdataset) {
   3117  1.1  christos 	dns_name_t *zcname;
   3118  1.1  christos 	rbtdb_rdatatype_t type;
   3119  1.1  christos 	dns_rbtnode_t *node;
   3120  1.1  christos 
   3121  1.1  christos 	/*
   3122  1.1  christos 	 * The caller MUST NOT be holding any node locks.
   3123  1.1  christos 	 */
   3124  1.1  christos 
   3125  1.1  christos 	node = search->zonecut;
   3126  1.1  christos 	type = search->zonecut_rdataset->type;
   3127  1.1  christos 
   3128  1.1  christos 	/*
   3129  1.1  christos 	 * If we have to set foundname, we do it before anything else.
   3130  1.1  christos 	 * If we were to set foundname after we had set nodep or bound the
   3131  1.1  christos 	 * rdataset, then we'd have to undo that work if dns_name_copy()
   3132  1.1  christos 	 * failed.  By setting foundname first, there's nothing to undo if
   3133  1.1  christos 	 * we have trouble.
   3134  1.1  christos 	 */
   3135  1.1  christos 	if (foundname != NULL && search->copy_name) {
   3136  1.1  christos 		zcname = dns_fixedname_name(&search->zonecut_name);
   3137  1.6  christos 		dns_name_copynf(zcname, foundname);
   3138  1.1  christos 	}
   3139  1.1  christos 	if (nodep != NULL) {
   3140  1.1  christos 		/*
   3141  1.1  christos 		 * Note that we don't have to increment the node's reference
   3142  1.1  christos 		 * count here because we're going to use the reference we
   3143  1.1  christos 		 * already have in the search block.
   3144  1.1  christos 		 */
   3145  1.1  christos 		*nodep = node;
   3146  1.3  christos 		search->need_cleanup = false;
   3147  1.1  christos 	}
   3148  1.1  christos 	if (rdataset != NULL) {
   3149  1.1  christos 		NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
   3150  1.1  christos 			  isc_rwlocktype_read);
   3151  1.1  christos 		bind_rdataset(search->rbtdb, node, search->zonecut_rdataset,
   3152  1.1  christos 			      search->now, rdataset);
   3153  1.1  christos 		if (sigrdataset != NULL && search->zonecut_sigrdataset != NULL)
   3154  1.7  christos 		{
   3155  1.1  christos 			bind_rdataset(search->rbtdb, node,
   3156  1.7  christos 				      search->zonecut_sigrdataset, search->now,
   3157  1.7  christos 				      sigrdataset);
   3158  1.7  christos 		}
   3159  1.1  christos 		NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
   3160  1.1  christos 			    isc_rwlocktype_read);
   3161  1.1  christos 	}
   3162  1.1  christos 
   3163  1.7  christos 	if (type == dns_rdatatype_dname) {
   3164  1.1  christos 		return (DNS_R_DNAME);
   3165  1.7  christos 	}
   3166  1.1  christos 	return (DNS_R_DELEGATION);
   3167  1.1  christos }
   3168  1.1  christos 
   3169  1.3  christos static inline bool
   3170  1.1  christos valid_glue(rbtdb_search_t *search, dns_name_t *name, rbtdb_rdatatype_t type,
   3171  1.7  christos 	   dns_rbtnode_t *node) {
   3172  1.7  christos 	unsigned char *raw; /* RDATASLAB */
   3173  1.1  christos 	unsigned int count, size;
   3174  1.1  christos 	dns_name_t ns_name;
   3175  1.3  christos 	bool valid = false;
   3176  1.1  christos 	dns_offsets_t offsets;
   3177  1.1  christos 	isc_region_t region;
   3178  1.1  christos 	rdatasetheader_t *header;
   3179  1.1  christos 
   3180  1.1  christos 	/*
   3181  1.1  christos 	 * No additional locking is required.
   3182  1.1  christos 	 */
   3183  1.1  christos 
   3184  1.1  christos 	/*
   3185  1.1  christos 	 * Valid glue types are A, AAAA, A6.  NS is also a valid glue type
   3186  1.1  christos 	 * if it occurs at a zone cut, but is not valid below it.
   3187  1.1  christos 	 */
   3188  1.1  christos 	if (type == dns_rdatatype_ns) {
   3189  1.1  christos 		if (node != search->zonecut) {
   3190  1.3  christos 			return (false);
   3191  1.1  christos 		}
   3192  1.7  christos 	} else if (type != dns_rdatatype_a && type != dns_rdatatype_aaaa &&
   3193  1.7  christos 		   type != dns_rdatatype_a6)
   3194  1.7  christos 	{
   3195  1.3  christos 		return (false);
   3196  1.1  christos 	}
   3197  1.1  christos 
   3198  1.1  christos 	header = search->zonecut_rdataset;
   3199  1.1  christos 	raw = (unsigned char *)header + sizeof(*header);
   3200  1.1  christos 	count = raw[0] * 256 + raw[1];
   3201  1.5  christos 	raw += DNS_RDATASET_COUNT + DNS_RDATASET_LENGTH;
   3202  1.1  christos 
   3203  1.1  christos 	while (count > 0) {
   3204  1.1  christos 		count--;
   3205  1.1  christos 		size = raw[0] * 256 + raw[1];
   3206  1.5  christos 		raw += DNS_RDATASET_ORDER + DNS_RDATASET_LENGTH;
   3207  1.1  christos 		region.base = raw;
   3208  1.1  christos 		region.length = size;
   3209  1.1  christos 		raw += size;
   3210  1.1  christos 		/*
   3211  1.1  christos 		 * XXX Until we have rdata structures, we have no choice but
   3212  1.1  christos 		 * to directly access the rdata format.
   3213  1.1  christos 		 */
   3214  1.1  christos 		dns_name_init(&ns_name, offsets);
   3215  1.1  christos 		dns_name_fromregion(&ns_name, &region);
   3216  1.1  christos 		if (dns_name_compare(&ns_name, name) == 0) {
   3217  1.3  christos 			valid = true;
   3218  1.1  christos 			break;
   3219  1.1  christos 		}
   3220  1.1  christos 	}
   3221  1.1  christos 
   3222  1.1  christos 	return (valid);
   3223  1.1  christos }
   3224  1.1  christos 
   3225  1.3  christos static inline bool
   3226  1.1  christos activeempty(rbtdb_search_t *search, dns_rbtnodechain_t *chain,
   3227  1.7  christos 	    const dns_name_t *name) {
   3228  1.1  christos 	dns_fixedname_t fnext;
   3229  1.1  christos 	dns_fixedname_t forigin;
   3230  1.1  christos 	dns_name_t *next;
   3231  1.1  christos 	dns_name_t *origin;
   3232  1.1  christos 	dns_name_t prefix;
   3233  1.1  christos 	dns_rbtdb_t *rbtdb;
   3234  1.1  christos 	dns_rbtnode_t *node;
   3235  1.1  christos 	isc_result_t result;
   3236  1.3  christos 	bool answer = false;
   3237  1.1  christos 	rdatasetheader_t *header;
   3238  1.1  christos 
   3239  1.1  christos 	rbtdb = search->rbtdb;
   3240  1.1  christos 
   3241  1.1  christos 	dns_name_init(&prefix, NULL);
   3242  1.1  christos 	next = dns_fixedname_initname(&fnext);
   3243  1.1  christos 	origin = dns_fixedname_initname(&forigin);
   3244  1.1  christos 
   3245  1.1  christos 	result = dns_rbtnodechain_next(chain, NULL, NULL);
   3246  1.1  christos 	while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
   3247  1.1  christos 		node = NULL;
   3248  1.7  christos 		result = dns_rbtnodechain_current(chain, &prefix, origin,
   3249  1.7  christos 						  &node);
   3250  1.7  christos 		if (result != ISC_R_SUCCESS) {
   3251  1.1  christos 			break;
   3252  1.7  christos 		}
   3253  1.1  christos 		NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
   3254  1.1  christos 			  isc_rwlocktype_read);
   3255  1.7  christos 		for (header = node->data; header != NULL; header = header->next)
   3256  1.7  christos 		{
   3257  1.1  christos 			if (header->serial <= search->serial &&
   3258  1.7  christos 			    !IGNORE(header) && EXISTS(header)) {
   3259  1.1  christos 				break;
   3260  1.7  christos 			}
   3261  1.1  christos 		}
   3262  1.1  christos 		NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
   3263  1.1  christos 			    isc_rwlocktype_read);
   3264  1.7  christos 		if (header != NULL) {
   3265  1.1  christos 			break;
   3266  1.7  christos 		}
   3267  1.1  christos 		result = dns_rbtnodechain_next(chain, NULL, NULL);
   3268  1.1  christos 	}
   3269  1.7  christos 	if (result == ISC_R_SUCCESS) {
   3270  1.1  christos 		result = dns_name_concatenate(&prefix, origin, next, NULL);
   3271  1.7  christos 	}
   3272  1.7  christos 	if (result == ISC_R_SUCCESS && dns_name_issubdomain(next, name)) {
   3273  1.3  christos 		answer = true;
   3274  1.7  christos 	}
   3275  1.1  christos 	return (answer);
   3276  1.1  christos }
   3277  1.1  christos 
   3278  1.3  christos static inline bool
   3279  1.1  christos activeemtpynode(rbtdb_search_t *search, const dns_name_t *qname,
   3280  1.7  christos 		dns_name_t *wname) {
   3281  1.1  christos 	dns_fixedname_t fnext;
   3282  1.1  christos 	dns_fixedname_t forigin;
   3283  1.1  christos 	dns_fixedname_t fprev;
   3284  1.1  christos 	dns_name_t *next;
   3285  1.1  christos 	dns_name_t *origin;
   3286  1.1  christos 	dns_name_t *prev;
   3287  1.1  christos 	dns_name_t name;
   3288  1.1  christos 	dns_name_t rname;
   3289  1.1  christos 	dns_name_t tname;
   3290  1.1  christos 	dns_rbtdb_t *rbtdb;
   3291  1.1  christos 	dns_rbtnode_t *node;
   3292  1.1  christos 	dns_rbtnodechain_t chain;
   3293  1.3  christos 	bool check_next = true;
   3294  1.3  christos 	bool check_prev = true;
   3295  1.3  christos 	bool answer = false;
   3296  1.1  christos 	isc_result_t result;
   3297  1.1  christos 	rdatasetheader_t *header;
   3298  1.1  christos 	unsigned int n;
   3299  1.1  christos 
   3300  1.1  christos 	rbtdb = search->rbtdb;
   3301  1.1  christos 
   3302  1.1  christos 	dns_name_init(&name, NULL);
   3303  1.1  christos 	dns_name_init(&tname, NULL);
   3304  1.1  christos 	dns_name_init(&rname, NULL);
   3305  1.1  christos 	next = dns_fixedname_initname(&fnext);
   3306  1.1  christos 	prev = dns_fixedname_initname(&fprev);
   3307  1.1  christos 	origin = dns_fixedname_initname(&forigin);
   3308  1.1  christos 
   3309  1.1  christos 	/*
   3310  1.1  christos 	 * Find if qname is at or below a empty node.
   3311  1.1  christos 	 * Use our own copy of the chain.
   3312  1.1  christos 	 */
   3313  1.1  christos 
   3314  1.1  christos 	chain = search->chain;
   3315  1.1  christos 	do {
   3316  1.1  christos 		node = NULL;
   3317  1.7  christos 		result = dns_rbtnodechain_current(&chain, &name, origin, &node);
   3318  1.7  christos 		if (result != ISC_R_SUCCESS) {
   3319  1.1  christos 			break;
   3320  1.7  christos 		}
   3321  1.1  christos 		NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
   3322  1.1  christos 			  isc_rwlocktype_read);
   3323  1.7  christos 		for (header = node->data; header != NULL; header = header->next)
   3324  1.7  christos 		{
   3325  1.1  christos 			if (header->serial <= search->serial &&
   3326  1.7  christos 			    !IGNORE(header) && EXISTS(header)) {
   3327  1.1  christos 				break;
   3328  1.7  christos 			}
   3329  1.1  christos 		}
   3330  1.1  christos 		NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
   3331  1.1  christos 			    isc_rwlocktype_read);
   3332  1.7  christos 		if (header != NULL) {
   3333  1.1  christos 			break;
   3334  1.7  christos 		}
   3335  1.1  christos 		result = dns_rbtnodechain_prev(&chain, NULL, NULL);
   3336  1.1  christos 	} while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN);
   3337  1.7  christos 	if (result == ISC_R_SUCCESS) {
   3338  1.1  christos 		result = dns_name_concatenate(&name, origin, prev, NULL);
   3339  1.7  christos 	}
   3340  1.7  christos 	if (result != ISC_R_SUCCESS) {
   3341  1.3  christos 		check_prev = false;
   3342  1.7  christos 	}
   3343  1.1  christos 
   3344  1.1  christos 	result = dns_rbtnodechain_next(&chain, NULL, NULL);
   3345  1.1  christos 	while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
   3346  1.1  christos 		node = NULL;
   3347  1.7  christos 		result = dns_rbtnodechain_current(&chain, &name, origin, &node);
   3348  1.7  christos 		if (result != ISC_R_SUCCESS) {
   3349  1.1  christos 			break;
   3350  1.7  christos 		}
   3351  1.1  christos 		NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
   3352  1.1  christos 			  isc_rwlocktype_read);
   3353  1.7  christos 		for (header = node->data; header != NULL; header = header->next)
   3354  1.7  christos 		{
   3355  1.1  christos 			if (header->serial <= search->serial &&
   3356  1.7  christos 			    !IGNORE(header) && EXISTS(header)) {
   3357  1.1  christos 				break;
   3358  1.7  christos 			}
   3359  1.1  christos 		}
   3360  1.1  christos 		NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
   3361  1.1  christos 			    isc_rwlocktype_read);
   3362  1.7  christos 		if (header != NULL) {
   3363  1.1  christos 			break;
   3364  1.7  christos 		}
   3365  1.1  christos 		result = dns_rbtnodechain_next(&chain, NULL, NULL);
   3366  1.1  christos 	}
   3367  1.7  christos 	if (result == ISC_R_SUCCESS) {
   3368  1.1  christos 		result = dns_name_concatenate(&name, origin, next, NULL);
   3369  1.7  christos 	}
   3370  1.7  christos 	if (result != ISC_R_SUCCESS) {
   3371  1.3  christos 		check_next = false;
   3372  1.7  christos 	}
   3373  1.1  christos 
   3374  1.1  christos 	dns_name_clone(qname, &rname);
   3375  1.1  christos 
   3376  1.1  christos 	/*
   3377  1.1  christos 	 * Remove the wildcard label to find the terminal name.
   3378  1.1  christos 	 */
   3379  1.1  christos 	n = dns_name_countlabels(wname);
   3380  1.1  christos 	dns_name_getlabelsequence(wname, 1, n - 1, &tname);
   3381  1.1  christos 
   3382  1.1  christos 	do {
   3383  1.1  christos 		if ((check_prev && dns_name_issubdomain(prev, &rname)) ||
   3384  1.7  christos 		    (check_next && dns_name_issubdomain(next, &rname)))
   3385  1.7  christos 		{
   3386  1.3  christos 			answer = true;
   3387  1.1  christos 			break;
   3388  1.1  christos 		}
   3389  1.1  christos 		/*
   3390  1.1  christos 		 * Remove the left hand label.
   3391  1.1  christos 		 */
   3392  1.1  christos 		n = dns_name_countlabels(&rname);
   3393  1.1  christos 		dns_name_getlabelsequence(&rname, 1, n - 1, &rname);
   3394  1.1  christos 	} while (!dns_name_equal(&rname, &tname));
   3395  1.1  christos 	return (answer);
   3396  1.1  christos }
   3397  1.1  christos 
   3398  1.1  christos static inline isc_result_t
   3399  1.1  christos find_wildcard(rbtdb_search_t *search, dns_rbtnode_t **nodep,
   3400  1.7  christos 	      const dns_name_t *qname) {
   3401  1.1  christos 	unsigned int i, j;
   3402  1.1  christos 	dns_rbtnode_t *node, *level_node, *wnode;
   3403  1.1  christos 	rdatasetheader_t *header;
   3404  1.1  christos 	isc_result_t result = ISC_R_NOTFOUND;
   3405  1.1  christos 	dns_name_t name;
   3406  1.1  christos 	dns_name_t *wname;
   3407  1.1  christos 	dns_fixedname_t fwname;
   3408  1.1  christos 	dns_rbtdb_t *rbtdb;
   3409  1.3  christos 	bool done, wild, active;
   3410  1.1  christos 	dns_rbtnodechain_t wchain;
   3411  1.1  christos 
   3412  1.1  christos 	/*
   3413  1.1  christos 	 * Caller must be holding the tree lock and MUST NOT be holding
   3414  1.1  christos 	 * any node locks.
   3415  1.1  christos 	 */
   3416  1.1  christos 
   3417  1.1  christos 	/*
   3418  1.1  christos 	 * Examine each ancestor level.  If the level's wild bit
   3419  1.1  christos 	 * is set, then construct the corresponding wildcard name and
   3420  1.1  christos 	 * search for it.  If the wildcard node exists, and is active in
   3421  1.1  christos 	 * this version, we're done.  If not, then we next check to see
   3422  1.1  christos 	 * if the ancestor is active in this version.  If so, then there
   3423  1.1  christos 	 * can be no possible wildcard match and again we're done.  If not,
   3424  1.1  christos 	 * continue the search.
   3425  1.1  christos 	 */
   3426  1.1  christos 
   3427  1.1  christos 	rbtdb = search->rbtdb;
   3428  1.1  christos 	i = search->chain.level_matches;
   3429  1.3  christos 	done = false;
   3430  1.1  christos 	node = *nodep;
   3431  1.1  christos 	do {
   3432  1.1  christos 		NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
   3433  1.1  christos 			  isc_rwlocktype_read);
   3434  1.1  christos 
   3435  1.1  christos 		/*
   3436  1.1  christos 		 * First we try to figure out if this node is active in
   3437  1.1  christos 		 * the search's version.  We do this now, even though we
   3438  1.1  christos 		 * may not need the information, because it simplifies the
   3439  1.1  christos 		 * locking and code flow.
   3440  1.1  christos 		 */
   3441  1.7  christos 		for (header = node->data; header != NULL; header = header->next)
   3442  1.7  christos 		{
   3443  1.1  christos 			if (header->serial <= search->serial &&
   3444  1.7  christos 			    !IGNORE(header) && EXISTS(header)) {
   3445  1.1  christos 				break;
   3446  1.7  christos 			}
   3447  1.1  christos 		}
   3448  1.7  christos 		if (header != NULL) {
   3449  1.3  christos 			active = true;
   3450  1.7  christos 		} else {
   3451  1.3  christos 			active = false;
   3452  1.7  christos 		}
   3453  1.1  christos 
   3454  1.7  christos 		if (node->wild) {
   3455  1.3  christos 			wild = true;
   3456  1.7  christos 		} else {
   3457  1.3  christos 			wild = false;
   3458  1.7  christos 		}
   3459  1.1  christos 
   3460  1.1  christos 		NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
   3461  1.1  christos 			    isc_rwlocktype_read);
   3462  1.1  christos 
   3463  1.1  christos 		if (wild) {
   3464  1.1  christos 			/*
   3465  1.1  christos 			 * Construct the wildcard name for this level.
   3466  1.1  christos 			 */
   3467  1.1  christos 			dns_name_init(&name, NULL);
   3468  1.1  christos 			dns_rbt_namefromnode(node, &name);
   3469  1.1  christos 			wname = dns_fixedname_initname(&fwname);
   3470  1.1  christos 			result = dns_name_concatenate(dns_wildcardname, &name,
   3471  1.1  christos 						      wname, NULL);
   3472  1.1  christos 			j = i;
   3473  1.1  christos 			while (result == ISC_R_SUCCESS && j != 0) {
   3474  1.1  christos 				j--;
   3475  1.1  christos 				level_node = search->chain.levels[j];
   3476  1.1  christos 				dns_name_init(&name, NULL);
   3477  1.1  christos 				dns_rbt_namefromnode(level_node, &name);
   3478  1.7  christos 				result = dns_name_concatenate(wname, &name,
   3479  1.7  christos 							      wname, NULL);
   3480  1.1  christos 			}
   3481  1.7  christos 			if (result != ISC_R_SUCCESS) {
   3482  1.1  christos 				break;
   3483  1.7  christos 			}
   3484  1.1  christos 
   3485  1.1  christos 			wnode = NULL;
   3486  1.7  christos 			dns_rbtnodechain_init(&wchain);
   3487  1.7  christos 			result = dns_rbt_findnode(
   3488  1.7  christos 				rbtdb->tree, wname, NULL, &wnode, &wchain,
   3489  1.7  christos 				DNS_RBTFIND_EMPTYDATA, NULL, NULL);
   3490  1.1  christos 			if (result == ISC_R_SUCCESS) {
   3491  1.1  christos 				nodelock_t *lock;
   3492  1.1  christos 
   3493  1.1  christos 				/*
   3494  1.1  christos 				 * We have found the wildcard node.  If it
   3495  1.1  christos 				 * is active in the search's version, we're
   3496  1.1  christos 				 * done.
   3497  1.1  christos 				 */
   3498  1.1  christos 				lock = &rbtdb->node_locks[wnode->locknum].lock;
   3499  1.1  christos 				NODE_LOCK(lock, isc_rwlocktype_read);
   3500  1.7  christos 				for (header = wnode->data; header != NULL;
   3501  1.1  christos 				     header = header->next) {
   3502  1.1  christos 					if (header->serial <= search->serial &&
   3503  1.7  christos 					    !IGNORE(header) && EXISTS(header)) {
   3504  1.1  christos 						break;
   3505  1.7  christos 					}
   3506  1.1  christos 				}
   3507  1.1  christos 				NODE_UNLOCK(lock, isc_rwlocktype_read);
   3508  1.1  christos 				if (header != NULL ||
   3509  1.1  christos 				    activeempty(search, &wchain, wname)) {
   3510  1.1  christos 					if (activeemtpynode(search, qname,
   3511  1.1  christos 							    wname)) {
   3512  1.1  christos 						return (ISC_R_NOTFOUND);
   3513  1.1  christos 					}
   3514  1.1  christos 					/*
   3515  1.1  christos 					 * The wildcard node is active!
   3516  1.1  christos 					 *
   3517  1.1  christos 					 * Note: result is still ISC_R_SUCCESS
   3518  1.1  christos 					 * so we don't have to set it.
   3519  1.1  christos 					 */
   3520  1.1  christos 					*nodep = wnode;
   3521  1.1  christos 					break;
   3522  1.1  christos 				}
   3523  1.1  christos 			} else if (result != ISC_R_NOTFOUND &&
   3524  1.1  christos 				   result != DNS_R_PARTIALMATCH) {
   3525  1.1  christos 				/*
   3526  1.1  christos 				 * An error has occurred.  Bail out.
   3527  1.1  christos 				 */
   3528  1.1  christos 				break;
   3529  1.1  christos 			}
   3530  1.1  christos 		}
   3531  1.1  christos 
   3532  1.1  christos 		if (active) {
   3533  1.1  christos 			/*
   3534  1.1  christos 			 * The level node is active.  Any wildcarding
   3535  1.1  christos 			 * present at higher levels has no
   3536  1.1  christos 			 * effect and we're done.
   3537  1.1  christos 			 */
   3538  1.1  christos 			result = ISC_R_NOTFOUND;
   3539  1.1  christos 			break;
   3540  1.1  christos 		}
   3541  1.1  christos 
   3542  1.1  christos 		if (i > 0) {
   3543  1.1  christos 			i--;
   3544  1.1  christos 			node = search->chain.levels[i];
   3545  1.7  christos 		} else {
   3546  1.3  christos 			done = true;
   3547  1.7  christos 		}
   3548  1.1  christos 	} while (!done);
   3549  1.1  christos 
   3550  1.1  christos 	return (result);
   3551  1.1  christos }
   3552  1.1  christos 
   3553  1.3  christos static bool
   3554  1.7  christos matchparams(rdatasetheader_t *header, rbtdb_search_t *search) {
   3555  1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
   3556  1.1  christos 	dns_rdata_nsec3_t nsec3;
   3557  1.7  christos 	unsigned char *raw; /* RDATASLAB */
   3558  1.1  christos 	unsigned int rdlen, count;
   3559  1.1  christos 	isc_region_t region;
   3560  1.1  christos 	isc_result_t result;
   3561  1.1  christos 
   3562  1.1  christos 	REQUIRE(header->type == dns_rdatatype_nsec3);
   3563  1.1  christos 
   3564  1.1  christos 	raw = (unsigned char *)header + sizeof(*header);
   3565  1.1  christos 	count = raw[0] * 256 + raw[1]; /* count */
   3566  1.5  christos 	raw += DNS_RDATASET_COUNT + DNS_RDATASET_LENGTH;
   3567  1.5  christos 
   3568  1.1  christos 	while (count-- > 0) {
   3569  1.1  christos 		rdlen = raw[0] * 256 + raw[1];
   3570  1.5  christos 		raw += DNS_RDATASET_ORDER + DNS_RDATASET_LENGTH;
   3571  1.1  christos 		region.base = raw;
   3572  1.1  christos 		region.length = rdlen;
   3573  1.1  christos 		dns_rdata_fromregion(&rdata, search->rbtdb->common.rdclass,
   3574  1.1  christos 				     dns_rdatatype_nsec3, &region);
   3575  1.1  christos 		raw += rdlen;
   3576  1.1  christos 		result = dns_rdata_tostruct(&rdata, &nsec3, NULL);
   3577  1.1  christos 		INSIST(result == ISC_R_SUCCESS);
   3578  1.1  christos 		if (nsec3.hash == search->rbtversion->hash &&
   3579  1.1  christos 		    nsec3.iterations == search->rbtversion->iterations &&
   3580  1.1  christos 		    nsec3.salt_length == search->rbtversion->salt_length &&
   3581  1.1  christos 		    memcmp(nsec3.salt, search->rbtversion->salt,
   3582  1.1  christos 			   nsec3.salt_length) == 0)
   3583  1.7  christos 		{
   3584  1.3  christos 			return (true);
   3585  1.7  christos 		}
   3586  1.1  christos 		dns_rdata_reset(&rdata);
   3587  1.1  christos 	}
   3588  1.3  christos 	return (false);
   3589  1.1  christos }
   3590  1.1  christos 
   3591  1.1  christos /*
   3592  1.1  christos  * Find node of the NSEC/NSEC3 record that is 'name'.
   3593  1.1  christos  */
   3594  1.1  christos static inline isc_result_t
   3595  1.1  christos previous_closest_nsec(dns_rdatatype_t type, rbtdb_search_t *search,
   3596  1.1  christos 		      dns_name_t *name, dns_name_t *origin,
   3597  1.1  christos 		      dns_rbtnode_t **nodep, dns_rbtnodechain_t *nsecchain,
   3598  1.7  christos 		      bool *firstp) {
   3599  1.1  christos 	dns_fixedname_t ftarget;
   3600  1.1  christos 	dns_name_t *target;
   3601  1.1  christos 	dns_rbtnode_t *nsecnode;
   3602  1.1  christos 	isc_result_t result;
   3603  1.1  christos 
   3604  1.1  christos 	REQUIRE(nodep != NULL && *nodep == NULL);
   3605  1.1  christos 
   3606  1.1  christos 	if (type == dns_rdatatype_nsec3) {
   3607  1.1  christos 		result = dns_rbtnodechain_prev(&search->chain, NULL, NULL);
   3608  1.7  christos 		if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
   3609  1.1  christos 			return (result);
   3610  1.7  christos 		}
   3611  1.1  christos 		result = dns_rbtnodechain_current(&search->chain, name, origin,
   3612  1.1  christos 						  nodep);
   3613  1.1  christos 		return (result);
   3614  1.1  christos 	}
   3615  1.1  christos 
   3616  1.1  christos 	target = dns_fixedname_initname(&ftarget);
   3617  1.1  christos 
   3618  1.1  christos 	for (;;) {
   3619  1.1  christos 		if (*firstp) {
   3620  1.1  christos 			/*
   3621  1.1  christos 			 * Construct the name of the second node to check.
   3622  1.1  christos 			 * It is the first node sought in the NSEC tree.
   3623  1.1  christos 			 */
   3624  1.3  christos 			*firstp = false;
   3625  1.7  christos 			dns_rbtnodechain_init(nsecchain);
   3626  1.7  christos 			result = dns_name_concatenate(name, origin, target,
   3627  1.7  christos 						      NULL);
   3628  1.7  christos 			if (result != ISC_R_SUCCESS) {
   3629  1.1  christos 				return (result);
   3630  1.7  christos 			}
   3631  1.1  christos 			nsecnode = NULL;
   3632  1.7  christos 			result = dns_rbt_findnode(
   3633  1.7  christos 				search->rbtdb->nsec, target, NULL, &nsecnode,
   3634  1.7  christos 				nsecchain, DNS_RBTFIND_NOOPTIONS, NULL, NULL);
   3635  1.1  christos 			if (result == ISC_R_SUCCESS) {
   3636  1.1  christos 				/*
   3637  1.1  christos 				 * Since this was the first loop, finding the
   3638  1.1  christos 				 * name in the NSEC tree implies that the first
   3639  1.1  christos 				 * node checked in the main tree had an
   3640  1.1  christos 				 * unacceptable NSEC record.
   3641  1.1  christos 				 * Try the previous node in the NSEC tree.
   3642  1.1  christos 				 */
   3643  1.7  christos 				result = dns_rbtnodechain_prev(nsecchain, name,
   3644  1.7  christos 							       origin);
   3645  1.7  christos 				if (result == DNS_R_NEWORIGIN) {
   3646  1.1  christos 					result = ISC_R_SUCCESS;
   3647  1.7  christos 				}
   3648  1.1  christos 			} else if (result == ISC_R_NOTFOUND ||
   3649  1.1  christos 				   result == DNS_R_PARTIALMATCH) {
   3650  1.7  christos 				result = dns_rbtnodechain_current(
   3651  1.7  christos 					nsecchain, name, origin, NULL);
   3652  1.7  christos 				if (result == ISC_R_NOTFOUND) {
   3653  1.1  christos 					result = ISC_R_NOMORE;
   3654  1.7  christos 				}
   3655  1.1  christos 			}
   3656  1.1  christos 		} else {
   3657  1.1  christos 			/*
   3658  1.1  christos 			 * This is a second or later trip through the auxiliary
   3659  1.1  christos 			 * tree for the name of a third or earlier NSEC node in
   3660  1.1  christos 			 * the main tree.  Previous trips through the NSEC tree
   3661  1.1  christos 			 * must have found nodes in the main tree with NSEC
   3662  1.1  christos 			 * records.  Perhaps they lacked signature records.
   3663  1.1  christos 			 */
   3664  1.1  christos 			result = dns_rbtnodechain_prev(nsecchain, name, origin);
   3665  1.7  christos 			if (result == DNS_R_NEWORIGIN) {
   3666  1.1  christos 				result = ISC_R_SUCCESS;
   3667  1.7  christos 			}
   3668  1.1  christos 		}
   3669  1.7  christos 		if (result != ISC_R_SUCCESS) {
   3670  1.1  christos 			return (result);
   3671  1.7  christos 		}
   3672  1.1  christos 
   3673  1.1  christos 		/*
   3674  1.1  christos 		 * Construct the name to seek in the main tree.
   3675  1.1  christos 		 */
   3676  1.1  christos 		result = dns_name_concatenate(name, origin, target, NULL);
   3677  1.7  christos 		if (result != ISC_R_SUCCESS) {
   3678  1.1  christos 			return (result);
   3679  1.7  christos 		}
   3680  1.1  christos 
   3681  1.1  christos 		*nodep = NULL;
   3682  1.1  christos 		result = dns_rbt_findnode(search->rbtdb->tree, target, NULL,
   3683  1.1  christos 					  nodep, &search->chain,
   3684  1.1  christos 					  DNS_RBTFIND_NOOPTIONS, NULL, NULL);
   3685  1.7  christos 		if (result == ISC_R_SUCCESS) {
   3686  1.1  christos 			return (result);
   3687  1.7  christos 		}
   3688  1.1  christos 
   3689  1.1  christos 		/*
   3690  1.1  christos 		 * There should always be a node in the main tree with the
   3691  1.1  christos 		 * same name as the node in the auxiliary NSEC tree, except for
   3692  1.1  christos 		 * nodes in the auxiliary tree that are awaiting deletion.
   3693  1.1  christos 		 */
   3694  1.1  christos 		if (result != DNS_R_PARTIALMATCH && result != ISC_R_NOTFOUND) {
   3695  1.1  christos 			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   3696  1.1  christos 				      DNS_LOGMODULE_CACHE, ISC_LOG_ERROR,
   3697  1.1  christos 				      "previous_closest_nsec(): %s",
   3698  1.1  christos 				      isc_result_totext(result));
   3699  1.1  christos 			return (DNS_R_BADDB);
   3700  1.1  christos 		}
   3701  1.1  christos 	}
   3702  1.1  christos }
   3703  1.1  christos 
   3704  1.1  christos /*
   3705  1.1  christos  * Find the NSEC/NSEC3 which is or before the current point on the
   3706  1.1  christos  * search chain.  For NSEC3 records only NSEC3 records that match the
   3707  1.1  christos  * current NSEC3PARAM record are considered.
   3708  1.1  christos  */
   3709  1.1  christos static inline isc_result_t
   3710  1.1  christos find_closest_nsec(rbtdb_search_t *search, dns_dbnode_t **nodep,
   3711  1.1  christos 		  dns_name_t *foundname, dns_rdataset_t *rdataset,
   3712  1.1  christos 		  dns_rdataset_t *sigrdataset, dns_rbt_t *tree,
   3713  1.7  christos 		  dns_db_secure_t secure) {
   3714  1.1  christos 	dns_rbtnode_t *node, *prevnode;
   3715  1.1  christos 	rdatasetheader_t *header, *header_next, *found, *foundsig;
   3716  1.1  christos 	dns_rbtnodechain_t nsecchain;
   3717  1.3  christos 	bool empty_node;
   3718  1.1  christos 	isc_result_t result;
   3719  1.1  christos 	dns_fixedname_t fname, forigin;
   3720  1.1  christos 	dns_name_t *name, *origin;
   3721  1.1  christos 	dns_rdatatype_t type;
   3722  1.1  christos 	rbtdb_rdatatype_t sigtype;
   3723  1.3  christos 	bool wraps;
   3724  1.3  christos 	bool first = true;
   3725  1.3  christos 	bool need_sig = (secure == dns_db_secure);
   3726  1.1  christos 
   3727  1.1  christos 	if (tree == search->rbtdb->nsec3) {
   3728  1.1  christos 		type = dns_rdatatype_nsec3;
   3729  1.1  christos 		sigtype = RBTDB_RDATATYPE_SIGNSEC3;
   3730  1.3  christos 		wraps = true;
   3731  1.1  christos 	} else {
   3732  1.1  christos 		type = dns_rdatatype_nsec;
   3733  1.1  christos 		sigtype = RBTDB_RDATATYPE_SIGNSEC;
   3734  1.3  christos 		wraps = false;
   3735  1.1  christos 	}
   3736  1.1  christos 
   3737  1.1  christos 	/*
   3738  1.1  christos 	 * Use the auxiliary tree only starting with the second node in the
   3739  1.1  christos 	 * hope that the original node will be right much of the time.
   3740  1.1  christos 	 */
   3741  1.1  christos 	name = dns_fixedname_initname(&fname);
   3742  1.1  christos 	origin = dns_fixedname_initname(&forigin);
   3743  1.7  christos again:
   3744  1.1  christos 	node = NULL;
   3745  1.1  christos 	prevnode = NULL;
   3746  1.1  christos 	result = dns_rbtnodechain_current(&search->chain, name, origin, &node);
   3747  1.7  christos 	if (result != ISC_R_SUCCESS) {
   3748  1.1  christos 		return (result);
   3749  1.7  christos 	}
   3750  1.1  christos 	do {
   3751  1.1  christos 		NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
   3752  1.1  christos 			  isc_rwlocktype_read);
   3753  1.1  christos 		found = NULL;
   3754  1.1  christos 		foundsig = NULL;
   3755  1.3  christos 		empty_node = true;
   3756  1.7  christos 		for (header = node->data; header != NULL; header = header_next)
   3757  1.7  christos 		{
   3758  1.1  christos 			header_next = header->next;
   3759  1.1  christos 			/*
   3760  1.1  christos 			 * Look for an active, extant NSEC or RRSIG NSEC.
   3761  1.1  christos 			 */
   3762  1.1  christos 			do {
   3763  1.1  christos 				if (header->serial <= search->serial &&
   3764  1.1  christos 				    !IGNORE(header)) {
   3765  1.1  christos 					/*
   3766  1.1  christos 					 * Is this a "this rdataset doesn't
   3767  1.1  christos 					 * exist" record?
   3768  1.1  christos 					 */
   3769  1.7  christos 					if (NONEXISTENT(header)) {
   3770  1.1  christos 						header = NULL;
   3771  1.7  christos 					}
   3772  1.1  christos 					break;
   3773  1.7  christos 				} else {
   3774  1.1  christos 					header = header->down;
   3775  1.7  christos 				}
   3776  1.1  christos 			} while (header != NULL);
   3777  1.1  christos 			if (header != NULL) {
   3778  1.1  christos 				/*
   3779  1.1  christos 				 * We now know that there is at least one
   3780  1.1  christos 				 * active rdataset at this node.
   3781  1.1  christos 				 */
   3782  1.3  christos 				empty_node = false;
   3783  1.1  christos 				if (header->type == type) {
   3784  1.1  christos 					found = header;
   3785  1.7  christos 					if (foundsig != NULL) {
   3786  1.1  christos 						break;
   3787  1.7  christos 					}
   3788  1.1  christos 				} else if (header->type == sigtype) {
   3789  1.1  christos 					foundsig = header;
   3790  1.7  christos 					if (found != NULL) {
   3791  1.1  christos 						break;
   3792  1.7  christos 					}
   3793  1.1  christos 				}
   3794  1.1  christos 			}
   3795  1.1  christos 		}
   3796  1.1  christos 		if (!empty_node) {
   3797  1.1  christos 			if (found != NULL && search->rbtversion->havensec3 &&
   3798  1.1  christos 			    found->type == dns_rdatatype_nsec3 &&
   3799  1.7  christos 			    !matchparams(found, search))
   3800  1.7  christos 			{
   3801  1.3  christos 				empty_node = true;
   3802  1.1  christos 				found = NULL;
   3803  1.1  christos 				foundsig = NULL;
   3804  1.7  christos 				result = previous_closest_nsec(
   3805  1.7  christos 					type, search, name, origin, &prevnode,
   3806  1.7  christos 					NULL, NULL);
   3807  1.1  christos 			} else if (found != NULL &&
   3808  1.1  christos 				   (foundsig != NULL || !need_sig)) {
   3809  1.1  christos 				/*
   3810  1.1  christos 				 * We've found the right NSEC/NSEC3 record.
   3811  1.1  christos 				 *
   3812  1.1  christos 				 * Note: for this to really be the right
   3813  1.1  christos 				 * NSEC record, it's essential that the NSEC
   3814  1.1  christos 				 * records of any nodes obscured by a zone
   3815  1.1  christos 				 * cut have been removed; we assume this is
   3816  1.1  christos 				 * the case.
   3817  1.1  christos 				 */
   3818  1.1  christos 				result = dns_name_concatenate(name, origin,
   3819  1.1  christos 							      foundname, NULL);
   3820  1.1  christos 				if (result == ISC_R_SUCCESS) {
   3821  1.1  christos 					if (nodep != NULL) {
   3822  1.1  christos 						new_reference(search->rbtdb,
   3823  1.1  christos 							      node);
   3824  1.1  christos 						*nodep = node;
   3825  1.1  christos 					}
   3826  1.1  christos 					bind_rdataset(search->rbtdb, node,
   3827  1.1  christos 						      found, search->now,
   3828  1.1  christos 						      rdataset);
   3829  1.7  christos 					if (foundsig != NULL) {
   3830  1.1  christos 						bind_rdataset(search->rbtdb,
   3831  1.7  christos 							      node, foundsig,
   3832  1.1  christos 							      search->now,
   3833  1.1  christos 							      sigrdataset);
   3834  1.7  christos 					}
   3835  1.1  christos 				}
   3836  1.1  christos 			} else if (found == NULL && foundsig == NULL) {
   3837  1.1  christos 				/*
   3838  1.1  christos 				 * This node is active, but has no NSEC or
   3839  1.1  christos 				 * RRSIG NSEC.  That means it's glue or
   3840  1.1  christos 				 * other obscured zone data that isn't
   3841  1.1  christos 				 * relevant for our search.  Treat the
   3842  1.1  christos 				 * node as if it were empty and keep looking.
   3843  1.1  christos 				 */
   3844  1.3  christos 				empty_node = true;
   3845  1.7  christos 				result = previous_closest_nsec(
   3846  1.7  christos 					type, search, name, origin, &prevnode,
   3847  1.7  christos 					&nsecchain, &first);
   3848  1.1  christos 			} else {
   3849  1.1  christos 				/*
   3850  1.1  christos 				 * We found an active node, but either the
   3851  1.1  christos 				 * NSEC or the RRSIG NSEC is missing.  This
   3852  1.1  christos 				 * shouldn't happen.
   3853  1.1  christos 				 */
   3854  1.1  christos 				result = DNS_R_BADDB;
   3855  1.1  christos 			}
   3856  1.1  christos 		} else {
   3857  1.1  christos 			/*
   3858  1.1  christos 			 * This node isn't active.  We've got to keep
   3859  1.1  christos 			 * looking.
   3860  1.1  christos 			 */
   3861  1.7  christos 			result = previous_closest_nsec(type, search, name,
   3862  1.7  christos 						       origin, &prevnode,
   3863  1.1  christos 						       &nsecchain, &first);
   3864  1.1  christos 		}
   3865  1.1  christos 		NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
   3866  1.1  christos 			    isc_rwlocktype_read);
   3867  1.1  christos 		node = prevnode;
   3868  1.1  christos 		prevnode = NULL;
   3869  1.1  christos 	} while (empty_node && result == ISC_R_SUCCESS);
   3870  1.1  christos 
   3871  1.7  christos 	if (!first) {
   3872  1.1  christos 		dns_rbtnodechain_invalidate(&nsecchain);
   3873  1.7  christos 	}
   3874  1.1  christos 
   3875  1.1  christos 	if (result == ISC_R_NOMORE && wraps) {
   3876  1.7  christos 		result = dns_rbtnodechain_last(&search->chain, tree, NULL,
   3877  1.7  christos 					       NULL);
   3878  1.1  christos 		if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
   3879  1.3  christos 			wraps = false;
   3880  1.1  christos 			goto again;
   3881  1.1  christos 		}
   3882  1.1  christos 	}
   3883  1.1  christos 
   3884  1.1  christos 	/*
   3885  1.1  christos 	 * If the result is ISC_R_NOMORE, then we got to the beginning of
   3886  1.1  christos 	 * the database and didn't find a NSEC record.  This shouldn't
   3887  1.1  christos 	 * happen.
   3888  1.1  christos 	 */
   3889  1.7  christos 	if (result == ISC_R_NOMORE) {
   3890  1.1  christos 		result = DNS_R_BADDB;
   3891  1.7  christos 	}
   3892  1.1  christos 
   3893  1.1  christos 	return (result);
   3894  1.1  christos }
   3895  1.1  christos 
   3896  1.1  christos static isc_result_t
   3897  1.1  christos zone_find(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
   3898  1.1  christos 	  dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
   3899  1.7  christos 	  dns_dbnode_t **nodep, dns_name_t *foundname, dns_rdataset_t *rdataset,
   3900  1.7  christos 	  dns_rdataset_t *sigrdataset) {
   3901  1.1  christos 	dns_rbtnode_t *node = NULL;
   3902  1.1  christos 	isc_result_t result;
   3903  1.1  christos 	rbtdb_search_t search;
   3904  1.3  christos 	bool cname_ok = true;
   3905  1.3  christos 	bool close_version = false;
   3906  1.3  christos 	bool maybe_zonecut = false;
   3907  1.3  christos 	bool at_zonecut = false;
   3908  1.3  christos 	bool wild;
   3909  1.3  christos 	bool empty_node;
   3910  1.1  christos 	rdatasetheader_t *header, *header_next, *found, *nsecheader;
   3911  1.1  christos 	rdatasetheader_t *foundsig, *cnamesig, *nsecsig;
   3912  1.1  christos 	rbtdb_rdatatype_t sigtype;
   3913  1.3  christos 	bool active;
   3914  1.1  christos 	dns_rbtnodechain_t chain;
   3915  1.1  christos 	nodelock_t *lock;
   3916  1.1  christos 	dns_rbt_t *tree;
   3917  1.1  christos 
   3918  1.1  christos 	search.rbtdb = (dns_rbtdb_t *)db;
   3919  1.1  christos 
   3920  1.1  christos 	REQUIRE(VALID_RBTDB(search.rbtdb));
   3921  1.1  christos 	INSIST(version == NULL ||
   3922  1.1  christos 	       ((rbtdb_version_t *)version)->rbtdb == (dns_rbtdb_t *)db);
   3923  1.1  christos 
   3924  1.1  christos 	/*
   3925  1.1  christos 	 * We don't care about 'now'.
   3926  1.1  christos 	 */
   3927  1.1  christos 	UNUSED(now);
   3928  1.1  christos 
   3929  1.1  christos 	/*
   3930  1.1  christos 	 * If the caller didn't supply a version, attach to the current
   3931  1.1  christos 	 * version.
   3932  1.1  christos 	 */
   3933  1.1  christos 	if (version == NULL) {
   3934  1.1  christos 		currentversion(db, &version);
   3935  1.3  christos 		close_version = true;
   3936  1.1  christos 	}
   3937  1.1  christos 
   3938  1.1  christos 	search.rbtversion = version;
   3939  1.1  christos 	search.serial = search.rbtversion->serial;
   3940  1.1  christos 	search.options = options;
   3941  1.3  christos 	search.copy_name = false;
   3942  1.3  christos 	search.need_cleanup = false;
   3943  1.3  christos 	search.wild = false;
   3944  1.1  christos 	search.zonecut = NULL;
   3945  1.1  christos 	dns_fixedname_init(&search.zonecut_name);
   3946  1.7  christos 	dns_rbtnodechain_init(&search.chain);
   3947  1.1  christos 	search.now = 0;
   3948  1.1  christos 
   3949  1.1  christos 	/*
   3950  1.1  christos 	 * 'wild' will be true iff. we've matched a wildcard.
   3951  1.1  christos 	 */
   3952  1.3  christos 	wild = false;
   3953  1.1  christos 
   3954  1.1  christos 	RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
   3955  1.1  christos 
   3956  1.1  christos 	/*
   3957  1.1  christos 	 * Search down from the root of the tree.  If, while going down, we
   3958  1.1  christos 	 * encounter a callback node, zone_zonecut_callback() will search the
   3959  1.1  christos 	 * rdatasets at the zone cut for active DNAME or NS rdatasets.
   3960  1.1  christos 	 */
   3961  1.7  christos 	tree = (options & DNS_DBFIND_FORCENSEC3) != 0 ? search.rbtdb->nsec3
   3962  1.7  christos 						      : search.rbtdb->tree;
   3963  1.7  christos 	result = dns_rbt_findnode(tree, name, foundname, &node, &search.chain,
   3964  1.7  christos 				  DNS_RBTFIND_EMPTYDATA, zone_zonecut_callback,
   3965  1.7  christos 				  &search);
   3966  1.1  christos 
   3967  1.1  christos 	if (result == DNS_R_PARTIALMATCH) {
   3968  1.1  christos 	partial_match:
   3969  1.1  christos 		if (search.zonecut != NULL) {
   3970  1.7  christos 			result = setup_delegation(&search, nodep, foundname,
   3971  1.7  christos 						  rdataset, sigrdataset);
   3972  1.7  christos 			goto tree_exit;
   3973  1.1  christos 		}
   3974  1.1  christos 
   3975  1.1  christos 		if (search.wild) {
   3976  1.1  christos 			/*
   3977  1.1  christos 			 * At least one of the levels in the search chain
   3978  1.1  christos 			 * potentially has a wildcard.  For each such level,
   3979  1.1  christos 			 * we must see if there's a matching wildcard active
   3980  1.1  christos 			 * in the current version.
   3981  1.1  christos 			 */
   3982  1.1  christos 			result = find_wildcard(&search, &node, name);
   3983  1.1  christos 			if (result == ISC_R_SUCCESS) {
   3984  1.6  christos 				dns_name_copynf(name, foundname);
   3985  1.3  christos 				wild = true;
   3986  1.1  christos 				goto found;
   3987  1.7  christos 			} else if (result != ISC_R_NOTFOUND) {
   3988  1.7  christos 				goto tree_exit;
   3989  1.1  christos 			}
   3990  1.1  christos 		}
   3991  1.1  christos 
   3992  1.1  christos 		chain = search.chain;
   3993  1.1  christos 		active = activeempty(&search, &chain, name);
   3994  1.1  christos 
   3995  1.1  christos 		/*
   3996  1.1  christos 		 * If we're here, then the name does not exist, is not
   3997  1.1  christos 		 * beneath a zonecut, and there's no matching wildcard.
   3998  1.1  christos 		 */
   3999  1.1  christos 		if ((search.rbtversion->secure == dns_db_secure &&
   4000  1.1  christos 		     !search.rbtversion->havensec3) ||
   4001  1.1  christos 		    (search.options & DNS_DBFIND_FORCENSEC) != 0 ||
   4002  1.1  christos 		    (search.options & DNS_DBFIND_FORCENSEC3) != 0)
   4003  1.1  christos 		{
   4004  1.1  christos 			result = find_closest_nsec(&search, nodep, foundname,
   4005  1.1  christos 						   rdataset, sigrdataset, tree,
   4006  1.1  christos 						   search.rbtversion->secure);
   4007  1.7  christos 			if (result == ISC_R_SUCCESS) {
   4008  1.7  christos 				result = active ? DNS_R_EMPTYNAME
   4009  1.7  christos 						: DNS_R_NXDOMAIN;
   4010  1.7  christos 			}
   4011  1.7  christos 		} else {
   4012  1.1  christos 			result = active ? DNS_R_EMPTYNAME : DNS_R_NXDOMAIN;
   4013  1.7  christos 		}
   4014  1.1  christos 		goto tree_exit;
   4015  1.7  christos 	} else if (result != ISC_R_SUCCESS) {
   4016  1.1  christos 		goto tree_exit;
   4017  1.7  christos 	}
   4018  1.1  christos 
   4019  1.7  christos found:
   4020  1.1  christos 	/*
   4021  1.1  christos 	 * We have found a node whose name is the desired name, or we
   4022  1.1  christos 	 * have matched a wildcard.
   4023  1.1  christos 	 */
   4024  1.1  christos 
   4025  1.1  christos 	if (search.zonecut != NULL) {
   4026  1.1  christos 		/*
   4027  1.1  christos 		 * If we're beneath a zone cut, we don't want to look for
   4028  1.1  christos 		 * CNAMEs because they're not legitimate zone glue.
   4029  1.1  christos 		 */
   4030  1.3  christos 		cname_ok = false;
   4031  1.1  christos 	} else {
   4032  1.1  christos 		/*
   4033  1.1  christos 		 * The node may be a zone cut itself.  If it might be one,
   4034  1.1  christos 		 * make sure we check for it later.
   4035  1.1  christos 		 *
   4036  1.1  christos 		 * DS records live above the zone cut in ordinary zone so
   4037  1.1  christos 		 * we want to ignore any referral.
   4038  1.1  christos 		 *
   4039  1.7  christos 		 * Stub zones don't have anything "above" the delegation so
   4040  1.1  christos 		 * we always return a referral.
   4041  1.1  christos 		 */
   4042  1.1  christos 		if (node->find_callback &&
   4043  1.1  christos 		    ((node != search.rbtdb->origin_node &&
   4044  1.1  christos 		      !dns_rdatatype_atparent(type)) ||
   4045  1.1  christos 		     IS_STUB(search.rbtdb)))
   4046  1.7  christos 		{
   4047  1.3  christos 			maybe_zonecut = true;
   4048  1.7  christos 		}
   4049  1.1  christos 	}
   4050  1.1  christos 
   4051  1.1  christos 	/*
   4052  1.1  christos 	 * Certain DNSSEC types are not subject to CNAME matching
   4053  1.1  christos 	 * (RFC4035, section 2.5 and RFC3007).
   4054  1.1  christos 	 *
   4055  1.1  christos 	 * We don't check for RRSIG, because we don't store RRSIG records
   4056  1.1  christos 	 * directly.
   4057  1.1  christos 	 */
   4058  1.7  christos 	if (type == dns_rdatatype_key || type == dns_rdatatype_nsec) {
   4059  1.3  christos 		cname_ok = false;
   4060  1.7  christos 	}
   4061  1.1  christos 
   4062  1.1  christos 	/*
   4063  1.1  christos 	 * We now go looking for rdata...
   4064  1.1  christos 	 */
   4065  1.1  christos 
   4066  1.1  christos 	lock = &search.rbtdb->node_locks[node->locknum].lock;
   4067  1.1  christos 	NODE_LOCK(lock, isc_rwlocktype_read);
   4068  1.1  christos 
   4069  1.1  christos 	found = NULL;
   4070  1.1  christos 	foundsig = NULL;
   4071  1.1  christos 	sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
   4072  1.1  christos 	nsecheader = NULL;
   4073  1.1  christos 	nsecsig = NULL;
   4074  1.1  christos 	cnamesig = NULL;
   4075  1.3  christos 	empty_node = true;
   4076  1.1  christos 	for (header = node->data; header != NULL; header = header_next) {
   4077  1.1  christos 		header_next = header->next;
   4078  1.1  christos 		/*
   4079  1.1  christos 		 * Look for an active, extant rdataset.
   4080  1.1  christos 		 */
   4081  1.1  christos 		do {
   4082  1.7  christos 			if (header->serial <= search.serial && !IGNORE(header))
   4083  1.7  christos 			{
   4084  1.1  christos 				/*
   4085  1.1  christos 				 * Is this a "this rdataset doesn't
   4086  1.1  christos 				 * exist" record?
   4087  1.1  christos 				 */
   4088  1.7  christos 				if (NONEXISTENT(header)) {
   4089  1.1  christos 					header = NULL;
   4090  1.7  christos 				}
   4091  1.1  christos 				break;
   4092  1.7  christos 			} else {
   4093  1.1  christos 				header = header->down;
   4094  1.7  christos 			}
   4095  1.1  christos 		} while (header != NULL);
   4096  1.1  christos 		if (header != NULL) {
   4097  1.1  christos 			/*
   4098  1.1  christos 			 * We now know that there is at least one active
   4099  1.1  christos 			 * rdataset at this node.
   4100  1.1  christos 			 */
   4101  1.3  christos 			empty_node = false;
   4102  1.1  christos 
   4103  1.1  christos 			/*
   4104  1.1  christos 			 * Do special zone cut handling, if requested.
   4105  1.1  christos 			 */
   4106  1.7  christos 			if (maybe_zonecut && header->type == dns_rdatatype_ns) {
   4107  1.1  christos 				/*
   4108  1.1  christos 				 * We increment the reference count on node to
   4109  1.1  christos 				 * ensure that search->zonecut_rdataset will
   4110  1.1  christos 				 * still be valid later.
   4111  1.1  christos 				 */
   4112  1.1  christos 				new_reference(search.rbtdb, node);
   4113  1.1  christos 				search.zonecut = node;
   4114  1.1  christos 				search.zonecut_rdataset = header;
   4115  1.1  christos 				search.zonecut_sigrdataset = NULL;
   4116  1.3  christos 				search.need_cleanup = true;
   4117  1.3  christos 				maybe_zonecut = false;
   4118  1.3  christos 				at_zonecut = true;
   4119  1.1  christos 				/*
   4120  1.1  christos 				 * It is not clear if KEY should still be
   4121  1.1  christos 				 * allowed at the parent side of the zone
   4122  1.1  christos 				 * cut or not.  It is needed for RFC3007
   4123  1.1  christos 				 * validated updates.
   4124  1.1  christos 				 */
   4125  1.7  christos 				if ((search.options & DNS_DBFIND_GLUEOK) == 0 &&
   4126  1.7  christos 				    type != dns_rdatatype_nsec &&
   4127  1.7  christos 				    type != dns_rdatatype_key)
   4128  1.7  christos 				{
   4129  1.1  christos 					/*
   4130  1.1  christos 					 * Glue is not OK, but any answer we
   4131  1.1  christos 					 * could return would be glue.  Return
   4132  1.1  christos 					 * the delegation.
   4133  1.1  christos 					 */
   4134  1.1  christos 					found = NULL;
   4135  1.1  christos 					break;
   4136  1.1  christos 				}
   4137  1.7  christos 				if (found != NULL && foundsig != NULL) {
   4138  1.1  christos 					break;
   4139  1.7  christos 				}
   4140  1.1  christos 			}
   4141  1.1  christos 
   4142  1.1  christos 			/*
   4143  1.1  christos 			 * If the NSEC3 record doesn't match the chain
   4144  1.1  christos 			 * we are using behave as if it isn't here.
   4145  1.1  christos 			 */
   4146  1.1  christos 			if (header->type == dns_rdatatype_nsec3 &&
   4147  1.7  christos 			    !matchparams(header, &search)) {
   4148  1.1  christos 				NODE_UNLOCK(lock, isc_rwlocktype_read);
   4149  1.1  christos 				goto partial_match;
   4150  1.1  christos 			}
   4151  1.1  christos 			/*
   4152  1.1  christos 			 * If we found a type we were looking for,
   4153  1.1  christos 			 * remember it.
   4154  1.1  christos 			 */
   4155  1.7  christos 			if (header->type == type || type == dns_rdatatype_any ||
   4156  1.7  christos 			    (header->type == dns_rdatatype_cname && cname_ok))
   4157  1.7  christos 			{
   4158  1.1  christos 				/*
   4159  1.1  christos 				 * We've found the answer!
   4160  1.1  christos 				 */
   4161  1.1  christos 				found = header;
   4162  1.1  christos 				if (header->type == dns_rdatatype_cname &&
   4163  1.1  christos 				    cname_ok) {
   4164  1.1  christos 					/*
   4165  1.1  christos 					 * We may be finding a CNAME instead
   4166  1.1  christos 					 * of the desired type.
   4167  1.1  christos 					 *
   4168  1.1  christos 					 * If we've already got the CNAME RRSIG,
   4169  1.1  christos 					 * use it, otherwise change sigtype
   4170  1.1  christos 					 * so that we find it.
   4171  1.1  christos 					 */
   4172  1.7  christos 					if (cnamesig != NULL) {
   4173  1.1  christos 						foundsig = cnamesig;
   4174  1.7  christos 					} else {
   4175  1.1  christos 						sigtype =
   4176  1.7  christos 							RBTDB_RDATATYPE_SIGCNAME;
   4177  1.7  christos 					}
   4178  1.1  christos 				}
   4179  1.1  christos 				/*
   4180  1.1  christos 				 * If we've got all we need, end the search.
   4181  1.1  christos 				 */
   4182  1.7  christos 				if (!maybe_zonecut && foundsig != NULL) {
   4183  1.1  christos 					break;
   4184  1.7  christos 				}
   4185  1.1  christos 			} else if (header->type == sigtype) {
   4186  1.1  christos 				/*
   4187  1.1  christos 				 * We've found the RRSIG rdataset for our
   4188  1.1  christos 				 * target type.  Remember it.
   4189  1.1  christos 				 */
   4190  1.1  christos 				foundsig = header;
   4191  1.1  christos 				/*
   4192  1.1  christos 				 * If we've got all we need, end the search.
   4193  1.1  christos 				 */
   4194  1.7  christos 				if (!maybe_zonecut && found != NULL) {
   4195  1.1  christos 					break;
   4196  1.7  christos 				}
   4197  1.1  christos 			} else if (header->type == dns_rdatatype_nsec &&
   4198  1.1  christos 				   !search.rbtversion->havensec3) {
   4199  1.1  christos 				/*
   4200  1.1  christos 				 * Remember a NSEC rdataset even if we're
   4201  1.1  christos 				 * not specifically looking for it, because
   4202  1.1  christos 				 * we might need it later.
   4203  1.1  christos 				 */
   4204  1.1  christos 				nsecheader = header;
   4205  1.1  christos 			} else if (header->type == RBTDB_RDATATYPE_SIGNSEC &&
   4206  1.7  christos 				   !search.rbtversion->havensec3)
   4207  1.7  christos 			{
   4208  1.1  christos 				/*
   4209  1.1  christos 				 * If we need the NSEC rdataset, we'll also
   4210  1.1  christos 				 * need its signature.
   4211  1.1  christos 				 */
   4212  1.1  christos 				nsecsig = header;
   4213  1.1  christos 			} else if (cname_ok &&
   4214  1.1  christos 				   header->type == RBTDB_RDATATYPE_SIGCNAME) {
   4215  1.1  christos 				/*
   4216  1.1  christos 				 * If we get a CNAME match, we'll also need
   4217  1.1  christos 				 * its signature.
   4218  1.1  christos 				 */
   4219  1.1  christos 				cnamesig = header;
   4220  1.1  christos 			}
   4221  1.1  christos 		}
   4222  1.1  christos 	}
   4223  1.1  christos 
   4224  1.1  christos 	if (empty_node) {
   4225  1.1  christos 		/*
   4226  1.1  christos 		 * We have an exact match for the name, but there are no
   4227  1.1  christos 		 * active rdatasets in the desired version.  That means that
   4228  1.1  christos 		 * this node doesn't exist in the desired version, and that
   4229  1.1  christos 		 * we really have a partial match.
   4230  1.1  christos 		 */
   4231  1.1  christos 		if (!wild) {
   4232  1.1  christos 			NODE_UNLOCK(lock, isc_rwlocktype_read);
   4233  1.1  christos 			goto partial_match;
   4234  1.1  christos 		}
   4235  1.1  christos 	}
   4236  1.1  christos 
   4237  1.1  christos 	/*
   4238  1.1  christos 	 * If we didn't find what we were looking for...
   4239  1.1  christos 	 */
   4240  1.1  christos 	if (found == NULL) {
   4241  1.1  christos 		if (search.zonecut != NULL) {
   4242  1.1  christos 			/*
   4243  1.1  christos 			 * We were trying to find glue at a node beneath a
   4244  1.1  christos 			 * zone cut, but didn't.
   4245  1.1  christos 			 *
   4246  1.1  christos 			 * Return the delegation.
   4247  1.1  christos 			 */
   4248  1.1  christos 			NODE_UNLOCK(lock, isc_rwlocktype_read);
   4249  1.1  christos 			result = setup_delegation(&search, nodep, foundname,
   4250  1.1  christos 						  rdataset, sigrdataset);
   4251  1.1  christos 			goto tree_exit;
   4252  1.1  christos 		}
   4253  1.1  christos 		/*
   4254  1.1  christos 		 * The desired type doesn't exist.
   4255  1.1  christos 		 */
   4256  1.1  christos 		result = DNS_R_NXRRSET;
   4257  1.1  christos 		if (search.rbtversion->secure == dns_db_secure &&
   4258  1.1  christos 		    !search.rbtversion->havensec3 &&
   4259  1.7  christos 		    (nsecheader == NULL || nsecsig == NULL))
   4260  1.7  christos 		{
   4261  1.1  christos 			/*
   4262  1.1  christos 			 * The zone is secure but there's no NSEC,
   4263  1.1  christos 			 * or the NSEC has no signature!
   4264  1.1  christos 			 */
   4265  1.1  christos 			if (!wild) {
   4266  1.1  christos 				result = DNS_R_BADDB;
   4267  1.1  christos 				goto node_exit;
   4268  1.1  christos 			}
   4269  1.1  christos 
   4270  1.1  christos 			NODE_UNLOCK(lock, isc_rwlocktype_read);
   4271  1.1  christos 			result = find_closest_nsec(&search, nodep, foundname,
   4272  1.1  christos 						   rdataset, sigrdataset,
   4273  1.1  christos 						   search.rbtdb->tree,
   4274  1.1  christos 						   search.rbtversion->secure);
   4275  1.7  christos 			if (result == ISC_R_SUCCESS) {
   4276  1.1  christos 				result = DNS_R_EMPTYWILD;
   4277  1.7  christos 			}
   4278  1.1  christos 			goto tree_exit;
   4279  1.1  christos 		}
   4280  1.1  christos 		if ((search.options & DNS_DBFIND_FORCENSEC) != 0 &&
   4281  1.7  christos 		    nsecheader == NULL) {
   4282  1.1  christos 			/*
   4283  1.1  christos 			 * There's no NSEC record, and we were told
   4284  1.1  christos 			 * to find one.
   4285  1.1  christos 			 */
   4286  1.1  christos 			result = DNS_R_BADDB;
   4287  1.1  christos 			goto node_exit;
   4288  1.1  christos 		}
   4289  1.1  christos 		if (nodep != NULL) {
   4290  1.1  christos 			new_reference(search.rbtdb, node);
   4291  1.1  christos 			*nodep = node;
   4292  1.1  christos 		}
   4293  1.1  christos 		if ((search.rbtversion->secure == dns_db_secure &&
   4294  1.1  christos 		     !search.rbtversion->havensec3) ||
   4295  1.1  christos 		    (search.options & DNS_DBFIND_FORCENSEC) != 0)
   4296  1.1  christos 		{
   4297  1.7  christos 			bind_rdataset(search.rbtdb, node, nsecheader, 0,
   4298  1.7  christos 				      rdataset);
   4299  1.7  christos 			if (nsecsig != NULL) {
   4300  1.7  christos 				bind_rdataset(search.rbtdb, node, nsecsig, 0,
   4301  1.7  christos 					      sigrdataset);
   4302  1.7  christos 			}
   4303  1.1  christos 		}
   4304  1.7  christos 		if (wild) {
   4305  1.1  christos 			foundname->attributes |= DNS_NAMEATTR_WILDCARD;
   4306  1.7  christos 		}
   4307  1.1  christos 		goto node_exit;
   4308  1.1  christos 	}
   4309  1.1  christos 
   4310  1.1  christos 	/*
   4311  1.1  christos 	 * We found what we were looking for, or we found a CNAME.
   4312  1.1  christos 	 */
   4313  1.1  christos 
   4314  1.7  christos 	if (type != found->type && type != dns_rdatatype_any &&
   4315  1.7  christos 	    found->type == dns_rdatatype_cname)
   4316  1.7  christos 	{
   4317  1.1  christos 		/*
   4318  1.1  christos 		 * We weren't doing an ANY query and we found a CNAME instead
   4319  1.1  christos 		 * of the type we were looking for, so we need to indicate
   4320  1.1  christos 		 * that result to the caller.
   4321  1.1  christos 		 */
   4322  1.1  christos 		result = DNS_R_CNAME;
   4323  1.1  christos 	} else if (search.zonecut != NULL) {
   4324  1.1  christos 		/*
   4325  1.1  christos 		 * If we're beneath a zone cut, we must indicate that the
   4326  1.1  christos 		 * result is glue, unless we're actually at the zone cut
   4327  1.1  christos 		 * and the type is NSEC or KEY.
   4328  1.1  christos 		 */
   4329  1.1  christos 		if (search.zonecut == node) {
   4330  1.1  christos 			/*
   4331  1.1  christos 			 * It is not clear if KEY should still be
   4332  1.1  christos 			 * allowed at the parent side of the zone
   4333  1.1  christos 			 * cut or not.  It is needed for RFC3007
   4334  1.1  christos 			 * validated updates.
   4335  1.1  christos 			 */
   4336  1.1  christos 			if (type == dns_rdatatype_nsec ||
   4337  1.1  christos 			    type == dns_rdatatype_nsec3 ||
   4338  1.1  christos 			    type == dns_rdatatype_key)
   4339  1.7  christos 			{
   4340  1.1  christos 				result = ISC_R_SUCCESS;
   4341  1.7  christos 			} else if (type == dns_rdatatype_any) {
   4342  1.1  christos 				result = DNS_R_ZONECUT;
   4343  1.7  christos 			} else {
   4344  1.1  christos 				result = DNS_R_GLUE;
   4345  1.7  christos 			}
   4346  1.7  christos 		} else {
   4347  1.1  christos 			result = DNS_R_GLUE;
   4348  1.7  christos 		}
   4349  1.1  christos 		/*
   4350  1.1  christos 		 * We might have found data that isn't glue, but was occluded
   4351  1.1  christos 		 * by a dynamic update.  If the caller cares about this, they
   4352  1.1  christos 		 * will have told us to validate glue.
   4353  1.1  christos 		 *
   4354  1.1  christos 		 * XXX We should cache the glue validity state!
   4355  1.1  christos 		 */
   4356  1.1  christos 		if (result == DNS_R_GLUE &&
   4357  1.1  christos 		    (search.options & DNS_DBFIND_VALIDATEGLUE) != 0 &&
   4358  1.7  christos 		    !valid_glue(&search, foundname, type, node))
   4359  1.7  christos 		{
   4360  1.1  christos 			NODE_UNLOCK(lock, isc_rwlocktype_read);
   4361  1.1  christos 			result = setup_delegation(&search, nodep, foundname,
   4362  1.1  christos 						  rdataset, sigrdataset);
   4363  1.7  christos 			goto tree_exit;
   4364  1.1  christos 		}
   4365  1.1  christos 	} else {
   4366  1.1  christos 		/*
   4367  1.1  christos 		 * An ordinary successful query!
   4368  1.1  christos 		 */
   4369  1.1  christos 		result = ISC_R_SUCCESS;
   4370  1.1  christos 	}
   4371  1.1  christos 
   4372  1.1  christos 	if (nodep != NULL) {
   4373  1.7  christos 		if (!at_zonecut) {
   4374  1.1  christos 			new_reference(search.rbtdb, node);
   4375  1.7  christos 		} else {
   4376  1.3  christos 			search.need_cleanup = false;
   4377  1.7  christos 		}
   4378  1.1  christos 		*nodep = node;
   4379  1.1  christos 	}
   4380  1.1  christos 
   4381  1.1  christos 	if (type != dns_rdatatype_any) {
   4382  1.1  christos 		bind_rdataset(search.rbtdb, node, found, 0, rdataset);
   4383  1.7  christos 		if (foundsig != NULL) {
   4384  1.1  christos 			bind_rdataset(search.rbtdb, node, foundsig, 0,
   4385  1.1  christos 				      sigrdataset);
   4386  1.7  christos 		}
   4387  1.1  christos 	}
   4388  1.1  christos 
   4389  1.7  christos 	if (wild) {
   4390  1.1  christos 		foundname->attributes |= DNS_NAMEATTR_WILDCARD;
   4391  1.7  christos 	}
   4392  1.1  christos 
   4393  1.7  christos node_exit:
   4394  1.1  christos 	NODE_UNLOCK(lock, isc_rwlocktype_read);
   4395  1.1  christos 
   4396  1.7  christos tree_exit:
   4397  1.1  christos 	RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
   4398  1.1  christos 
   4399  1.1  christos 	/*
   4400  1.1  christos 	 * If we found a zonecut but aren't going to use it, we have to
   4401  1.1  christos 	 * let go of it.
   4402  1.1  christos 	 */
   4403  1.1  christos 	if (search.need_cleanup) {
   4404  1.1  christos 		node = search.zonecut;
   4405  1.1  christos 		INSIST(node != NULL);
   4406  1.1  christos 		lock = &(search.rbtdb->node_locks[node->locknum].lock);
   4407  1.1  christos 
   4408  1.1  christos 		NODE_LOCK(lock, isc_rwlocktype_read);
   4409  1.7  christos 		decrement_reference(search.rbtdb, node, 0, isc_rwlocktype_read,
   4410  1.7  christos 				    isc_rwlocktype_none, false);
   4411  1.1  christos 		NODE_UNLOCK(lock, isc_rwlocktype_read);
   4412  1.1  christos 	}
   4413  1.1  christos 
   4414  1.7  christos 	if (close_version) {
   4415  1.3  christos 		closeversion(db, &version, false);
   4416  1.7  christos 	}
   4417  1.1  christos 
   4418  1.1  christos 	dns_rbtnodechain_reset(&search.chain);
   4419  1.1  christos 
   4420  1.1  christos 	return (result);
   4421  1.1  christos }
   4422  1.1  christos 
   4423  1.1  christos static isc_result_t
   4424  1.1  christos zone_findzonecut(dns_db_t *db, const dns_name_t *name, unsigned int options,
   4425  1.7  christos 		 isc_stdtime_t now, dns_dbnode_t **nodep, dns_name_t *foundname,
   4426  1.7  christos 		 dns_name_t *dcname, dns_rdataset_t *rdataset,
   4427  1.7  christos 		 dns_rdataset_t *sigrdataset) {
   4428  1.1  christos 	UNUSED(db);
   4429  1.1  christos 	UNUSED(name);
   4430  1.1  christos 	UNUSED(options);
   4431  1.1  christos 	UNUSED(now);
   4432  1.1  christos 	UNUSED(nodep);
   4433  1.1  christos 	UNUSED(foundname);
   4434  1.3  christos 	UNUSED(dcname);
   4435  1.1  christos 	UNUSED(rdataset);
   4436  1.1  christos 	UNUSED(sigrdataset);
   4437  1.1  christos 
   4438  1.1  christos 	FATAL_ERROR(__FILE__, __LINE__, "zone_findzonecut() called!");
   4439  1.1  christos 
   4440  1.1  christos 	/* NOTREACHED */
   4441  1.1  christos 	return (ISC_R_NOTIMPLEMENTED);
   4442  1.1  christos }
   4443  1.1  christos 
   4444  1.3  christos static bool
   4445  1.1  christos check_stale_header(dns_rbtnode_t *node, rdatasetheader_t *header,
   4446  1.1  christos 		   isc_rwlocktype_t *locktype, nodelock_t *lock,
   4447  1.7  christos 		   rbtdb_search_t *search, rdatasetheader_t **header_prev) {
   4448  1.1  christos 	if (!ACTIVE(header, search->now)) {
   4449  1.1  christos 		dns_ttl_t stale = header->rdh_ttl +
   4450  1.1  christos 				  search->rbtdb->serve_stale_ttl;
   4451  1.1  christos 		/*
   4452  1.1  christos 		 * If this data is in the stale window keep it and if
   4453  1.1  christos 		 * DNS_DBFIND_STALEOK is not set we tell the caller to
   4454  1.1  christos 		 * skip this record.
   4455  1.1  christos 		 */
   4456  1.1  christos 		if (KEEPSTALE(search->rbtdb) && stale > search->now) {
   4457  1.5  christos 			mark_header_stale(search->rbtdb, header);
   4458  1.1  christos 			*header_prev = header;
   4459  1.1  christos 			return ((search->options & DNS_DBFIND_STALEOK) == 0);
   4460  1.1  christos 		}
   4461  1.1  christos 
   4462  1.1  christos 		/*
   4463  1.1  christos 		 * This rdataset is stale.  If no one else is using the
   4464  1.1  christos 		 * node, we can clean it up right now, otherwise we mark
   4465  1.5  christos 		 * it as ancient, and the node as dirty, so it will get
   4466  1.1  christos 		 * cleaned up later.
   4467  1.1  christos 		 */
   4468  1.1  christos 		if ((header->rdh_ttl < search->now - RBTDB_VIRTUAL) &&
   4469  1.1  christos 		    (*locktype == isc_rwlocktype_write ||
   4470  1.1  christos 		     NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS))
   4471  1.1  christos 		{
   4472  1.1  christos 			/*
   4473  1.1  christos 			 * We update the node's status only when we can
   4474  1.1  christos 			 * get write access; otherwise, we leave others
   4475  1.1  christos 			 * to this work.  Periodical cleaning will
   4476  1.1  christos 			 * eventually take the job as the last resort.
   4477  1.1  christos 			 * We won't downgrade the lock, since other
   4478  1.1  christos 			 * rdatasets are probably stale, too.
   4479  1.1  christos 			 */
   4480  1.1  christos 			*locktype = isc_rwlocktype_write;
   4481  1.1  christos 
   4482  1.3  christos 			if (isc_refcount_current(&node->references) == 0) {
   4483  1.1  christos 				isc_mem_t *mctx;
   4484  1.1  christos 
   4485  1.1  christos 				/*
   4486  1.1  christos 				 * header->down can be non-NULL if the
   4487  1.1  christos 				 * refcount has just decremented to 0
   4488  1.1  christos 				 * but decrement_reference() has not
   4489  1.1  christos 				 * performed clean_cache_node(), in
   4490  1.1  christos 				 * which case we need to purge the stale
   4491  1.1  christos 				 * headers first.
   4492  1.1  christos 				 */
   4493  1.1  christos 				mctx = search->rbtdb->common.mctx;
   4494  1.7  christos 				clean_stale_headers(search->rbtdb, mctx,
   4495  1.7  christos 						    header);
   4496  1.7  christos 				if (*header_prev != NULL) {
   4497  1.1  christos 					(*header_prev)->next = header->next;
   4498  1.7  christos 				} else {
   4499  1.1  christos 					node->data = header->next;
   4500  1.7  christos 				}
   4501  1.1  christos 				free_rdataset(search->rbtdb, mctx, header);
   4502  1.1  christos 			} else {
   4503  1.1  christos 				mark_header_ancient(search->rbtdb, header);
   4504  1.1  christos 				*header_prev = header;
   4505  1.1  christos 			}
   4506  1.7  christos 		} else {
   4507  1.1  christos 			*header_prev = header;
   4508  1.7  christos 		}
   4509  1.3  christos 		return (true);
   4510  1.1  christos 	}
   4511  1.3  christos 	return (false);
   4512  1.1  christos }
   4513  1.1  christos 
   4514  1.1  christos static isc_result_t
   4515  1.1  christos cache_zonecut_callback(dns_rbtnode_t *node, dns_name_t *name, void *arg) {
   4516  1.1  christos 	rbtdb_search_t *search = arg;
   4517  1.1  christos 	rdatasetheader_t *header, *header_prev, *header_next;
   4518  1.1  christos 	rdatasetheader_t *dname_header, *sigdname_header;
   4519  1.1  christos 	isc_result_t result;
   4520  1.1  christos 	nodelock_t *lock;
   4521  1.1  christos 	isc_rwlocktype_t locktype;
   4522  1.1  christos 
   4523  1.1  christos 	/* XXX comment */
   4524  1.1  christos 
   4525  1.1  christos 	REQUIRE(search->zonecut == NULL);
   4526  1.1  christos 
   4527  1.1  christos 	/*
   4528  1.1  christos 	 * Keep compiler silent.
   4529  1.1  christos 	 */
   4530  1.1  christos 	UNUSED(name);
   4531  1.1  christos 
   4532  1.1  christos 	lock = &(search->rbtdb->node_locks[node->locknum].lock);
   4533  1.1  christos 	locktype = isc_rwlocktype_read;
   4534  1.1  christos 	NODE_LOCK(lock, locktype);
   4535  1.1  christos 
   4536  1.1  christos 	/*
   4537  1.1  christos 	 * Look for a DNAME or RRSIG DNAME rdataset.
   4538  1.1  christos 	 */
   4539  1.1  christos 	dname_header = NULL;
   4540  1.1  christos 	sigdname_header = NULL;
   4541  1.1  christos 	header_prev = NULL;
   4542  1.1  christos 	for (header = node->data; header != NULL; header = header_next) {
   4543  1.1  christos 		header_next = header->next;
   4544  1.7  christos 		if (check_stale_header(node, header, &locktype, lock, search,
   4545  1.1  christos 				       &header_prev)) {
   4546  1.1  christos 			/* Do nothing. */
   4547  1.1  christos 		} else if (header->type == dns_rdatatype_dname &&
   4548  1.1  christos 			   EXISTS(header)) {
   4549  1.1  christos 			dname_header = header;
   4550  1.1  christos 			header_prev = header;
   4551  1.1  christos 		} else if (header->type == RBTDB_RDATATYPE_SIGDNAME &&
   4552  1.7  christos 			   EXISTS(header)) {
   4553  1.1  christos 			sigdname_header = header;
   4554  1.1  christos 			header_prev = header;
   4555  1.7  christos 		} else {
   4556  1.1  christos 			header_prev = header;
   4557  1.7  christos 		}
   4558  1.1  christos 	}
   4559  1.1  christos 
   4560  1.1  christos 	if (dname_header != NULL &&
   4561  1.1  christos 	    (!DNS_TRUST_PENDING(dname_header->trust) ||
   4562  1.7  christos 	     (search->options & DNS_DBFIND_PENDINGOK) != 0))
   4563  1.7  christos 	{
   4564  1.1  christos 		/*
   4565  1.1  christos 		 * We increment the reference count on node to ensure that
   4566  1.1  christos 		 * search->zonecut_rdataset will still be valid later.
   4567  1.1  christos 		 */
   4568  1.1  christos 		new_reference(search->rbtdb, node);
   4569  1.1  christos 		INSIST(!ISC_LINK_LINKED(node, deadlink));
   4570  1.1  christos 		search->zonecut = node;
   4571  1.1  christos 		search->zonecut_rdataset = dname_header;
   4572  1.1  christos 		search->zonecut_sigrdataset = sigdname_header;
   4573  1.3  christos 		search->need_cleanup = true;
   4574  1.1  christos 		result = DNS_R_PARTIALMATCH;
   4575  1.7  christos 	} else {
   4576  1.1  christos 		result = DNS_R_CONTINUE;
   4577  1.7  christos 	}
   4578  1.1  christos 
   4579  1.1  christos 	NODE_UNLOCK(lock, locktype);
   4580  1.1  christos 
   4581  1.1  christos 	return (result);
   4582  1.1  christos }
   4583  1.1  christos 
   4584  1.1  christos static inline isc_result_t
   4585  1.1  christos find_deepest_zonecut(rbtdb_search_t *search, dns_rbtnode_t *node,
   4586  1.1  christos 		     dns_dbnode_t **nodep, dns_name_t *foundname,
   4587  1.7  christos 		     dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset) {
   4588  1.1  christos 	unsigned int i;
   4589  1.1  christos 	dns_rbtnode_t *level_node;
   4590  1.1  christos 	rdatasetheader_t *header, *header_prev, *header_next;
   4591  1.1  christos 	rdatasetheader_t *found, *foundsig;
   4592  1.1  christos 	isc_result_t result = ISC_R_NOTFOUND;
   4593  1.1  christos 	dns_name_t name;
   4594  1.1  christos 	dns_rbtdb_t *rbtdb;
   4595  1.3  christos 	bool done;
   4596  1.1  christos 	nodelock_t *lock;
   4597  1.1  christos 	isc_rwlocktype_t locktype;
   4598  1.1  christos 
   4599  1.1  christos 	/*
   4600  1.1  christos 	 * Caller must be holding the tree lock.
   4601  1.1  christos 	 */
   4602  1.1  christos 
   4603  1.1  christos 	rbtdb = search->rbtdb;
   4604  1.1  christos 	i = search->chain.level_matches;
   4605  1.3  christos 	done = false;
   4606  1.1  christos 	do {
   4607  1.1  christos 		locktype = isc_rwlocktype_read;
   4608  1.1  christos 		lock = &rbtdb->node_locks[node->locknum].lock;
   4609  1.1  christos 		NODE_LOCK(lock, locktype);
   4610  1.1  christos 
   4611  1.1  christos 		/*
   4612  1.1  christos 		 * Look for NS and RRSIG NS rdatasets.
   4613  1.1  christos 		 */
   4614  1.1  christos 		found = NULL;
   4615  1.1  christos 		foundsig = NULL;
   4616  1.1  christos 		header_prev = NULL;
   4617  1.7  christos 		for (header = node->data; header != NULL; header = header_next)
   4618  1.7  christos 		{
   4619  1.1  christos 			header_next = header->next;
   4620  1.7  christos 			if (check_stale_header(node, header, &locktype, lock,
   4621  1.7  christos 					       search, &header_prev)) {
   4622  1.1  christos 				/* Do nothing. */
   4623  1.1  christos 			} else if (EXISTS(header)) {
   4624  1.1  christos 				/*
   4625  1.1  christos 				 * We've found an extant rdataset.  See if
   4626  1.1  christos 				 * we're interested in it.
   4627  1.1  christos 				 */
   4628  1.1  christos 				if (header->type == dns_rdatatype_ns) {
   4629  1.1  christos 					found = header;
   4630  1.7  christos 					if (foundsig != NULL) {
   4631  1.1  christos 						break;
   4632  1.7  christos 					}
   4633  1.1  christos 				} else if (header->type ==
   4634  1.1  christos 					   RBTDB_RDATATYPE_SIGNS) {
   4635  1.1  christos 					foundsig = header;
   4636  1.7  christos 					if (found != NULL) {
   4637  1.1  christos 						break;
   4638  1.7  christos 					}
   4639  1.1  christos 				}
   4640  1.1  christos 				header_prev = header;
   4641  1.7  christos 			} else {
   4642  1.1  christos 				header_prev = header;
   4643  1.7  christos 			}
   4644  1.1  christos 		}
   4645  1.1  christos 
   4646  1.1  christos 		if (found != NULL) {
   4647  1.1  christos 			/*
   4648  1.1  christos 			 * If we have to set foundname, we do it before
   4649  1.1  christos 			 * anything else.  If we were to set foundname after
   4650  1.1  christos 			 * we had set nodep or bound the rdataset, then we'd
   4651  1.1  christos 			 * have to undo that work if dns_name_concatenate()
   4652  1.1  christos 			 * failed.  By setting foundname first, there's
   4653  1.1  christos 			 * nothing to undo if we have trouble.
   4654  1.1  christos 			 */
   4655  1.1  christos 			if (foundname != NULL) {
   4656  1.1  christos 				dns_name_init(&name, NULL);
   4657  1.1  christos 				dns_rbt_namefromnode(node, &name);
   4658  1.6  christos 				dns_name_copynf(&name, foundname);
   4659  1.6  christos 				while (i > 0) {
   4660  1.1  christos 					i--;
   4661  1.1  christos 					level_node = search->chain.levels[i];
   4662  1.1  christos 					dns_name_init(&name, NULL);
   4663  1.7  christos 					dns_rbt_namefromnode(level_node, &name);
   4664  1.7  christos 					result = dns_name_concatenate(
   4665  1.7  christos 						foundname, &name, foundname,
   4666  1.7  christos 						NULL);
   4667  1.6  christos 					if (result != ISC_R_SUCCESS) {
   4668  1.6  christos 						if (nodep != NULL) {
   4669  1.6  christos 							*nodep = NULL;
   4670  1.6  christos 						}
   4671  1.6  christos 						goto node_exit;
   4672  1.6  christos 					}
   4673  1.1  christos 				}
   4674  1.1  christos 			}
   4675  1.1  christos 			result = DNS_R_DELEGATION;
   4676  1.1  christos 			if (nodep != NULL) {
   4677  1.1  christos 				new_reference(search->rbtdb, node);
   4678  1.1  christos 				*nodep = node;
   4679  1.1  christos 			}
   4680  1.1  christos 			bind_rdataset(search->rbtdb, node, found, search->now,
   4681  1.1  christos 				      rdataset);
   4682  1.7  christos 			if (foundsig != NULL) {
   4683  1.1  christos 				bind_rdataset(search->rbtdb, node, foundsig,
   4684  1.1  christos 					      search->now, sigrdataset);
   4685  1.7  christos 			}
   4686  1.1  christos 			if (need_headerupdate(found, search->now) ||
   4687  1.1  christos 			    (foundsig != NULL &&
   4688  1.7  christos 			     need_headerupdate(foundsig, search->now)))
   4689  1.7  christos 			{
   4690  1.1  christos 				if (locktype != isc_rwlocktype_write) {
   4691  1.1  christos 					NODE_UNLOCK(lock, locktype);
   4692  1.1  christos 					NODE_LOCK(lock, isc_rwlocktype_write);
   4693  1.1  christos 					locktype = isc_rwlocktype_write;
   4694  1.1  christos 					POST(locktype);
   4695  1.1  christos 				}
   4696  1.7  christos 				if (need_headerupdate(found, search->now)) {
   4697  1.1  christos 					update_header(search->rbtdb, found,
   4698  1.1  christos 						      search->now);
   4699  1.7  christos 				}
   4700  1.1  christos 				if (foundsig != NULL &&
   4701  1.1  christos 				    need_headerupdate(foundsig, search->now)) {
   4702  1.1  christos 					update_header(search->rbtdb, foundsig,
   4703  1.1  christos 						      search->now);
   4704  1.1  christos 				}
   4705  1.1  christos 			}
   4706  1.1  christos 		}
   4707  1.1  christos 
   4708  1.1  christos 	node_exit:
   4709  1.1  christos 		NODE_UNLOCK(lock, locktype);
   4710  1.1  christos 
   4711  1.1  christos 		if (found == NULL && i > 0) {
   4712  1.1  christos 			i--;
   4713  1.1  christos 			node = search->chain.levels[i];
   4714  1.7  christos 		} else {
   4715  1.3  christos 			done = true;
   4716  1.7  christos 		}
   4717  1.1  christos 	} while (!done);
   4718  1.1  christos 
   4719  1.1  christos 	return (result);
   4720  1.1  christos }
   4721  1.1  christos 
   4722  1.1  christos static isc_result_t
   4723  1.1  christos find_coveringnsec(rbtdb_search_t *search, dns_dbnode_t **nodep,
   4724  1.1  christos 		  isc_stdtime_t now, dns_name_t *foundname,
   4725  1.7  christos 		  dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset) {
   4726  1.1  christos 	dns_rbtnode_t *node;
   4727  1.1  christos 	rdatasetheader_t *header, *header_next, *header_prev;
   4728  1.1  christos 	rdatasetheader_t *found, *foundsig;
   4729  1.3  christos 	bool empty_node;
   4730  1.1  christos 	isc_result_t result;
   4731  1.1  christos 	dns_fixedname_t fname, forigin;
   4732  1.1  christos 	dns_name_t *name, *origin;
   4733  1.1  christos 	rbtdb_rdatatype_t matchtype, sigmatchtype;
   4734  1.1  christos 	nodelock_t *lock;
   4735  1.1  christos 	isc_rwlocktype_t locktype;
   4736  1.1  christos 	dns_rbtnodechain_t chain;
   4737  1.1  christos 
   4738  1.1  christos 	chain = search->chain;
   4739  1.1  christos 
   4740  1.1  christos 	matchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_nsec, 0);
   4741  1.1  christos 	sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig,
   4742  1.1  christos 					     dns_rdatatype_nsec);
   4743  1.1  christos 
   4744  1.1  christos 	do {
   4745  1.1  christos 		node = NULL;
   4746  1.1  christos 		name = dns_fixedname_initname(&fname);
   4747  1.1  christos 		origin = dns_fixedname_initname(&forigin);
   4748  1.1  christos 		result = dns_rbtnodechain_current(&chain, name, origin, &node);
   4749  1.7  christos 		if (result != ISC_R_SUCCESS) {
   4750  1.1  christos 			return (result);
   4751  1.7  christos 		}
   4752  1.1  christos 		locktype = isc_rwlocktype_read;
   4753  1.1  christos 		lock = &(search->rbtdb->node_locks[node->locknum].lock);
   4754  1.1  christos 		NODE_LOCK(lock, locktype);
   4755  1.1  christos 		found = NULL;
   4756  1.1  christos 		foundsig = NULL;
   4757  1.3  christos 		empty_node = true;
   4758  1.1  christos 		header_prev = NULL;
   4759  1.1  christos 		for (header = node->data; header != NULL; header = header_next)
   4760  1.1  christos 		{
   4761  1.1  christos 			header_next = header->next;
   4762  1.7  christos 			if (check_stale_header(node, header, &locktype, lock,
   4763  1.7  christos 					       search, &header_prev)) {
   4764  1.1  christos 				continue;
   4765  1.1  christos 			}
   4766  1.1  christos 			if (NONEXISTENT(header) ||
   4767  1.1  christos 			    RBTDB_RDATATYPE_BASE(header->type) == 0) {
   4768  1.1  christos 				header_prev = header;
   4769  1.1  christos 				continue;
   4770  1.1  christos 			}
   4771  1.1  christos 			/*
   4772  1.1  christos 			 * Don't stop on provable noqname / RRSIG.
   4773  1.1  christos 			 */
   4774  1.1  christos 			if (header->noqname == NULL &&
   4775  1.7  christos 			    RBTDB_RDATATYPE_BASE(header->type) !=
   4776  1.7  christos 				    dns_rdatatype_rrsig)
   4777  1.1  christos 			{
   4778  1.3  christos 				empty_node = false;
   4779  1.1  christos 			}
   4780  1.7  christos 			if (header->type == matchtype) {
   4781  1.1  christos 				found = header;
   4782  1.7  christos 			} else if (header->type == sigmatchtype) {
   4783  1.1  christos 				foundsig = header;
   4784  1.7  christos 			}
   4785  1.1  christos 			header_prev = header;
   4786  1.1  christos 		}
   4787  1.1  christos 		if (found != NULL) {
   4788  1.7  christos 			result = dns_name_concatenate(name, origin, foundname,
   4789  1.7  christos 						      NULL);
   4790  1.7  christos 			if (result != ISC_R_SUCCESS) {
   4791  1.1  christos 				goto unlock_node;
   4792  1.7  christos 			}
   4793  1.7  christos 			bind_rdataset(search->rbtdb, node, found, now,
   4794  1.7  christos 				      rdataset);
   4795  1.7  christos 			if (foundsig != NULL) {
   4796  1.1  christos 				bind_rdataset(search->rbtdb, node, foundsig,
   4797  1.1  christos 					      now, sigrdataset);
   4798  1.7  christos 			}
   4799  1.1  christos 			new_reference(search->rbtdb, node);
   4800  1.1  christos 			*nodep = node;
   4801  1.1  christos 			result = DNS_R_COVERINGNSEC;
   4802  1.1  christos 		} else if (!empty_node) {
   4803  1.1  christos 			result = ISC_R_NOTFOUND;
   4804  1.7  christos 		} else {
   4805  1.1  christos 			result = dns_rbtnodechain_prev(&chain, NULL, NULL);
   4806  1.7  christos 		}
   4807  1.7  christos 	unlock_node:
   4808  1.1  christos 		NODE_UNLOCK(lock, locktype);
   4809  1.1  christos 	} while (empty_node && result == ISC_R_SUCCESS);
   4810  1.1  christos 	return (result);
   4811  1.1  christos }
   4812  1.1  christos 
   4813  1.1  christos static isc_result_t
   4814  1.1  christos cache_find(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
   4815  1.1  christos 	   dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
   4816  1.1  christos 	   dns_dbnode_t **nodep, dns_name_t *foundname,
   4817  1.7  christos 	   dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset) {
   4818  1.1  christos 	dns_rbtnode_t *node = NULL;
   4819  1.1  christos 	isc_result_t result;
   4820  1.1  christos 	rbtdb_search_t search;
   4821  1.3  christos 	bool cname_ok = true;
   4822  1.3  christos 	bool empty_node;
   4823  1.1  christos 	nodelock_t *lock;
   4824  1.1  christos 	isc_rwlocktype_t locktype;
   4825  1.1  christos 	rdatasetheader_t *header, *header_prev, *header_next;
   4826  1.1  christos 	rdatasetheader_t *found, *nsheader;
   4827  1.1  christos 	rdatasetheader_t *foundsig, *nssig, *cnamesig;
   4828  1.1  christos 	rdatasetheader_t *update, *updatesig;
   4829  1.1  christos 	rdatasetheader_t *nsecheader, *nsecsig;
   4830  1.1  christos 	rbtdb_rdatatype_t sigtype, negtype;
   4831  1.1  christos 
   4832  1.1  christos 	UNUSED(version);
   4833  1.1  christos 
   4834  1.1  christos 	search.rbtdb = (dns_rbtdb_t *)db;
   4835  1.1  christos 
   4836  1.1  christos 	REQUIRE(VALID_RBTDB(search.rbtdb));
   4837  1.1  christos 	REQUIRE(version == NULL);
   4838  1.1  christos 
   4839  1.7  christos 	if (now == 0) {
   4840  1.1  christos 		isc_stdtime_get(&now);
   4841  1.7  christos 	}
   4842  1.1  christos 
   4843  1.1  christos 	search.rbtversion = NULL;
   4844  1.1  christos 	search.serial = 1;
   4845  1.1  christos 	search.options = options;
   4846  1.3  christos 	search.copy_name = false;
   4847  1.3  christos 	search.need_cleanup = false;
   4848  1.3  christos 	search.wild = false;
   4849  1.1  christos 	search.zonecut = NULL;
   4850  1.1  christos 	dns_fixedname_init(&search.zonecut_name);
   4851  1.7  christos 	dns_rbtnodechain_init(&search.chain);
   4852  1.1  christos 	search.now = now;
   4853  1.1  christos 	update = NULL;
   4854  1.1  christos 	updatesig = NULL;
   4855  1.1  christos 
   4856  1.1  christos 	RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
   4857  1.1  christos 
   4858  1.1  christos 	/*
   4859  1.1  christos 	 * Search down from the root of the tree.  If, while going down, we
   4860  1.1  christos 	 * encounter a callback node, cache_zonecut_callback() will search the
   4861  1.1  christos 	 * rdatasets at the zone cut for a DNAME rdataset.
   4862  1.1  christos 	 */
   4863  1.1  christos 	result = dns_rbt_findnode(search.rbtdb->tree, name, foundname, &node,
   4864  1.1  christos 				  &search.chain, DNS_RBTFIND_EMPTYDATA,
   4865  1.1  christos 				  cache_zonecut_callback, &search);
   4866  1.1  christos 
   4867  1.1  christos 	if (result == DNS_R_PARTIALMATCH) {
   4868  1.1  christos 		if ((search.options & DNS_DBFIND_COVERINGNSEC) != 0) {
   4869  1.1  christos 			result = find_coveringnsec(&search, nodep, now,
   4870  1.1  christos 						   foundname, rdataset,
   4871  1.1  christos 						   sigrdataset);
   4872  1.7  christos 			if (result == DNS_R_COVERINGNSEC) {
   4873  1.1  christos 				goto tree_exit;
   4874  1.7  christos 			}
   4875  1.1  christos 		}
   4876  1.1  christos 		if (search.zonecut != NULL) {
   4877  1.7  christos 			result = setup_delegation(&search, nodep, foundname,
   4878  1.7  christos 						  rdataset, sigrdataset);
   4879  1.7  christos 			goto tree_exit;
   4880  1.1  christos 		} else {
   4881  1.1  christos 		find_ns:
   4882  1.1  christos 			result = find_deepest_zonecut(&search, node, nodep,
   4883  1.1  christos 						      foundname, rdataset,
   4884  1.1  christos 						      sigrdataset);
   4885  1.1  christos 			goto tree_exit;
   4886  1.1  christos 		}
   4887  1.7  christos 	} else if (result != ISC_R_SUCCESS) {
   4888  1.1  christos 		goto tree_exit;
   4889  1.7  christos 	}
   4890  1.1  christos 
   4891  1.1  christos 	/*
   4892  1.1  christos 	 * Certain DNSSEC types are not subject to CNAME matching
   4893  1.1  christos 	 * (RFC4035, section 2.5 and RFC3007).
   4894  1.1  christos 	 *
   4895  1.1  christos 	 * We don't check for RRSIG, because we don't store RRSIG records
   4896  1.1  christos 	 * directly.
   4897  1.1  christos 	 */
   4898  1.7  christos 	if (type == dns_rdatatype_key || type == dns_rdatatype_nsec) {
   4899  1.3  christos 		cname_ok = false;
   4900  1.7  christos 	}
   4901  1.1  christos 
   4902  1.1  christos 	/*
   4903  1.1  christos 	 * We now go looking for rdata...
   4904  1.1  christos 	 */
   4905  1.1  christos 
   4906  1.1  christos 	lock = &(search.rbtdb->node_locks[node->locknum].lock);
   4907  1.1  christos 	locktype = isc_rwlocktype_read;
   4908  1.1  christos 	NODE_LOCK(lock, locktype);
   4909  1.1  christos 
   4910  1.1  christos 	found = NULL;
   4911  1.1  christos 	foundsig = NULL;
   4912  1.1  christos 	sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
   4913  1.1  christos 	negtype = RBTDB_RDATATYPE_VALUE(0, type);
   4914  1.1  christos 	nsheader = NULL;
   4915  1.1  christos 	nsecheader = NULL;
   4916  1.1  christos 	nssig = NULL;
   4917  1.1  christos 	nsecsig = NULL;
   4918  1.1  christos 	cnamesig = NULL;
   4919  1.3  christos 	empty_node = true;
   4920  1.1  christos 	header_prev = NULL;
   4921  1.1  christos 	for (header = node->data; header != NULL; header = header_next) {
   4922  1.1  christos 		header_next = header->next;
   4923  1.7  christos 		if (check_stale_header(node, header, &locktype, lock, &search,
   4924  1.1  christos 				       &header_prev)) {
   4925  1.1  christos 			/* Do nothing. */
   4926  1.1  christos 		} else if (EXISTS(header) && !ANCIENT(header)) {
   4927  1.1  christos 			/*
   4928  1.1  christos 			 * We now know that there is at least one active
   4929  1.1  christos 			 * non-stale rdataset at this node.
   4930  1.1  christos 			 */
   4931  1.3  christos 			empty_node = false;
   4932  1.1  christos 
   4933  1.1  christos 			/*
   4934  1.1  christos 			 * If we found a type we were looking for, remember
   4935  1.1  christos 			 * it.
   4936  1.1  christos 			 */
   4937  1.1  christos 			if (header->type == type ||
   4938  1.1  christos 			    (type == dns_rdatatype_any &&
   4939  1.1  christos 			     RBTDB_RDATATYPE_BASE(header->type) != 0) ||
   4940  1.7  christos 			    (cname_ok && header->type == dns_rdatatype_cname))
   4941  1.7  christos 			{
   4942  1.1  christos 				/*
   4943  1.1  christos 				 * We've found the answer.
   4944  1.1  christos 				 */
   4945  1.1  christos 				found = header;
   4946  1.1  christos 				if (header->type == dns_rdatatype_cname &&
   4947  1.7  christos 				    cname_ok && cnamesig != NULL) {
   4948  1.1  christos 					/*
   4949  1.1  christos 					 * If we've already got the
   4950  1.1  christos 					 * CNAME RRSIG, use it.
   4951  1.1  christos 					 */
   4952  1.1  christos 					foundsig = cnamesig;
   4953  1.1  christos 				}
   4954  1.1  christos 			} else if (header->type == sigtype) {
   4955  1.1  christos 				/*
   4956  1.1  christos 				 * We've found the RRSIG rdataset for our
   4957  1.1  christos 				 * target type.  Remember it.
   4958  1.1  christos 				 */
   4959  1.1  christos 				foundsig = header;
   4960  1.1  christos 			} else if (header->type == RBTDB_RDATATYPE_NCACHEANY ||
   4961  1.1  christos 				   header->type == negtype) {
   4962  1.1  christos 				/*
   4963  1.1  christos 				 * We've found a negative cache entry.
   4964  1.1  christos 				 */
   4965  1.1  christos 				found = header;
   4966  1.1  christos 			} else if (header->type == dns_rdatatype_ns) {
   4967  1.1  christos 				/*
   4968  1.1  christos 				 * Remember a NS rdataset even if we're
   4969  1.1  christos 				 * not specifically looking for it, because
   4970  1.1  christos 				 * we might need it later.
   4971  1.1  christos 				 */
   4972  1.1  christos 				nsheader = header;
   4973  1.1  christos 			} else if (header->type == RBTDB_RDATATYPE_SIGNS) {
   4974  1.1  christos 				/*
   4975  1.1  christos 				 * If we need the NS rdataset, we'll also
   4976  1.1  christos 				 * need its signature.
   4977  1.1  christos 				 */
   4978  1.1  christos 				nssig = header;
   4979  1.1  christos 			} else if (header->type == dns_rdatatype_nsec) {
   4980  1.1  christos 				nsecheader = header;
   4981  1.1  christos 			} else if (header->type == RBTDB_RDATATYPE_SIGNSEC) {
   4982  1.1  christos 				nsecsig = header;
   4983  1.1  christos 			} else if (cname_ok &&
   4984  1.1  christos 				   header->type == RBTDB_RDATATYPE_SIGCNAME) {
   4985  1.1  christos 				/*
   4986  1.1  christos 				 * If we get a CNAME match, we'll also need
   4987  1.1  christos 				 * its signature.
   4988  1.1  christos 				 */
   4989  1.1  christos 				cnamesig = header;
   4990  1.1  christos 			}
   4991  1.1  christos 			header_prev = header;
   4992  1.7  christos 		} else {
   4993  1.1  christos 			header_prev = header;
   4994  1.7  christos 		}
   4995  1.1  christos 	}
   4996  1.1  christos 
   4997  1.1  christos 	if (empty_node) {
   4998  1.1  christos 		/*
   4999  1.1  christos 		 * We have an exact match for the name, but there are no
   5000  1.1  christos 		 * extant rdatasets.  That means that this node doesn't
   5001  1.1  christos 		 * meaningfully exist, and that we really have a partial match.
   5002  1.1  christos 		 */
   5003  1.1  christos 		NODE_UNLOCK(lock, locktype);
   5004  1.1  christos 		goto find_ns;
   5005  1.1  christos 	}
   5006  1.1  christos 
   5007  1.1  christos 	/*
   5008  1.1  christos 	 * If we didn't find what we were looking for...
   5009  1.1  christos 	 */
   5010  1.1  christos 	if (found == NULL ||
   5011  1.1  christos 	    (DNS_TRUST_ADDITIONAL(found->trust) &&
   5012  1.1  christos 	     ((options & DNS_DBFIND_ADDITIONALOK) == 0)) ||
   5013  1.1  christos 	    (found->trust == dns_trust_glue &&
   5014  1.1  christos 	     ((options & DNS_DBFIND_GLUEOK) == 0)) ||
   5015  1.1  christos 	    (DNS_TRUST_PENDING(found->trust) &&
   5016  1.7  christos 	     ((options & DNS_DBFIND_PENDINGOK) == 0)))
   5017  1.7  christos 	{
   5018  1.1  christos 		/*
   5019  1.1  christos 		 * Return covering NODATA NSEC record.
   5020  1.1  christos 		 */
   5021  1.1  christos 		if ((search.options & DNS_DBFIND_COVERINGNSEC) != 0 &&
   5022  1.7  christos 		    nsecheader != NULL) {
   5023  1.1  christos 			if (nodep != NULL) {
   5024  1.1  christos 				new_reference(search.rbtdb, node);
   5025  1.1  christos 				INSIST(!ISC_LINK_LINKED(node, deadlink));
   5026  1.1  christos 				*nodep = node;
   5027  1.1  christos 			}
   5028  1.1  christos 			bind_rdataset(search.rbtdb, node, nsecheader,
   5029  1.1  christos 				      search.now, rdataset);
   5030  1.7  christos 			if (need_headerupdate(nsecheader, search.now)) {
   5031  1.1  christos 				update = nsecheader;
   5032  1.7  christos 			}
   5033  1.1  christos 			if (nsecsig != NULL) {
   5034  1.1  christos 				bind_rdataset(search.rbtdb, node, nsecsig,
   5035  1.1  christos 					      search.now, sigrdataset);
   5036  1.7  christos 				if (need_headerupdate(nsecsig, search.now)) {
   5037  1.1  christos 					updatesig = nsecsig;
   5038  1.7  christos 				}
   5039  1.1  christos 			}
   5040  1.1  christos 			result = DNS_R_COVERINGNSEC;
   5041  1.1  christos 			goto node_exit;
   5042  1.1  christos 		}
   5043  1.1  christos 
   5044  1.1  christos 		/*
   5045  1.1  christos 		 * If there is an NS rdataset at this node, then this is the
   5046  1.1  christos 		 * deepest zone cut.
   5047  1.1  christos 		 */
   5048  1.1  christos 		if (nsheader != NULL) {
   5049  1.1  christos 			if (nodep != NULL) {
   5050  1.1  christos 				new_reference(search.rbtdb, node);
   5051  1.1  christos 				INSIST(!ISC_LINK_LINKED(node, deadlink));
   5052  1.1  christos 				*nodep = node;
   5053  1.1  christos 			}
   5054  1.1  christos 			bind_rdataset(search.rbtdb, node, nsheader, search.now,
   5055  1.1  christos 				      rdataset);
   5056  1.7  christos 			if (need_headerupdate(nsheader, search.now)) {
   5057  1.1  christos 				update = nsheader;
   5058  1.7  christos 			}
   5059  1.1  christos 			if (nssig != NULL) {
   5060  1.1  christos 				bind_rdataset(search.rbtdb, node, nssig,
   5061  1.1  christos 					      search.now, sigrdataset);
   5062  1.7  christos 				if (need_headerupdate(nssig, search.now)) {
   5063  1.1  christos 					updatesig = nssig;
   5064  1.7  christos 				}
   5065  1.1  christos 			}
   5066  1.1  christos 			result = DNS_R_DELEGATION;
   5067  1.1  christos 			goto node_exit;
   5068  1.1  christos 		}
   5069  1.1  christos 
   5070  1.1  christos 		/*
   5071  1.1  christos 		 * Go find the deepest zone cut.
   5072  1.1  christos 		 */
   5073  1.1  christos 		NODE_UNLOCK(lock, locktype);
   5074  1.1  christos 		goto find_ns;
   5075  1.1  christos 	}
   5076  1.1  christos 
   5077  1.1  christos 	/*
   5078  1.1  christos 	 * We found what we were looking for, or we found a CNAME.
   5079  1.1  christos 	 */
   5080  1.1  christos 
   5081  1.1  christos 	if (nodep != NULL) {
   5082  1.1  christos 		new_reference(search.rbtdb, node);
   5083  1.1  christos 		INSIST(!ISC_LINK_LINKED(node, deadlink));
   5084  1.1  christos 		*nodep = node;
   5085  1.1  christos 	}
   5086  1.1  christos 
   5087  1.1  christos 	if (NEGATIVE(found)) {
   5088  1.1  christos 		/*
   5089  1.1  christos 		 * We found a negative cache entry.
   5090  1.1  christos 		 */
   5091  1.7  christos 		if (NXDOMAIN(found)) {
   5092  1.1  christos 			result = DNS_R_NCACHENXDOMAIN;
   5093  1.7  christos 		} else {
   5094  1.1  christos 			result = DNS_R_NCACHENXRRSET;
   5095  1.7  christos 		}
   5096  1.7  christos 	} else if (type != found->type && type != dns_rdatatype_any &&
   5097  1.7  christos 		   found->type == dns_rdatatype_cname)
   5098  1.7  christos 	{
   5099  1.1  christos 		/*
   5100  1.1  christos 		 * We weren't doing an ANY query and we found a CNAME instead
   5101  1.1  christos 		 * of the type we were looking for, so we need to indicate
   5102  1.1  christos 		 * that result to the caller.
   5103  1.1  christos 		 */
   5104  1.1  christos 		result = DNS_R_CNAME;
   5105  1.1  christos 	} else {
   5106  1.1  christos 		/*
   5107  1.1  christos 		 * An ordinary successful query!
   5108  1.1  christos 		 */
   5109  1.1  christos 		result = ISC_R_SUCCESS;
   5110  1.1  christos 	}
   5111  1.1  christos 
   5112  1.1  christos 	if (type != dns_rdatatype_any || result == DNS_R_NCACHENXDOMAIN ||
   5113  1.7  christos 	    result == DNS_R_NCACHENXRRSET)
   5114  1.7  christos 	{
   5115  1.7  christos 		bind_rdataset(search.rbtdb, node, found, search.now, rdataset);
   5116  1.7  christos 		if (need_headerupdate(found, search.now)) {
   5117  1.1  christos 			update = found;
   5118  1.7  christos 		}
   5119  1.1  christos 		if (!NEGATIVE(found) && foundsig != NULL) {
   5120  1.1  christos 			bind_rdataset(search.rbtdb, node, foundsig, search.now,
   5121  1.1  christos 				      sigrdataset);
   5122  1.7  christos 			if (need_headerupdate(foundsig, search.now)) {
   5123  1.1  christos 				updatesig = foundsig;
   5124  1.7  christos 			}
   5125  1.1  christos 		}
   5126  1.1  christos 	}
   5127  1.1  christos 
   5128  1.7  christos node_exit:
   5129  1.1  christos 	if ((update != NULL || updatesig != NULL) &&
   5130  1.1  christos 	    locktype != isc_rwlocktype_write) {
   5131  1.1  christos 		NODE_UNLOCK(lock, locktype);
   5132  1.1  christos 		NODE_LOCK(lock, isc_rwlocktype_write);
   5133  1.1  christos 		locktype = isc_rwlocktype_write;
   5134  1.1  christos 		POST(locktype);
   5135  1.1  christos 	}
   5136  1.7  christos 	if (update != NULL && need_headerupdate(update, search.now)) {
   5137  1.1  christos 		update_header(search.rbtdb, update, search.now);
   5138  1.7  christos 	}
   5139  1.7  christos 	if (updatesig != NULL && need_headerupdate(updatesig, search.now)) {
   5140  1.1  christos 		update_header(search.rbtdb, updatesig, search.now);
   5141  1.7  christos 	}
   5142  1.1  christos 
   5143  1.1  christos 	NODE_UNLOCK(lock, locktype);
   5144  1.1  christos 
   5145  1.7  christos tree_exit:
   5146  1.1  christos 	RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
   5147  1.1  christos 
   5148  1.1  christos 	/*
   5149  1.1  christos 	 * If we found a zonecut but aren't going to use it, we have to
   5150  1.1  christos 	 * let go of it.
   5151  1.1  christos 	 */
   5152  1.1  christos 	if (search.need_cleanup) {
   5153  1.1  christos 		node = search.zonecut;
   5154  1.1  christos 		INSIST(node != NULL);
   5155  1.1  christos 		lock = &(search.rbtdb->node_locks[node->locknum].lock);
   5156  1.1  christos 
   5157  1.1  christos 		NODE_LOCK(lock, isc_rwlocktype_read);
   5158  1.7  christos 		decrement_reference(search.rbtdb, node, 0, isc_rwlocktype_read,
   5159  1.7  christos 				    isc_rwlocktype_none, false);
   5160  1.1  christos 		NODE_UNLOCK(lock, isc_rwlocktype_read);
   5161  1.1  christos 	}
   5162  1.1  christos 
   5163  1.1  christos 	dns_rbtnodechain_reset(&search.chain);
   5164  1.1  christos 
   5165  1.1  christos 	update_cachestats(search.rbtdb, result);
   5166  1.1  christos 	return (result);
   5167  1.1  christos }
   5168  1.1  christos 
   5169  1.1  christos static isc_result_t
   5170  1.1  christos cache_findzonecut(dns_db_t *db, const dns_name_t *name, unsigned int options,
   5171  1.1  christos 		  isc_stdtime_t now, dns_dbnode_t **nodep,
   5172  1.3  christos 		  dns_name_t *foundname, dns_name_t *dcname,
   5173  1.7  christos 		  dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset) {
   5174  1.1  christos 	dns_rbtnode_t *node = NULL;
   5175  1.1  christos 	nodelock_t *lock;
   5176  1.1  christos 	isc_result_t result;
   5177  1.1  christos 	rbtdb_search_t search;
   5178  1.1  christos 	rdatasetheader_t *header, *header_prev, *header_next;
   5179  1.1  christos 	rdatasetheader_t *found, *foundsig;
   5180  1.1  christos 	unsigned int rbtoptions = DNS_RBTFIND_EMPTYDATA;
   5181  1.1  christos 	isc_rwlocktype_t locktype;
   5182  1.3  christos 	bool dcnull = (dcname == NULL);
   5183  1.1  christos 
   5184  1.1  christos 	search.rbtdb = (dns_rbtdb_t *)db;
   5185  1.1  christos 
   5186  1.1  christos 	REQUIRE(VALID_RBTDB(search.rbtdb));
   5187  1.1  christos 
   5188  1.7  christos 	if (now == 0) {
   5189  1.1  christos 		isc_stdtime_get(&now);
   5190  1.7  christos 	}
   5191  1.1  christos 
   5192  1.1  christos 	search.rbtversion = NULL;
   5193  1.1  christos 	search.serial = 1;
   5194  1.1  christos 	search.options = options;
   5195  1.3  christos 	search.copy_name = false;
   5196  1.3  christos 	search.need_cleanup = false;
   5197  1.3  christos 	search.wild = false;
   5198  1.1  christos 	search.zonecut = NULL;
   5199  1.1  christos 	dns_fixedname_init(&search.zonecut_name);
   5200  1.7  christos 	dns_rbtnodechain_init(&search.chain);
   5201  1.1  christos 	search.now = now;
   5202  1.1  christos 
   5203  1.3  christos 	if (dcnull) {
   5204  1.3  christos 		dcname = foundname;
   5205  1.3  christos 	}
   5206  1.3  christos 
   5207  1.7  christos 	if ((options & DNS_DBFIND_NOEXACT) != 0) {
   5208  1.1  christos 		rbtoptions |= DNS_RBTFIND_NOEXACT;
   5209  1.7  christos 	}
   5210  1.1  christos 
   5211  1.1  christos 	RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
   5212  1.1  christos 
   5213  1.1  christos 	/*
   5214  1.1  christos 	 * Search down from the root of the tree.
   5215  1.1  christos 	 */
   5216  1.3  christos 	result = dns_rbt_findnode(search.rbtdb->tree, name, dcname, &node,
   5217  1.1  christos 				  &search.chain, rbtoptions, NULL, &search);
   5218  1.1  christos 
   5219  1.1  christos 	if (result == DNS_R_PARTIALMATCH) {
   5220  1.1  christos 		result = find_deepest_zonecut(&search, node, nodep, foundname,
   5221  1.1  christos 					      rdataset, sigrdataset);
   5222  1.1  christos 		goto tree_exit;
   5223  1.3  christos 	} else if (result != ISC_R_SUCCESS) {
   5224  1.1  christos 		goto tree_exit;
   5225  1.3  christos 	} else if (!dcnull) {
   5226  1.6  christos 		dns_name_copynf(dcname, foundname);
   5227  1.3  christos 	}
   5228  1.1  christos 	/*
   5229  1.1  christos 	 * We now go looking for an NS rdataset at the node.
   5230  1.1  christos 	 */
   5231  1.1  christos 
   5232  1.1  christos 	lock = &(search.rbtdb->node_locks[node->locknum].lock);
   5233  1.1  christos 	locktype = isc_rwlocktype_read;
   5234  1.1  christos 	NODE_LOCK(lock, locktype);
   5235  1.1  christos 
   5236  1.1  christos 	found = NULL;
   5237  1.1  christos 	foundsig = NULL;
   5238  1.1  christos 	header_prev = NULL;
   5239  1.1  christos 	for (header = node->data; header != NULL; header = header_next) {
   5240  1.1  christos 		header_next = header->next;
   5241  1.7  christos 		if (check_stale_header(node, header, &locktype, lock, &search,
   5242  1.1  christos 				       &header_prev)) {
   5243  1.1  christos 			/* Do nothing. */
   5244  1.1  christos 		} else if (EXISTS(header)) {
   5245  1.1  christos 			/*
   5246  1.1  christos 			 * If we found a type we were looking for, remember
   5247  1.1  christos 			 * it.
   5248  1.1  christos 			 */
   5249  1.1  christos 			if (header->type == dns_rdatatype_ns) {
   5250  1.1  christos 				/*
   5251  1.1  christos 				 * Remember a NS rdataset even if we're
   5252  1.1  christos 				 * not specifically looking for it, because
   5253  1.1  christos 				 * we might need it later.
   5254  1.1  christos 				 */
   5255  1.1  christos 				found = header;
   5256  1.1  christos 			} else if (header->type == RBTDB_RDATATYPE_SIGNS) {
   5257  1.1  christos 				/*
   5258  1.1  christos 				 * If we need the NS rdataset, we'll also
   5259  1.1  christos 				 * need its signature.
   5260  1.1  christos 				 */
   5261  1.1  christos 				foundsig = header;
   5262  1.1  christos 			}
   5263  1.1  christos 			header_prev = header;
   5264  1.7  christos 		} else {
   5265  1.1  christos 			header_prev = header;
   5266  1.7  christos 		}
   5267  1.1  christos 	}
   5268  1.1  christos 
   5269  1.1  christos 	if (found == NULL) {
   5270  1.1  christos 		/*
   5271  1.1  christos 		 * No NS records here.
   5272  1.1  christos 		 */
   5273  1.1  christos 		NODE_UNLOCK(lock, locktype);
   5274  1.3  christos 		result = find_deepest_zonecut(&search, node, nodep, foundname,
   5275  1.3  christos 					      rdataset, sigrdataset);
   5276  1.3  christos 		goto tree_exit;
   5277  1.1  christos 	}
   5278  1.1  christos 
   5279  1.1  christos 	if (nodep != NULL) {
   5280  1.1  christos 		new_reference(search.rbtdb, node);
   5281  1.1  christos 		INSIST(!ISC_LINK_LINKED(node, deadlink));
   5282  1.1  christos 		*nodep = node;
   5283  1.1  christos 	}
   5284  1.1  christos 
   5285  1.1  christos 	bind_rdataset(search.rbtdb, node, found, search.now, rdataset);
   5286  1.7  christos 	if (foundsig != NULL) {
   5287  1.1  christos 		bind_rdataset(search.rbtdb, node, foundsig, search.now,
   5288  1.1  christos 			      sigrdataset);
   5289  1.7  christos 	}
   5290  1.1  christos 
   5291  1.1  christos 	if (need_headerupdate(found, search.now) ||
   5292  1.7  christos 	    (foundsig != NULL && need_headerupdate(foundsig, search.now)))
   5293  1.7  christos 	{
   5294  1.1  christos 		if (locktype != isc_rwlocktype_write) {
   5295  1.1  christos 			NODE_UNLOCK(lock, locktype);
   5296  1.1  christos 			NODE_LOCK(lock, isc_rwlocktype_write);
   5297  1.1  christos 			locktype = isc_rwlocktype_write;
   5298  1.1  christos 			POST(locktype);
   5299  1.1  christos 		}
   5300  1.7  christos 		if (need_headerupdate(found, search.now)) {
   5301  1.1  christos 			update_header(search.rbtdb, found, search.now);
   5302  1.7  christos 		}
   5303  1.7  christos 		if (foundsig != NULL && need_headerupdate(foundsig, search.now))
   5304  1.7  christos 		{
   5305  1.1  christos 			update_header(search.rbtdb, foundsig, search.now);
   5306  1.1  christos 		}
   5307  1.1  christos 	}
   5308  1.1  christos 
   5309  1.1  christos 	NODE_UNLOCK(lock, locktype);
   5310  1.1  christos 
   5311  1.7  christos tree_exit:
   5312  1.1  christos 	RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
   5313  1.1  christos 
   5314  1.1  christos 	INSIST(!search.need_cleanup);
   5315  1.1  christos 
   5316  1.1  christos 	dns_rbtnodechain_reset(&search.chain);
   5317  1.1  christos 
   5318  1.7  christos 	if (result == DNS_R_DELEGATION) {
   5319  1.1  christos 		result = ISC_R_SUCCESS;
   5320  1.7  christos 	}
   5321  1.1  christos 
   5322  1.1  christos 	return (result);
   5323  1.1  christos }
   5324  1.1  christos 
   5325  1.1  christos static void
   5326  1.1  christos attachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) {
   5327  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   5328  1.1  christos 	dns_rbtnode_t *node = (dns_rbtnode_t *)source;
   5329  1.1  christos 
   5330  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   5331  1.1  christos 	REQUIRE(targetp != NULL && *targetp == NULL);
   5332  1.1  christos 
   5333  1.3  christos 	isc_refcount_increment(&node->references);
   5334  1.1  christos 
   5335  1.1  christos 	*targetp = source;
   5336  1.1  christos }
   5337  1.1  christos 
   5338  1.1  christos static void
   5339  1.1  christos detachnode(dns_db_t *db, dns_dbnode_t **targetp) {
   5340  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   5341  1.1  christos 	dns_rbtnode_t *node;
   5342  1.3  christos 	bool want_free = false;
   5343  1.3  christos 	bool inactive = false;
   5344  1.1  christos 	rbtdb_nodelock_t *nodelock;
   5345  1.1  christos 
   5346  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   5347  1.1  christos 	REQUIRE(targetp != NULL && *targetp != NULL);
   5348  1.1  christos 
   5349  1.1  christos 	node = (dns_rbtnode_t *)(*targetp);
   5350  1.1  christos 	nodelock = &rbtdb->node_locks[node->locknum];
   5351  1.1  christos 
   5352  1.1  christos 	NODE_LOCK(&nodelock->lock, isc_rwlocktype_read);
   5353  1.1  christos 
   5354  1.1  christos 	if (decrement_reference(rbtdb, node, 0, isc_rwlocktype_read,
   5355  1.7  christos 				isc_rwlocktype_none, false))
   5356  1.7  christos 	{
   5357  1.1  christos 		if (isc_refcount_current(&nodelock->references) == 0 &&
   5358  1.1  christos 		    nodelock->exiting) {
   5359  1.3  christos 			inactive = true;
   5360  1.1  christos 		}
   5361  1.1  christos 	}
   5362  1.1  christos 
   5363  1.1  christos 	NODE_UNLOCK(&nodelock->lock, isc_rwlocktype_read);
   5364  1.1  christos 
   5365  1.1  christos 	*targetp = NULL;
   5366  1.1  christos 
   5367  1.1  christos 	if (inactive) {
   5368  1.1  christos 		RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
   5369  1.1  christos 		rbtdb->active--;
   5370  1.7  christos 		if (rbtdb->active == 0) {
   5371  1.3  christos 			want_free = true;
   5372  1.7  christos 		}
   5373  1.1  christos 		RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   5374  1.1  christos 		if (want_free) {
   5375  1.1  christos 			char buf[DNS_NAME_FORMATSIZE];
   5376  1.7  christos 			if (dns_name_dynamic(&rbtdb->common.origin)) {
   5377  1.1  christos 				dns_name_format(&rbtdb->common.origin, buf,
   5378  1.1  christos 						sizeof(buf));
   5379  1.7  christos 			} else {
   5380  1.1  christos 				strlcpy(buf, "<UNKNOWN>", sizeof(buf));
   5381  1.7  christos 			}
   5382  1.1  christos 			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   5383  1.1  christos 				      DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
   5384  1.1  christos 				      "calling free_rbtdb(%s)", buf);
   5385  1.3  christos 			free_rbtdb(rbtdb, true, NULL);
   5386  1.1  christos 		}
   5387  1.1  christos 	}
   5388  1.1  christos }
   5389  1.1  christos 
   5390  1.1  christos static isc_result_t
   5391  1.1  christos expirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {
   5392  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   5393  1.1  christos 	dns_rbtnode_t *rbtnode = node;
   5394  1.1  christos 	rdatasetheader_t *header;
   5395  1.3  christos 	bool force_expire = false;
   5396  1.1  christos 	/*
   5397  1.1  christos 	 * These are the category and module used by the cache cleaner.
   5398  1.1  christos 	 */
   5399  1.3  christos 	bool log = false;
   5400  1.1  christos 	isc_logcategory_t *category = DNS_LOGCATEGORY_DATABASE;
   5401  1.1  christos 	isc_logmodule_t *module = DNS_LOGMODULE_CACHE;
   5402  1.1  christos 	int level = ISC_LOG_DEBUG(2);
   5403  1.1  christos 	char printname[DNS_NAME_FORMATSIZE];
   5404  1.1  christos 
   5405  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   5406  1.1  christos 
   5407  1.1  christos 	/*
   5408  1.1  christos 	 * Caller must hold a tree lock.
   5409  1.1  christos 	 */
   5410  1.1  christos 
   5411  1.7  christos 	if (now == 0) {
   5412  1.1  christos 		isc_stdtime_get(&now);
   5413  1.7  christos 	}
   5414  1.1  christos 
   5415  1.1  christos 	if (isc_mem_isovermem(rbtdb->common.mctx)) {
   5416  1.1  christos 		/*
   5417  1.3  christos 		 * Force expire with 25% probability.
   5418  1.1  christos 		 * XXXDCL Could stand to have a better policy, like LRU.
   5419  1.1  christos 		 */
   5420  1.3  christos 		force_expire = (rbtnode->down == NULL &&
   5421  1.3  christos 				(isc_random32() % 4) == 0);
   5422  1.1  christos 
   5423  1.1  christos 		/*
   5424  1.1  christos 		 * Note that 'log' can be true IFF overmem is also true.
   5425  1.1  christos 		 * overmem can currently only be true for cache
   5426  1.1  christos 		 * databases -- hence all of the "overmem cache" log strings.
   5427  1.1  christos 		 */
   5428  1.3  christos 		log = isc_log_wouldlog(dns_lctx, level);
   5429  1.7  christos 		if (log) {
   5430  1.7  christos 			isc_log_write(
   5431  1.7  christos 				dns_lctx, category, module, level,
   5432  1.7  christos 				"overmem cache: %s %s",
   5433  1.7  christos 				force_expire ? "FORCE" : "check",
   5434  1.7  christos 				dns_rbt_formatnodename(rbtnode, printname,
   5435  1.7  christos 						       sizeof(printname)));
   5436  1.7  christos 		}
   5437  1.1  christos 	}
   5438  1.1  christos 
   5439  1.1  christos 	/*
   5440  1.1  christos 	 * We may not need write access, but this code path is not performance
   5441  1.1  christos 	 * sensitive, so it should be okay to always lock as a writer.
   5442  1.1  christos 	 */
   5443  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   5444  1.1  christos 		  isc_rwlocktype_write);
   5445  1.1  christos 
   5446  1.7  christos 	for (header = rbtnode->data; header != NULL; header = header->next) {
   5447  1.1  christos 		if (header->rdh_ttl <= now - RBTDB_VIRTUAL) {
   5448  1.1  christos 			/*
   5449  1.1  christos 			 * We don't check if refcurrent(rbtnode) == 0 and try
   5450  1.1  christos 			 * to free like we do in cache_find(), because
   5451  1.1  christos 			 * refcurrent(rbtnode) must be non-zero.  This is so
   5452  1.1  christos 			 * because 'node' is an argument to the function.
   5453  1.1  christos 			 */
   5454  1.1  christos 			mark_header_ancient(rbtdb, header);
   5455  1.7  christos 			if (log) {
   5456  1.7  christos 				isc_log_write(dns_lctx, category, module, level,
   5457  1.5  christos 					      "overmem cache: ancient %s",
   5458  1.1  christos 					      printname);
   5459  1.7  christos 			}
   5460  1.1  christos 		} else if (force_expire) {
   5461  1.7  christos 			if (!RETAIN(header)) {
   5462  1.1  christos 				set_ttl(rbtdb, header, 0);
   5463  1.1  christos 				mark_header_ancient(rbtdb, header);
   5464  1.1  christos 			} else if (log) {
   5465  1.7  christos 				isc_log_write(dns_lctx, category, module, level,
   5466  1.7  christos 					      "overmem cache: "
   5467  1.1  christos 					      "reprieve by RETAIN() %s",
   5468  1.1  christos 					      printname);
   5469  1.1  christos 			}
   5470  1.7  christos 		} else if (isc_mem_isovermem(rbtdb->common.mctx) && log) {
   5471  1.1  christos 			isc_log_write(dns_lctx, category, module, level,
   5472  1.1  christos 				      "overmem cache: saved %s", printname);
   5473  1.7  christos 		}
   5474  1.7  christos 	}
   5475  1.1  christos 
   5476  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   5477  1.1  christos 		    isc_rwlocktype_write);
   5478  1.1  christos 
   5479  1.1  christos 	return (ISC_R_SUCCESS);
   5480  1.1  christos }
   5481  1.1  christos 
   5482  1.1  christos static void
   5483  1.3  christos overmem(dns_db_t *db, bool over) {
   5484  1.1  christos 	/* This is an empty callback.  See adb.c:water() */
   5485  1.1  christos 
   5486  1.1  christos 	UNUSED(db);
   5487  1.1  christos 	UNUSED(over);
   5488  1.1  christos 
   5489  1.1  christos 	return;
   5490  1.1  christos }
   5491  1.1  christos 
   5492  1.1  christos static void
   5493  1.1  christos printnode(dns_db_t *db, dns_dbnode_t *node, FILE *out) {
   5494  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   5495  1.1  christos 	dns_rbtnode_t *rbtnode = node;
   5496  1.3  christos 	bool first;
   5497  1.3  christos 	uint32_t refs;
   5498  1.1  christos 
   5499  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   5500  1.1  christos 
   5501  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   5502  1.1  christos 		  isc_rwlocktype_read);
   5503  1.1  christos 
   5504  1.3  christos 	refs = isc_refcount_current(&rbtnode->references);
   5505  1.7  christos 	fprintf(out, "node %p, %" PRIu32 " references, locknum = %u\n", rbtnode,
   5506  1.7  christos 		refs, rbtnode->locknum);
   5507  1.1  christos 	if (rbtnode->data != NULL) {
   5508  1.1  christos 		rdatasetheader_t *current, *top_next;
   5509  1.1  christos 
   5510  1.1  christos 		for (current = rbtnode->data; current != NULL;
   5511  1.1  christos 		     current = top_next) {
   5512  1.1  christos 			top_next = current->next;
   5513  1.3  christos 			first = true;
   5514  1.1  christos 			fprintf(out, "\ttype %u", current->type);
   5515  1.1  christos 			do {
   5516  1.7  christos 				if (!first) {
   5517  1.1  christos 					fprintf(out, "\t");
   5518  1.7  christos 				}
   5519  1.3  christos 				first = false;
   5520  1.1  christos 				fprintf(out,
   5521  1.1  christos 					"\tserial = %lu, ttl = %u, "
   5522  1.1  christos 					"trust = %u, attributes = %u, "
   5523  1.1  christos 					"resign = %u\n",
   5524  1.1  christos 					(unsigned long)current->serial,
   5525  1.7  christos 					current->rdh_ttl, current->trust,
   5526  1.1  christos 					current->attributes,
   5527  1.1  christos 					(current->resign << 1) |
   5528  1.7  christos 						current->resign_lsb);
   5529  1.1  christos 				current = current->down;
   5530  1.1  christos 			} while (current != NULL);
   5531  1.1  christos 		}
   5532  1.7  christos 	} else {
   5533  1.1  christos 		fprintf(out, "(empty)\n");
   5534  1.7  christos 	}
   5535  1.1  christos 
   5536  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   5537  1.1  christos 		    isc_rwlocktype_read);
   5538  1.1  christos }
   5539  1.1  christos 
   5540  1.1  christos static isc_result_t
   5541  1.7  christos createiterator(dns_db_t *db, unsigned int options,
   5542  1.7  christos 	       dns_dbiterator_t **iteratorp) {
   5543  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   5544  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter;
   5545  1.1  christos 
   5546  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   5547  1.1  christos 
   5548  1.1  christos 	rbtdbiter = isc_mem_get(rbtdb->common.mctx, sizeof(*rbtdbiter));
   5549  1.1  christos 
   5550  1.1  christos 	rbtdbiter->common.methods = &dbiterator_methods;
   5551  1.1  christos 	rbtdbiter->common.db = NULL;
   5552  1.1  christos 	dns_db_attach(db, &rbtdbiter->common.db);
   5553  1.7  christos 	rbtdbiter->common.relative_names = ((options & DNS_DB_RELATIVENAMES) !=
   5554  1.7  christos 					    0);
   5555  1.1  christos 	rbtdbiter->common.magic = DNS_DBITERATOR_MAGIC;
   5556  1.3  christos 	rbtdbiter->common.cleaning = false;
   5557  1.3  christos 	rbtdbiter->paused = true;
   5558  1.1  christos 	rbtdbiter->tree_locked = isc_rwlocktype_none;
   5559  1.1  christos 	rbtdbiter->result = ISC_R_SUCCESS;
   5560  1.1  christos 	dns_fixedname_init(&rbtdbiter->name);
   5561  1.1  christos 	dns_fixedname_init(&rbtdbiter->origin);
   5562  1.1  christos 	rbtdbiter->node = NULL;
   5563  1.1  christos 	rbtdbiter->delcnt = 0;
   5564  1.3  christos 	rbtdbiter->nsec3only = ((options & DNS_DB_NSEC3ONLY) != 0);
   5565  1.3  christos 	rbtdbiter->nonsec3 = ((options & DNS_DB_NONSEC3) != 0);
   5566  1.1  christos 	memset(rbtdbiter->deletions, 0, sizeof(rbtdbiter->deletions));
   5567  1.7  christos 	dns_rbtnodechain_init(&rbtdbiter->chain);
   5568  1.7  christos 	dns_rbtnodechain_init(&rbtdbiter->nsec3chain);
   5569  1.7  christos 	if (rbtdbiter->nsec3only) {
   5570  1.1  christos 		rbtdbiter->current = &rbtdbiter->nsec3chain;
   5571  1.7  christos 	} else {
   5572  1.1  christos 		rbtdbiter->current = &rbtdbiter->chain;
   5573  1.7  christos 	}
   5574  1.1  christos 
   5575  1.1  christos 	*iteratorp = (dns_dbiterator_t *)rbtdbiter;
   5576  1.1  christos 
   5577  1.1  christos 	return (ISC_R_SUCCESS);
   5578  1.1  christos }
   5579  1.1  christos 
   5580  1.1  christos static isc_result_t
   5581  1.1  christos zone_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
   5582  1.1  christos 		  dns_rdatatype_t type, dns_rdatatype_t covers,
   5583  1.1  christos 		  isc_stdtime_t now, dns_rdataset_t *rdataset,
   5584  1.7  christos 		  dns_rdataset_t *sigrdataset) {
   5585  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   5586  1.1  christos 	dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
   5587  1.1  christos 	rdatasetheader_t *header, *header_next, *found, *foundsig;
   5588  1.1  christos 	rbtdb_serial_t serial;
   5589  1.1  christos 	rbtdb_version_t *rbtversion = version;
   5590  1.3  christos 	bool close_version = false;
   5591  1.1  christos 	rbtdb_rdatatype_t matchtype, sigmatchtype;
   5592  1.1  christos 
   5593  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   5594  1.1  christos 	REQUIRE(type != dns_rdatatype_any);
   5595  1.1  christos 	INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
   5596  1.1  christos 
   5597  1.1  christos 	if (rbtversion == NULL) {
   5598  1.7  christos 		currentversion(db, (dns_dbversion_t **)(void *)(&rbtversion));
   5599  1.3  christos 		close_version = true;
   5600  1.1  christos 	}
   5601  1.1  christos 	serial = rbtversion->serial;
   5602  1.1  christos 	now = 0;
   5603  1.1  christos 
   5604  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   5605  1.1  christos 		  isc_rwlocktype_read);
   5606  1.1  christos 
   5607  1.1  christos 	found = NULL;
   5608  1.1  christos 	foundsig = NULL;
   5609  1.1  christos 	matchtype = RBTDB_RDATATYPE_VALUE(type, covers);
   5610  1.7  christos 	if (covers == 0) {
   5611  1.1  christos 		sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
   5612  1.7  christos 	} else {
   5613  1.1  christos 		sigmatchtype = 0;
   5614  1.7  christos 	}
   5615  1.1  christos 
   5616  1.1  christos 	for (header = rbtnode->data; header != NULL; header = header_next) {
   5617  1.1  christos 		header_next = header->next;
   5618  1.1  christos 		do {
   5619  1.7  christos 			if (header->serial <= serial && !IGNORE(header)) {
   5620  1.1  christos 				/*
   5621  1.1  christos 				 * Is this a "this rdataset doesn't
   5622  1.1  christos 				 * exist" record?
   5623  1.1  christos 				 */
   5624  1.7  christos 				if (NONEXISTENT(header)) {
   5625  1.1  christos 					header = NULL;
   5626  1.7  christos 				}
   5627  1.1  christos 				break;
   5628  1.7  christos 			} else {
   5629  1.1  christos 				header = header->down;
   5630  1.7  christos 			}
   5631  1.1  christos 		} while (header != NULL);
   5632  1.1  christos 		if (header != NULL) {
   5633  1.1  christos 			/*
   5634  1.1  christos 			 * We have an active, extant rdataset.  If it's a
   5635  1.1  christos 			 * type we're looking for, remember it.
   5636  1.1  christos 			 */
   5637  1.1  christos 			if (header->type == matchtype) {
   5638  1.1  christos 				found = header;
   5639  1.7  christos 				if (foundsig != NULL) {
   5640  1.1  christos 					break;
   5641  1.7  christos 				}
   5642  1.1  christos 			} else if (header->type == sigmatchtype) {
   5643  1.1  christos 				foundsig = header;
   5644  1.7  christos 				if (found != NULL) {
   5645  1.1  christos 					break;
   5646  1.7  christos 				}
   5647  1.1  christos 			}
   5648  1.1  christos 		}
   5649  1.1  christos 	}
   5650  1.1  christos 	if (found != NULL) {
   5651  1.1  christos 		bind_rdataset(rbtdb, rbtnode, found, now, rdataset);
   5652  1.7  christos 		if (foundsig != NULL) {
   5653  1.1  christos 			bind_rdataset(rbtdb, rbtnode, foundsig, now,
   5654  1.1  christos 				      sigrdataset);
   5655  1.7  christos 		}
   5656  1.1  christos 	}
   5657  1.1  christos 
   5658  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   5659  1.1  christos 		    isc_rwlocktype_read);
   5660  1.1  christos 
   5661  1.7  christos 	if (close_version) {
   5662  1.7  christos 		closeversion(db, (dns_dbversion_t **)(void *)(&rbtversion),
   5663  1.3  christos 			     false);
   5664  1.7  christos 	}
   5665  1.1  christos 
   5666  1.7  christos 	if (found == NULL) {
   5667  1.1  christos 		return (ISC_R_NOTFOUND);
   5668  1.7  christos 	}
   5669  1.1  christos 
   5670  1.1  christos 	return (ISC_R_SUCCESS);
   5671  1.1  christos }
   5672  1.1  christos 
   5673  1.1  christos static isc_result_t
   5674  1.1  christos cache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
   5675  1.1  christos 		   dns_rdatatype_t type, dns_rdatatype_t covers,
   5676  1.1  christos 		   isc_stdtime_t now, dns_rdataset_t *rdataset,
   5677  1.7  christos 		   dns_rdataset_t *sigrdataset) {
   5678  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   5679  1.1  christos 	dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
   5680  1.1  christos 	rdatasetheader_t *header, *header_next, *found, *foundsig;
   5681  1.1  christos 	rbtdb_rdatatype_t matchtype, sigmatchtype, negtype;
   5682  1.1  christos 	isc_result_t result;
   5683  1.1  christos 	nodelock_t *lock;
   5684  1.1  christos 	isc_rwlocktype_t locktype;
   5685  1.1  christos 
   5686  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   5687  1.1  christos 	REQUIRE(type != dns_rdatatype_any);
   5688  1.1  christos 
   5689  1.1  christos 	UNUSED(version);
   5690  1.1  christos 
   5691  1.1  christos 	result = ISC_R_SUCCESS;
   5692  1.1  christos 
   5693  1.7  christos 	if (now == 0) {
   5694  1.1  christos 		isc_stdtime_get(&now);
   5695  1.7  christos 	}
   5696  1.1  christos 
   5697  1.1  christos 	lock = &rbtdb->node_locks[rbtnode->locknum].lock;
   5698  1.1  christos 	locktype = isc_rwlocktype_read;
   5699  1.1  christos 	NODE_LOCK(lock, locktype);
   5700  1.1  christos 
   5701  1.1  christos 	found = NULL;
   5702  1.1  christos 	foundsig = NULL;
   5703  1.1  christos 	matchtype = RBTDB_RDATATYPE_VALUE(type, covers);
   5704  1.1  christos 	negtype = RBTDB_RDATATYPE_VALUE(0, type);
   5705  1.7  christos 	if (covers == 0) {
   5706  1.1  christos 		sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
   5707  1.7  christos 	} else {
   5708  1.1  christos 		sigmatchtype = 0;
   5709  1.7  christos 	}
   5710  1.1  christos 
   5711  1.1  christos 	for (header = rbtnode->data; header != NULL; header = header_next) {
   5712  1.1  christos 		header_next = header->next;
   5713  1.1  christos 		if (!ACTIVE(header, now)) {
   5714  1.1  christos 			if ((header->rdh_ttl < now - RBTDB_VIRTUAL) &&
   5715  1.1  christos 			    (locktype == isc_rwlocktype_write ||
   5716  1.7  christos 			     NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS))
   5717  1.7  christos 			{
   5718  1.1  christos 				/*
   5719  1.1  christos 				 * We update the node's status only when we
   5720  1.1  christos 				 * can get write access.
   5721  1.1  christos 				 */
   5722  1.1  christos 				locktype = isc_rwlocktype_write;
   5723  1.1  christos 
   5724  1.1  christos 				/*
   5725  1.1  christos 				 * We don't check if refcurrent(rbtnode) == 0
   5726  1.1  christos 				 * and try to free like we do in cache_find(),
   5727  1.1  christos 				 * because refcurrent(rbtnode) must be
   5728  1.1  christos 				 * non-zero.  This is so because 'node' is an
   5729  1.1  christos 				 * argument to the function.
   5730  1.1  christos 				 */
   5731  1.1  christos 				mark_header_ancient(rbtdb, header);
   5732  1.1  christos 			}
   5733  1.1  christos 		} else if (EXISTS(header) && !ANCIENT(header)) {
   5734  1.7  christos 			if (header->type == matchtype) {
   5735  1.1  christos 				found = header;
   5736  1.7  christos 			} else if (header->type == RBTDB_RDATATYPE_NCACHEANY ||
   5737  1.7  christos 				   header->type == negtype) {
   5738  1.1  christos 				found = header;
   5739  1.7  christos 			} else if (header->type == sigmatchtype) {
   5740  1.1  christos 				foundsig = header;
   5741  1.7  christos 			}
   5742  1.1  christos 		}
   5743  1.1  christos 	}
   5744  1.1  christos 	if (found != NULL) {
   5745  1.1  christos 		bind_rdataset(rbtdb, rbtnode, found, now, rdataset);
   5746  1.7  christos 		if (!NEGATIVE(found) && foundsig != NULL) {
   5747  1.1  christos 			bind_rdataset(rbtdb, rbtnode, foundsig, now,
   5748  1.1  christos 				      sigrdataset);
   5749  1.7  christos 		}
   5750  1.1  christos 	}
   5751  1.1  christos 
   5752  1.1  christos 	NODE_UNLOCK(lock, locktype);
   5753  1.1  christos 
   5754  1.7  christos 	if (found == NULL) {
   5755  1.1  christos 		return (ISC_R_NOTFOUND);
   5756  1.7  christos 	}
   5757  1.1  christos 
   5758  1.1  christos 	if (NEGATIVE(found)) {
   5759  1.1  christos 		/*
   5760  1.1  christos 		 * We found a negative cache entry.
   5761  1.1  christos 		 */
   5762  1.7  christos 		if (NXDOMAIN(found)) {
   5763  1.1  christos 			result = DNS_R_NCACHENXDOMAIN;
   5764  1.7  christos 		} else {
   5765  1.1  christos 			result = DNS_R_NCACHENXRRSET;
   5766  1.7  christos 		}
   5767  1.1  christos 	}
   5768  1.1  christos 
   5769  1.1  christos 	update_cachestats(rbtdb, result);
   5770  1.1  christos 
   5771  1.1  christos 	return (result);
   5772  1.1  christos }
   5773  1.1  christos 
   5774  1.1  christos static isc_result_t
   5775  1.1  christos allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
   5776  1.7  christos 	     isc_stdtime_t now, dns_rdatasetiter_t **iteratorp) {
   5777  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   5778  1.1  christos 	dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
   5779  1.1  christos 	rbtdb_version_t *rbtversion = version;
   5780  1.1  christos 	rbtdb_rdatasetiter_t *iterator;
   5781  1.1  christos 
   5782  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   5783  1.1  christos 
   5784  1.1  christos 	iterator = isc_mem_get(rbtdb->common.mctx, sizeof(*iterator));
   5785  1.1  christos 
   5786  1.1  christos 	if ((db->attributes & DNS_DBATTR_CACHE) == 0) {
   5787  1.1  christos 		now = 0;
   5788  1.7  christos 		if (rbtversion == NULL) {
   5789  1.7  christos 			currentversion(
   5790  1.7  christos 				db, (dns_dbversion_t **)(void *)(&rbtversion));
   5791  1.7  christos 		} else {
   5792  1.1  christos 			INSIST(rbtversion->rbtdb == rbtdb);
   5793  1.1  christos 
   5794  1.3  christos 			(void)isc_refcount_increment(&rbtversion->references);
   5795  1.1  christos 		}
   5796  1.1  christos 	} else {
   5797  1.7  christos 		if (now == 0) {
   5798  1.1  christos 			isc_stdtime_get(&now);
   5799  1.7  christos 		}
   5800  1.1  christos 		rbtversion = NULL;
   5801  1.1  christos 	}
   5802  1.1  christos 
   5803  1.1  christos 	iterator->common.magic = DNS_RDATASETITER_MAGIC;
   5804  1.1  christos 	iterator->common.methods = &rdatasetiter_methods;
   5805  1.1  christos 	iterator->common.db = db;
   5806  1.1  christos 	iterator->common.node = node;
   5807  1.1  christos 	iterator->common.version = (dns_dbversion_t *)rbtversion;
   5808  1.1  christos 	iterator->common.now = now;
   5809  1.1  christos 
   5810  1.3  christos 	isc_refcount_increment(&rbtnode->references);
   5811  1.1  christos 
   5812  1.1  christos 	iterator->current = NULL;
   5813  1.1  christos 
   5814  1.1  christos 	*iteratorp = (dns_rdatasetiter_t *)iterator;
   5815  1.1  christos 
   5816  1.1  christos 	return (ISC_R_SUCCESS);
   5817  1.1  christos }
   5818  1.1  christos 
   5819  1.3  christos static bool
   5820  1.1  christos cname_and_other_data(dns_rbtnode_t *node, rbtdb_serial_t serial) {
   5821  1.1  christos 	rdatasetheader_t *header, *header_next;
   5822  1.3  christos 	bool cname, other_data;
   5823  1.1  christos 	dns_rdatatype_t rdtype;
   5824  1.1  christos 
   5825  1.1  christos 	/*
   5826  1.1  christos 	 * The caller must hold the node lock.
   5827  1.1  christos 	 */
   5828  1.1  christos 
   5829  1.1  christos 	/*
   5830  1.1  christos 	 * Look for CNAME and "other data" rdatasets active in our version.
   5831  1.1  christos 	 */
   5832  1.3  christos 	cname = false;
   5833  1.3  christos 	other_data = false;
   5834  1.1  christos 	for (header = node->data; header != NULL; header = header_next) {
   5835  1.1  christos 		header_next = header->next;
   5836  1.1  christos 		if (header->type == dns_rdatatype_cname) {
   5837  1.1  christos 			/*
   5838  1.1  christos 			 * Look for an active extant CNAME.
   5839  1.1  christos 			 */
   5840  1.1  christos 			do {
   5841  1.7  christos 				if (header->serial <= serial && !IGNORE(header))
   5842  1.7  christos 				{
   5843  1.1  christos 					/*
   5844  1.1  christos 					 * Is this a "this rdataset doesn't
   5845  1.1  christos 					 * exist" record?
   5846  1.1  christos 					 */
   5847  1.7  christos 					if (NONEXISTENT(header)) {
   5848  1.1  christos 						header = NULL;
   5849  1.7  christos 					}
   5850  1.1  christos 					break;
   5851  1.7  christos 				} else {
   5852  1.1  christos 					header = header->down;
   5853  1.7  christos 				}
   5854  1.1  christos 			} while (header != NULL);
   5855  1.7  christos 			if (header != NULL) {
   5856  1.3  christos 				cname = true;
   5857  1.7  christos 			}
   5858  1.1  christos 		} else {
   5859  1.1  christos 			/*
   5860  1.1  christos 			 * Look for active extant "other data".
   5861  1.1  christos 			 *
   5862  1.1  christos 			 * "Other data" is any rdataset whose type is not
   5863  1.1  christos 			 * KEY, NSEC, SIG or RRSIG.
   5864  1.1  christos 			 */
   5865  1.1  christos 			rdtype = RBTDB_RDATATYPE_BASE(header->type);
   5866  1.1  christos 			if (rdtype != dns_rdatatype_key &&
   5867  1.1  christos 			    rdtype != dns_rdatatype_sig &&
   5868  1.1  christos 			    rdtype != dns_rdatatype_nsec &&
   5869  1.7  christos 			    rdtype != dns_rdatatype_rrsig)
   5870  1.7  christos 			{
   5871  1.1  christos 				/*
   5872  1.1  christos 				 * Is it active and extant?
   5873  1.1  christos 				 */
   5874  1.1  christos 				do {
   5875  1.1  christos 					if (header->serial <= serial &&
   5876  1.1  christos 					    !IGNORE(header)) {
   5877  1.1  christos 						/*
   5878  1.1  christos 						 * Is this a "this rdataset
   5879  1.1  christos 						 * doesn't exist" record?
   5880  1.1  christos 						 */
   5881  1.7  christos 						if (NONEXISTENT(header)) {
   5882  1.1  christos 							header = NULL;
   5883  1.7  christos 						}
   5884  1.1  christos 						break;
   5885  1.7  christos 					} else {
   5886  1.1  christos 						header = header->down;
   5887  1.7  christos 					}
   5888  1.1  christos 				} while (header != NULL);
   5889  1.7  christos 				if (header != NULL) {
   5890  1.3  christos 					other_data = true;
   5891  1.7  christos 				}
   5892  1.1  christos 			}
   5893  1.1  christos 		}
   5894  1.1  christos 	}
   5895  1.1  christos 
   5896  1.7  christos 	if (cname && other_data) {
   5897  1.3  christos 		return (true);
   5898  1.7  christos 	}
   5899  1.1  christos 
   5900  1.3  christos 	return (false);
   5901  1.1  christos }
   5902  1.1  christos 
   5903  1.1  christos static isc_result_t
   5904  1.1  christos resign_insert(dns_rbtdb_t *rbtdb, int idx, rdatasetheader_t *newheader) {
   5905  1.1  christos 	isc_result_t result;
   5906  1.1  christos 
   5907  1.1  christos 	INSIST(!IS_CACHE(rbtdb));
   5908  1.1  christos 	INSIST(newheader->heap_index == 0);
   5909  1.1  christos 	INSIST(!ISC_LINK_LINKED(newheader, link));
   5910  1.1  christos 
   5911  1.1  christos 	result = isc_heap_insert(rbtdb->heaps[idx], newheader);
   5912  1.1  christos 	return (result);
   5913  1.1  christos }
   5914  1.1  christos 
   5915  1.1  christos static void
   5916  1.1  christos resign_delete(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
   5917  1.7  christos 	      rdatasetheader_t *header) {
   5918  1.1  christos 	/*
   5919  1.1  christos 	 * Remove the old header from the heap
   5920  1.1  christos 	 */
   5921  1.1  christos 	if (header != NULL && header->heap_index != 0) {
   5922  1.1  christos 		isc_heap_delete(rbtdb->heaps[header->node->locknum],
   5923  1.1  christos 				header->heap_index);
   5924  1.1  christos 		header->heap_index = 0;
   5925  1.1  christos 		if (version != NULL) {
   5926  1.1  christos 			new_reference(rbtdb, header->node);
   5927  1.1  christos 			ISC_LIST_APPEND(version->resigned_list, header, link);
   5928  1.1  christos 		}
   5929  1.1  christos 	}
   5930  1.1  christos }
   5931  1.1  christos 
   5932  1.7  christos static inline uint64_t
   5933  1.7  christos recordsize(rdatasetheader_t *header, unsigned int namelen) {
   5934  1.7  christos 	return (dns_rdataslab_rdatasize((unsigned char *)header,
   5935  1.7  christos 					sizeof(*header)) +
   5936  1.7  christos 		sizeof(dns_ttl_t) + sizeof(dns_rdatatype_t) +
   5937  1.7  christos 		sizeof(dns_rdataclass_t) + namelen);
   5938  1.7  christos }
   5939  1.7  christos 
   5940  1.1  christos static void
   5941  1.3  christos update_recordsandbytes(bool add, rbtdb_version_t *rbtversion,
   5942  1.7  christos 		       rdatasetheader_t *header, unsigned int namelen) {
   5943  1.1  christos 	unsigned char *hdr = (unsigned char *)header;
   5944  1.7  christos 	size_t hdrsize = sizeof(*header);
   5945  1.1  christos 
   5946  1.7  christos 	RWLOCK(&rbtversion->rwlock, isc_rwlocktype_write);
   5947  1.1  christos 	if (add) {
   5948  1.1  christos 		rbtversion->records += dns_rdataslab_count(hdr, hdrsize);
   5949  1.7  christos 		rbtversion->bytes += recordsize(header, namelen);
   5950  1.1  christos 	} else {
   5951  1.1  christos 		rbtversion->records -= dns_rdataslab_count(hdr, hdrsize);
   5952  1.7  christos 		rbtversion->bytes -= recordsize(header, namelen);
   5953  1.1  christos 	}
   5954  1.7  christos 	RWUNLOCK(&rbtversion->rwlock, isc_rwlocktype_write);
   5955  1.1  christos }
   5956  1.1  christos 
   5957  1.1  christos static isc_result_t
   5958  1.1  christos add32(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
   5959  1.3  christos       rdatasetheader_t *newheader, unsigned int options, bool loading,
   5960  1.7  christos       dns_rdataset_t *addedrdataset, isc_stdtime_t now) {
   5961  1.1  christos 	rbtdb_changed_t *changed = NULL;
   5962  1.1  christos 	rdatasetheader_t *topheader, *topheader_prev, *header, *sigheader;
   5963  1.7  christos 	dns_fixedname_t fname;
   5964  1.7  christos 	dns_name_t *nodename = dns_fixedname_initname(&fname);
   5965  1.1  christos 	unsigned char *merged;
   5966  1.1  christos 	isc_result_t result;
   5967  1.3  christos 	bool header_nx;
   5968  1.3  christos 	bool newheader_nx;
   5969  1.3  christos 	bool merge;
   5970  1.1  christos 	dns_rdatatype_t rdtype, covers;
   5971  1.1  christos 	rbtdb_rdatatype_t negtype, sigtype;
   5972  1.1  christos 	dns_trust_t trust;
   5973  1.1  christos 	int idx;
   5974  1.1  christos 
   5975  1.1  christos 	/*
   5976  1.1  christos 	 * Add an rdatasetheader_t to a node.
   5977  1.1  christos 	 */
   5978  1.1  christos 
   5979  1.1  christos 	/*
   5980  1.1  christos 	 * Caller must be holding the node lock.
   5981  1.1  christos 	 */
   5982  1.1  christos 
   5983  1.7  christos 	dns_rbt_fullnamefromnode(rbtnode, nodename);
   5984  1.7  christos 
   5985  1.1  christos 	if ((options & DNS_DBADD_MERGE) != 0) {
   5986  1.1  christos 		REQUIRE(rbtversion != NULL);
   5987  1.3  christos 		merge = true;
   5988  1.7  christos 	} else {
   5989  1.3  christos 		merge = false;
   5990  1.7  christos 	}
   5991  1.1  christos 
   5992  1.7  christos 	if ((options & DNS_DBADD_FORCE) != 0) {
   5993  1.1  christos 		trust = dns_trust_ultimate;
   5994  1.7  christos 	} else {
   5995  1.1  christos 		trust = newheader->trust;
   5996  1.7  christos 	}
   5997  1.1  christos 
   5998  1.1  christos 	if (rbtversion != NULL && !loading) {
   5999  1.1  christos 		/*
   6000  1.1  christos 		 * We always add a changed record, even if no changes end up
   6001  1.1  christos 		 * being made to this node, because it's harmless and
   6002  1.1  christos 		 * simplifies the code.
   6003  1.1  christos 		 */
   6004  1.1  christos 		changed = add_changed(rbtdb, rbtversion, rbtnode);
   6005  1.1  christos 		if (changed == NULL) {
   6006  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6007  1.1  christos 			return (ISC_R_NOMEMORY);
   6008  1.1  christos 		}
   6009  1.1  christos 	}
   6010  1.1  christos 
   6011  1.3  christos 	newheader_nx = NONEXISTENT(newheader) ? true : false;
   6012  1.1  christos 	topheader_prev = NULL;
   6013  1.1  christos 	sigheader = NULL;
   6014  1.1  christos 	negtype = 0;
   6015  1.1  christos 	if (rbtversion == NULL && !newheader_nx) {
   6016  1.1  christos 		rdtype = RBTDB_RDATATYPE_BASE(newheader->type);
   6017  1.1  christos 		covers = RBTDB_RDATATYPE_EXT(newheader->type);
   6018  1.1  christos 		sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, covers);
   6019  1.1  christos 		if (NEGATIVE(newheader)) {
   6020  1.1  christos 			/*
   6021  1.1  christos 			 * We're adding a negative cache entry.
   6022  1.1  christos 			 */
   6023  1.1  christos 			if (covers == dns_rdatatype_any) {
   6024  1.1  christos 				/*
   6025  1.1  christos 				 * If we're adding an negative cache entry
   6026  1.1  christos 				 * which covers all types (NXDOMAIN,
   6027  1.1  christos 				 * NODATA(QTYPE=ANY)),
   6028  1.1  christos 				 *
   6029  1.5  christos 				 * We make all other data ancient so that the
   6030  1.1  christos 				 * only rdataset that can be found at this
   6031  1.1  christos 				 * node is the negative cache entry.
   6032  1.1  christos 				 */
   6033  1.1  christos 				for (topheader = rbtnode->data;
   6034  1.1  christos 				     topheader != NULL;
   6035  1.7  christos 				     topheader = topheader->next) {
   6036  1.1  christos 					set_ttl(rbtdb, topheader, 0);
   6037  1.1  christos 					mark_header_ancient(rbtdb, topheader);
   6038  1.1  christos 				}
   6039  1.1  christos 				goto find_header;
   6040  1.1  christos 			}
   6041  1.1  christos 			/*
   6042  1.1  christos 			 * Otherwise look for any RRSIGs of the given
   6043  1.5  christos 			 * type so they can be marked ancient later.
   6044  1.1  christos 			 */
   6045  1.7  christos 			for (topheader = rbtnode->data; topheader != NULL;
   6046  1.7  christos 			     topheader = topheader->next) {
   6047  1.7  christos 				if (topheader->type == sigtype) {
   6048  1.1  christos 					sigheader = topheader;
   6049  1.7  christos 				}
   6050  1.7  christos 			}
   6051  1.1  christos 			negtype = RBTDB_RDATATYPE_VALUE(covers, 0);
   6052  1.1  christos 		} else {
   6053  1.1  christos 			/*
   6054  1.1  christos 			 * We're adding something that isn't a
   6055  1.1  christos 			 * negative cache entry.  Look for an extant
   6056  1.5  christos 			 * non-ancient NXDOMAIN/NODATA(QTYPE=ANY) negative
   6057  1.1  christos 			 * cache entry.  If we're adding an RRSIG, also
   6058  1.5  christos 			 * check for an extant non-ancient NODATA ncache
   6059  1.1  christos 			 * entry which covers the same type as the RRSIG.
   6060  1.1  christos 			 */
   6061  1.7  christos 			for (topheader = rbtnode->data; topheader != NULL;
   6062  1.1  christos 			     topheader = topheader->next) {
   6063  1.1  christos 				if ((topheader->type ==
   6064  1.7  christos 				     RBTDB_RDATATYPE_NCACHEANY) ||
   6065  1.7  christos 				    (newheader->type == sigtype &&
   6066  1.7  christos 				     topheader->type ==
   6067  1.7  christos 					     RBTDB_RDATATYPE_VALUE(0, covers)))
   6068  1.7  christos 				{
   6069  1.7  christos 					break;
   6070  1.7  christos 				}
   6071  1.1  christos 			}
   6072  1.1  christos 			if (topheader != NULL && EXISTS(topheader) &&
   6073  1.1  christos 			    ACTIVE(topheader, now)) {
   6074  1.1  christos 				/*
   6075  1.1  christos 				 * Found one.
   6076  1.1  christos 				 */
   6077  1.1  christos 				if (trust < topheader->trust) {
   6078  1.1  christos 					/*
   6079  1.1  christos 					 * The NXDOMAIN/NODATA(QTYPE=ANY)
   6080  1.1  christos 					 * is more trusted.
   6081  1.1  christos 					 */
   6082  1.7  christos 					free_rdataset(rbtdb, rbtdb->common.mctx,
   6083  1.1  christos 						      newheader);
   6084  1.7  christos 					if (addedrdataset != NULL) {
   6085  1.1  christos 						bind_rdataset(rbtdb, rbtnode,
   6086  1.1  christos 							      topheader, now,
   6087  1.1  christos 							      addedrdataset);
   6088  1.7  christos 					}
   6089  1.1  christos 					return (DNS_R_UNCHANGED);
   6090  1.1  christos 				}
   6091  1.1  christos 				/*
   6092  1.1  christos 				 * The new rdataset is better.  Expire the
   6093  1.1  christos 				 * ncache entry.
   6094  1.1  christos 				 */
   6095  1.1  christos 				set_ttl(rbtdb, topheader, 0);
   6096  1.1  christos 				mark_header_ancient(rbtdb, topheader);
   6097  1.1  christos 				topheader = NULL;
   6098  1.1  christos 				goto find_header;
   6099  1.1  christos 			}
   6100  1.1  christos 			negtype = RBTDB_RDATATYPE_VALUE(0, rdtype);
   6101  1.1  christos 		}
   6102  1.1  christos 	}
   6103  1.1  christos 
   6104  1.7  christos 	for (topheader = rbtnode->data; topheader != NULL;
   6105  1.1  christos 	     topheader = topheader->next) {
   6106  1.1  christos 		if (topheader->type == newheader->type ||
   6107  1.7  christos 		    topheader->type == negtype) {
   6108  1.1  christos 			break;
   6109  1.7  christos 		}
   6110  1.1  christos 		topheader_prev = topheader;
   6111  1.1  christos 	}
   6112  1.1  christos 
   6113  1.7  christos find_header:
   6114  1.1  christos 	/*
   6115  1.1  christos 	 * If header isn't NULL, we've found the right type.  There may be
   6116  1.1  christos 	 * IGNORE rdatasets between the top of the chain and the first real
   6117  1.1  christos 	 * data.  We skip over them.
   6118  1.1  christos 	 */
   6119  1.1  christos 	header = topheader;
   6120  1.7  christos 	while (header != NULL && IGNORE(header)) {
   6121  1.1  christos 		header = header->down;
   6122  1.7  christos 	}
   6123  1.1  christos 	if (header != NULL) {
   6124  1.3  christos 		header_nx = NONEXISTENT(header) ? true : false;
   6125  1.1  christos 
   6126  1.1  christos 		/*
   6127  1.1  christos 		 * Deleting an already non-existent rdataset has no effect.
   6128  1.1  christos 		 */
   6129  1.1  christos 		if (header_nx && newheader_nx) {
   6130  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6131  1.1  christos 			return (DNS_R_UNCHANGED);
   6132  1.1  christos 		}
   6133  1.1  christos 
   6134  1.1  christos 		/*
   6135  1.1  christos 		 * Trying to add an rdataset with lower trust to a cache
   6136  1.1  christos 		 * DB has no effect, provided that the cache data isn't
   6137  1.1  christos 		 * stale. If the cache data is stale, new lower trust
   6138  1.1  christos 		 * data will supersede it below. Unclear what the best
   6139  1.1  christos 		 * policy is here.
   6140  1.1  christos 		 */
   6141  1.1  christos 		if (rbtversion == NULL && trust < header->trust &&
   6142  1.7  christos 		    (ACTIVE(header, now) || header_nx))
   6143  1.7  christos 		{
   6144  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6145  1.7  christos 			if (addedrdataset != NULL) {
   6146  1.1  christos 				bind_rdataset(rbtdb, rbtnode, header, now,
   6147  1.1  christos 					      addedrdataset);
   6148  1.7  christos 			}
   6149  1.1  christos 			return (DNS_R_UNCHANGED);
   6150  1.1  christos 		}
   6151  1.1  christos 
   6152  1.1  christos 		/*
   6153  1.1  christos 		 * Don't merge if a nonexistent rdataset is involved.
   6154  1.1  christos 		 */
   6155  1.7  christos 		if (merge && (header_nx || newheader_nx)) {
   6156  1.3  christos 			merge = false;
   6157  1.7  christos 		}
   6158  1.1  christos 
   6159  1.1  christos 		/*
   6160  1.3  christos 		 * If 'merge' is true, we'll try to create a new rdataset
   6161  1.1  christos 		 * that is the union of 'newheader' and 'header'.
   6162  1.1  christos 		 */
   6163  1.1  christos 		if (merge) {
   6164  1.1  christos 			unsigned int flags = 0;
   6165  1.1  christos 			INSIST(rbtversion->serial >= header->serial);
   6166  1.1  christos 			merged = NULL;
   6167  1.1  christos 			result = ISC_R_SUCCESS;
   6168  1.1  christos 
   6169  1.7  christos 			if ((options & DNS_DBADD_EXACT) != 0) {
   6170  1.1  christos 				flags |= DNS_RDATASLAB_EXACT;
   6171  1.7  christos 			}
   6172  1.1  christos 			/*
   6173  1.1  christos 			 * TTL use here is irrelevant to the cache;
   6174  1.1  christos 			 * merge is only done with zonedbs.
   6175  1.1  christos 			 */
   6176  1.1  christos 			if ((options & DNS_DBADD_EXACTTTL) != 0 &&
   6177  1.7  christos 			    newheader->rdh_ttl != header->rdh_ttl)
   6178  1.7  christos 			{
   6179  1.7  christos 				result = DNS_R_NOTEXACT;
   6180  1.7  christos 			} else if (newheader->rdh_ttl != header->rdh_ttl) {
   6181  1.1  christos 				flags |= DNS_RDATASLAB_FORCE;
   6182  1.7  christos 			}
   6183  1.7  christos 			if (result == ISC_R_SUCCESS) {
   6184  1.1  christos 				result = dns_rdataslab_merge(
   6185  1.7  christos 					(unsigned char *)header,
   6186  1.7  christos 					(unsigned char *)newheader,
   6187  1.7  christos 					(unsigned int)(sizeof(*newheader)),
   6188  1.7  christos 					rbtdb->common.mctx,
   6189  1.7  christos 					rbtdb->common.rdclass,
   6190  1.7  christos 					(dns_rdatatype_t)header->type, flags,
   6191  1.7  christos 					&merged);
   6192  1.7  christos 			}
   6193  1.1  christos 			if (result == ISC_R_SUCCESS) {
   6194  1.1  christos 				/*
   6195  1.1  christos 				 * If 'header' has the same serial number as
   6196  1.1  christos 				 * we do, we could clean it up now if we knew
   6197  1.1  christos 				 * that our caller had no references to it.
   6198  1.1  christos 				 * We don't know this, however, so we leave it
   6199  1.1  christos 				 * alone.  It will get cleaned up when
   6200  1.1  christos 				 * clean_zone_node() runs.
   6201  1.1  christos 				 */
   6202  1.1  christos 				free_rdataset(rbtdb, rbtdb->common.mctx,
   6203  1.1  christos 					      newheader);
   6204  1.1  christos 				newheader = (rdatasetheader_t *)merged;
   6205  1.1  christos 				init_rdataset(rbtdb, newheader);
   6206  1.1  christos 				update_newheader(newheader, header);
   6207  1.1  christos 				if (loading && RESIGN(newheader) &&
   6208  1.1  christos 				    RESIGN(header) &&
   6209  1.1  christos 				    resign_sooner(header, newheader))
   6210  1.1  christos 				{
   6211  1.1  christos 					newheader->resign = header->resign;
   6212  1.1  christos 					newheader->resign_lsb =
   6213  1.7  christos 						header->resign_lsb;
   6214  1.1  christos 				}
   6215  1.1  christos 			} else {
   6216  1.1  christos 				free_rdataset(rbtdb, rbtdb->common.mctx,
   6217  1.1  christos 					      newheader);
   6218  1.1  christos 				return (result);
   6219  1.1  christos 			}
   6220  1.1  christos 		}
   6221  1.1  christos 		/*
   6222  1.1  christos 		 * Don't replace existing NS, A and AAAA RRsets in the
   6223  1.1  christos 		 * cache if they are already exist. This prevents named
   6224  1.1  christos 		 * being locked to old servers. Don't lower trust of
   6225  1.1  christos 		 * existing record if the update is forced. Nothing
   6226  1.1  christos 		 * special to be done w.r.t stale data; it gets replaced
   6227  1.1  christos 		 * normally further down.
   6228  1.1  christos 		 */
   6229  1.1  christos 		if (IS_CACHE(rbtdb) && ACTIVE(header, now) &&
   6230  1.7  christos 		    header->type == dns_rdatatype_ns && !header_nx &&
   6231  1.7  christos 		    !newheader_nx && header->trust >= newheader->trust &&
   6232  1.1  christos 		    dns_rdataslab_equalx((unsigned char *)header,
   6233  1.1  christos 					 (unsigned char *)newheader,
   6234  1.1  christos 					 (unsigned int)(sizeof(*newheader)),
   6235  1.1  christos 					 rbtdb->common.rdclass,
   6236  1.7  christos 					 (dns_rdatatype_t)header->type))
   6237  1.7  christos 		{
   6238  1.1  christos 			/*
   6239  1.1  christos 			 * Honour the new ttl if it is less than the
   6240  1.1  christos 			 * older one.
   6241  1.1  christos 			 */
   6242  1.7  christos 			if (header->rdh_ttl > newheader->rdh_ttl) {
   6243  1.1  christos 				set_ttl(rbtdb, header, newheader->rdh_ttl);
   6244  1.7  christos 			}
   6245  1.1  christos 			if (header->noqname == NULL &&
   6246  1.1  christos 			    newheader->noqname != NULL) {
   6247  1.1  christos 				header->noqname = newheader->noqname;
   6248  1.1  christos 				newheader->noqname = NULL;
   6249  1.1  christos 			}
   6250  1.1  christos 			if (header->closest == NULL &&
   6251  1.1  christos 			    newheader->closest != NULL) {
   6252  1.1  christos 				header->closest = newheader->closest;
   6253  1.1  christos 				newheader->closest = NULL;
   6254  1.1  christos 			}
   6255  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6256  1.7  christos 			if (addedrdataset != NULL) {
   6257  1.1  christos 				bind_rdataset(rbtdb, rbtnode, header, now,
   6258  1.1  christos 					      addedrdataset);
   6259  1.7  christos 			}
   6260  1.1  christos 			return (ISC_R_SUCCESS);
   6261  1.1  christos 		}
   6262  1.1  christos 		/*
   6263  1.1  christos 		 * If we have will be replacing a NS RRset force its TTL
   6264  1.1  christos 		 * to be no more than the current NS RRset's TTL.  This
   6265  1.1  christos 		 * ensures the delegations that are withdrawn are honoured.
   6266  1.1  christos 		 */
   6267  1.1  christos 		if (IS_CACHE(rbtdb) && ACTIVE(header, now) &&
   6268  1.7  christos 		    header->type == dns_rdatatype_ns && !header_nx &&
   6269  1.7  christos 		    !newheader_nx && header->trust <= newheader->trust)
   6270  1.7  christos 		{
   6271  1.1  christos 			if (newheader->rdh_ttl > header->rdh_ttl) {
   6272  1.1  christos 				newheader->rdh_ttl = header->rdh_ttl;
   6273  1.1  christos 			}
   6274  1.1  christos 		}
   6275  1.1  christos 		if (IS_CACHE(rbtdb) && ACTIVE(header, now) &&
   6276  1.1  christos 		    (options & DNS_DBADD_PREFETCH) == 0 &&
   6277  1.1  christos 		    (header->type == dns_rdatatype_a ||
   6278  1.1  christos 		     header->type == dns_rdatatype_aaaa ||
   6279  1.1  christos 		     header->type == dns_rdatatype_ds ||
   6280  1.7  christos 		     header->type == RBTDB_RDATATYPE_SIGDS) &&
   6281  1.1  christos 		    !header_nx && !newheader_nx &&
   6282  1.1  christos 		    header->trust >= newheader->trust &&
   6283  1.1  christos 		    dns_rdataslab_equal((unsigned char *)header,
   6284  1.1  christos 					(unsigned char *)newheader,
   6285  1.7  christos 					(unsigned int)(sizeof(*newheader))))
   6286  1.7  christos 		{
   6287  1.1  christos 			/*
   6288  1.1  christos 			 * Honour the new ttl if it is less than the
   6289  1.1  christos 			 * older one.
   6290  1.1  christos 			 */
   6291  1.7  christos 			if (header->rdh_ttl > newheader->rdh_ttl) {
   6292  1.1  christos 				set_ttl(rbtdb, header, newheader->rdh_ttl);
   6293  1.7  christos 			}
   6294  1.1  christos 			if (header->noqname == NULL &&
   6295  1.1  christos 			    newheader->noqname != NULL) {
   6296  1.1  christos 				header->noqname = newheader->noqname;
   6297  1.1  christos 				newheader->noqname = NULL;
   6298  1.1  christos 			}
   6299  1.1  christos 			if (header->closest == NULL &&
   6300  1.1  christos 			    newheader->closest != NULL) {
   6301  1.1  christos 				header->closest = newheader->closest;
   6302  1.1  christos 				newheader->closest = NULL;
   6303  1.1  christos 			}
   6304  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6305  1.7  christos 			if (addedrdataset != NULL) {
   6306  1.1  christos 				bind_rdataset(rbtdb, rbtnode, header, now,
   6307  1.1  christos 					      addedrdataset);
   6308  1.7  christos 			}
   6309  1.1  christos 			return (ISC_R_SUCCESS);
   6310  1.1  christos 		}
   6311  1.1  christos 		INSIST(rbtversion == NULL ||
   6312  1.1  christos 		       rbtversion->serial >= topheader->serial);
   6313  1.1  christos 		if (loading) {
   6314  1.1  christos 			newheader->down = NULL;
   6315  1.1  christos 			idx = newheader->node->locknum;
   6316  1.1  christos 			if (IS_CACHE(rbtdb)) {
   6317  1.7  christos 				if (ZEROTTL(newheader)) {
   6318  1.1  christos 					ISC_LIST_APPEND(rbtdb->rdatasets[idx],
   6319  1.1  christos 							newheader, link);
   6320  1.7  christos 				} else {
   6321  1.1  christos 					ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
   6322  1.1  christos 							 newheader, link);
   6323  1.7  christos 				}
   6324  1.1  christos 				INSIST(rbtdb->heaps != NULL);
   6325  1.1  christos 				result = isc_heap_insert(rbtdb->heaps[idx],
   6326  1.1  christos 							 newheader);
   6327  1.1  christos 				if (result != ISC_R_SUCCESS) {
   6328  1.7  christos 					free_rdataset(rbtdb, rbtdb->common.mctx,
   6329  1.1  christos 						      newheader);
   6330  1.1  christos 					return (result);
   6331  1.1  christos 				}
   6332  1.1  christos 			} else if (RESIGN(newheader)) {
   6333  1.1  christos 				result = resign_insert(rbtdb, idx, newheader);
   6334  1.1  christos 				if (result != ISC_R_SUCCESS) {
   6335  1.7  christos 					free_rdataset(rbtdb, rbtdb->common.mctx,
   6336  1.1  christos 						      newheader);
   6337  1.1  christos 					return (result);
   6338  1.1  christos 				}
   6339  1.1  christos 				/*
   6340  1.1  christos 				 * Don't call resign_delete as we don't need
   6341  1.1  christos 				 * to reverse the delete.  The free_rdataset
   6342  1.1  christos 				 * call below will clean up the heap entry.
   6343  1.1  christos 				 */
   6344  1.1  christos 			}
   6345  1.1  christos 
   6346  1.1  christos 			/*
   6347  1.1  christos 			 * There are no other references to 'header' when
   6348  1.1  christos 			 * loading, so we MAY clean up 'header' now.
   6349  1.1  christos 			 * Since we don't generate changed records when
   6350  1.1  christos 			 * loading, we MUST clean up 'header' now.
   6351  1.1  christos 			 */
   6352  1.7  christos 			if (topheader_prev != NULL) {
   6353  1.1  christos 				topheader_prev->next = newheader;
   6354  1.7  christos 			} else {
   6355  1.1  christos 				rbtnode->data = newheader;
   6356  1.7  christos 			}
   6357  1.1  christos 			newheader->next = topheader->next;
   6358  1.1  christos 			if (rbtversion != NULL && !header_nx) {
   6359  1.3  christos 				update_recordsandbytes(false, rbtversion,
   6360  1.7  christos 						       header,
   6361  1.7  christos 						       nodename->length);
   6362  1.1  christos 			}
   6363  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, header);
   6364  1.1  christos 		} else {
   6365  1.1  christos 			idx = newheader->node->locknum;
   6366  1.1  christos 			if (IS_CACHE(rbtdb)) {
   6367  1.1  christos 				INSIST(rbtdb->heaps != NULL);
   6368  1.1  christos 				result = isc_heap_insert(rbtdb->heaps[idx],
   6369  1.1  christos 							 newheader);
   6370  1.1  christos 				if (result != ISC_R_SUCCESS) {
   6371  1.7  christos 					free_rdataset(rbtdb, rbtdb->common.mctx,
   6372  1.1  christos 						      newheader);
   6373  1.1  christos 					return (result);
   6374  1.1  christos 				}
   6375  1.7  christos 				if (ZEROTTL(newheader)) {
   6376  1.1  christos 					ISC_LIST_APPEND(rbtdb->rdatasets[idx],
   6377  1.1  christos 							newheader, link);
   6378  1.7  christos 				} else {
   6379  1.1  christos 					ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
   6380  1.1  christos 							 newheader, link);
   6381  1.7  christos 				}
   6382  1.1  christos 			} else if (RESIGN(newheader)) {
   6383  1.1  christos 				result = resign_insert(rbtdb, idx, newheader);
   6384  1.1  christos 				if (result != ISC_R_SUCCESS) {
   6385  1.7  christos 					free_rdataset(rbtdb, rbtdb->common.mctx,
   6386  1.1  christos 						      newheader);
   6387  1.1  christos 					return (result);
   6388  1.1  christos 				}
   6389  1.1  christos 				resign_delete(rbtdb, rbtversion, header);
   6390  1.1  christos 			}
   6391  1.7  christos 			if (topheader_prev != NULL) {
   6392  1.1  christos 				topheader_prev->next = newheader;
   6393  1.7  christos 			} else {
   6394  1.1  christos 				rbtnode->data = newheader;
   6395  1.7  christos 			}
   6396  1.1  christos 			newheader->next = topheader->next;
   6397  1.1  christos 			newheader->down = topheader;
   6398  1.1  christos 			topheader->next = newheader;
   6399  1.1  christos 			rbtnode->dirty = 1;
   6400  1.7  christos 			if (changed != NULL) {
   6401  1.3  christos 				changed->dirty = true;
   6402  1.7  christos 			}
   6403  1.1  christos 			if (rbtversion == NULL) {
   6404  1.1  christos 				set_ttl(rbtdb, header, 0);
   6405  1.1  christos 				mark_header_ancient(rbtdb, header);
   6406  1.1  christos 				if (sigheader != NULL) {
   6407  1.1  christos 					set_ttl(rbtdb, sigheader, 0);
   6408  1.1  christos 					mark_header_ancient(rbtdb, sigheader);
   6409  1.1  christos 				}
   6410  1.1  christos 			}
   6411  1.1  christos 			if (rbtversion != NULL && !header_nx) {
   6412  1.3  christos 				update_recordsandbytes(false, rbtversion,
   6413  1.7  christos 						       header,
   6414  1.7  christos 						       nodename->length);
   6415  1.1  christos 			}
   6416  1.1  christos 		}
   6417  1.1  christos 	} else {
   6418  1.1  christos 		/*
   6419  1.1  christos 		 * No non-IGNORED rdatasets of the given type exist at
   6420  1.1  christos 		 * this node.
   6421  1.1  christos 		 */
   6422  1.1  christos 
   6423  1.1  christos 		/*
   6424  1.1  christos 		 * If we're trying to delete the type, don't bother.
   6425  1.1  christos 		 */
   6426  1.1  christos 		if (newheader_nx) {
   6427  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6428  1.1  christos 			return (DNS_R_UNCHANGED);
   6429  1.1  christos 		}
   6430  1.1  christos 
   6431  1.1  christos 		idx = newheader->node->locknum;
   6432  1.1  christos 		if (IS_CACHE(rbtdb)) {
   6433  1.1  christos 			result = isc_heap_insert(rbtdb->heaps[idx], newheader);
   6434  1.1  christos 			if (result != ISC_R_SUCCESS) {
   6435  1.1  christos 				free_rdataset(rbtdb, rbtdb->common.mctx,
   6436  1.1  christos 					      newheader);
   6437  1.1  christos 				return (result);
   6438  1.1  christos 			}
   6439  1.7  christos 			if (ZEROTTL(newheader)) {
   6440  1.1  christos 				ISC_LIST_APPEND(rbtdb->rdatasets[idx],
   6441  1.1  christos 						newheader, link);
   6442  1.7  christos 			} else {
   6443  1.1  christos 				ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
   6444  1.1  christos 						 newheader, link);
   6445  1.7  christos 			}
   6446  1.1  christos 		} else if (RESIGN(newheader)) {
   6447  1.1  christos 			result = resign_insert(rbtdb, idx, newheader);
   6448  1.1  christos 			if (result != ISC_R_SUCCESS) {
   6449  1.1  christos 				free_rdataset(rbtdb, rbtdb->common.mctx,
   6450  1.1  christos 					      newheader);
   6451  1.1  christos 				return (result);
   6452  1.1  christos 			}
   6453  1.1  christos 			resign_delete(rbtdb, rbtversion, header);
   6454  1.1  christos 		}
   6455  1.1  christos 
   6456  1.1  christos 		if (topheader != NULL) {
   6457  1.1  christos 			/*
   6458  1.1  christos 			 * We have an list of rdatasets of the given type,
   6459  1.1  christos 			 * but they're all marked IGNORE.  We simply insert
   6460  1.1  christos 			 * the new rdataset at the head of the list.
   6461  1.1  christos 			 *
   6462  1.1  christos 			 * Ignored rdatasets cannot occur during loading, so
   6463  1.1  christos 			 * we INSIST on it.
   6464  1.1  christos 			 */
   6465  1.1  christos 			INSIST(!loading);
   6466  1.1  christos 			INSIST(rbtversion == NULL ||
   6467  1.1  christos 			       rbtversion->serial >= topheader->serial);
   6468  1.7  christos 			if (topheader_prev != NULL) {
   6469  1.1  christos 				topheader_prev->next = newheader;
   6470  1.7  christos 			} else {
   6471  1.1  christos 				rbtnode->data = newheader;
   6472  1.7  christos 			}
   6473  1.1  christos 			newheader->next = topheader->next;
   6474  1.1  christos 			newheader->down = topheader;
   6475  1.1  christos 			topheader->next = newheader;
   6476  1.1  christos 			rbtnode->dirty = 1;
   6477  1.7  christos 			if (changed != NULL) {
   6478  1.3  christos 				changed->dirty = true;
   6479  1.7  christos 			}
   6480  1.1  christos 		} else {
   6481  1.1  christos 			/*
   6482  1.1  christos 			 * No rdatasets of the given type exist at the node.
   6483  1.1  christos 			 */
   6484  1.1  christos 			newheader->next = rbtnode->data;
   6485  1.1  christos 			newheader->down = NULL;
   6486  1.1  christos 			rbtnode->data = newheader;
   6487  1.1  christos 		}
   6488  1.1  christos 	}
   6489  1.1  christos 
   6490  1.1  christos 	if (rbtversion != NULL && !newheader_nx) {
   6491  1.7  christos 		update_recordsandbytes(true, rbtversion, newheader,
   6492  1.7  christos 				       nodename->length);
   6493  1.1  christos 	}
   6494  1.1  christos 
   6495  1.1  christos 	/*
   6496  1.1  christos 	 * Check if the node now contains CNAME and other data.
   6497  1.1  christos 	 */
   6498  1.1  christos 	if (rbtversion != NULL &&
   6499  1.7  christos 	    cname_and_other_data(rbtnode, rbtversion->serial)) {
   6500  1.1  christos 		return (DNS_R_CNAMEANDOTHER);
   6501  1.7  christos 	}
   6502  1.1  christos 
   6503  1.7  christos 	if (addedrdataset != NULL) {
   6504  1.1  christos 		bind_rdataset(rbtdb, rbtnode, newheader, now, addedrdataset);
   6505  1.7  christos 	}
   6506  1.1  christos 
   6507  1.1  christos 	return (ISC_R_SUCCESS);
   6508  1.1  christos }
   6509  1.1  christos 
   6510  1.3  christos static inline bool
   6511  1.1  christos delegating_type(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
   6512  1.7  christos 		rbtdb_rdatatype_t type) {
   6513  1.1  christos 	if (IS_CACHE(rbtdb)) {
   6514  1.7  christos 		if (type == dns_rdatatype_dname) {
   6515  1.3  christos 			return (true);
   6516  1.7  christos 		} else {
   6517  1.3  christos 			return (false);
   6518  1.7  christos 		}
   6519  1.1  christos 	} else if (type == dns_rdatatype_dname ||
   6520  1.1  christos 		   (type == dns_rdatatype_ns &&
   6521  1.1  christos 		    (node != rbtdb->origin_node || IS_STUB(rbtdb))))
   6522  1.7  christos 	{
   6523  1.3  christos 		return (true);
   6524  1.7  christos 	}
   6525  1.3  christos 	return (false);
   6526  1.1  christos }
   6527  1.1  christos 
   6528  1.1  christos static inline isc_result_t
   6529  1.1  christos addnoqname(dns_rbtdb_t *rbtdb, rdatasetheader_t *newheader,
   6530  1.7  christos 	   dns_rdataset_t *rdataset) {
   6531  1.1  christos 	struct noqname *noqname;
   6532  1.1  christos 	isc_mem_t *mctx = rbtdb->common.mctx;
   6533  1.1  christos 	dns_name_t name;
   6534  1.1  christos 	dns_rdataset_t neg, negsig;
   6535  1.1  christos 	isc_result_t result;
   6536  1.1  christos 	isc_region_t r;
   6537  1.1  christos 
   6538  1.1  christos 	dns_name_init(&name, NULL);
   6539  1.1  christos 	dns_rdataset_init(&neg);
   6540  1.1  christos 	dns_rdataset_init(&negsig);
   6541  1.1  christos 
   6542  1.1  christos 	result = dns_rdataset_getnoqname(rdataset, &name, &neg, &negsig);
   6543  1.1  christos 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
   6544  1.1  christos 
   6545  1.1  christos 	noqname = isc_mem_get(mctx, sizeof(*noqname));
   6546  1.1  christos 	dns_name_init(&noqname->name, NULL);
   6547  1.1  christos 	noqname->neg = NULL;
   6548  1.1  christos 	noqname->negsig = NULL;
   6549  1.1  christos 	noqname->type = neg.type;
   6550  1.7  christos 	dns_name_dup(&name, mctx, &noqname->name);
   6551  1.1  christos 	result = dns_rdataslab_fromrdataset(&neg, mctx, &r, 0);
   6552  1.7  christos 	if (result != ISC_R_SUCCESS) {
   6553  1.1  christos 		goto cleanup;
   6554  1.7  christos 	}
   6555  1.1  christos 	noqname->neg = r.base;
   6556  1.1  christos 	result = dns_rdataslab_fromrdataset(&negsig, mctx, &r, 0);
   6557  1.7  christos 	if (result != ISC_R_SUCCESS) {
   6558  1.1  christos 		goto cleanup;
   6559  1.7  christos 	}
   6560  1.1  christos 	noqname->negsig = r.base;
   6561  1.1  christos 	dns_rdataset_disassociate(&neg);
   6562  1.1  christos 	dns_rdataset_disassociate(&negsig);
   6563  1.1  christos 	newheader->noqname = noqname;
   6564  1.1  christos 	return (ISC_R_SUCCESS);
   6565  1.1  christos 
   6566  1.1  christos cleanup:
   6567  1.1  christos 	dns_rdataset_disassociate(&neg);
   6568  1.1  christos 	dns_rdataset_disassociate(&negsig);
   6569  1.7  christos 	free_noqname(mctx, &noqname);
   6570  1.7  christos 	return (result);
   6571  1.1  christos }
   6572  1.1  christos 
   6573  1.1  christos static inline isc_result_t
   6574  1.1  christos addclosest(dns_rbtdb_t *rbtdb, rdatasetheader_t *newheader,
   6575  1.7  christos 	   dns_rdataset_t *rdataset) {
   6576  1.1  christos 	struct noqname *closest;
   6577  1.1  christos 	isc_mem_t *mctx = rbtdb->common.mctx;
   6578  1.1  christos 	dns_name_t name;
   6579  1.1  christos 	dns_rdataset_t neg, negsig;
   6580  1.1  christos 	isc_result_t result;
   6581  1.1  christos 	isc_region_t r;
   6582  1.1  christos 
   6583  1.1  christos 	dns_name_init(&name, NULL);
   6584  1.1  christos 	dns_rdataset_init(&neg);
   6585  1.1  christos 	dns_rdataset_init(&negsig);
   6586  1.1  christos 
   6587  1.1  christos 	result = dns_rdataset_getclosest(rdataset, &name, &neg, &negsig);
   6588  1.1  christos 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
   6589  1.1  christos 
   6590  1.1  christos 	closest = isc_mem_get(mctx, sizeof(*closest));
   6591  1.1  christos 	dns_name_init(&closest->name, NULL);
   6592  1.1  christos 	closest->neg = NULL;
   6593  1.1  christos 	closest->negsig = NULL;
   6594  1.1  christos 	closest->type = neg.type;
   6595  1.7  christos 	dns_name_dup(&name, mctx, &closest->name);
   6596  1.1  christos 	result = dns_rdataslab_fromrdataset(&neg, mctx, &r, 0);
   6597  1.7  christos 	if (result != ISC_R_SUCCESS) {
   6598  1.1  christos 		goto cleanup;
   6599  1.7  christos 	}
   6600  1.1  christos 	closest->neg = r.base;
   6601  1.1  christos 	result = dns_rdataslab_fromrdataset(&negsig, mctx, &r, 0);
   6602  1.7  christos 	if (result != ISC_R_SUCCESS) {
   6603  1.1  christos 		goto cleanup;
   6604  1.7  christos 	}
   6605  1.1  christos 	closest->negsig = r.base;
   6606  1.1  christos 	dns_rdataset_disassociate(&neg);
   6607  1.1  christos 	dns_rdataset_disassociate(&negsig);
   6608  1.1  christos 	newheader->closest = closest;
   6609  1.1  christos 	return (ISC_R_SUCCESS);
   6610  1.1  christos 
   6611  1.7  christos cleanup:
   6612  1.1  christos 	dns_rdataset_disassociate(&neg);
   6613  1.1  christos 	dns_rdataset_disassociate(&negsig);
   6614  1.7  christos 	free_noqname(mctx, &closest);
   6615  1.7  christos 	return (result);
   6616  1.1  christos }
   6617  1.1  christos 
   6618  1.1  christos static dns_dbmethods_t zone_methods;
   6619  1.1  christos 
   6620  1.1  christos static isc_result_t
   6621  1.1  christos addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
   6622  1.1  christos 	    isc_stdtime_t now, dns_rdataset_t *rdataset, unsigned int options,
   6623  1.7  christos 	    dns_rdataset_t *addedrdataset) {
   6624  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   6625  1.1  christos 	dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
   6626  1.1  christos 	rbtdb_version_t *rbtversion = version;
   6627  1.1  christos 	isc_region_t region;
   6628  1.1  christos 	rdatasetheader_t *newheader;
   6629  1.1  christos 	rdatasetheader_t *header;
   6630  1.1  christos 	isc_result_t result;
   6631  1.3  christos 	bool delegating;
   6632  1.3  christos 	bool newnsec;
   6633  1.3  christos 	bool tree_locked = false;
   6634  1.3  christos 	bool cache_is_overmem = false;
   6635  1.1  christos 	dns_fixedname_t fixed;
   6636  1.1  christos 	dns_name_t *name;
   6637  1.1  christos 
   6638  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   6639  1.1  christos 	INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
   6640  1.1  christos 
   6641  1.7  christos 	if (rbtdb->common.methods == &zone_methods) {
   6642  1.7  christos 		RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   6643  1.1  christos 		REQUIRE(((rbtnode->nsec == DNS_RBT_NSEC_NSEC3 &&
   6644  1.1  christos 			  (rdataset->type == dns_rdatatype_nsec3 ||
   6645  1.1  christos 			   rdataset->covers == dns_rdatatype_nsec3)) ||
   6646  1.1  christos 			 (rbtnode->nsec != DNS_RBT_NSEC_NSEC3 &&
   6647  1.7  christos 			  rdataset->type != dns_rdatatype_nsec3 &&
   6648  1.7  christos 			  rdataset->covers != dns_rdatatype_nsec3)));
   6649  1.7  christos 		RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   6650  1.7  christos 	}
   6651  1.1  christos 
   6652  1.1  christos 	if (rbtversion == NULL) {
   6653  1.7  christos 		if (now == 0) {
   6654  1.1  christos 			isc_stdtime_get(&now);
   6655  1.7  christos 		}
   6656  1.7  christos 	} else {
   6657  1.1  christos 		now = 0;
   6658  1.7  christos 	}
   6659  1.1  christos 
   6660  1.1  christos 	result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
   6661  1.1  christos 					    &region, sizeof(rdatasetheader_t));
   6662  1.7  christos 	if (result != ISC_R_SUCCESS) {
   6663  1.1  christos 		return (result);
   6664  1.7  christos 	}
   6665  1.1  christos 
   6666  1.1  christos 	name = dns_fixedname_initname(&fixed);
   6667  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   6668  1.1  christos 	dns_rbt_fullnamefromnode(node, name);
   6669  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   6670  1.1  christos 	dns_rdataset_getownercase(rdataset, name);
   6671  1.1  christos 
   6672  1.1  christos 	newheader = (rdatasetheader_t *)region.base;
   6673  1.1  christos 	init_rdataset(rbtdb, newheader);
   6674  1.1  christos 	setownercase(newheader, name);
   6675  1.1  christos 	set_ttl(rbtdb, newheader, rdataset->ttl + now);
   6676  1.1  christos 	newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
   6677  1.1  christos 						rdataset->covers);
   6678  1.1  christos 	newheader->attributes = 0;
   6679  1.7  christos 	if (rdataset->ttl == 0U) {
   6680  1.1  christos 		newheader->attributes |= RDATASET_ATTR_ZEROTTL;
   6681  1.7  christos 	}
   6682  1.1  christos 	newheader->noqname = NULL;
   6683  1.1  christos 	newheader->closest = NULL;
   6684  1.7  christos 	atomic_init(&newheader->count,
   6685  1.7  christos 		    atomic_fetch_add_relaxed(&init_count, 1));
   6686  1.1  christos 	newheader->trust = rdataset->trust;
   6687  1.1  christos 	newheader->last_used = now;
   6688  1.1  christos 	newheader->node = rbtnode;
   6689  1.1  christos 	if (rbtversion != NULL) {
   6690  1.1  christos 		newheader->serial = rbtversion->serial;
   6691  1.1  christos 		now = 0;
   6692  1.1  christos 
   6693  1.1  christos 		if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
   6694  1.1  christos 			newheader->attributes |= RDATASET_ATTR_RESIGN;
   6695  1.7  christos 			newheader->resign = (isc_stdtime_t)(
   6696  1.7  christos 				dns_time64_from32(rdataset->resign) >> 1);
   6697  1.1  christos 			newheader->resign_lsb = rdataset->resign & 0x1;
   6698  1.1  christos 		} else {
   6699  1.1  christos 			newheader->resign = 0;
   6700  1.1  christos 			newheader->resign_lsb = 0;
   6701  1.1  christos 		}
   6702  1.1  christos 	} else {
   6703  1.1  christos 		newheader->serial = 1;
   6704  1.1  christos 		newheader->resign = 0;
   6705  1.1  christos 		newheader->resign_lsb = 0;
   6706  1.7  christos 		if ((rdataset->attributes & DNS_RDATASETATTR_PREFETCH) != 0) {
   6707  1.1  christos 			newheader->attributes |= RDATASET_ATTR_PREFETCH;
   6708  1.7  christos 		}
   6709  1.7  christos 		if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) {
   6710  1.1  christos 			newheader->attributes |= RDATASET_ATTR_NEGATIVE;
   6711  1.7  christos 		}
   6712  1.7  christos 		if ((rdataset->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0) {
   6713  1.1  christos 			newheader->attributes |= RDATASET_ATTR_NXDOMAIN;
   6714  1.7  christos 		}
   6715  1.7  christos 		if ((rdataset->attributes & DNS_RDATASETATTR_OPTOUT) != 0) {
   6716  1.1  christos 			newheader->attributes |= RDATASET_ATTR_OPTOUT;
   6717  1.7  christos 		}
   6718  1.1  christos 		if ((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0) {
   6719  1.1  christos 			result = addnoqname(rbtdb, newheader, rdataset);
   6720  1.1  christos 			if (result != ISC_R_SUCCESS) {
   6721  1.1  christos 				free_rdataset(rbtdb, rbtdb->common.mctx,
   6722  1.1  christos 					      newheader);
   6723  1.1  christos 				return (result);
   6724  1.1  christos 			}
   6725  1.1  christos 		}
   6726  1.1  christos 		if ((rdataset->attributes & DNS_RDATASETATTR_CLOSEST) != 0) {
   6727  1.1  christos 			result = addclosest(rbtdb, newheader, rdataset);
   6728  1.1  christos 			if (result != ISC_R_SUCCESS) {
   6729  1.1  christos 				free_rdataset(rbtdb, rbtdb->common.mctx,
   6730  1.1  christos 					      newheader);
   6731  1.1  christos 				return (result);
   6732  1.1  christos 			}
   6733  1.1  christos 		}
   6734  1.1  christos 	}
   6735  1.1  christos 
   6736  1.1  christos 	/*
   6737  1.1  christos 	 * If we're adding a delegation type (e.g. NS or DNAME for a zone,
   6738  1.1  christos 	 * just DNAME for the cache), then we need to set the callback bit
   6739  1.1  christos 	 * on the node.
   6740  1.1  christos 	 */
   6741  1.7  christos 	if (delegating_type(rbtdb, rbtnode, rdataset->type)) {
   6742  1.3  christos 		delegating = true;
   6743  1.7  christos 	} else {
   6744  1.3  christos 		delegating = false;
   6745  1.7  christos 	}
   6746  1.1  christos 
   6747  1.1  christos 	/*
   6748  1.1  christos 	 * Add to the auxiliary NSEC tree if we're adding an NSEC record.
   6749  1.1  christos 	 */
   6750  1.7  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   6751  1.1  christos 	if (rbtnode->nsec != DNS_RBT_NSEC_HAS_NSEC &&
   6752  1.1  christos 	    rdataset->type == dns_rdatatype_nsec)
   6753  1.7  christos 	{
   6754  1.3  christos 		newnsec = true;
   6755  1.7  christos 	} else {
   6756  1.3  christos 		newnsec = false;
   6757  1.7  christos 	}
   6758  1.7  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   6759  1.1  christos 
   6760  1.1  christos 	/*
   6761  1.1  christos 	 * If we're adding a delegation type, adding to the auxiliary NSEC tree,
   6762  1.1  christos 	 * or the DB is a cache in an overmem state, hold an exclusive lock on
   6763  1.1  christos 	 * the tree.  In the latter case the lock does not necessarily have to
   6764  1.5  christos 	 * be acquired but it will help purge ancient entries more effectively.
   6765  1.1  christos 	 */
   6766  1.7  christos 	if (IS_CACHE(rbtdb) && isc_mem_isovermem(rbtdb->common.mctx)) {
   6767  1.3  christos 		cache_is_overmem = true;
   6768  1.7  christos 	}
   6769  1.1  christos 	if (delegating || newnsec || cache_is_overmem) {
   6770  1.3  christos 		tree_locked = true;
   6771  1.1  christos 		RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   6772  1.1  christos 	}
   6773  1.1  christos 
   6774  1.7  christos 	if (cache_is_overmem) {
   6775  1.1  christos 		overmem_purge(rbtdb, rbtnode->locknum, now, tree_locked);
   6776  1.7  christos 	}
   6777  1.1  christos 
   6778  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   6779  1.1  christos 		  isc_rwlocktype_write);
   6780  1.1  christos 
   6781  1.1  christos 	if (rbtdb->rrsetstats != NULL) {
   6782  1.1  christos 		newheader->attributes |= RDATASET_ATTR_STATCOUNT;
   6783  1.3  christos 		update_rrsetstats(rbtdb, newheader, true);
   6784  1.1  christos 	}
   6785  1.1  christos 
   6786  1.1  christos 	if (IS_CACHE(rbtdb)) {
   6787  1.7  christos 		if (tree_locked) {
   6788  1.1  christos 			cleanup_dead_nodes(rbtdb, rbtnode->locknum);
   6789  1.7  christos 		}
   6790  1.1  christos 
   6791  1.1  christos 		header = isc_heap_element(rbtdb->heaps[rbtnode->locknum], 1);
   6792  1.7  christos 		if (header && header->rdh_ttl < now - RBTDB_VIRTUAL) {
   6793  1.7  christos 			expire_header(rbtdb, header, tree_locked, expire_ttl);
   6794  1.7  christos 		}
   6795  1.1  christos 
   6796  1.1  christos 		/*
   6797  1.1  christos 		 * If we've been holding a write lock on the tree just for
   6798  1.1  christos 		 * cleaning, we can release it now.  However, we still need the
   6799  1.1  christos 		 * node lock.
   6800  1.1  christos 		 */
   6801  1.1  christos 		if (tree_locked && !delegating && !newnsec) {
   6802  1.1  christos 			RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   6803  1.3  christos 			tree_locked = false;
   6804  1.1  christos 		}
   6805  1.1  christos 	}
   6806  1.1  christos 
   6807  1.1  christos 	result = ISC_R_SUCCESS;
   6808  1.1  christos 	if (newnsec) {
   6809  1.1  christos 		dns_rbtnode_t *nsecnode;
   6810  1.1  christos 
   6811  1.1  christos 		dns_rbt_fullnamefromnode(rbtnode, name);
   6812  1.1  christos 		nsecnode = NULL;
   6813  1.1  christos 		result = dns_rbt_addnode(rbtdb->nsec, name, &nsecnode);
   6814  1.1  christos 		if (result == ISC_R_SUCCESS) {
   6815  1.1  christos 			nsecnode->nsec = DNS_RBT_NSEC_NSEC;
   6816  1.1  christos 			rbtnode->nsec = DNS_RBT_NSEC_HAS_NSEC;
   6817  1.1  christos 		} else if (result == ISC_R_EXISTS) {
   6818  1.1  christos 			rbtnode->nsec = DNS_RBT_NSEC_HAS_NSEC;
   6819  1.1  christos 			result = ISC_R_SUCCESS;
   6820  1.1  christos 		}
   6821  1.1  christos 	}
   6822  1.1  christos 
   6823  1.7  christos 	if (result == ISC_R_SUCCESS) {
   6824  1.1  christos 		result = add32(rbtdb, rbtnode, rbtversion, newheader, options,
   6825  1.3  christos 			       false, addedrdataset, now);
   6826  1.7  christos 	}
   6827  1.7  christos 	if (result == ISC_R_SUCCESS && delegating) {
   6828  1.1  christos 		rbtnode->find_callback = 1;
   6829  1.7  christos 	}
   6830  1.1  christos 
   6831  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   6832  1.1  christos 		    isc_rwlocktype_write);
   6833  1.1  christos 
   6834  1.7  christos 	if (tree_locked) {
   6835  1.1  christos 		RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   6836  1.7  christos 	}
   6837  1.1  christos 
   6838  1.1  christos 	/*
   6839  1.1  christos 	 * Update the zone's secure status.  If version is non-NULL
   6840  1.1  christos 	 * this is deferred until closeversion() is called.
   6841  1.1  christos 	 */
   6842  1.7  christos 	if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb)) {
   6843  1.1  christos 		iszonesecure(db, version, rbtdb->origin_node);
   6844  1.7  christos 	}
   6845  1.1  christos 
   6846  1.1  christos 	return (result);
   6847  1.1  christos }
   6848  1.1  christos 
   6849  1.1  christos static isc_result_t
   6850  1.1  christos subtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
   6851  1.1  christos 		 dns_rdataset_t *rdataset, unsigned int options,
   6852  1.7  christos 		 dns_rdataset_t *newrdataset) {
   6853  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   6854  1.1  christos 	dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
   6855  1.1  christos 	rbtdb_version_t *rbtversion = version;
   6856  1.7  christos 	dns_fixedname_t fname;
   6857  1.7  christos 	dns_name_t *nodename = dns_fixedname_initname(&fname);
   6858  1.1  christos 	rdatasetheader_t *topheader, *topheader_prev, *header, *newheader;
   6859  1.1  christos 	unsigned char *subresult;
   6860  1.1  christos 	isc_region_t region;
   6861  1.1  christos 	isc_result_t result;
   6862  1.1  christos 	rbtdb_changed_t *changed;
   6863  1.1  christos 
   6864  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   6865  1.1  christos 	REQUIRE(rbtversion != NULL && rbtversion->rbtdb == rbtdb);
   6866  1.1  christos 
   6867  1.7  christos 	dns_rbt_fullnamefromnode(rbtnode, nodename);
   6868  1.7  christos 
   6869  1.7  christos 	if (rbtdb->common.methods == &zone_methods) {
   6870  1.7  christos 		RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   6871  1.1  christos 		REQUIRE(((rbtnode->nsec == DNS_RBT_NSEC_NSEC3 &&
   6872  1.1  christos 			  (rdataset->type == dns_rdatatype_nsec3 ||
   6873  1.1  christos 			   rdataset->covers == dns_rdatatype_nsec3)) ||
   6874  1.1  christos 			 (rbtnode->nsec != DNS_RBT_NSEC_NSEC3 &&
   6875  1.7  christos 			  rdataset->type != dns_rdatatype_nsec3 &&
   6876  1.7  christos 			  rdataset->covers != dns_rdatatype_nsec3)));
   6877  1.7  christos 		RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   6878  1.7  christos 	}
   6879  1.1  christos 
   6880  1.1  christos 	result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
   6881  1.1  christos 					    &region, sizeof(rdatasetheader_t));
   6882  1.7  christos 	if (result != ISC_R_SUCCESS) {
   6883  1.1  christos 		return (result);
   6884  1.7  christos 	}
   6885  1.1  christos 	newheader = (rdatasetheader_t *)region.base;
   6886  1.1  christos 	init_rdataset(rbtdb, newheader);
   6887  1.1  christos 	set_ttl(rbtdb, newheader, rdataset->ttl);
   6888  1.1  christos 	newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
   6889  1.1  christos 						rdataset->covers);
   6890  1.1  christos 	newheader->attributes = 0;
   6891  1.1  christos 	newheader->serial = rbtversion->serial;
   6892  1.1  christos 	newheader->trust = 0;
   6893  1.1  christos 	newheader->noqname = NULL;
   6894  1.1  christos 	newheader->closest = NULL;
   6895  1.7  christos 	atomic_init(&newheader->count,
   6896  1.7  christos 		    atomic_fetch_add_relaxed(&init_count, 1));
   6897  1.1  christos 	newheader->last_used = 0;
   6898  1.1  christos 	newheader->node = rbtnode;
   6899  1.1  christos 	if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
   6900  1.1  christos 		newheader->attributes |= RDATASET_ATTR_RESIGN;
   6901  1.7  christos 		newheader->resign = (isc_stdtime_t)(
   6902  1.7  christos 			dns_time64_from32(rdataset->resign) >> 1);
   6903  1.1  christos 		newheader->resign_lsb = rdataset->resign & 0x1;
   6904  1.1  christos 	} else {
   6905  1.1  christos 		newheader->resign = 0;
   6906  1.1  christos 		newheader->resign_lsb = 0;
   6907  1.1  christos 	}
   6908  1.1  christos 
   6909  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   6910  1.1  christos 		  isc_rwlocktype_write);
   6911  1.1  christos 
   6912  1.1  christos 	changed = add_changed(rbtdb, rbtversion, rbtnode);
   6913  1.1  christos 	if (changed == NULL) {
   6914  1.1  christos 		free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6915  1.1  christos 		NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   6916  1.1  christos 			    isc_rwlocktype_write);
   6917  1.1  christos 		return (ISC_R_NOMEMORY);
   6918  1.1  christos 	}
   6919  1.1  christos 
   6920  1.1  christos 	topheader_prev = NULL;
   6921  1.7  christos 	for (topheader = rbtnode->data; topheader != NULL;
   6922  1.1  christos 	     topheader = topheader->next) {
   6923  1.7  christos 		if (topheader->type == newheader->type) {
   6924  1.1  christos 			break;
   6925  1.7  christos 		}
   6926  1.1  christos 		topheader_prev = topheader;
   6927  1.1  christos 	}
   6928  1.1  christos 	/*
   6929  1.1  christos 	 * If header isn't NULL, we've found the right type.  There may be
   6930  1.1  christos 	 * IGNORE rdatasets between the top of the chain and the first real
   6931  1.1  christos 	 * data.  We skip over them.
   6932  1.1  christos 	 */
   6933  1.1  christos 	header = topheader;
   6934  1.7  christos 	while (header != NULL && IGNORE(header)) {
   6935  1.1  christos 		header = header->down;
   6936  1.7  christos 	}
   6937  1.1  christos 	if (header != NULL && EXISTS(header)) {
   6938  1.1  christos 		unsigned int flags = 0;
   6939  1.1  christos 		subresult = NULL;
   6940  1.1  christos 		result = ISC_R_SUCCESS;
   6941  1.1  christos 		if ((options & DNS_DBSUB_EXACT) != 0) {
   6942  1.1  christos 			flags |= DNS_RDATASLAB_EXACT;
   6943  1.7  christos 			if (newheader->rdh_ttl != header->rdh_ttl) {
   6944  1.1  christos 				result = DNS_R_NOTEXACT;
   6945  1.7  christos 			}
   6946  1.1  christos 		}
   6947  1.7  christos 		if (result == ISC_R_SUCCESS) {
   6948  1.1  christos 			result = dns_rdataslab_subtract(
   6949  1.7  christos 				(unsigned char *)header,
   6950  1.7  christos 				(unsigned char *)newheader,
   6951  1.7  christos 				(unsigned int)(sizeof(*newheader)),
   6952  1.7  christos 				rbtdb->common.mctx, rbtdb->common.rdclass,
   6953  1.7  christos 				(dns_rdatatype_t)header->type, flags,
   6954  1.7  christos 				&subresult);
   6955  1.7  christos 		}
   6956  1.1  christos 		if (result == ISC_R_SUCCESS) {
   6957  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6958  1.1  christos 			newheader = (rdatasetheader_t *)subresult;
   6959  1.1  christos 			init_rdataset(rbtdb, newheader);
   6960  1.1  christos 			update_newheader(newheader, header);
   6961  1.1  christos 			if (RESIGN(header)) {
   6962  1.1  christos 				newheader->attributes |= RDATASET_ATTR_RESIGN;
   6963  1.1  christos 				newheader->resign = header->resign;
   6964  1.1  christos 				newheader->resign_lsb = header->resign_lsb;
   6965  1.1  christos 				result = resign_insert(rbtdb, rbtnode->locknum,
   6966  1.1  christos 						       newheader);
   6967  1.1  christos 				if (result != ISC_R_SUCCESS) {
   6968  1.7  christos 					free_rdataset(rbtdb, rbtdb->common.mctx,
   6969  1.1  christos 						      newheader);
   6970  1.1  christos 					goto unlock;
   6971  1.1  christos 				}
   6972  1.1  christos 			}
   6973  1.1  christos 			/*
   6974  1.1  christos 			 * We have to set the serial since the rdataslab
   6975  1.1  christos 			 * subtraction routine copies the reserved portion of
   6976  1.1  christos 			 * header, not newheader.
   6977  1.1  christos 			 */
   6978  1.1  christos 			newheader->serial = rbtversion->serial;
   6979  1.1  christos 			/*
   6980  1.1  christos 			 * XXXJT: dns_rdataslab_subtract() copied the pointers
   6981  1.1  christos 			 * to additional info.  We need to clear these fields
   6982  1.1  christos 			 * to avoid having duplicated references.
   6983  1.1  christos 			 */
   6984  1.7  christos 			update_recordsandbytes(true, rbtversion, newheader,
   6985  1.7  christos 					       nodename->length);
   6986  1.1  christos 		} else if (result == DNS_R_NXRRSET) {
   6987  1.1  christos 			/*
   6988  1.1  christos 			 * This subtraction would remove all of the rdata;
   6989  1.1  christos 			 * add a nonexistent header instead.
   6990  1.1  christos 			 */
   6991  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   6992  1.1  christos 			newheader = new_rdataset(rbtdb, rbtdb->common.mctx);
   6993  1.1  christos 			if (newheader == NULL) {
   6994  1.1  christos 				result = ISC_R_NOMEMORY;
   6995  1.1  christos 				goto unlock;
   6996  1.1  christos 			}
   6997  1.1  christos 			init_rdataset(rbtdb, newheader);
   6998  1.1  christos 			set_ttl(rbtdb, newheader, 0);
   6999  1.1  christos 			newheader->type = topheader->type;
   7000  1.1  christos 			newheader->attributes = RDATASET_ATTR_NONEXISTENT;
   7001  1.1  christos 			newheader->trust = 0;
   7002  1.1  christos 			newheader->serial = rbtversion->serial;
   7003  1.1  christos 			newheader->noqname = NULL;
   7004  1.1  christos 			newheader->closest = NULL;
   7005  1.7  christos 			atomic_init(&newheader->count, 0);
   7006  1.1  christos 			newheader->node = rbtnode;
   7007  1.1  christos 			newheader->resign = 0;
   7008  1.1  christos 			newheader->resign_lsb = 0;
   7009  1.1  christos 			newheader->last_used = 0;
   7010  1.1  christos 		} else {
   7011  1.1  christos 			free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   7012  1.1  christos 			goto unlock;
   7013  1.1  christos 		}
   7014  1.1  christos 
   7015  1.1  christos 		/*
   7016  1.1  christos 		 * If we're here, we want to link newheader in front of
   7017  1.1  christos 		 * topheader.
   7018  1.1  christos 		 */
   7019  1.1  christos 		INSIST(rbtversion->serial >= topheader->serial);
   7020  1.7  christos 		update_recordsandbytes(false, rbtversion, header,
   7021  1.7  christos 				       nodename->length);
   7022  1.7  christos 		if (topheader_prev != NULL) {
   7023  1.1  christos 			topheader_prev->next = newheader;
   7024  1.7  christos 		} else {
   7025  1.1  christos 			rbtnode->data = newheader;
   7026  1.7  christos 		}
   7027  1.1  christos 		newheader->next = topheader->next;
   7028  1.1  christos 		newheader->down = topheader;
   7029  1.1  christos 		topheader->next = newheader;
   7030  1.1  christos 		rbtnode->dirty = 1;
   7031  1.3  christos 		changed->dirty = true;
   7032  1.1  christos 		resign_delete(rbtdb, rbtversion, header);
   7033  1.1  christos 	} else {
   7034  1.1  christos 		/*
   7035  1.1  christos 		 * The rdataset doesn't exist, so we don't need to do anything
   7036  1.1  christos 		 * to satisfy the deletion request.
   7037  1.1  christos 		 */
   7038  1.1  christos 		free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
   7039  1.7  christos 		if ((options & DNS_DBSUB_EXACT) != 0) {
   7040  1.1  christos 			result = DNS_R_NOTEXACT;
   7041  1.7  christos 		} else {
   7042  1.1  christos 			result = DNS_R_UNCHANGED;
   7043  1.7  christos 		}
   7044  1.1  christos 	}
   7045  1.1  christos 
   7046  1.7  christos 	if (result == ISC_R_SUCCESS && newrdataset != NULL) {
   7047  1.1  christos 		bind_rdataset(rbtdb, rbtnode, newheader, 0, newrdataset);
   7048  1.7  christos 	}
   7049  1.1  christos 
   7050  1.1  christos 	if (result == DNS_R_NXRRSET && newrdataset != NULL &&
   7051  1.1  christos 	    (options & DNS_DBSUB_WANTOLD) != 0)
   7052  1.7  christos 	{
   7053  1.1  christos 		bind_rdataset(rbtdb, rbtnode, header, 0, newrdataset);
   7054  1.7  christos 	}
   7055  1.1  christos 
   7056  1.7  christos unlock:
   7057  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   7058  1.1  christos 		    isc_rwlocktype_write);
   7059  1.1  christos 
   7060  1.1  christos 	/*
   7061  1.1  christos 	 * Update the zone's secure status.  If version is non-NULL
   7062  1.1  christos 	 * this is deferred until closeversion() is called.
   7063  1.1  christos 	 */
   7064  1.7  christos 	if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb)) {
   7065  1.7  christos 		RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   7066  1.7  christos 		version = rbtdb->current_version;
   7067  1.7  christos 		RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   7068  1.7  christos 		iszonesecure(db, version, rbtdb->origin_node);
   7069  1.7  christos 	}
   7070  1.1  christos 
   7071  1.1  christos 	return (result);
   7072  1.1  christos }
   7073  1.1  christos 
   7074  1.1  christos static isc_result_t
   7075  1.1  christos deleterdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
   7076  1.7  christos 	       dns_rdatatype_t type, dns_rdatatype_t covers) {
   7077  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   7078  1.1  christos 	dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
   7079  1.1  christos 	rbtdb_version_t *rbtversion = version;
   7080  1.1  christos 	isc_result_t result;
   7081  1.1  christos 	rdatasetheader_t *newheader;
   7082  1.1  christos 
   7083  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7084  1.1  christos 	INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
   7085  1.1  christos 
   7086  1.7  christos 	if (type == dns_rdatatype_any) {
   7087  1.1  christos 		return (ISC_R_NOTIMPLEMENTED);
   7088  1.7  christos 	}
   7089  1.7  christos 	if (type == dns_rdatatype_rrsig && covers == 0) {
   7090  1.1  christos 		return (ISC_R_NOTIMPLEMENTED);
   7091  1.7  christos 	}
   7092  1.1  christos 
   7093  1.1  christos 	newheader = new_rdataset(rbtdb, rbtdb->common.mctx);
   7094  1.7  christos 	if (newheader == NULL) {
   7095  1.1  christos 		return (ISC_R_NOMEMORY);
   7096  1.7  christos 	}
   7097  1.1  christos 	init_rdataset(rbtdb, newheader);
   7098  1.1  christos 	set_ttl(rbtdb, newheader, 0);
   7099  1.1  christos 	newheader->type = RBTDB_RDATATYPE_VALUE(type, covers);
   7100  1.1  christos 	newheader->attributes = RDATASET_ATTR_NONEXISTENT;
   7101  1.1  christos 	newheader->trust = 0;
   7102  1.1  christos 	newheader->noqname = NULL;
   7103  1.1  christos 	newheader->closest = NULL;
   7104  1.7  christos 	if (rbtversion != NULL) {
   7105  1.1  christos 		newheader->serial = rbtversion->serial;
   7106  1.7  christos 	} else {
   7107  1.1  christos 		newheader->serial = 0;
   7108  1.7  christos 	}
   7109  1.7  christos 	atomic_init(&newheader->count, 0);
   7110  1.1  christos 	newheader->last_used = 0;
   7111  1.1  christos 	newheader->node = rbtnode;
   7112  1.1  christos 
   7113  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   7114  1.1  christos 		  isc_rwlocktype_write);
   7115  1.1  christos 
   7116  1.1  christos 	result = add32(rbtdb, rbtnode, rbtversion, newheader, DNS_DBADD_FORCE,
   7117  1.3  christos 		       false, NULL, 0);
   7118  1.1  christos 
   7119  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   7120  1.1  christos 		    isc_rwlocktype_write);
   7121  1.1  christos 
   7122  1.1  christos 	/*
   7123  1.1  christos 	 * Update the zone's secure status.  If version is non-NULL
   7124  1.1  christos 	 * this is deferred until closeversion() is called.
   7125  1.1  christos 	 */
   7126  1.7  christos 	if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb)) {
   7127  1.7  christos 		RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   7128  1.7  christos 		version = rbtdb->current_version;
   7129  1.7  christos 		RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   7130  1.7  christos 		iszonesecure(db, version, rbtdb->origin_node);
   7131  1.7  christos 	}
   7132  1.1  christos 
   7133  1.1  christos 	return (result);
   7134  1.1  christos }
   7135  1.1  christos 
   7136  1.1  christos /*
   7137  1.1  christos  * load a non-NSEC3 node in the main tree and optionally to the auxiliary NSEC
   7138  1.1  christos  */
   7139  1.1  christos static isc_result_t
   7140  1.1  christos loadnode(dns_rbtdb_t *rbtdb, const dns_name_t *name, dns_rbtnode_t **nodep,
   7141  1.7  christos 	 bool hasnsec) {
   7142  1.1  christos 	isc_result_t noderesult, nsecresult, tmpresult;
   7143  1.1  christos 	dns_rbtnode_t *nsecnode = NULL, *node = NULL;
   7144  1.1  christos 
   7145  1.1  christos 	noderesult = dns_rbt_addnode(rbtdb->tree, name, &node);
   7146  1.7  christos 	if (!hasnsec) {
   7147  1.1  christos 		goto done;
   7148  1.7  christos 	}
   7149  1.1  christos 	if (noderesult == ISC_R_EXISTS) {
   7150  1.1  christos 		/*
   7151  1.1  christos 		 * Add a node to the auxiliary NSEC tree for an old node
   7152  1.1  christos 		 * just now getting an NSEC record.
   7153  1.1  christos 		 */
   7154  1.7  christos 		if (node->nsec == DNS_RBT_NSEC_HAS_NSEC) {
   7155  1.1  christos 			goto done;
   7156  1.7  christos 		}
   7157  1.7  christos 	} else if (noderesult != ISC_R_SUCCESS) {
   7158  1.1  christos 		goto done;
   7159  1.7  christos 	}
   7160  1.1  christos 
   7161  1.1  christos 	/*
   7162  1.1  christos 	 * Build the auxiliary tree for NSECs as we go.
   7163  1.1  christos 	 * This tree speeds searches for closest NSECs that would otherwise
   7164  1.1  christos 	 * need to examine many irrelevant nodes in large TLDs.
   7165  1.1  christos 	 *
   7166  1.1  christos 	 * Add nodes to the auxiliary tree after corresponding nodes have
   7167  1.1  christos 	 * been added to the main tree.
   7168  1.1  christos 	 */
   7169  1.1  christos 	nsecresult = dns_rbt_addnode(rbtdb->nsec, name, &nsecnode);
   7170  1.1  christos 	if (nsecresult == ISC_R_SUCCESS) {
   7171  1.1  christos 		nsecnode->nsec = DNS_RBT_NSEC_NSEC;
   7172  1.1  christos 		node->nsec = DNS_RBT_NSEC_HAS_NSEC;
   7173  1.1  christos 		goto done;
   7174  1.1  christos 	}
   7175  1.1  christos 
   7176  1.1  christos 	if (nsecresult == ISC_R_EXISTS) {
   7177  1.1  christos #if 1 /* 0 */
   7178  1.7  christos 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   7179  1.7  christos 			      DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
   7180  1.1  christos 			      "addnode: NSEC node already exists");
   7181  1.7  christos #endif /* if 1 */
   7182  1.1  christos 		node->nsec = DNS_RBT_NSEC_HAS_NSEC;
   7183  1.1  christos 		goto done;
   7184  1.1  christos 	}
   7185  1.1  christos 
   7186  1.1  christos 	if (noderesult == ISC_R_SUCCESS) {
   7187  1.1  christos 		/*
   7188  1.1  christos 		 * Remove the node we just added above.
   7189  1.1  christos 		 */
   7190  1.3  christos 		tmpresult = dns_rbt_deletenode(rbtdb->tree, node, false);
   7191  1.1  christos 		if (tmpresult != ISC_R_SUCCESS) {
   7192  1.7  christos 			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   7193  1.7  christos 				      DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
   7194  1.1  christos 				      "loading_addrdataset: "
   7195  1.1  christos 				      "dns_rbt_deletenode: %s after "
   7196  1.1  christos 				      "dns_rbt_addnode(NSEC): %s",
   7197  1.1  christos 				      isc_result_totext(tmpresult),
   7198  1.1  christos 				      isc_result_totext(noderesult));
   7199  1.1  christos 		}
   7200  1.1  christos 	}
   7201  1.1  christos 
   7202  1.1  christos 	/*
   7203  1.1  christos 	 * Set the error condition to be returned.
   7204  1.1  christos 	 */
   7205  1.1  christos 	noderesult = nsecresult;
   7206  1.1  christos 
   7207  1.7  christos done:
   7208  1.7  christos 	if (noderesult == ISC_R_SUCCESS || noderesult == ISC_R_EXISTS) {
   7209  1.1  christos 		*nodep = node;
   7210  1.7  christos 	}
   7211  1.1  christos 
   7212  1.1  christos 	return (noderesult);
   7213  1.1  christos }
   7214  1.1  christos 
   7215  1.1  christos static isc_result_t
   7216  1.1  christos loading_addrdataset(void *arg, const dns_name_t *name,
   7217  1.7  christos 		    dns_rdataset_t *rdataset) {
   7218  1.1  christos 	rbtdb_load_t *loadctx = arg;
   7219  1.1  christos 	dns_rbtdb_t *rbtdb = loadctx->rbtdb;
   7220  1.1  christos 	dns_rbtnode_t *node;
   7221  1.1  christos 	isc_result_t result;
   7222  1.1  christos 	isc_region_t region;
   7223  1.1  christos 	rdatasetheader_t *newheader;
   7224  1.1  christos 
   7225  1.1  christos 	REQUIRE(rdataset->rdclass == rbtdb->common.rdclass);
   7226  1.1  christos 
   7227  1.1  christos 	/*
   7228  1.1  christos 	 * This routine does no node locking.  See comments in
   7229  1.1  christos 	 * 'load' below for more information on loading and
   7230  1.1  christos 	 * locking.
   7231  1.1  christos 	 */
   7232  1.1  christos 
   7233  1.1  christos 	/*
   7234  1.1  christos 	 * SOA records are only allowed at top of zone.
   7235  1.1  christos 	 */
   7236  1.7  christos 	if (rdataset->type == dns_rdatatype_soa && !IS_CACHE(rbtdb) &&
   7237  1.7  christos 	    !dns_name_equal(name, &rbtdb->common.origin))
   7238  1.7  christos 	{
   7239  1.1  christos 		return (DNS_R_NOTZONETOP);
   7240  1.7  christos 	}
   7241  1.1  christos 
   7242  1.1  christos 	if (rdataset->type != dns_rdatatype_nsec3 &&
   7243  1.1  christos 	    rdataset->covers != dns_rdatatype_nsec3)
   7244  1.7  christos 	{
   7245  1.1  christos 		add_empty_wildcards(rbtdb, name);
   7246  1.7  christos 	}
   7247  1.1  christos 
   7248  1.1  christos 	if (dns_name_iswildcard(name)) {
   7249  1.1  christos 		/*
   7250  1.1  christos 		 * NS record owners cannot legally be wild cards.
   7251  1.1  christos 		 */
   7252  1.7  christos 		if (rdataset->type == dns_rdatatype_ns) {
   7253  1.1  christos 			return (DNS_R_INVALIDNS);
   7254  1.7  christos 		}
   7255  1.1  christos 		/*
   7256  1.1  christos 		 * NSEC3 record owners cannot legally be wild cards.
   7257  1.1  christos 		 */
   7258  1.7  christos 		if (rdataset->type == dns_rdatatype_nsec3) {
   7259  1.1  christos 			return (DNS_R_INVALIDNSEC3);
   7260  1.7  christos 		}
   7261  1.1  christos 		result = add_wildcard_magic(rbtdb, name);
   7262  1.7  christos 		if (result != ISC_R_SUCCESS) {
   7263  1.1  christos 			return (result);
   7264  1.7  christos 		}
   7265  1.1  christos 	}
   7266  1.1  christos 
   7267  1.1  christos 	node = NULL;
   7268  1.1  christos 	if (rdataset->type == dns_rdatatype_nsec3 ||
   7269  1.7  christos 	    rdataset->covers == dns_rdatatype_nsec3)
   7270  1.7  christos 	{
   7271  1.1  christos 		result = dns_rbt_addnode(rbtdb->nsec3, name, &node);
   7272  1.7  christos 		if (result == ISC_R_SUCCESS) {
   7273  1.1  christos 			node->nsec = DNS_RBT_NSEC_NSEC3;
   7274  1.7  christos 		}
   7275  1.1  christos 	} else if (rdataset->type == dns_rdatatype_nsec) {
   7276  1.3  christos 		result = loadnode(rbtdb, name, &node, true);
   7277  1.1  christos 	} else {
   7278  1.3  christos 		result = loadnode(rbtdb, name, &node, false);
   7279  1.1  christos 	}
   7280  1.7  christos 	if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
   7281  1.1  christos 		return (result);
   7282  1.7  christos 	}
   7283  1.1  christos 	if (result == ISC_R_SUCCESS) {
   7284  1.1  christos 		dns_name_t foundname;
   7285  1.1  christos 		dns_name_init(&foundname, NULL);
   7286  1.1  christos 		dns_rbt_namefromnode(node, &foundname);
   7287  1.1  christos 		node->locknum = node->hashval % rbtdb->node_lock_count;
   7288  1.1  christos 	}
   7289  1.1  christos 
   7290  1.1  christos 	result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
   7291  1.7  christos 					    &region, sizeof(rdatasetheader_t));
   7292  1.7  christos 	if (result != ISC_R_SUCCESS) {
   7293  1.1  christos 		return (result);
   7294  1.7  christos 	}
   7295  1.1  christos 	newheader = (rdatasetheader_t *)region.base;
   7296  1.1  christos 	init_rdataset(rbtdb, newheader);
   7297  1.7  christos 	set_ttl(rbtdb, newheader, rdataset->ttl + loadctx->now); /* XXX overflow
   7298  1.7  christos 								  * check */
   7299  1.1  christos 	newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
   7300  1.1  christos 						rdataset->covers);
   7301  1.1  christos 	newheader->attributes = 0;
   7302  1.1  christos 	newheader->trust = rdataset->trust;
   7303  1.1  christos 	newheader->serial = 1;
   7304  1.1  christos 	newheader->noqname = NULL;
   7305  1.1  christos 	newheader->closest = NULL;
   7306  1.7  christos 	atomic_init(&newheader->count,
   7307  1.7  christos 		    atomic_fetch_add_relaxed(&init_count, 1));
   7308  1.1  christos 	newheader->last_used = 0;
   7309  1.1  christos 	newheader->node = node;
   7310  1.1  christos 	setownercase(newheader, name);
   7311  1.1  christos 
   7312  1.1  christos 	if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
   7313  1.1  christos 		newheader->attributes |= RDATASET_ATTR_RESIGN;
   7314  1.7  christos 		newheader->resign = (isc_stdtime_t)(
   7315  1.7  christos 			dns_time64_from32(rdataset->resign) >> 1);
   7316  1.1  christos 		newheader->resign_lsb = rdataset->resign & 0x1;
   7317  1.1  christos 	} else {
   7318  1.1  christos 		newheader->resign = 0;
   7319  1.1  christos 		newheader->resign_lsb = 0;
   7320  1.1  christos 	}
   7321  1.1  christos 
   7322  1.7  christos 	NODE_LOCK(&rbtdb->node_locks[node->locknum].lock, isc_rwlocktype_write);
   7323  1.7  christos 
   7324  1.1  christos 	result = add32(rbtdb, node, rbtdb->current_version, newheader,
   7325  1.3  christos 		       DNS_DBADD_MERGE, true, NULL, 0);
   7326  1.7  christos 
   7327  1.7  christos 	NODE_UNLOCK(&rbtdb->node_locks[node->locknum].lock,
   7328  1.7  christos 		    isc_rwlocktype_write);
   7329  1.7  christos 
   7330  1.1  christos 	if (result == ISC_R_SUCCESS &&
   7331  1.7  christos 	    delegating_type(rbtdb, node, rdataset->type)) {
   7332  1.1  christos 		node->find_callback = 1;
   7333  1.7  christos 	} else if (result == DNS_R_UNCHANGED) {
   7334  1.1  christos 		result = ISC_R_SUCCESS;
   7335  1.7  christos 	}
   7336  1.1  christos 
   7337  1.1  christos 	return (result);
   7338  1.1  christos }
   7339  1.1  christos 
   7340  1.1  christos static isc_result_t
   7341  1.7  christos rbt_datafixer(dns_rbtnode_t *rbtnode, void *base, size_t filesize, void *arg,
   7342  1.7  christos 	      uint64_t *crc) {
   7343  1.1  christos 	isc_result_t result;
   7344  1.7  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)arg;
   7345  1.1  christos 	rdatasetheader_t *header;
   7346  1.7  christos 	unsigned char *limit = ((unsigned char *)base) + filesize;
   7347  1.1  christos 
   7348  1.1  christos 	REQUIRE(rbtnode != NULL);
   7349  1.6  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7350  1.1  christos 
   7351  1.1  christos 	for (header = rbtnode->data; header != NULL; header = header->next) {
   7352  1.7  christos 		unsigned char *p = (unsigned char *)header;
   7353  1.6  christos 		size_t size = dns_rdataslab_size(p, sizeof(*header));
   7354  1.1  christos 		isc_crc64_update(crc, p, size);
   7355  1.1  christos #ifdef DEBUG
   7356  1.1  christos 		hexdump("hashing header", p, sizeof(rdatasetheader_t));
   7357  1.1  christos 		hexdump("hashing slab", p + sizeof(rdatasetheader_t),
   7358  1.1  christos 			size - sizeof(rdatasetheader_t));
   7359  1.7  christos #endif /* ifdef DEBUG */
   7360  1.1  christos 		header->serial = 1;
   7361  1.1  christos 		header->is_mmapped = 1;
   7362  1.1  christos 		header->node = rbtnode;
   7363  1.1  christos 		header->node_is_relative = 0;
   7364  1.1  christos 
   7365  1.6  christos 		if (RESIGN(header) &&
   7366  1.7  christos 		    (header->resign != 0 || header->resign_lsb != 0)) {
   7367  1.1  christos 			int idx = header->node->locknum;
   7368  1.1  christos 			result = isc_heap_insert(rbtdb->heaps[idx], header);
   7369  1.7  christos 			if (result != ISC_R_SUCCESS) {
   7370  1.1  christos 				return (result);
   7371  1.7  christos 			}
   7372  1.1  christos 		}
   7373  1.1  christos 
   7374  1.1  christos 		if (header->next != NULL) {
   7375  1.1  christos 			size_t cooked = dns_rbt_serialize_align(size);
   7376  1.1  christos 			if ((uintptr_t)header->next !=
   7377  1.7  christos 			    (p - (unsigned char *)base) + cooked) {
   7378  1.1  christos 				return (ISC_R_INVALIDFILE);
   7379  1.7  christos 			}
   7380  1.1  christos 			header->next = (rdatasetheader_t *)(p + cooked);
   7381  1.1  christos 			header->next_is_relative = 0;
   7382  1.7  christos 			if ((header->next < (rdatasetheader_t *)base) ||
   7383  1.7  christos 			    (header->next > (rdatasetheader_t *)limit))
   7384  1.7  christos 			{
   7385  1.1  christos 				return (ISC_R_INVALIDFILE);
   7386  1.7  christos 			}
   7387  1.1  christos 		}
   7388  1.7  christos 
   7389  1.7  christos 		update_recordsandbytes(true, rbtdb->current_version, header,
   7390  1.7  christos 				       rbtnode->fullnamelen);
   7391  1.1  christos 	}
   7392  1.1  christos 
   7393  1.7  christos 	/* We're done deserializing; clear fullnamelen */
   7394  1.7  christos 	rbtnode->fullnamelen = 0;
   7395  1.7  christos 
   7396  1.1  christos 	return (ISC_R_SUCCESS);
   7397  1.1  christos }
   7398  1.1  christos 
   7399  1.1  christos /*
   7400  1.1  christos  * Load the RBT database from the image in 'f'
   7401  1.1  christos  */
   7402  1.1  christos static isc_result_t
   7403  1.1  christos deserialize32(void *arg, FILE *f, off_t offset) {
   7404  1.1  christos 	isc_result_t result;
   7405  1.1  christos 	rbtdb_load_t *loadctx = arg;
   7406  1.1  christos 	dns_rbtdb_t *rbtdb = loadctx->rbtdb;
   7407  1.1  christos 	rbtdb_file_header_t *header;
   7408  1.1  christos 	int fd;
   7409  1.1  christos 	off_t filesize = 0;
   7410  1.1  christos 	char *base;
   7411  1.1  christos 	dns_rbt_t *tree = NULL, *nsec = NULL, *nsec3 = NULL;
   7412  1.1  christos 	int protect, flags;
   7413  1.1  christos 	dns_rbtnode_t *origin_node = NULL;
   7414  1.1  christos 
   7415  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7416  1.1  christos 
   7417  1.1  christos 	/*
   7418  1.1  christos 	 * TODO CKB: since this is read-write (had to be to add nodes later)
   7419  1.1  christos 	 * we will need to lock the file or the nodes in it before modifying
   7420  1.1  christos 	 * the nodes in the file.
   7421  1.1  christos 	 */
   7422  1.1  christos 
   7423  1.1  christos 	/* Map in the whole file in one go */
   7424  1.1  christos 	fd = fileno(f);
   7425  1.1  christos 	isc_file_getsizefd(fd, &filesize);
   7426  1.7  christos 	protect = PROT_READ | PROT_WRITE;
   7427  1.1  christos 	flags = MAP_PRIVATE;
   7428  1.1  christos #ifdef MAP_FILE
   7429  1.1  christos 	flags |= MAP_FILE;
   7430  1.7  christos #endif /* ifdef MAP_FILE */
   7431  1.1  christos 
   7432  1.1  christos 	base = isc_file_mmap(NULL, filesize, protect, flags, fd, 0);
   7433  1.1  christos 	if (base == NULL || base == MAP_FAILED) {
   7434  1.1  christos 		return (ISC_R_FAILURE);
   7435  1.1  christos 	}
   7436  1.1  christos 
   7437  1.1  christos 	header = (rbtdb_file_header_t *)(base + offset);
   7438  1.1  christos 	if (!match_header_version(header)) {
   7439  1.1  christos 		result = ISC_R_INVALIDFILE;
   7440  1.1  christos 		goto cleanup;
   7441  1.1  christos 	}
   7442  1.1  christos 
   7443  1.1  christos 	if (header->tree != 0) {
   7444  1.7  christos 		result = dns_rbt_deserialize_tree(
   7445  1.7  christos 			base, filesize, (off_t)header->tree, rbtdb->common.mctx,
   7446  1.7  christos 			delete_callback, rbtdb, rbt_datafixer, rbtdb, NULL,
   7447  1.7  christos 			&tree);
   7448  1.7  christos 		if (result != ISC_R_SUCCESS) {
   7449  1.1  christos 			goto cleanup;
   7450  1.7  christos 		}
   7451  1.1  christos 
   7452  1.1  christos 		result = dns_rbt_findnode(tree, &rbtdb->common.origin, NULL,
   7453  1.1  christos 					  &origin_node, NULL,
   7454  1.1  christos 					  DNS_RBTFIND_EMPTYDATA, NULL, NULL);
   7455  1.7  christos 		if (result != ISC_R_SUCCESS) {
   7456  1.1  christos 			goto cleanup;
   7457  1.7  christos 		}
   7458  1.1  christos 	}
   7459  1.1  christos 
   7460  1.1  christos 	if (header->nsec != 0) {
   7461  1.7  christos 		result = dns_rbt_deserialize_tree(
   7462  1.7  christos 			base, filesize, (off_t)header->nsec, rbtdb->common.mctx,
   7463  1.7  christos 			delete_callback, rbtdb, rbt_datafixer, rbtdb, NULL,
   7464  1.7  christos 			&nsec);
   7465  1.7  christos 		if (result != ISC_R_SUCCESS) {
   7466  1.1  christos 			goto cleanup;
   7467  1.7  christos 		}
   7468  1.1  christos 	}
   7469  1.1  christos 
   7470  1.1  christos 	if (header->nsec3 != 0) {
   7471  1.7  christos 		result = dns_rbt_deserialize_tree(
   7472  1.7  christos 			base, filesize, (off_t)header->nsec3,
   7473  1.7  christos 			rbtdb->common.mctx, delete_callback, rbtdb,
   7474  1.7  christos 			rbt_datafixer, rbtdb, NULL, &nsec3);
   7475  1.7  christos 		if (result != ISC_R_SUCCESS) {
   7476  1.1  christos 			goto cleanup;
   7477  1.7  christos 		}
   7478  1.1  christos 	}
   7479  1.1  christos 
   7480  1.1  christos 	/*
   7481  1.1  christos 	 * We have a successfully loaded all the rbt trees now update
   7482  1.1  christos 	 * rbtdb to use them.
   7483  1.1  christos 	 */
   7484  1.1  christos 
   7485  1.1  christos 	rbtdb->mmap_location = base;
   7486  1.7  christos 	rbtdb->mmap_size = (size_t)filesize;
   7487  1.1  christos 
   7488  1.1  christos 	if (tree != NULL) {
   7489  1.1  christos 		dns_rbt_destroy(&rbtdb->tree);
   7490  1.1  christos 		rbtdb->tree = tree;
   7491  1.1  christos 		rbtdb->origin_node = origin_node;
   7492  1.1  christos 	}
   7493  1.1  christos 
   7494  1.1  christos 	if (nsec != NULL) {
   7495  1.1  christos 		dns_rbt_destroy(&rbtdb->nsec);
   7496  1.1  christos 		rbtdb->nsec = nsec;
   7497  1.1  christos 	}
   7498  1.1  christos 
   7499  1.1  christos 	if (nsec3 != NULL) {
   7500  1.1  christos 		dns_rbt_destroy(&rbtdb->nsec3);
   7501  1.1  christos 		rbtdb->nsec3 = nsec3;
   7502  1.1  christos 	}
   7503  1.1  christos 
   7504  1.1  christos 	return (ISC_R_SUCCESS);
   7505  1.1  christos 
   7506  1.7  christos cleanup:
   7507  1.7  christos 	if (tree != NULL) {
   7508  1.1  christos 		dns_rbt_destroy(&tree);
   7509  1.7  christos 	}
   7510  1.7  christos 	if (nsec != NULL) {
   7511  1.1  christos 		dns_rbt_destroy(&nsec);
   7512  1.7  christos 	}
   7513  1.7  christos 	if (nsec3 != NULL) {
   7514  1.1  christos 		dns_rbt_destroy(&nsec3);
   7515  1.7  christos 	}
   7516  1.7  christos 	isc_file_munmap(base, (size_t)filesize);
   7517  1.1  christos 	return (result);
   7518  1.1  christos }
   7519  1.1  christos 
   7520  1.1  christos static isc_result_t
   7521  1.1  christos beginload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
   7522  1.1  christos 	rbtdb_load_t *loadctx;
   7523  1.1  christos 	dns_rbtdb_t *rbtdb;
   7524  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7525  1.1  christos 
   7526  1.1  christos 	REQUIRE(DNS_CALLBACK_VALID(callbacks));
   7527  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7528  1.1  christos 
   7529  1.1  christos 	loadctx = isc_mem_get(rbtdb->common.mctx, sizeof(*loadctx));
   7530  1.1  christos 
   7531  1.1  christos 	loadctx->rbtdb = rbtdb;
   7532  1.7  christos 	if (IS_CACHE(rbtdb)) {
   7533  1.1  christos 		isc_stdtime_get(&loadctx->now);
   7534  1.7  christos 	} else {
   7535  1.1  christos 		loadctx->now = 0;
   7536  1.7  christos 	}
   7537  1.1  christos 
   7538  1.1  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
   7539  1.1  christos 
   7540  1.7  christos 	REQUIRE((rbtdb->attributes &
   7541  1.7  christos 		 (RBTDB_ATTR_LOADED | RBTDB_ATTR_LOADING)) == 0);
   7542  1.1  christos 	rbtdb->attributes |= RBTDB_ATTR_LOADING;
   7543  1.1  christos 
   7544  1.1  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   7545  1.1  christos 
   7546  1.1  christos 	callbacks->add = loading_addrdataset;
   7547  1.1  christos 	callbacks->add_private = loadctx;
   7548  1.1  christos 	callbacks->deserialize = deserialize32;
   7549  1.1  christos 	callbacks->deserialize_private = loadctx;
   7550  1.1  christos 
   7551  1.1  christos 	return (ISC_R_SUCCESS);
   7552  1.1  christos }
   7553  1.1  christos 
   7554  1.1  christos static isc_result_t
   7555  1.1  christos endload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
   7556  1.1  christos 	rbtdb_load_t *loadctx;
   7557  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   7558  1.1  christos 
   7559  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7560  1.1  christos 	REQUIRE(DNS_CALLBACK_VALID(callbacks));
   7561  1.1  christos 	loadctx = callbacks->add_private;
   7562  1.1  christos 	REQUIRE(loadctx != NULL);
   7563  1.1  christos 	REQUIRE(loadctx->rbtdb == rbtdb);
   7564  1.1  christos 
   7565  1.1  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
   7566  1.1  christos 
   7567  1.1  christos 	REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADING) != 0);
   7568  1.1  christos 	REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADED) == 0);
   7569  1.1  christos 
   7570  1.1  christos 	rbtdb->attributes &= ~RBTDB_ATTR_LOADING;
   7571  1.1  christos 	rbtdb->attributes |= RBTDB_ATTR_LOADED;
   7572  1.1  christos 
   7573  1.1  christos 	/*
   7574  1.1  christos 	 * If there's a KEY rdataset at the zone origin containing a
   7575  1.1  christos 	 * zone key, we consider the zone secure.
   7576  1.1  christos 	 */
   7577  1.7  christos 	if (!IS_CACHE(rbtdb) && rbtdb->origin_node != NULL) {
   7578  1.7  christos 		dns_dbversion_t *version = rbtdb->current_version;
   7579  1.7  christos 		RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   7580  1.7  christos 		iszonesecure(db, version, rbtdb->origin_node);
   7581  1.7  christos 	} else {
   7582  1.7  christos 		RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   7583  1.7  christos 	}
   7584  1.1  christos 
   7585  1.1  christos 	callbacks->add = NULL;
   7586  1.1  christos 	callbacks->add_private = NULL;
   7587  1.1  christos 	callbacks->deserialize = NULL;
   7588  1.1  christos 	callbacks->deserialize_private = NULL;
   7589  1.1  christos 
   7590  1.1  christos 	isc_mem_put(rbtdb->common.mctx, loadctx, sizeof(*loadctx));
   7591  1.1  christos 
   7592  1.1  christos 	return (ISC_R_SUCCESS);
   7593  1.1  christos }
   7594  1.1  christos 
   7595  1.1  christos /*
   7596  1.1  christos  * helper function to handle writing out the rdataset data pointed to
   7597  1.1  christos  * by the void *data pointer in the dns_rbtnode
   7598  1.1  christos  */
   7599  1.1  christos static isc_result_t
   7600  1.7  christos rbt_datawriter(FILE *rbtfile, unsigned char *data, void *arg, uint64_t *crc) {
   7601  1.7  christos 	rbtdb_version_t *version = (rbtdb_version_t *)arg;
   7602  1.1  christos 	rbtdb_serial_t serial;
   7603  1.1  christos 	rdatasetheader_t newheader;
   7604  1.7  christos 	rdatasetheader_t *header = (rdatasetheader_t *)data, *next;
   7605  1.1  christos 	off_t where;
   7606  1.1  christos 	size_t cooked, size;
   7607  1.1  christos 	unsigned char *p;
   7608  1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   7609  1.1  christos 	char pad[sizeof(char *)];
   7610  1.1  christos 	uintptr_t off;
   7611  1.1  christos 
   7612  1.1  christos 	REQUIRE(rbtfile != NULL);
   7613  1.1  christos 	REQUIRE(data != NULL);
   7614  1.1  christos 	REQUIRE(version != NULL);
   7615  1.1  christos 
   7616  1.1  christos 	serial = version->serial;
   7617  1.1  christos 
   7618  1.1  christos 	for (; header != NULL; header = next) {
   7619  1.1  christos 		next = header->next;
   7620  1.1  christos 		do {
   7621  1.1  christos 			if (header->serial <= serial && !IGNORE(header)) {
   7622  1.7  christos 				if (NONEXISTENT(header)) {
   7623  1.1  christos 					header = NULL;
   7624  1.7  christos 				}
   7625  1.1  christos 				break;
   7626  1.7  christos 			} else {
   7627  1.1  christos 				header = header->down;
   7628  1.7  christos 			}
   7629  1.1  christos 		} while (header != NULL);
   7630  1.1  christos 
   7631  1.7  christos 		if (header == NULL) {
   7632  1.1  christos 			continue;
   7633  1.7  christos 		}
   7634  1.1  christos 
   7635  1.1  christos 		CHECK(isc_stdio_tell(rbtfile, &where));
   7636  1.7  christos 		size = dns_rdataslab_size((unsigned char *)header,
   7637  1.1  christos 					  sizeof(rdatasetheader_t));
   7638  1.1  christos 
   7639  1.7  christos 		p = (unsigned char *)header;
   7640  1.1  christos 		memmove(&newheader, p, sizeof(rdatasetheader_t));
   7641  1.1  christos 		newheader.down = NULL;
   7642  1.1  christos 		newheader.next = NULL;
   7643  1.1  christos 		off = where;
   7644  1.7  christos 		if ((off_t)off != where) {
   7645  1.1  christos 			return (ISC_R_RANGE);
   7646  1.7  christos 		}
   7647  1.7  christos 		newheader.node = (dns_rbtnode_t *)off;
   7648  1.1  christos 		newheader.node_is_relative = 1;
   7649  1.1  christos 		newheader.serial = 1;
   7650  1.1  christos 
   7651  1.1  christos 		/*
   7652  1.1  christos 		 * Round size up to the next pointer sized offset so it
   7653  1.1  christos 		 * will be properly aligned when read back in.
   7654  1.1  christos 		 */
   7655  1.1  christos 		cooked = dns_rbt_serialize_align(size);
   7656  1.1  christos 		if (next != NULL) {
   7657  1.7  christos 			newheader.next = (rdatasetheader_t *)(off + cooked);
   7658  1.1  christos 			newheader.next_is_relative = 1;
   7659  1.1  christos 		}
   7660  1.1  christos 
   7661  1.1  christos #ifdef DEBUG
   7662  1.7  christos 		hexdump("writing header", (unsigned char *)&newheader,
   7663  1.1  christos 			sizeof(rdatasetheader_t));
   7664  1.1  christos 		hexdump("writing slab", p + sizeof(rdatasetheader_t),
   7665  1.1  christos 			size - sizeof(rdatasetheader_t));
   7666  1.7  christos #endif /* ifdef DEBUG */
   7667  1.7  christos 		isc_crc64_update(crc, (unsigned char *)&newheader,
   7668  1.1  christos 				 sizeof(rdatasetheader_t));
   7669  1.1  christos 		CHECK(isc_stdio_write(&newheader, sizeof(rdatasetheader_t), 1,
   7670  1.1  christos 				      rbtfile, NULL));
   7671  1.1  christos 
   7672  1.1  christos 		isc_crc64_update(crc, p + sizeof(rdatasetheader_t),
   7673  1.1  christos 				 size - sizeof(rdatasetheader_t));
   7674  1.1  christos 		CHECK(isc_stdio_write(p + sizeof(rdatasetheader_t),
   7675  1.1  christos 				      size - sizeof(rdatasetheader_t), 1,
   7676  1.1  christos 				      rbtfile, NULL));
   7677  1.1  christos 		/*
   7678  1.1  christos 		 * Pad to force alignment.
   7679  1.1  christos 		 */
   7680  1.7  christos 		if (size != (size_t)cooked) {
   7681  1.1  christos 			memset(pad, 0, sizeof(pad));
   7682  1.7  christos 			CHECK(isc_stdio_write(pad, cooked - size, 1, rbtfile,
   7683  1.7  christos 					      NULL));
   7684  1.1  christos 		}
   7685  1.1  christos 	}
   7686  1.1  christos 
   7687  1.7  christos failure:
   7688  1.1  christos 	return (result);
   7689  1.1  christos }
   7690  1.1  christos 
   7691  1.1  christos /*
   7692  1.1  christos  * Write out a zeroed header as a placeholder.  Doing this ensures
   7693  1.1  christos  * that the file will not read while it is partially written, should
   7694  1.1  christos  * writing fail or be interrupted.
   7695  1.1  christos  */
   7696  1.1  christos static isc_result_t
   7697  1.1  christos rbtdb_zero_header(FILE *rbtfile) {
   7698  1.1  christos 	char buffer[RBTDB_HEADER_LENGTH];
   7699  1.1  christos 	isc_result_t result;
   7700  1.1  christos 
   7701  1.1  christos 	memset(buffer, 0, RBTDB_HEADER_LENGTH);
   7702  1.1  christos 	result = isc_stdio_write(buffer, 1, RBTDB_HEADER_LENGTH, rbtfile, NULL);
   7703  1.1  christos 	fflush(rbtfile);
   7704  1.1  christos 
   7705  1.1  christos 	return (result);
   7706  1.1  christos }
   7707  1.1  christos 
   7708  1.1  christos static isc_once_t once = ISC_ONCE_INIT;
   7709  1.1  christos 
   7710  1.1  christos static void
   7711  1.1  christos init_file_version(void) {
   7712  1.1  christos 	int n;
   7713  1.1  christos 
   7714  1.1  christos 	memset(FILE_VERSION, 0, sizeof(FILE_VERSION));
   7715  1.7  christos 	n = snprintf(FILE_VERSION, sizeof(FILE_VERSION), "RBTDB Image %s %s",
   7716  1.7  christos 		     dns_major, dns_mapapi);
   7717  1.1  christos 	INSIST(n > 0 && (unsigned int)n < sizeof(FILE_VERSION));
   7718  1.1  christos }
   7719  1.1  christos 
   7720  1.1  christos /*
   7721  1.1  christos  * Write the file header out, recording the locations of the three
   7722  1.1  christos  * RBT's used in the rbtdb: tree, nsec, and nsec3, and including NodeDump
   7723  1.1  christos  * version information and any information stored in the rbtdb object
   7724  1.1  christos  * itself that should be stored here.
   7725  1.1  christos  */
   7726  1.1  christos static isc_result_t
   7727  1.1  christos rbtdb_write_header(FILE *rbtfile, off_t tree_location, off_t nsec_location,
   7728  1.7  christos 		   off_t nsec3_location) {
   7729  1.1  christos 	rbtdb_file_header_t header;
   7730  1.1  christos 	isc_result_t result;
   7731  1.1  christos 
   7732  1.1  christos 	RUNTIME_CHECK(isc_once_do(&once, init_file_version) == ISC_R_SUCCESS);
   7733  1.1  christos 
   7734  1.1  christos 	memset(&header, 0, sizeof(rbtdb_file_header_t));
   7735  1.1  christos 	memmove(header.version1, FILE_VERSION, sizeof(header.version1));
   7736  1.1  christos 	memmove(header.version2, FILE_VERSION, sizeof(header.version2));
   7737  1.7  christos 	header.ptrsize = (uint32_t)sizeof(void *);
   7738  1.1  christos 	header.bigendian = (1 == htonl(1)) ? 1 : 0;
   7739  1.7  christos 	header.tree = (uint64_t)tree_location;
   7740  1.7  christos 	header.nsec = (uint64_t)nsec_location;
   7741  1.7  christos 	header.nsec3 = (uint64_t)nsec3_location;
   7742  1.1  christos 	result = isc_stdio_write(&header, 1, sizeof(rbtdb_file_header_t),
   7743  1.7  christos 				 rbtfile, NULL);
   7744  1.1  christos 	fflush(rbtfile);
   7745  1.1  christos 
   7746  1.1  christos 	return (result);
   7747  1.1  christos }
   7748  1.1  christos 
   7749  1.3  christos static bool
   7750  1.1  christos match_header_version(rbtdb_file_header_t *header) {
   7751  1.1  christos 	RUNTIME_CHECK(isc_once_do(&once, init_file_version) == ISC_R_SUCCESS);
   7752  1.1  christos 
   7753  1.7  christos 	if (memcmp(header->version1, FILE_VERSION, sizeof(header->version1)) !=
   7754  1.7  christos 		    0 ||
   7755  1.7  christos 	    memcmp(header->version2, FILE_VERSION, sizeof(header->version1)) !=
   7756  1.7  christos 		    0)
   7757  1.1  christos 	{
   7758  1.3  christos 		return (false);
   7759  1.1  christos 	}
   7760  1.1  christos 
   7761  1.3  christos 	return (true);
   7762  1.1  christos }
   7763  1.1  christos 
   7764  1.1  christos static isc_result_t
   7765  1.1  christos serialize(dns_db_t *db, dns_dbversion_t *ver, FILE *rbtfile) {
   7766  1.7  christos 	rbtdb_version_t *version = (rbtdb_version_t *)ver;
   7767  1.1  christos 	dns_rbtdb_t *rbtdb;
   7768  1.1  christos 	isc_result_t result;
   7769  1.1  christos 	off_t tree_location, nsec_location, nsec3_location, header_location;
   7770  1.1  christos 
   7771  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7772  1.1  christos 
   7773  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7774  1.1  christos 	REQUIRE(rbtfile != NULL);
   7775  1.1  christos 
   7776  1.1  christos 	/* Ensure we're writing to a plain file */
   7777  1.1  christos 	CHECK(isc_file_isplainfilefd(fileno(rbtfile)));
   7778  1.1  christos 
   7779  1.1  christos 	/*
   7780  1.1  christos 	 * first, write out a zeroed header to store rbtdb information
   7781  1.1  christos 	 *
   7782  1.1  christos 	 * then for each of the three trees, store the current position
   7783  1.1  christos 	 * in the file and call dns_rbt_serialize_tree
   7784  1.1  christos 	 *
   7785  1.1  christos 	 * finally, write out the rbtdb header, storing the locations of the
   7786  1.1  christos 	 * rbtheaders
   7787  1.1  christos 	 *
   7788  1.1  christos 	 * NOTE: need to do something better with the return codes, &= will
   7789  1.1  christos 	 * not work.
   7790  1.1  christos 	 */
   7791  1.1  christos 	CHECK(isc_stdio_tell(rbtfile, &header_location));
   7792  1.1  christos 	CHECK(rbtdb_zero_header(rbtfile));
   7793  1.1  christos 	CHECK(dns_rbt_serialize_tree(rbtfile, rbtdb->tree, rbt_datawriter,
   7794  1.1  christos 				     version, &tree_location));
   7795  1.1  christos 	CHECK(dns_rbt_serialize_tree(rbtfile, rbtdb->nsec, rbt_datawriter,
   7796  1.1  christos 				     version, &nsec_location));
   7797  1.1  christos 	CHECK(dns_rbt_serialize_tree(rbtfile, rbtdb->nsec3, rbt_datawriter,
   7798  1.1  christos 				     version, &nsec3_location));
   7799  1.1  christos 
   7800  1.1  christos 	CHECK(isc_stdio_seek(rbtfile, header_location, SEEK_SET));
   7801  1.1  christos 	CHECK(rbtdb_write_header(rbtfile, tree_location, nsec_location,
   7802  1.1  christos 				 nsec3_location));
   7803  1.7  christos failure:
   7804  1.1  christos 	return (result);
   7805  1.1  christos }
   7806  1.1  christos 
   7807  1.1  christos static isc_result_t
   7808  1.1  christos dump(dns_db_t *db, dns_dbversion_t *version, const char *filename,
   7809  1.7  christos      dns_masterformat_t masterformat) {
   7810  1.1  christos 	dns_rbtdb_t *rbtdb;
   7811  1.1  christos 	rbtdb_version_t *rbtversion = version;
   7812  1.1  christos 
   7813  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7814  1.1  christos 
   7815  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7816  1.1  christos 	INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
   7817  1.1  christos 
   7818  1.3  christos 	return (dns_master_dump(rbtdb->common.mctx, db, version,
   7819  1.7  christos 				&dns_master_style_default, filename,
   7820  1.7  christos 				masterformat, NULL));
   7821  1.1  christos }
   7822  1.1  christos 
   7823  1.1  christos static void
   7824  1.1  christos delete_callback(void *data, void *arg) {
   7825  1.1  christos 	dns_rbtdb_t *rbtdb = arg;
   7826  1.1  christos 	rdatasetheader_t *current, *next;
   7827  1.1  christos 	unsigned int locknum;
   7828  1.1  christos 
   7829  1.1  christos 	current = data;
   7830  1.1  christos 	locknum = current->node->locknum;
   7831  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
   7832  1.1  christos 	while (current != NULL) {
   7833  1.1  christos 		next = current->next;
   7834  1.1  christos 		free_rdataset(rbtdb, rbtdb->common.mctx, current);
   7835  1.1  christos 		current = next;
   7836  1.1  christos 	}
   7837  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
   7838  1.1  christos }
   7839  1.1  christos 
   7840  1.3  christos static bool
   7841  1.1  christos issecure(dns_db_t *db) {
   7842  1.1  christos 	dns_rbtdb_t *rbtdb;
   7843  1.3  christos 	bool secure;
   7844  1.1  christos 
   7845  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7846  1.1  christos 
   7847  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7848  1.1  christos 
   7849  1.7  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   7850  1.3  christos 	secure = (rbtdb->current_version->secure == dns_db_secure);
   7851  1.7  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   7852  1.1  christos 
   7853  1.1  christos 	return (secure);
   7854  1.1  christos }
   7855  1.1  christos 
   7856  1.3  christos static bool
   7857  1.1  christos isdnssec(dns_db_t *db) {
   7858  1.1  christos 	dns_rbtdb_t *rbtdb;
   7859  1.3  christos 	bool dnssec;
   7860  1.1  christos 
   7861  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7862  1.1  christos 
   7863  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7864  1.1  christos 
   7865  1.7  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   7866  1.3  christos 	dnssec = (rbtdb->current_version->secure != dns_db_insecure);
   7867  1.7  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   7868  1.1  christos 
   7869  1.1  christos 	return (dnssec);
   7870  1.1  christos }
   7871  1.1  christos 
   7872  1.1  christos static unsigned int
   7873  1.1  christos nodecount(dns_db_t *db) {
   7874  1.1  christos 	dns_rbtdb_t *rbtdb;
   7875  1.1  christos 	unsigned int count;
   7876  1.1  christos 
   7877  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7878  1.1  christos 
   7879  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7880  1.1  christos 
   7881  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   7882  1.1  christos 	count = dns_rbt_nodecount(rbtdb->tree);
   7883  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   7884  1.1  christos 
   7885  1.1  christos 	return (count);
   7886  1.1  christos }
   7887  1.1  christos 
   7888  1.1  christos static size_t
   7889  1.1  christos hashsize(dns_db_t *db) {
   7890  1.1  christos 	dns_rbtdb_t *rbtdb;
   7891  1.1  christos 	size_t size;
   7892  1.1  christos 
   7893  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7894  1.1  christos 
   7895  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7896  1.1  christos 
   7897  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   7898  1.1  christos 	size = dns_rbt_hashsize(rbtdb->tree);
   7899  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   7900  1.1  christos 
   7901  1.1  christos 	return (size);
   7902  1.1  christos }
   7903  1.1  christos 
   7904  1.1  christos static void
   7905  1.1  christos settask(dns_db_t *db, isc_task_t *task) {
   7906  1.1  christos 	dns_rbtdb_t *rbtdb;
   7907  1.1  christos 
   7908  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7909  1.1  christos 
   7910  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7911  1.1  christos 
   7912  1.1  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
   7913  1.7  christos 	if (rbtdb->task != NULL) {
   7914  1.1  christos 		isc_task_detach(&rbtdb->task);
   7915  1.7  christos 	}
   7916  1.7  christos 	if (task != NULL) {
   7917  1.1  christos 		isc_task_attach(task, &rbtdb->task);
   7918  1.7  christos 	}
   7919  1.1  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
   7920  1.1  christos }
   7921  1.1  christos 
   7922  1.3  christos static bool
   7923  1.1  christos ispersistent(dns_db_t *db) {
   7924  1.1  christos 	UNUSED(db);
   7925  1.3  christos 	return (false);
   7926  1.1  christos }
   7927  1.1  christos 
   7928  1.1  christos static isc_result_t
   7929  1.1  christos getoriginnode(dns_db_t *db, dns_dbnode_t **nodep) {
   7930  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   7931  1.1  christos 	dns_rbtnode_t *onode;
   7932  1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   7933  1.1  christos 
   7934  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7935  1.1  christos 	REQUIRE(nodep != NULL && *nodep == NULL);
   7936  1.1  christos 
   7937  1.1  christos 	/* Note that the access to origin_node doesn't require a DB lock */
   7938  1.1  christos 	onode = (dns_rbtnode_t *)rbtdb->origin_node;
   7939  1.1  christos 	if (onode != NULL) {
   7940  1.1  christos 		new_reference(rbtdb, onode);
   7941  1.1  christos 
   7942  1.1  christos 		*nodep = rbtdb->origin_node;
   7943  1.1  christos 	} else {
   7944  1.1  christos 		INSIST(IS_CACHE(rbtdb));
   7945  1.1  christos 		result = ISC_R_NOTFOUND;
   7946  1.1  christos 	}
   7947  1.1  christos 
   7948  1.1  christos 	return (result);
   7949  1.1  christos }
   7950  1.1  christos 
   7951  1.1  christos static isc_result_t
   7952  1.1  christos getnsec3parameters(dns_db_t *db, dns_dbversion_t *version, dns_hash_t *hash,
   7953  1.7  christos 		   uint8_t *flags, uint16_t *iterations, unsigned char *salt,
   7954  1.7  christos 		   size_t *salt_length) {
   7955  1.1  christos 	dns_rbtdb_t *rbtdb;
   7956  1.1  christos 	isc_result_t result = ISC_R_NOTFOUND;
   7957  1.1  christos 	rbtdb_version_t *rbtversion = version;
   7958  1.1  christos 
   7959  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   7960  1.1  christos 
   7961  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   7962  1.1  christos 	INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
   7963  1.1  christos 
   7964  1.7  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   7965  1.7  christos 	if (rbtversion == NULL) {
   7966  1.1  christos 		rbtversion = rbtdb->current_version;
   7967  1.7  christos 	}
   7968  1.1  christos 
   7969  1.1  christos 	if (rbtversion->havensec3) {
   7970  1.7  christos 		if (hash != NULL) {
   7971  1.1  christos 			*hash = rbtversion->hash;
   7972  1.7  christos 		}
   7973  1.1  christos 		if (salt != NULL && salt_length != NULL) {
   7974  1.1  christos 			REQUIRE(*salt_length >= rbtversion->salt_length);
   7975  1.1  christos 			memmove(salt, rbtversion->salt,
   7976  1.1  christos 				rbtversion->salt_length);
   7977  1.1  christos 		}
   7978  1.7  christos 		if (salt_length != NULL) {
   7979  1.1  christos 			*salt_length = rbtversion->salt_length;
   7980  1.7  christos 		}
   7981  1.7  christos 		if (iterations != NULL) {
   7982  1.1  christos 			*iterations = rbtversion->iterations;
   7983  1.7  christos 		}
   7984  1.7  christos 		if (flags != NULL) {
   7985  1.1  christos 			*flags = rbtversion->flags;
   7986  1.7  christos 		}
   7987  1.1  christos 		result = ISC_R_SUCCESS;
   7988  1.1  christos 	}
   7989  1.7  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   7990  1.1  christos 
   7991  1.1  christos 	return (result);
   7992  1.1  christos }
   7993  1.1  christos 
   7994  1.1  christos static isc_result_t
   7995  1.3  christos getsize(dns_db_t *db, dns_dbversion_t *version, uint64_t *records,
   7996  1.7  christos 	uint64_t *bytes) {
   7997  1.1  christos 	dns_rbtdb_t *rbtdb;
   7998  1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   7999  1.1  christos 	rbtdb_version_t *rbtversion = version;
   8000  1.1  christos 
   8001  1.1  christos 	rbtdb = (dns_rbtdb_t *)db;
   8002  1.1  christos 
   8003  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8004  1.1  christos 	INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
   8005  1.1  christos 
   8006  1.7  christos 	RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
   8007  1.7  christos 	if (rbtversion == NULL) {
   8008  1.1  christos 		rbtversion = rbtdb->current_version;
   8009  1.7  christos 	}
   8010  1.1  christos 
   8011  1.1  christos 	RWLOCK(&rbtversion->rwlock, isc_rwlocktype_read);
   8012  1.7  christos 	if (records != NULL) {
   8013  1.1  christos 		*records = rbtversion->records;
   8014  1.7  christos 	}
   8015  1.1  christos 
   8016  1.7  christos 	if (bytes != NULL) {
   8017  1.1  christos 		*bytes = rbtversion->bytes;
   8018  1.7  christos 	}
   8019  1.1  christos 	RWUNLOCK(&rbtversion->rwlock, isc_rwlocktype_read);
   8020  1.7  christos 	RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
   8021  1.1  christos 
   8022  1.1  christos 	return (result);
   8023  1.1  christos }
   8024  1.1  christos 
   8025  1.1  christos static isc_result_t
   8026  1.1  christos setsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, isc_stdtime_t resign) {
   8027  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8028  1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   8029  1.1  christos 	rdatasetheader_t *header, oldheader;
   8030  1.1  christos 
   8031  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8032  1.1  christos 	REQUIRE(!IS_CACHE(rbtdb));
   8033  1.1  christos 	REQUIRE(rdataset != NULL);
   8034  1.1  christos 
   8035  1.1  christos 	header = rdataset->private3;
   8036  1.1  christos 	header--;
   8037  1.1  christos 
   8038  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[header->node->locknum].lock,
   8039  1.1  christos 		  isc_rwlocktype_write);
   8040  1.1  christos 
   8041  1.1  christos 	oldheader = *header;
   8042  1.1  christos 	/*
   8043  1.1  christos 	 * Only break the heap invariant (by adjusting resign and resign_lsb)
   8044  1.1  christos 	 * if we are going to be restoring it by calling isc_heap_increased
   8045  1.1  christos 	 * or isc_heap_decreased.
   8046  1.1  christos 	 */
   8047  1.1  christos 	if (resign != 0) {
   8048  1.7  christos 		header->resign = (isc_stdtime_t)(dns_time64_from32(resign) >>
   8049  1.7  christos 						 1);
   8050  1.1  christos 		header->resign_lsb = resign & 0x1;
   8051  1.1  christos 	}
   8052  1.1  christos 	if (header->heap_index != 0) {
   8053  1.1  christos 		INSIST(RESIGN(header));
   8054  1.1  christos 		if (resign == 0) {
   8055  1.1  christos 			isc_heap_delete(rbtdb->heaps[header->node->locknum],
   8056  1.1  christos 					header->heap_index);
   8057  1.1  christos 			header->heap_index = 0;
   8058  1.1  christos 		} else if (resign_sooner(header, &oldheader)) {
   8059  1.1  christos 			isc_heap_increased(rbtdb->heaps[header->node->locknum],
   8060  1.1  christos 					   header->heap_index);
   8061  1.1  christos 		} else if (resign_sooner(&oldheader, header)) {
   8062  1.1  christos 			isc_heap_decreased(rbtdb->heaps[header->node->locknum],
   8063  1.1  christos 					   header->heap_index);
   8064  1.1  christos 		}
   8065  1.1  christos 	} else if (resign != 0) {
   8066  1.1  christos 		header->attributes |= RDATASET_ATTR_RESIGN;
   8067  1.1  christos 		result = resign_insert(rbtdb, header->node->locknum, header);
   8068  1.1  christos 	}
   8069  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[header->node->locknum].lock,
   8070  1.1  christos 		    isc_rwlocktype_write);
   8071  1.1  christos 	return (result);
   8072  1.1  christos }
   8073  1.1  christos 
   8074  1.1  christos static isc_result_t
   8075  1.7  christos getsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, dns_name_t *foundname) {
   8076  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8077  1.1  christos 	rdatasetheader_t *header = NULL, *this;
   8078  1.1  christos 	unsigned int i;
   8079  1.1  christos 	isc_result_t result = ISC_R_NOTFOUND;
   8080  1.7  christos 	unsigned int locknum = 0;
   8081  1.1  christos 
   8082  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8083  1.1  christos 
   8084  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   8085  1.1  christos 
   8086  1.1  christos 	for (i = 0; i < rbtdb->node_lock_count; i++) {
   8087  1.1  christos 		NODE_LOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_read);
   8088  1.7  christos 
   8089  1.7  christos 		/*
   8090  1.7  christos 		 * Find for the earliest signing time among all of the
   8091  1.7  christos 		 * heaps, each of which is covered by a different bucket
   8092  1.7  christos 		 * lock.
   8093  1.7  christos 		 */
   8094  1.1  christos 		this = isc_heap_element(rbtdb->heaps[i], 1);
   8095  1.1  christos 		if (this == NULL) {
   8096  1.7  christos 			/* Nothing found; unlock and try the next heap. */
   8097  1.1  christos 			NODE_UNLOCK(&rbtdb->node_locks[i].lock,
   8098  1.1  christos 				    isc_rwlocktype_read);
   8099  1.1  christos 			continue;
   8100  1.1  christos 		}
   8101  1.7  christos 
   8102  1.7  christos 		if (header == NULL) {
   8103  1.7  christos 			/*
   8104  1.7  christos 			 * Found a signing time: retain the bucket lock and
   8105  1.7  christos 			 * preserve the lock number so we can unlock it
   8106  1.7  christos 			 * later.
   8107  1.7  christos 			 */
   8108  1.1  christos 			header = this;
   8109  1.7  christos 			locknum = i;
   8110  1.7  christos 		} else if (resign_sooner(this, header)) {
   8111  1.7  christos 			/*
   8112  1.7  christos 			 * Found an earlier signing time; release the
   8113  1.7  christos 			 * previous bucket lock and retain this one instead.
   8114  1.7  christos 			 */
   8115  1.1  christos 			NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
   8116  1.1  christos 				    isc_rwlocktype_read);
   8117  1.1  christos 			header = this;
   8118  1.7  christos 			locknum = i;
   8119  1.7  christos 		} else {
   8120  1.7  christos 			/*
   8121  1.7  christos 			 * Earliest signing time in this heap isn't
   8122  1.7  christos 			 * an improvement; unlock and try the next heap.
   8123  1.7  christos 			 */
   8124  1.1  christos 			NODE_UNLOCK(&rbtdb->node_locks[i].lock,
   8125  1.1  christos 				    isc_rwlocktype_read);
   8126  1.7  christos 		}
   8127  1.1  christos 	}
   8128  1.1  christos 
   8129  1.7  christos 	if (header != NULL) {
   8130  1.7  christos 		/*
   8131  1.7  christos 		 * Found something; pass back the answer and unlock
   8132  1.7  christos 		 * the bucket.
   8133  1.7  christos 		 */
   8134  1.7  christos 		bind_rdataset(rbtdb, header->node, header, 0, rdataset);
   8135  1.1  christos 
   8136  1.7  christos 		if (foundname != NULL) {
   8137  1.7  christos 			dns_rbt_fullnamefromnode(header->node, foundname);
   8138  1.7  christos 		}
   8139  1.1  christos 
   8140  1.7  christos 		NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
   8141  1.7  christos 			    isc_rwlocktype_read);
   8142  1.1  christos 
   8143  1.7  christos 		result = ISC_R_SUCCESS;
   8144  1.7  christos 	}
   8145  1.1  christos 
   8146  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   8147  1.1  christos 
   8148  1.1  christos 	return (result);
   8149  1.1  christos }
   8150  1.1  christos 
   8151  1.1  christos static void
   8152  1.7  christos resigned(dns_db_t *db, dns_rdataset_t *rdataset, dns_dbversion_t *version) {
   8153  1.1  christos 	rbtdb_version_t *rbtversion = (rbtdb_version_t *)version;
   8154  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8155  1.1  christos 	dns_rbtnode_t *node;
   8156  1.1  christos 	rdatasetheader_t *header;
   8157  1.1  christos 
   8158  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8159  1.1  christos 	REQUIRE(rdataset != NULL);
   8160  1.1  christos 	REQUIRE(rdataset->methods == &rdataset_methods);
   8161  1.1  christos 	REQUIRE(rbtdb->future_version == rbtversion);
   8162  1.1  christos 	REQUIRE(rbtversion != NULL);
   8163  1.1  christos 	REQUIRE(rbtversion->writer);
   8164  1.1  christos 	REQUIRE(rbtversion->rbtdb == rbtdb);
   8165  1.1  christos 
   8166  1.1  christos 	node = rdataset->private2;
   8167  1.1  christos 	INSIST(node != NULL);
   8168  1.1  christos 	header = rdataset->private3;
   8169  1.1  christos 	INSIST(header != NULL);
   8170  1.1  christos 	header--;
   8171  1.1  christos 
   8172  1.7  christos 	if (header->heap_index == 0) {
   8173  1.1  christos 		return;
   8174  1.7  christos 	}
   8175  1.1  christos 
   8176  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   8177  1.7  christos 	NODE_LOCK(&rbtdb->node_locks[node->locknum].lock, isc_rwlocktype_write);
   8178  1.1  christos 	/*
   8179  1.1  christos 	 * Delete from heap and save to re-signed list so that it can
   8180  1.1  christos 	 * be restored if we backout of this change.
   8181  1.1  christos 	 */
   8182  1.1  christos 	resign_delete(rbtdb, rbtversion, header);
   8183  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[node->locknum].lock,
   8184  1.1  christos 		    isc_rwlocktype_write);
   8185  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   8186  1.1  christos }
   8187  1.1  christos 
   8188  1.1  christos static isc_result_t
   8189  1.1  christos setcachestats(dns_db_t *db, isc_stats_t *stats) {
   8190  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8191  1.1  christos 
   8192  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8193  1.1  christos 	REQUIRE(IS_CACHE(rbtdb)); /* current restriction */
   8194  1.1  christos 	REQUIRE(stats != NULL);
   8195  1.1  christos 
   8196  1.1  christos 	isc_stats_attach(stats, &rbtdb->cachestats);
   8197  1.1  christos 	return (ISC_R_SUCCESS);
   8198  1.1  christos }
   8199  1.1  christos 
   8200  1.1  christos static isc_result_t
   8201  1.1  christos setgluecachestats(dns_db_t *db, isc_stats_t *stats) {
   8202  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8203  1.1  christos 
   8204  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8205  1.1  christos 	REQUIRE(!IS_CACHE(rbtdb) && !IS_STUB(rbtdb));
   8206  1.1  christos 	REQUIRE(stats != NULL);
   8207  1.1  christos 
   8208  1.1  christos 	isc_stats_attach(stats, &rbtdb->gluecachestats);
   8209  1.1  christos 	return (ISC_R_SUCCESS);
   8210  1.1  christos }
   8211  1.1  christos 
   8212  1.1  christos static dns_stats_t *
   8213  1.1  christos getrrsetstats(dns_db_t *db) {
   8214  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8215  1.1  christos 
   8216  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8217  1.1  christos 	REQUIRE(IS_CACHE(rbtdb)); /* current restriction */
   8218  1.1  christos 
   8219  1.1  christos 	return (rbtdb->rrsetstats);
   8220  1.1  christos }
   8221  1.1  christos 
   8222  1.1  christos static isc_result_t
   8223  1.1  christos nodefullname(dns_db_t *db, dns_dbnode_t *node, dns_name_t *name) {
   8224  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8225  1.1  christos 	dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
   8226  1.1  christos 	isc_result_t result;
   8227  1.1  christos 
   8228  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8229  1.1  christos 	REQUIRE(node != NULL);
   8230  1.1  christos 	REQUIRE(name != NULL);
   8231  1.1  christos 
   8232  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   8233  1.1  christos 	result = dns_rbt_fullnamefromnode(rbtnode, name);
   8234  1.1  christos 	RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   8235  1.1  christos 
   8236  1.1  christos 	return (result);
   8237  1.1  christos }
   8238  1.1  christos 
   8239  1.1  christos static isc_result_t
   8240  1.1  christos setservestalettl(dns_db_t *db, dns_ttl_t ttl) {
   8241  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8242  1.1  christos 
   8243  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8244  1.1  christos 	REQUIRE(IS_CACHE(rbtdb));
   8245  1.1  christos 
   8246  1.1  christos 	/* currently no bounds checking.  0 means disable. */
   8247  1.1  christos 	rbtdb->serve_stale_ttl = ttl;
   8248  1.1  christos 	return (ISC_R_SUCCESS);
   8249  1.1  christos }
   8250  1.1  christos 
   8251  1.1  christos static isc_result_t
   8252  1.1  christos getservestalettl(dns_db_t *db, dns_ttl_t *ttl) {
   8253  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
   8254  1.1  christos 
   8255  1.1  christos 	REQUIRE(VALID_RBTDB(rbtdb));
   8256  1.1  christos 	REQUIRE(IS_CACHE(rbtdb));
   8257  1.1  christos 
   8258  1.1  christos 	*ttl = rbtdb->serve_stale_ttl;
   8259  1.1  christos 	return (ISC_R_SUCCESS);
   8260  1.1  christos }
   8261  1.1  christos 
   8262  1.7  christos static dns_dbmethods_t zone_methods = { attach,
   8263  1.7  christos 					detach,
   8264  1.7  christos 					beginload,
   8265  1.7  christos 					endload,
   8266  1.7  christos 					serialize,
   8267  1.7  christos 					dump,
   8268  1.7  christos 					currentversion,
   8269  1.7  christos 					newversion,
   8270  1.7  christos 					attachversion,
   8271  1.7  christos 					closeversion,
   8272  1.7  christos 					findnode,
   8273  1.7  christos 					zone_find,
   8274  1.7  christos 					zone_findzonecut,
   8275  1.7  christos 					attachnode,
   8276  1.7  christos 					detachnode,
   8277  1.7  christos 					expirenode,
   8278  1.7  christos 					printnode,
   8279  1.7  christos 					createiterator,
   8280  1.7  christos 					zone_findrdataset,
   8281  1.7  christos 					allrdatasets,
   8282  1.7  christos 					addrdataset,
   8283  1.7  christos 					subtractrdataset,
   8284  1.7  christos 					deleterdataset,
   8285  1.7  christos 					issecure,
   8286  1.7  christos 					nodecount,
   8287  1.7  christos 					ispersistent,
   8288  1.7  christos 					overmem,
   8289  1.7  christos 					settask,
   8290  1.7  christos 					getoriginnode,
   8291  1.7  christos 					NULL, /* transfernode */
   8292  1.7  christos 					getnsec3parameters,
   8293  1.7  christos 					findnsec3node,
   8294  1.7  christos 					setsigningtime,
   8295  1.7  christos 					getsigningtime,
   8296  1.7  christos 					resigned,
   8297  1.7  christos 					isdnssec,
   8298  1.7  christos 					NULL, /* getrrsetstats */
   8299  1.7  christos 					NULL, /* rpz_attach */
   8300  1.7  christos 					NULL, /* rpz_ready */
   8301  1.7  christos 					NULL, /* findnodeext */
   8302  1.7  christos 					NULL, /* findext */
   8303  1.7  christos 					NULL, /* setcachestats */
   8304  1.7  christos 					hashsize,
   8305  1.7  christos 					nodefullname,
   8306  1.7  christos 					getsize,
   8307  1.7  christos 					NULL, /* setservestalettl */
   8308  1.7  christos 					NULL, /* getservestalettl */
   8309  1.7  christos 					setgluecachestats };
   8310  1.7  christos 
   8311  1.7  christos static dns_dbmethods_t cache_methods = { attach,
   8312  1.7  christos 					 detach,
   8313  1.7  christos 					 beginload,
   8314  1.7  christos 					 endload,
   8315  1.7  christos 					 NULL, /* serialize */
   8316  1.7  christos 					 dump,
   8317  1.7  christos 					 currentversion,
   8318  1.7  christos 					 newversion,
   8319  1.7  christos 					 attachversion,
   8320  1.7  christos 					 closeversion,
   8321  1.7  christos 					 findnode,
   8322  1.7  christos 					 cache_find,
   8323  1.7  christos 					 cache_findzonecut,
   8324  1.7  christos 					 attachnode,
   8325  1.7  christos 					 detachnode,
   8326  1.7  christos 					 expirenode,
   8327  1.7  christos 					 printnode,
   8328  1.7  christos 					 createiterator,
   8329  1.7  christos 					 cache_findrdataset,
   8330  1.7  christos 					 allrdatasets,
   8331  1.7  christos 					 addrdataset,
   8332  1.7  christos 					 subtractrdataset,
   8333  1.7  christos 					 deleterdataset,
   8334  1.7  christos 					 issecure,
   8335  1.7  christos 					 nodecount,
   8336  1.7  christos 					 ispersistent,
   8337  1.7  christos 					 overmem,
   8338  1.7  christos 					 settask,
   8339  1.7  christos 					 getoriginnode,
   8340  1.7  christos 					 NULL, /* transfernode */
   8341  1.7  christos 					 NULL, /* getnsec3parameters */
   8342  1.7  christos 					 NULL, /* findnsec3node */
   8343  1.7  christos 					 NULL, /* setsigningtime */
   8344  1.7  christos 					 NULL, /* getsigningtime */
   8345  1.7  christos 					 NULL, /* resigned */
   8346  1.7  christos 					 isdnssec,
   8347  1.7  christos 					 getrrsetstats,
   8348  1.7  christos 					 NULL, /* rpz_attach */
   8349  1.7  christos 					 NULL, /* rpz_ready */
   8350  1.7  christos 					 NULL, /* findnodeext */
   8351  1.7  christos 					 NULL, /* findext */
   8352  1.7  christos 					 setcachestats,
   8353  1.7  christos 					 hashsize,
   8354  1.7  christos 					 nodefullname,
   8355  1.7  christos 					 NULL, /* getsize */
   8356  1.7  christos 					 setservestalettl,
   8357  1.7  christos 					 getservestalettl,
   8358  1.7  christos 					 NULL };
   8359  1.1  christos 
   8360  1.1  christos isc_result_t
   8361  1.3  christos dns_rbtdb_create(isc_mem_t *mctx, const dns_name_t *origin, dns_dbtype_t type,
   8362  1.1  christos 		 dns_rdataclass_t rdclass, unsigned int argc, char *argv[],
   8363  1.7  christos 		 void *driverarg, dns_db_t **dbp) {
   8364  1.1  christos 	dns_rbtdb_t *rbtdb;
   8365  1.1  christos 	isc_result_t result;
   8366  1.1  christos 	int i;
   8367  1.1  christos 	dns_name_t name;
   8368  1.3  christos 	bool (*sooner)(void *, void *);
   8369  1.1  christos 	isc_mem_t *hmctx = mctx;
   8370  1.1  christos 
   8371  1.1  christos 	/* Keep the compiler happy. */
   8372  1.1  christos 	UNUSED(driverarg);
   8373  1.1  christos 
   8374  1.1  christos 	rbtdb = isc_mem_get(mctx, sizeof(*rbtdb));
   8375  1.1  christos 
   8376  1.1  christos 	/*
   8377  1.1  christos 	 * If argv[0] exists, it points to a memory context to use for heap
   8378  1.1  christos 	 */
   8379  1.7  christos 	if (argc != 0) {
   8380  1.7  christos 		hmctx = (isc_mem_t *)argv[0];
   8381  1.7  christos 	}
   8382  1.1  christos 
   8383  1.1  christos 	memset(rbtdb, '\0', sizeof(*rbtdb));
   8384  1.1  christos 	dns_name_init(&rbtdb->common.origin, NULL);
   8385  1.1  christos 	rbtdb->common.attributes = 0;
   8386  1.1  christos 	if (type == dns_dbtype_cache) {
   8387  1.1  christos 		rbtdb->common.methods = &cache_methods;
   8388  1.1  christos 		rbtdb->common.attributes |= DNS_DBATTR_CACHE;
   8389  1.1  christos 	} else if (type == dns_dbtype_stub) {
   8390  1.1  christos 		rbtdb->common.methods = &zone_methods;
   8391  1.1  christos 		rbtdb->common.attributes |= DNS_DBATTR_STUB;
   8392  1.7  christos 	} else {
   8393  1.1  christos 		rbtdb->common.methods = &zone_methods;
   8394  1.7  christos 	}
   8395  1.1  christos 	rbtdb->common.rdclass = rdclass;
   8396  1.1  christos 	rbtdb->common.mctx = NULL;
   8397  1.1  christos 
   8398  1.1  christos 	ISC_LIST_INIT(rbtdb->common.update_listeners);
   8399  1.1  christos 
   8400  1.1  christos 	result = RBTDB_INITLOCK(&rbtdb->lock);
   8401  1.7  christos 	if (result != ISC_R_SUCCESS) {
   8402  1.1  christos 		goto cleanup_rbtdb;
   8403  1.7  christos 	}
   8404  1.1  christos 
   8405  1.1  christos 	result = isc_rwlock_init(&rbtdb->tree_lock, 0, 0);
   8406  1.7  christos 	if (result != ISC_R_SUCCESS) {
   8407  1.1  christos 		goto cleanup_lock;
   8408  1.7  christos 	}
   8409  1.1  christos 
   8410  1.1  christos 	/*
   8411  1.1  christos 	 * Initialize node_lock_count in a generic way to support future
   8412  1.1  christos 	 * extension which allows the user to specify this value on creation.
   8413  1.1  christos 	 * Note that when specified for a cache DB it must be larger than 1
   8414  1.1  christos 	 * as commented with the definition of DEFAULT_CACHE_NODE_LOCK_COUNT.
   8415  1.1  christos 	 */
   8416  1.1  christos 	if (rbtdb->node_lock_count == 0) {
   8417  1.7  christos 		if (IS_CACHE(rbtdb)) {
   8418  1.1  christos 			rbtdb->node_lock_count = DEFAULT_CACHE_NODE_LOCK_COUNT;
   8419  1.7  christos 		} else {
   8420  1.1  christos 			rbtdb->node_lock_count = DEFAULT_NODE_LOCK_COUNT;
   8421  1.7  christos 		}
   8422  1.1  christos 	} else if (rbtdb->node_lock_count < 2 && IS_CACHE(rbtdb)) {
   8423  1.1  christos 		result = ISC_R_RANGE;
   8424  1.1  christos 		goto cleanup_tree_lock;
   8425  1.1  christos 	}
   8426  1.1  christos 	INSIST(rbtdb->node_lock_count < (1 << DNS_RBT_LOCKLENGTH));
   8427  1.1  christos 	rbtdb->node_locks = isc_mem_get(mctx, rbtdb->node_lock_count *
   8428  1.7  christos 						      sizeof(rbtdb_nodelock_t));
   8429  1.1  christos 
   8430  1.1  christos 	rbtdb->cachestats = NULL;
   8431  1.1  christos 	rbtdb->gluecachestats = NULL;
   8432  1.1  christos 
   8433  1.1  christos 	rbtdb->rrsetstats = NULL;
   8434  1.1  christos 	if (IS_CACHE(rbtdb)) {
   8435  1.1  christos 		result = dns_rdatasetstats_create(mctx, &rbtdb->rrsetstats);
   8436  1.7  christos 		if (result != ISC_R_SUCCESS) {
   8437  1.1  christos 			goto cleanup_node_locks;
   8438  1.1  christos 		}
   8439  1.7  christos 		rbtdb->rdatasets = isc_mem_get(
   8440  1.7  christos 			mctx,
   8441  1.7  christos 			rbtdb->node_lock_count * sizeof(rdatasetheaderlist_t));
   8442  1.7  christos 		for (i = 0; i < (int)rbtdb->node_lock_count; i++) {
   8443  1.1  christos 			ISC_LIST_INIT(rbtdb->rdatasets[i]);
   8444  1.7  christos 		}
   8445  1.7  christos 	} else {
   8446  1.1  christos 		rbtdb->rdatasets = NULL;
   8447  1.7  christos 	}
   8448  1.1  christos 
   8449  1.1  christos 	/*
   8450  1.1  christos 	 * Create the heaps.
   8451  1.1  christos 	 */
   8452  1.1  christos 	rbtdb->heaps = isc_mem_get(hmctx, rbtdb->node_lock_count *
   8453  1.7  christos 						  sizeof(isc_heap_t *));
   8454  1.7  christos 	for (i = 0; i < (int)rbtdb->node_lock_count; i++) {
   8455  1.7  christos 		rbtdb->heaps[i] = NULL;
   8456  1.1  christos 	}
   8457  1.1  christos 	sooner = IS_CACHE(rbtdb) ? ttl_sooner : resign_sooner;
   8458  1.1  christos 	for (i = 0; i < (int)rbtdb->node_lock_count; i++) {
   8459  1.1  christos 		result = isc_heap_create(hmctx, sooner, set_index, 0,
   8460  1.1  christos 					 &rbtdb->heaps[i]);
   8461  1.7  christos 		if (result != ISC_R_SUCCESS) {
   8462  1.1  christos 			goto cleanup_heaps;
   8463  1.7  christos 		}
   8464  1.1  christos 	}
   8465  1.1  christos 
   8466  1.1  christos 	/*
   8467  1.1  christos 	 * Create deadnode lists.
   8468  1.1  christos 	 */
   8469  1.1  christos 	rbtdb->deadnodes = isc_mem_get(mctx, rbtdb->node_lock_count *
   8470  1.7  christos 						     sizeof(rbtnodelist_t));
   8471  1.7  christos 	for (i = 0; i < (int)rbtdb->node_lock_count; i++) {
   8472  1.7  christos 		ISC_LIST_INIT(rbtdb->deadnodes[i]);
   8473  1.1  christos 	}
   8474  1.1  christos 
   8475  1.1  christos 	rbtdb->active = rbtdb->node_lock_count;
   8476  1.1  christos 
   8477  1.1  christos 	for (i = 0; i < (int)(rbtdb->node_lock_count); i++) {
   8478  1.1  christos 		result = NODE_INITLOCK(&rbtdb->node_locks[i].lock);
   8479  1.1  christos 		if (result == ISC_R_SUCCESS) {
   8480  1.3  christos 			isc_refcount_init(&rbtdb->node_locks[i].references, 0);
   8481  1.1  christos 		}
   8482  1.1  christos 		if (result != ISC_R_SUCCESS) {
   8483  1.1  christos 			while (i-- > 0) {
   8484  1.1  christos 				NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
   8485  1.7  christos 				isc_refcount_destroy(
   8486  1.7  christos 					&rbtdb->node_locks[i].references);
   8487  1.1  christos 			}
   8488  1.1  christos 			goto cleanup_deadnodes;
   8489  1.1  christos 		}
   8490  1.3  christos 		rbtdb->node_locks[i].exiting = false;
   8491  1.1  christos 	}
   8492  1.1  christos 
   8493  1.1  christos 	/*
   8494  1.1  christos 	 * Attach to the mctx.  The database will persist so long as there
   8495  1.1  christos 	 * are references to it, and attaching to the mctx ensures that our
   8496  1.1  christos 	 * mctx won't disappear out from under us.
   8497  1.1  christos 	 */
   8498  1.1  christos 	isc_mem_attach(mctx, &rbtdb->common.mctx);
   8499  1.1  christos 	isc_mem_attach(hmctx, &rbtdb->hmctx);
   8500  1.1  christos 
   8501  1.1  christos 	/*
   8502  1.1  christos 	 * Make a copy of the origin name.
   8503  1.1  christos 	 */
   8504  1.1  christos 	result = dns_name_dupwithoffsets(origin, mctx, &rbtdb->common.origin);
   8505  1.1  christos 	if (result != ISC_R_SUCCESS) {
   8506  1.3  christos 		free_rbtdb(rbtdb, false, NULL);
   8507  1.1  christos 		return (result);
   8508  1.1  christos 	}
   8509  1.1  christos 
   8510  1.1  christos 	/*
   8511  1.1  christos 	 * Make the Red-Black Trees.
   8512  1.1  christos 	 */
   8513  1.1  christos 	result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->tree);
   8514  1.1  christos 	if (result != ISC_R_SUCCESS) {
   8515  1.3  christos 		free_rbtdb(rbtdb, false, NULL);
   8516  1.1  christos 		return (result);
   8517  1.1  christos 	}
   8518  1.1  christos 
   8519  1.1  christos 	result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->nsec);
   8520  1.1  christos 	if (result != ISC_R_SUCCESS) {
   8521  1.3  christos 		free_rbtdb(rbtdb, false, NULL);
   8522  1.1  christos 		return (result);
   8523  1.1  christos 	}
   8524  1.1  christos 
   8525  1.1  christos 	result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->nsec3);
   8526  1.1  christos 	if (result != ISC_R_SUCCESS) {
   8527  1.3  christos 		free_rbtdb(rbtdb, false, NULL);
   8528  1.1  christos 		return (result);
   8529  1.1  christos 	}
   8530  1.1  christos 
   8531  1.1  christos 	/*
   8532  1.1  christos 	 * In order to set the node callback bit correctly in zone databases,
   8533  1.1  christos 	 * we need to know if the node has the origin name of the zone.
   8534  1.1  christos 	 * In loading_addrdataset() we could simply compare the new name
   8535  1.1  christos 	 * to the origin name, but this is expensive.  Also, we don't know the
   8536  1.1  christos 	 * node name in addrdataset(), so we need another way of knowing the
   8537  1.1  christos 	 * zone's top.
   8538  1.1  christos 	 *
   8539  1.1  christos 	 * We now explicitly create a node for the zone's origin, and then
   8540  1.1  christos 	 * we simply remember the node's address.  This is safe, because
   8541  1.1  christos 	 * the top-of-zone node can never be deleted, nor can its address
   8542  1.1  christos 	 * change.
   8543  1.1  christos 	 */
   8544  1.1  christos 	if (!IS_CACHE(rbtdb)) {
   8545  1.1  christos 		rbtdb->origin_node = NULL;
   8546  1.1  christos 		result = dns_rbt_addnode(rbtdb->tree, &rbtdb->common.origin,
   8547  1.1  christos 					 &rbtdb->origin_node);
   8548  1.1  christos 		if (result != ISC_R_SUCCESS) {
   8549  1.1  christos 			INSIST(result != ISC_R_EXISTS);
   8550  1.3  christos 			free_rbtdb(rbtdb, false, NULL);
   8551  1.1  christos 			return (result);
   8552  1.1  christos 		}
   8553  1.1  christos 		INSIST(rbtdb->origin_node != NULL);
   8554  1.1  christos 		rbtdb->origin_node->nsec = DNS_RBT_NSEC_NORMAL;
   8555  1.1  christos 		/*
   8556  1.1  christos 		 * We need to give the origin node the right locknum.
   8557  1.1  christos 		 */
   8558  1.1  christos 		dns_name_init(&name, NULL);
   8559  1.1  christos 		dns_rbt_namefromnode(rbtdb->origin_node, &name);
   8560  1.7  christos 		rbtdb->origin_node->locknum = rbtdb->origin_node->hashval %
   8561  1.7  christos 					      rbtdb->node_lock_count;
   8562  1.1  christos 		/*
   8563  1.1  christos 		 * Add an apex node to the NSEC3 tree so that NSEC3 searches
   8564  1.1  christos 		 * return partial matches when there is only a single NSEC3
   8565  1.1  christos 		 * record in the tree.
   8566  1.1  christos 		 */
   8567  1.1  christos 		rbtdb->nsec3_origin_node = NULL;
   8568  1.1  christos 		result = dns_rbt_addnode(rbtdb->nsec3, &rbtdb->common.origin,
   8569  1.1  christos 					 &rbtdb->nsec3_origin_node);
   8570  1.1  christos 		if (result != ISC_R_SUCCESS) {
   8571  1.1  christos 			INSIST(result != ISC_R_EXISTS);
   8572  1.3  christos 			free_rbtdb(rbtdb, false, NULL);
   8573  1.1  christos 			return (result);
   8574  1.1  christos 		}
   8575  1.1  christos 		rbtdb->nsec3_origin_node->nsec = DNS_RBT_NSEC_NSEC3;
   8576  1.1  christos 		/*
   8577  1.1  christos 		 * We need to give the nsec3 origin node the right locknum.
   8578  1.1  christos 		 */
   8579  1.1  christos 		dns_name_init(&name, NULL);
   8580  1.1  christos 		dns_rbt_namefromnode(rbtdb->nsec3_origin_node, &name);
   8581  1.1  christos 		rbtdb->nsec3_origin_node->locknum =
   8582  1.1  christos 			rbtdb->nsec3_origin_node->hashval %
   8583  1.1  christos 			rbtdb->node_lock_count;
   8584  1.1  christos 	}
   8585  1.1  christos 
   8586  1.1  christos 	/*
   8587  1.1  christos 	 * Misc. Initialization.
   8588  1.1  christos 	 */
   8589  1.3  christos 	isc_refcount_init(&rbtdb->references, 1);
   8590  1.1  christos 	rbtdb->attributes = 0;
   8591  1.1  christos 	rbtdb->task = NULL;
   8592  1.1  christos 	rbtdb->serve_stale_ttl = 0;
   8593  1.1  christos 
   8594  1.1  christos 	/*
   8595  1.1  christos 	 * Version Initialization.
   8596  1.1  christos 	 */
   8597  1.1  christos 	rbtdb->current_serial = 1;
   8598  1.1  christos 	rbtdb->least_serial = 1;
   8599  1.1  christos 	rbtdb->next_serial = 2;
   8600  1.3  christos 	rbtdb->current_version = allocate_version(mctx, 1, 1, false);
   8601  1.1  christos 	if (rbtdb->current_version == NULL) {
   8602  1.7  christos 		isc_refcount_decrement(&rbtdb->references);
   8603  1.3  christos 		free_rbtdb(rbtdb, false, NULL);
   8604  1.1  christos 		return (ISC_R_NOMEMORY);
   8605  1.1  christos 	}
   8606  1.1  christos 	rbtdb->current_version->rbtdb = rbtdb;
   8607  1.1  christos 	rbtdb->current_version->secure = dns_db_insecure;
   8608  1.3  christos 	rbtdb->current_version->havensec3 = false;
   8609  1.1  christos 	rbtdb->current_version->flags = 0;
   8610  1.1  christos 	rbtdb->current_version->iterations = 0;
   8611  1.1  christos 	rbtdb->current_version->hash = 0;
   8612  1.1  christos 	rbtdb->current_version->salt_length = 0;
   8613  1.1  christos 	memset(rbtdb->current_version->salt, 0,
   8614  1.1  christos 	       sizeof(rbtdb->current_version->salt));
   8615  1.1  christos 	result = isc_rwlock_init(&rbtdb->current_version->rwlock, 0, 0);
   8616  1.1  christos 	if (result != ISC_R_SUCCESS) {
   8617  1.1  christos 		free_gluetable(rbtdb->current_version);
   8618  1.1  christos 		isc_rwlock_destroy(&rbtdb->current_version->glue_rwlock);
   8619  1.1  christos 		isc_refcount_destroy(&rbtdb->current_version->references);
   8620  1.1  christos 		isc_mem_put(mctx, rbtdb->current_version,
   8621  1.1  christos 			    sizeof(*rbtdb->current_version));
   8622  1.1  christos 		rbtdb->current_version = NULL;
   8623  1.7  christos 		isc_refcount_decrement(&rbtdb->references);
   8624  1.3  christos 		free_rbtdb(rbtdb, false, NULL);
   8625  1.1  christos 		return (result);
   8626  1.1  christos 	}
   8627  1.1  christos 
   8628  1.1  christos 	rbtdb->current_version->records = 0;
   8629  1.1  christos 	rbtdb->current_version->bytes = 0;
   8630  1.1  christos 	rbtdb->future_version = NULL;
   8631  1.1  christos 	ISC_LIST_INIT(rbtdb->open_versions);
   8632  1.1  christos 	/*
   8633  1.1  christos 	 * Keep the current version in the open list so that list operation
   8634  1.1  christos 	 * won't happen in normal lookup operations.
   8635  1.1  christos 	 */
   8636  1.1  christos 	PREPEND(rbtdb->open_versions, rbtdb->current_version, link);
   8637  1.1  christos 
   8638  1.1  christos 	rbtdb->common.magic = DNS_DB_MAGIC;
   8639  1.1  christos 	rbtdb->common.impmagic = RBTDB_MAGIC;
   8640  1.1  christos 
   8641  1.1  christos 	*dbp = (dns_db_t *)rbtdb;
   8642  1.1  christos 
   8643  1.1  christos 	return (ISC_R_SUCCESS);
   8644  1.1  christos 
   8645  1.7  christos cleanup_deadnodes:
   8646  1.1  christos 	isc_mem_put(mctx, rbtdb->deadnodes,
   8647  1.1  christos 		    rbtdb->node_lock_count * sizeof(rbtnodelist_t));
   8648  1.1  christos 
   8649  1.7  christos cleanup_heaps:
   8650  1.1  christos 	if (rbtdb->heaps != NULL) {
   8651  1.7  christos 		for (i = 0; i < (int)rbtdb->node_lock_count; i++) {
   8652  1.7  christos 			if (rbtdb->heaps[i] != NULL) {
   8653  1.1  christos 				isc_heap_destroy(&rbtdb->heaps[i]);
   8654  1.7  christos 			}
   8655  1.7  christos 		}
   8656  1.1  christos 		isc_mem_put(hmctx, rbtdb->heaps,
   8657  1.1  christos 			    rbtdb->node_lock_count * sizeof(isc_heap_t *));
   8658  1.1  christos 	}
   8659  1.1  christos 
   8660  1.7  christos 	if (rbtdb->rdatasets != NULL) {
   8661  1.7  christos 		isc_mem_put(mctx, rbtdb->rdatasets,
   8662  1.7  christos 			    rbtdb->node_lock_count *
   8663  1.7  christos 				    sizeof(rdatasetheaderlist_t));
   8664  1.7  christos 	}
   8665  1.7  christos 	if (rbtdb->rrsetstats != NULL) {
   8666  1.1  christos 		dns_stats_detach(&rbtdb->rrsetstats);
   8667  1.7  christos 	}
   8668  1.1  christos 
   8669  1.7  christos cleanup_node_locks:
   8670  1.1  christos 	isc_mem_put(mctx, rbtdb->node_locks,
   8671  1.1  christos 		    rbtdb->node_lock_count * sizeof(rbtdb_nodelock_t));
   8672  1.1  christos 
   8673  1.7  christos cleanup_tree_lock:
   8674  1.1  christos 	isc_rwlock_destroy(&rbtdb->tree_lock);
   8675  1.1  christos 
   8676  1.7  christos cleanup_lock:
   8677  1.1  christos 	RBTDB_DESTROYLOCK(&rbtdb->lock);
   8678  1.1  christos 
   8679  1.7  christos cleanup_rbtdb:
   8680  1.7  christos 	isc_mem_put(mctx, rbtdb, sizeof(*rbtdb));
   8681  1.1  christos 	return (result);
   8682  1.1  christos }
   8683  1.1  christos 
   8684  1.1  christos /*
   8685  1.1  christos  * Slabbed Rdataset Methods
   8686  1.1  christos  */
   8687  1.1  christos 
   8688  1.1  christos static void
   8689  1.1  christos rdataset_disassociate(dns_rdataset_t *rdataset) {
   8690  1.1  christos 	dns_db_t *db = rdataset->private1;
   8691  1.1  christos 	dns_dbnode_t *node = rdataset->private2;
   8692  1.1  christos 
   8693  1.1  christos 	detachnode(db, &node);
   8694  1.1  christos }
   8695  1.1  christos 
   8696  1.1  christos static isc_result_t
   8697  1.1  christos rdataset_first(dns_rdataset_t *rdataset) {
   8698  1.7  christos 	unsigned char *raw = rdataset->private3; /* RDATASLAB */
   8699  1.1  christos 	unsigned int count;
   8700  1.1  christos 
   8701  1.1  christos 	count = raw[0] * 256 + raw[1];
   8702  1.1  christos 	if (count == 0) {
   8703  1.1  christos 		rdataset->private5 = NULL;
   8704  1.1  christos 		return (ISC_R_NOMORE);
   8705  1.1  christos 	}
   8706  1.1  christos 
   8707  1.5  christos 	if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) == 0) {
   8708  1.5  christos 		raw += DNS_RDATASET_COUNT;
   8709  1.5  christos 	}
   8710  1.5  christos 
   8711  1.5  christos 	raw += DNS_RDATASET_LENGTH;
   8712  1.1  christos 
   8713  1.1  christos 	/*
   8714  1.1  christos 	 * The privateuint4 field is the number of rdata beyond the
   8715  1.1  christos 	 * cursor position, so we decrement the total count by one
   8716  1.1  christos 	 * before storing it.
   8717  1.1  christos 	 *
   8718  1.1  christos 	 * If DNS_RDATASETATTR_LOADORDER is not set 'raw' points to the
   8719  1.1  christos 	 * first record.  If DNS_RDATASETATTR_LOADORDER is set 'raw' points
   8720  1.1  christos 	 * to the first entry in the offset table.
   8721  1.1  christos 	 */
   8722  1.1  christos 	count--;
   8723  1.1  christos 	rdataset->privateuint4 = count;
   8724  1.1  christos 	rdataset->private5 = raw;
   8725  1.1  christos 
   8726  1.1  christos 	return (ISC_R_SUCCESS);
   8727  1.1  christos }
   8728  1.1  christos 
   8729  1.1  christos static isc_result_t
   8730  1.1  christos rdataset_next(dns_rdataset_t *rdataset) {
   8731  1.1  christos 	unsigned int count;
   8732  1.1  christos 	unsigned int length;
   8733  1.7  christos 	unsigned char *raw; /* RDATASLAB */
   8734  1.1  christos 
   8735  1.1  christos 	count = rdataset->privateuint4;
   8736  1.7  christos 	if (count == 0) {
   8737  1.1  christos 		return (ISC_R_NOMORE);
   8738  1.7  christos 	}
   8739  1.1  christos 	count--;
   8740  1.1  christos 	rdataset->privateuint4 = count;
   8741  1.1  christos 
   8742  1.1  christos 	/*
   8743  1.1  christos 	 * Skip forward one record (length + 4) or one offset (4).
   8744  1.1  christos 	 */
   8745  1.1  christos 	raw = rdataset->private5;
   8746  1.1  christos #if DNS_RDATASET_FIXED
   8747  1.5  christos 	if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) == 0)
   8748  1.5  christos #endif /* DNS_RDATASET_FIXED */
   8749  1.5  christos 	{
   8750  1.1  christos 		length = raw[0] * 256 + raw[1];
   8751  1.1  christos 		raw += length;
   8752  1.1  christos 	}
   8753  1.5  christos 
   8754  1.5  christos 	rdataset->private5 = raw + DNS_RDATASET_ORDER + DNS_RDATASET_LENGTH;
   8755  1.1  christos 
   8756  1.1  christos 	return (ISC_R_SUCCESS);
   8757  1.1  christos }
   8758  1.1  christos 
   8759  1.1  christos static void
   8760  1.1  christos rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
   8761  1.7  christos 	unsigned char *raw = rdataset->private5; /* RDATASLAB */
   8762  1.1  christos 	unsigned int length;
   8763  1.1  christos 	isc_region_t r;
   8764  1.1  christos 	unsigned int flags = 0;
   8765  1.1  christos 
   8766  1.1  christos 	REQUIRE(raw != NULL);
   8767  1.1  christos 
   8768  1.1  christos 	/*
   8769  1.1  christos 	 * Find the start of the record if not already in private5
   8770  1.1  christos 	 * then skip the length and order fields.
   8771  1.1  christos 	 */
   8772  1.1  christos #if DNS_RDATASET_FIXED
   8773  1.1  christos 	if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) != 0) {
   8774  1.5  christos 		unsigned int offset;
   8775  1.7  christos 		offset = (raw[0] << 24) + (raw[1] << 16) + (raw[2] << 8) +
   8776  1.7  christos 			 raw[3];
   8777  1.1  christos 		raw = rdataset->private3;
   8778  1.1  christos 		raw += offset;
   8779  1.1  christos 	}
   8780  1.7  christos #endif /* if DNS_RDATASET_FIXED */
   8781  1.5  christos 
   8782  1.1  christos 	length = raw[0] * 256 + raw[1];
   8783  1.5  christos 
   8784  1.5  christos 	raw += DNS_RDATASET_ORDER + DNS_RDATASET_LENGTH;
   8785  1.5  christos 
   8786  1.1  christos 	if (rdataset->type == dns_rdatatype_rrsig) {
   8787  1.7  christos 		if (*raw & DNS_RDATASLAB_OFFLINE) {
   8788  1.1  christos 			flags |= DNS_RDATA_OFFLINE;
   8789  1.7  christos 		}
   8790  1.1  christos 		length--;
   8791  1.1  christos 		raw++;
   8792  1.1  christos 	}
   8793  1.1  christos 	r.length = length;
   8794  1.1  christos 	r.base = raw;
   8795  1.1  christos 	dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
   8796  1.1  christos 	rdata->flags |= flags;
   8797  1.1  christos }
   8798  1.1  christos 
   8799  1.1  christos static void
   8800  1.1  christos rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
   8801  1.1  christos 	dns_db_t *db = source->private1;
   8802  1.1  christos 	dns_dbnode_t *node = source->private2;
   8803  1.1  christos 	dns_dbnode_t *cloned_node = NULL;
   8804  1.1  christos 
   8805  1.1  christos 	attachnode(db, node, &cloned_node);
   8806  1.1  christos 	INSIST(!ISC_LINK_LINKED(target, link));
   8807  1.1  christos 	*target = *source;
   8808  1.1  christos 	ISC_LINK_INIT(target, link);
   8809  1.1  christos 
   8810  1.1  christos 	/*
   8811  1.1  christos 	 * Reset iterator state.
   8812  1.1  christos 	 */
   8813  1.1  christos 	target->privateuint4 = 0;
   8814  1.1  christos 	target->private5 = NULL;
   8815  1.1  christos }
   8816  1.1  christos 
   8817  1.1  christos static unsigned int
   8818  1.1  christos rdataset_count(dns_rdataset_t *rdataset) {
   8819  1.7  christos 	unsigned char *raw = rdataset->private3; /* RDATASLAB */
   8820  1.1  christos 	unsigned int count;
   8821  1.1  christos 
   8822  1.1  christos 	count = raw[0] * 256 + raw[1];
   8823  1.1  christos 
   8824  1.1  christos 	return (count);
   8825  1.1  christos }
   8826  1.1  christos 
   8827  1.1  christos static isc_result_t
   8828  1.1  christos rdataset_getnoqname(dns_rdataset_t *rdataset, dns_name_t *name,
   8829  1.7  christos 		    dns_rdataset_t *nsec, dns_rdataset_t *nsecsig) {
   8830  1.1  christos 	dns_db_t *db = rdataset->private1;
   8831  1.1  christos 	dns_dbnode_t *node = rdataset->private2;
   8832  1.1  christos 	dns_dbnode_t *cloned_node;
   8833  1.1  christos 	const struct noqname *noqname = rdataset->private6;
   8834  1.1  christos 
   8835  1.1  christos 	cloned_node = NULL;
   8836  1.1  christos 	attachnode(db, node, &cloned_node);
   8837  1.1  christos 	nsec->methods = &slab_methods;
   8838  1.1  christos 	nsec->rdclass = db->rdclass;
   8839  1.1  christos 	nsec->type = noqname->type;
   8840  1.1  christos 	nsec->covers = 0;
   8841  1.1  christos 	nsec->ttl = rdataset->ttl;
   8842  1.1  christos 	nsec->trust = rdataset->trust;
   8843  1.1  christos 	nsec->private1 = rdataset->private1;
   8844  1.1  christos 	nsec->private2 = rdataset->private2;
   8845  1.1  christos 	nsec->private3 = noqname->neg;
   8846  1.1  christos 	nsec->privateuint4 = 0;
   8847  1.1  christos 	nsec->private5 = NULL;
   8848  1.1  christos 	nsec->private6 = NULL;
   8849  1.1  christos 	nsec->private7 = NULL;
   8850  1.1  christos 
   8851  1.1  christos 	cloned_node = NULL;
   8852  1.1  christos 	attachnode(db, node, &cloned_node);
   8853  1.1  christos 	nsecsig->methods = &slab_methods;
   8854  1.1  christos 	nsecsig->rdclass = db->rdclass;
   8855  1.1  christos 	nsecsig->type = dns_rdatatype_rrsig;
   8856  1.1  christos 	nsecsig->covers = noqname->type;
   8857  1.1  christos 	nsecsig->ttl = rdataset->ttl;
   8858  1.1  christos 	nsecsig->trust = rdataset->trust;
   8859  1.1  christos 	nsecsig->private1 = rdataset->private1;
   8860  1.1  christos 	nsecsig->private2 = rdataset->private2;
   8861  1.1  christos 	nsecsig->private3 = noqname->negsig;
   8862  1.1  christos 	nsecsig->privateuint4 = 0;
   8863  1.1  christos 	nsecsig->private5 = NULL;
   8864  1.1  christos 	nsec->private6 = NULL;
   8865  1.1  christos 	nsec->private7 = NULL;
   8866  1.1  christos 
   8867  1.1  christos 	dns_name_clone(&noqname->name, name);
   8868  1.1  christos 
   8869  1.1  christos 	return (ISC_R_SUCCESS);
   8870  1.1  christos }
   8871  1.1  christos 
   8872  1.1  christos static isc_result_t
   8873  1.1  christos rdataset_getclosest(dns_rdataset_t *rdataset, dns_name_t *name,
   8874  1.7  christos 		    dns_rdataset_t *nsec, dns_rdataset_t *nsecsig) {
   8875  1.1  christos 	dns_db_t *db = rdataset->private1;
   8876  1.1  christos 	dns_dbnode_t *node = rdataset->private2;
   8877  1.1  christos 	dns_dbnode_t *cloned_node;
   8878  1.1  christos 	const struct noqname *closest = rdataset->private7;
   8879  1.1  christos 
   8880  1.1  christos 	cloned_node = NULL;
   8881  1.1  christos 	attachnode(db, node, &cloned_node);
   8882  1.1  christos 	nsec->methods = &slab_methods;
   8883  1.1  christos 	nsec->rdclass = db->rdclass;
   8884  1.1  christos 	nsec->type = closest->type;
   8885  1.1  christos 	nsec->covers = 0;
   8886  1.1  christos 	nsec->ttl = rdataset->ttl;
   8887  1.1  christos 	nsec->trust = rdataset->trust;
   8888  1.1  christos 	nsec->private1 = rdataset->private1;
   8889  1.1  christos 	nsec->private2 = rdataset->private2;
   8890  1.1  christos 	nsec->private3 = closest->neg;
   8891  1.1  christos 	nsec->privateuint4 = 0;
   8892  1.1  christos 	nsec->private5 = NULL;
   8893  1.1  christos 	nsec->private6 = NULL;
   8894  1.1  christos 	nsec->private7 = NULL;
   8895  1.1  christos 
   8896  1.1  christos 	cloned_node = NULL;
   8897  1.1  christos 	attachnode(db, node, &cloned_node);
   8898  1.1  christos 	nsecsig->methods = &slab_methods;
   8899  1.1  christos 	nsecsig->rdclass = db->rdclass;
   8900  1.1  christos 	nsecsig->type = dns_rdatatype_rrsig;
   8901  1.1  christos 	nsecsig->covers = closest->type;
   8902  1.1  christos 	nsecsig->ttl = rdataset->ttl;
   8903  1.1  christos 	nsecsig->trust = rdataset->trust;
   8904  1.1  christos 	nsecsig->private1 = rdataset->private1;
   8905  1.1  christos 	nsecsig->private2 = rdataset->private2;
   8906  1.1  christos 	nsecsig->private3 = closest->negsig;
   8907  1.1  christos 	nsecsig->privateuint4 = 0;
   8908  1.1  christos 	nsecsig->private5 = NULL;
   8909  1.1  christos 	nsec->private6 = NULL;
   8910  1.1  christos 	nsec->private7 = NULL;
   8911  1.1  christos 
   8912  1.1  christos 	dns_name_clone(&closest->name, name);
   8913  1.1  christos 
   8914  1.1  christos 	return (ISC_R_SUCCESS);
   8915  1.1  christos }
   8916  1.1  christos 
   8917  1.1  christos static void
   8918  1.1  christos rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust) {
   8919  1.1  christos 	dns_rbtdb_t *rbtdb = rdataset->private1;
   8920  1.1  christos 	dns_rbtnode_t *rbtnode = rdataset->private2;
   8921  1.1  christos 	rdatasetheader_t *header = rdataset->private3;
   8922  1.1  christos 
   8923  1.1  christos 	header--;
   8924  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   8925  1.1  christos 		  isc_rwlocktype_write);
   8926  1.1  christos 	header->trust = rdataset->trust = trust;
   8927  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   8928  1.7  christos 		    isc_rwlocktype_write);
   8929  1.1  christos }
   8930  1.1  christos 
   8931  1.1  christos static void
   8932  1.1  christos rdataset_expire(dns_rdataset_t *rdataset) {
   8933  1.1  christos 	dns_rbtdb_t *rbtdb = rdataset->private1;
   8934  1.1  christos 	dns_rbtnode_t *rbtnode = rdataset->private2;
   8935  1.1  christos 	rdatasetheader_t *header = rdataset->private3;
   8936  1.1  christos 
   8937  1.1  christos 	header--;
   8938  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   8939  1.1  christos 		  isc_rwlocktype_write);
   8940  1.3  christos 	expire_header(rbtdb, header, false, expire_flush);
   8941  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   8942  1.7  christos 		    isc_rwlocktype_write);
   8943  1.1  christos }
   8944  1.1  christos 
   8945  1.1  christos static void
   8946  1.1  christos rdataset_clearprefetch(dns_rdataset_t *rdataset) {
   8947  1.1  christos 	dns_rbtdb_t *rbtdb = rdataset->private1;
   8948  1.1  christos 	dns_rbtnode_t *rbtnode = rdataset->private2;
   8949  1.1  christos 	rdatasetheader_t *header = rdataset->private3;
   8950  1.1  christos 
   8951  1.1  christos 	header--;
   8952  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   8953  1.1  christos 		  isc_rwlocktype_write);
   8954  1.1  christos 	header->attributes &= ~RDATASET_ATTR_PREFETCH;
   8955  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   8956  1.7  christos 		    isc_rwlocktype_write);
   8957  1.1  christos }
   8958  1.1  christos 
   8959  1.1  christos /*
   8960  1.1  christos  * Rdataset Iterator Methods
   8961  1.1  christos  */
   8962  1.1  christos 
   8963  1.1  christos static void
   8964  1.1  christos rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp) {
   8965  1.1  christos 	rbtdb_rdatasetiter_t *rbtiterator;
   8966  1.1  christos 
   8967  1.1  christos 	rbtiterator = (rbtdb_rdatasetiter_t *)(*iteratorp);
   8968  1.1  christos 
   8969  1.7  christos 	if (rbtiterator->common.version != NULL) {
   8970  1.1  christos 		closeversion(rbtiterator->common.db,
   8971  1.3  christos 			     &rbtiterator->common.version, false);
   8972  1.7  christos 	}
   8973  1.1  christos 	detachnode(rbtiterator->common.db, &rbtiterator->common.node);
   8974  1.1  christos 	isc_mem_put(rbtiterator->common.db->mctx, rbtiterator,
   8975  1.1  christos 		    sizeof(*rbtiterator));
   8976  1.1  christos 
   8977  1.1  christos 	*iteratorp = NULL;
   8978  1.1  christos }
   8979  1.1  christos 
   8980  1.1  christos static isc_result_t
   8981  1.1  christos rdatasetiter_first(dns_rdatasetiter_t *iterator) {
   8982  1.1  christos 	rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
   8983  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
   8984  1.1  christos 	dns_rbtnode_t *rbtnode = rbtiterator->common.node;
   8985  1.1  christos 	rbtdb_version_t *rbtversion = rbtiterator->common.version;
   8986  1.1  christos 	rdatasetheader_t *header, *top_next;
   8987  1.1  christos 	rbtdb_serial_t serial;
   8988  1.1  christos 	isc_stdtime_t now;
   8989  1.1  christos 
   8990  1.1  christos 	if (IS_CACHE(rbtdb)) {
   8991  1.1  christos 		serial = 1;
   8992  1.1  christos 		now = rbtiterator->common.now;
   8993  1.1  christos 	} else {
   8994  1.1  christos 		serial = rbtversion->serial;
   8995  1.1  christos 		now = 0;
   8996  1.1  christos 	}
   8997  1.1  christos 
   8998  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   8999  1.1  christos 		  isc_rwlocktype_read);
   9000  1.1  christos 
   9001  1.1  christos 	for (header = rbtnode->data; header != NULL; header = top_next) {
   9002  1.1  christos 		top_next = header->next;
   9003  1.1  christos 		do {
   9004  1.1  christos 			if (header->serial <= serial && !IGNORE(header)) {
   9005  1.1  christos 				/*
   9006  1.1  christos 				 * Is this a "this rdataset doesn't exist"
   9007  1.1  christos 				 * record?  Or is it too old in the cache?
   9008  1.1  christos 				 *
   9009  1.1  christos 				 * Note: unlike everywhere else, we
   9010  1.1  christos 				 * check for now > header->rdh_ttl instead
   9011  1.1  christos 				 * of now >= header->rdh_ttl.  This allows
   9012  1.1  christos 				 * ANY and RRSIG queries for 0 TTL
   9013  1.1  christos 				 * rdatasets to work.
   9014  1.1  christos 				 */
   9015  1.1  christos 				if (NONEXISTENT(header) ||
   9016  1.7  christos 				    (now != 0 &&
   9017  1.7  christos 				     now > header->rdh_ttl +
   9018  1.7  christos 						     rbtdb->serve_stale_ttl))
   9019  1.7  christos 				{
   9020  1.1  christos 					header = NULL;
   9021  1.7  christos 				}
   9022  1.1  christos 				break;
   9023  1.7  christos 			} else {
   9024  1.1  christos 				header = header->down;
   9025  1.7  christos 			}
   9026  1.1  christos 		} while (header != NULL);
   9027  1.7  christos 		if (header != NULL) {
   9028  1.1  christos 			break;
   9029  1.7  christos 		}
   9030  1.1  christos 	}
   9031  1.1  christos 
   9032  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9033  1.1  christos 		    isc_rwlocktype_read);
   9034  1.1  christos 
   9035  1.1  christos 	rbtiterator->current = header;
   9036  1.1  christos 
   9037  1.7  christos 	if (header == NULL) {
   9038  1.1  christos 		return (ISC_R_NOMORE);
   9039  1.7  christos 	}
   9040  1.1  christos 
   9041  1.1  christos 	return (ISC_R_SUCCESS);
   9042  1.1  christos }
   9043  1.1  christos 
   9044  1.1  christos static isc_result_t
   9045  1.1  christos rdatasetiter_next(dns_rdatasetiter_t *iterator) {
   9046  1.1  christos 	rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
   9047  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
   9048  1.1  christos 	dns_rbtnode_t *rbtnode = rbtiterator->common.node;
   9049  1.1  christos 	rbtdb_version_t *rbtversion = rbtiterator->common.version;
   9050  1.1  christos 	rdatasetheader_t *header, *top_next;
   9051  1.1  christos 	rbtdb_serial_t serial;
   9052  1.1  christos 	isc_stdtime_t now;
   9053  1.1  christos 	rbtdb_rdatatype_t type, negtype;
   9054  1.1  christos 	dns_rdatatype_t rdtype, covers;
   9055  1.1  christos 
   9056  1.1  christos 	header = rbtiterator->current;
   9057  1.7  christos 	if (header == NULL) {
   9058  1.1  christos 		return (ISC_R_NOMORE);
   9059  1.7  christos 	}
   9060  1.1  christos 
   9061  1.1  christos 	if (IS_CACHE(rbtdb)) {
   9062  1.1  christos 		serial = 1;
   9063  1.1  christos 		now = rbtiterator->common.now;
   9064  1.1  christos 	} else {
   9065  1.1  christos 		serial = rbtversion->serial;
   9066  1.1  christos 		now = 0;
   9067  1.1  christos 	}
   9068  1.1  christos 
   9069  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9070  1.1  christos 		  isc_rwlocktype_read);
   9071  1.1  christos 
   9072  1.1  christos 	type = header->type;
   9073  1.1  christos 	rdtype = RBTDB_RDATATYPE_BASE(header->type);
   9074  1.1  christos 	if (NEGATIVE(header)) {
   9075  1.1  christos 		covers = RBTDB_RDATATYPE_EXT(header->type);
   9076  1.1  christos 		negtype = RBTDB_RDATATYPE_VALUE(covers, 0);
   9077  1.7  christos 	} else {
   9078  1.1  christos 		negtype = RBTDB_RDATATYPE_VALUE(0, rdtype);
   9079  1.7  christos 	}
   9080  1.1  christos 	for (header = header->next; header != NULL; header = top_next) {
   9081  1.1  christos 		top_next = header->next;
   9082  1.1  christos 		/*
   9083  1.1  christos 		 * If not walking back up the down list.
   9084  1.1  christos 		 */
   9085  1.1  christos 		if (header->type != type && header->type != negtype) {
   9086  1.1  christos 			do {
   9087  1.7  christos 				if (header->serial <= serial && !IGNORE(header))
   9088  1.7  christos 				{
   9089  1.1  christos 					/*
   9090  1.1  christos 					 * Is this a "this rdataset doesn't
   9091  1.1  christos 					 * exist" record?
   9092  1.1  christos 					 *
   9093  1.1  christos 					 * Note: unlike everywhere else, we
   9094  1.1  christos 					 * check for now > header->ttl instead
   9095  1.1  christos 					 * of now >= header->ttl.  This allows
   9096  1.1  christos 					 * ANY and RRSIG queries for 0 TTL
   9097  1.1  christos 					 * rdatasets to work.
   9098  1.1  christos 					 */
   9099  1.1  christos 					if (NONEXISTENT(header) ||
   9100  1.1  christos 					    (now != 0 && now > header->rdh_ttl))
   9101  1.7  christos 					{
   9102  1.1  christos 						header = NULL;
   9103  1.7  christos 					}
   9104  1.1  christos 					break;
   9105  1.7  christos 				} else {
   9106  1.1  christos 					header = header->down;
   9107  1.7  christos 				}
   9108  1.1  christos 			} while (header != NULL);
   9109  1.7  christos 			if (header != NULL) {
   9110  1.1  christos 				break;
   9111  1.7  christos 			}
   9112  1.1  christos 		}
   9113  1.1  christos 	}
   9114  1.1  christos 
   9115  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9116  1.1  christos 		    isc_rwlocktype_read);
   9117  1.1  christos 
   9118  1.1  christos 	rbtiterator->current = header;
   9119  1.1  christos 
   9120  1.7  christos 	if (header == NULL) {
   9121  1.1  christos 		return (ISC_R_NOMORE);
   9122  1.7  christos 	}
   9123  1.1  christos 
   9124  1.1  christos 	return (ISC_R_SUCCESS);
   9125  1.1  christos }
   9126  1.1  christos 
   9127  1.1  christos static void
   9128  1.1  christos rdatasetiter_current(dns_rdatasetiter_t *iterator, dns_rdataset_t *rdataset) {
   9129  1.1  christos 	rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
   9130  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
   9131  1.1  christos 	dns_rbtnode_t *rbtnode = rbtiterator->common.node;
   9132  1.1  christos 	rdatasetheader_t *header;
   9133  1.1  christos 
   9134  1.1  christos 	header = rbtiterator->current;
   9135  1.1  christos 	REQUIRE(header != NULL);
   9136  1.1  christos 
   9137  1.1  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9138  1.1  christos 		  isc_rwlocktype_read);
   9139  1.1  christos 
   9140  1.1  christos 	bind_rdataset(rbtdb, rbtnode, header, rbtiterator->common.now,
   9141  1.1  christos 		      rdataset);
   9142  1.1  christos 
   9143  1.1  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9144  1.1  christos 		    isc_rwlocktype_read);
   9145  1.1  christos }
   9146  1.1  christos 
   9147  1.1  christos /*
   9148  1.1  christos  * Database Iterator Methods
   9149  1.1  christos  */
   9150  1.1  christos 
   9151  1.1  christos static inline void
   9152  1.1  christos reference_iter_node(rbtdb_dbiterator_t *rbtdbiter) {
   9153  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
   9154  1.1  christos 	dns_rbtnode_t *node = rbtdbiter->node;
   9155  1.1  christos 
   9156  1.7  christos 	if (node == NULL) {
   9157  1.1  christos 		return;
   9158  1.7  christos 	}
   9159  1.1  christos 
   9160  1.1  christos 	INSIST(rbtdbiter->tree_locked != isc_rwlocktype_none);
   9161  1.1  christos 	reactivate_node(rbtdb, node, rbtdbiter->tree_locked);
   9162  1.1  christos }
   9163  1.1  christos 
   9164  1.1  christos static inline void
   9165  1.1  christos dereference_iter_node(rbtdb_dbiterator_t *rbtdbiter) {
   9166  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
   9167  1.1  christos 	dns_rbtnode_t *node = rbtdbiter->node;
   9168  1.1  christos 	nodelock_t *lock;
   9169  1.1  christos 
   9170  1.7  christos 	if (node == NULL) {
   9171  1.1  christos 		return;
   9172  1.7  christos 	}
   9173  1.1  christos 
   9174  1.1  christos 	lock = &rbtdb->node_locks[node->locknum].lock;
   9175  1.1  christos 	NODE_LOCK(lock, isc_rwlocktype_read);
   9176  1.1  christos 	decrement_reference(rbtdb, node, 0, isc_rwlocktype_read,
   9177  1.3  christos 			    rbtdbiter->tree_locked, false);
   9178  1.1  christos 	NODE_UNLOCK(lock, isc_rwlocktype_read);
   9179  1.1  christos 
   9180  1.1  christos 	rbtdbiter->node = NULL;
   9181  1.1  christos }
   9182  1.1  christos 
   9183  1.1  christos static void
   9184  1.1  christos flush_deletions(rbtdb_dbiterator_t *rbtdbiter) {
   9185  1.1  christos 	dns_rbtnode_t *node;
   9186  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
   9187  1.3  christos 	bool was_read_locked = false;
   9188  1.1  christos 	nodelock_t *lock;
   9189  1.1  christos 	int i;
   9190  1.1  christos 
   9191  1.1  christos 	if (rbtdbiter->delcnt != 0) {
   9192  1.1  christos 		/*
   9193  1.1  christos 		 * Note that "%d node of %d in tree" can report things like
   9194  1.1  christos 		 * "flush_deletions: 59 nodes of 41 in tree".  This means
   9195  1.1  christos 		 * That some nodes appear on the deletions list more than
   9196  1.7  christos 		 * once.  Only the last occurrence will actually be deleted.
   9197  1.1  christos 		 */
   9198  1.1  christos 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
   9199  1.1  christos 			      DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
   9200  1.1  christos 			      "flush_deletions: %d nodes of %d in tree",
   9201  1.1  christos 			      rbtdbiter->delcnt,
   9202  1.1  christos 			      dns_rbt_nodecount(rbtdb->tree));
   9203  1.1  christos 
   9204  1.1  christos 		if (rbtdbiter->tree_locked == isc_rwlocktype_read) {
   9205  1.1  christos 			RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   9206  1.3  christos 			was_read_locked = true;
   9207  1.1  christos 		}
   9208  1.1  christos 		RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   9209  1.1  christos 		rbtdbiter->tree_locked = isc_rwlocktype_write;
   9210  1.1  christos 
   9211  1.1  christos 		for (i = 0; i < rbtdbiter->delcnt; i++) {
   9212  1.1  christos 			node = rbtdbiter->deletions[i];
   9213  1.1  christos 			lock = &rbtdb->node_locks[node->locknum].lock;
   9214  1.1  christos 
   9215  1.1  christos 			NODE_LOCK(lock, isc_rwlocktype_read);
   9216  1.7  christos 			decrement_reference(rbtdb, node, 0, isc_rwlocktype_read,
   9217  1.3  christos 					    rbtdbiter->tree_locked, false);
   9218  1.1  christos 			NODE_UNLOCK(lock, isc_rwlocktype_read);
   9219  1.1  christos 		}
   9220  1.1  christos 
   9221  1.1  christos 		rbtdbiter->delcnt = 0;
   9222  1.1  christos 
   9223  1.1  christos 		RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
   9224  1.1  christos 		if (was_read_locked) {
   9225  1.1  christos 			RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   9226  1.1  christos 			rbtdbiter->tree_locked = isc_rwlocktype_read;
   9227  1.1  christos 		} else {
   9228  1.1  christos 			rbtdbiter->tree_locked = isc_rwlocktype_none;
   9229  1.1  christos 		}
   9230  1.1  christos 	}
   9231  1.1  christos }
   9232  1.1  christos 
   9233  1.1  christos static inline void
   9234  1.1  christos resume_iteration(rbtdb_dbiterator_t *rbtdbiter) {
   9235  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
   9236  1.1  christos 
   9237  1.1  christos 	REQUIRE(rbtdbiter->paused);
   9238  1.1  christos 	REQUIRE(rbtdbiter->tree_locked == isc_rwlocktype_none);
   9239  1.1  christos 
   9240  1.1  christos 	RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   9241  1.1  christos 	rbtdbiter->tree_locked = isc_rwlocktype_read;
   9242  1.1  christos 
   9243  1.3  christos 	rbtdbiter->paused = false;
   9244  1.1  christos }
   9245  1.1  christos 
   9246  1.1  christos static void
   9247  1.1  christos dbiterator_destroy(dns_dbiterator_t **iteratorp) {
   9248  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)(*iteratorp);
   9249  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
   9250  1.1  christos 	dns_db_t *db = NULL;
   9251  1.1  christos 
   9252  1.1  christos 	if (rbtdbiter->tree_locked == isc_rwlocktype_read) {
   9253  1.1  christos 		RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   9254  1.1  christos 		rbtdbiter->tree_locked = isc_rwlocktype_none;
   9255  1.7  christos 	} else {
   9256  1.1  christos 		INSIST(rbtdbiter->tree_locked == isc_rwlocktype_none);
   9257  1.7  christos 	}
   9258  1.1  christos 
   9259  1.1  christos 	dereference_iter_node(rbtdbiter);
   9260  1.1  christos 
   9261  1.1  christos 	flush_deletions(rbtdbiter);
   9262  1.1  christos 
   9263  1.1  christos 	dns_db_attach(rbtdbiter->common.db, &db);
   9264  1.1  christos 	dns_db_detach(&rbtdbiter->common.db);
   9265  1.1  christos 
   9266  1.1  christos 	dns_rbtnodechain_reset(&rbtdbiter->chain);
   9267  1.1  christos 	dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
   9268  1.1  christos 	isc_mem_put(db->mctx, rbtdbiter, sizeof(*rbtdbiter));
   9269  1.1  christos 	dns_db_detach(&db);
   9270  1.1  christos 
   9271  1.1  christos 	*iteratorp = NULL;
   9272  1.1  christos }
   9273  1.1  christos 
   9274  1.1  christos static isc_result_t
   9275  1.1  christos dbiterator_first(dns_dbiterator_t *iterator) {
   9276  1.1  christos 	isc_result_t result;
   9277  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
   9278  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
   9279  1.1  christos 	dns_name_t *name, *origin;
   9280  1.1  christos 
   9281  1.1  christos 	if (rbtdbiter->result != ISC_R_SUCCESS &&
   9282  1.1  christos 	    rbtdbiter->result != ISC_R_NOTFOUND &&
   9283  1.1  christos 	    rbtdbiter->result != DNS_R_PARTIALMATCH &&
   9284  1.1  christos 	    rbtdbiter->result != ISC_R_NOMORE)
   9285  1.7  christos 	{
   9286  1.1  christos 		return (rbtdbiter->result);
   9287  1.7  christos 	}
   9288  1.1  christos 
   9289  1.7  christos 	if (rbtdbiter->paused) {
   9290  1.1  christos 		resume_iteration(rbtdbiter);
   9291  1.7  christos 	}
   9292  1.1  christos 
   9293  1.1  christos 	dereference_iter_node(rbtdbiter);
   9294  1.1  christos 
   9295  1.1  christos 	name = dns_fixedname_name(&rbtdbiter->name);
   9296  1.1  christos 	origin = dns_fixedname_name(&rbtdbiter->origin);
   9297  1.1  christos 	dns_rbtnodechain_reset(&rbtdbiter->chain);
   9298  1.1  christos 	dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
   9299  1.1  christos 
   9300  1.1  christos 	if (rbtdbiter->nsec3only) {
   9301  1.1  christos 		rbtdbiter->current = &rbtdbiter->nsec3chain;
   9302  1.1  christos 		result = dns_rbtnodechain_first(rbtdbiter->current,
   9303  1.1  christos 						rbtdb->nsec3, name, origin);
   9304  1.1  christos 	} else {
   9305  1.1  christos 		rbtdbiter->current = &rbtdbiter->chain;
   9306  1.7  christos 		result = dns_rbtnodechain_first(rbtdbiter->current, rbtdb->tree,
   9307  1.7  christos 						name, origin);
   9308  1.1  christos 		if (!rbtdbiter->nonsec3 && result == ISC_R_NOTFOUND) {
   9309  1.1  christos 			rbtdbiter->current = &rbtdbiter->nsec3chain;
   9310  1.7  christos 			result = dns_rbtnodechain_first(
   9311  1.7  christos 				rbtdbiter->current, rbtdb->nsec3, name, origin);
   9312  1.1  christos 		}
   9313  1.1  christos 	}
   9314  1.1  christos 	if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
   9315  1.1  christos 		result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
   9316  1.1  christos 						  NULL, &rbtdbiter->node);
   9317  1.1  christos 		if (result == ISC_R_SUCCESS) {
   9318  1.3  christos 			rbtdbiter->new_origin = true;
   9319  1.1  christos 			reference_iter_node(rbtdbiter);
   9320  1.1  christos 		}
   9321  1.1  christos 	} else {
   9322  1.1  christos 		INSIST(result == ISC_R_NOTFOUND);
   9323  1.1  christos 		result = ISC_R_NOMORE; /* The tree is empty. */
   9324  1.1  christos 	}
   9325  1.1  christos 
   9326  1.1  christos 	rbtdbiter->result = result;
   9327  1.1  christos 
   9328  1.7  christos 	if (result != ISC_R_SUCCESS) {
   9329  1.1  christos 		ENSURE(!rbtdbiter->paused);
   9330  1.7  christos 	}
   9331  1.1  christos 
   9332  1.1  christos 	return (result);
   9333  1.1  christos }
   9334  1.1  christos 
   9335  1.1  christos static isc_result_t
   9336  1.1  christos dbiterator_last(dns_dbiterator_t *iterator) {
   9337  1.1  christos 	isc_result_t result;
   9338  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
   9339  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
   9340  1.1  christos 	dns_name_t *name, *origin;
   9341  1.1  christos 
   9342  1.1  christos 	if (rbtdbiter->result != ISC_R_SUCCESS &&
   9343  1.1  christos 	    rbtdbiter->result != ISC_R_NOTFOUND &&
   9344  1.1  christos 	    rbtdbiter->result != DNS_R_PARTIALMATCH &&
   9345  1.1  christos 	    rbtdbiter->result != ISC_R_NOMORE)
   9346  1.7  christos 	{
   9347  1.1  christos 		return (rbtdbiter->result);
   9348  1.7  christos 	}
   9349  1.1  christos 
   9350  1.7  christos 	if (rbtdbiter->paused) {
   9351  1.1  christos 		resume_iteration(rbtdbiter);
   9352  1.7  christos 	}
   9353  1.1  christos 
   9354  1.1  christos 	dereference_iter_node(rbtdbiter);
   9355  1.1  christos 
   9356  1.1  christos 	name = dns_fixedname_name(&rbtdbiter->name);
   9357  1.1  christos 	origin = dns_fixedname_name(&rbtdbiter->origin);
   9358  1.1  christos 	dns_rbtnodechain_reset(&rbtdbiter->chain);
   9359  1.1  christos 	dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
   9360  1.1  christos 
   9361  1.1  christos 	result = ISC_R_NOTFOUND;
   9362  1.1  christos 	if (rbtdbiter->nsec3only && !rbtdbiter->nonsec3) {
   9363  1.1  christos 		rbtdbiter->current = &rbtdbiter->nsec3chain;
   9364  1.7  christos 		result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->nsec3,
   9365  1.7  christos 					       name, origin);
   9366  1.1  christos 	}
   9367  1.1  christos 	if (!rbtdbiter->nsec3only && result == ISC_R_NOTFOUND) {
   9368  1.1  christos 		rbtdbiter->current = &rbtdbiter->chain;
   9369  1.1  christos 		result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->tree,
   9370  1.1  christos 					       name, origin);
   9371  1.1  christos 	}
   9372  1.1  christos 	if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
   9373  1.1  christos 		result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
   9374  1.1  christos 						  NULL, &rbtdbiter->node);
   9375  1.1  christos 		if (result == ISC_R_SUCCESS) {
   9376  1.3  christos 			rbtdbiter->new_origin = true;
   9377  1.1  christos 			reference_iter_node(rbtdbiter);
   9378  1.1  christos 		}
   9379  1.1  christos 	} else {
   9380  1.1  christos 		INSIST(result == ISC_R_NOTFOUND);
   9381  1.1  christos 		result = ISC_R_NOMORE; /* The tree is empty. */
   9382  1.1  christos 	}
   9383  1.1  christos 
   9384  1.1  christos 	rbtdbiter->result = result;
   9385  1.1  christos 
   9386  1.1  christos 	return (result);
   9387  1.1  christos }
   9388  1.1  christos 
   9389  1.1  christos static isc_result_t
   9390  1.1  christos dbiterator_seek(dns_dbiterator_t *iterator, const dns_name_t *name) {
   9391  1.1  christos 	isc_result_t result, tresult;
   9392  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
   9393  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
   9394  1.1  christos 	dns_name_t *iname, *origin;
   9395  1.1  christos 
   9396  1.1  christos 	if (rbtdbiter->result != ISC_R_SUCCESS &&
   9397  1.1  christos 	    rbtdbiter->result != ISC_R_NOTFOUND &&
   9398  1.1  christos 	    rbtdbiter->result != DNS_R_PARTIALMATCH &&
   9399  1.1  christos 	    rbtdbiter->result != ISC_R_NOMORE)
   9400  1.7  christos 	{
   9401  1.1  christos 		return (rbtdbiter->result);
   9402  1.7  christos 	}
   9403  1.1  christos 
   9404  1.7  christos 	if (rbtdbiter->paused) {
   9405  1.1  christos 		resume_iteration(rbtdbiter);
   9406  1.7  christos 	}
   9407  1.1  christos 
   9408  1.1  christos 	dereference_iter_node(rbtdbiter);
   9409  1.1  christos 
   9410  1.1  christos 	iname = dns_fixedname_name(&rbtdbiter->name);
   9411  1.1  christos 	origin = dns_fixedname_name(&rbtdbiter->origin);
   9412  1.1  christos 	dns_rbtnodechain_reset(&rbtdbiter->chain);
   9413  1.1  christos 	dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
   9414  1.1  christos 
   9415  1.1  christos 	if (rbtdbiter->nsec3only) {
   9416  1.1  christos 		rbtdbiter->current = &rbtdbiter->nsec3chain;
   9417  1.1  christos 		result = dns_rbt_findnode(rbtdb->nsec3, name, NULL,
   9418  1.7  christos 					  &rbtdbiter->node, rbtdbiter->current,
   9419  1.1  christos 					  DNS_RBTFIND_EMPTYDATA, NULL, NULL);
   9420  1.1  christos 	} else if (rbtdbiter->nonsec3) {
   9421  1.1  christos 		rbtdbiter->current = &rbtdbiter->chain;
   9422  1.1  christos 		result = dns_rbt_findnode(rbtdb->tree, name, NULL,
   9423  1.7  christos 					  &rbtdbiter->node, rbtdbiter->current,
   9424  1.1  christos 					  DNS_RBTFIND_EMPTYDATA, NULL, NULL);
   9425  1.1  christos 	} else {
   9426  1.1  christos 		/*
   9427  1.1  christos 		 * Stay on main chain if not found on either chain.
   9428  1.1  christos 		 */
   9429  1.1  christos 		rbtdbiter->current = &rbtdbiter->chain;
   9430  1.1  christos 		result = dns_rbt_findnode(rbtdb->tree, name, NULL,
   9431  1.7  christos 					  &rbtdbiter->node, rbtdbiter->current,
   9432  1.1  christos 					  DNS_RBTFIND_EMPTYDATA, NULL, NULL);
   9433  1.1  christos 		if (result == DNS_R_PARTIALMATCH) {
   9434  1.1  christos 			dns_rbtnode_t *node = NULL;
   9435  1.7  christos 			tresult = dns_rbt_findnode(
   9436  1.7  christos 				rbtdb->nsec3, name, NULL, &node,
   9437  1.7  christos 				&rbtdbiter->nsec3chain, DNS_RBTFIND_EMPTYDATA,
   9438  1.7  christos 				NULL, NULL);
   9439  1.1  christos 			if (tresult == ISC_R_SUCCESS) {
   9440  1.1  christos 				rbtdbiter->node = node;
   9441  1.1  christos 				rbtdbiter->current = &rbtdbiter->nsec3chain;
   9442  1.1  christos 				result = tresult;
   9443  1.1  christos 			}
   9444  1.1  christos 		}
   9445  1.1  christos 	}
   9446  1.1  christos 
   9447  1.1  christos 	if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
   9448  1.1  christos 		tresult = dns_rbtnodechain_current(rbtdbiter->current, iname,
   9449  1.1  christos 						   origin, NULL);
   9450  1.1  christos 		if (tresult == ISC_R_SUCCESS) {
   9451  1.3  christos 			rbtdbiter->new_origin = true;
   9452  1.1  christos 			reference_iter_node(rbtdbiter);
   9453  1.1  christos 		} else {
   9454  1.1  christos 			result = tresult;
   9455  1.1  christos 			rbtdbiter->node = NULL;
   9456  1.1  christos 		}
   9457  1.7  christos 	} else {
   9458  1.1  christos 		rbtdbiter->node = NULL;
   9459  1.7  christos 	}
   9460  1.1  christos 
   9461  1.7  christos 	rbtdbiter->result = (result == DNS_R_PARTIALMATCH) ? ISC_R_SUCCESS
   9462  1.7  christos 							   : result;
   9463  1.1  christos 
   9464  1.1  christos 	return (result);
   9465  1.1  christos }
   9466  1.1  christos 
   9467  1.1  christos static isc_result_t
   9468  1.1  christos dbiterator_prev(dns_dbiterator_t *iterator) {
   9469  1.1  christos 	isc_result_t result;
   9470  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
   9471  1.1  christos 	dns_name_t *name, *origin;
   9472  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
   9473  1.1  christos 
   9474  1.1  christos 	REQUIRE(rbtdbiter->node != NULL);
   9475  1.1  christos 
   9476  1.7  christos 	if (rbtdbiter->result != ISC_R_SUCCESS) {
   9477  1.1  christos 		return (rbtdbiter->result);
   9478  1.7  christos 	}
   9479  1.1  christos 
   9480  1.7  christos 	if (rbtdbiter->paused) {
   9481  1.1  christos 		resume_iteration(rbtdbiter);
   9482  1.7  christos 	}
   9483  1.1  christos 
   9484  1.1  christos 	name = dns_fixedname_name(&rbtdbiter->name);
   9485  1.1  christos 	origin = dns_fixedname_name(&rbtdbiter->origin);
   9486  1.1  christos 	result = dns_rbtnodechain_prev(rbtdbiter->current, name, origin);
   9487  1.1  christos 	if (result == ISC_R_NOMORE && !rbtdbiter->nsec3only &&
   9488  1.7  christos 	    !rbtdbiter->nonsec3 && &rbtdbiter->nsec3chain == rbtdbiter->current)
   9489  1.7  christos 	{
   9490  1.1  christos 		rbtdbiter->current = &rbtdbiter->chain;
   9491  1.1  christos 		dns_rbtnodechain_reset(rbtdbiter->current);
   9492  1.1  christos 		result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->tree,
   9493  1.1  christos 					       name, origin);
   9494  1.7  christos 		if (result == ISC_R_NOTFOUND) {
   9495  1.1  christos 			result = ISC_R_NOMORE;
   9496  1.7  christos 		}
   9497  1.1  christos 	}
   9498  1.1  christos 
   9499  1.1  christos 	dereference_iter_node(rbtdbiter);
   9500  1.1  christos 
   9501  1.1  christos 	if (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
   9502  1.3  christos 		rbtdbiter->new_origin = (result == DNS_R_NEWORIGIN);
   9503  1.1  christos 		result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
   9504  1.1  christos 						  NULL, &rbtdbiter->node);
   9505  1.1  christos 	}
   9506  1.1  christos 
   9507  1.7  christos 	if (result == ISC_R_SUCCESS) {
   9508  1.1  christos 		reference_iter_node(rbtdbiter);
   9509  1.7  christos 	}
   9510  1.1  christos 
   9511  1.1  christos 	rbtdbiter->result = result;
   9512  1.1  christos 
   9513  1.1  christos 	return (result);
   9514  1.1  christos }
   9515  1.1  christos 
   9516  1.1  christos static isc_result_t
   9517  1.1  christos dbiterator_next(dns_dbiterator_t *iterator) {
   9518  1.1  christos 	isc_result_t result;
   9519  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
   9520  1.1  christos 	dns_name_t *name, *origin;
   9521  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
   9522  1.1  christos 
   9523  1.1  christos 	REQUIRE(rbtdbiter->node != NULL);
   9524  1.1  christos 
   9525  1.7  christos 	if (rbtdbiter->result != ISC_R_SUCCESS) {
   9526  1.1  christos 		return (rbtdbiter->result);
   9527  1.7  christos 	}
   9528  1.1  christos 
   9529  1.7  christos 	if (rbtdbiter->paused) {
   9530  1.1  christos 		resume_iteration(rbtdbiter);
   9531  1.7  christos 	}
   9532  1.1  christos 
   9533  1.1  christos 	name = dns_fixedname_name(&rbtdbiter->name);
   9534  1.1  christos 	origin = dns_fixedname_name(&rbtdbiter->origin);
   9535  1.1  christos 	result = dns_rbtnodechain_next(rbtdbiter->current, name, origin);
   9536  1.1  christos 	if (result == ISC_R_NOMORE && !rbtdbiter->nsec3only &&
   9537  1.7  christos 	    !rbtdbiter->nonsec3 && &rbtdbiter->chain == rbtdbiter->current)
   9538  1.7  christos 	{
   9539  1.1  christos 		rbtdbiter->current = &rbtdbiter->nsec3chain;
   9540  1.1  christos 		dns_rbtnodechain_reset(rbtdbiter->current);
   9541  1.1  christos 		result = dns_rbtnodechain_first(rbtdbiter->current,
   9542  1.1  christos 						rbtdb->nsec3, name, origin);
   9543  1.7  christos 		if (result == ISC_R_NOTFOUND) {
   9544  1.1  christos 			result = ISC_R_NOMORE;
   9545  1.7  christos 		}
   9546  1.1  christos 	}
   9547  1.1  christos 
   9548  1.1  christos 	dereference_iter_node(rbtdbiter);
   9549  1.1  christos 
   9550  1.1  christos 	if (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
   9551  1.3  christos 		rbtdbiter->new_origin = (result == DNS_R_NEWORIGIN);
   9552  1.1  christos 		result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
   9553  1.1  christos 						  NULL, &rbtdbiter->node);
   9554  1.1  christos 	}
   9555  1.7  christos 	if (result == ISC_R_SUCCESS) {
   9556  1.1  christos 		reference_iter_node(rbtdbiter);
   9557  1.7  christos 	}
   9558  1.1  christos 
   9559  1.1  christos 	rbtdbiter->result = result;
   9560  1.1  christos 
   9561  1.1  christos 	return (result);
   9562  1.1  christos }
   9563  1.1  christos 
   9564  1.1  christos static isc_result_t
   9565  1.1  christos dbiterator_current(dns_dbiterator_t *iterator, dns_dbnode_t **nodep,
   9566  1.7  christos 		   dns_name_t *name) {
   9567  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
   9568  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
   9569  1.1  christos 	dns_rbtnode_t *node = rbtdbiter->node;
   9570  1.1  christos 	isc_result_t result;
   9571  1.1  christos 	dns_name_t *nodename = dns_fixedname_name(&rbtdbiter->name);
   9572  1.1  christos 	dns_name_t *origin = dns_fixedname_name(&rbtdbiter->origin);
   9573  1.1  christos 
   9574  1.1  christos 	REQUIRE(rbtdbiter->result == ISC_R_SUCCESS);
   9575  1.1  christos 	REQUIRE(rbtdbiter->node != NULL);
   9576  1.1  christos 
   9577  1.7  christos 	if (rbtdbiter->paused) {
   9578  1.1  christos 		resume_iteration(rbtdbiter);
   9579  1.7  christos 	}
   9580  1.1  christos 
   9581  1.1  christos 	if (name != NULL) {
   9582  1.7  christos 		if (rbtdbiter->common.relative_names) {
   9583  1.1  christos 			origin = NULL;
   9584  1.7  christos 		}
   9585  1.1  christos 		result = dns_name_concatenate(nodename, origin, name, NULL);
   9586  1.7  christos 		if (result != ISC_R_SUCCESS) {
   9587  1.1  christos 			return (result);
   9588  1.7  christos 		}
   9589  1.7  christos 		if (rbtdbiter->common.relative_names && rbtdbiter->new_origin) {
   9590  1.1  christos 			result = DNS_R_NEWORIGIN;
   9591  1.7  christos 		}
   9592  1.7  christos 	} else {
   9593  1.1  christos 		result = ISC_R_SUCCESS;
   9594  1.7  christos 	}
   9595  1.1  christos 
   9596  1.1  christos 	new_reference(rbtdb, node);
   9597  1.1  christos 
   9598  1.1  christos 	*nodep = rbtdbiter->node;
   9599  1.1  christos 
   9600  1.1  christos 	if (iterator->cleaning && result == ISC_R_SUCCESS) {
   9601  1.1  christos 		isc_result_t expire_result;
   9602  1.1  christos 
   9603  1.1  christos 		/*
   9604  1.1  christos 		 * If the deletion array is full, flush it before trying
   9605  1.1  christos 		 * to expire the current node.  The current node can't
   9606  1.1  christos 		 * fully deleted while the iteration cursor is still on it.
   9607  1.1  christos 		 */
   9608  1.7  christos 		if (rbtdbiter->delcnt == DELETION_BATCH_MAX) {
   9609  1.1  christos 			flush_deletions(rbtdbiter);
   9610  1.7  christos 		}
   9611  1.1  christos 
   9612  1.1  christos 		expire_result = expirenode(iterator->db, *nodep, 0);
   9613  1.1  christos 
   9614  1.1  christos 		/*
   9615  1.1  christos 		 * expirenode() currently always returns success.
   9616  1.1  christos 		 */
   9617  1.1  christos 		if (expire_result == ISC_R_SUCCESS && node->down == NULL) {
   9618  1.1  christos 			rbtdbiter->deletions[rbtdbiter->delcnt++] = node;
   9619  1.3  christos 			isc_refcount_increment(&node->references);
   9620  1.1  christos 		}
   9621  1.1  christos 	}
   9622  1.1  christos 
   9623  1.1  christos 	return (result);
   9624  1.1  christos }
   9625  1.1  christos 
   9626  1.1  christos static isc_result_t
   9627  1.1  christos dbiterator_pause(dns_dbiterator_t *iterator) {
   9628  1.1  christos 	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
   9629  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
   9630  1.1  christos 
   9631  1.1  christos 	if (rbtdbiter->result != ISC_R_SUCCESS &&
   9632  1.1  christos 	    rbtdbiter->result != ISC_R_NOTFOUND &&
   9633  1.1  christos 	    rbtdbiter->result != DNS_R_PARTIALMATCH &&
   9634  1.1  christos 	    rbtdbiter->result != ISC_R_NOMORE)
   9635  1.7  christos 	{
   9636  1.1  christos 		return (rbtdbiter->result);
   9637  1.7  christos 	}
   9638  1.1  christos 
   9639  1.7  christos 	if (rbtdbiter->paused) {
   9640  1.1  christos 		return (ISC_R_SUCCESS);
   9641  1.7  christos 	}
   9642  1.1  christos 
   9643  1.3  christos 	rbtdbiter->paused = true;
   9644  1.1  christos 
   9645  1.1  christos 	if (rbtdbiter->tree_locked != isc_rwlocktype_none) {
   9646  1.1  christos 		INSIST(rbtdbiter->tree_locked == isc_rwlocktype_read);
   9647  1.1  christos 		RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
   9648  1.1  christos 		rbtdbiter->tree_locked = isc_rwlocktype_none;
   9649  1.1  christos 	}
   9650  1.1  christos 
   9651  1.1  christos 	flush_deletions(rbtdbiter);
   9652  1.1  christos 
   9653  1.1  christos 	return (ISC_R_SUCCESS);
   9654  1.1  christos }
   9655  1.1  christos 
   9656  1.1  christos static isc_result_t
   9657  1.1  christos dbiterator_origin(dns_dbiterator_t *iterator, dns_name_t *name) {
   9658  1.1  christos 	rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
   9659  1.1  christos 	dns_name_t *origin = dns_fixedname_name(&rbtdbiter->origin);
   9660  1.1  christos 
   9661  1.7  christos 	if (rbtdbiter->result != ISC_R_SUCCESS) {
   9662  1.1  christos 		return (rbtdbiter->result);
   9663  1.7  christos 	}
   9664  1.1  christos 
   9665  1.6  christos 	dns_name_copynf(origin, name);
   9666  1.6  christos 	return (ISC_R_SUCCESS);
   9667  1.1  christos }
   9668  1.1  christos 
   9669  1.1  christos static void
   9670  1.1  christos setownercase(rdatasetheader_t *header, const dns_name_t *name) {
   9671  1.1  christos 	unsigned int i;
   9672  1.3  christos 	bool fully_lower;
   9673  1.1  christos 
   9674  1.1  christos 	/*
   9675  1.1  christos 	 * We do not need to worry about label lengths as they are all
   9676  1.1  christos 	 * less than or equal to 63.
   9677  1.1  christos 	 */
   9678  1.1  christos 	memset(header->upper, 0, sizeof(header->upper));
   9679  1.3  christos 	fully_lower = true;
   9680  1.7  christos 	for (i = 0; i < name->length; i++) {
   9681  1.1  christos 		if (name->ndata[i] >= 0x41 && name->ndata[i] <= 0x5a) {
   9682  1.7  christos 			{
   9683  1.7  christos 				header->upper[i / 8] |= 1 << (i % 8);
   9684  1.7  christos 				fully_lower = false;
   9685  1.7  christos 			}
   9686  1.1  christos 		}
   9687  1.7  christos 	}
   9688  1.1  christos 	header->attributes |= RDATASET_ATTR_CASESET;
   9689  1.7  christos 	if (ISC_LIKELY(fully_lower)) {
   9690  1.1  christos 		header->attributes |= RDATASET_ATTR_CASEFULLYLOWER;
   9691  1.7  christos 	}
   9692  1.1  christos }
   9693  1.1  christos 
   9694  1.1  christos static void
   9695  1.1  christos rdataset_setownercase(dns_rdataset_t *rdataset, const dns_name_t *name) {
   9696  1.7  christos 	dns_rbtdb_t *rbtdb = rdataset->private1;
   9697  1.7  christos 	dns_rbtnode_t *rbtnode = rdataset->private2;
   9698  1.7  christos 	unsigned char *raw = rdataset->private3; /* RDATASLAB */
   9699  1.1  christos 	rdatasetheader_t *header;
   9700  1.1  christos 
   9701  1.1  christos 	header = (struct rdatasetheader *)(raw - sizeof(*header));
   9702  1.7  christos 
   9703  1.7  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9704  1.7  christos 		  isc_rwlocktype_write);
   9705  1.1  christos 	setownercase(header, name);
   9706  1.7  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9707  1.7  christos 		    isc_rwlocktype_write);
   9708  1.1  christos }
   9709  1.1  christos 
   9710  1.1  christos static const unsigned char charmask[] = {
   9711  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9712  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9713  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9714  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9715  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9716  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
   9717  1.7  christos 	0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
   9718  1.7  christos 	0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
   9719  1.7  christos 	0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
   9720  1.7  christos 	0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
   9721  1.7  christos 	0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9722  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9723  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9724  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9725  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9726  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9727  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9728  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9729  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9730  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9731  1.7  christos 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   9732  1.7  christos 	0x00, 0x00, 0x00, 0x00
   9733  1.1  christos };
   9734  1.1  christos 
   9735  1.1  christos static const unsigned char maptolower[] = {
   9736  1.7  christos 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
   9737  1.7  christos 	0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
   9738  1.7  christos 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
   9739  1.7  christos 	0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
   9740  1.7  christos 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
   9741  1.7  christos 	0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
   9742  1.7  christos 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
   9743  1.7  christos 	0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
   9744  1.7  christos 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
   9745  1.7  christos 	0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
   9746  1.7  christos 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
   9747  1.7  christos 	0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
   9748  1.7  christos 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
   9749  1.7  christos 	0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
   9750  1.7  christos 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
   9751  1.7  christos 	0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
   9752  1.7  christos 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
   9753  1.7  christos 	0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
   9754  1.7  christos 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
   9755  1.7  christos 	0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
   9756  1.7  christos 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
   9757  1.7  christos 	0xfc, 0xfd, 0xfe, 0xff
   9758  1.1  christos };
   9759  1.1  christos 
   9760  1.1  christos static void
   9761  1.1  christos rdataset_getownercase(const dns_rdataset_t *rdataset, dns_name_t *name) {
   9762  1.7  christos 	dns_rbtdb_t *rbtdb = rdataset->private1;
   9763  1.7  christos 	dns_rbtnode_t *rbtnode = rdataset->private2;
   9764  1.7  christos 	const unsigned char *raw = rdataset->private3; /* RDATASLAB */
   9765  1.1  christos 	const rdatasetheader_t *header;
   9766  1.1  christos 	unsigned int i, j;
   9767  1.1  christos 	unsigned char bits;
   9768  1.1  christos 	unsigned char c, flip;
   9769  1.1  christos 
   9770  1.1  christos 	header = (const struct rdatasetheader *)(raw - sizeof(*header));
   9771  1.1  christos 
   9772  1.7  christos 	NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9773  1.7  christos 		  isc_rwlocktype_read);
   9774  1.7  christos 
   9775  1.7  christos 	if (!CASESET(header)) {
   9776  1.7  christos 		goto unlock;
   9777  1.7  christos 	}
   9778  1.1  christos 
   9779  1.1  christos #if 0
   9780  1.1  christos 	/*
   9781  1.1  christos 	 * This was the original code, and is implemented differently in
   9782  1.1  christos 	 * the #else block that follows.
   9783  1.1  christos 	 */
   9784  1.1  christos 	for (i = 0; i < name->length; i++) {
   9785  1.1  christos 		/*
   9786  1.1  christos 		 * Set the case bit if it does not match the recorded bit.
   9787  1.1  christos 		 */
   9788  1.1  christos 		if (name->ndata[i] >= 0x61 && name->ndata[i] <= 0x7a &&
   9789  1.7  christos 		    (header->upper[i / 8] & (1 << (i % 8))) != 0)
   9790  1.7  christos 		{
   9791  1.1  christos 			name->ndata[i] &= ~0x20; /* clear the lower case bit */
   9792  1.7  christos 		} else if (name->ndata[i] >= 0x41 && name->ndata[i] <= 0x5a &&
   9793  1.7  christos 			   (header->upper[i / 8] & (1 << (i % 8))) == 0)
   9794  1.7  christos 		{
   9795  1.1  christos 			name->ndata[i] |= 0x20; /* set the lower case bit */
   9796  1.7  christos 		}
   9797  1.1  christos 	}
   9798  1.7  christos #else  /* if 0 */
   9799  1.1  christos 
   9800  1.1  christos 	if (ISC_LIKELY(CASEFULLYLOWER(header))) {
   9801  1.1  christos 		unsigned char *bp, *be;
   9802  1.1  christos 		bp = name->ndata;
   9803  1.1  christos 		be = bp + name->length;
   9804  1.1  christos 
   9805  1.1  christos 		while (bp <= be - 4) {
   9806  1.1  christos 			c = bp[0];
   9807  1.1  christos 			bp[0] = maptolower[c];
   9808  1.1  christos 			c = bp[1];
   9809  1.1  christos 			bp[1] = maptolower[c];
   9810  1.1  christos 			c = bp[2];
   9811  1.1  christos 			bp[2] = maptolower[c];
   9812  1.1  christos 			c = bp[3];
   9813  1.1  christos 			bp[3] = maptolower[c];
   9814  1.1  christos 			bp += 4;
   9815  1.1  christos 		}
   9816  1.1  christos 		while (bp < be) {
   9817  1.1  christos 			c = *bp;
   9818  1.1  christos 			*bp++ = maptolower[c];
   9819  1.1  christos 		}
   9820  1.7  christos 		goto unlock;
   9821  1.1  christos 	}
   9822  1.1  christos 
   9823  1.1  christos 	i = 0;
   9824  1.1  christos 	for (j = 0; j < (name->length >> 3); j++) {
   9825  1.1  christos 		unsigned int k;
   9826  1.1  christos 
   9827  1.1  christos 		bits = ~(header->upper[j]);
   9828  1.1  christos 
   9829  1.1  christos 		for (k = 0; k < 8; k++) {
   9830  1.1  christos 			c = name->ndata[i];
   9831  1.1  christos 			flip = (bits & 1) << 5;
   9832  1.1  christos 			flip ^= c;
   9833  1.1  christos 			flip &= charmask[c];
   9834  1.1  christos 			name->ndata[i] ^= flip;
   9835  1.1  christos 
   9836  1.1  christos 			i++;
   9837  1.1  christos 			bits >>= 1;
   9838  1.1  christos 		}
   9839  1.1  christos 	}
   9840  1.1  christos 
   9841  1.7  christos 	if (ISC_UNLIKELY(i == name->length)) {
   9842  1.7  christos 		goto unlock;
   9843  1.7  christos 	}
   9844  1.1  christos 
   9845  1.1  christos 	bits = ~(header->upper[j]);
   9846  1.1  christos 
   9847  1.1  christos 	for (; i < name->length; i++) {
   9848  1.1  christos 		c = name->ndata[i];
   9849  1.1  christos 		flip = (bits & 1) << 5;
   9850  1.1  christos 		flip ^= c;
   9851  1.1  christos 		flip &= charmask[c];
   9852  1.1  christos 		name->ndata[i] ^= flip;
   9853  1.1  christos 
   9854  1.1  christos 		bits >>= 1;
   9855  1.1  christos 	}
   9856  1.7  christos #endif /* if 0 */
   9857  1.7  christos 
   9858  1.7  christos unlock:
   9859  1.7  christos 	NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
   9860  1.7  christos 		    isc_rwlocktype_read);
   9861  1.1  christos }
   9862  1.1  christos 
   9863  1.1  christos struct rbtdb_glue {
   9864  1.1  christos 	struct rbtdb_glue *next;
   9865  1.1  christos 	dns_fixedname_t fixedname;
   9866  1.1  christos 	dns_rdataset_t rdataset_a;
   9867  1.1  christos 	dns_rdataset_t sigrdataset_a;
   9868  1.1  christos 	dns_rdataset_t rdataset_aaaa;
   9869  1.1  christos 	dns_rdataset_t sigrdataset_aaaa;
   9870  1.1  christos };
   9871  1.1  christos 
   9872  1.1  christos typedef struct {
   9873  1.1  christos 	rbtdb_glue_t *glue_list;
   9874  1.1  christos 	dns_rbtdb_t *rbtdb;
   9875  1.1  christos 	rbtdb_version_t *rbtversion;
   9876  1.1  christos } rbtdb_glue_additionaldata_ctx_t;
   9877  1.1  christos 
   9878  1.1  christos static void
   9879  1.1  christos free_gluelist(rbtdb_glue_t *glue_list, dns_rbtdb_t *rbtdb) {
   9880  1.1  christos 	rbtdb_glue_t *cur, *cur_next;
   9881  1.1  christos 
   9882  1.7  christos 	if (glue_list == (void *)-1) {
   9883  1.1  christos 		return;
   9884  1.7  christos 	}
   9885  1.1  christos 
   9886  1.1  christos 	cur = glue_list;
   9887  1.1  christos 	while (cur != NULL) {
   9888  1.1  christos 		cur_next = cur->next;
   9889  1.1  christos 
   9890  1.7  christos 		if (dns_rdataset_isassociated(&cur->rdataset_a)) {
   9891  1.1  christos 			dns_rdataset_disassociate(&cur->rdataset_a);
   9892  1.7  christos 		}
   9893  1.7  christos 		if (dns_rdataset_isassociated(&cur->sigrdataset_a)) {
   9894  1.1  christos 			dns_rdataset_disassociate(&cur->sigrdataset_a);
   9895  1.7  christos 		}
   9896  1.1  christos 
   9897  1.7  christos 		if (dns_rdataset_isassociated(&cur->rdataset_aaaa)) {
   9898  1.1  christos 			dns_rdataset_disassociate(&cur->rdataset_aaaa);
   9899  1.7  christos 		}
   9900  1.7  christos 		if (dns_rdataset_isassociated(&cur->sigrdataset_aaaa)) {
   9901  1.1  christos 			dns_rdataset_disassociate(&cur->sigrdataset_aaaa);
   9902  1.7  christos 		}
   9903  1.1  christos 
   9904  1.1  christos 		dns_rdataset_invalidate(&cur->rdataset_a);
   9905  1.1  christos 		dns_rdataset_invalidate(&cur->sigrdataset_a);
   9906  1.1  christos 		dns_rdataset_invalidate(&cur->rdataset_aaaa);
   9907  1.1  christos 		dns_rdataset_invalidate(&cur->sigrdataset_aaaa);
   9908  1.1  christos 
   9909  1.1  christos 		isc_mem_put(rbtdb->common.mctx, cur, sizeof(*cur));
   9910  1.1  christos 		cur = cur_next;
   9911  1.1  christos 	}
   9912  1.1  christos }
   9913  1.1  christos 
   9914  1.1  christos static void
   9915  1.1  christos free_gluetable(rbtdb_version_t *version) {
   9916  1.1  christos 	dns_rbtdb_t *rbtdb;
   9917  1.1  christos 	size_t i;
   9918  1.1  christos 
   9919  1.1  christos 	RWLOCK(&version->glue_rwlock, isc_rwlocktype_write);
   9920  1.1  christos 
   9921  1.1  christos 	rbtdb = version->rbtdb;
   9922  1.1  christos 
   9923  1.1  christos 	for (i = 0; i < version->glue_table_size; i++) {
   9924  1.1  christos 		rbtdb_glue_table_node_t *cur, *cur_next;
   9925  1.1  christos 
   9926  1.1  christos 		cur = version->glue_table[i];
   9927  1.1  christos 		while (cur != NULL) {
   9928  1.1  christos 			cur_next = cur->next;
   9929  1.3  christos 			/* isc_refcount_decrement(&cur->node->references); */
   9930  1.1  christos 			cur->node = NULL;
   9931  1.1  christos 			free_gluelist(cur->glue_list, rbtdb);
   9932  1.1  christos 			cur->glue_list = NULL;
   9933  1.1  christos 			isc_mem_put(rbtdb->common.mctx, cur, sizeof(*cur));
   9934  1.1  christos 			cur = cur_next;
   9935  1.1  christos 		}
   9936  1.1  christos 		version->glue_table[i] = NULL;
   9937  1.1  christos 	}
   9938  1.1  christos 
   9939  1.1  christos 	isc_mem_put(rbtdb->common.mctx, version->glue_table,
   9940  1.7  christos 		    (sizeof(*version->glue_table) * version->glue_table_size));
   9941  1.1  christos 
   9942  1.1  christos 	RWUNLOCK(&version->glue_rwlock, isc_rwlocktype_write);
   9943  1.1  christos }
   9944  1.1  christos 
   9945  1.3  christos static bool
   9946  1.1  christos rehash_gluetable(rbtdb_version_t *version) {
   9947  1.1  christos 	size_t oldsize, i;
   9948  1.1  christos 	rbtdb_glue_table_node_t **oldtable;
   9949  1.1  christos 	rbtdb_glue_table_node_t *gluenode;
   9950  1.1  christos 	rbtdb_glue_table_node_t *nextgluenode;
   9951  1.3  christos 	uint32_t hash;
   9952  1.1  christos 
   9953  1.1  christos 	if (ISC_LIKELY(version->glue_table_nodecount <
   9954  1.7  christos 		       (version->glue_table_size * 3U))) {
   9955  1.3  christos 		return (false);
   9956  1.7  christos 	}
   9957  1.1  christos 
   9958  1.1  christos 	oldsize = version->glue_table_size;
   9959  1.1  christos 	oldtable = version->glue_table;
   9960  1.1  christos 	do {
   9961  1.1  christos 		INSIST((version->glue_table_size * 2 + 1) >
   9962  1.1  christos 		       version->glue_table_size);
   9963  1.1  christos 		version->glue_table_size = version->glue_table_size * 2 + 1;
   9964  1.1  christos 	} while (version->glue_table_nodecount >=
   9965  1.1  christos 		 (version->glue_table_size * 3U));
   9966  1.1  christos 
   9967  1.7  christos 	version->glue_table = isc_mem_get(
   9968  1.7  christos 		version->rbtdb->common.mctx,
   9969  1.7  christos 		(version->glue_table_size * sizeof(*version->glue_table)));
   9970  1.1  christos 	if (ISC_UNLIKELY(version->glue_table == NULL)) {
   9971  1.1  christos 		version->glue_table = oldtable;
   9972  1.1  christos 		version->glue_table_size = oldsize;
   9973  1.3  christos 		return (false);
   9974  1.1  christos 	}
   9975  1.1  christos 
   9976  1.7  christos 	for (i = 0; i < version->glue_table_size; i++) {
   9977  1.1  christos 		version->glue_table[i] = NULL;
   9978  1.7  christos 	}
   9979  1.1  christos 
   9980  1.1  christos 	for (i = 0; i < oldsize; i++) {
   9981  1.7  christos 		for (gluenode = oldtable[i]; gluenode != NULL;
   9982  1.7  christos 		     gluenode = nextgluenode) {
   9983  1.1  christos 			hash = isc_hash_function(&gluenode->node,
   9984  1.7  christos 						 sizeof(gluenode->node), true) %
   9985  1.7  christos 			       version->glue_table_size;
   9986  1.1  christos 			nextgluenode = gluenode->next;
   9987  1.1  christos 			gluenode->next = version->glue_table[hash];
   9988  1.1  christos 			version->glue_table[hash] = gluenode;
   9989  1.1  christos 		}
   9990  1.1  christos 	}
   9991  1.1  christos 
   9992  1.1  christos 	isc_mem_put(version->rbtdb->common.mctx, oldtable,
   9993  1.1  christos 		    oldsize * sizeof(*version->glue_table));
   9994  1.1  christos 
   9995  1.7  christos 	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ZONE,
   9996  1.7  christos 		      ISC_LOG_DEBUG(3),
   9997  1.1  christos 		      "rehash_gluetable(): "
   9998  1.3  christos 		      "resized glue table from %" PRIu64 " to "
   9999  1.3  christos 		      "%" PRIu64,
   10000  1.7  christos 		      (uint64_t)oldsize, (uint64_t)version->glue_table_size);
   10001  1.1  christos 
   10002  1.3  christos 	return (true);
   10003  1.1  christos }
   10004  1.1  christos 
   10005  1.1  christos static isc_result_t
   10006  1.1  christos glue_nsdname_cb(void *arg, const dns_name_t *name, dns_rdatatype_t qtype) {
   10007  1.1  christos 	rbtdb_glue_additionaldata_ctx_t *ctx;
   10008  1.1  christos 	isc_result_t result;
   10009  1.1  christos 	dns_fixedname_t fixedname_a;
   10010  1.1  christos 	dns_name_t *name_a = NULL;
   10011  1.1  christos 	dns_rdataset_t rdataset_a, sigrdataset_a;
   10012  1.1  christos 	dns_rbtnode_t *node_a = NULL;
   10013  1.1  christos 	dns_fixedname_t fixedname_aaaa;
   10014  1.1  christos 	dns_name_t *name_aaaa = NULL;
   10015  1.1  christos 	dns_rdataset_t rdataset_aaaa, sigrdataset_aaaa;
   10016  1.1  christos 	dns_rbtnode_t *node_aaaa = NULL;
   10017  1.1  christos 	rbtdb_glue_t *glue = NULL;
   10018  1.1  christos 	dns_name_t *gluename = NULL;
   10019  1.1  christos 
   10020  1.1  christos 	/*
   10021  1.1  christos 	 * NS records want addresses in additional records.
   10022  1.1  christos 	 */
   10023  1.1  christos 	INSIST(qtype == dns_rdatatype_a);
   10024  1.1  christos 
   10025  1.7  christos 	ctx = (rbtdb_glue_additionaldata_ctx_t *)arg;
   10026  1.1  christos 
   10027  1.1  christos 	name_a = dns_fixedname_initname(&fixedname_a);
   10028  1.1  christos 	dns_rdataset_init(&rdataset_a);
   10029  1.1  christos 	dns_rdataset_init(&sigrdataset_a);
   10030  1.1  christos 
   10031  1.1  christos 	name_aaaa = dns_fixedname_initname(&fixedname_aaaa);
   10032  1.1  christos 	dns_rdataset_init(&rdataset_aaaa);
   10033  1.1  christos 	dns_rdataset_init(&sigrdataset_aaaa);
   10034  1.1  christos 
   10035  1.7  christos 	result = zone_find((dns_db_t *)ctx->rbtdb, name, ctx->rbtversion,
   10036  1.1  christos 			   dns_rdatatype_a, DNS_DBFIND_GLUEOK, 0,
   10037  1.7  christos 			   (dns_dbnode_t **)&node_a, name_a, &rdataset_a,
   10038  1.7  christos 			   &sigrdataset_a);
   10039  1.1  christos 	if (result == DNS_R_GLUE) {
   10040  1.1  christos 		glue = isc_mem_get(ctx->rbtdb->common.mctx, sizeof(*glue));
   10041  1.1  christos 
   10042  1.1  christos 		gluename = dns_fixedname_initname(&glue->fixedname);
   10043  1.6  christos 		dns_name_copynf(name_a, gluename);
   10044  1.1  christos 
   10045  1.1  christos 		dns_rdataset_init(&glue->rdataset_a);
   10046  1.1  christos 		dns_rdataset_init(&glue->sigrdataset_a);
   10047  1.1  christos 		dns_rdataset_init(&glue->rdataset_aaaa);
   10048  1.1  christos 		dns_rdataset_init(&glue->sigrdataset_aaaa);
   10049  1.1  christos 
   10050  1.1  christos 		dns_rdataset_clone(&rdataset_a, &glue->rdataset_a);
   10051  1.1  christos 		if (dns_rdataset_isassociated(&sigrdataset_a)) {
   10052  1.1  christos 			dns_rdataset_clone(&sigrdataset_a,
   10053  1.1  christos 					   &glue->sigrdataset_a);
   10054  1.1  christos 		}
   10055  1.1  christos 	}
   10056  1.1  christos 
   10057  1.7  christos 	result = zone_find((dns_db_t *)ctx->rbtdb, name, ctx->rbtversion,
   10058  1.1  christos 			   dns_rdatatype_aaaa, DNS_DBFIND_GLUEOK, 0,
   10059  1.7  christos 			   (dns_dbnode_t **)&node_aaaa, name_aaaa,
   10060  1.1  christos 			   &rdataset_aaaa, &sigrdataset_aaaa);
   10061  1.1  christos 	if (result == DNS_R_GLUE) {
   10062  1.1  christos 		if (glue == NULL) {
   10063  1.1  christos 			glue = isc_mem_get(ctx->rbtdb->common.mctx,
   10064  1.1  christos 					   sizeof(*glue));
   10065  1.1  christos 
   10066  1.1  christos 			gluename = dns_fixedname_initname(&glue->fixedname);
   10067  1.6  christos 			dns_name_copynf(name_aaaa, gluename);
   10068  1.1  christos 
   10069  1.1  christos 			dns_rdataset_init(&glue->rdataset_a);
   10070  1.1  christos 			dns_rdataset_init(&glue->sigrdataset_a);
   10071  1.1  christos 			dns_rdataset_init(&glue->rdataset_aaaa);
   10072  1.1  christos 			dns_rdataset_init(&glue->sigrdataset_aaaa);
   10073  1.1  christos 		} else {
   10074  1.1  christos 			INSIST(node_a == node_aaaa);
   10075  1.1  christos 			INSIST(dns_name_equal(name_a, name_aaaa));
   10076  1.1  christos 		}
   10077  1.1  christos 
   10078  1.1  christos 		dns_rdataset_clone(&rdataset_aaaa, &glue->rdataset_aaaa);
   10079  1.1  christos 		if (dns_rdataset_isassociated(&sigrdataset_aaaa)) {
   10080  1.1  christos 			dns_rdataset_clone(&sigrdataset_aaaa,
   10081  1.1  christos 					   &glue->sigrdataset_aaaa);
   10082  1.1  christos 		}
   10083  1.1  christos 	}
   10084  1.1  christos 
   10085  1.1  christos 	if (glue != NULL) {
   10086  1.1  christos 		glue->next = ctx->glue_list;
   10087  1.1  christos 		ctx->glue_list = glue;
   10088  1.1  christos 	}
   10089  1.1  christos 
   10090  1.1  christos 	result = ISC_R_SUCCESS;
   10091  1.1  christos 
   10092  1.7  christos 	if (dns_rdataset_isassociated(&rdataset_a)) {
   10093  1.1  christos 		rdataset_disassociate(&rdataset_a);
   10094  1.7  christos 	}
   10095  1.7  christos 	if (dns_rdataset_isassociated(&sigrdataset_a)) {
   10096  1.1  christos 		rdataset_disassociate(&sigrdataset_a);
   10097  1.7  christos 	}
   10098  1.1  christos 
   10099  1.7  christos 	if (dns_rdataset_isassociated(&rdataset_aaaa)) {
   10100  1.1  christos 		rdataset_disassociate(&rdataset_aaaa);
   10101  1.7  christos 	}
   10102  1.7  christos 	if (dns_rdataset_isassociated(&sigrdataset_aaaa)) {
   10103  1.1  christos 		rdataset_disassociate(&sigrdataset_aaaa);
   10104  1.7  christos 	}
   10105  1.1  christos 
   10106  1.7  christos 	if (node_a != NULL) {
   10107  1.7  christos 		detachnode((dns_db_t *)ctx->rbtdb, (dns_dbnode_t *)&node_a);
   10108  1.7  christos 	}
   10109  1.7  christos 	if (node_aaaa != NULL) {
   10110  1.7  christos 		detachnode((dns_db_t *)ctx->rbtdb, (dns_dbnode_t *)&node_aaaa);
   10111  1.7  christos 	}
   10112  1.1  christos 
   10113  1.1  christos 	return (result);
   10114  1.1  christos }
   10115  1.1  christos 
   10116  1.1  christos static isc_result_t
   10117  1.3  christos rdataset_addglue(dns_rdataset_t *rdataset, dns_dbversion_t *version,
   10118  1.7  christos 		 dns_message_t *msg) {
   10119  1.1  christos 	dns_rbtdb_t *rbtdb = rdataset->private1;
   10120  1.1  christos 	dns_rbtnode_t *node = rdataset->private2;
   10121  1.1  christos 	rbtdb_version_t *rbtversion = version;
   10122  1.3  christos 	uint32_t idx;
   10123  1.1  christos 	rbtdb_glue_table_node_t *cur;
   10124  1.3  christos 	bool found = false;
   10125  1.3  christos 	bool restarted = false;
   10126  1.1  christos 	rbtdb_glue_t *ge;
   10127  1.1  christos 	rbtdb_glue_additionaldata_ctx_t ctx;
   10128  1.1  christos 	isc_result_t result;
   10129  1.1  christos 
   10130  1.1  christos 	REQUIRE(rdataset->type == dns_rdatatype_ns);
   10131  1.1  christos 	REQUIRE(rbtdb == rbtversion->rbtdb);
   10132  1.1  christos 	REQUIRE(!IS_CACHE(rbtdb) && !IS_STUB(rbtdb));
   10133  1.1  christos 
   10134  1.1  christos 	/*
   10135  1.1  christos 	 * The glue table cache that forms a part of the DB version
   10136  1.1  christos 	 * structure is not explicitly bounded and there's no cache
   10137  1.1  christos 	 * cleaning. The zone data size itself is an implicit bound.
   10138  1.1  christos 	 *
   10139  1.1  christos 	 * The key into the glue hashtable is the node pointer. This is
   10140  1.1  christos 	 * because the glue hashtable is a property of the DB version,
   10141  1.1  christos 	 * and the glue is keyed for the ownername/NS tuple. We don't
   10142  1.1  christos 	 * bother with using an expensive dns_name_t comparison here as
   10143  1.1  christos 	 * the node pointer is a fixed value that won't change for a DB
   10144  1.1  christos 	 * version and can be compared directly.
   10145  1.1  christos 	 */
   10146  1.5  christos 	idx = isc_hash_function(&node, sizeof(node), true) %
   10147  1.7  christos 	      rbtversion->glue_table_size;
   10148  1.1  christos 
   10149  1.1  christos restart:
   10150  1.1  christos 	/*
   10151  1.1  christos 	 * First, check if we have the additional entries already cached
   10152  1.1  christos 	 * in the glue table.
   10153  1.1  christos 	 */
   10154  1.1  christos 	RWLOCK(&rbtversion->glue_rwlock, isc_rwlocktype_read);
   10155  1.1  christos 
   10156  1.7  christos 	for (cur = rbtversion->glue_table[idx]; cur != NULL; cur = cur->next) {
   10157  1.7  christos 		if (cur->node == node) {
   10158  1.1  christos 			break;
   10159  1.7  christos 		}
   10160  1.7  christos 	}
   10161  1.1  christos 
   10162  1.1  christos 	if (cur == NULL) {
   10163  1.1  christos 		goto no_glue;
   10164  1.1  christos 	}
   10165  1.1  christos 	/*
   10166  1.1  christos 	 * We found a cached result. Add it to the message and
   10167  1.1  christos 	 * return.
   10168  1.1  christos 	 */
   10169  1.3  christos 	found = true;
   10170  1.1  christos 	ge = cur->glue_list;
   10171  1.1  christos 
   10172  1.1  christos 	/*
   10173  1.1  christos 	 * (void *) -1 is a special value that means no glue is
   10174  1.1  christos 	 * present in the zone.
   10175  1.1  christos 	 */
   10176  1.7  christos 	if (ge == (void *)-1) {
   10177  1.1  christos 		if (!restarted && (rbtdb->gluecachestats != NULL)) {
   10178  1.7  christos 			isc_stats_increment(
   10179  1.7  christos 				rbtdb->gluecachestats,
   10180  1.7  christos 				dns_gluecachestatscounter_hits_absent);
   10181  1.1  christos 		}
   10182  1.1  christos 		goto no_glue;
   10183  1.1  christos 	} else {
   10184  1.1  christos 		if (!restarted && (rbtdb->gluecachestats != NULL)) {
   10185  1.7  christos 			isc_stats_increment(
   10186  1.7  christos 				rbtdb->gluecachestats,
   10187  1.7  christos 				dns_gluecachestatscounter_hits_present);
   10188  1.1  christos 		}
   10189  1.1  christos 	}
   10190  1.1  christos 
   10191  1.1  christos 	for (; ge != NULL; ge = ge->next) {
   10192  1.1  christos 		isc_buffer_t *buffer = NULL;
   10193  1.1  christos 		dns_name_t *name = NULL;
   10194  1.1  christos 		dns_rdataset_t *rdataset_a = NULL;
   10195  1.1  christos 		dns_rdataset_t *sigrdataset_a = NULL;
   10196  1.1  christos 		dns_rdataset_t *rdataset_aaaa = NULL;
   10197  1.1  christos 		dns_rdataset_t *sigrdataset_aaaa = NULL;
   10198  1.1  christos 		dns_name_t *gluename = dns_fixedname_name(&ge->fixedname);
   10199  1.1  christos 
   10200  1.7  christos 		isc_buffer_allocate(msg->mctx, &buffer, 512);
   10201  1.1  christos 
   10202  1.1  christos 		result = dns_message_gettempname(msg, &name);
   10203  1.1  christos 		if (ISC_UNLIKELY(result != ISC_R_SUCCESS)) {
   10204  1.1  christos 			isc_buffer_free(&buffer);
   10205  1.1  christos 			goto no_glue;
   10206  1.1  christos 		}
   10207  1.1  christos 
   10208  1.1  christos 		dns_name_copy(gluename, name, buffer);
   10209  1.1  christos 		dns_message_takebuffer(msg, &buffer);
   10210  1.1  christos 
   10211  1.1  christos 		if (dns_rdataset_isassociated(&ge->rdataset_a)) {
   10212  1.1  christos 			result = dns_message_gettemprdataset(msg, &rdataset_a);
   10213  1.1  christos 			if (ISC_UNLIKELY(result != ISC_R_SUCCESS)) {
   10214  1.1  christos 				dns_message_puttempname(msg, &name);
   10215  1.1  christos 				goto no_glue;
   10216  1.1  christos 			}
   10217  1.1  christos 		}
   10218  1.1  christos 
   10219  1.1  christos 		if (dns_rdataset_isassociated(&ge->sigrdataset_a)) {
   10220  1.1  christos 			result = dns_message_gettemprdataset(msg,
   10221  1.1  christos 							     &sigrdataset_a);
   10222  1.1  christos 			if (ISC_UNLIKELY(result != ISC_R_SUCCESS)) {
   10223  1.1  christos 				if (rdataset_a != NULL) {
   10224  1.7  christos 					dns_message_puttemprdataset(
   10225  1.7  christos 						msg, &rdataset_a);
   10226  1.1  christos 				}
   10227  1.1  christos 				dns_message_puttempname(msg, &name);
   10228  1.1  christos 				goto no_glue;
   10229  1.1  christos 			}
   10230  1.1  christos 		}
   10231  1.1  christos 
   10232  1.3  christos 		if (dns_rdataset_isassociated(&ge->rdataset_aaaa)) {
   10233  1.3  christos 			result = dns_message_gettemprdataset(msg,
   10234  1.7  christos 							     &rdataset_aaaa);
   10235  1.3  christos 			if (ISC_UNLIKELY(result != ISC_R_SUCCESS)) {
   10236  1.3  christos 				dns_message_puttempname(msg, &name);
   10237  1.3  christos 				if (rdataset_a != NULL) {
   10238  1.7  christos 					dns_message_puttemprdataset(
   10239  1.7  christos 						msg, &rdataset_a);
   10240  1.3  christos 				}
   10241  1.3  christos 				if (sigrdataset_a != NULL) {
   10242  1.7  christos 					dns_message_puttemprdataset(
   10243  1.7  christos 						msg, &sigrdataset_a);
   10244  1.1  christos 				}
   10245  1.3  christos 				goto no_glue;
   10246  1.1  christos 			}
   10247  1.3  christos 		}
   10248  1.1  christos 
   10249  1.3  christos 		if (dns_rdataset_isassociated(&ge->sigrdataset_aaaa)) {
   10250  1.3  christos 			result = dns_message_gettemprdataset(msg,
   10251  1.7  christos 							     &sigrdataset_aaaa);
   10252  1.3  christos 			if (ISC_UNLIKELY(result != ISC_R_SUCCESS)) {
   10253  1.3  christos 				dns_message_puttempname(msg, &name);
   10254  1.3  christos 				if (rdataset_a != NULL) {
   10255  1.7  christos 					dns_message_puttemprdataset(
   10256  1.7  christos 						msg, &rdataset_a);
   10257  1.7  christos 				}
   10258  1.7  christos 				if (sigrdataset_a != NULL) {
   10259  1.7  christos 					dns_message_puttemprdataset(
   10260  1.7  christos 						msg, &sigrdataset_a);
   10261  1.7  christos 				}
   10262  1.7  christos 				if (rdataset_aaaa != NULL) {
   10263  1.7  christos 					dns_message_puttemprdataset(
   10264  1.7  christos 						msg, &rdataset_aaaa);
   10265  1.1  christos 				}
   10266  1.3  christos 				goto no_glue;
   10267  1.1  christos 			}
   10268  1.1  christos 		}
   10269  1.1  christos 
   10270  1.1  christos 		if (ISC_LIKELY(rdataset_a != NULL)) {
   10271  1.1  christos 			dns_rdataset_clone(&ge->rdataset_a, rdataset_a);
   10272  1.1  christos 			ISC_LIST_APPEND(name->list, rdataset_a, link);
   10273  1.1  christos 		}
   10274  1.1  christos 
   10275  1.1  christos 		if (sigrdataset_a != NULL) {
   10276  1.1  christos 			dns_rdataset_clone(&ge->sigrdataset_a, sigrdataset_a);
   10277  1.1  christos 			ISC_LIST_APPEND(name->list, sigrdataset_a, link);
   10278  1.1  christos 		}
   10279  1.1  christos 
   10280  1.3  christos 		if (rdataset_aaaa != NULL) {
   10281  1.7  christos 			dns_rdataset_clone(&ge->rdataset_aaaa, rdataset_aaaa);
   10282  1.7  christos 			ISC_LIST_APPEND(name->list, rdataset_aaaa, link);
   10283  1.3  christos 		}
   10284  1.3  christos 		if (sigrdataset_aaaa != NULL) {
   10285  1.3  christos 			dns_rdataset_clone(&ge->sigrdataset_aaaa,
   10286  1.3  christos 					   sigrdataset_aaaa);
   10287  1.7  christos 			ISC_LIST_APPEND(name->list, sigrdataset_aaaa, link);
   10288  1.1  christos 		}
   10289  1.1  christos 
   10290  1.1  christos 		dns_message_addname(msg, name, DNS_SECTION_ADDITIONAL);
   10291  1.1  christos 	}
   10292  1.1  christos 
   10293  1.1  christos no_glue:
   10294  1.1  christos 	RWUNLOCK(&rbtversion->glue_rwlock, isc_rwlocktype_read);
   10295  1.1  christos 
   10296  1.1  christos 	if (found) {
   10297  1.1  christos 		return (ISC_R_SUCCESS);
   10298  1.1  christos 	}
   10299  1.1  christos 
   10300  1.1  christos 	if (restarted) {
   10301  1.1  christos 		return (ISC_R_FAILURE);
   10302  1.1  christos 	}
   10303  1.1  christos 
   10304  1.1  christos 	/*
   10305  1.1  christos 	 * No cached glue was found in the table. Cache it and restart
   10306  1.1  christos 	 * this function.
   10307  1.1  christos 	 *
   10308  1.1  christos 	 * Due to the gap between the read lock and the write lock, it's
   10309  1.1  christos 	 * possible that we may cache a duplicate glue table entry, but
   10310  1.1  christos 	 * we don't care.
   10311  1.1  christos 	 */
   10312  1.1  christos 
   10313  1.1  christos 	ctx.glue_list = NULL;
   10314  1.1  christos 	ctx.rbtdb = rbtdb;
   10315  1.1  christos 	ctx.rbtversion = rbtversion;
   10316  1.1  christos 
   10317  1.1  christos 	RWLOCK(&rbtversion->glue_rwlock, isc_rwlocktype_write);
   10318  1.1  christos 
   10319  1.1  christos 	if (ISC_UNLIKELY(rehash_gluetable(rbtversion))) {
   10320  1.5  christos 		idx = isc_hash_function(&node, sizeof(node), true) %
   10321  1.7  christos 		      rbtversion->glue_table_size;
   10322  1.1  christos 	}
   10323  1.1  christos 
   10324  1.1  christos 	(void)dns_rdataset_additionaldata(rdataset, glue_nsdname_cb, &ctx);
   10325  1.1  christos 
   10326  1.1  christos 	cur = isc_mem_get(rbtdb->common.mctx, sizeof(*cur));
   10327  1.1  christos 
   10328  1.1  christos 	/*
   10329  1.1  christos 	 * XXXMUKS: it looks like the dns_dbversion is not destroyed
   10330  1.1  christos 	 * when named is terminated by a keyboard break. This doesn't
   10331  1.1  christos 	 * cleanup the node reference and keeps the process dangling.
   10332  1.1  christos 	 */
   10333  1.3  christos 	/* isc_refcount_increment0(&node->references); */
   10334  1.1  christos 	cur->node = node;
   10335  1.1  christos 
   10336  1.1  christos 	if (ctx.glue_list == NULL) {
   10337  1.1  christos 		/*
   10338  1.1  christos 		 * No glue was found. Cache it so.
   10339  1.1  christos 		 */
   10340  1.7  christos 		cur->glue_list = (void *)-1;
   10341  1.1  christos 		if (rbtdb->gluecachestats != NULL) {
   10342  1.7  christos 			isc_stats_increment(
   10343  1.7  christos 				rbtdb->gluecachestats,
   10344  1.7  christos 				dns_gluecachestatscounter_inserts_absent);
   10345  1.1  christos 		}
   10346  1.1  christos 	} else {
   10347  1.1  christos 		cur->glue_list = ctx.glue_list;
   10348  1.1  christos 		if (rbtdb->gluecachestats != NULL) {
   10349  1.7  christos 			isc_stats_increment(
   10350  1.7  christos 				rbtdb->gluecachestats,
   10351  1.7  christos 				dns_gluecachestatscounter_inserts_present);
   10352  1.1  christos 		}
   10353  1.1  christos 	}
   10354  1.1  christos 
   10355  1.1  christos 	cur->next = rbtversion->glue_table[idx];
   10356  1.1  christos 	rbtversion->glue_table[idx] = cur;
   10357  1.1  christos 	rbtversion->glue_table_nodecount++;
   10358  1.1  christos 
   10359  1.1  christos 	RWUNLOCK(&rbtversion->glue_rwlock, isc_rwlocktype_write);
   10360  1.1  christos 
   10361  1.7  christos 	restarted = true;
   10362  1.7  christos 	goto restart;
   10363  1.1  christos 
   10364  1.7  christos 	/* UNREACHABLE */
   10365  1.1  christos }
   10366  1.1  christos 
   10367  1.1  christos /*%
   10368  1.1  christos  * Routines for LRU-based cache management.
   10369  1.1  christos  */
   10370  1.1  christos 
   10371  1.1  christos /*%
   10372  1.1  christos  * See if a given cache entry that is being reused needs to be updated
   10373  1.1  christos  * in the LRU-list.  From the LRU management point of view, this function is
   10374  1.1  christos  * expected to return true for almost all cases.  When used with threads,
   10375  1.1  christos  * however, this may cause a non-negligible performance penalty because a
   10376  1.1  christos  * writer lock will have to be acquired before updating the list.
   10377  1.1  christos  * If DNS_RBTDB_LIMITLRUUPDATE is defined to be non 0 at compilation time, this
   10378  1.1  christos  * function returns true if the entry has not been updated for some period of
   10379  1.1  christos  * time.  We differentiate the NS or glue address case and the others since
   10380  1.1  christos  * experiments have shown that the former tends to be accessed relatively
   10381  1.1  christos  * infrequently and the cost of cache miss is higher (e.g., a missing NS records
   10382  1.1  christos  * may cause external queries at a higher level zone, involving more
   10383  1.1  christos  * transactions).
   10384  1.1  christos  *
   10385  1.1  christos  * Caller must hold the node (read or write) lock.
   10386  1.1  christos  */
   10387  1.3  christos static inline bool
   10388  1.1  christos need_headerupdate(rdatasetheader_t *header, isc_stdtime_t now) {
   10389  1.1  christos 	if ((header->attributes &
   10390  1.7  christos 	     (RDATASET_ATTR_NONEXISTENT | RDATASET_ATTR_ANCIENT |
   10391  1.1  christos 	      RDATASET_ATTR_ZEROTTL)) != 0)
   10392  1.7  christos 	{
   10393  1.3  christos 		return (false);
   10394  1.7  christos 	}
   10395  1.1  christos 
   10396  1.1  christos #if DNS_RBTDB_LIMITLRUUPDATE
   10397  1.1  christos 	if (header->type == dns_rdatatype_ns ||
   10398  1.1  christos 	    (header->trust == dns_trust_glue &&
   10399  1.1  christos 	     (header->type == dns_rdatatype_a ||
   10400  1.7  christos 	      header->type == dns_rdatatype_aaaa)))
   10401  1.7  christos 	{
   10402  1.1  christos 		/*
   10403  1.7  christos 		 * Glue records are updated if at least DNS_RBTDB_LRUUPDATE_GLUE
   10404  1.7  christos 		 * seconds have passed since the previous update time.
   10405  1.1  christos 		 */
   10406  1.7  christos 		return (header->last_used + DNS_RBTDB_LRUUPDATE_GLUE <= now);
   10407  1.1  christos 	}
   10408  1.1  christos 
   10409  1.7  christos 	/*
   10410  1.7  christos 	 * Other records are updated if DNS_RBTDB_LRUUPDATE_REGULAR seconds
   10411  1.7  christos 	 * have passed.
   10412  1.7  christos 	 */
   10413  1.7  christos 	return (header->last_used + DNS_RBTDB_LRUUPDATE_REGULAR <= now);
   10414  1.1  christos #else
   10415  1.1  christos 	UNUSED(now);
   10416  1.1  christos 
   10417  1.3  christos 	return (true);
   10418  1.7  christos #endif /* if DNS_RBTDB_LIMITLRUUPDATE */
   10419  1.1  christos }
   10420  1.1  christos 
   10421  1.1  christos /*%
   10422  1.1  christos  * Update the timestamp of a given cache entry and move it to the head
   10423  1.1  christos  * of the corresponding LRU list.
   10424  1.1  christos  *
   10425  1.1  christos  * Caller must hold the node (write) lock.
   10426  1.1  christos  *
   10427  1.1  christos  * Note that the we do NOT touch the heap here, as the TTL has not changed.
   10428  1.1  christos  */
   10429  1.1  christos static void
   10430  1.7  christos update_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header, isc_stdtime_t now) {
   10431  1.1  christos 	INSIST(IS_CACHE(rbtdb));
   10432  1.1  christos 
   10433  1.1  christos 	/* To be checked: can we really assume this? XXXMLG */
   10434  1.1  christos 	INSIST(ISC_LINK_LINKED(header, link));
   10435  1.1  christos 
   10436  1.1  christos 	ISC_LIST_UNLINK(rbtdb->rdatasets[header->node->locknum], header, link);
   10437  1.1  christos 	header->last_used = now;
   10438  1.1  christos 	ISC_LIST_PREPEND(rbtdb->rdatasets[header->node->locknum], header, link);
   10439  1.1  christos }
   10440  1.1  christos 
   10441  1.1  christos /*%
   10442  1.1  christos  * Purge some expired and/or stale (i.e. unused for some period) cache entries
   10443  1.1  christos  * under an overmem condition.  To recover from this condition quickly, up to
   10444  1.1  christos  * 2 entries will be purged.  This process is triggered while adding a new
   10445  1.1  christos  * entry, and we specifically avoid purging entries in the same LRU bucket as
   10446  1.1  christos  * the one to which the new entry will belong.  Otherwise, we might purge
   10447  1.1  christos  * entries of the same name of different RR types while adding RRsets from a
   10448  1.1  christos  * single response (consider the case where we're adding A and AAAA glue records
   10449  1.1  christos  * of the same NS name).
   10450  1.1  christos  */
   10451  1.1  christos static void
   10452  1.7  christos overmem_purge(dns_rbtdb_t *rbtdb, unsigned int locknum_start, isc_stdtime_t now,
   10453  1.7  christos 	      bool tree_locked) {
   10454  1.1  christos 	rdatasetheader_t *header, *header_prev;
   10455  1.1  christos 	unsigned int locknum;
   10456  1.1  christos 	int purgecount = 2;
   10457  1.1  christos 
   10458  1.1  christos 	for (locknum = (locknum_start + 1) % rbtdb->node_lock_count;
   10459  1.1  christos 	     locknum != locknum_start && purgecount > 0;
   10460  1.7  christos 	     locknum = (locknum + 1) % rbtdb->node_lock_count)
   10461  1.7  christos 	{
   10462  1.1  christos 		NODE_LOCK(&rbtdb->node_locks[locknum].lock,
   10463  1.1  christos 			  isc_rwlocktype_write);
   10464  1.1  christos 
   10465  1.1  christos 		header = isc_heap_element(rbtdb->heaps[locknum], 1);
   10466  1.1  christos 		if (header && header->rdh_ttl < now - RBTDB_VIRTUAL) {
   10467  1.7  christos 			expire_header(rbtdb, header, tree_locked, expire_ttl);
   10468  1.1  christos 			purgecount--;
   10469  1.1  christos 		}
   10470  1.1  christos 
   10471  1.1  christos 		for (header = ISC_LIST_TAIL(rbtdb->rdatasets[locknum]);
   10472  1.7  christos 		     header != NULL && purgecount > 0; header = header_prev)
   10473  1.7  christos 		{
   10474  1.1  christos 			header_prev = ISC_LIST_PREV(header, link);
   10475  1.1  christos 			/*
   10476  1.1  christos 			 * Unlink the entry at this point to avoid checking it
   10477  1.1  christos 			 * again even if it's currently used someone else and
   10478  1.1  christos 			 * cannot be purged at this moment.  This entry won't be
   10479  1.1  christos 			 * referenced any more (so unlinking is safe) since the
   10480  1.1  christos 			 * TTL was reset to 0.
   10481  1.1  christos 			 */
   10482  1.1  christos 			ISC_LIST_UNLINK(rbtdb->rdatasets[locknum], header,
   10483  1.1  christos 					link);
   10484  1.7  christos 			expire_header(rbtdb, header, tree_locked, expire_lru);
   10485  1.1  christos 			purgecount--;
   10486  1.1  christos 		}
   10487  1.1  christos 
   10488  1.1  christos 		NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
   10489  1.7  christos 			    isc_rwlocktype_write);
   10490  1.1  christos 	}
   10491  1.1  christos }
   10492  1.1  christos 
   10493  1.1  christos static void
   10494  1.7  christos expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header, bool tree_locked,
   10495  1.7  christos 	      expire_t reason) {
   10496  1.1  christos 	set_ttl(rbtdb, header, 0);
   10497  1.1  christos 	mark_header_ancient(rbtdb, header);
   10498  1.1  christos 
   10499  1.1  christos 	/*
   10500  1.1  christos 	 * Caller must hold the node (write) lock.
   10501  1.1  christos 	 */
   10502  1.1  christos 
   10503  1.3  christos 	if (isc_refcount_current(&header->node->references) == 0) {
   10504  1.1  christos 		/*
   10505  1.1  christos 		 * If no one else is using the node, we can clean it up now.
   10506  1.1  christos 		 * We first need to gain a new reference to the node to meet a
   10507  1.1  christos 		 * requirement of decrement_reference().
   10508  1.1  christos 		 */
   10509  1.1  christos 		new_reference(rbtdb, header->node);
   10510  1.1  christos 		decrement_reference(rbtdb, header->node, 0,
   10511  1.1  christos 				    isc_rwlocktype_write,
   10512  1.7  christos 				    tree_locked ? isc_rwlocktype_write
   10513  1.7  christos 						: isc_rwlocktype_none,
   10514  1.7  christos 				    false);
   10515  1.1  christos 
   10516  1.7  christos 		if (rbtdb->cachestats == NULL) {
   10517  1.1  christos 			return;
   10518  1.7  christos 		}
   10519  1.1  christos 
   10520  1.1  christos 		switch (reason) {
   10521  1.1  christos 		case expire_ttl:
   10522  1.1  christos 			isc_stats_increment(rbtdb->cachestats,
   10523  1.1  christos 					    dns_cachestatscounter_deletettl);
   10524  1.1  christos 			break;
   10525  1.1  christos 		case expire_lru:
   10526  1.1  christos 			isc_stats_increment(rbtdb->cachestats,
   10527  1.1  christos 					    dns_cachestatscounter_deletelru);
   10528  1.1  christos 			break;
   10529  1.1  christos 		default:
   10530  1.1  christos 			break;
   10531  1.1  christos 		}
   10532  1.1  christos 	}
   10533  1.1  christos }
   10534