cache.c revision 1a69a1a78cfaa86f3b68bbc965232b7876d4da2a
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1999, 2000 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Permission to use, copy, modify, and distribute this software for any
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * purpose with or without fee is hereby granted, provided that the above
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * copyright notice and this permission notice appear in all copies.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* $Id: cache.c,v 1.21 2000/05/08 14:34:26 tale Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/mem.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/task.h>
f4b4e7c16211137332e50bcad3fef0d15639a4f1Brian Wellington#include <isc/time.h>
f4b4e7c16211137332e50bcad3fef0d15639a4f1Brian Wellington#include <isc/timer.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/util.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#include <dns/cache.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/db.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/dbiterator.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/events.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/log.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/result.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define CACHE_MAGIC 0x24242424U /* $$$$. */
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington#define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/***
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *** Types
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ***/
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * A cache_cleaner_t encapsulsates the state of the periodic
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * cache cleaning.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtontypedef struct cache_cleaner cache_cleaner_t;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtontypedef enum {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cleaner_s_idle, /* Waiting for cleaning-interval to expire. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cleaner_s_busy /* Currently cleaning. */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington} cleaner_state_t;
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington * Convenience macros for comprehensive assertion checking.
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington */
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington#define CLEANER_IDLE(c) ((c)->state == cleaner_s_idle && \
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington (c)->iterator == NULL && \
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington (c)->resched_event != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define CLEANER_BUSY(c) ((c)->state == cleaner_s_busy && \
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (c)->iterator != NULL && \
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (c)->resched_event == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstruct cache_cleaner {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington dns_cache_t *cache;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_task_t *task;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int cleaning_interval; /* The cleaning-interval from
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington named.conf, in seconds. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_t *cleaning_timer;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_event_t *resched_event; /* Sent by cleaner task to
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington itself to reschedule */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_dbiterator_t *iterator;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int increment; /* Number of names to
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington clean in one increment */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cleaner_state_t state; /* Idle/Busy. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington};
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The actual cache object.
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstruct dns_cache {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Unlocked */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int magic;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mutex_t lock;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mutex_t filelock;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mem_t *mctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /* Locked by 'lock'. */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington int references;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington int live_tasks;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_rdataclass_t rdclass;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_db_t *db;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cache_cleaner_t cleaner;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Locked by 'filelock'. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char * filename;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Access to the on-disk cache file is also locked by 'filelock'. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington};
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence/***
571688b02f955f6304649866e768b1f81739cbedBrian Wellington *** Functions
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ***/
571688b02f955f6304649866e768b1f81739cbedBrian Wellington
571688b02f955f6304649866e768b1f81739cbedBrian Wellingtonstatic isc_result_t
571688b02f955f6304649866e768b1f81739cbedBrian Wellingtoncache_cleaner_init(dns_cache_t *cache,
571688b02f955f6304649866e768b1f81739cbedBrian Wellington isc_taskmgr_t *taskmgr, isc_timermgr_t *timermgr,
571688b02f955f6304649866e768b1f81739cbedBrian Wellington cache_cleaner_t *cleaner);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncleaning_timer_action(isc_task_t *task, isc_event_t *event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinincremental_cleaning_action(isc_task_t *task, isc_event_t *event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncleaner_shutdown_action(isc_task_t *task, isc_event_t *event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonisc_result_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timermgr_t *timermgr, dns_rdataclass_t rdclass,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington char *db_type, unsigned int db_argc, char **db_argv,
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington dns_cache_t **cachep)
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington{
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington isc_result_t result;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington dns_cache_t *cache;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington REQUIRE(cachep != NULL);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington REQUIRE(*cachep == NULL);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington REQUIRE(mctx != NULL);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington cache = isc_mem_get(mctx, sizeof *cache);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington if (cache == NULL)
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington return (ISC_R_NOMEMORY);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington cache->mctx = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_attach(mctx, &cache->mctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington result = isc_mutex_init(&cache->lock);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (result != ISC_R_SUCCESS) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington "isc_mutex_init() failed: %s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = ISC_R_UNEXPECTED;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto cleanup_mem;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache->references = 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache->live_tasks = 0;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cache->rdclass = rdclass;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cache->db = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dns_db_create(cache->mctx, db_type, dns_rootname, ISC_TRUE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdclass, db_argc, db_argv, &cache->db);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto cleanup_mutex;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache->filename = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache->magic = CACHE_MAGIC;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = cache_cleaner_init(cache, taskmgr, timermgr,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington &cache->cleaner);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (result != ISC_R_SUCCESS)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington goto cleanup_db;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington *cachep = cache;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (ISC_R_SUCCESS);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cleanup_db:
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_db_detach(&cache->db);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cleanup_mutex:
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mutex_destroy(&cache->lock);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cleanup_mem:
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mem_put(mctx, cache, sizeof *cache);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mem_detach(&mctx);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (result);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtoncache_free(dns_cache_t *cache) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mem_t *mctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(VALID_CACHE(cache));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(cache->references == 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (cache->cleaner.task != NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_task_detach(&cache->cleaner.task);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (cache->cleaner.resched_event != NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_event_free(&cache->cleaner.resched_event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cache->cleaner.iterator != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_dbiterator_destroy(&cache->cleaner.iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cache->filename) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_free(cache->mctx, cache->filename);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache->filename = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cache->db)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_db_detach(&cache->db);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington isc_mutex_destroy(&cache->lock);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington cache->magic = 0;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington mctx = cache->mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_put(cache->mctx, cache, sizeof *cache);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington isc_mem_detach(&mctx);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtonvoid
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtondns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(VALID_CACHE(cache));
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington REQUIRE(targetp != NULL && *targetp == NULL);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington LOCK(&cache->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache->references++;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington UNLOCK(&cache->lock);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington *targetp = cache;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonvoid
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtondns_cache_detach(dns_cache_t **cachep) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington dns_cache_t *cache;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington isc_boolean_t free_cache = ISC_FALSE;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington REQUIRE(cachep != NULL);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington cache = *cachep;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington REQUIRE(VALID_CACHE(cache));
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington LOCK(&cache->lock);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington REQUIRE(cache->references > 0);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington cache->references--;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (cache->references == 0)
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington free_cache = ISC_TRUE;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington UNLOCK(&cache->lock);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington *cachep = NULL;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (free_cache) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXXRTH This is not locked! */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cache->live_tasks > 0)
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington isc_task_shutdown(cache->cleaner.task);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington else
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington cache_free(cache);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington }
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington}
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington REQUIRE(VALID_CACHE(cache));
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(dbp != NULL && *dbp == NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington REQUIRE(cache->db != NULL);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington LOCK(&cache->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_db_attach(cache->db, dbp);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington UNLOCK(&cache->lock);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington}
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#ifdef NOTYET
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington/* ARGSUSED */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonisc_result_t
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtondns_cache_setfilename(dns_cache_t *cahce, char *filename) {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington char *newname = isc_mem_strdup(filename);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (newname == NULL)
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington return (ISC_R_NOMEMORY);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&cache->filelock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cache->filename)
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington isc_mem_free(cache->mctx, cache->filename);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cache->filename = newname;
de32cbd34e78bdd276e69cff239846760d4ee16eBrian Wellington UNLOCK(&cache->filelock);
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington return (ISC_R_SUCCESS);
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington}
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellingtonisc_result_t
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellingtondns_cache_load(dns_cache_t *cache) {
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cache->filename == NULL)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (ISC_R_SUCCESS);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson LOCK(&cache->filelock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXX handle TTLs in a way appropriate for the cache */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_db_load(cache->db, cache->filename);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&cache->filelock);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (result);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonisc_result_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_cache_dump(dns_cache_t *cache) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXX to be written */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ISC_R_NOTIMPLEMENTED);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#endif
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonvoid
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondns_cache_setcleaninginterval(dns_cache_t *cache, unsigned int t) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&cache->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache->cleaner.cleaning_interval = t;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (t == 0) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_reset(cache->cleaner.cleaning_timer,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timertype_inactive, NULL, NULL, ISC_TRUE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_interval_t interval;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_interval_set(&interval, cache->cleaner.cleaning_interval,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_reset(cache->cleaner.cleaning_timer,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timertype_ticker,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington NULL, &interval, ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&cache->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Initialize the cache cleaner object at *cleaner.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Space for the object must be allocated by the caller.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic isc_result_t
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssoncache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_timermgr_t *timermgr, cache_cleaner_t *cleaner)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson{
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_result_t result;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cleaner->increment = 100;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->state = cleaner_s_idle;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->cache = cache;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->iterator = NULL;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->task = NULL;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->cleaning_timer = NULL;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->resched_event = NULL;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (taskmgr != NULL && timermgr != NULL) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = isc_task_create(taskmgr, 1, &cleaner->task);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (result != ISC_R_SUCCESS) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "isc_task_create() failed: %s",
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_result_totext(result));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = ISC_R_UNEXPECTED;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson goto cleanup;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->cache->live_tasks++;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_task_setname(cleaner->task, "cachecleaner", cleaner);
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = isc_task_onshutdown(cleaner->task,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner_shutdown_action, cache);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (result != ISC_R_SUCCESS) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "cache cleaner: "
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "isc_task_onshutdown() failed: %s",
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_result_totext(result));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson goto cleanup;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->cleaning_interval = 0; /* Initially turned off. */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = isc_timer_create(timermgr, isc_timertype_inactive,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson NULL, NULL,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaner->task,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cleaning_timer_action, cleaner,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson &cleaner->cleaning_timer);
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews if (result != ISC_R_SUCCESS) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews "isc_timer_create() failed: %s",
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_result_totext(result));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = ISC_R_UNEXPECTED;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto cleanup;
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington }
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington cleaner->resched_event =
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington isc_event_allocate(cache->mctx, cleaner,
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington DNS_EVENT_CACHECLEAN,
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington incremental_cleaning_action,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cleaner, sizeof(isc_event_t));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cleaner->resched_event == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = ISC_R_NOMEMORY;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto cleanup;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (ISC_R_SUCCESS);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cleanup:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cleaner->cleaning_timer != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_detach(&cleaner->cleaning_timer);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cleaner->task != NULL)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_task_detach(&cleaner->task);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson return (result);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic void
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonbegin_cleaning(cache_cleaner_t *cleaner) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_result_t result;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson REQUIRE(CLEANER_IDLE(cleaner));
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews /*
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * Create an iterator and position it at the beginning of the cache.
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = dns_db_createiterator(cleaner->cache->db, ISC_FALSE,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson &cleaner->iterator);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (result != ISC_R_SUCCESS) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner could not create "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "iterator: %s", isc_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto idle;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_dbiterator_first(cleaner->iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result == ISC_R_NOMORE) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson /*
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * The database is empty. We are done.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto destroyiter;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_dbiterator_first() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failed: %s", dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto destroyiter;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "begin cache cleaning");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cleaner->state = cleaner_s_busy;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_task_send(cleaner->task, &cleaner->resched_event);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ENSURE(CLEANER_BUSY(cleaner));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington destroyiter:
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_dbiterator_destroy(&cleaner->iterator);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington idle:
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ENSURE(CLEANER_IDLE(cleaner));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonend_cleaning(cache_cleaner_t *cleaner, isc_event_t *event) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington REQUIRE(CLEANER_BUSY(cleaner));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "end cache cleaning");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_dbiterator_destroy(&cleaner->iterator);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cleaner->state = cleaner_s_idle;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cleaner->resched_event = event;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ENSURE(CLEANER_IDLE(cleaner));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * This is run once for every cache-cleaning-interval as defined in named.conf.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtoncleaning_timer_action(isc_task_t *task, isc_event_t *event) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington cache_cleaner_t *cleaner = event->ev_arg;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNUSED(task);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(task == cleaner->task);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(event->ev_type == ISC_TIMEREVENT_TICK);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (cleaner->state == cleaner_s_idle) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington begin_cleaning(cleaner);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington } else {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(CLEANER_BUSY(cleaner));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "cache cleaner did not finish "
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington "in one cleaning-interval");
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_event_free(&event);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Do incremental cleaning.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonincremental_cleaning_action(isc_task_t *task, isc_event_t *event) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_result_t result;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson cache_cleaner_t *cleaner = event->ev_arg;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_stdtime_t now;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson int n_names;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson INSIST(event->ev_type == DNS_EVENT_CACHECLEAN);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson INSIST(CLEANER_BUSY(cleaner));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson n_names = cleaner->increment;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_stdtime_get(&now);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson REQUIRE(DNS_DBITERATOR_VALID(cleaner->iterator));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (n_names-- > 0) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_dbnode_t *node = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = dns_dbiterator_current(cleaner->iterator, &node,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson (dns_name_t *) NULL);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (result != ISC_R_SUCCESS) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "cache cleaner: dns_dbiterator_current() "
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson "failed: %s", dns_result_totext(result));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson goto idle;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson INSIST(node != NULL);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson /* Check TTLs, mark expired rdatasets stale. */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = dns_db_expirenode(cleaner->cache->db, node, now);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (result != ISC_R_SUCCESS) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_db_expirenode() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failed: %s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Continue anyway. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* This is where the actual freeing takes place. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_db_detachnode(cleaner->cache->db, &node);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Step to the next node */
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson result = dns_dbiterator_next(cleaner->iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result == ISC_R_NOMORE) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* We have successfully cleaned the whole cache. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto idle;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_dbiterator_next() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failed: %s", dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto idle;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* We have successfully performed a cleaning increment. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_dbiterator_pause(cleaner->iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_dbiterator_pause() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failed: %s", dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Try to continue. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Still busy, reschedule. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_task_send(task, &event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(CLEANER_BUSY(cleaner));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington idle:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* No longer busy; save the event for later use. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington end_cleaning(cleaner, event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(CLEANER_IDLE(cleaner));
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington * Do immediate cleaning.
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington */
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtonisc_result_t
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtondns_cache_clean(dns_cache_t *cache, isc_stdtime_t now) {
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington isc_result_t result;
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington dns_dbiterator_t *iterator = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_db_createiterator(cache->db, ISC_FALSE, &iterator);
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington if (result != ISC_R_SUCCESS)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_dbiterator_first(iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington while (result == ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_dbnode_t *node = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_dbiterator_current(iterator, &node,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington (dns_name_t *) NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington break;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Check TTLs, mark expired rdatasets stale. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_db_expirenode(cache->db, node, now);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "cache cleaner: dns_db_expirenode() "
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington "failed: %s",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_result_totext(result));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Continue anyway. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* This is where the actual freeing takes place. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_db_detachnode(cache->db, &node);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_dbiterator_next(iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_dbiterator_destroy(&iterator);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result == ISC_R_NOMORE)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = ISC_R_SUCCESS;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The cleaner task is shutting down; do the necessary cleanup.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncleaner_shutdown_action(isc_task_t *task, isc_event_t *event) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_cache_t *cache = event->ev_arg;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t should_free = ISC_FALSE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNUSED(task);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&cache->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_event_free(&event);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache->live_tasks--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(cache->live_tasks == 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cache->references == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington should_free = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * By detaching the timer in the context of its task,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * we are guaranteed that there will be no further timer
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * events.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (cache->cleaner.cleaning_timer != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timer_detach(&cache->cleaner.cleaning_timer);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* Make sure we don't reschedule anymore. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_task_purge(task, NULL, DNS_EVENT_CACHECLEAN, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&cache->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (should_free)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cache_free(cache);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington