f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
d63ae51ba337165172a9b50fd5d84805ffe50044Tinderbox User * Copyright (C) 1999-2009, 2011-2017 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews/* $Id: cache.c,v 1.91 2011/08/26 05:12:56 marka Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <config.h>
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt#include <isc/json.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/mem.h>
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt#include <isc/print.h>
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉#include <isc/string.h>
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt#include <isc/stats.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/task.h>
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence#include <isc/time.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/timer.h>
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson#include <isc/util.h>
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt#include <isc/xml.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/cache.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/db.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/dbiterator.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/events.h>
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews#include <dns/lib.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/log.h>
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington#include <dns/masterdump.h>
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews#include <dns/rdata.h>
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews#include <dns/rdataset.h>
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews#include <dns/rdatasetiter.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <dns/result.h>
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt#include <dns/stats.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt#include "rbtdb.h"
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉#define CACHE_MAGIC ISC_MAGIC('$', '$', '$', '$')
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉#define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*!
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Control incremental cleaning.
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * DNS_CACHE_MINSIZE is how many bytes is the floor for dns_cache_setcachesize().
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * See also DNS_CACHE_CLEANERINCREMENT
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews#define DNS_CACHE_MINSIZE 2097152U /*%< Bytes. 2097152 = 2 MB */
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff/*!
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Control incremental cleaning.
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * CLEANERINCREMENT is how many nodes are examined in one pass.
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff * See also DNS_CACHE_MINSIZE
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉#define DNS_CACHE_CLEANERINCREMENT 1000U /*%< Number of nodes. */
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/***
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 *** Types
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ***/
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
584848087f7463c1f659ce4712dc047d8e7f2b07Francis Dupont * A cache_cleaner_t encapsulates the state of the periodic
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * cache cleaning.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssontypedef struct cache_cleaner cache_cleaner_t;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssontypedef enum {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner_s_idle, /*%< Waiting for cleaning-interval to expire. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner_s_busy, /*%< Currently cleaning. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner_s_done /*%< Freed enough memory after being overmem. */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson} cleaner_state_t;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence/*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Convenience macros for comprehensive assertion checking.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉#define CLEANER_IDLE(c) ((c)->state == cleaner_s_idle && \
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (c)->resched_event != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉#define CLEANER_BUSY(c) ((c)->state == cleaner_s_busy && \
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (c)->iterator != NULL && \
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (c)->resched_event == NULL)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * Accesses to a cache cleaner object are synchronized through
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence * task/event serialization, or locked from the cache object.
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstruct cache_cleaner {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_mutex_t lock;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*%<
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Locks overmem_event, overmem. Note: never allocate memory
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * while holding this lock - that could lead to deadlock since
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * the lock is take by water() which is called from the memory
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * allocator.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_cache_t *cache;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_task_t *task;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 unsigned int cleaning_interval; /*% The cleaning-interval from
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater named.conf, in seconds. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_timer_t *cleaning_timer;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_t *resched_event; /*% Sent by cleaner task to
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 itself to reschedule */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_t *overmem_event;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_dbiterator_t *iterator;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 unsigned int increment; /*% Number of names to
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater clean in one increment */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner_state_t state; /*% Idle/Busy. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_boolean_t overmem; /*% The cache is in an overmem state. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_boolean_t replaceiterator;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson};
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*%
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * The actual cache object.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstruct dns_cache {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /* Unlocked. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 unsigned int magic;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_mutex_t lock;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_mutex_t filelock;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_t *mctx; /* Main cache memory */
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_t *hmctx; /* Heap memory */
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 char *name;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /* Locked by 'lock'. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 int references;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 int live_tasks;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_rdataclass_t rdclass;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_db_t *db;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cache_cleaner_t cleaner;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 char *db_type;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 int db_argc;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 char **db_argv;
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Hunt size_t size;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_stats_t *stats;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /* Locked by 'filelock'. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 char *filename;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /* Access to the on-disk cache file is also locked by 'filelock'. */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson};
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/***
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 *** Functions
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ***/
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonstatic isc_result_t
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrencecache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_timermgr_t *timermgr, cache_cleaner_t *cleaner);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncleaning_timer_action(isc_task_t *task, isc_event_t *event);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉incremental_cleaning_action(isc_task_t *task, isc_event_t *event);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsstatic void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉cleaner_shutdown_action(isc_task_t *task, isc_event_t *event);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrewsstatic void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉overmem_cleaning_action(isc_task_t *task, isc_event_t *event);
261a6a1f7d95eaf0cd882f3123dcfd775517a54fMark Andrews
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellingtonstatic inline isc_result_t
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellingtoncache_create_db(dns_cache_t *cache, dns_db_t **db) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (dns_db_create(cache->mctx, cache->db_type, dns_rootname,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_dbtype_cache, cache->rdclass,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->db_argc, cache->db_argv, db));
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington}
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceisc_result_t
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Huntdns_cache_create(isc_mem_t *cmctx, isc_taskmgr_t *taskmgr,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_timermgr_t *timermgr, dns_rdataclass_t rdclass,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater const char *db_type, unsigned int db_argc, char **db_argv,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_cache_t **cachep)
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉{
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt return (dns_cache_create3(cmctx, cmctx, taskmgr, timermgr, rdclass, "",
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 db_type, db_argc, db_argv, cachep));
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉}
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉isc_result_t
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Huntdns_cache_create2(isc_mem_t *cmctx, isc_taskmgr_t *taskmgr,
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_timermgr_t *timermgr, dns_rdataclass_t rdclass,
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt const char *cachename, const char *db_type,
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt unsigned int db_argc, char **db_argv, dns_cache_t **cachep)
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt{
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt return (dns_cache_create3(cmctx, cmctx, taskmgr, timermgr, rdclass,
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cachename, db_type, db_argc, db_argv,
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cachep));
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt}
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Huntisc_result_t
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Huntdns_cache_create3(isc_mem_t *cmctx, isc_mem_t *hmctx, isc_taskmgr_t *taskmgr,
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 isc_timermgr_t *timermgr, dns_rdataclass_t rdclass,
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 const char *cachename, const char *db_type,
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 unsigned int db_argc, char **db_argv, dns_cache_t **cachep)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_cache_t *cache;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt int i, extra = 0;
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 isc_task_t *dbtask;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(cachep != NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(*cachep == NULL);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt REQUIRE(cmctx != NULL);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt REQUIRE(hmctx != NULL);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 REQUIRE(cachename != NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache = isc_mem_get(cmctx, sizeof(*cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_R_NOMEMORY);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache->mctx = cache->hmctx = NULL;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_attach(cmctx, &cache->mctx);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_attach(hmctx, &cache->hmctx);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 cache->name = NULL;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 if (cachename != NULL) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache->name = isc_mem_strdup(cmctx, cachename);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 if (cache->name == NULL) {
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 result = ISC_R_NOMEMORY;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 goto cleanup_mem;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 }
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 }
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_mutex_init(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup_mem;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_mutex_init(&cache->filelock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup_lock;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->references = 1;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->live_tasks = 0;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->rdclass = rdclass;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt cache->stats = NULL;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt result = isc_stats_create(cmctx, &cache->stats,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dns_cachestatscounter_max);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (result != ISC_R_SUCCESS)
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt goto cleanup_filelock;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache->db_type = isc_mem_strdup(cmctx, db_type);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db_type == NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = ISC_R_NOMEMORY;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt goto cleanup_stats;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt /*
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * For databases of type "rbt" we pass hmctx to dns_db_create()
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * via cache->db_argv, followed by the rest of the arguments in
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * db_argv (of which there really shouldn't be any).
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt */
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt if (strcmp(cache->db_type, "rbt") == 0)
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt extra = 1;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache->db_argc = db_argc + extra;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache->db_argv = NULL;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt if (cache->db_argc != 0) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache->db_argv = isc_mem_get(cmctx,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->db_argc * sizeof(char *));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db_argv == NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = ISC_R_NOMEMORY;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup_dbtype;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater for (i = 0; i < cache->db_argc; i++)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->db_argv[i] = NULL;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache->db_argv[0] = (char *) hmctx;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt for (i = extra; i < cache->db_argc; i++) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt cache->db_argv[i] = isc_mem_strdup(cmctx,
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt db_argv[i - extra]);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db_argv[i] == NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = ISC_R_NOMEMORY;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup_dbargv;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt /*
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * Create the database
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->db = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = cache_create_db(cache, &cache->db);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup_dbargv;
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 if (taskmgr != NULL) {
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 dbtask = NULL;
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 result = isc_task_create(taskmgr, 1, &dbtask);
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 goto cleanup_db;
b8a9632333a92d73a503afe1aaa7990016c8bee9Evan Hunt
b8a9632333a92d73a503afe1aaa7990016c8bee9Evan Hunt isc_task_setname(dbtask, "cache_dbtask", NULL);
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 dns_db_settask(cache->db, dbtask);
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 isc_task_detach(&dbtask);
5d7849ad7ffc6d08870dbfbc8d6bfffd90007488Tatuya JINMEI 神明達哉 }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->filename = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->magic = CACHE_MAGIC;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * RBT-type cache DB has its own mechanism of cache cleaning and doesn't
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * need the control of the generic cleaner.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (strcmp(db_type, "rbt") == 0)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = cache_cleaner_init(cache, NULL, NULL, &cache->cleaner);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 else {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = cache_cleaner_init(cache, taskmgr, timermgr,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 &cache->cleaner);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup_db;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt result = dns_db_setcachestats(cache->db, cache->stats);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (result != ISC_R_SUCCESS)
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt goto cleanup_db;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater *cachep = cache;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_R_SUCCESS);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson cleanup_db:
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_db_detach(&cache->db);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington cleanup_dbargv:
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt for (i = extra; i < cache->db_argc; i++)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db_argv[i] != NULL)
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_free(cmctx, cache->db_argv[i]);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db_argv != NULL)
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_put(cmctx, cache->db_argv,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->db_argc * sizeof(char *));
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington cleanup_dbtype:
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_free(cmctx, cache->db_type);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington cleanup_filelock:
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater DESTROYLOCK(&cache->filelock);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt cleanup_stats:
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_stats_detach(&cache->stats);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington cleanup_lock:
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater DESTROYLOCK(&cache->lock);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson cleanup_mem:
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 if (cache->name != NULL)
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_free(cmctx, cache->name);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_detach(&cache->hmctx);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (result);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncache_free(dns_cache_t *cache) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater int i;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(cache->references == 0);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_setwater(cache->mctx, NULL, NULL, 0, 0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->cleaner.task != NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_task_detach(&cache->cleaner.task);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cache->cleaner.overmem_event != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_free(&cache->cleaner.overmem_event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cache->cleaner.resched_event != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_free(&cache->cleaner.resched_event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cache->cleaner.iterator != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_dbiterator_destroy(&cache->cleaner.iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater DESTROYLOCK(&cache->cleaner.lock);
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->filename) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_free(cache->mctx, cache->filename);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->filename = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db != NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_db_detach(&cache->db);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db_argv != NULL) {
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt /*
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * We don't free db_argv[0] in "rbt" cache databases
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt * as it's a pointer to hmctx
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt */
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt int extra = 0;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt if (strcmp(cache->db_type, "rbt") == 0)
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt extra = 1;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt for (i = extra; i < cache->db_argc; i++)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db_argv[i] != NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_free(cache->mctx, cache->db_argv[i]);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_put(cache->mctx, cache->db_argv,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->db_argc * sizeof(char *));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->db_type != NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_free(cache->mctx, cache->db_type);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 if (cache->name != NULL)
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 isc_mem_free(cache->mctx, cache->name);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt if (cache->stats != NULL)
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_stats_detach(&cache->stats);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater DESTROYLOCK(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater DESTROYLOCK(&cache->filelock);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->magic = 0;
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_detach(&cache->hmctx);
70c7f4fb4fc589b04a68d67479d34eecd99c1991Evan Hunt isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(targetp != NULL && *targetp == NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->references++;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->lock);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater *targetp = cache;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_cache_detach(dns_cache_t **cachep) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_cache_t *cache;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_boolean_t free_cache = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(cachep != NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache = *cachep;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(cache->references > 0);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->references--;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->references == 0) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->cleaner.overmem = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater free_cache = ISC_TRUE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater *cachep = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (free_cache) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * When the cache is shut down, dump it to a file if one is
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * specified.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result = dns_cache_dump(cache);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "error dumping cache: %s ",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * If the cleaner task exists, let it free the cache.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->live_tasks > 0) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_task_shutdown(cache->cleaner.task);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater free_cache = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (free_cache)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache_free(cache);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
b120e39deac3e9c323cdc7801ed2313f8ebbb744Andreas Gustafssondns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(dbp != NULL && *dbp == NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(cache->db != NULL);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_db_attach(cache->db, dbp);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->lock);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonisc_result_t
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrewsdns_cache_setfilename(dns_cache_t *cache, const char *filename) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater char *newname;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(filename != NULL);
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater newname = isc_mem_strdup(cache->mctx, filename);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (newname == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_R_NOMEMORY);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->filelock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->filename)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_free(cache->mctx, cache->filename);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->filename = newname;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->filelock);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_R_SUCCESS);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonisc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_cache_load(dns_cache_t *cache) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->filename == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_R_SUCCESS);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->filelock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_db_load(cache->db, cache->filename);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->filelock);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (result);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonisc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_cache_dump(dns_cache_t *cache) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->filename == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_R_SUCCESS);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->filelock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_master_dump(cache->mctx, cache->db, NULL,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &dns_master_style_cache, cache->filename);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->filelock);
7963a67df0bd8f4cea292228dbe1060fbb924dc7Mark Andrews return (result);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f0e246e271f84c6fe960a3c36703a56d1067431cBrian Wellington}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonvoid
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssondns_cache_setcleaninginterval(dns_cache_t *cache, unsigned int t) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_interval_t interval;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * It may be the case that the cache has already shut down.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * If so, it has no timer.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->cleaner.cleaning_timer == NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto unlock;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->cleaner.cleaning_interval = t;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (t == 0) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_timer_reset(cache->cleaner.cleaning_timer,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_timertype_inactive,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater NULL, NULL, ISC_TRUE);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater } else {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_interval_set(&interval, cache->cleaner.cleaning_interval,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater 0);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_timer_reset(cache->cleaner.cleaning_timer,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_timertype_ticker,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater NULL, &interval, ISC_FALSE);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "could not set cache cleaning interval: %s",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_totext(result));
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
618a5e4923c3d064c1cc0f1866e27e8ddca90700Andreas Gustafsson unlock:
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->lock);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson}
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉unsigned int
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉dns_cache_getcleaninginterval(dns_cache_t *cache) {
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 unsigned int t;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 REQUIRE(VALID_CACHE(cache));
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 LOCK(&cache->lock);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 t = cache->cleaner.cleaning_interval;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 UNLOCK(&cache->lock);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 return (t);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉}
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉const char *
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉dns_cache_getname(dns_cache_t *cache) {
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 REQUIRE(VALID_CACHE(cache));
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 return (cache->name);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉}
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Initialize the cache cleaner object at *cleaner.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Space for the object must be allocated by the caller.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonstatic isc_result_t
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencecache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_timermgr_t *timermgr, cache_cleaner_t *cleaner)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_mutex_init(&cleaner->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto fail;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaner->increment = DNS_CACHE_CLEANERINCREMENT;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaner->state = cleaner_s_idle;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaner->cache = cache;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->iterator = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaner->overmem = ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->replaceiterator = ISC_FALSE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaner->task = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaner->cleaning_timer = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->resched_event = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->overmem_event = NULL;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 cleaner->cleaning_interval = 0; /* Initially turned off. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_db_createiterator(cleaner->cache->db, ISC_FALSE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 &cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 goto cleanup;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (taskmgr != NULL && timermgr != NULL) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_task_create(taskmgr, 1, &cleaner->task);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNEXPECTED_ERROR(__FILE__, __LINE__,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "isc_task_create() failed: %s",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = ISC_R_UNEXPECTED;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaner->cache->live_tasks++;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_task_setname(cleaner->task, "cachecleaner", cleaner);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_task_onshutdown(cleaner->task,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaner_shutdown_action, cache);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNEXPECTED_ERROR(__FILE__, __LINE__,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "cache cleaner: "
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "isc_task_onshutdown() failed: %s",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = isc_timer_create(timermgr, isc_timertype_inactive,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 NULL, NULL, cleaner->task,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleaning_timer_action, cleaner,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater &cleaner->cleaning_timer);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNEXPECTED_ERROR(__FILE__, __LINE__,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "isc_timer_create() failed: %s",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = ISC_R_UNEXPECTED;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater goto cleanup;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->resched_event =
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_allocate(cache->mctx, cleaner,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 DNS_EVENT_CACHECLEAN,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 incremental_cleaning_action,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner, sizeof(isc_event_t));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->resched_event == NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = ISC_R_NOMEMORY;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 goto cleanup;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->overmem_event =
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_allocate(cache->mctx, cleaner,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 DNS_EVENT_CACHEOVERMEM,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 overmem_cleaning_action,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner, sizeof(isc_event_t));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->overmem_event == NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = ISC_R_NOMEMORY;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 goto cleanup;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_R_SUCCESS);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson cleanup:
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->overmem_event != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_free(&cleaner->overmem_event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->resched_event != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_free(&cleaner->resched_event);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cleaner->cleaning_timer != NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_timer_detach(&cleaner->cleaning_timer);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cleaner->task != NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_task_detach(&cleaner->task);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->iterator != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_dbiterator_destroy(&cleaner->iterator);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater DESTROYLOCK(&cleaner->lock);
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson fail:
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (result);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉begin_cleaning(cache_cleaner_t *cleaner) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_result_t result = ISC_R_SUCCESS;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 REQUIRE(CLEANER_IDLE(cleaner));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Create an iterator, if it does not already exist, and
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * position it at the beginning of the cache.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->iterator == NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_db_createiterator(cleaner->cache->db, ISC_FALSE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 &cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "cache cleaner could not create "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "iterator: %s", isc_result_totext(result));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 else {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_dbiterator_setcleanmode(cleaner->iterator, ISC_TRUE);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_dbiterator_first(cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * If the result is ISC_R_NOMORE, the database is empty,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * so there is nothing to be cleaned.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_NOMORE && cleaner->iterator != NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 UNEXPECTED_ERROR(__FILE__, __LINE__,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "cache cleaner: "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "dns_dbiterator_first() failed: %s",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_result_totext(result));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_dbiterator_destroy(&cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 } else if (cleaner->iterator != NULL) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_dbiterator_pause(cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 } else {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Pause the iterator to free its lock.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_dbiterator_pause(cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "begin cache cleaning, mem inuse %lu",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (unsigned long)isc_mem_inuse(cleaner->cache->mctx));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->state = cleaner_s_busy;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_task_send(cleaner->task, &cleaner->resched_event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉end_cleaning(cache_cleaner_t *cleaner, isc_event_t *event) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_result_t result;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 REQUIRE(CLEANER_BUSY(cleaner));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 REQUIRE(event != NULL);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_dbiterator_pause(cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_dbiterator_destroy(&cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_cache_setcleaninginterval(cleaner->cache,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->cleaning_interval);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LOG_DEBUG(1), "end cache cleaning, mem inuse %lu",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (unsigned long)isc_mem_inuse(cleaner->cache->mctx));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->state = cleaner_s_idle;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->resched_event = event;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson/*
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * This is run once for every cache-cleaning-interval as defined in named.conf.
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson */
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonstatic void
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssoncleaning_timer_action(isc_task_t *task, isc_event_t *event) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache_cleaner_t *cleaner = event->ev_arg;
03609d0b8f6e458d6bd4b460f4eb3f7270f487efDavid Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNUSED(task);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater INSIST(task == cleaner->task);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater INSIST(event->ev_type == ISC_TIMEREVENT_TICK);
86a292d45977f1d8400df04b04bf659d9a5d68a1Andreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater ISC_LOG_DEBUG(1), "cache cleaning timer fired, "
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "cleaner state = %d", cleaner->state);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->state == cleaner_s_idle)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 begin_cleaning(cleaner);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_event_free(&event);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * This is called when the cache either surpasses its upper limit
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * or shrinks beyond its lower limit.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉overmem_cleaning_action(isc_task_t *task, isc_event_t *event) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cache_cleaner_t *cleaner = event->ev_arg;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_boolean_t want_cleaning = ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 UNUSED(task);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(task == cleaner->task);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(event->ev_type == DNS_EVENT_CACHEOVERMEM);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(cleaner->overmem_event == NULL);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LOG_DEBUG(1), "overmem_cleaning_action called, "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "overmem = %d, state = %d", cleaner->overmem,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->state);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 LOCK(&cleaner->lock);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->overmem) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->state == cleaner_s_idle)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 want_cleaning = ISC_TRUE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 } else {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->state == cleaner_s_busy)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * end_cleaning() can't be called here because
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * then both cleaner->overmem_event and
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * cleaner->resched_event will point to this
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * event. Set the state to done, and then
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * when the incremental_cleaning_action() event
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * is posted, it will handle the end_cleaning.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->state = cleaner_s_done;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->overmem_event = event;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 UNLOCK(&cleaner->lock);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (want_cleaning)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 begin_cleaning(cleaner);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉/*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Do incremental cleaning.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉static void
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉incremental_cleaning_action(isc_task_t *task, isc_event_t *event) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cache_cleaner_t *cleaner = event->ev_arg;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_result_t result;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 unsigned int n_names;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_time_t start;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 UNUSED(task);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(task == cleaner->task);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(event->ev_type == DNS_EVENT_CACHECLEAN);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->state == cleaner_s_done) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->state = cleaner_s_busy;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 end_cleaning(cleaner, event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 LOCK(&cleaner->cache->lock);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 LOCK(&cleaner->lock);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cleaner->replaceiterator) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_dbiterator_destroy(&cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (void) dns_db_createiterator(cleaner->cache->db,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_FALSE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 &cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cleaner->replaceiterator = ISC_FALSE;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 UNLOCK(&cleaner->lock);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 UNLOCK(&cleaner->cache->lock);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(CLEANER_BUSY(cleaner));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 n_names = cleaner->increment;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 REQUIRE(DNS_DBITERATOR_VALID(cleaner->iterator));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_time_now(&start);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 while (n_names-- > 0) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_dbnode_t *node = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_dbiterator_current(cleaner->iterator, &node,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 NULL);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 UNEXPECTED_ERROR(__FILE__, __LINE__,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "cache cleaner: dns_dbiterator_current() "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "failed: %s", dns_result_totext(result));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 end_cleaning(cleaner, event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * The node was not needed, but was required by
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * dns_dbiterator_current(). Give up its reference.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_db_detachnode(cleaner->cache->db, &node);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Step to the next node.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_dbiterator_next(cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * Either the end was reached (ISC_R_NOMORE) or
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * some error was signaled. If the cache is still
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * overmem and no error was encountered,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * keep trying to clean it, otherwise stop cleaning.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result != ISC_R_NOMORE)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 UNEXPECTED_ERROR(__FILE__, __LINE__,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "cache cleaner: "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "dns_dbiterator_next() "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "failed: %s",
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 dns_result_totext(result));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 else if (cleaner->overmem) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_dbiterator_first(cleaner->
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (result == ISC_R_SUCCESS) {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_log_write(dns_lctx,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 DNS_LOGCATEGORY_DATABASE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 DNS_LOGMODULE_CACHE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LOG_DEBUG(1),
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "cache cleaner: "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "still overmem, "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "reset and try again");
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 continue;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 end_cleaning(cleaner, event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 }
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 /*
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * We have successfully performed a cleaning increment but have
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * not gone through the entire cache. Free the iterator locks
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * and reschedule another batch. If it fails, just try to continue
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 * anyway.
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 result = dns_dbiterator_pause(cleaner->iterator);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 RUNTIME_CHECK(result == ISC_R_SUCCESS);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 ISC_LOG_DEBUG(1), "cache cleaner: checked %u nodes, "
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 "mem inuse %lu, sleeping", cleaner->increment,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 (unsigned long)isc_mem_inuse(cleaner->cache->mctx));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_task_send(task, &event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 INSIST(CLEANER_BUSY(cleaner));
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 return;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉}
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Do immediate cleaning.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonisc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_cache_clean(dns_cache_t *cache, isc_stdtime_t now) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_dbiterator_t *iterator = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews result = dns_db_createiterator(cache->db, 0, &iterator);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_dbiterator_first(iterator);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater while (result == ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_dbnode_t *node = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_dbiterator_current(iterator, &node,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater (dns_name_t *)NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater break;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Check TTLs, mark expired rdatasets stale.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_db_expirenode(cache->db, node, now);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNEXPECTED_ERROR(__FILE__, __LINE__,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "cache cleaner: dns_db_expirenode() "
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater "failed: %s",
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_result_totext(result));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Continue anyway.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * This is where the actual freeing takes place.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_db_detachnode(cache->db, &node);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_dbiterator_next(iterator);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_dbiterator_destroy(&iterator);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result == ISC_R_NOMORE)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = ISC_R_SUCCESS;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (result);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsstatic void
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewswater(void *arg, int mark) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_cache_t *cache = arg;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->cleaner.lock);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews if (overmem != cache->cleaner.overmem) {
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews dns_db_overmem(cache->db, overmem);
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews cache->cleaner.overmem = overmem;
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews isc_mem_waterack(cache->mctx, mark);
9a7d202077fae00fbdca610d8a8d90689e30f331Mark Andrews }
ce3c351d815569455892c30dc78d8c2e85ec1076Andreas Gustafsson
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cache->cleaner.overmem_event != NULL)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_task_send(cache->cleaner.task,
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 &cache->cleaner.overmem_event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->cleaner.lock);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews}
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrewsvoid
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Huntdns_cache_setcachesize(dns_cache_t *cache, size_t size) {
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Hunt size_t hiwater, lowater;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater REQUIRE(VALID_CACHE(cache));
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
584848087f7463c1f659ce4712dc047d8e7f2b07Francis Dupont * Impose a minimum cache size; pathological things happen if there
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * is too little room.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews if (size != 0U && size < DNS_CACHE_MINSIZE)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater size = DNS_CACHE_MINSIZE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 LOCK(&cache->lock);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 cache->size = size;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 UNLOCK(&cache->lock);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 hiwater = size - (size >> 3); /* Approximately 7/8ths. */
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 lowater = size - (size >> 2); /* Approximately 3/4ths. */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * If the cache was overmem and cleaning, but now with the new limits
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * it is no longer in an overmem condition, then the next
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * isc_mem_put for cache memory will do the right thing and trigger
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * water().
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews if (size == 0U || hiwater == 0U || lowater == 0U)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Disable cache memory limiting.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_setwater(cache->mctx, water, cache, 0, 0);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater else
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Establish new cache memory limits (either for the first
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * time, or replacing other limits).
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_mem_setwater(cache->mctx, water, cache, hiwater, lowater);
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews}
3a34b87c878990c6303358efd22265c2c5980c65Mark Andrews
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Huntsize_t
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉dns_cache_getcachesize(dns_cache_t *cache) {
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Hunt size_t size;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 REQUIRE(VALID_CACHE(cache));
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 LOCK(&cache->lock);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 size = cache->size;
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 UNLOCK(&cache->lock);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉 return (size);
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉}
7781f25078c491a9650dec555bdc86cb0ed49861Tatuya JINMEI 神明達哉
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * The cleaner task is shutting down; do the necessary cleanup.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoncleaner_shutdown_action(isc_task_t *task, isc_event_t *event) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_cache_t *cache = event->ev_arg;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_boolean_t should_free = ISC_FALSE;
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNUSED(task);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater INSIST(task == cache->cleaner.task);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater INSIST(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (CLEANER_BUSY(&cache->cleaner))
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 end_cleaning(&cache->cleaner, event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 else
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 isc_event_free(&event);
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->lock);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->live_tasks--;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater INSIST(cache->live_tasks == 0);
f991461dc144794d80ff8d85e430fd8a6e6113f3David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->references == 0)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater should_free = ISC_TRUE;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /*
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * By detaching the timer in the context of its task,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * we are guaranteed that there will be no further timer
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * events.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->cleaner.cleaning_timer != NULL)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_timer_detach(&cache->cleaner.cleaning_timer);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater /* Make sure we don't reschedule anymore. */
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater (void)isc_task_purge(task, NULL, DNS_EVENT_CACHECLEAN, NULL);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->lock);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (should_free)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache_free(cache);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellingtonisc_result_t
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellingtondns_cache_flush(dns_cache_t *cache) {
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews dns_db_t *db = NULL, *olddb;
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews dns_dbiterator_t *dbiterator = NULL, *olddbiterator = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = cache_create_db(cache, &db);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (result);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews result = dns_db_createiterator(db, ISC_FALSE, &dbiterator);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews if (result != ISC_R_SUCCESS) {
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews dns_db_detach(&db);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews return (result);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews }
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater LOCK(&cache->cleaner.lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (cache->cleaner.state == cleaner_s_idle) {
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews olddbiterator = cache->cleaner.iterator;
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews cache->cleaner.iterator = dbiterator;
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews dbiterator = NULL;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 } else {
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 if (cache->cleaner.state == cleaner_s_busy)
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cache->cleaner.state = cleaner_s_done;
5c024f787777143031c2c49f9811c39c84bfa259Tatuya JINMEI 神明達哉 cache->cleaner.replaceiterator = ISC_TRUE;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews olddb = cache->db;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cache->db = db;
d81b6b94c41c682041e2a7b6deba002cbbfda278Evan Hunt dns_db_setcachestats(cache->db, cache->stats);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->cleaner.lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater UNLOCK(&cache->lock);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews if (dbiterator != NULL)
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews dns_dbiterator_destroy(&dbiterator);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews if (olddbiterator != NULL)
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews dns_dbiterator_destroy(&olddbiterator);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews dns_db_detach(&olddb);
307adf67922dd222bacf84ef0f074a0eb84d3a0aMark Andrews
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (ISC_R_SUCCESS);
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington}
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
01279934803dd45c007a58b61fe20c2852135b52Evan Huntstatic isc_result_t
01279934803dd45c007a58b61fe20c2852135b52Evan Huntclearnode(dns_db_t *db, dns_dbnode_t *node) {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater isc_result_t result;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rdatasetiter_t *iter = NULL;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_db_allrdatasets(db, node, NULL, (isc_stdtime_t)0, &iter);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt return (result);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater for (result = dns_rdatasetiter_first(iter);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result == ISC_R_SUCCESS;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = dns_rdatasetiter_next(iter))
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater {
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rdataset_t rdataset;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rdataset_init(&rdataset);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rdatasetiter_current(iter, &rdataset);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_db_deleterdataset(db, node, NULL,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater rdataset.type, rdataset.covers);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rdataset_disassociate(&rdataset);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result != ISC_R_SUCCESS && result != DNS_R_UNCHANGED)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater break;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater if (result == ISC_R_NOMORE)
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = ISC_R_SUCCESS;
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_rdatasetiter_destroy(&iter);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt return (result);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt}
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
01279934803dd45c007a58b61fe20c2852135b52Evan Huntstatic isc_result_t
01279934803dd45c007a58b61fe20c2852135b52Evan Huntcleartree(dns_db_t *db, dns_name_t *name) {
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews isc_result_t result, answer = ISC_R_SUCCESS;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_dbiterator_t *iter = NULL;
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews dns_dbnode_t *node = NULL, *top = NULL;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_fixedname_t fnodename;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_name_t *nodename;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews /*
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews * Create the node if it doesn't exist so dns_dbiterator_seek()
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews * can find it. We will continue even if this fails.
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews */
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews (void)dns_db_findnode(db, name, ISC_TRUE, &top);
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_fixedname_init(&fnodename);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt nodename = dns_fixedname_name(&fnodename);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_db_createiterator(db, 0, &iter);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (result != ISC_R_SUCCESS)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt goto cleanup;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_dbiterator_seek(iter, name);
6214c3c93a43dc86f080dc0219e4560b69721f53Mark Andrews if (result == DNS_R_PARTIALMATCH)
6214c3c93a43dc86f080dc0219e4560b69721f53Mark Andrews result = dns_dbiterator_next(iter);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (result != ISC_R_SUCCESS)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt goto cleanup;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt while (result == ISC_R_SUCCESS) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_dbiterator_current(iter, &node, nodename);
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews if (result == DNS_R_NEWORIGIN)
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews result = ISC_R_SUCCESS;
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews if (result != ISC_R_SUCCESS)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt goto cleanup;
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews /*
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews * Are we done?
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews */
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (! dns_name_issubdomain(nodename, name))
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt goto cleanup;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews /*
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews * If clearnode fails record and move onto the next node.
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews */
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = clearnode(db, node);
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews if (result != ISC_R_SUCCESS && answer == ISC_R_SUCCESS)
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews answer = result;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_db_detachnode(db, &node);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_dbiterator_next(iter);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt cleanup:
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (result == ISC_R_NOMORE || result == ISC_R_NOTFOUND)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = ISC_R_SUCCESS;
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews if (result != ISC_R_SUCCESS && answer == ISC_R_SUCCESS)
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews answer = result;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (node != NULL)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_db_detachnode(db, &node);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (iter != NULL)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_dbiterator_destroy(&iter);
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews if (top != NULL)
dd185fb371632b9cf1b7be3097960185c9645a09Mark Andrews dns_db_detachnode(db, &top);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
e1cf6fd20a9f56c6c31581480445dea96fc7ac60Mark Andrews return (answer);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt}
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Huntisc_result_t
01279934803dd45c007a58b61fe20c2852135b52Evan Huntdns_cache_flushname(dns_cache_t *cache, dns_name_t *name) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt return (dns_cache_flushnode(cache, name, ISC_FALSE));
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt}
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Huntisc_result_t
01279934803dd45c007a58b61fe20c2852135b52Evan Huntdns_cache_flushnode(dns_cache_t *cache, dns_name_t *name,
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt isc_boolean_t tree)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt{
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt isc_result_t result;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_dbnode_t *node = NULL;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_db_t *db = NULL;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
6214c3c93a43dc86f080dc0219e4560b69721f53Mark Andrews if (tree && dns_name_equal(name, dns_rootname))
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt return (dns_cache_flush(cache));
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt LOCK(&cache->lock);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (cache->db != NULL)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_db_attach(cache->db, &db);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt UNLOCK(&cache->lock);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (db == NULL)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt return (ISC_R_SUCCESS);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (tree) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = cleartree(cache->db, name);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt } else {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = dns_db_findnode(cache->db, name, ISC_FALSE, &node);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (result == ISC_R_NOTFOUND) {
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = ISC_R_SUCCESS;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt goto cleanup_db;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt }
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt if (result != ISC_R_SUCCESS)
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt goto cleanup_db;
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt result = clearnode(cache->db, node);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt dns_db_detachnode(cache->db, &node);
01279934803dd45c007a58b61fe20c2852135b52Evan Hunt }
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews
6c8abf481df85a67c3f32f5f107b554d3ff5a3edMark Andrews cleanup_db:
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater dns_db_detach(&db);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater return (result);
b239c8294a5653d21876d084e0c5b029f6b9fc5dMichael Graff}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntisc_stats_t *
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntdns_cache_getstats(dns_cache_t *cache) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt REQUIRE(VALID_CACHE(cache));
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt return (cache->stats);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Huntvoid
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Huntdns_cache_updatestats(dns_cache_t *cache, isc_result_t result) {
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt REQUIRE(VALID_CACHE(cache));
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt if (cache->stats == NULL)
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt return;
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt switch (result) {
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt case ISC_R_SUCCESS:
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt case DNS_R_NCACHENXDOMAIN:
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt case DNS_R_NCACHENXRRSET:
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt case DNS_R_CNAME:
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt case DNS_R_DNAME:
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt case DNS_R_GLUE:
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt case DNS_R_ZONECUT:
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt isc_stats_increment(cache->stats,
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt dns_cachestatscounter_queryhits);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt break;
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt default:
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt isc_stats_increment(cache->stats,
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt dns_cachestatscounter_querymisses);
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt }
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt}
d640b4a0ab9dec252749793f78a1ed1e8551ea19Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt/*
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * XXX: Much of the following code has been copied in from statschannel.c.
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * We should refactor this into a generic function in stats.c that can be
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt * called from both places.
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunttypedef struct
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntcache_dumparg {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_statsformat_t type;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt void *arg; /* type dependent argument */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt int ncounters; /* for general statistics */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt int *counterindices; /* for general statistics */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_uint64_t *countervalues; /* for general statistics */
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_result_t result;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt} cache_dumparg_t;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntstatic void
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntgetcounter(isc_statscounter_t counter, isc_uint64_t val, void *arg) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt cache_dumparg_t *dumparg = arg;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt REQUIRE(counter < dumparg->ncounters);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dumparg->countervalues[counter] = val;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntstatic void
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntgetcounters(isc_stats_t *stats, isc_statsformat_t type, int ncounters,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt int *indices, isc_uint64_t *values)
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt{
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt cache_dumparg_t dumparg;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt memset(values, 0, sizeof(values[0]) * ncounters);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dumparg.type = type;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dumparg.ncounters = ncounters;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dumparg.counterindices = indices;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dumparg.countervalues = values;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_stats_dump(stats, getcounter, &dumparg, ISC_STATSDUMP_VERBOSE);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntvoid
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntdns_cache_dumpstats(dns_cache_t *cache, FILE *fp) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt int indices[dns_cachestatscounter_max];
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_uint64_t values[dns_cachestatscounter_max];
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt REQUIRE(VALID_CACHE(cache));
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt getcounters(cache->stats, isc_statsformat_file,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dns_cachestatscounter_max, indices, values);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n",
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt values[dns_cachestatscounter_hits],
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache hits");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n",
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt values[dns_cachestatscounter_misses],
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache misses");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n",
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt values[dns_cachestatscounter_queryhits],
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache hits (from query)");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n",
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt values[dns_cachestatscounter_querymisses],
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache misses (from query)");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n",
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt values[dns_cachestatscounter_deletelru],
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache records deleted due to memory exhaustion");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n",
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt values[dns_cachestatscounter_deletettl],
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache records deleted due to TTL expiration");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt fprintf(fp, "%20u %s\n", dns_db_nodecount(cache->db),
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache database nodes");
7472cd350fe9245553e9da7f2df8e7e364d136abMukund Sivaraman fprintf(fp, "%20" ISC_PLATFORM_QUADFORMAT "u %s\n",
7472cd350fe9245553e9da7f2df8e7e364d136abMukund Sivaraman (isc_uint64_t) dns_db_hashsize(cache->db),
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache database hash buckets");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews fprintf(fp, "%20" ISC_PLATFORM_QUADFORMAT "u %s\n",
d63ae51ba337165172a9b50fd5d84805ffe50044Tinderbox User (isc_uint64_t) isc_mem_total(cache->mctx),
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache tree memory total");
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews fprintf(fp, "%20" ISC_PLATFORM_QUADFORMAT "u %s\n",
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews (isc_uint64_t) isc_mem_inuse(cache->mctx),
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache tree memory in use");
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews fprintf(fp, "%20" ISC_PLATFORM_QUADFORMAT "u %s\n",
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews (isc_uint64_t) isc_mem_maxinuse(cache->mctx),
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache tree highest memory in use");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews fprintf(fp, "%20" ISC_PLATFORM_QUADFORMAT "u %s\n",
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews (isc_uint64_t) isc_mem_total(cache->hmctx),
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache heap memory total");
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews fprintf(fp, "%20" ISC_PLATFORM_QUADFORMAT "u %s\n",
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews (isc_uint64_t) isc_mem_inuse(cache->hmctx),
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache heap memory in use");
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews fprintf(fp, "%20" ISC_PLATFORM_QUADFORMAT "u %s\n",
da4823c08a9a41e7b5ff1f3a83c80007f60fd21cMark Andrews (isc_uint64_t) isc_mem_maxinuse(cache->hmctx),
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt "cache heap highest memory in use");
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt#ifdef HAVE_LIBXML2
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews#define TRY0(a) do { xmlrc = (a); if (xmlrc < 0) goto error; } while(0)
bbf31e6b623af575138669bdd7031232867b9166Mark Andrewsstatic int
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntrenderstat(const char *name, isc_uint64_t value, xmlTextWriterPtr writer) {
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews int xmlrc;
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt TRY0(xmlTextWriterWriteAttribute(writer,
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt ISC_XMLCHAR "name", ISC_XMLCHAR name));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(xmlTextWriterWriteFormatString(writer,
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt "%" ISC_PRINT_QUADFORMAT "u",
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt value));
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt TRY0(xmlTextWriterEndElement(writer)); /* counter */
b748b5e2c2fa40f758e3b35ad4f1fde99ab5e11fEvan Hunt
bbf31e6b623af575138669bdd7031232867b9166Mark Andrewserror:
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews return (xmlrc);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
bbf31e6b623af575138669bdd7031232867b9166Mark Andrewsint
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Huntdns_cache_renderxml(dns_cache_t *cache, xmlTextWriterPtr writer) {
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt int indices[dns_cachestatscounter_max];
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt isc_uint64_t values[dns_cachestatscounter_max];
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews int xmlrc;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt REQUIRE(VALID_CACHE(cache));
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt getcounters(cache->stats, isc_statsformat_file,
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt dns_cachestatscounter_max, indices, values);
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("CacheHits",
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews values[dns_cachestatscounter_hits], writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("CacheMisses",
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews values[dns_cachestatscounter_misses], writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("QueryHits",
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews values[dns_cachestatscounter_queryhits], writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("QueryMisses",
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews values[dns_cachestatscounter_querymisses], writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("DeleteLRU",
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews values[dns_cachestatscounter_deletelru], writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("DeleteTTL",
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews values[dns_cachestatscounter_deletettl], writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("CacheNodes", dns_db_nodecount(cache->db), writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("CacheBuckets", dns_db_hashsize(cache->db), writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("TreeMemTotal", isc_mem_total(cache->mctx), writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("TreeMemInUse", isc_mem_inuse(cache->mctx), writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("TreeMemMax", isc_mem_maxinuse(cache->mctx), writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("HeapMemTotal", isc_mem_total(cache->hmctx), writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("HeapMemInUse", isc_mem_inuse(cache->hmctx), writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews TRY0(renderstat("HeapMemMax", isc_mem_maxinuse(cache->hmctx), writer));
bbf31e6b623af575138669bdd7031232867b9166Mark Andrewserror:
bbf31e6b623af575138669bdd7031232867b9166Mark Andrews return (xmlrc);
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt}
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt#endif
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt#ifdef HAVE_JSON
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt#define CHECKMEM(m) do { \
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt if (m == NULL) { \
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt result = ISC_R_NOMEMORY;\
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt goto error;\
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt } \
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt} while(0)
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Huntisc_result_t
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Huntdns_cache_renderjson(dns_cache_t *cache, json_object *cstats) {
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_result_t result = ISC_R_SUCCESS;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt int indices[dns_cachestatscounter_max];
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt isc_uint64_t values[dns_cachestatscounter_max];
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object *obj;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt REQUIRE(VALID_CACHE(cache));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt getcounters(cache->stats, isc_statsformat_file,
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt dns_cachestatscounter_max, indices, values);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(values[dns_cachestatscounter_hits]);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "CacheHits", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(values[dns_cachestatscounter_misses]);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "CacheMisses", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(values[dns_cachestatscounter_queryhits]);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "QueryHits", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(values[dns_cachestatscounter_querymisses]);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "QueryMisses", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(values[dns_cachestatscounter_deletelru]);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "DeleteLRU", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(values[dns_cachestatscounter_deletettl]);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "DeleteTTL", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(dns_db_nodecount(cache->db));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "CacheNodes", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(dns_db_hashsize(cache->db));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "CacheBuckets", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(isc_mem_total(cache->mctx));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "TreeMemTotal", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(isc_mem_inuse(cache->mctx));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "TreeMemInUse", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(isc_mem_maxinuse(cache->mctx));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
1feffc6fdb1ba386b22d45ea2d2f1613e717cb9eMark Andrews json_object_object_add(cstats, "TreeMemMax", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(isc_mem_total(cache->hmctx));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "HeapMemTotal", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(isc_mem_inuse(cache->hmctx));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "HeapMemInUse", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt obj = json_object_new_int64(isc_mem_maxinuse(cache->hmctx));
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt CHECKMEM(obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt json_object_object_add(cstats, "HeapMemMax", obj);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt result = ISC_R_SUCCESS;
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunterror:
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt return (result);
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt}
feb067b25a8e33db62e2a7bf2e83bbb7f6eee845Evan Hunt#endif