cache.c revision 70c7f4fb4fc589b04a68d67479d34eecd99c1991
/*
* Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2003 Internet Software Consortium.
*
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND 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.
*/
/* $Id: cache.c,v 1.88 2011/03/03 04:42:25 each Exp $ */
/*! \file */
#include <config.h>
#include <dns/dbiterator.h>
#include <dns/masterdump.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
#include "rbtdb.h"
/*!
* Control incremental cleaning.
* DNS_CACHE_MINSIZE is how many bytes is the floor for dns_cache_setcachesize().
* See also DNS_CACHE_CLEANERINCREMENT
*/
/*!
* Control incremental cleaning.
* CLEANERINCREMENT is how many nodes are examined in one pass.
* See also DNS_CACHE_MINSIZE
*/
/***
*** Types
***/
/*
* A cache_cleaner_t encapsulates the state of the periodic
* cache cleaning.
*/
typedef struct cache_cleaner cache_cleaner_t;
typedef enum {
cleaner_s_idle, /*%< Waiting for cleaning-interval to expire. */
cleaner_s_busy, /*%< Currently cleaning. */
cleaner_s_done /*%< Freed enough memory after being overmem. */
/*
* Convenience macros for comprehensive assertion checking.
*/
(c)->resched_event != NULL)
(c)->resched_event == NULL)
/*%
* Accesses to a cache cleaner object are synchronized through
*/
struct cache_cleaner {
/*%<
* Locks overmem_event, overmem. Note: never allocate memory
* while holding this lock - that could lead to deadlock since
* the lock is take by water() which is called from the memory
* allocator.
*/
unsigned int cleaning_interval; /*% The cleaning-interval from
named.conf, in seconds. */
itself to reschedule */
unsigned int increment; /*% Number of names to
clean in one increment */
};
/*%
* The actual cache object.
*/
struct dns_cache {
/* Unlocked. */
unsigned int magic;
char *name;
/* Locked by 'lock'. */
int references;
int live_tasks;
char *db_type;
int db_argc;
char **db_argv;
/* Locked by 'filelock'. */
char *filename;
/* Access to the on-disk cache file is also locked by 'filelock'. */
};
/***
*** Functions
***/
static isc_result_t
static void
static void
static void
static void
static inline isc_result_t
}
{
}
{
cachep));
}
{
int i, extra = 0;
return (ISC_R_NOMEMORY);
goto cleanup_mem;
}
}
if (result != ISC_R_SUCCESS)
goto cleanup_mem;
if (result != ISC_R_SUCCESS)
goto cleanup_lock;
cache->live_tasks = 0;
goto cleanup_filelock;
}
/*
* 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).
*/
extra = 1;
goto cleanup_dbtype;
}
goto cleanup_dbargv;
}
}
}
/*
* Create the database
*/
if (result != ISC_R_SUCCESS)
goto cleanup_dbargv;
if (result != ISC_R_SUCCESS)
goto cleanup_db;
}
/*
* RBT-type cache DB has its own mechanism of cache cleaning and doesn't
* need the control of the generic cleaner.
*/
else {
}
if (result != ISC_R_SUCCESS)
goto cleanup_db;
return (ISC_R_SUCCESS);
return (result);
}
static void
int i;
}
/*
* We don't free db_argv[0] in "rbt" cache databases
* as it's a pointer to hmctx
*/
int extra = 0;
extra = 1;
}
}
void
cache->references++;
}
void
cache->references--;
if (cache->references == 0) {
}
if (free_cache) {
/*
* When the cache is shut down, dump it to a file if one is
* specified.
*/
if (result != ISC_R_SUCCESS)
"error dumping cache: %s ",
/*
* If the cleaner task exists, let it free the cache.
*/
if (cache->live_tasks > 0) {
}
}
if (free_cache)
}
void
}
char *newname;
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
}
#ifdef BIND9
return (ISC_R_SUCCESS);
return (result);
}
#endif /* BIND9 */
#ifdef BIND9
#endif
return (ISC_R_SUCCESS);
#ifdef BIND9
return (result);
#else
return (ISC_R_NOTIMPLEMENTED);
#endif
}
void
/*
* It may be the case that the cache has already shut down.
* If so, it has no timer.
*/
goto unlock;
if (t == 0) {
} else {
0);
}
if (result != ISC_R_SUCCESS)
"could not set cache cleaning interval: %s",
}
unsigned int
unsigned int t;
return (t);
}
const char *
}
/*
* Initialize the cache cleaner object at *cleaner.
* Space for the object must be allocated by the caller.
*/
static isc_result_t
{
if (result != ISC_R_SUCCESS)
goto fail;
if (result != ISC_R_SUCCESS)
goto cleanup;
if (result != ISC_R_SUCCESS) {
"isc_task_create() failed: %s",
goto cleanup;
}
if (result != ISC_R_SUCCESS) {
"cache cleaner: "
"isc_task_onshutdown() failed: %s",
goto cleanup;
}
if (result != ISC_R_SUCCESS) {
"isc_timer_create() failed: %s",
goto cleanup;
}
cleaner, sizeof(isc_event_t));
goto cleanup;
}
cleaner, sizeof(isc_event_t));
goto cleanup;
}
}
return (ISC_R_SUCCESS);
fail:
return (result);
}
static void
/*
* Create an iterator, if it does not already exist, and
* position it at the beginning of the cache.
*/
if (result != ISC_R_SUCCESS)
"cache cleaner could not create "
else {
}
if (result != ISC_R_SUCCESS) {
/*
* If the result is ISC_R_NOMORE, the database is empty,
* so there is nothing to be cleaned.
*/
"cache cleaner: "
"dns_dbiterator_first() failed: %s",
}
} else {
/*
* Pause the iterator to free its lock.
*/
"begin cache cleaning, mem inuse %lu",
}
return;
}
static void
if (result != ISC_R_SUCCESS)
}
/*
* This is run once for every cache-cleaning-interval as defined in named.conf.
*/
static void
}
/*
* This is called when the cache either surpasses its upper limit
* or shrinks beyond its lower limit.
*/
static void
} else {
/*
* 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.
*/
}
if (want_cleaning)
}
/*
* Do incremental cleaning.
*/
static void
unsigned int n_names;
if (cleaner->replaceiterator) {
}
return;
}
while (n_names-- > 0) {
NULL);
if (result != ISC_R_SUCCESS) {
"cache cleaner: dns_dbiterator_current() "
return;
}
/*
* The node was not needed, but was required by
* dns_dbiterator_current(). Give up its reference.
*/
/*
* Step to the next node.
*/
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.
*/
if (result != ISC_R_NOMORE)
"cache cleaner: "
"dns_dbiterator_next() "
"failed: %s",
iterator);
if (result == ISC_R_SUCCESS) {
ISC_LOG_DEBUG(1),
"cache cleaner: "
"still overmem, "
"reset and try again");
continue;
}
}
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.
*/
return;
}
/*
* Do immediate cleaning.
*/
if (result != ISC_R_SUCCESS)
return result;
while (result == ISC_R_SUCCESS) {
(dns_name_t *)NULL);
if (result != ISC_R_SUCCESS)
break;
/*
* Check TTLs, mark expired rdatasets stale.
*/
if (result != ISC_R_SUCCESS) {
"cache cleaner: dns_db_expirenode() "
"failed: %s",
/*
* Continue anyway.
*/
}
/*
* This is where the actual freeing takes place.
*/
}
if (result == ISC_R_NOMORE)
return (result);
}
static void
}
}
void
/*
* Impose a minimum cache size; pathological things happen if there
* is too little room.
*/
/*
* If the cache was overmem and cleaning, but now with the new limits
* it is no longer in an overmem condition, then the next
* isc_mem_put for cache memory will do the right thing and trigger
* water().
*/
/*
* Disable cache memory limiting.
*/
else
/*
* Establish new cache memory limits (either for the first
* time, or replacing other limits).
*/
}
return (size);
}
/*
* The cleaner task is shutting down; do the necessary cleanup.
*/
static void
else
cache->live_tasks--;
if (cache->references == 0)
/*
* By detaching the timer in the context of its task,
* we are guaranteed that there will be no further timer
* events.
*/
/* Make sure we don't reschedule anymore. */
if (should_free)
}
if (result != ISC_R_SUCCESS)
return (result);
} else {
}
return (ISC_R_SUCCESS);
}
return (ISC_R_SUCCESS);
if (result == ISC_R_NOTFOUND) {
goto cleanup_db;
}
if (result != ISC_R_SUCCESS)
goto cleanup_db;
(isc_stdtime_t)0, &iter);
if (result != ISC_R_SUCCESS)
goto cleanup_node;
result == ISC_R_SUCCESS;
{
break;
}
if (result == ISC_R_NOMORE)
dns_db_detach(&db);
return (result);
}