cache.c 36.8 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10 11
 */

12
/*! \file */
13

14
#include <inttypes.h>
15
#include <stdbool.h>
16

17
#include <isc/mem.h>
18
#include <isc/print.h>
19
#include <isc/refcount.h>
20
#include <isc/stats.h>
21
#include <isc/string.h>
22
#include <isc/task.h>
David Lawrence's avatar
David Lawrence committed
23
#include <isc/time.h>
24
#include <isc/timer.h>
25
#include <isc/util.h>
26 27 28 29 30

#include <dns/cache.h>
#include <dns/db.h>
#include <dns/dbiterator.h>
#include <dns/events.h>
31
#include <dns/lib.h>
32
#include <dns/log.h>
33
#include <dns/masterdump.h>
34 35 36
#include <dns/rdata.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
37
#include <dns/result.h>
38
#include <dns/stats.h>
39

40 41 42 43
#ifdef HAVE_JSON_C
#include <json_object.h>
#endif /* HAVE_JSON_C */

44 45 46 47 48
#ifdef HAVE_LIBXML2
#include <libxml/xmlwriter.h>
#define ISC_XMLCHAR (const xmlChar *)
#endif /* HAVE_LIBXML2 */

49 50
#include "rbtdb.h"

Evan Hunt's avatar
Evan Hunt committed
51
#define CACHE_MAGIC	   ISC_MAGIC('$', '$', '$', '$')
52
#define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC)
53

54
/*!
55
 * Control incremental cleaning.
56 57
 * DNS_CACHE_MINSIZE is how many bytes is the floor for
 * dns_cache_setcachesize(). See also DNS_CACHE_CLEANERINCREMENT
58
 */
59
#define DNS_CACHE_MINSIZE 2097152U /*%< Bytes.  2097152 = 2 MB */
60
/*!
61
 * Control incremental cleaning.
62
 * CLEANERINCREMENT is how many nodes are examined in one pass.
63
 * See also DNS_CACHE_MINSIZE
64
 */
65
#define DNS_CACHE_CLEANERINCREMENT 1000U /*%< Number of nodes. */
66

67
/***
68
 ***	Types
69 70 71
 ***/

/*
Francis Dupont's avatar
Francis Dupont committed
72
 * A cache_cleaner_t encapsulates the state of the periodic
73
 * cache cleaning.
74 75 76 77 78
 */

typedef struct cache_cleaner cache_cleaner_t;

typedef enum {
79 80
	cleaner_s_idle, /*%< Waiting for cleaning-interval to expire. */
	cleaner_s_busy, /*%< Currently cleaning. */
81
	cleaner_s_done	/*%< Freed enough memory after being overmem. */
82
} cleaner_state_t;
83

84 85 86
/*
 * Convenience macros for comprehensive assertion checking.
 */
87 88 89 90 91
#define CLEANER_IDLE(c) \
	((c)->state == cleaner_s_idle && (c)->resched_event != NULL)
#define CLEANER_BUSY(c)                                           \
	((c)->state == cleaner_s_busy && (c)->iterator != NULL && \
	 (c)->resched_event == NULL)
92

93
/*%
94 95 96
 * Accesses to a cache cleaner object are synchronized through
 * task/event serialization, or locked from the cache object.
 */
97
struct cache_cleaner {
98
	isc_mutex_t lock;
Automatic Updater's avatar
Automatic Updater committed
99
	/*%<
100
	 * Locks overmem_event, overmem.  Note: never allocate memory
Automatic Updater's avatar
Automatic Updater committed
101 102 103 104 105
	 * while holding this lock - that could lead to deadlock since
	 * the lock is take by water() which is called from the memory
	 * allocator.
	 */

106
	dns_cache_t *cache;
Evan Hunt's avatar
Evan Hunt committed
107
	isc_task_t *task;
108
	isc_event_t *resched_event; /*% Sent by cleaner task to
109
				     * itself to reschedule */
110
	isc_event_t *overmem_event;
Automatic Updater's avatar
Automatic Updater committed
111

112
	dns_dbiterator_t *iterator;
Evan Hunt's avatar
Evan Hunt committed
113 114 115 116 117
	unsigned int increment; /*% Number of names to
				 * clean in one increment */
	cleaner_state_t state;	/*% Idle/Busy. */
	bool overmem;		/*% The cache is in an overmem state.
				 * */
118
	bool replaceiterator;
119 120
};

121
/*%
122
 * The actual cache object.
123 124 125
 */

struct dns_cache {
Automatic Updater's avatar
Automatic Updater committed
126
	/* Unlocked. */
Evan Hunt's avatar
Evan Hunt committed
127 128 129 130 131 132
	unsigned int magic;
	isc_mutex_t lock;
	isc_mutex_t filelock;
	isc_mem_t *mctx;  /* Main cache memory */
	isc_mem_t *hmctx; /* Heap memory */
	char *name;
133 134
	isc_refcount_t references;
	isc_refcount_t live_tasks;
Automatic Updater's avatar
Automatic Updater committed
135 136

	/* Locked by 'lock'. */
137
	dns_rdataclass_t rdclass;
Evan Hunt's avatar
Evan Hunt committed
138 139 140 141 142 143 144 145
	dns_db_t *db;
	cache_cleaner_t cleaner;
	char *db_type;
	int db_argc;
	char **db_argv;
	size_t size;
	dns_ttl_t serve_stale_ttl;
	isc_stats_t *stats;
Automatic Updater's avatar
Automatic Updater committed
146 147

	/* Locked by 'filelock'. */
148
	char *filename;
Automatic Updater's avatar
Automatic Updater committed
149
	/* Access to the on-disk cache file is also locked by 'filelock'. */
150 151 152
};

/***
153
 ***	Functions
154 155
 ***/

Ondřej Surý's avatar
Ondřej Surý committed
156 157 158
static isc_result_t
cache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,
		   isc_timermgr_t *timermgr, cache_cleaner_t *cleaner);
159

Ondřej Surý's avatar
Ondřej Surý committed
160 161
static void
incremental_cleaning_action(isc_task_t *task, isc_event_t *event);
162

Ondřej Surý's avatar
Ondřej Surý committed
163 164
static void
cleaner_shutdown_action(isc_task_t *task, isc_event_t *event);
165

Ondřej Surý's avatar
Ondřej Surý committed
166 167
static void
overmem_cleaning_action(isc_task_t *task, isc_event_t *event);
168

169
static inline isc_result_t
Evan Hunt's avatar
Evan Hunt committed
170
cache_create_db(dns_cache_t *cache, dns_db_t **db) {
171 172
	isc_result_t result;
	result = dns_db_create(cache->mctx, cache->db_type, dns_rootname,
173 174
			       dns_dbtype_cache, cache->rdclass, cache->db_argc,
			       cache->db_argv, db);
175
	if (result == ISC_R_SUCCESS) {
176
		dns_db_setservestalettl(*db, cache->serve_stale_ttl);
177
	}
178
	return (result);
179 180
}

181
isc_result_t
182
dns_cache_create(isc_mem_t *cmctx, isc_mem_t *hmctx, isc_taskmgr_t *taskmgr,
Automatic Updater's avatar
Automatic Updater committed
183
		 isc_timermgr_t *timermgr, dns_rdataclass_t rdclass,
184
		 const char *cachename, const char *db_type,
Evan Hunt's avatar
Evan Hunt committed
185
		 unsigned int db_argc, char **db_argv, dns_cache_t **cachep) {
Automatic Updater's avatar
Automatic Updater committed
186 187
	isc_result_t result;
	dns_cache_t *cache;
Evan Hunt's avatar
Evan Hunt committed
188 189
	int i, extra = 0;
	isc_task_t *dbtask;
Automatic Updater's avatar
Automatic Updater committed
190 191 192

	REQUIRE(cachep != NULL);
	REQUIRE(*cachep == NULL);
193 194
	REQUIRE(cmctx != NULL);
	REQUIRE(hmctx != NULL);
195
	REQUIRE(cachename != NULL);
Automatic Updater's avatar
Automatic Updater committed
196

197
	cache = isc_mem_get(cmctx, sizeof(*cache));
Automatic Updater's avatar
Automatic Updater committed
198

199 200 201
	cache->mctx = cache->hmctx = NULL;
	isc_mem_attach(cmctx, &cache->mctx);
	isc_mem_attach(hmctx, &cache->hmctx);
Automatic Updater's avatar
Automatic Updater committed
202

203 204
	cache->name = NULL;
	if (cachename != NULL) {
205
		cache->name = isc_mem_strdup(cmctx, cachename);
206 207
	}

Ondřej Surý's avatar
Ondřej Surý committed
208 209
	isc_mutex_init(&cache->lock);
	isc_mutex_init(&cache->filelock);
Automatic Updater's avatar
Automatic Updater committed
210

211
	isc_refcount_init(&cache->references, 1);
212
	isc_refcount_init(&cache->live_tasks, 1);
Automatic Updater's avatar
Automatic Updater committed
213
	cache->rdclass = rdclass;
Mark Andrews's avatar
Mark Andrews committed
214
	cache->serve_stale_ttl = 0;
Automatic Updater's avatar
Automatic Updater committed
215

216 217 218
	cache->stats = NULL;
	result = isc_stats_create(cmctx, &cache->stats,
				  dns_cachestatscounter_max);
219
	if (result != ISC_R_SUCCESS) {
220
		goto cleanup_filelock;
221
	}
222

223
	cache->db_type = isc_mem_strdup(cmctx, db_type);
Automatic Updater's avatar
Automatic Updater committed
224

225 226 227 228 229
	/*
	 * For databases of type "rbt" we pass hmctx to dns_db_create()
	 * via cache->db_argv, followed by the rest of the arguments in
	 * db_argv (of which there really shouldn't be any).
	 */
230
	if (strcmp(cache->db_type, "rbt") == 0) {
231
		extra = 1;
232
	}
233 234 235 236 237

	cache->db_argc = db_argc + extra;
	cache->db_argv = NULL;

	if (cache->db_argc != 0) {
Evan Hunt's avatar
Evan Hunt committed
238 239
		cache->db_argv = isc_mem_get(cmctx,
					     cache->db_argc * sizeof(char *));
240

241
		for (i = 0; i < cache->db_argc; i++) {
Automatic Updater's avatar
Automatic Updater committed
242
			cache->db_argv[i] = NULL;
243
		}
244

245
		cache->db_argv[0] = (char *)hmctx;
246
		for (i = extra; i < cache->db_argc; i++) {
Evan Hunt's avatar
Evan Hunt committed
247 248
			cache->db_argv[i] = isc_mem_strdup(cmctx,
							   db_argv[i - extra]);
Automatic Updater's avatar
Automatic Updater committed
249 250 251
		}
	}

252 253 254
	/*
	 * Create the database
	 */
Automatic Updater's avatar
Automatic Updater committed
255 256
	cache->db = NULL;
	result = cache_create_db(cache, &cache->db);
257
	if (result != ISC_R_SUCCESS) {
Automatic Updater's avatar
Automatic Updater committed
258
		goto cleanup_dbargv;
259
	}
260 261 262
	if (taskmgr != NULL) {
		dbtask = NULL;
		result = isc_task_create(taskmgr, 1, &dbtask);
263
		if (result != ISC_R_SUCCESS) {
264
			goto cleanup_db;
265
		}
Evan Hunt's avatar
Evan Hunt committed
266 267

		isc_task_setname(dbtask, "cache_dbtask", NULL);
268 269 270
		dns_db_settask(cache->db, dbtask);
		isc_task_detach(&dbtask);
	}
Automatic Updater's avatar
Automatic Updater committed
271 272 273 274 275

	cache->filename = NULL;

	cache->magic = CACHE_MAGIC;

276 277 278 279
	/*
	 * RBT-type cache DB has its own mechanism of cache cleaning and doesn't
	 * need the control of the generic cleaner.
	 */
280
	if (strcmp(db_type, "rbt") == 0) {
281
		result = cache_cleaner_init(cache, NULL, NULL, &cache->cleaner);
282
	} else {
283 284 285
		result = cache_cleaner_init(cache, taskmgr, timermgr,
					    &cache->cleaner);
	}
286
	if (result != ISC_R_SUCCESS) {
Automatic Updater's avatar
Automatic Updater committed
287
		goto cleanup_db;
288
	}
Automatic Updater's avatar
Automatic Updater committed
289

290
	result = dns_db_setcachestats(cache->db, cache->stats);
291
	if (result != ISC_R_SUCCESS) {
292
		goto cleanup_db;
293
	}
294

Automatic Updater's avatar
Automatic Updater committed
295 296
	*cachep = cache;
	return (ISC_R_SUCCESS);
297

Ondřej Surý's avatar
Ondřej Surý committed
298
cleanup_db:
Automatic Updater's avatar
Automatic Updater committed
299
	dns_db_detach(&cache->db);
Ondřej Surý's avatar
Ondřej Surý committed
300
cleanup_dbargv:
301 302
	for (i = extra; i < cache->db_argc; i++) {
		if (cache->db_argv[i] != NULL) {
303
			isc_mem_free(cmctx, cache->db_argv[i]);
304 305
		}
	}
306
	if (cache->db_argv != NULL) {
307
		isc_mem_put(cmctx, cache->db_argv,
Automatic Updater's avatar
Automatic Updater committed
308
			    cache->db_argc * sizeof(char *));
309
	}
310
	isc_mem_free(cmctx, cache->db_type);
Ondřej Surý's avatar
Ondřej Surý committed
311
cleanup_filelock:
312
	isc_mutex_destroy(&cache->filelock);
313
	isc_stats_detach(&cache->stats);
314
	isc_mutex_destroy(&cache->lock);
Ondřej Surý's avatar
Ondřej Surý committed
315
	if (cache->name != NULL) {
316
		isc_mem_free(cmctx, cache->name);
Ondřej Surý's avatar
Ondřej Surý committed
317
	}
318 319
	isc_mem_detach(&cache->hmctx);
	isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
Automatic Updater's avatar
Automatic Updater committed
320
	return (result);
321 322
}

323
static void
Evan Hunt's avatar
Evan Hunt committed
324
cache_free(dns_cache_t *cache) {
Automatic Updater's avatar
Automatic Updater committed
325
	REQUIRE(VALID_CACHE(cache));
326 327 328

	isc_refcount_destroy(&cache->references);
	isc_refcount_destroy(&cache->live_tasks);
329

Automatic Updater's avatar
Automatic Updater committed
330
	isc_mem_setwater(cache->mctx, NULL, NULL, 0, 0);
331

332
	if (cache->cleaner.task != NULL) {
Automatic Updater's avatar
Automatic Updater committed
333
		isc_task_detach(&cache->cleaner.task);
334
	}
335

336
	if (cache->cleaner.overmem_event != NULL) {
337
		isc_event_free(&cache->cleaner.overmem_event);
338
	}
339

340
	if (cache->cleaner.resched_event != NULL) {
341
		isc_event_free(&cache->cleaner.resched_event);
342
	}
343

344
	if (cache->cleaner.iterator != NULL) {
345
		dns_dbiterator_destroy(&cache->cleaner.iterator);
346
	}
347

348
	isc_mutex_destroy(&cache->cleaner.lock);
349

Automatic Updater's avatar
Automatic Updater committed
350 351 352 353
	if (cache->filename) {
		isc_mem_free(cache->mctx, cache->filename);
		cache->filename = NULL;
	}
354

355
	if (cache->db != NULL) {
Automatic Updater's avatar
Automatic Updater committed
356
		dns_db_detach(&cache->db);
357
	}
358

Automatic Updater's avatar
Automatic Updater committed
359
	if (cache->db_argv != NULL) {
360 361 362 363 364
		/*
		 * We don't free db_argv[0] in "rbt" cache databases
		 * as it's a pointer to hmctx
		 */
		int extra = 0;
365
		if (strcmp(cache->db_type, "rbt") == 0) {
366
			extra = 1;
367
		}
368
		for (int i = extra; i < cache->db_argc; i++) {
369
			if (cache->db_argv[i] != NULL) {
370
				isc_mem_free(cache->mctx, cache->db_argv[i]);
371 372
			}
		}
Automatic Updater's avatar
Automatic Updater committed
373 374 375
		isc_mem_put(cache->mctx, cache->db_argv,
			    cache->db_argc * sizeof(char *));
	}
376

377
	if (cache->db_type != NULL) {
Automatic Updater's avatar
Automatic Updater committed
378
		isc_mem_free(cache->mctx, cache->db_type);
379
	}
380

381
	if (cache->name != NULL) {
382
		isc_mem_free(cache->mctx, cache->name);
383
	}
384

385
	if (cache->stats != NULL) {
386
		isc_stats_detach(&cache->stats);
387
	}
388

389 390
	isc_mutex_destroy(&cache->lock);
	isc_mutex_destroy(&cache->filelock);
391

Automatic Updater's avatar
Automatic Updater committed
392
	cache->magic = 0;
393 394
	isc_mem_detach(&cache->hmctx);
	isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
395
}
396 397

void
Evan Hunt's avatar
Evan Hunt committed
398
dns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp) {
Automatic Updater's avatar
Automatic Updater committed
399 400
	REQUIRE(VALID_CACHE(cache));
	REQUIRE(targetp != NULL && *targetp == NULL);
401

402
	isc_refcount_increment(&cache->references);
403

Automatic Updater's avatar
Automatic Updater committed
404
	*targetp = cache;
405 406 407
}

void
Evan Hunt's avatar
Evan Hunt committed
408
dns_cache_detach(dns_cache_t **cachep) {
Automatic Updater's avatar
Automatic Updater committed
409 410 411 412 413
	dns_cache_t *cache;

	REQUIRE(cachep != NULL);
	cache = *cachep;
	*cachep = NULL;
414
	REQUIRE(VALID_CACHE(cache));
Automatic Updater's avatar
Automatic Updater committed
415

416 417
	if (isc_refcount_decrement(&cache->references) == 1) {
		cache->cleaner.overmem = false;
Automatic Updater's avatar
Automatic Updater committed
418 419 420 421 422
		/*
		 * When the cache is shut down, dump it to a file if one is
		 * specified.
		 */
		isc_result_t result = dns_cache_dump(cache);
423
		if (result != ISC_R_SUCCESS) {
Automatic Updater's avatar
Automatic Updater committed
424 425 426 427
			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
				      DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
				      "error dumping cache: %s ",
				      isc_result_totext(result));
428
		}
Automatic Updater's avatar
Automatic Updater committed
429 430 431 432

		/*
		 * If the cleaner task exists, let it free the cache.
		 */
433
		if (isc_refcount_decrement(&cache->live_tasks) > 1) {
Automatic Updater's avatar
Automatic Updater committed
434
			isc_task_shutdown(cache->cleaner.task);
435 436
		} else {
			cache_free(cache);
Automatic Updater's avatar
Automatic Updater committed
437
		}
438
	}
439 440 441
}

void
Evan Hunt's avatar
Evan Hunt committed
442
dns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) {
Automatic Updater's avatar
Automatic Updater committed
443 444 445
	REQUIRE(VALID_CACHE(cache));
	REQUIRE(dbp != NULL && *dbp == NULL);
	REQUIRE(cache->db != NULL);
446

Automatic Updater's avatar
Automatic Updater committed
447 448 449
	LOCK(&cache->lock);
	dns_db_attach(cache->db, dbp);
	UNLOCK(&cache->lock);
450 451
}

452
isc_result_t
Evan Hunt's avatar
Evan Hunt committed
453
dns_cache_setfilename(dns_cache_t *cache, const char *filename) {
Automatic Updater's avatar
Automatic Updater committed
454
	char *newname;
455

Automatic Updater's avatar
Automatic Updater committed
456 457
	REQUIRE(VALID_CACHE(cache));
	REQUIRE(filename != NULL);
458

Automatic Updater's avatar
Automatic Updater committed
459
	newname = isc_mem_strdup(cache->mctx, filename);
460

Automatic Updater's avatar
Automatic Updater committed
461
	LOCK(&cache->filelock);
462
	if (cache->filename) {
Automatic Updater's avatar
Automatic Updater committed
463
		isc_mem_free(cache->mctx, cache->filename);
464
	}
Automatic Updater's avatar
Automatic Updater committed
465 466
	cache->filename = newname;
	UNLOCK(&cache->filelock);
467

Automatic Updater's avatar
Automatic Updater committed
468
	return (ISC_R_SUCCESS);
469 470
}

471
isc_result_t
Evan Hunt's avatar
Evan Hunt committed
472
dns_cache_load(dns_cache_t *cache) {
Automatic Updater's avatar
Automatic Updater committed
473
	isc_result_t result;
474

Automatic Updater's avatar
Automatic Updater committed
475
	REQUIRE(VALID_CACHE(cache));
476

477
	if (cache->filename == NULL) {
Automatic Updater's avatar
Automatic Updater committed
478
		return (ISC_R_SUCCESS);
479
	}
480

Automatic Updater's avatar
Automatic Updater committed
481
	LOCK(&cache->filelock);
482 483
	result = dns_db_load(cache->db, cache->filename, dns_masterformat_text,
			     0);
Automatic Updater's avatar
Automatic Updater committed
484
	UNLOCK(&cache->filelock);
485

Automatic Updater's avatar
Automatic Updater committed
486
	return (result);
487 488
}

489
isc_result_t
Evan Hunt's avatar
Evan Hunt committed
490
dns_cache_dump(dns_cache_t *cache) {
Automatic Updater's avatar
Automatic Updater committed
491
	isc_result_t result;
492

Automatic Updater's avatar
Automatic Updater committed
493
	REQUIRE(VALID_CACHE(cache));
494

495
	if (cache->filename == NULL) {
Automatic Updater's avatar
Automatic Updater committed
496
		return (ISC_R_SUCCESS);
497
	}
498

Automatic Updater's avatar
Automatic Updater committed
499 500
	LOCK(&cache->filelock);
	result = dns_master_dump(cache->mctx, cache->db, NULL,
501 502
				 &dns_master_style_cache, cache->filename,
				 dns_masterformat_text, NULL);
Automatic Updater's avatar
Automatic Updater committed
503
	UNLOCK(&cache->filelock);
504
	return (result);
505
}
506

507
const char *
Evan Hunt's avatar
Evan Hunt committed
508
dns_cache_getname(dns_cache_t *cache) {
509 510 511 512 513
	REQUIRE(VALID_CACHE(cache));

	return (cache->name);
}

514
/*
515
 * Initialize the cache cleaner object at *cleaner.
516 517 518
 * Space for the object must be allocated by the caller.
 */

519
static isc_result_t
520
cache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,
Evan Hunt's avatar
Evan Hunt committed
521
		   isc_timermgr_t *timermgr, cache_cleaner_t *cleaner) {
Automatic Updater's avatar
Automatic Updater committed
522
	isc_result_t result;
523

Ondřej Surý's avatar
Ondřej Surý committed
524
	isc_mutex_init(&cleaner->lock);
Automatic Updater's avatar
Automatic Updater committed
525 526 527 528

	cleaner->increment = DNS_CACHE_CLEANERINCREMENT;
	cleaner->state = cleaner_s_idle;
	cleaner->cache = cache;
529
	cleaner->iterator = NULL;
530 531
	cleaner->overmem = false;
	cleaner->replaceiterator = false;
Automatic Updater's avatar
Automatic Updater committed
532 533

	cleaner->task = NULL;
534 535 536
	cleaner->resched_event = NULL;
	cleaner->overmem_event = NULL;

537
	result = dns_db_createiterator(cleaner->cache->db, false,
538
				       &cleaner->iterator);
539
	if (result != ISC_R_SUCCESS) {
540
		goto cleanup;
541
	}
Automatic Updater's avatar
Automatic Updater committed
542 543 544 545 546 547 548 549 550 551

	if (taskmgr != NULL && timermgr != NULL) {
		result = isc_task_create(taskmgr, 1, &cleaner->task);
		if (result != ISC_R_SUCCESS) {
			UNEXPECTED_ERROR(__FILE__, __LINE__,
					 "isc_task_create() failed: %s",
					 dns_result_totext(result));
			result = ISC_R_UNEXPECTED;
			goto cleanup;
		}
552
		isc_refcount_increment(&cleaner->cache->live_tasks);
Automatic Updater's avatar
Automatic Updater committed
553 554 555 556 557
		isc_task_setname(cleaner->task, "cachecleaner", cleaner);

		result = isc_task_onshutdown(cleaner->task,
					     cleaner_shutdown_action, cache);
		if (result != ISC_R_SUCCESS) {
558
			isc_refcount_decrement(&cleaner->cache->live_tasks);
Automatic Updater's avatar
Automatic Updater committed
559 560 561 562 563 564 565
			UNEXPECTED_ERROR(__FILE__, __LINE__,
					 "cache cleaner: "
					 "isc_task_onshutdown() failed: %s",
					 dns_result_totext(result));
			goto cleanup;
		}

566 567 568 569 570 571 572 573
		cleaner->resched_event = isc_event_allocate(
			cache->mctx, cleaner, DNS_EVENT_CACHECLEAN,
			incremental_cleaning_action, cleaner,
			sizeof(isc_event_t));

		cleaner->overmem_event = isc_event_allocate(
			cache->mctx, cleaner, DNS_EVENT_CACHEOVERMEM,
			overmem_cleaning_action, cleaner, sizeof(isc_event_t));
Automatic Updater's avatar
Automatic Updater committed
574
	}
575

Automatic Updater's avatar
Automatic Updater committed
576
	return (ISC_R_SUCCESS);
577

578
cleanup:
579
	if (cleaner->overmem_event != NULL) {
580
		isc_event_free(&cleaner->overmem_event);
581 582
	}
	if (cleaner->resched_event != NULL) {
583
		isc_event_free(&cleaner->resched_event);
584 585
	}
	if (cleaner->task != NULL) {
Automatic Updater's avatar
Automatic Updater committed
586
		isc_task_detach(&cleaner->task);
587 588
	}
	if (cleaner->iterator != NULL) {
589
		dns_dbiterator_destroy(&cleaner->iterator);
590
	}
591
	isc_mutex_destroy(&cleaner->lock);
Ondřej Surý's avatar
Ondřej Surý committed
592

Automatic Updater's avatar
Automatic Updater committed
593
	return (result);
594 595
}

596
static void
Evan Hunt's avatar
Evan Hunt committed
597
begin_cleaning(cache_cleaner_t *cleaner) {
598 599 600 601 602 603 604 605
	isc_result_t result = ISC_R_SUCCESS;

	REQUIRE(CLEANER_IDLE(cleaner));

	/*
	 * Create an iterator, if it does not already exist, and
	 * position it at the beginning of the cache.
	 */
606
	if (cleaner->iterator == NULL) {
607
		result = dns_db_createiterator(cleaner->cache->db, false,
608
					       &cleaner->iterator);
609 610
	}
	if (result != ISC_R_SUCCESS) {
611 612 613
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
			      "cache cleaner could not create "
614 615
			      "iterator: %s",
			      isc_result_totext(result));
616
	} else {
617
		dns_dbiterator_setcleanmode(cleaner->iterator, true);
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
		result = dns_dbiterator_first(cleaner->iterator);
	}
	if (result != ISC_R_SUCCESS) {
		/*
		 * If the result is ISC_R_NOMORE, the database is empty,
		 * so there is nothing to be cleaned.
		 */
		if (result != ISC_R_NOMORE && cleaner->iterator != NULL) {
			UNEXPECTED_ERROR(__FILE__, __LINE__,
					 "cache cleaner: "
					 "dns_dbiterator_first() failed: %s",
					 dns_result_totext(result));
			dns_dbiterator_destroy(&cleaner->iterator);
		} else if (cleaner->iterator != NULL) {
			result = dns_dbiterator_pause(cleaner->iterator);
			RUNTIME_CHECK(result == ISC_R_SUCCESS);
		}
	} else {
		/*
		 * Pause the iterator to free its lock.
		 */
		result = dns_dbiterator_pause(cleaner->iterator);
		RUNTIME_CHECK(result == ISC_R_SUCCESS);

642 643 644 645
		isc_log_write(
			dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
			ISC_LOG_DEBUG(1), "begin cache cleaning, mem inuse %lu",
			(unsigned long)isc_mem_inuse(cleaner->cache->mctx));
646 647 648 649 650 651 652 653
		cleaner->state = cleaner_s_busy;
		isc_task_send(cleaner->task, &cleaner->resched_event);
	}

	return;
}

static void
Evan Hunt's avatar
Evan Hunt committed
654
end_cleaning(cache_cleaner_t *cleaner, isc_event_t *event) {
655 656 657 658 659 660
	isc_result_t result;

	REQUIRE(CLEANER_BUSY(cleaner));
	REQUIRE(event != NULL);

	result = dns_dbiterator_pause(cleaner->iterator);
661
	if (result != ISC_R_SUCCESS) {
662
		dns_dbiterator_destroy(&cleaner->iterator);
663
	}
664 665 666 667 668 669 670 671 672 673 674 675 676 677

	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
		      ISC_LOG_DEBUG(1), "end cache cleaning, mem inuse %lu",
		      (unsigned long)isc_mem_inuse(cleaner->cache->mctx));

	cleaner->state = cleaner_s_idle;
	cleaner->resched_event = event;
}

/*
 * This is called when the cache either surpasses its upper limit
 * or shrinks beyond its lower limit.
 */
static void
Evan Hunt's avatar
Evan Hunt committed
678
overmem_cleaning_action(isc_task_t *task, isc_event_t *event) {
679
	cache_cleaner_t *cleaner = event->ev_arg;
Evan Hunt's avatar
Evan Hunt committed
680
	bool want_cleaning = false;
681 682 683 684 685 686 687 688

	UNUSED(task);

	INSIST(task == cleaner->task);
	INSIST(event->ev_type == DNS_EVENT_CACHEOVERMEM);
	INSIST(cleaner->overmem_event == NULL);

	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
689 690 691 692
		      ISC_LOG_DEBUG(1),
		      "overmem_cleaning_action called, "
		      "overmem = %d, state = %d",
		      cleaner->overmem, cleaner->state);
693 694 695 696

	LOCK(&cleaner->lock);

	if (cleaner->overmem) {
697
		if (cleaner->state == cleaner_s_idle) {
698
			want_cleaning = true;
699
		}
700
	} else {
701
		if (cleaner->state == cleaner_s_busy) {
702 703 704 705 706 707 708 709 710
			/*
			 * end_cleaning() can't be called here because
			 * then both cleaner->overmem_event and
			 * cleaner->resched_event will point to this
			 * event.  Set the state to done, and then
			 * when the incremental_cleaning_action() event
			 * is posted, it will handle the end_cleaning.
			 */
			cleaner->state = cleaner_s_done;
711
		}
712 713 714 715 716 717
	}

	cleaner->overmem_event = event;

	UNLOCK(&cleaner->lock);

718
	if (want_cleaning) {
719
		begin_cleaning(cleaner);
720
	}
721 722 723 724 725 726
}

/*
 * Do incremental cleaning.
 */
static void
Evan Hunt's avatar
Evan Hunt committed
727
incremental_cleaning_action(isc_task_t *task, isc_event_t *event) {
728
	cache_cleaner_t *cleaner = event->ev_arg;
Evan Hunt's avatar
Evan Hunt committed
729 730 731
	isc_result_t result;
	unsigned int n_names;
	isc_time_t start;
732 733 734 735 736 737 738 739 740 741 742 743 744

	UNUSED(task);

	INSIST(task == cleaner->task);
	INSIST(event->ev_type == DNS_EVENT_CACHECLEAN);

	if (cleaner->state == cleaner_s_done) {
		cleaner->state = cleaner_s_busy;
		end_cleaning(cleaner, event);
		LOCK(&cleaner->cache->lock);
		LOCK(&cleaner->lock);
		if (cleaner->replaceiterator) {
			dns_dbiterator_destroy(&cleaner->iterator);
745 746
			(void)dns_db_createiterator(cleaner->cache->db, false,
						    &cleaner->iterator);
747
			cleaner->replaceiterator = false;
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
		}
		UNLOCK(&cleaner->lock);
		UNLOCK(&cleaner->cache->lock);
		return;
	}

	INSIST(CLEANER_BUSY(cleaner));

	n_names = cleaner->increment;

	REQUIRE(DNS_DBITERATOR_VALID(cleaner->iterator));

	isc_time_now(&start);
	while (n_names-- > 0) {
		dns_dbnode_t *node = NULL;

764
		result = dns_dbiterator_current(cleaner->iterator, &node, NULL);
765 766
		if (result != ISC_R_SUCCESS) {
			UNEXPECTED_ERROR(__FILE__, __LINE__,
767 768 769 770
					 "cache cleaner: "
					 "dns_dbiterator_current() "
					 "failed: %s",
					 dns_result_totext(result));
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793

			end_cleaning(cleaner, event);
			return;
		}

		/*
		 * The node was not needed, but was required by
		 * dns_dbiterator_current().  Give up its reference.
		 */
		dns_db_detachnode(cleaner->cache->db, &node);

		/*
		 * Step to the next node.
		 */
		result = dns_dbiterator_next(cleaner->iterator);

		if (result != ISC_R_SUCCESS) {
			/*
			 * Either the end was reached (ISC_R_NOMORE) or
			 * some error was signaled.  If the cache is still
			 * overmem and no error was encountered,
			 * keep trying to clean it, otherwise stop cleaning.
			 */
794
			if (result != ISC_R_NOMORE) {
795 796 797 798 799
				UNEXPECTED_ERROR(__FILE__, __LINE__,
						 "cache cleaner: "
						 "dns_dbiterator_next() "
						 "failed: %s",
						 dns_result_totext(result));
800
			} else if (cleaner->overmem) {
801 802
				result =
					dns_dbiterator_first(cleaner->iterator);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
				if (result == ISC_R_SUCCESS) {
					isc_log_write(dns_lctx,
						      DNS_LOGCATEGORY_DATABASE,
						      DNS_LOGMODULE_CACHE,
						      ISC_LOG_DEBUG(1),
						      "cache cleaner: "
						      "still overmem, "
						      "reset and try again");
					continue;
				}
			}

			end_cleaning(cleaner, event);
			return;
		}
	}

	/*
	 * We have successfully performed a cleaning increment but have
	 * not gone through the entire cache.  Free the iterator locks
	 * and reschedule another batch.  If it fails, just try to continue
	 * anyway.
	 */
	result = dns_dbiterator_pause(cleaner->iterator);
	RUNTIME_CHECK(result == ISC_R_SUCCESS);

	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
830 831 832 833
		      ISC_LOG_DEBUG(1),
		      "cache cleaner: checked %u nodes, "
		      "mem inuse %lu, sleeping",
		      cleaner->increment,
834 835 836 837 838 839 840
		      (unsigned long)isc_mem_inuse(cleaner->cache->mctx));

	isc_task_send(task, &event);
	INSIST(CLEANER_BUSY(cleaner));
	return;
}

841
/*
842
 * Do immediate cleaning.
843
 */
844
isc_result_t
Evan Hunt's avatar
Evan Hunt committed
845 846
dns_cache_clean(dns_cache_t *cache, isc_stdtime_t now) {
	isc_result_t result;
Automatic Updater's avatar
Automatic Updater committed
847 848 849 850
	dns_dbiterator_t *iterator = NULL;

	REQUIRE(VALID_CACHE(cache));

851
	result = dns_db_createiterator(cache->db, 0, &iterator);
852 853 854
	if (result != ISC_R_SUCCESS) {
		return (result);
	}
Automatic Updater's avatar