Home | History | Annotate | Line # | Download | only in kern
vfs_cache.c revision 1.66
      1  1.66  christos /*	$NetBSD: vfs_cache.c,v 1.66 2006/10/25 18:56:38 christos Exp $	*/
      2   1.6       cgd 
      3   1.1       cgd /*
      4   1.5   mycroft  * Copyright (c) 1989, 1993
      5   1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.51       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  *
     31  1.10   mycroft  *	@(#)vfs_cache.c	8.3 (Berkeley) 8/22/94
     32   1.1       cgd  */
     33  1.32     lukem 
     34  1.32     lukem #include <sys/cdefs.h>
     35  1.66  christos __KERNEL_RCSID(0, "$NetBSD: vfs_cache.c,v 1.66 2006/10/25 18:56:38 christos Exp $");
     36   1.1       cgd 
     37  1.28       chs #include "opt_ddb.h"
     38  1.29      fvdl #include "opt_revcache.h"
     39  1.28       chs 
     40   1.4   mycroft #include <sys/param.h>
     41   1.4   mycroft #include <sys/systm.h>
     42   1.4   mycroft #include <sys/time.h>
     43   1.4   mycroft #include <sys/mount.h>
     44   1.4   mycroft #include <sys/vnode.h>
     45   1.4   mycroft #include <sys/namei.h>
     46   1.4   mycroft #include <sys/errno.h>
     47   1.4   mycroft #include <sys/malloc.h>
     48  1.18   thorpej #include <sys/pool.h>
     49  1.39        pk #include <sys/lock.h>
     50   1.1       cgd 
     51  1.66  christos #define NAMECACHE_ENTER_REVERSE
     52   1.1       cgd /*
     53   1.1       cgd  * Name caching works as follows:
     54   1.1       cgd  *
     55   1.1       cgd  * Names found by directory scans are retained in a cache
     56   1.1       cgd  * for future reference.  It is managed LRU, so frequently
     57   1.1       cgd  * used names will hang around.  Cache is indexed by hash value
     58  1.20  jdolecek  * obtained from (dvp, name) where dvp refers to the directory
     59   1.1       cgd  * containing name.
     60   1.1       cgd  *
     61   1.1       cgd  * For simplicity (and economy of storage), names longer than
     62   1.1       cgd  * a maximum length of NCHNAMLEN are not cached; they occur
     63   1.1       cgd  * infrequently in any case, and are almost never of interest.
     64   1.1       cgd  *
     65   1.1       cgd  * Upon reaching the last segment of a path, if the reference
     66   1.1       cgd  * is for DELETE, or NOCACHE is set (rewrite), and the
     67   1.1       cgd  * name is located in the cache, it will be dropped.
     68  1.20  jdolecek  * The entry is dropped also when it was not possible to lock
     69  1.20  jdolecek  * the cached vnode, either because vget() failed or the generation
     70  1.20  jdolecek  * number has changed while waiting for the lock.
     71   1.1       cgd  */
     72   1.1       cgd 
     73   1.1       cgd /*
     74   1.1       cgd  * Structures associated with name cacheing.
     75   1.1       cgd  */
     76   1.9   mycroft LIST_HEAD(nchashhead, namecache) *nchashtbl;
     77   1.1       cgd u_long	nchash;				/* size of hash table - 1 */
     78   1.1       cgd long	numcache;			/* number of cache entries allocated */
     79  1.49      yamt #define	NCHASH(cnp, dvp)	\
     80  1.49      yamt 	(((cnp)->cn_hash ^ ((uintptr_t)(dvp) >> 3)) & nchash)
     81  1.19  sommerfe 
     82  1.19  sommerfe LIST_HEAD(ncvhashhead, namecache) *ncvhashtbl;
     83  1.19  sommerfe u_long	ncvhash;			/* size of hash table - 1 */
     84  1.48      yamt #define	NCVHASH(vp)		(((uintptr_t)(vp) >> 3) & ncvhash)
     85  1.19  sommerfe 
     86   1.9   mycroft TAILQ_HEAD(, namecache) nclruhead;		/* LRU chain */
     87   1.1       cgd struct	nchstats nchstats;		/* cache effectiveness statistics */
     88   1.1       cgd 
     89  1.56    simonb POOL_INIT(namecache_pool, sizeof(struct namecache), 0, 0, 0, "ncachepl",
     90  1.56    simonb     &pool_allocator_nointr);
     91  1.38   thorpej 
     92  1.38   thorpej MALLOC_DEFINE(M_CACHE, "namecache", "Dynamically allocated cache entries");
     93  1.18   thorpej 
     94   1.7    chopps int doingcache = 1;			/* 1 => enable the cache */
     95   1.1       cgd 
     96  1.39        pk /* A single lock to protect cache insertion, removal and lookup */
     97  1.39        pk static struct simplelock namecache_slock = SIMPLELOCK_INITIALIZER;
     98  1.39        pk 
     99  1.46      yamt static void cache_remove(struct namecache *);
    100  1.46      yamt static void cache_free(struct namecache *);
    101  1.63     perry static inline struct namecache *cache_lookup_entry(
    102  1.55      yamt     const struct vnode *, const struct componentname *);
    103  1.46      yamt 
    104  1.46      yamt static void
    105  1.46      yamt cache_remove(struct namecache *ncp)
    106  1.46      yamt {
    107  1.46      yamt 
    108  1.46      yamt 	LOCK_ASSERT(simple_lock_held(&namecache_slock));
    109  1.46      yamt 
    110  1.46      yamt 	ncp->nc_dvp = NULL;
    111  1.46      yamt 	ncp->nc_vp = NULL;
    112  1.46      yamt 
    113  1.46      yamt 	TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    114  1.46      yamt 	if (ncp->nc_hash.le_prev != NULL) {
    115  1.46      yamt 		LIST_REMOVE(ncp, nc_hash);
    116  1.46      yamt 		ncp->nc_hash.le_prev = NULL;
    117  1.46      yamt 	}
    118  1.46      yamt 	if (ncp->nc_vhash.le_prev != NULL) {
    119  1.46      yamt 		LIST_REMOVE(ncp, nc_vhash);
    120  1.46      yamt 		ncp->nc_vhash.le_prev = NULL;
    121  1.46      yamt 	}
    122  1.46      yamt 	if (ncp->nc_vlist.le_prev != NULL) {
    123  1.46      yamt 		LIST_REMOVE(ncp, nc_vlist);
    124  1.46      yamt 		ncp->nc_vlist.le_prev = NULL;
    125  1.46      yamt 	}
    126  1.46      yamt 	if (ncp->nc_dvlist.le_prev != NULL) {
    127  1.46      yamt 		LIST_REMOVE(ncp, nc_dvlist);
    128  1.46      yamt 		ncp->nc_dvlist.le_prev = NULL;
    129  1.46      yamt 	}
    130  1.46      yamt }
    131  1.46      yamt 
    132  1.46      yamt static void
    133  1.46      yamt cache_free(struct namecache *ncp)
    134  1.46      yamt {
    135  1.46      yamt 
    136  1.46      yamt 	pool_put(&namecache_pool, ncp);
    137  1.57        pk 	numcache--;
    138  1.46      yamt }
    139  1.46      yamt 
    140  1.63     perry static inline struct namecache *
    141  1.55      yamt cache_lookup_entry(const struct vnode *dvp, const struct componentname *cnp)
    142  1.55      yamt {
    143  1.55      yamt 	struct nchashhead *ncpp;
    144  1.55      yamt 	struct namecache *ncp;
    145  1.55      yamt 
    146  1.55      yamt 	LOCK_ASSERT(simple_lock_held(&namecache_slock));
    147  1.55      yamt 
    148  1.55      yamt 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    149  1.55      yamt 
    150  1.55      yamt 	LIST_FOREACH(ncp, ncpp, nc_hash) {
    151  1.55      yamt 		if (ncp->nc_dvp == dvp &&
    152  1.55      yamt 		    ncp->nc_nlen == cnp->cn_namelen &&
    153  1.55      yamt 		    !memcmp(ncp->nc_name, cnp->cn_nameptr, (u_int)ncp->nc_nlen))
    154  1.55      yamt 			break;
    155  1.55      yamt 	}
    156  1.55      yamt 
    157  1.55      yamt 	return ncp;
    158  1.55      yamt }
    159  1.55      yamt 
    160   1.1       cgd /*
    161   1.1       cgd  * Look for a the name in the cache. We don't do this
    162   1.1       cgd  * if the segment name is long, simply so the cache can avoid
    163   1.1       cgd  * holding long names (which would either waste space, or
    164   1.1       cgd  * add greatly to the complexity).
    165   1.1       cgd  *
    166   1.1       cgd  * Lookup is called with ni_dvp pointing to the directory to search,
    167   1.1       cgd  * ni_ptr pointing to the name of the entry being sought, ni_namelen
    168   1.1       cgd  * tells the length of the name, and ni_hash contains a hash of
    169  1.20  jdolecek  * the name. If the lookup succeeds, the vnode is locked, stored in ni_vp
    170  1.20  jdolecek  * and a status of zero is returned. If the locking fails for whatever
    171  1.20  jdolecek  * reason, the vnode is unlocked and the error is returned to caller.
    172  1.20  jdolecek  * If the lookup determines that the name does not exist (negative cacheing),
    173  1.20  jdolecek  * a status of ENOENT is returned. If the lookup fails, a status of -1
    174  1.20  jdolecek  * is returned.
    175   1.1       cgd  */
    176   1.5   mycroft int
    177  1.34     enami cache_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
    178   1.1       cgd {
    179  1.23  augustss 	struct namecache *ncp;
    180  1.20  jdolecek 	struct vnode *vp;
    181  1.36   thorpej 	int error;
    182   1.1       cgd 
    183   1.8       cgd 	if (!doingcache) {
    184   1.8       cgd 		cnp->cn_flags &= ~MAKEENTRY;
    185  1.34     enami 		*vpp = NULL;
    186  1.20  jdolecek 		return (-1);
    187   1.8       cgd 	}
    188  1.39        pk 
    189   1.5   mycroft 	if (cnp->cn_namelen > NCHNAMLEN) {
    190  1.53      yamt 		/* XXXSMP - updating stats without lock; do we care? */
    191   1.1       cgd 		nchstats.ncs_long++;
    192   1.5   mycroft 		cnp->cn_flags &= ~MAKEENTRY;
    193  1.53      yamt 		goto fail;
    194   1.1       cgd 	}
    195  1.53      yamt 	simple_lock(&namecache_slock);
    196  1.55      yamt 	ncp = cache_lookup_entry(dvp, cnp);
    197  1.55      yamt 	if (ncp == NULL) {
    198   1.1       cgd 		nchstats.ncs_miss++;
    199  1.39        pk 		goto fail_wlock;
    200   1.1       cgd 	}
    201   1.9   mycroft 	if ((cnp->cn_flags & MAKEENTRY) == 0) {
    202   1.1       cgd 		nchstats.ncs_badhits++;
    203  1.20  jdolecek 		goto remove;
    204   1.1       cgd 	} else if (ncp->nc_vp == NULL) {
    205  1.11   mycroft 		/*
    206  1.11   mycroft 		 * Restore the ISWHITEOUT flag saved earlier.
    207  1.11   mycroft 		 */
    208  1.50      yamt 		cnp->cn_flags |= ncp->nc_flags;
    209  1.21   mycroft 		if (cnp->cn_nameiop != CREATE ||
    210  1.21   mycroft 		    (cnp->cn_flags & ISLASTCN) == 0) {
    211   1.1       cgd 			nchstats.ncs_neghits++;
    212   1.1       cgd 			/*
    213   1.1       cgd 			 * Move this slot to end of LRU chain,
    214   1.1       cgd 			 * if not already there.
    215   1.1       cgd 			 */
    216  1.37      matt 			if (TAILQ_NEXT(ncp, nc_lru) != 0) {
    217   1.9   mycroft 				TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    218   1.9   mycroft 				TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    219   1.1       cgd 			}
    220  1.39        pk 			simple_unlock(&namecache_slock);
    221   1.1       cgd 			return (ENOENT);
    222  1.20  jdolecek 		} else {
    223  1.15      fvdl 			nchstats.ncs_badhits++;
    224  1.20  jdolecek 			goto remove;
    225  1.20  jdolecek 		}
    226  1.20  jdolecek 	}
    227  1.20  jdolecek 
    228  1.20  jdolecek 	vp = ncp->nc_vp;
    229  1.52      yamt 
    230  1.52      yamt 	/*
    231  1.52      yamt 	 * Move this slot to end of LRU chain, if not already there.
    232  1.52      yamt 	 */
    233  1.52      yamt 	if (TAILQ_NEXT(ncp, nc_lru) != 0) {
    234  1.52      yamt 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    235  1.52      yamt 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    236  1.52      yamt 	}
    237  1.52      yamt 
    238  1.60      yamt 	error = vget(vp, LK_NOWAIT);
    239  1.53      yamt 
    240  1.60      yamt 	/* Release the name cache mutex while we get reference to the vnode */
    241  1.39        pk 	simple_unlock(&namecache_slock);
    242  1.39        pk 
    243  1.52      yamt #ifdef DEBUG
    244  1.52      yamt 	/*
    245  1.52      yamt 	 * since we released namecache_slock,
    246  1.52      yamt 	 * we can't use this pointer any more.
    247  1.52      yamt 	 */
    248  1.52      yamt 	ncp = NULL;
    249  1.52      yamt #endif /* DEBUG */
    250  1.52      yamt 
    251  1.60      yamt 	if (error) {
    252  1.60      yamt 		KASSERT(error == EBUSY);
    253  1.53      yamt 		/*
    254  1.53      yamt 		 * this vnode is being cleaned out.
    255  1.53      yamt 		 */
    256  1.53      yamt 		nchstats.ncs_falsehits++; /* XXX badhits? */
    257  1.53      yamt 		goto fail;
    258  1.53      yamt 	}
    259  1.53      yamt 
    260  1.20  jdolecek 	if (vp == dvp) {	/* lookup on "." */
    261  1.20  jdolecek 		error = 0;
    262  1.20  jdolecek 	} else if (cnp->cn_flags & ISDOTDOT) {
    263  1.20  jdolecek 		VOP_UNLOCK(dvp, 0);
    264  1.20  jdolecek 		cnp->cn_flags |= PDIRUNLOCK;
    265  1.60      yamt 		error = vn_lock(vp, LK_EXCLUSIVE);
    266  1.34     enami 		/*
    267  1.60      yamt 		 * If the above vn_lock() succeeded and both LOCKPARENT and
    268  1.34     enami 		 * ISLASTCN is set, lock the directory vnode as well.
    269  1.34     enami 		 */
    270  1.20  jdolecek 		if (!error && (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) == 0) {
    271  1.20  jdolecek 			if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0) {
    272  1.20  jdolecek 				vput(vp);
    273  1.20  jdolecek 				return (error);
    274  1.20  jdolecek 			}
    275  1.20  jdolecek 			cnp->cn_flags &= ~PDIRUNLOCK;
    276  1.20  jdolecek 		}
    277   1.1       cgd 	} else {
    278  1.60      yamt 		error = vn_lock(vp, LK_EXCLUSIVE);
    279  1.34     enami 		/*
    280  1.60      yamt 		 * If the above vn_lock() failed or either of LOCKPARENT or
    281  1.65       dbj 		 * ISLASTCN is not set, unlock the directory vnode.
    282  1.34     enami 		 */
    283  1.20  jdolecek 		if (error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
    284  1.20  jdolecek 			VOP_UNLOCK(dvp, 0);
    285  1.20  jdolecek 			cnp->cn_flags |= PDIRUNLOCK;
    286  1.20  jdolecek 		}
    287  1.20  jdolecek 	}
    288  1.20  jdolecek 
    289  1.20  jdolecek 	/*
    290  1.54      yamt 	 * Check that the lock succeeded.
    291  1.20  jdolecek 	 */
    292  1.47      yamt 	if (error) {
    293  1.39        pk 		/* XXXSMP - updating stats without lock; do we care? */
    294  1.53      yamt 		nchstats.ncs_badhits++;
    295  1.39        pk 
    296   1.1       cgd 		/*
    297  1.20  jdolecek 		 * The parent needs to be locked when we return to VOP_LOOKUP().
    298  1.20  jdolecek 		 * The `.' case here should be extremely rare (if it can happen
    299  1.20  jdolecek 		 * at all), so we don't bother optimizing out the unlock/relock.
    300   1.1       cgd 		 */
    301  1.64  christos 		if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0)
    302  1.64  christos 			return (error);
    303  1.64  christos 		cnp->cn_flags &= ~PDIRUNLOCK;
    304  1.31       chs 		*vpp = NULL;
    305  1.31       chs 		return (-1);
    306  1.20  jdolecek 	}
    307  1.20  jdolecek 
    308  1.52      yamt 	/* XXXSMP - updating stats without lock; do we care? */
    309  1.20  jdolecek 	nchstats.ncs_goodhits++;
    310  1.20  jdolecek 	*vpp = vp;
    311  1.20  jdolecek 	return (0);
    312   1.1       cgd 
    313  1.20  jdolecek remove:
    314   1.1       cgd 	/*
    315   1.1       cgd 	 * Last component and we are renaming or deleting,
    316   1.1       cgd 	 * the cache entry is invalid, or otherwise don't
    317   1.1       cgd 	 * want cache entry to exist.
    318   1.1       cgd 	 */
    319  1.55      yamt 	cache_remove(ncp);
    320  1.55      yamt 	cache_free(ncp);
    321  1.39        pk 
    322  1.39        pk fail_wlock:
    323  1.39        pk 	simple_unlock(&namecache_slock);
    324  1.53      yamt fail:
    325  1.34     enami 	*vpp = NULL;
    326  1.20  jdolecek 	return (-1);
    327   1.1       cgd }
    328   1.1       cgd 
    329  1.61      yamt int
    330  1.61      yamt cache_lookup_raw(struct vnode *dvp, struct vnode **vpp,
    331  1.61      yamt     struct componentname *cnp)
    332  1.61      yamt {
    333  1.61      yamt 	struct namecache *ncp;
    334  1.61      yamt 	struct vnode *vp;
    335  1.61      yamt 	int error;
    336  1.61      yamt 
    337  1.61      yamt 	if (!doingcache) {
    338  1.61      yamt 		cnp->cn_flags &= ~MAKEENTRY;
    339  1.61      yamt 		*vpp = NULL;
    340  1.61      yamt 		return (-1);
    341  1.61      yamt 	}
    342  1.61      yamt 
    343  1.61      yamt 	if (cnp->cn_namelen > NCHNAMLEN) {
    344  1.61      yamt 		/* XXXSMP - updating stats without lock; do we care? */
    345  1.61      yamt 		nchstats.ncs_long++;
    346  1.61      yamt 		cnp->cn_flags &= ~MAKEENTRY;
    347  1.61      yamt 		goto fail;
    348  1.61      yamt 	}
    349  1.61      yamt 	simple_lock(&namecache_slock);
    350  1.61      yamt 	ncp = cache_lookup_entry(dvp, cnp);
    351  1.61      yamt 	if (ncp == NULL) {
    352  1.61      yamt 		nchstats.ncs_miss++;
    353  1.61      yamt 		goto fail_wlock;
    354  1.61      yamt 	}
    355  1.61      yamt 	/*
    356  1.61      yamt 	 * Move this slot to end of LRU chain,
    357  1.61      yamt 	 * if not already there.
    358  1.61      yamt 	 */
    359  1.61      yamt 	if (TAILQ_NEXT(ncp, nc_lru) != 0) {
    360  1.61      yamt 		TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
    361  1.61      yamt 		TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    362  1.61      yamt 	}
    363  1.61      yamt 
    364  1.61      yamt 	vp = ncp->nc_vp;
    365  1.61      yamt 	if (vp == NULL) {
    366  1.61      yamt 		/*
    367  1.61      yamt 		 * Restore the ISWHITEOUT flag saved earlier.
    368  1.61      yamt 		 */
    369  1.61      yamt 		cnp->cn_flags |= ncp->nc_flags;
    370  1.61      yamt 		nchstats.ncs_neghits++;
    371  1.61      yamt 		simple_unlock(&namecache_slock);
    372  1.61      yamt 		return (ENOENT);
    373  1.61      yamt 	}
    374  1.61      yamt 
    375  1.61      yamt 	error = vget(vp, LK_NOWAIT);
    376  1.61      yamt 
    377  1.61      yamt 	/* Release the name cache mutex while we get reference to the vnode */
    378  1.61      yamt 	simple_unlock(&namecache_slock);
    379  1.61      yamt 
    380  1.61      yamt 	if (error) {
    381  1.61      yamt 		KASSERT(error == EBUSY);
    382  1.61      yamt 		/*
    383  1.61      yamt 		 * this vnode is being cleaned out.
    384  1.61      yamt 		 */
    385  1.61      yamt 		nchstats.ncs_falsehits++; /* XXX badhits? */
    386  1.61      yamt 		goto fail;
    387  1.61      yamt 	}
    388  1.61      yamt 
    389  1.61      yamt 	*vpp = vp;
    390  1.61      yamt 
    391  1.61      yamt 	return 0;
    392  1.61      yamt 
    393  1.61      yamt fail_wlock:
    394  1.61      yamt 	simple_unlock(&namecache_slock);
    395  1.61      yamt fail:
    396  1.61      yamt 	*vpp = NULL;
    397  1.61      yamt 	return -1;
    398  1.61      yamt }
    399  1.61      yamt 
    400   1.1       cgd /*
    401  1.19  sommerfe  * Scan cache looking for name of directory entry pointing at vp.
    402  1.19  sommerfe  *
    403  1.19  sommerfe  * Fill in dvpp.
    404  1.19  sommerfe  *
    405  1.19  sommerfe  * If bufp is non-NULL, also place the name in the buffer which starts
    406  1.19  sommerfe  * at bufp, immediately before *bpp, and move bpp backwards to point
    407  1.19  sommerfe  * at the start of it.  (Yes, this is a little baroque, but it's done
    408  1.19  sommerfe  * this way to cater to the whims of getcwd).
    409  1.19  sommerfe  *
    410  1.19  sommerfe  * Returns 0 on success, -1 on cache miss, positive errno on failure.
    411  1.19  sommerfe  */
    412  1.19  sommerfe int
    413  1.34     enami cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
    414  1.19  sommerfe {
    415  1.19  sommerfe 	struct namecache *ncp;
    416  1.19  sommerfe 	struct vnode *dvp;
    417  1.19  sommerfe 	struct ncvhashhead *nvcpp;
    418  1.34     enami 	char *bp;
    419  1.34     enami 
    420  1.19  sommerfe 	if (!doingcache)
    421  1.19  sommerfe 		goto out;
    422  1.19  sommerfe 
    423  1.30       chs 	nvcpp = &ncvhashtbl[NCVHASH(vp)];
    424  1.19  sommerfe 
    425  1.39        pk 	simple_lock(&namecache_slock);
    426  1.27       chs 	LIST_FOREACH(ncp, nvcpp, nc_vhash) {
    427  1.34     enami 		if (ncp->nc_vp == vp &&
    428  1.34     enami 		    (dvp = ncp->nc_dvp) != NULL &&
    429  1.47      yamt 		    dvp != vp) { 		/* avoid pesky . entries.. */
    430  1.34     enami 
    431  1.19  sommerfe #ifdef DIAGNOSTIC
    432  1.34     enami 			if (ncp->nc_nlen == 1 &&
    433  1.34     enami 			    ncp->nc_name[0] == '.')
    434  1.19  sommerfe 				panic("cache_revlookup: found entry for .");
    435  1.19  sommerfe 
    436  1.34     enami 			if (ncp->nc_nlen == 2 &&
    437  1.34     enami 			    ncp->nc_name[0] == '.' &&
    438  1.34     enami 			    ncp->nc_name[1] == '.')
    439  1.19  sommerfe 				panic("cache_revlookup: found entry for ..");
    440  1.19  sommerfe #endif
    441  1.19  sommerfe 			nchstats.ncs_revhits++;
    442  1.19  sommerfe 
    443  1.19  sommerfe 			if (bufp) {
    444  1.19  sommerfe 				bp = *bpp;
    445  1.19  sommerfe 				bp -= ncp->nc_nlen;
    446  1.19  sommerfe 				if (bp <= bufp) {
    447  1.34     enami 					*dvpp = NULL;
    448  1.42      fvdl 					simple_unlock(&namecache_slock);
    449  1.34     enami 					return (ERANGE);
    450  1.19  sommerfe 				}
    451  1.19  sommerfe 				memcpy(bp, ncp->nc_name, ncp->nc_nlen);
    452  1.19  sommerfe 				*bpp = bp;
    453  1.19  sommerfe 			}
    454  1.34     enami 
    455  1.19  sommerfe 			/* XXX MP: how do we know dvp won't evaporate? */
    456  1.19  sommerfe 			*dvpp = dvp;
    457  1.39        pk 			simple_unlock(&namecache_slock);
    458  1.34     enami 			return (0);
    459  1.19  sommerfe 		}
    460  1.19  sommerfe 	}
    461  1.19  sommerfe 	nchstats.ncs_revmiss++;
    462  1.39        pk 	simple_unlock(&namecache_slock);
    463  1.19  sommerfe  out:
    464  1.34     enami 	*dvpp = NULL;
    465  1.34     enami 	return (-1);
    466  1.19  sommerfe }
    467  1.19  sommerfe 
    468  1.19  sommerfe /*
    469   1.1       cgd  * Add an entry to the cache
    470   1.1       cgd  */
    471  1.13  christos void
    472  1.34     enami cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
    473   1.1       cgd {
    474  1.23  augustss 	struct namecache *ncp;
    475  1.59      yamt 	struct namecache *oncp;
    476  1.23  augustss 	struct nchashhead *ncpp;
    477  1.23  augustss 	struct ncvhashhead *nvcpp;
    478   1.1       cgd 
    479   1.5   mycroft #ifdef DIAGNOSTIC
    480   1.5   mycroft 	if (cnp->cn_namelen > NCHNAMLEN)
    481   1.5   mycroft 		panic("cache_enter: name too long");
    482   1.5   mycroft #endif
    483   1.1       cgd 	if (!doingcache)
    484   1.1       cgd 		return;
    485   1.1       cgd 	/*
    486   1.1       cgd 	 * Free the cache slot at head of lru chain.
    487   1.1       cgd 	 */
    488  1.39        pk 	simple_lock(&namecache_slock);
    489  1.58      yamt 
    490  1.59      yamt 	if (numcache < numvnodes) {
    491  1.39        pk 		numcache++;
    492  1.39        pk 		simple_unlock(&namecache_slock);
    493  1.18   thorpej 		ncp = pool_get(&namecache_pool, PR_WAITOK);
    494  1.27       chs 		memset(ncp, 0, sizeof(*ncp));
    495  1.39        pk 		simple_lock(&namecache_slock);
    496  1.27       chs 	} else if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL) {
    497  1.46      yamt 		cache_remove(ncp);
    498  1.39        pk 	} else {
    499  1.39        pk 		simple_unlock(&namecache_slock);
    500   1.1       cgd 		return;
    501  1.39        pk 	}
    502  1.57        pk 
    503  1.59      yamt 	/*
    504  1.59      yamt 	 * Concurrent lookups in the same directory may race for a
    505  1.59      yamt 	 * cache entry.  if there's a duplicated entry, free it.
    506  1.59      yamt 	 */
    507  1.59      yamt 	oncp = cache_lookup_entry(dvp, cnp);
    508  1.59      yamt 	if (oncp) {
    509  1.59      yamt 		cache_remove(oncp);
    510  1.59      yamt 		cache_free(oncp);
    511  1.59      yamt 	}
    512  1.59      yamt 	KASSERT(cache_lookup_entry(dvp, cnp) == NULL);
    513  1.59      yamt 
    514  1.34     enami 	/* Grab the vnode we just found. */
    515   1.5   mycroft 	ncp->nc_vp = vp;
    516  1.47      yamt 	if (vp == NULL) {
    517  1.11   mycroft 		/*
    518  1.11   mycroft 		 * For negative hits, save the ISWHITEOUT flag so we can
    519  1.11   mycroft 		 * restore it later when the cache entry is used again.
    520  1.11   mycroft 		 */
    521  1.50      yamt 		ncp->nc_flags = cnp->cn_flags & ISWHITEOUT;
    522  1.11   mycroft 	}
    523  1.34     enami 	/* Fill in cache info. */
    524   1.5   mycroft 	ncp->nc_dvp = dvp;
    525  1.46      yamt 	LIST_INSERT_HEAD(&dvp->v_dnclist, ncp, nc_dvlist);
    526  1.46      yamt 	if (vp)
    527  1.46      yamt 		LIST_INSERT_HEAD(&vp->v_nclist, ncp, nc_vlist);
    528   1.5   mycroft 	ncp->nc_nlen = cnp->cn_namelen;
    529  1.17     perry 	memcpy(ncp->nc_name, cnp->cn_nameptr, (unsigned)ncp->nc_nlen);
    530   1.9   mycroft 	TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
    531  1.30       chs 	ncpp = &nchashtbl[NCHASH(cnp, dvp)];
    532   1.9   mycroft 	LIST_INSERT_HEAD(ncpp, ncp, nc_hash);
    533  1.19  sommerfe 
    534  1.34     enami 	ncp->nc_vhash.le_prev = NULL;
    535  1.34     enami 	ncp->nc_vhash.le_next = NULL;
    536  1.34     enami 
    537  1.19  sommerfe 	/*
    538  1.19  sommerfe 	 * Create reverse-cache entries (used in getcwd) for directories.
    539  1.66  christos 	 * (and in linux procfs exe node)
    540  1.19  sommerfe 	 */
    541  1.33     enami 	if (vp != NULL &&
    542  1.33     enami 	    vp != dvp &&
    543  1.29      fvdl #ifndef NAMECACHE_ENTER_REVERSE
    544  1.33     enami 	    vp->v_type == VDIR &&
    545  1.29      fvdl #endif
    546  1.33     enami 	    (ncp->nc_nlen > 2 ||
    547  1.33     enami 	    (ncp->nc_nlen > 1 && ncp->nc_name[1] != '.') ||
    548  1.33     enami 	    (/* ncp->nc_nlen > 0 && */ ncp->nc_name[0] != '.'))) {
    549  1.30       chs 		nvcpp = &ncvhashtbl[NCVHASH(vp)];
    550  1.19  sommerfe 		LIST_INSERT_HEAD(nvcpp, ncp, nc_vhash);
    551  1.19  sommerfe 	}
    552  1.39        pk 	simple_unlock(&namecache_slock);
    553   1.1       cgd }
    554   1.1       cgd 
    555   1.1       cgd /*
    556   1.1       cgd  * Name cache initialization, from vfs_init() when we are booting
    557   1.1       cgd  */
    558  1.13  christos void
    559  1.34     enami nchinit(void)
    560   1.1       cgd {
    561   1.1       cgd 
    562   1.9   mycroft 	TAILQ_INIT(&nclruhead);
    563  1.26        ad 	nchashtbl =
    564  1.26        ad 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &nchash);
    565  1.26        ad 	ncvhashtbl =
    566  1.29      fvdl #ifdef NAMECACHE_ENTER_REVERSE
    567  1.29      fvdl 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    568  1.29      fvdl #else
    569  1.26        ad 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &ncvhash);
    570  1.29      fvdl #endif
    571  1.30       chs }
    572  1.30       chs 
    573  1.30       chs /*
    574  1.30       chs  * Name cache reinitialization, for when the maximum number of vnodes increases.
    575  1.30       chs  */
    576  1.30       chs void
    577  1.34     enami nchreinit(void)
    578  1.30       chs {
    579  1.30       chs 	struct namecache *ncp;
    580  1.30       chs 	struct nchashhead *oldhash1, *hash1;
    581  1.30       chs 	struct ncvhashhead *oldhash2, *hash2;
    582  1.36   thorpej 	u_long i, oldmask1, oldmask2, mask1, mask2;
    583  1.30       chs 
    584  1.30       chs 	hash1 = hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask1);
    585  1.30       chs 	hash2 =
    586  1.30       chs #ifdef NAMECACHE_ENTER_REVERSE
    587  1.30       chs 	    hashinit(desiredvnodes, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    588  1.30       chs #else
    589  1.30       chs 	    hashinit(desiredvnodes/8, HASH_LIST, M_CACHE, M_WAITOK, &mask2);
    590  1.30       chs #endif
    591  1.40       jmc 	simple_lock(&namecache_slock);
    592  1.30       chs 	oldhash1 = nchashtbl;
    593  1.30       chs 	oldmask1 = nchash;
    594  1.30       chs 	nchashtbl = hash1;
    595  1.30       chs 	nchash = mask1;
    596  1.30       chs 	oldhash2 = ncvhashtbl;
    597  1.30       chs 	oldmask2 = ncvhash;
    598  1.30       chs 	ncvhashtbl = hash2;
    599  1.30       chs 	ncvhash = mask2;
    600  1.30       chs 	for (i = 0; i <= oldmask1; i++) {
    601  1.30       chs 		while ((ncp = LIST_FIRST(&oldhash1[i])) != NULL) {
    602  1.30       chs 			LIST_REMOVE(ncp, nc_hash);
    603  1.30       chs 			ncp->nc_hash.le_prev = NULL;
    604  1.30       chs 		}
    605  1.30       chs 	}
    606  1.30       chs 	for (i = 0; i <= oldmask2; i++) {
    607  1.30       chs 		while ((ncp = LIST_FIRST(&oldhash2[i])) != NULL) {
    608  1.30       chs 			LIST_REMOVE(ncp, nc_vhash);
    609  1.30       chs 			ncp->nc_vhash.le_prev = NULL;
    610  1.30       chs 		}
    611  1.30       chs 	}
    612  1.41     enami 	simple_unlock(&namecache_slock);
    613  1.30       chs 	hashdone(oldhash1, M_CACHE);
    614  1.30       chs 	hashdone(oldhash2, M_CACHE);
    615   1.1       cgd }
    616   1.1       cgd 
    617   1.1       cgd /*
    618   1.1       cgd  * Cache flush, a particular vnode; called when a vnode is renamed to
    619   1.1       cgd  * hide entries that would now be invalid
    620   1.1       cgd  */
    621  1.13  christos void
    622  1.55      yamt cache_purge1(struct vnode *vp, const struct componentname *cnp, int flags)
    623   1.1       cgd {
    624  1.46      yamt 	struct namecache *ncp, *ncnext;
    625   1.1       cgd 
    626  1.39        pk 	simple_lock(&namecache_slock);
    627  1.55      yamt 	if (flags & PURGE_PARENTS) {
    628  1.55      yamt 		for (ncp = LIST_FIRST(&vp->v_nclist); ncp != NULL;
    629  1.55      yamt 		    ncp = ncnext) {
    630  1.55      yamt 			ncnext = LIST_NEXT(ncp, nc_vlist);
    631  1.55      yamt 			cache_remove(ncp);
    632  1.55      yamt 			cache_free(ncp);
    633  1.55      yamt 		}
    634  1.55      yamt 	}
    635  1.55      yamt 	if (flags & PURGE_CHILDREN) {
    636  1.55      yamt 		for (ncp = LIST_FIRST(&vp->v_dnclist); ncp != NULL;
    637  1.55      yamt 		    ncp = ncnext) {
    638  1.55      yamt 			ncnext = LIST_NEXT(ncp, nc_dvlist);
    639  1.55      yamt 			cache_remove(ncp);
    640  1.55      yamt 			cache_free(ncp);
    641  1.55      yamt 		}
    642  1.46      yamt 	}
    643  1.55      yamt 	if (cnp != NULL) {
    644  1.55      yamt 		ncp = cache_lookup_entry(vp, cnp);
    645  1.55      yamt 		if (ncp) {
    646  1.55      yamt 			cache_remove(ncp);
    647  1.55      yamt 			cache_free(ncp);
    648  1.55      yamt 		}
    649  1.46      yamt 	}
    650  1.39        pk 	simple_unlock(&namecache_slock);
    651   1.1       cgd }
    652   1.1       cgd 
    653   1.1       cgd /*
    654   1.1       cgd  * Cache flush, a whole filesystem; called when filesys is umounted to
    655  1.27       chs  * remove entries that would now be invalid.
    656   1.1       cgd  */
    657  1.13  christos void
    658  1.34     enami cache_purgevfs(struct mount *mp)
    659   1.1       cgd {
    660  1.23  augustss 	struct namecache *ncp, *nxtcp;
    661   1.1       cgd 
    662  1.39        pk 	simple_lock(&namecache_slock);
    663  1.27       chs 	for (ncp = TAILQ_FIRST(&nclruhead); ncp != NULL; ncp = nxtcp) {
    664  1.27       chs 		nxtcp = TAILQ_NEXT(ncp, nc_lru);
    665   1.5   mycroft 		if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp) {
    666   1.1       cgd 			continue;
    667   1.5   mycroft 		}
    668  1.34     enami 		/* Free the resources we had. */
    669  1.46      yamt 		cache_remove(ncp);
    670  1.46      yamt 		cache_free(ncp);
    671   1.1       cgd 	}
    672  1.39        pk 	simple_unlock(&namecache_slock);
    673   1.1       cgd }
    674  1.19  sommerfe 
    675  1.28       chs #ifdef DDB
    676  1.28       chs void
    677  1.28       chs namecache_print(struct vnode *vp, void (*pr)(const char *, ...))
    678  1.28       chs {
    679  1.28       chs 	struct vnode *dvp = NULL;
    680  1.28       chs 	struct namecache *ncp;
    681  1.28       chs 
    682  1.28       chs 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    683  1.47      yamt 		if (ncp->nc_vp == vp) {
    684  1.28       chs 			(*pr)("name %.*s\n", ncp->nc_nlen, ncp->nc_name);
    685  1.28       chs 			dvp = ncp->nc_dvp;
    686  1.28       chs 		}
    687  1.28       chs 	}
    688  1.28       chs 	if (dvp == NULL) {
    689  1.28       chs 		(*pr)("name not found\n");
    690  1.28       chs 		return;
    691  1.28       chs 	}
    692  1.28       chs 	vp = dvp;
    693  1.28       chs 	TAILQ_FOREACH(ncp, &nclruhead, nc_lru) {
    694  1.47      yamt 		if (ncp->nc_vp == vp) {
    695  1.28       chs 			(*pr)("parent %.*s\n", ncp->nc_nlen, ncp->nc_name);
    696  1.28       chs 		}
    697  1.28       chs 	}
    698  1.28       chs }
    699  1.28       chs #endif
    700