zt.c 12 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
2
 * Copyright (C) 2004-2007, 2011-2016  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2002  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
Mark Andrews's avatar
Mark Andrews committed
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC 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.
Mark Andrews's avatar
Mark Andrews committed
16 17
 */

Mark Andrews's avatar
Mark Andrews committed
18
/* $Id$ */
19 20

/*! \file */
David Lawrence's avatar
David Lawrence committed
21

Mark Andrews's avatar
Mark Andrews committed
22
#include <config.h>
23

24
#include <isc/file.h>
Bob Halley's avatar
Bob Halley committed
25
#include <isc/magic.h>
26
#include <isc/mem.h>
27
#include <isc/string.h>
Evan Hunt's avatar
Evan Hunt committed
28
#include <isc/task.h>
Michael Graff's avatar
Michael Graff committed
29
#include <isc/util.h>
30

31 32
#include <dns/log.h>
#include <dns/name.h>
David Lawrence's avatar
David Lawrence committed
33
#include <dns/rbt.h>
34
#include <dns/rdataclass.h>
35
#include <dns/result.h>
36
#include <dns/view.h>
37
#include <dns/zone.h>
David Lawrence's avatar
David Lawrence committed
38
#include <dns/zt.h>
Mark Andrews's avatar
Mark Andrews committed
39 40

struct dns_zt {
41
	/* Unlocked. */
Brian Wellington's avatar
Brian Wellington committed
42
	unsigned int		magic;
43
	isc_mem_t		*mctx;
Mark Andrews's avatar
Mark Andrews committed
44
	dns_rdataclass_t	rdclass;
45
	isc_rwlock_t		rwlock;
46 47
	dns_zt_allloaded_t	loaddone;
	void *			loaddone_arg;
48
	/* Locked by lock. */
49
	isc_boolean_t		flush;
50
	isc_uint32_t		references;
51
	unsigned int		loads_pending;
52
	dns_rbt_t		*table;
Mark Andrews's avatar
Mark Andrews committed
53 54
};

55
#define ZTMAGIC			ISC_MAGIC('Z', 'T', 'b', 'l')
56
#define VALID_ZT(zt) 		ISC_MAGIC_VALID(zt, ZTMAGIC)
Mark Andrews's avatar
Mark Andrews committed
57

58 59 60 61 62
static void
auto_detach(void *, void *);

static isc_result_t
load(dns_zone_t *zone, void *uap);
Mark Andrews's avatar
Mark Andrews committed
63

64 65 66
static isc_result_t
asyncload(dns_zone_t *zone, void *callback);

67 68 69
static isc_result_t
loadnew(dns_zone_t *zone, void *uap);

70 71 72
static isc_result_t
freezezones(dns_zone_t *zone, void *uap);

73
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
74
doneloading(dns_zt_t *zt, dns_zone_t *zone, isc_task_t *task);
75

76
isc_result_t
Evan Hunt's avatar
Evan Hunt committed
77
dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **ztp) {
78 79 80 81
	dns_zt_t *zt;
	isc_result_t result;

	REQUIRE(ztp != NULL && *ztp == NULL);
Mark Andrews's avatar
Mark Andrews committed
82

Andreas Gustafsson's avatar
Andreas Gustafsson committed
83
	zt = isc_mem_get(mctx, sizeof(*zt));
84
	if (zt == NULL)
85
		return (ISC_R_NOMEMORY);
Mark Andrews's avatar
Mark Andrews committed
86

87
	zt->table = NULL;
88
	result = dns_rbt_create(mctx, auto_detach, zt, &zt->table);
89 90
	if (result != ISC_R_SUCCESS)
		goto cleanup_zt;
91

92
	result = isc_rwlock_init(&zt->rwlock, 0, 0);
93
	if (result != ISC_R_SUCCESS)
94
		goto cleanup_rbt;
95

96 97
	zt->mctx = NULL;
	isc_mem_attach(mctx, &zt->mctx);
98
	zt->references = 1;
99
	zt->flush = ISC_FALSE;
100 101
	zt->rdclass = rdclass;
	zt->magic = ZTMAGIC;
102 103 104
	zt->loaddone = NULL;
	zt->loaddone_arg = NULL;
	zt->loads_pending = 0;
105
	*ztp = zt;
106

107 108 109 110 111 112
	return (ISC_R_SUCCESS);

   cleanup_rbt:
	dns_rbt_destroy(&zt->table);

   cleanup_zt:
Andreas Gustafsson's avatar
Andreas Gustafsson committed
113
	isc_mem_put(mctx, zt, sizeof(*zt));
114 115

	return (result);
Mark Andrews's avatar
Mark Andrews committed
116 117
}

118
isc_result_t
119
dns_zt_mount(dns_zt_t *zt, dns_zone_t *zone) {
120
	isc_result_t result;
Mark Andrews's avatar
Mark Andrews committed
121
	dns_zone_t *dummy = NULL;
122
	dns_name_t *name;
Mark Andrews's avatar
Mark Andrews committed
123 124 125

	REQUIRE(VALID_ZT(zt));

126
	name = dns_zone_getorigin(zone);
127 128 129

	RWLOCK(&zt->rwlock, isc_rwlocktype_write);

130
	result = dns_rbt_addname(zt->table, name, zone);
131
	if (result == ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
132
		dns_zone_attach(zone, &dummy);
133

134 135
	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);

Mark Andrews's avatar
Mark Andrews committed
136 137 138
	return (result);
}

139
isc_result_t
140
dns_zt_unmount(dns_zt_t *zt, dns_zone_t *zone) {
141
	isc_result_t result;
142
	dns_name_t *name;
Mark Andrews's avatar
Mark Andrews committed
143 144 145

	REQUIRE(VALID_ZT(zt));

146
	name = dns_zone_getorigin(zone);
147 148 149

	RWLOCK(&zt->rwlock, isc_rwlocktype_write);

150
	result = dns_rbt_deletename(zt->table, name, ISC_FALSE);
151 152 153

	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);

Mark Andrews's avatar
Mark Andrews committed
154 155 156
	return (result);
}

157
isc_result_t
158
dns_zt_find(dns_zt_t *zt, const dns_name_t *name, unsigned int options,
Bob Halley's avatar
Bob Halley committed
159
	    dns_name_t *foundname, dns_zone_t **zonep)
Mark Andrews's avatar
Mark Andrews committed
160
{
161
	isc_result_t result;
Bob Halley's avatar
Bob Halley committed
162
	dns_zone_t *dummy = NULL;
Bob Halley's avatar
Bob Halley committed
163
	unsigned int rbtoptions = 0;
Mark Andrews's avatar
Mark Andrews committed
164 165 166

	REQUIRE(VALID_ZT(zt));

Bob Halley's avatar
Bob Halley committed
167 168 169
	if ((options & DNS_ZTFIND_NOEXACT) != 0)
		rbtoptions |= DNS_RBTFIND_NOEXACT;

170
	RWLOCK(&zt->rwlock, isc_rwlocktype_read);
171

Bob Halley's avatar
Bob Halley committed
172
	result = dns_rbt_findname(zt->table, name, rbtoptions, foundname,
Mark Andrews's avatar
Mark Andrews committed
173
				  (void **) (void*)&dummy);
174
	if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
175 176 177
		dns_zone_attach(dummy, zonep);

	RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
Bob Halley's avatar
Bob Halley committed
178

Mark Andrews's avatar
Mark Andrews committed
179 180 181
	return (result);
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
void
dns_zt_attach(dns_zt_t *zt, dns_zt_t **ztp) {

	REQUIRE(VALID_ZT(zt));
	REQUIRE(ztp != NULL && *ztp == NULL);

	RWLOCK(&zt->rwlock, isc_rwlocktype_write);

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

	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);

	*ztp = zt;
}

199 200 201
static isc_result_t
flush(dns_zone_t *zone, void *uap) {
	UNUSED(uap);
202
	return (dns_zone_flush(zone));
203 204
}

205 206 207 208 209 210 211 212 213 214
static void
zt_destroy(dns_zt_t *zt) {
	if (zt->flush)
		(void)dns_zt_apply(zt, ISC_FALSE, flush, NULL);
	dns_rbt_destroy(&zt->table);
	isc_rwlock_destroy(&zt->rwlock);
	zt->magic = 0;
	isc_mem_putanddetach(&zt->mctx, zt, sizeof(*zt));
}

215 216
static void
zt_flushanddetach(dns_zt_t **ztp, isc_boolean_t need_flush) {
217 218 219 220 221 222 223
	isc_boolean_t destroy = ISC_FALSE;
	dns_zt_t *zt;

	REQUIRE(ztp != NULL && VALID_ZT(*ztp));

	zt = *ztp;

224
	RWLOCK(&zt->rwlock, isc_rwlocktype_write);
225

226 227 228 229
	INSIST(zt->references > 0);
	zt->references--;
	if (zt->references == 0)
		destroy = ISC_TRUE;
230 231
	if (need_flush)
		zt->flush = ISC_TRUE;
232 233

	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
234

235 236
	if (destroy)
		zt_destroy(zt);
237 238 239 240

	*ztp = NULL;
}

241 242 243 244 245 246 247 248 249 250
void
dns_zt_flushanddetach(dns_zt_t **ztp) {
	zt_flushanddetach(ztp, ISC_TRUE);
}

void
dns_zt_detach(dns_zt_t **ztp) {
	zt_flushanddetach(ztp, ISC_FALSE);
}

251 252
isc_result_t
dns_zt_load(dns_zt_t *zt, isc_boolean_t stop) {
253
	isc_result_t result;
254 255 256

	REQUIRE(VALID_ZT(zt));

257 258 259 260
	RWLOCK(&zt->rwlock, isc_rwlocktype_read);
	result = dns_zt_apply(zt, stop, load, NULL);
	RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
	return (result);
Mark Andrews's avatar
Mark Andrews committed
261 262
}

263
static isc_result_t
Mark Andrews's avatar
Mark Andrews committed
264
load(dns_zone_t *zone, void *uap) {
265
	isc_result_t result;
266
	UNUSED(uap);
267

268 269 270
	result = dns_zone_load(zone);
	if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE)
		result = ISC_R_SUCCESS;
271 272 273 274 275 276

	return (result);
}

isc_result_t
dns_zt_asyncload(dns_zt_t *zt, dns_zt_allloaded_t alldone, void *arg) {
277
	isc_result_t result;
278
	static dns_zt_zoneloaded_t dl = doneloading;
279 280 281 282
	int pending;

	REQUIRE(VALID_ZT(zt));

283
	RWLOCK(&zt->rwlock, isc_rwlocktype_write);
284 285

	INSIST(zt->loads_pending == 0);
286
	result = dns_zt_apply2(zt, ISC_FALSE, NULL, asyncload, &dl);
287 288 289 290 291 292 293

	pending = zt->loads_pending;
	if (pending != 0) {
		zt->loaddone = alldone;
		zt->loaddone_arg = arg;
	}

294
	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
295 296 297 298

	if (pending == 0)
		alldone(arg);

299
	return (result);
Mark Andrews's avatar
Mark Andrews committed
300 301
}

302
/*
Automatic Updater's avatar
Automatic Updater committed
303
 * Initiates asynchronous loading of zone 'zone'.  'callback' is a
304 305 306 307 308 309
 * pointer to a function which will be used to inform the caller when
 * the zone loading is complete.
 */
static isc_result_t
asyncload(dns_zone_t *zone, void *callback) {
	isc_result_t result;
310
	dns_zt_zoneloaded_t *loaded = callback;
311 312 313 314
	dns_zt_t *zt;

	REQUIRE(zone != NULL);
	zt = dns_zone_getview(zone)->zonetable;
315
	INSIST(VALID_ZT(zt));
316

Evan Hunt's avatar
Evan Hunt committed
317 318 319 320
	INSIST(zt->references > 0);
	zt->references++;
	zt->loads_pending++;

321
	result = dns_zone_asyncload(zone, *loaded, zt);
Evan Hunt's avatar
Evan Hunt committed
322 323 324
	if (result != ISC_R_SUCCESS) {
		zt->references--;
		zt->loads_pending--;
325 326
		INSIST(zt->references > 0);
	}
327 328 329
	return (ISC_R_SUCCESS);
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343
isc_result_t
dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop) {
	isc_result_t result;

	REQUIRE(VALID_ZT(zt));

	RWLOCK(&zt->rwlock, isc_rwlocktype_read);
	result = dns_zt_apply(zt, stop, loadnew, NULL);
	RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
	return (result);
}

static isc_result_t
loadnew(dns_zone_t *zone, void *uap) {
344
	isc_result_t result;
345
	UNUSED(uap);
346

347
	result = dns_zone_loadnew(zone);
348 349
	if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE ||
	    result == DNS_R_DYNAMIC)
350 351
		result = ISC_R_SUCCESS;
	return (result);
352 353
}

354 355 356 357 358 359 360 361 362
isc_result_t
dns_zt_freezezones(dns_zt_t *zt, isc_boolean_t freeze) {
	isc_result_t result, tresult;

	REQUIRE(VALID_ZT(zt));

	RWLOCK(&zt->rwlock, isc_rwlocktype_read);
	result = dns_zt_apply2(zt, ISC_FALSE, &tresult, freezezones, &freeze);
	RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
363 364
	if (tresult == ISC_R_NOTFOUND)
		tresult = ISC_R_SUCCESS;
365 366 367 368 369 370 371 372 373 374
	return ((result == ISC_R_SUCCESS) ? tresult : result);
}

static isc_result_t
freezezones(dns_zone_t *zone, void *uap) {
	isc_boolean_t freeze = *(isc_boolean_t *)uap;
	isc_boolean_t frozen;
	isc_result_t result = ISC_R_SUCCESS;
	char classstr[DNS_RDATACLASS_FORMATSIZE];
	char zonename[DNS_NAME_FORMATSIZE];
375
	dns_zone_t *raw = NULL;
376 377 378 379 380
	dns_view_t *view;
	const char *vname;
	const char *sep;
	int level;

381 382 383 384 385 386
	dns_zone_getraw(zone, &raw);
	if (raw != NULL)
		zone = raw;
	if (dns_zone_gettype(zone) != dns_zone_master) {
		if (raw != NULL)
			dns_zone_detach(&raw);
387
		return (ISC_R_SUCCESS);
388 389 390 391
	}
	if (!dns_zone_isdynamic(zone, ISC_TRUE)) {
		if (raw != NULL)
			dns_zone_detach(&raw);
392
		return (ISC_R_SUCCESS);
393
	}
394 395 396 397 398 399 400

	frozen = dns_zone_getupdatedisabled(zone);
	if (freeze) {
		if (frozen)
			result = DNS_R_FROZEN;
		if (result == ISC_R_SUCCESS)
			result = dns_zone_flush(zone);
401 402
		if (result == ISC_R_SUCCESS)
			dns_zone_setupdatedisabled(zone, freeze);
403 404
	} else {
		if (frozen) {
405
			result = dns_zone_loadandthaw(zone);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
			if (result == DNS_R_CONTINUE ||
			    result == DNS_R_UPTODATE)
				result = ISC_R_SUCCESS;
		}
	}
	view = dns_zone_getview(zone);
	if (strcmp(view->name, "_bind") == 0 ||
	    strcmp(view->name, "_default") == 0)
	{
		vname = "";
		sep = "";
	} else {
		vname = view->name;
		sep = " ";
	}
	dns_rdataclass_format(dns_zone_getclass(zone), classstr,
			      sizeof(classstr));
	dns_name_format(dns_zone_getorigin(zone), zonename, sizeof(zonename));
	level = (result != ISC_R_SUCCESS) ? ISC_LOG_ERROR : ISC_LOG_DEBUG(1);
	isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE,
		      level, "%s zone '%s/%s'%s%s: %s",
		      freeze ? "freezing" : "thawing",
		      zonename, classstr, sep, vname,
		      isc_result_totext(result));
430 431
	if (raw != NULL)
		dns_zone_detach(&raw);
432 433 434
	return (result);
}

435
isc_result_t
436
dns_zt_apply(dns_zt_t *zt, isc_boolean_t stop,
437
	     isc_result_t (*action)(dns_zone_t *, void *), void *uap)
438 439 440 441 442 443 444
{
	return (dns_zt_apply2(zt, stop, NULL, action, uap));
}

isc_result_t
dns_zt_apply2(dns_zt_t *zt, isc_boolean_t stop, isc_result_t *sub,
	      isc_result_t (*action)(dns_zone_t *, void *), void *uap)
445
{
Bob Halley's avatar
Bob Halley committed
446 447
	dns_rbtnode_t *node;
	dns_rbtnodechain_t chain;
448
	isc_result_t result, tresult = ISC_R_SUCCESS;
Bob Halley's avatar
Bob Halley committed
449 450 451
	dns_zone_t *zone;

	REQUIRE(VALID_ZT(zt));
Mark Andrews's avatar
Mark Andrews committed
452
	REQUIRE(action != NULL);
Bob Halley's avatar
Bob Halley committed
453 454 455

	dns_rbtnodechain_init(&chain, zt->mctx);
	result = dns_rbtnodechain_first(&chain, zt->table, NULL, NULL);
456
	if (result == ISC_R_NOTFOUND) {
457 458 459
		/*
		 * The tree is empty.
		 */
460
		tresult = result;
461
		result = ISC_R_NOMORE;
462
	}
463
	while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
464 465
		result = dns_rbtnodechain_current(&chain, NULL, NULL,
						  &node);
466
		if (result == ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
467 468
			zone = node->data;
			if (zone != NULL)
469
				result = (action)(zone, uap);
470 471
			if (result != ISC_R_SUCCESS && stop) {
				tresult = result;
472
				goto cleanup;	/* don't break */
473
			} else if (result != ISC_R_SUCCESS &&
474
				   tresult == ISC_R_SUCCESS)
475
				tresult = result;
Bob Halley's avatar
Bob Halley committed
476 477 478
		}
		result = dns_rbtnodechain_next(&chain, NULL, NULL);
	}
479 480
	if (result == ISC_R_NOMORE)
		result = ISC_R_SUCCESS;
481 482

 cleanup:
Bob Halley's avatar
Bob Halley committed
483
	dns_rbtnodechain_invalidate(&chain);
484 485
	if (sub != NULL)
		*sub = tresult;
486

487
	return (result);
Bob Halley's avatar
Bob Halley committed
488 489
}

490 491 492 493 494
/*
 * Decrement the loads_pending counter; when counter reaches
 * zero, call the loaddone callback that was initially set by
 * dns_zt_asyncload().
 */
495
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
496
doneloading(dns_zt_t *zt, dns_zone_t *zone, isc_task_t *task) {
497
	isc_boolean_t destroy = ISC_FALSE;
498 499 500 501
	dns_zt_allloaded_t alldone = NULL;
	void *arg = NULL;

	UNUSED(zone);
Evan Hunt's avatar
Evan Hunt committed
502
	UNUSED(task);
503 504 505 506 507

	REQUIRE(VALID_ZT(zt));

	RWLOCK(&zt->rwlock, isc_rwlocktype_write);
	INSIST(zt->loads_pending != 0);
508 509 510 511
	INSIST(zt->references != 0);
	zt->references--;
	if (zt->references == 0)
		destroy = ISC_TRUE;
512 513 514 515 516 517 518 519 520 521 522
	zt->loads_pending--;
	if (zt->loads_pending == 0) {
		alldone = zt->loaddone;
		arg = zt->loaddone_arg;
		zt->loaddone = NULL;
		zt->loaddone_arg = NULL;
	}
	RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);

	if (alldone != NULL)
		alldone(arg);
523

524 525 526
	if (destroy)
		zt_destroy(zt);

527
	return (ISC_R_SUCCESS);
528 529
}

Bob Halley's avatar
Bob Halley committed
530 531 532 533
/***
 *** Private
 ***/

Mark Andrews's avatar
Mark Andrews committed
534
static void
535 536
auto_detach(void *data, void *arg) {
	dns_zone_t *zone = data;
Mark Andrews's avatar
Mark Andrews committed
537

538
	UNUSED(arg);
539
	dns_zone_detach(&zone);
Mark Andrews's avatar
Mark Andrews committed
540
}