1994-05-24 10:09:53 +00:00
|
|
|
/*
|
1997-02-10 02:22:35 +00:00
|
|
|
* Copyright (c) 1989, 1993, 1995
|
1994-05-24 10:09:53 +00:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1997-02-10 02:22:35 +00:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Poul-Henning Kamp of the FreeBSD Project.
|
1994-05-24 10:09:53 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1997-03-08 15:22:14 +00:00
|
|
|
* @(#)vfs_cache.c 8.5 (Berkeley) 3/22/95
|
|
|
|
* $Id: vfs_cache.c,v 1.23 1997/02/22 09:39:31 peter Exp $
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1995-12-14 09:55:16 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/sysctl.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
|
1996-01-19 04:00:31 +00:00
|
|
|
#define MAXVNODEUSE 32
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Name caching works as follows:
|
|
|
|
*
|
|
|
|
* Names found by directory scans are retained in a cache
|
|
|
|
* for future reference. It is managed LRU, so frequently
|
|
|
|
* used names will hang around. Cache is indexed by hash value
|
|
|
|
* obtained from (vp, name) where vp refers to the directory
|
|
|
|
* containing name.
|
|
|
|
*
|
1997-02-10 02:22:35 +00:00
|
|
|
* If it is a "negative" entry, (i.e. for a name that is known NOT to
|
|
|
|
* exist) the vnode pointer will be NULL.
|
1995-03-09 20:23:45 +00:00
|
|
|
*
|
1994-05-24 10:09:53 +00:00
|
|
|
* For simplicity (and economy of storage), names longer than
|
|
|
|
* a maximum length of NCHNAMLEN are not cached; they occur
|
|
|
|
* infrequently in any case, and are almost never of interest.
|
|
|
|
*
|
|
|
|
* Upon reaching the last segment of a path, if the reference
|
|
|
|
* is for DELETE, or NOCACHE is set (rewrite), and the
|
|
|
|
* name is located in the cache, it will be dropped.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Structures associated with name cacheing.
|
|
|
|
*/
|
1997-02-10 02:22:35 +00:00
|
|
|
#define NCHHASH(dvp, cnp) \
|
1997-03-08 15:22:14 +00:00
|
|
|
(&nchashtbl[((dvp)->v_id + (cnp)->cn_hash) % nchash])
|
1995-12-14 09:55:16 +00:00
|
|
|
static LIST_HEAD(nchashhead, namecache) *nchashtbl; /* Hash Table */
|
1997-03-08 15:22:14 +00:00
|
|
|
static u_long nchash; /* size of hash table */
|
|
|
|
static u_long numcache; /* number of cache entries allocated */
|
1997-02-10 02:22:35 +00:00
|
|
|
static TAILQ_HEAD(, namecache) nclruhead; /* LRU chain */
|
|
|
|
struct nchstats nchstats; /* cache effectiveness statistics */
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1997-03-08 15:22:14 +00:00
|
|
|
static int doingcache = 1; /* 1 => enable the cache */
|
|
|
|
SYSCTL_INT(_debug, OID_AUTO, vfscache, CTLFLAG_RW, &doingcache, 0, "");
|
|
|
|
|
1995-03-09 20:23:45 +00:00
|
|
|
#ifdef NCH_STATISTICS
|
|
|
|
u_long nchnbr;
|
|
|
|
#define NCHNBR(ncp) (ncp)->nc_nbr = ++nchnbr;
|
|
|
|
#define NCHHIT(ncp) (ncp)->nc_hits++
|
|
|
|
#else
|
|
|
|
#define NCHNBR(ncp)
|
|
|
|
#define NCHHIT(ncp)
|
|
|
|
#endif
|
|
|
|
|
1997-02-10 02:22:35 +00:00
|
|
|
/*
|
|
|
|
* Delete an entry from its hash list and move it to the front
|
|
|
|
* of the LRU list for immediate reuse.
|
|
|
|
*/
|
1995-03-09 20:23:45 +00:00
|
|
|
#define PURGE(ncp) { \
|
|
|
|
LIST_REMOVE(ncp, nc_hash); \
|
|
|
|
ncp->nc_hash.le_prev = 0; \
|
|
|
|
TAILQ_REMOVE(&nclruhead, ncp, nc_lru); \
|
1997-02-10 02:22:35 +00:00
|
|
|
TAILQ_INSERT_HEAD(&nclruhead, ncp, nc_lru); \
|
|
|
|
}
|
1995-03-09 20:23:45 +00:00
|
|
|
|
1997-02-10 02:22:35 +00:00
|
|
|
/*
|
|
|
|
* Move an entry that has been used to the tail of the LRU list
|
|
|
|
* so that it will be preserved for future use.
|
|
|
|
*/
|
1995-03-09 20:23:45 +00:00
|
|
|
#define TOUCH(ncp) { \
|
1997-02-10 02:22:35 +00:00
|
|
|
if (ncp->nc_lru.tqe_next != 0) { \
|
1995-03-09 20:23:45 +00:00
|
|
|
TAILQ_REMOVE(&nclruhead, ncp, nc_lru); \
|
|
|
|
TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru); \
|
1997-02-10 02:22:35 +00:00
|
|
|
NCHNBR(ncp); \
|
|
|
|
} \
|
|
|
|
}
|
1995-03-09 20:23:45 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
1997-03-08 15:22:14 +00:00
|
|
|
* Lookup an entry in the cache
|
1995-03-09 20:23:45 +00:00
|
|
|
*
|
1997-03-08 15:22:14 +00:00
|
|
|
* We don't do this if the segment name is long, simply so the cache
|
1995-03-09 20:23:45 +00:00
|
|
|
* can avoid holding long names (which would either waste space, or
|
1994-05-24 10:09:53 +00:00
|
|
|
* add greatly to the complexity).
|
|
|
|
*
|
1995-03-09 20:23:45 +00:00
|
|
|
* Lookup is called with dvp pointing to the directory to search,
|
1997-02-10 02:22:35 +00:00
|
|
|
* cnp pointing to the name of the entry being sought. If the lookup
|
|
|
|
* succeeds, the vnode is returned in *vpp, and a status of -1 is
|
|
|
|
* returned. If the lookup determines that the name does not exist
|
|
|
|
* (negative cacheing), a status of ENOENT is returned. If the lookup
|
|
|
|
* fails, a status of zero is returned.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1995-03-09 20:23:45 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
int
|
|
|
|
cache_lookup(dvp, vpp, cnp)
|
|
|
|
struct vnode *dvp;
|
|
|
|
struct vnode **vpp;
|
|
|
|
struct componentname *cnp;
|
|
|
|
{
|
1997-02-10 02:22:35 +00:00
|
|
|
register struct namecache *ncp, *nnp;
|
1995-03-06 06:45:52 +00:00
|
|
|
register struct nchashhead *ncpp;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1995-03-06 06:45:52 +00:00
|
|
|
if (!doingcache) {
|
|
|
|
cnp->cn_flags &= ~MAKEENTRY;
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
1995-03-06 06:45:52 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
if (cnp->cn_namelen > NCHNAMLEN) {
|
|
|
|
nchstats.ncs_long++;
|
|
|
|
cnp->cn_flags &= ~MAKEENTRY;
|
|
|
|
return (0);
|
|
|
|
}
|
1995-03-09 20:23:45 +00:00
|
|
|
|
1997-02-10 02:22:35 +00:00
|
|
|
ncpp = NCHHASH(dvp, cnp);
|
1995-03-06 06:45:52 +00:00
|
|
|
for (ncp = ncpp->lh_first; ncp != 0; ncp = nnp) {
|
|
|
|
nnp = ncp->nc_hash.le_next;
|
1995-03-08 01:40:44 +00:00
|
|
|
/* If one of the vp's went stale, don't bother anymore. */
|
|
|
|
if ((ncp->nc_dvpid != ncp->nc_dvp->v_id) ||
|
1997-02-10 02:22:35 +00:00
|
|
|
(ncp->nc_vp && ncp->nc_vpid != ncp->nc_vp->v_id)) {
|
1995-03-12 02:01:20 +00:00
|
|
|
nchstats.ncs_falsehits++;
|
1995-03-09 20:23:45 +00:00
|
|
|
PURGE(ncp);
|
|
|
|
continue;
|
1995-03-06 06:45:52 +00:00
|
|
|
}
|
1995-03-09 20:23:45 +00:00
|
|
|
/* Now that we know the vp's to be valid, is it ours ? */
|
|
|
|
if (ncp->nc_dvp == dvp &&
|
|
|
|
ncp->nc_nlen == cnp->cn_namelen &&
|
|
|
|
!bcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
|
1997-02-10 02:22:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We failed to find an entry */
|
|
|
|
if (ncp == 0) {
|
|
|
|
nchstats.ncs_miss++;
|
|
|
|
return (0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1995-03-09 20:23:45 +00:00
|
|
|
|
|
|
|
NCHHIT(ncp);
|
|
|
|
|
|
|
|
/* We don't want to have an entry, so dump it */
|
1995-03-06 06:45:52 +00:00
|
|
|
if ((cnp->cn_flags & MAKEENTRY) == 0) {
|
1994-05-24 10:09:53 +00:00
|
|
|
nchstats.ncs_badhits++;
|
1995-03-09 20:23:45 +00:00
|
|
|
PURGE(ncp);
|
|
|
|
return (0);
|
1997-03-08 15:22:14 +00:00
|
|
|
}
|
1995-03-09 20:23:45 +00:00
|
|
|
|
|
|
|
/* We found a "positive" match, return the vnode */
|
1997-02-10 02:22:35 +00:00
|
|
|
if (ncp->nc_vp) {
|
1994-05-24 10:09:53 +00:00
|
|
|
nchstats.ncs_goodhits++;
|
1995-03-09 20:23:45 +00:00
|
|
|
TOUCH(ncp);
|
1994-05-24 10:09:53 +00:00
|
|
|
*vpp = ncp->nc_vp;
|
1996-01-19 04:00:31 +00:00
|
|
|
if ((*vpp)->v_usage < MAXVNODEUSE)
|
|
|
|
(*vpp)->v_usage++;
|
1994-05-24 10:09:53 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
1995-03-09 20:23:45 +00:00
|
|
|
/* We found a negative match, and want to create it, so purge */
|
|
|
|
if (cnp->cn_nameiop == CREATE) {
|
1995-03-12 02:01:20 +00:00
|
|
|
nchstats.ncs_badhits++;
|
1995-03-09 20:23:45 +00:00
|
|
|
PURGE(ncp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1997-02-10 02:22:35 +00:00
|
|
|
/*
|
|
|
|
* We found a "negative" match, ENOENT notifies client of this match.
|
|
|
|
* The nc_vpid field records whether this is a whiteout.
|
|
|
|
*/
|
1995-03-09 20:23:45 +00:00
|
|
|
nchstats.ncs_neghits++;
|
|
|
|
TOUCH(ncp);
|
1997-02-10 02:22:35 +00:00
|
|
|
cnp->cn_flags |= ncp->nc_vpid;
|
1995-03-09 20:23:45 +00:00
|
|
|
return (ENOENT);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1995-03-09 20:23:45 +00:00
|
|
|
* Add an entry to the cache.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
void
|
1994-05-24 10:09:53 +00:00
|
|
|
cache_enter(dvp, vp, cnp)
|
|
|
|
struct vnode *dvp;
|
|
|
|
struct vnode *vp;
|
|
|
|
struct componentname *cnp;
|
|
|
|
{
|
1995-03-06 06:45:52 +00:00
|
|
|
register struct namecache *ncp;
|
|
|
|
register struct nchashhead *ncpp;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
if (!doingcache)
|
|
|
|
return;
|
1995-03-09 20:23:45 +00:00
|
|
|
|
|
|
|
if (cnp->cn_namelen > NCHNAMLEN) {
|
|
|
|
printf("cache_enter: name too long");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-02-10 02:22:35 +00:00
|
|
|
/*
|
|
|
|
* We allocate a new entry if we are less than the maximum
|
|
|
|
* allowed and the one at the front of the LRU list is in use.
|
|
|
|
* Otherwise we use the one at the front of the LRU list.
|
|
|
|
*/
|
|
|
|
if (numcache < desiredvnodes &&
|
|
|
|
((ncp = nclruhead.tqh_first) == NULL ||
|
|
|
|
ncp->nc_hash.le_prev != 0)) {
|
1995-03-09 20:23:45 +00:00
|
|
|
/* Add one more entry */
|
1994-05-24 10:09:53 +00:00
|
|
|
ncp = (struct namecache *)
|
|
|
|
malloc((u_long)sizeof *ncp, M_CACHE, M_WAITOK);
|
|
|
|
bzero((char *)ncp, sizeof *ncp);
|
|
|
|
numcache++;
|
1995-03-06 06:45:52 +00:00
|
|
|
} else if (ncp = nclruhead.tqh_first) {
|
1995-03-09 20:23:45 +00:00
|
|
|
/* reuse an old entry */
|
1995-03-06 06:45:52 +00:00
|
|
|
TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
|
|
|
|
if (ncp->nc_hash.le_prev != 0) {
|
|
|
|
LIST_REMOVE(ncp, nc_hash);
|
|
|
|
ncp->nc_hash.le_prev = 0;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1995-03-09 20:23:45 +00:00
|
|
|
} else {
|
|
|
|
/* give up */
|
1995-03-06 06:45:52 +00:00
|
|
|
return;
|
1995-03-09 20:23:45 +00:00
|
|
|
}
|
1997-03-08 15:22:14 +00:00
|
|
|
|
1997-02-10 02:22:35 +00:00
|
|
|
/*
|
|
|
|
* Fill in cache info, if vp is NULL this is a "negative" cache entry.
|
|
|
|
* For negative entries, we have to record whether it is a whiteout.
|
|
|
|
* the whiteout flag is stored in the nc_vpid field which is
|
|
|
|
* otherwise unused.
|
|
|
|
*/
|
1995-03-09 20:23:45 +00:00
|
|
|
ncp->nc_vp = vp;
|
1997-02-10 02:22:35 +00:00
|
|
|
if (vp) {
|
|
|
|
ncp->nc_vpid = vp->v_id;
|
|
|
|
if (vp->v_usage < MAXVNODEUSE)
|
|
|
|
++vp->v_usage;
|
|
|
|
} else
|
|
|
|
ncp->nc_vpid = cnp->cn_flags & ISWHITEOUT;
|
1994-05-24 10:09:53 +00:00
|
|
|
ncp->nc_dvp = dvp;
|
|
|
|
ncp->nc_dvpid = dvp->v_id;
|
|
|
|
ncp->nc_nlen = cnp->cn_namelen;
|
|
|
|
bcopy(cnp->cn_nameptr, ncp->nc_name, (unsigned)ncp->nc_nlen);
|
1995-03-06 06:45:52 +00:00
|
|
|
TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
|
1997-02-10 02:22:35 +00:00
|
|
|
ncpp = NCHHASH(dvp, cnp);
|
1995-03-06 06:45:52 +00:00
|
|
|
LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Name cache initialization, from vfs_init() when we are booting
|
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
void
|
1994-05-24 10:09:53 +00:00
|
|
|
nchinit()
|
|
|
|
{
|
1997-03-08 15:22:14 +00:00
|
|
|
|
1995-03-06 06:45:52 +00:00
|
|
|
TAILQ_INIT(&nclruhead);
|
1995-04-04 02:01:13 +00:00
|
|
|
nchashtbl = phashinit(desiredvnodes, M_CACHE, &nchash);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-02-10 02:22:35 +00:00
|
|
|
* Invalidate all entries to particular vnode.
|
1997-03-08 15:22:14 +00:00
|
|
|
*
|
1997-02-10 02:22:35 +00:00
|
|
|
* We actually just increment the v_id, that will do it. The stale entries
|
|
|
|
* will be purged by lookup as they get found. If the v_id wraps around, we
|
|
|
|
* need to ditch the entire cache, to avoid confusion. No valid vnode will
|
|
|
|
* ever have (v_id == 0).
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
void
|
1994-05-24 10:09:53 +00:00
|
|
|
cache_purge(vp)
|
|
|
|
struct vnode *vp;
|
|
|
|
{
|
1997-02-10 02:22:35 +00:00
|
|
|
struct namecache *ncp;
|
1995-03-06 06:45:52 +00:00
|
|
|
struct nchashhead *ncpp;
|
1995-12-22 15:56:35 +00:00
|
|
|
static u_long nextvnodeid;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
vp->v_id = ++nextvnodeid;
|
|
|
|
if (nextvnodeid != 0)
|
|
|
|
return;
|
1995-04-15 00:49:35 +00:00
|
|
|
for (ncpp = &nchashtbl[nchash - 1]; ncpp >= nchashtbl; ncpp--) {
|
1997-02-10 02:22:35 +00:00
|
|
|
while (ncp = ncpp->lh_first)
|
|
|
|
PURGE(ncp);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
vp->v_id = ++nextvnodeid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1995-03-09 20:23:45 +00:00
|
|
|
* Flush all entries referencing a particular filesystem.
|
1994-05-24 10:09:53 +00:00
|
|
|
*
|
1995-03-09 20:23:45 +00:00
|
|
|
* Since we need to check it anyway, we will flush all the invalid
|
1995-12-22 15:56:35 +00:00
|
|
|
* entries at the same time.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-05-25 09:21:21 +00:00
|
|
|
void
|
1994-05-24 10:09:53 +00:00
|
|
|
cache_purgevfs(mp)
|
|
|
|
struct mount *mp;
|
|
|
|
{
|
1995-03-09 20:23:45 +00:00
|
|
|
struct nchashhead *ncpp;
|
1997-02-10 02:22:35 +00:00
|
|
|
struct namecache *ncp, *nnp;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1995-03-09 20:23:45 +00:00
|
|
|
/* Scan hash tables for applicable entries */
|
1995-04-15 00:49:35 +00:00
|
|
|
for (ncpp = &nchashtbl[nchash - 1]; ncpp >= nchashtbl; ncpp--) {
|
1997-02-10 02:22:35 +00:00
|
|
|
for (ncp = ncpp->lh_first; ncp != 0; ncp = nnp) {
|
|
|
|
nnp = ncp->nc_hash.le_next;
|
1995-03-09 20:23:45 +00:00
|
|
|
if (ncp->nc_dvpid != ncp->nc_dvp->v_id ||
|
1997-02-10 02:22:35 +00:00
|
|
|
(ncp->nc_vp && ncp->nc_vpid != ncp->nc_vp->v_id) ||
|
1995-03-09 20:23:45 +00:00
|
|
|
ncp->nc_dvp->v_mount == mp) {
|
|
|
|
PURGE(ncp);
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|