cache.c revision 499b34cea04a46823d003d4c0520c8b03e8513cb
/*
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: cache.c,v 1.32 2001/01/09 21:50:40 bwelling Exp $ */
#include <config.h>
#include <dns/dbiterator.h>
/***
*** Types
***/
/*
* A cache_cleaner_t encapsulsates 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. */
/*
* Convenience macros for comprehensive assertion checking.
*/
(c)->resched_event != NULL)
(c)->resched_event == NULL)
struct cache_cleaner {
unsigned int cleaning_interval; /* The cleaning-interval from
named.conf, in seconds. */
itself to reschedule */
int increment; /* Number of names to
clean in one increment */
};
/*
* The actual cache object.
*/
struct dns_cache {
/* Unlocked */
unsigned int magic;
/* Locked by 'lock'. */
int references;
int live_tasks;
/* 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
{
return (ISC_R_NOMEMORY);
if (result != ISC_R_SUCCESS) {
"isc_mutex_init() failed: %s",
goto cleanup_mem;
}
cache->live_tasks = 0;
if (result != ISC_R_SUCCESS)
goto cleanup_mutex;
if (result != ISC_R_SUCCESS)
goto cleanup_db;
return (ISC_R_SUCCESS);
return (result);
}
static void
}
}
void
cache->references++;
}
void
cache->references--;
if (cache->references == 0) {
}
if (free_cache) {
/* XXXRTH This is not locked! */
if (cache->live_tasks > 0)
else
}
}
void
}
#ifdef NOTYET
/* ARGSUSED */
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
}
return (ISC_R_SUCCESS);
/* XXX handle TTLs in a way appropriate for the cache */
return (result);
}
/* XXX to be written */
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);
}
}
/*
* 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) {
"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);
return (result);
}
static void
/*
* Create an iterator and position it at the beginning of the cache.
*/
if (result != ISC_R_SUCCESS) {
"cache cleaner could not create "
goto idle;
}
if (result == ISC_R_NOMORE) {
/*
* The database is empty. We are done.
*/
goto destroyiter;
}
if (result != ISC_R_SUCCESS) {
"cache cleaner: dns_dbiterator_first() "
goto destroyiter;
}
"begin cache cleaning");
return;
idle:
return;
}
static void
"end cache cleaning");
}
/*
* This is run once for every cache-cleaning-interval as defined in named.conf.
*/
static void
} else {
"cache cleaner did not finish "
"in one cleaning-interval");
}
}
static void
}
/*
* Do incremental cleaning.
*/
static void
int n_names;
while (n_names-- > 0) {
(dns_name_t *) NULL);
if (result != ISC_R_SUCCESS) {
"cache cleaner: dns_dbiterator_current() "
goto idle;
}
/*
* 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.
*/
/*
* Step to the next node.
*/
if (result == ISC_R_NOMORE) {
/*
* We have successfully cleaned the whole cache.
*/
goto idle;
}
if (result != ISC_R_SUCCESS) {
"cache cleaner: "
"dns_dbiterator_next() failed: %s",
goto idle;
}
}
#if 0
#endif
/*
* We have successfully performed a cleaning increment.
*/
"cache cleaner: dns_dbiterator_pause() "
/*
* Try to continue.
*/
}
/*
* Still busy, reschedule.
*/
return;
idle:
/*
* No longer busy; save the event for later use.
*/
/* Allow the iterators memory to be freed. */
/* XXX remove */
&cleaner->overmem_event);
}
#if 0
if (result == ISC_R_SUCCESS) {
goto pause;
}
#endif
}
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
#if 0
/* Impose a minumum cache size. */
size = 100000;
#endif
} else {
}
}
/*
* The cleaner task is shutting down; do the necessary cleanup.
*/
static void
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)
}