sdev_ncache.c revision facf4a8d7b59fde89a8662b4f4c73a758e6c402c
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* negative cache handling for the /dev fs
*/
#include <sys/sysmacros.h>
#include <sys/devctl_impl.h>
/*
* ncache is a negative cache of failed lookups. An entry
* is added after an attempt to configure a device by that
* name failed. An accumulation of these entries over time
* gives us a set of device name for which implicit reconfiguration
* does not need to be attempted. If a name is created matching
* an entry in ncache, that entry is removed, with the
* persistent store updated.
*
* Implicit reconfig is initiated for any name during lookup that
* can't be resolved from the backing store and that isn't
* present in the negative cache. This functionality is
* enabled during system startup once communication with devfsadm
* can be achieved. Since readdir is more general, implicit
* reconfig initiated by reading a directory isn't enabled until
* the system is more fully booted, at the time of the multi-user
* milestone, corresponding to init state 2.
*
* A maximum is imposed on the number of entries in the cache
* to limit some script going wild and as a defense against attack.
* The default limit is 64 and can be adjusted via sdev_nc_max_entries.
*
* Each entry also has a expiration count. When looked up a name in
* the cache is set to the default. Subsequent boots will decrement
* the count if a name isn't referenced. This permits a once-only
* entry to eventually be removed over time.
*
* sdev_reconfig_delay implements a "debounce" of the timing beyond
* system available indication, providing what the filesystem considers
* to be the system-is-fully-booted state. This is provided to adjust
* the timing if some application startup is performing a readdir
* in /dev that initiates a troublesome implicit reconfig on every boot.
*
* sdev_nc_disable_reset can be used to disable clearing the negative cache
* on reconfig boot. The default is to clear the cache on reconfig boot.
* sdev_nc_disable can be used to disable the negative cache itself.
*
* sdev_reconfig_disable can be used to disable implicit reconfig.
* The default is that implicit reconfig is enabled.
*/
/* tunables and defaults */
#define SDEV_NC_EXPIRECNT 4
#define SDEV_NC_MAX_ENTRIES 64
int sdev_reconfig_verbose = 0;
int sdev_reconfig_disable = 0;
int sdev_nc_disable = 0;
int sdev_nc_disable_reset = 0;
int sdev_nc_verbose = 0;
/* globals */
int sdev_reconfig_boot = 0;
static timeout_id_t sdev_timeout_id = 0;
/* static prototypes */
static void sdev_ncache_write_complete(nvfd_t *);
static void sdev_ncache_write(void);
static void sdev_ncache_process_store(void);
static sdev_nc_list_t *sdev_nc_newlist(void);
static void sdev_nc_free_unlinked_node(sdev_nc_node_t *);
static void sdev_nc_free_all_nodes(sdev_nc_list_t *);
static void sdev_nc_freelist(sdev_nc_list_t *);
static void sdev_nc_free_bootonly(void);
/*
* called once at filesystem initialization
*/
void
sdev_ncache_init(void)
{
}
/*
* called at mount of the global instance
* currently the global instance is never unmounted
*/
void
sdev_ncache_setup(void)
{
}
static void
{
}
}
static void
{
char *path;
int i, n;
if (sdev_nc_disable)
return;
for (i = 0; i < np->nvp_npaths; i++) {
sdcmn_err5((" %s %d\n",
KM_SLEEP);
} else if (sdev_nc_verbose) {
"?%s: truncating from ncache (max %d)\n",
}
}
}
}
static void
{
sdcmn_err5(("ncache write complete but dirty again\n"));
} else {
sdcmn_err5(("ncache write complete\n"));
}
}
static void
sdev_ncache_write(void)
{
int n, i;
if (sdev_cache_write_disable) {
return;
}
/* proper lock ordering here is essential */
n = ncl->ncl_nentries;
ASSERT(n <= sdev_nc_max_entries);
np->nvp_npaths = n;
i = 0;
sdcmn_err5((" %s %d\n",
i++;
}
}
static void
sdev_nc_flush_updates(void)
{
return;
(NCL_LIST_DIRTY | NCL_LIST_WENABLE))) {
} else {
}
}
static void
{
if (sdev_nc_disable || sdev_cache_write_disable ||
return;
}
} else {
}
}
static void
{
/*
* Once boot is complete, decrement the expire count of each entry
* in the cache not touched by a reference. Remove any that
* goes to zero. This effectively removes random entries over
* time.
*/
}
} else {
if (--lp->ncn_expirecnt == 0) {
ncl->ncl_nentries--;
}
}
}
}
}
/*
* Upon transition to the login state on a reconfigure boot,
* a debounce timer is set up so that we cache all the nonsense
* lookups we're hit with by the windowing system startup.
*/
/*ARGSUSED*/
static void
sdev_state_timeout(void *arg)
{
sdev_timeout_id = 0;
}
static void
{
int nsecs;
if (nsecs == 0) {
} else {
}
}
/*
* Called to inform the filesystem of progress during boot,
* either a notice of reconfiguration boot or an indication of
* system boot complete. At system boot complete, set up a
* timer at the expiration of which no further failed lookups
* will be added to the negative cache.
*
* The dev filesystem infers from reconfig boot that implicit
* reconfig need not be invoked at all as all available devices
* will have already been named.
*
* The dev filesystem infers from "system available" that devfsadmd
* can now be run and hence implicit reconfiguration may be initiated.
* During early stages of system startup, implicit reconfig is
* not done to avoid impacting boot performance.
*/
void
sdev_devstate_change(void)
{
int new_state;
/*
* Track system state and manage interesting transitions
*/
if (i_ddi_reconfig())
if (i_ddi_sysavail())
if (sdev_boot_state < new_state) {
switch (new_state) {
case SDEV_BOOT_STATE_RECONFIG:
sdcmn_err5(("state change: reconfigure boot\n"));
sdev_reconfig_boot = 1;
if (!sdev_nc_disable_reset)
break;
case SDEV_BOOT_STATE_SYSAVAIL:
sdcmn_err5(("system available\n"));
break;
}
}
}
/*
* Lookup: filter out entries in the negative cache
* Return 1 if the lookup should not cause a reconfig.
*/
int
{
int n;
char *path;
int rval = 0;
int changed = 0;
if (sdev_nc_disable)
return (0);
sdcmn_err5(("%s/%s: lookup by %s cached, no reconfig\n",
if (sdev_nc_verbose) {
"?%s/%s: lookup by %s cached, no reconfig\n",
}
changed = 1;
}
rval = 1;
}
if (changed)
return (rval);
}
void
{
if (sdev_nc_disable)
return;
/*
* If we're still in the initial boot stage, always update
* the cache - we may not have received notice of the
* reconfig boot state yet. On a reconfigure boot, entries
* from the backing store are not re-persisted on update,
* but new entries are marked as needing an update.
* Never cache dynamic or non-global nodes.
*/
!SDEV_IS_NO_NCACHE(dv) &&
((failed_flags & SLF_NO_NCACHE) == 0) &&
((sdev_reconfig_boot &&
(sdev_boot_state != SDEV_BOOT_STATE_COMPLETE)) ||
}
}
static sdev_nc_list_t *
sdev_nc_newlist(void)
{
return (ncl);
}
static void
{
}
static void
{
ncl->ncl_nentries--;
}
}
static void
{
}
static sdev_nc_node_t *
{
return (lp);
}
return (NULL);
}
static void
{
"%s by %s: not adding to ncache (max %d)\n",
ncl->ncl_nentries));
if (sdev_nc_verbose) {
"not adding to ncache (max %d)\n",
ncl->ncl_nentries);
}
} else {
ncl->ncl_nentries++;
/* don't mark list dirty for nodes from store */
sdcmn_err5(("%s by %s: add to ncache\n",
if (sdev_nc_verbose) {
"?%s by %s: add to ncache\n",
}
}
}
} else {
}
}
void
{
int n;
}
void
{
/* dynamic and non-global nodes are never cached */
!SDEV_IS_NO_NCACHE(dv)) {
}
}
void
{
if (sdev_nc_disable)
return;
return;
}
}
if (lp) {
ncl->ncl_nentries--;
} else {
}
sdcmn_err5(("%s by %s: removed from ncache\n",
if (sdev_nc_verbose) {
}
} else
}
static void
sdev_nc_free_bootonly(void)
{
ncl->ncl_nentries--;
}
}
}