rbtdb.c 93.5 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright (C) 1999  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

18
19
20
21
/*
 * Principal Author: Bob Halley
 */

22
23
#include <config.h>

Bob Halley's avatar
Bob Halley committed
24
25
#include <stddef.h>
#include <string.h>
26
#include <limits.h>
Bob Halley's avatar
Bob Halley committed
27
28
29
30
31
32

#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/mutex.h>
#include <isc/rwlock.h>

Bob Halley's avatar
Bob Halley committed
33
#include <dns/types.h>
Bob Halley's avatar
Bob Halley committed
34
#include <dns/name.h>
35
36
37
#include <dns/fixedname.h>
#include <dns/db.h>
#include <dns/dbiterator.h>
Bob Halley's avatar
Bob Halley committed
38
#include <dns/rbt.h>
Bob Halley's avatar
Bob Halley committed
39
40
#include <dns/master.h>
#include <dns/rdataslab.h>
41
#include <dns/rdata.h>
Bob Halley's avatar
Bob Halley committed
42
43
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
44
#include <dns/masterdump.h>
Bob Halley's avatar
Bob Halley committed
45

46
47
48
#ifdef DNS_RBTDB_VERSION64
#include "rbtdb64.h"
#else
Bob Halley's avatar
Bob Halley committed
49
#include "rbtdb.h"
50
#endif
Bob Halley's avatar
Bob Halley committed
51
52
53
54

/* Lame.  Move util.h to <isc/util.h> */
#include "../isc/util.h"

55
56
57
58
59
60
#ifdef DNS_RBTDB_VERSION64
#define RBTDB_MAGIC			0x52424438U	/* RBD8. */
#else
#define RBTDB_MAGIC			0x52424434U	/* RBD4. */
#endif

Bob Halley's avatar
Bob Halley committed
61
62
63
64
#define VALID_RBTDB(rbtdb)		((rbtdb) != NULL && \
					 (rbtdb)->common.impmagic == \
						RBTDB_MAGIC)

65
66
67
68
69
70
#ifdef DNS_RBTDB_VERSION64
typedef isc_uint64_t			rbtdb_serial_t;
#else
typedef isc_uint32_t			rbtdb_serial_t;
#endif

Bob Halley's avatar
Bob Halley committed
71
typedef struct rdatasetheader {
72
73
74
	/*
	 * Locked by the owning node's lock.
	 */
75
	rbtdb_serial_t			serial;
Bob Halley's avatar
Bob Halley committed
76
	dns_ttl_t			ttl;
Bob Halley's avatar
Bob Halley committed
77
	dns_rdatatype_t			type;
78
	isc_uint16_t			attributes;
Bob Halley's avatar
Bob Halley committed
79
	dns_trust_t			trust;
Bob Halley's avatar
Bob Halley committed
80
81
	/*
	 * We don't use the LIST macros, because the LIST structure has
82
	 * both head and tail pointers, and is doubly linked.
Bob Halley's avatar
Bob Halley committed
83
84
	 */
	struct rdatasetheader		*next;
85
	struct rdatasetheader		*down;
Bob Halley's avatar
Bob Halley committed
86
87
} rdatasetheader_t;

Bob Halley's avatar
Bob Halley committed
88
#define RDATASET_ATTR_NONEXISTENT	0x01
89
#define RDATASET_ATTR_STALE		0x02
90
91
#define RDATASET_ATTR_IGNORE		0x04

92
93
94
95
96
97
#define EXISTS(header) \
	(((header)->attributes & RDATASET_ATTR_NONEXISTENT) == 0)
#define NONEXISTENT(header) \
	(((header)->attributes & RDATASET_ATTR_NONEXISTENT) != 0)
#define IGNORE(header) \
	(((header)->attributes & RDATASET_ATTR_IGNORE) != 0)
98

Bob Halley's avatar
Bob Halley committed
99
#define DEFAULT_NODE_LOCK_COUNT		7		/* Should be prime. */
Bob Halley's avatar
Bob Halley committed
100
101
102
103

typedef struct {
	isc_mutex_t			lock;
	unsigned int			references;
104
105
106
107
108
109
110
111
112
113
114
} rbtdb_nodelock_t;

typedef struct rbtdb_changed {
	dns_rbtnode_t *			node;
	isc_boolean_t			dirty;
	ISC_LINK(struct rbtdb_changed)	link;
} rbtdb_changed_t;

typedef ISC_LIST(rbtdb_changed_t)	rbtdb_changedlist_t;

typedef struct rbtdb_version {
Bob Halley's avatar
Bob Halley committed
115
	/* Not locked */
116
	rbtdb_serial_t			serial;
Bob Halley's avatar
Bob Halley committed
117
	/* Locked by database lock. */
118
	isc_boolean_t			writer;
Bob Halley's avatar
Bob Halley committed
119
	unsigned int			references;
120
121
122
123
124
125
	isc_boolean_t			commit_ok;
	rbtdb_changedlist_t		changed_list;
	ISC_LINK(struct rbtdb_version)	link;
} rbtdb_version_t;

typedef ISC_LIST(rbtdb_version_t)	rbtdb_versionlist_t;
Bob Halley's avatar
Bob Halley committed
126
127

typedef struct {
128
	/* Unlocked. */
Bob Halley's avatar
Bob Halley committed
129
130
131
132
	dns_db_t			common;
	isc_mutex_t			lock;
	isc_rwlock_t			tree_lock;
	unsigned int			node_lock_count;
133
	rbtdb_nodelock_t *	       	node_locks;
134
	dns_rbtnode_t *			origin_node;
135
	/* Locked by lock. */
Bob Halley's avatar
Bob Halley committed
136
	unsigned int			references;
137
138
139
140
141
142
143
144
	unsigned int			attributes;
	rbtdb_serial_t			current_serial;
	rbtdb_serial_t			least_serial;
	rbtdb_serial_t			next_serial;
	rbtdb_version_t *		current_version;
	rbtdb_version_t *		future_version;
	rbtdb_versionlist_t		open_versions;
	/* Locked by tree_lock. */
Bob Halley's avatar
Bob Halley committed
145
146
147
	dns_rbt_t *			tree;
} dns_rbtdb_t;

148
#define RBTDB_ATTR_LOADED		0x01
Bob Halley's avatar
Bob Halley committed
149
#define RBTDB_ATTR_LOADING		0x02
150

151
152
153
154
155
156
157
158
159
160
161
/*
 * Search Context
 */
typedef struct {
	dns_rbtdb_t *		rbtdb;
	rbtdb_version_t *	rbtversion;
	rbtdb_serial_t		serial;
	unsigned int		options;
	dns_rbtnodechain_t	chain;
	isc_boolean_t		copy_name;
	isc_boolean_t		need_cleanup;
Bob Halley's avatar
Bob Halley committed
162
	isc_boolean_t		wild;
163
164
165
	dns_rbtnode_t *	       	zonecut;
	rdatasetheader_t *	zonecut_rdataset;
	dns_fixedname_t		zonecut_name;
166
	isc_stdtime_t		now;
167
168
} rbtdb_search_t;

169
170
171
172
173
174
175
176
/*
 * Load Context
 */
typedef struct {
	dns_rbtdb_t *		rbtdb;
	isc_stdtime_t		now;
} rbtdb_load_t;

Bob Halley's avatar
Bob Halley committed
177
static void rdataset_disassociate(dns_rdataset_t *rdataset);
Bob Halley's avatar
Bob Halley committed
178
179
180
static dns_result_t rdataset_first(dns_rdataset_t *rdataset);
static dns_result_t rdataset_next(dns_rdataset_t *rdataset);
static void rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata);
Bob Halley's avatar
Bob Halley committed
181
static void rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target);
182
183

static dns_rdatasetmethods_t rdataset_methods = {
Bob Halley's avatar
Bob Halley committed
184
185
186
	rdataset_disassociate,
	rdataset_first,
	rdataset_next,
Bob Halley's avatar
Bob Halley committed
187
188
	rdataset_current,
	rdataset_clone
Bob Halley's avatar
Bob Halley committed
189
190
191
192
193
194
195
196
197
198
199
200
201
};

static void rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp);
static dns_result_t rdatasetiter_first(dns_rdatasetiter_t *iterator);
static dns_result_t rdatasetiter_next(dns_rdatasetiter_t *iterator);
static void rdatasetiter_current(dns_rdatasetiter_t *iterator,
				 dns_rdataset_t *rdataset);

static dns_rdatasetitermethods_t rdatasetiter_methods = {
	rdatasetiter_destroy,
	rdatasetiter_first,
	rdatasetiter_next,
	rdatasetiter_current
202
203
};

Bob Halley's avatar
Bob Halley committed
204
205
206
207
208
typedef struct rbtdb_rdatasetiter {
	dns_rdatasetiter_t		common;
	rdatasetheader_t *		current;
} rbtdb_rdatasetiter_t;

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
static void		dbiterator_destroy(dns_dbiterator_t **iteratorp);
static dns_result_t	dbiterator_first(dns_dbiterator_t *iterator);
static dns_result_t	dbiterator_next(dns_dbiterator_t *iterator);
static dns_result_t	dbiterator_current(dns_dbiterator_t *iterator,
					   dns_dbnode_t **nodep,
					   dns_name_t *name);
static dns_result_t	dbiterator_pause(dns_dbiterator_t *iterator);
static dns_result_t	dbiterator_origin(dns_dbiterator_t *iterator,
					  dns_name_t *name);

static dns_dbiteratormethods_t dbiterator_methods = {
	dbiterator_destroy,
	dbiterator_first,
	dbiterator_next,
	dbiterator_current,
	dbiterator_pause,
	dbiterator_origin
};

typedef struct rbtdb_dbiterator {
	dns_dbiterator_t		common;
	isc_boolean_t			paused;
	isc_boolean_t			new_origin;
Bob Halley's avatar
Bob Halley committed
232
233
	isc_boolean_t			tree_locked;
	dns_result_t			result;
234
235
236
	dns_fixedname_t			name;
	dns_fixedname_t			origin;
	dns_rbtnode_t			*node;
Bob Halley's avatar
Bob Halley committed
237
	dns_rbtnodechain_t		chain;
238
239
240
} rbtdb_dbiterator_t;


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Locking
 *
 * If a routine is going to lock more than one lock in this module, then
 * the locking must be done in the following order:
 *
 *	Tree Lock
 *
 *	Node Lock	(Only one from the set may be locked at one time by
 *			 any caller)
 *
 *	Database Lock
 *
 * Failure to follow this hierarchy can result in deadlock.
 */

257
258
259
260
261
262
263
264
265
266
267
/*
 * Deleting Nodes
 *
 * Currently there is no deletion of nodes from the database, except when
 * the database is being destroyed.
 *
 * If node deletion is added in the future, then for zone databases the node
 * for the origin of the zone MUST NOT be deleted.
 */


268
269
270
271
/*
 * DB Routines
 */

Bob Halley's avatar
Bob Halley committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
static void
attach(dns_db_t *source, dns_db_t **targetp) {
	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)source;

	REQUIRE(VALID_RBTDB(rbtdb));

	LOCK(&rbtdb->lock);
	REQUIRE(rbtdb->references > 0);
	rbtdb->references++;
	UNLOCK(&rbtdb->lock);

	*targetp = source;
}

static void
free_rbtdb(dns_rbtdb_t *rbtdb) {
	unsigned int i;
Bob Halley's avatar
Bob Halley committed
289
	isc_region_t r;
Bob Halley's avatar
Bob Halley committed
290

291
292
	REQUIRE(EMPTY(rbtdb->open_versions));
	REQUIRE(rbtdb->future_version == NULL);
293

294
295
	isc_mem_put(rbtdb->common.mctx, rbtdb->current_version,
		    sizeof (rbtdb_version_t));
Bob Halley's avatar
Bob Halley committed
296
	dns_name_toregion(&rbtdb->common.origin, &r);
Bob Halley's avatar
Bob Halley committed
297
298
	if (r.base != NULL)
		isc_mem_put(rbtdb->common.mctx, r.base, r.length);
Bob Halley's avatar
Bob Halley committed
299
300
301
302
	if (rbtdb->tree != NULL)
		dns_rbt_destroy(&rbtdb->tree);
	for (i = 0; i < rbtdb->node_lock_count; i++)
		isc_mutex_destroy(&rbtdb->node_locks[i].lock);
Bob Halley's avatar
Bob Halley committed
303
	isc_mem_put(rbtdb->common.mctx, rbtdb->node_locks,
304
		    rbtdb->node_lock_count * sizeof (rbtdb_nodelock_t));
Bob Halley's avatar
Bob Halley committed
305
306
307
308
	isc_rwlock_destroy(&rbtdb->tree_lock);
	isc_mutex_destroy(&rbtdb->lock);
	rbtdb->common.magic = 0;
	rbtdb->common.impmagic = 0;
Bob Halley's avatar
Bob Halley committed
309
	isc_mem_put(rbtdb->common.mctx, rbtdb, sizeof *rbtdb);
Bob Halley's avatar
Bob Halley committed
310
311
}

312
static inline void
Bob Halley's avatar
Bob Halley committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
maybe_free_rbtdb(dns_rbtdb_t *rbtdb) {
	isc_boolean_t want_free = ISC_TRUE;
	unsigned int i;
	
	/* XXX check for open versions here */

	/*
	 * Even though there are no external direct references, there still
	 * may be nodes in use.
	 */
	for (i = 0; i < rbtdb->node_lock_count; i++) {
		LOCK(&rbtdb->node_locks[i].lock);
		if (rbtdb->node_locks[i].references != 0)
			want_free = ISC_FALSE;
		UNLOCK(&rbtdb->node_locks[i].lock);
	}

	if (want_free)
		free_rbtdb(rbtdb);
}

static void
detach(dns_db_t **dbp) {
	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(*dbp);
	isc_boolean_t maybe_free = ISC_FALSE;

	REQUIRE(VALID_RBTDB(rbtdb));

	LOCK(&rbtdb->lock);
	REQUIRE(rbtdb->references > 0);
	rbtdb->references--;
	if (rbtdb->references == 0)
		maybe_free = ISC_TRUE;
	UNLOCK(&rbtdb->lock);

	if (maybe_free)
		maybe_free_rbtdb(rbtdb);

Bob Halley's avatar
Bob Halley committed
351
	*dbp = NULL;
Bob Halley's avatar
Bob Halley committed
352
353
354
355
356
}

static void
currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
357
	rbtdb_version_t *version;
Bob Halley's avatar
Bob Halley committed
358
359
360

	REQUIRE(VALID_RBTDB(rbtdb));

361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
	LOCK(&rbtdb->lock);
	version = rbtdb->current_version;
	if (version->references == 0)
		PREPEND(rbtdb->open_versions, version, link);
	version->references++;
	UNLOCK(&rbtdb->lock);

	*versionp = (dns_dbversion_t *)version;
}

static inline rbtdb_version_t *
allocate_version(isc_mem_t *mctx, unsigned int serial,
		 unsigned int references, isc_boolean_t writer)
{
	rbtdb_version_t *version;

	version = isc_mem_get(mctx, sizeof *version);
	if (version == NULL)
		return (NULL);
	version->serial = serial;
	version->references = references;
	version->writer = writer;
	version->commit_ok = ISC_FALSE;
	ISC_LIST_INIT(version->changed_list);
	ISC_LINK_INIT(version, link);
	
	return (version);
Bob Halley's avatar
Bob Halley committed
388
389
390
391
392
}

static dns_result_t
newversion(dns_db_t *db, dns_dbversion_t **versionp) {
	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
393
	rbtdb_version_t *version;
Bob Halley's avatar
Bob Halley committed
394

Bob Halley's avatar
Bob Halley committed
395
	REQUIRE(VALID_RBTDB(rbtdb));
396
397
	REQUIRE(versionp != NULL && *versionp == NULL);
	REQUIRE(rbtdb->future_version == NULL);
Bob Halley's avatar
Bob Halley committed
398

399
400
401
402
403
	LOCK(&rbtdb->lock);
	RUNTIME_CHECK(rbtdb->next_serial != 0);		/* XXX Error? */
	version = allocate_version(rbtdb->common.mctx, rbtdb->next_serial, 1,
				   ISC_TRUE);
	if (version != NULL) {
404
		version->commit_ok = ISC_TRUE;
405
406
407
408
409
410
411
412
		rbtdb->next_serial++;
		rbtdb->future_version = version;
	}
	UNLOCK(&rbtdb->lock);

	if (version == NULL)
		return (DNS_R_NOMEMORY);

413
414
	*versionp = version;

415
416
417
	return (DNS_R_SUCCESS);
}

418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
static void
attachversion(dns_db_t *db, dns_dbversion_t *source,
	      dns_dbversion_t **targetp)
{
	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
	rbtdb_version_t *rbtversion = source;

	REQUIRE(VALID_RBTDB(rbtdb));

	LOCK(&rbtdb->lock);

	INSIST(rbtversion->references > 0);
	rbtversion->references++;
	INSIST(rbtversion->references != 0);

	UNLOCK(&rbtdb->lock);

	*targetp = rbtversion;
}

438
439
440
441
442
443
444
445
446
447
448
static rbtdb_changed_t *
add_changed(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
	    dns_rbtnode_t *node)
{
	rbtdb_changed_t *changed;

	/*
	 * Caller must be holding the node lock.
	 */

	changed = isc_mem_get(rbtdb->common.mctx, sizeof *changed);
Bob Halley's avatar
Bob Halley committed
449
450
451
452
453
454
455
456
457
458
459
460
461

	LOCK(&rbtdb->lock);

	REQUIRE(version->writer);

	if (changed != NULL) {
		INSIST(node->references > 0);
		node->references++;
		INSIST(node->references != 0);
		changed->node = node;
		changed->dirty = ISC_FALSE;
		APPEND(version->changed_list, changed, link);
	} else
462
		version->commit_ok = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
463
464

	UNLOCK(&rbtdb->lock);
465
466
467
468
469
470
471
472

	return (changed);
}

static inline void
free_rdataset(isc_mem_t *mctx, rdatasetheader_t *rdataset) {
	unsigned int size;

Bob Halley's avatar
Bob Halley committed
473
474
475
476
477
	if ((rdataset->attributes & RDATASET_ATTR_NONEXISTENT) != 0)
		size = sizeof *rdataset;
	else
		size = dns_rdataslab_size((unsigned char *)rdataset,
					  sizeof *rdataset);
478
479
480
	isc_mem_put(mctx, rdataset, size);
}

481
static inline void
482
483
rollback_node(dns_rbtnode_t *node, rbtdb_serial_t serial) {
	rdatasetheader_t *header, *dcurrent;
484
	isc_boolean_t make_dirty = ISC_FALSE;
485
486
487
488
489

	/*
	 * Caller must hold the node lock.
	 */

490
491
492
493
494
495
	/*
	 * We set the IGNORE attribute on rdatasets with serial number
	 * 'serial'.  When the reference count goes to zero, these rdatasets
	 * will be cleaned up; until that time, they will be ignored.
	 */
	for (header = node->data; header != NULL; header = header->next) {
496
		if (header->serial == serial) {
497
			header->attributes |= RDATASET_ATTR_IGNORE;
498
499
			make_dirty = ISC_TRUE;
		}
500
501
502
		for (dcurrent = header->down;
		     dcurrent != NULL;
		     dcurrent = dcurrent->down) {
503
			if (dcurrent->serial == serial) {
504
				dcurrent->attributes |= RDATASET_ATTR_IGNORE;
505
506
				make_dirty = ISC_TRUE;
			}
507
508
		}
	}
509
510
	if (make_dirty)
		node->dirty = 1;
511
512
513
514
}

static inline void
clean_cache_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
515
	rdatasetheader_t *current, *dcurrent, *top_prev, *top_next, *down_next;
516
517
518
519
520
521
	isc_mem_t *mctx = rbtdb->common.mctx;

	/*
	 * Caller must be holding the node lock.
	 */

522
	top_prev = NULL;
523
524
525
526
	for (current = node->data; current != NULL; current = top_next) {
		top_next = current->next;
		dcurrent = current->down;
		if (dcurrent != NULL) {
527
			do {
528
529
530
531
532
533
				down_next = dcurrent->down;
				free_rdataset(mctx, dcurrent);
				dcurrent = down_next;
			} while (dcurrent != NULL);
			current->down = NULL;
		}
534
535
536
537
538
		/*
		 * If current is nonexistent or stale, we can clean it up.
		 */
		if ((current->attributes &
		     (RDATASET_ATTR_NONEXISTENT|RDATASET_ATTR_STALE)) != 0) {
539
540
			if (top_prev != NULL)
				top_prev->next = current->next;
541
542
543
			else
				node->data = current->next;
			free_rdataset(mctx, current);
544
545
		} else
			top_prev = current;
546
	}
547
	node->dirty = 0;
548
549
550
551
552
553
}

static inline void
clean_zone_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
		rbtdb_serial_t least_serial)
{
554
555
	rdatasetheader_t *current, *dcurrent, *down_next, *dparent;
	rdatasetheader_t *top_prev, *top_next;
556
557
558
559
560
561
562
563
	isc_mem_t *mctx = rbtdb->common.mctx;
	isc_boolean_t still_dirty = ISC_FALSE;

	/*
	 * Caller must be holding the node lock.
	 */
	REQUIRE(least_serial != 0);

564
	top_prev = NULL;
565
566
	for (current = node->data; current != NULL; current = top_next) {
		top_next = current->next;
567

568
569
		/*
		 * Find the first rdataset less than the least serial, if
570
		 * any.  On the way down, clean up any instances of multiple
571
572
		 * rdatasets with the same serial number, or that have the
		 * IGNORE attribute.
573
574
575
576
		 */
		dparent = current;
		for (dcurrent = current->down;
		     dcurrent != NULL;
577
578
579
		     dcurrent = down_next) {
			down_next = dcurrent->down;
			INSIST(dcurrent->serial <= dparent->serial);
580
581
			if (dcurrent->serial < least_serial)
				break;
582
583
			if (dcurrent->serial == dparent->serial ||
			    IGNORE(dcurrent)) {
584
585
586
				if (down_next != NULL)
					down_next->next = dparent;
				dparent->down = down_next;
587
588
589
				free_rdataset(mctx, dcurrent);
			} else
				dparent = dcurrent;
590
		}
591

592
593
594
595
596
597
598
		/*
		 * If there is a such an rdataset, delete it and any older
		 * versions.
		 */
		if (dcurrent != NULL) {
			do {
				down_next = dcurrent->down;
599
				INSIST(dcurrent->serial <= least_serial);
600
601
602
603
604
				free_rdataset(mctx, dcurrent);
				dcurrent = down_next;
			} while (dcurrent != NULL);
			dparent->down = NULL;
		}
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637

		/*
		 * We've eliminated all IGNORE datasets with the possible
		 * exception of current, which we now check.
		 */
		if (IGNORE(current)) {
			down_next = current->down;
			if (down_next == NULL) {
				if (top_prev != NULL)
					top_prev->next = current->next;
				else
					node->data = current->next;
				free_rdataset(mctx, current);
				/*
				 * current no longer exists, so we can
				 * just continue with the loop.
				 */
				continue;
			} else {
				/*
				 * Pull up current->down, making it the new
				 * current.
				 */
				if (top_prev != NULL)
					top_prev->next = down_next;
				else
					node->data = down_next;
				down_next->next = top_next;
				free_rdataset(mctx, current);
				current = down_next;
			}
		}

638
639
640
		/*
		 * Note.  The serial number of 'current' might be less than
		 * least_serial too, but we cannot delete it because it is
Bob Halley's avatar
Bob Halley committed
641
		 * the most recent version, unless it is a NONEXISTENT
642
		 * rdataset or is IGNOREd.
643
		 */
644
		if (current->down != NULL) {
645
			still_dirty = ISC_TRUE;
646
647
			top_prev = current;
		} else {
Bob Halley's avatar
Bob Halley committed
648
649
650
651
652
			/*
			 * If this is a NONEXISTENT rdataset, we can delete it.
			 */
			if ((current->attributes & RDATASET_ATTR_NONEXISTENT)
			    != 0) {
653
654
				if (top_prev != NULL)
					top_prev->next = current->next;
Bob Halley's avatar
Bob Halley committed
655
656
657
				else
					node->data = current->next;
				free_rdataset(mctx, current);
658
659
			} else
				top_prev = current;
Bob Halley's avatar
Bob Halley committed
660
		}
661
662
663
664
665
	}
	if (!still_dirty)
		node->dirty = 0;
}

666
667
668
669
670
671
672
673
674
675
static inline void
new_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
	if (node->references == 0) {
		rbtdb->node_locks[node->locknum].references++;
		INSIST(rbtdb->node_locks[node->locknum].references != 0);
	}
	node->references++;
	INSIST(node->references != 0);
}

676
static void
677
678
679
680
681
682
no_references(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
	      rbtdb_serial_t least_serial)
{
	/*
	 * Caller must be holding the node lock.
	 */
683

684
685
	REQUIRE(node->references == 0);

686
687
688
689
690
691
692
693
694
695
696
697
698
699
	if (node->dirty) {
		if ((rbtdb->common.attributes & DNS_DBATTR_CACHE) != 0)
			clean_cache_node(rbtdb, node);
		else {
			if (least_serial == 0) {
				/*
				 * Caller doesn't know the least serial.
				 * Get it.
				 */
				LOCK(&rbtdb->lock);
				least_serial = rbtdb->least_serial;
				UNLOCK(&rbtdb->lock);
			}
			clean_zone_node(rbtdb, node, least_serial);
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
		}
	}

	INSIST(rbtdb->node_locks[node->locknum].references > 0);
	rbtdb->node_locks[node->locknum].references--;
}

static inline void
make_least_version(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
		   rbtdb_changedlist_t *cleanup_list)
{
	/*
	 * Caller must be holding the database lock.
	 */

	rbtdb->least_serial = version->serial;
	*cleanup_list = version->changed_list;
717
	ISC_LIST_INIT(version->changed_list);
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
}

static inline void
cleanup_nondirty(rbtdb_version_t *version, rbtdb_changedlist_t *cleanup_list) {
	rbtdb_changed_t *changed, *next_changed;

	/*
	 * If the changed record is dirty, then
	 * an update created multiple versions of
	 * a given rdataset.  We keep this list
	 * until we're the least open version, at
	 * which point it's safe to get rid of any
	 * older versions.
	 *
	 * If the changed record isn't dirty, then
	 * we don't need it anymore since we're
	 * committing and not rolling back.
Bob Halley's avatar
Bob Halley committed
735
736
	 *
	 * The caller must be holding the database lock.
737
738
739
740
741
742
743
744
745
746
747
748
	 */
	for (changed = HEAD(version->changed_list);
	     changed != NULL;
	     changed = next_changed) {
		next_changed = NEXT(changed, link);
		if (!changed->dirty) {
			UNLINK(version->changed_list,
			       changed, link);
			APPEND(*cleanup_list,
			       changed, link);
		}
	}
Bob Halley's avatar
Bob Halley committed
749
750
751
}

static void
Bob Halley's avatar
Bob Halley committed
752
closeversion(dns_db_t *db, dns_dbversion_t **versionp, isc_boolean_t commit) {
Bob Halley's avatar
Bob Halley committed
753
	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
754
	rbtdb_version_t *version, *cleanup_version, *least_greater;
755
756
757
758
759
760
761
762
	isc_boolean_t rollback = ISC_FALSE;
	rbtdb_changedlist_t cleanup_list;
	rbtdb_changed_t *changed, *next_changed;
	rbtdb_serial_t serial, least_serial;
	dns_rbtnode_t *rbtnode;
	
	REQUIRE(VALID_RBTDB(rbtdb));
	version = (rbtdb_version_t *)*versionp;
Bob Halley's avatar
Bob Halley committed
763

764
765
	cleanup_version = NULL;
	ISC_LIST_INIT(cleanup_list);
Bob Halley's avatar
Bob Halley committed
766

767
	LOCK(&rbtdb->lock);
Bob Halley's avatar
Bob Halley committed
768
769
	INSIST(version->references > 0);
	INSIST(!version->writer || !(commit && version->references > 1));
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
	version->references--;
	serial = version->serial;
	if (version->references == 0) {
		if (version->writer) {
			if (commit) {
				INSIST(version->commit_ok);
				INSIST(version == rbtdb->future_version);
				if (EMPTY(rbtdb->open_versions)) {
					/*
					 * We're going to become the least open
					 * version.
					 */
					make_least_version(rbtdb, version,
							   &cleanup_list);
				} else {
					/*
					 * Some other open version is the
					 * least version.  We can't cleanup
					 * records that were changed in this
					 * version because the older versions
					 * may still be in use by an open
					 * version.
					 *
					 * We can, however, discard the
					 * changed records for things that
					 * we've added that didn't exist in
					 * prior versions.
					 */
					cleanup_nondirty(version,
							 &cleanup_list);
				}
				/*
				 * If the (soon to be former) current version
				 * isn't being used by anyone, we can clean
				 * it up.
				 */
				if (rbtdb->current_version->references == 0)
					cleanup_version =
						rbtdb->current_version;
				/*
				 * Become the current version.
				 */
812
				version->writer = ISC_FALSE;
813
814
815
816
817
818
819
820
821
822
				rbtdb->current_version = version;
				rbtdb->current_serial = version->serial;
				rbtdb->future_version = NULL;
			} else {
				/*
				 * We're rolling back this transaction.
				 */
				cleanup_list = version->changed_list;
				rollback = ISC_TRUE;
				cleanup_version = version;
823
				rbtdb->future_version = NULL;
824
825
826
827
828
829
830
831
			}
		} else {
			if (version != rbtdb->current_version) {
				/*
				 * There are no external or internal references
				 * to this version and it can be cleaned up.
				 */
				cleanup_version = version;
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883

				/*
				 * Find the version with the least serial
				 * number greater than ours.
				 */
				least_greater = PREV(version, link);
				if (least_greater == NULL)
					least_greater = rbtdb->current_version;

				/*
				 * Is this the least open version?
				 */
				if (version->serial == rbtdb->least_serial) {
					/*
					 * Yes.  Install the new least open
					 * version.
					 */
					make_least_version(rbtdb,
							   least_greater,
							   &cleanup_list);
				} else {
					/*
					 * Add any unexecuted cleanups to
					 * those of the least greater version.
					 */
					APPENDLIST(least_greater->changed_list,
						   version->changed_list,
						   link);
				}
			}
			UNLINK(rbtdb->open_versions, version, link);
		}
	}
	least_serial = rbtdb->least_serial;
	UNLOCK(&rbtdb->lock);

	if (cleanup_version != NULL)
		isc_mem_put(rbtdb->common.mctx, cleanup_version,
			    sizeof *cleanup_version);

	if (!EMPTY(cleanup_list)) {
		for (changed = HEAD(cleanup_list);
		     changed != NULL;
		     changed = next_changed) {
			next_changed = NEXT(changed, link);
			rbtnode = changed->node;

			LOCK(&rbtdb->node_locks[rbtnode->locknum].lock);

			INSIST(rbtnode->references > 0);
			rbtnode->references--;
			if (rollback)
884
				rollback_node(rbtnode, serial);
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

			if (rbtnode->references == 0)
				no_references(rbtdb, rbtnode, least_serial);

			UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock);

			isc_mem_put(rbtdb->common.mctx, changed,
				    sizeof *changed);
		}
	}

	*versionp = NULL;
}

static dns_result_t
findnode(dns_db_t *db, dns_name_t *name, isc_boolean_t create,
	 dns_dbnode_t **nodep)
{
	dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
	dns_rbtnode_t *node = NULL;
	dns_name_t nodename;
	unsigned int locknum;
	dns_result_t result;
	isc_rwlocktype_t locktype = isc_rwlocktype_read;

	REQUIRE(VALID_RBTDB(rbtdb));

	dns_name_init(&nodename, NULL);
	RWLOCK(&rbtdb->tree_lock, locktype);
	result = dns_rbt_findnode(rbtdb->tree, name, NULL, &node, NULL,
				  ISC_TRUE, NULL, NULL);
916
	if (result != DNS_R_SUCCESS) {
917
918
919
920
921
922
923
924
925
926
		RWUNLOCK(&rbtdb->tree_lock, locktype);
		if (!create) {
			if (result == DNS_R_PARTIALMATCH)
				result = DNS_R_NOTFOUND;
			return (result);
		}
		/*
		 * It would be nice to try to upgrade the lock instead of
		 * unlocking then relocking.
		 */
927
		locktype = isc_rwlocktype_write;
928
		RWLOCK(&rbtdb->tree_lock, locktype);
929
		node = NULL;
930
		result = dns_rbt_addnode(rbtdb->tree, name, &node);
931
932
933
934
935
		if (result == DNS_R_SUCCESS) {
			dns_rbt_namefromnode(node, &nodename);
			node->locknum = dns_name_hash(&nodename, ISC_TRUE) %
				rbtdb->node_lock_count;
		} else if (result != DNS_R_EXISTS) {
936
937
938
939
			RWUNLOCK(&rbtdb->tree_lock, locktype);
			return (result);
		}
	}
940
941
942
943
	locknum = node->locknum;
	LOCK(&rbtdb->node_locks[locknum].lock);
	new_reference(rbtdb, node);
	UNLOCK(&rbtdb->node_locks[locknum].lock);
944
945
946
947
	RWUNLOCK(&rbtdb->tree_lock, locktype);

	*nodep = (dns_dbnode_t *)node;

948
	return (DNS_R_SUCCESS);
949
950
951
952
953
954
955
956
957
}

static dns_result_t
zone_zonecut_callback(dns_rbtnode_t *node, dns_name_t *name, void *arg) {
	rbtdb_search_t *search = arg;
	rdatasetheader_t *header, *header_next;
	rdatasetheader_t *found;
	dns_result_t result;

Bob Halley's avatar
Bob Halley committed
958
959
960
961
962
	/*
	 * We only want to remember the topmost zone cut, since it's the one
	 * that counts, so we'll just continue if we've already found a
	 * zonecut.
	 */
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
	if (search->zonecut != NULL)
		return (DNS_R_CONTINUE);

	found = NULL;
	result = DNS_R_CONTINUE;

	LOCK(&(search->rbtdb->node_locks[node->locknum].lock));
	
	/*
	 * Look for an NS or DNAME rdataset active in our version.
	 */
	for (header = node->data; header != NULL; header = header_next) {
		header_next = header->next;
		if (header->type == dns_rdatatype_ns ||
		    header->type == dns_rdatatype_dname) {
			do {
979
980
				if (header->serial <= search->serial &&
				    !IGNORE(header)) {
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
					/*
					 * Is this a "this rdataset doesn't
					 * exist" record?
					 */
					if ((header->attributes &
					     RDATASET_ATTR_NONEXISTENT) != 0)
						header = NULL;
					break;
				} else
					header = header->down;
			} while (header != NULL);
			if (header != NULL) {
				found = header;
				/*
				 * If we found a DNAME, then we don't need
				 * to keep looking for NS records, because the
				 * DNAME has precedence.
				 */
				if (found->type == dns_rdatatype_dname)
					break;
1001
			}
1002
1003
1004
1005
		}
	}

	if (found != NULL) {
Bob Halley's avatar
Bob Halley committed
1006
1007
1008
1009
		/*
		 * We increment the reference count on node to ensure that
		 * search->zonecut_rdataset will still be valid later.
		 */
1010
1011
1012
1013
		new_reference(search->rbtdb, node);
		search->zonecut = node;
		search->zonecut_rdataset = found;
		search->need_cleanup = ISC_TRUE;
Bob Halley's avatar
Bob Halley committed
1014
1015
1016
1017
1018
1019
		/*
		 * Since we've found a zonecut, anything beneath it is
		 * glue and is not subject to wildcard matching, so we
		 * may clear search->wild.
		 */
		search->wild = ISC_FALSE;
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
		if (found->type == dns_rdatatype_dname) {
			/*
			 * Finding a DNAME stops all further searching.
			 *
			 * Note: We return DNS_R_PARTIALMATCH instead of
			 * DNS_R_DNAME here because that way zone_find()
			 * does fewer result code comparisions.
			 */
			result = DNS_R_PARTIALMATCH;
		} else if ((search->options & DNS_DBFIND_GLUEOK) == 0) {
			/*
			 * If the caller does not want to find glue, then
			 * this is the best answer and the search should
			 * stop now.
			 *
			 * Note: We return DNS_R_PARTIALMATCH instead of
1036
			 * DNS_R_DELEGATION here because that way zone_find()
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
			 * does fewer result code comparisions.
			 */
			result = DNS_R_PARTIALMATCH;
		} else {
			dns_name_t *zcname;

			/*
			 * The search will continue beneath the zone cut.
			 * This may or may not be the best match.  In case it
			 * is, we need to remember the node name.
			 */
			zcname = dns_fixedname_name(&search->zonecut_name);
			RUNTIME_CHECK(dns_name_concatenate(name, NULL, zcname,
							   NULL) ==
				      DNS_R_SUCCESS);
			search->copy_name = ISC_TRUE;
		}
Bob Halley's avatar
Bob Halley committed
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
	} else {
		/*
		 * There is no zonecut at this node which is active in this
		 * version.
		 *
		 * If this is a "wild" node and the caller hasn't disabled
		 * wildcard matching, remember that we've seen a wild node
		 * in case we need to go searching for wildcard matches
		 * later on.
		 */
		if (node->wild && (search->options & DNS_DBFIND_NOWILD) == 0)
			search->wild = ISC_TRUE;
1066
1067
1068
1069
1070
1071
1072
1073
1074
	}

	UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock));

	return (result);
}

static inline void
bind_rdataset(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
1075
1076
	      rdatasetheader_t *header, isc_stdtime_t now,
	      dns_rdataset_t *rdataset)
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
{
	unsigned char *raw;
	unsigned int count;

	/*
	 * Caller must be holding the node lock.
	 */

	new_reference(rbtdb, node);

	rdataset->methods = &rdataset_methods;
	rdataset->rdclass = rbtdb->common.rdclass;
	rdataset->type = header->type;
1090
	rdataset->ttl = header->ttl - now;
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
	rdataset->private1 = rbtdb;
	rdataset->private2 = node;
	raw = (unsigned char *)header + sizeof *header;
	rdataset->private3 = raw;
	count = raw[0] * 256 + raw[1];
	raw += 2;
	if (count == 0) {
		rdataset->private4 = (void *)0;
		rdataset->private5 = NULL;
	} else {
		/*
		 * The private4 field is the number of rdata beyond
		 * the cursor position, so we decrement the total
		 * count by one before storing it.
		 */
		count--;
		rdataset->private4 = (void *)count; 
		rdataset->private5 = raw;
	}
}

static inline dns_result_t
setup_delegation(rbtdb_search_t *search, dns_dbnode_t **nodep,
		 dns_name_t *foundname, dns_rdataset_t *rdataset)
{
	dns_result_t result;
	dns_name_t *zcname;
Bob Halley's avatar
Bob Halley committed
1118
1119
	dns_rdatatype_t type;
	dns_rbtnode_t *node;
1120
1121

	/*
Bob Halley's avatar
Bob Halley committed
1122
	 * The caller MUST NOT be holding any node locks.
1123
1124
	 */

Bob Halley's avatar
Bob Halley committed
1125
1126
1127
	node = search->zonecut;
	type = search->zonecut_rdataset->type;

1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
	/*
	 * If we have to set foundname, we do it before anything else.
	 * If we were to set foundname after we had set nodep or bound the
	 * rdataset, then we'd have to undo that work if dns_name_concatenate()
	 * failed.  By setting foundname first, there's nothing to undo if
	 * we have trouble.
	 */
	if (foundname != NULL && search->copy_name) {
		zcname = dns_fixedname_name(&search->zonecut_name);
		result = dns_name_concatenate(zcname, NULL, foundname, NULL);
		if (result != DNS_R_SUCCESS)
			return (result);
	}
	if (nodep != NULL) {
		/*
		 * Note that we don't have to increment the node's reference
		 * count here because we're going to use the reference we
		 * already have in the search block.
		 */
Bob Halley's avatar
Bob Halley committed
1147
		*nodep = node;
1148
1149
		search->need_cleanup = ISC_FALSE;
	}
Bob Halley's avatar
Bob Halley committed
1150
1151
1152
	if (rdataset != NULL) {
		LOCK(&(search->rbtdb->node_locks[node->locknum].lock));
		bind_rdataset(search->rbtdb, node, search->zonecut_rdataset,
1153
			      search->now, rdataset);
Bob Halley's avatar
Bob Halley committed
1154
1155
		UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock));
	}
1156

Bob Halley's avatar
Bob Halley committed
1157
	if (type == dns_rdatatype_dname)
1158
1159
1160
1161
		return (DNS_R_DNAME);
	return (DNS_R_DELEGATION);
}

Bob Halley's avatar
Bob Halley committed
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
static inline isc_boolean_t
valid_glue(rbtdb_search_t *search, dns_name_t *name, dns_rdatatype_t type,
	   dns_rbtnode_t *node)
{
	unsigned char *raw;
	unsigned int count, size;
	dns_name_t ns_name;
	isc_boolean_t valid = ISC_FALSE;
	dns_offsets_t offsets;
	isc_region_t region;
	rdatasetheader_t *header;

	/*
	 * No additional locking is required.
	 */

	/*
	 * Valid glue types are A, AAAA, A6.  NS is also a valid glue type
	 * if it occurs at a zone cut, but is not valid below it.
	 */
	if (type == dns_rdatatype_ns) {
		if (node != search->zonecut) {
			return (ISC_FALSE);
		}
	} else if (type != dns_rdatatype_a &&
		   type != dns_rdatatype_aaaa &&
		   type != dns_rdatatype_a6) {
		return (ISC_FALSE);
	}

	header = search->zonecut_rdataset;
	raw = (unsigned char *)header + sizeof *header;
	count = raw[0] * 256 + raw[1];
	raw += 2;

	while (count > 0) {
		count--;
		size = raw[0] * 256 + raw[1];
		raw += 2;
		region.base = raw;
		region.length = size;
		raw += size;
		/*
		 * XXX Until we have rdata structures, we have no choice but
		 * to directly access the rdata format.
		 */
		dns_name_init(&ns_name, offsets);
		dns_name_fromregion(&ns_name, &region);
		if (dns_name_compare(&ns_name, name) == 0) {
			valid = ISC_TRUE;
			break;
		}
	}

	return (valid);
}

Bob Halley's avatar
Bob Halley committed
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
static inline isc_result_t
find_wildcard(rbtdb_search_t *search, dns_rbtnode_t **nodep) {
	unsigned int i, j;
	dns_rbtnode_t *node, *level_node, *wnode;
	rdatasetheader_t *header;
	dns_result_t result = DNS_R_NOTFOUND;
	dns_name_t name;
	dns_name_t *wname;
	dns_fixedname_t fwname;
	dns_rbtdb_t *rbtdb;
	isc_boolean_t wild, active;

	/*
	 * Caller must be holding the tree lock and MUST NOT be holding
	 * any node locks.
	 */

	/*
	 * Examine each ancestor level.  If the level's wild bit
	 * is set, then construct the corresponding wildcard name and
	 * search for it.  If the wildcard node exists, and is active in
	 * this version, we're done.  If not, then we next check to see
	 * if the ancestor is active in this version.  If so, then there
	 * can be no possible wildcard match and again we're done.  If not,
	 * continue the search.
	 */

	rbtdb = search->rbtdb;
	i = search->chain.level_matches;
	while (i > 0) {
		i--;
		node = search->chain.levels[i];

		LOCK(&(rbtdb->node_locks[node->locknum].lock));

		/*
		 * First we try to figure out if this node is active in
		 * the search's version.  We do this now, even though we
		 * may not need the information, because it simplifies the
		 * locking and code flow.
		 */
		for (header = node->data;
		     header != NULL;
		     header = header->next) {
			if (header->serial <= search->serial &&
			    !IGNORE(header) && EXISTS(header))
				break;
		}
		if (header != NULL)
			active = ISC_TRUE;
		else
			active = ISC_FALSE;
		
		if (node->wild)
			wild = ISC_TRUE;
		else
			wild = ISC_FALSE;

		UNLOCK(&(rbtdb->node_locks[node->locknum].lock));

		if (wild) {
			/*
			 * Construct the wildcard name for this level.
			 */
			dns_name_init(&name, NULL);
			dns_rbt_namefromnode(node, &name);
			dns_fixedname_init(&fwname);
			wname = dns_fixedname_name(&fwname);
			result = dns_name_concatenate(dns_wildcardname, &name,
						      wname, NULL);
			j = i;
			while (result == DNS_R_SUCCESS && j != 0) {
				j--;
				level_node = search->chain.levels[j];
				dns_name_init(&name, NULL);
				dns_rbt_namefromnode(level_node, &name);
				result = dns_name_concatenate(wname,
							      &name,
							      wname,
							      NULL);
			}
			if (result != DNS_R_SUCCESS)
				break;

			wnode = NULL;
			result = dns_rbt_findnode(rbtdb->tree, wname,
						  NULL, &wnode, NULL,
						  ISC_TRUE, NULL, NULL);
			if (result == DNS_R_SUCCESS) {
			    /*
			     * We have found the wildcard node.  If it
			     * is active in the search's version, we're
			     * done.
			     */
			    LOCK(&(rbtdb->node_locks[wnode->locknum].lock));
			    for (header = wnode->data;
				 header != NULL;
				 header = header->next) {
				    if (header->serial <= search->serial &&
					!IGNORE(header) && EXISTS(header))
					    break;
			    }
			    UNLOCK(&(rbtdb->node_locks[wnode->locknum].lock));
			    if (header != NULL) {
				    /*
				     * The wildcard node is active!
				     *
				     * Note: result is still DNS_R_SUCCESS
				     * so we don't have to set it.
				     */
				    *nodep = wnode;
				    break;
			    }
			} else if (result != DNS_R_NOTFOUND &&
				   result != DNS_R_PARTIALMATCH) {
				/*
				 * An error has occurred.  Bail out.
				 */
				break;
			}
		}
		
		if (active) {
			/*
			 * The level node is active.  Any wildcarding
			 * present at higher levels has no
			 * effect and we're done.
			 */
			result = DNS_R_NOTFOUND;
			break;
		}
	}

	return (result);
}

1355
1356
static dns_result_t
zone_find(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
1357
	  dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
1358
1359
1360
1361
1362
1363
1364
1365
1366
	  dns_dbnode_t **nodep, dns_name_t *foundname,
	  dns_rdataset_t *rdataset)
{
	dns_rbtnode_t *node = NULL;
	dns_result_t result;
	rbtdb_search_t search;
	isc_boolean_t cname_ok = ISC_TRUE;
	isc_boolean_t must_succeed = ISC_FALSE;
	isc_boolean_t close_version = ISC_FALSE;
1367
1368
	isc_boolean_t maybe_zonecut = ISC_FALSE;
	isc_boolean_t at_zonecut = ISC_FALSE;
1369
	isc_boolean_t secure_zone;
Bob Halley's avatar
Bob Halley committed
1370
	isc_boolean_t wild;
1371
	isc_boolean_t empty_node;
1372
	rdatasetheader_t *header, *header_next, *found, *nxtheader;
1373
1374
1375
1376
1377

	search.rbtdb = (dns_rbtdb_t *)db;

	REQUIRE(VALID_RBTDB(search.rbtdb));

1378
1379
1380
1381
1382
1383
	/*
	 * We don't care about 'now'.  We set it to zero so compilers won't
	 * complain about it being unused.
	 */
	now = 0;

1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
	/*
	 * If the caller didn't supply a version, attach to the current
	 * version.
	 */
	if (version == NULL) {
		currentversion(db, &version);
		close_version = ISC_TRUE;
	}

	search.rbtversion = version;
	search.serial = search.rbtversion->serial;
	search.options = options;
	search.copy_name = ISC_FALSE;
	search.need_cleanup = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
1398
	search.wild = ISC_FALSE;
1399
1400
1401
	search.zonecut = NULL;
	dns_fixedname_init(&search.zonecut_name);
	dns_rbtnodechain_init(&search.chain, search.rbtdb->common.mctx);
1402
	search.now = 0;
1403
1404
1405
1406
1407
1408

	/*
	 * XXXDNSSEC Set secure_zone properly when implementing DNSSEC.
	 */
	secure_zone = ISC_FALSE;

Bob Halley's avatar
Bob Halley committed
1409
1410
1411
1412
1413
	/*
	 * 'wild' will be true iff. we've matched a wildcard.
	 */
	wild = ISC_FALSE;

1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
	RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);

	/*
	 * Search down from the root of the tree.  If, while going down, we
	 * encounter a callback node, zone_zonecut_callback() will search the
	 * rdatasets at the zone cut for active DNAME or NS rdatasets.
	 */
	result = dns_rbt_findnode(search.rbtdb->tree, name, foundname, &node,
				  &search.chain, ISC_TRUE,
				  zone_zonecut_callback, &search);

	if (result == DNS_R_PARTIALMATCH) {
	partial_match:
		if (search.zonecut != NULL) {
		    result = setup_delegation(&search, nodep, foundname,
					      rdataset);
		    goto tree_exit;
Bob Halley's avatar
Bob Halley committed
1431
1432
1433
		}

		if (search.wild) {
1434
			/*
Bob Halley's avatar
Bob Halley committed
1435
1436
1437
1438
			 * At least one of the levels in the search chain
			 * potentially has a wildcard.  For each such level,
			 * we must see if there's a matching wildcard active
			 * in the current version.
1439
			 */
Bob Halley's avatar
Bob Halley committed
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
			result = find_wildcard(&search, &node);
			if (result == DNS_R_SUCCESS) {
				result = dns_name_concatenate(name, NULL,
							      foundname, NULL);
				if (result != DNS_R_SUCCESS)
					goto tree_exit;	      
				wild = ISC_TRUE;
				goto found;
			}
			else if (result != ISC_R_NOTFOUND)
				goto tree_exit;
1451
		}
Bob Halley's avatar
Bob Halley committed
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462

		/*
		 * If we're here, then the name does not exist, is not
		 * beneath a zonecut, and there's no matching wildcard.
		 */
		result = DNS_R_NXDOMAIN;
		if (secure_zone)
			result = DNS_R_NOTIMPLEMENTED; /* XXXDNSSEC */
		if (nodep != NULL)
			*nodep = NULL;
		goto tree_exit;
1463
1464
1465
	} else if (result != DNS_R_SUCCESS)
		goto tree_exit;

Bob Halley's avatar
Bob Halley committed
1466
 found:
1467
	/*
Bob Halley's avatar
Bob Halley committed
1468
1469
	 * We have found a node whose name is the desired name, or we
	 * have matched a wildcard.
1470
1471
	 */

1472
	if (search.zonecut != NULL) {
Bob Halley's avatar
Bob Halley committed
1473
1474
1475
1476
		/*
		 * If we're beneath a zone cut, we don't want to look for
		 * CNAMEs because they're not legitimate zone glue.
		 */
1477
		cname_ok = ISC_FALSE;
1478
1479
1480
1481
1482
1483
1484
	} else {
		/*
		 * The node may be a zone cut itself.  If it might be one,
		 * make sure we check for it later.
		 */
		if (node->find_callback && node != search.rbtdb->origin_node)
			maybe_zonecut = ISC_TRUE;
1485
1486
1487
1488
1489
1490
	}

	/*
	 * Certain DNSSEC types are not subject to CNAME matching
	 * (RFC 2535, section 2.3.5).
	 *
1491
1492
1493
	 * We don't check for SIG, because we don't store SIG records
	 * directly.
	 *
1494
1495
1496
1497
1498
	 * XXX This should be a general purpose subroutine in the rdata
	 * module.
	 *
	 * XXX This 'if' could be an 'else if' of the 'if' above.
	 */
1499
	if (type == dns_rdatatype_key || type == dns_rdatatype_nxt)
1500
1501
1502
1503
1504
1505
1506
1507
		cname_ok = ISC_FALSE;

	/*
	 * We now go looking for rdata...
	 */

	LOCK(&(search.rbtdb->node_locks[node->locknum].lock));
	
1508
	found = NULL;
1509
1510
1511
1512
1513
1514
1515
1516
	nxtheader = NULL;
	empty_node = ISC_TRUE;
	for (header = node->data; header != NULL; header = header_next) {
		header_next = header->next;
		/*
		 * Look for an active, extant rdataset.
		 */
		do {
1517
1518
			if (header->serial <= search.serial &&
			    !IGNORE(header)) {
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
				/*
				 * Is this a "this rdataset doesn't
				 * exist" record?
				 */
				if ((header->attributes &
				     RDATASET_ATTR_NONEXISTENT) != 0)
					header = NULL;
				break;
			} else
				header = header->down;
		} while (header != NULL);
		if (header != NULL) {
			/*
			 * We now know that there is at least one active
			 * rdataset at this node.
			 */
			empty_node = ISC_FALSE;
1536
1537
1538
1539
1540
1541

			/*
			 * Do special zone cut handling, if requested.
			 */
			if (maybe_zonecut &&
			    header->type == dns_rdatatype_ns) {
Bob Halley's avatar
Bob Halley committed
1542
1543
1544
1545
1546
				/*
				 * We increment the reference count on node to
				 * ensure that search->zonecut_rdataset will
				 * still be valid later.
				 */
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
				new_reference(search.rbtdb, node);
				search.zonecut = node;
				search.zonecut_rdataset = header;
				search.need_cleanup = ISC_TRUE;
				maybe_zonecut = ISC_FALSE;
				at_zonecut = ISC_TRUE;
				if ((search.options & DNS_DBFIND_GLUEOK) == 0
				    && type != dns_rdatatype_nxt
				    && type != dns_rdatatype_key
				    && type != dns_rdatatype_any) {
					/*
					 * Glue is not OK, but any answer we
					 * could return would be glue.  Return
					 * the delegation.
					 */
					found = NULL;
					break;
				}
				if (found != NULL)
					break;
			}

1569
1570
1571
1572
1573
1574
1575
			/*
			 * If we found a type we were looking for, we're done.
			 */
			if (header->type == type ||
			    type == dns_rdatatype_any ||
			    (cname_ok &&
			     header->type == dns_rdatatype_cname)) {
1576
1577
1578
				found = header;
				if (!maybe_zonecut)
					break;
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
			} else if (header->type == dns_rdatatype_nxt) {
				/*
				 * Remember a NXT rdataset even if we're
				 * not specifically looking for it, because
				 * we might need it later.
				 */
				nxtheader = header;
			}
		}
	}

	if (empty_node) {
		/*
		 * We have an exact match for the name, but there are no
		 * active rdatasets the desired version.  That means that
		 * this node doesn't exist in the desired version, and that
		 * we really have a partial match.
Bob Halley's avatar
Bob Halley committed
1596
1597
1598
1599
		 *
		 * If the node is the result of a wildcard match, then
		 * it must be active in the desired version, and hence
		 * empty_node should never be true.  We INSIST upon it.
1600
		 */
Bob Halley's avatar
Bob Halley committed
1601
		INSIST(!wild);
1602
1603
1604
1605
1606
1607
1608
		UNLOCK(&(search.rbtdb->node_locks[node->locknum].lock));
		goto partial_match;
	}

	/*
	 * If we didn't find what we were looking for...
	 */
1609
	if (found == NULL) {
1610
1611
1612
1613
1614
1615
1616
		if (must_succeed) {
			/*
			 * We were looking for a type which must be in the
			 * database, but isn't for some reason.
			 */
			result = DNS_R_BADDB;
		} else if (search.zonecut != NULL) {
Bob Halley's avatar
Bob Halley committed
1617
1618
1619
1620
1621
1622
1623
1624
		    /*
		     * We were trying to find glue at a node beneath a
		     * zone cut, but didn't, so we return the delegation.
		     */
		    UNLOCK(&(search